1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2017 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
20 #include "config.h"
21 #include "ncmpc.h"
22 #include "mpdclient.h"
23 #include "callbacks.h"
24 #include "charset.h"
25 #include "options.h"
26 #include "command.h"
27 #include "ncu.h"
28 #include "screen.h"
29 #include "screen_status.h"
30 #include "xterm_title.h"
31 #include "strfsong.h"
32 #include "i18n.h"
33 #include "player_command.h"
34 #include "keyboard.h"
35 #include "lirc.h"
36 #include "signals.h"
38 #ifndef NCMPC_MINI
39 #include "conf.h"
40 #endif
42 #ifdef ENABLE_LYRICS_SCREEN
43 #include "lyrics.h"
44 #endif
46 #include <mpd/client.h>
48 #include <stdlib.h>
49 #include <unistd.h>
50 #include <fcntl.h>
51 #include <signal.h>
52 #include <string.h>
54 #ifdef ENABLE_LOCALE
55 #include <locale.h>
56 #endif
58 /* time between mpd updates [ms] */
59 static const guint update_interval = 500;
61 #define BUFSIZE 1024
63 static struct mpdclient *mpd = NULL;
64 static GMainLoop *main_loop;
65 static guint reconnect_source_id, update_source_id;
67 #ifndef NCMPC_MINI
68 static guint check_key_bindings_source_id;
69 #endif
71 #ifndef NCMPC_MINI
72 static void
73 update_xterm_title(void)
74 {
75 const struct mpd_song *song = mpd->song;
77 char tmp[BUFSIZE];
78 if (options.xterm_title_format && mpd->playing && song)
79 strfsong(tmp, BUFSIZE, options.xterm_title_format, song);
80 else
81 g_strlcpy(tmp, PACKAGE " version " VERSION, BUFSIZE);
83 static char title[BUFSIZE];
84 if (strncmp(title, tmp, BUFSIZE)) {
85 g_strlcpy(title, tmp, BUFSIZE);
86 set_xterm_title("%s", title);
87 }
88 }
89 #endif
91 static gboolean
92 timer_mpd_update(gpointer data);
94 static void
95 enable_update_timer(void)
96 {
97 if (update_source_id != 0)
98 return;
100 update_source_id = g_timeout_add(update_interval,
101 timer_mpd_update, NULL);
102 }
104 static void
105 disable_update_timer(void)
106 {
107 if (update_source_id == 0)
108 return;
110 g_source_remove(update_source_id);
111 update_source_id = 0;
112 }
114 static bool
115 should_enable_update_timer(void)
116 {
117 return mpd->playing;
118 }
120 static void
121 auto_update_timer(void)
122 {
123 if (should_enable_update_timer())
124 enable_update_timer();
125 else
126 disable_update_timer();
127 }
129 static void
130 do_mpd_update(void)
131 {
132 if (mpdclient_is_connected(mpd) &&
133 (mpd->events != 0 || mpd->playing))
134 mpdclient_update(mpd);
136 #ifndef NCMPC_MINI
137 if (options.enable_xterm_title)
138 update_xterm_title();
139 #endif
141 screen_update(mpd);
142 mpd->events = 0;
143 }
145 static char *
146 settings_name(const struct mpd_settings *settings)
147 {
148 const char *host = mpd_settings_get_host(settings);
149 if (host == NULL)
150 host = _("unknown");
152 if (host[0] == '/')
153 return g_strdup(host);
155 unsigned port = mpd_settings_get_port(settings);
156 if (port == 0 || port == 6600)
157 return g_strdup(host);
159 return g_strdup_printf("%s:%u", host, port);
160 }
162 static char *
163 default_settings_name(void)
164 {
165 struct mpd_settings *settings =
166 mpd_settings_new(options.host, options.port, 0,
167 NULL, options.password);
168 if (settings == NULL)
169 return g_strdup(_("unknown"));
171 char *name = settings_name(settings);
172 mpd_settings_free(settings);
174 return name;
175 }
177 /**
178 * This timer is installed when the connection to the MPD server is
179 * broken. It tries to recover by reconnecting periodically.
180 */
181 static gboolean
182 timer_reconnect(gcc_unused gpointer data)
183 {
184 assert(mpdclient_is_dead(mpd));
186 reconnect_source_id = 0;
188 char *name = default_settings_name();
189 screen_status_printf(_("Connecting to %s... [Press %s to abort]"),
190 name, get_key_names(CMD_QUIT, false));
191 g_free(name);
192 doupdate();
194 mpdclient_connect(mpd);
196 return FALSE;
197 }
199 void
200 mpdclient_connected_callback(void)
201 {
202 assert(reconnect_source_id == 0);
204 #ifndef NCMPC_MINI
205 /* quit if mpd is pre 0.14 - song id not supported by mpd */
206 struct mpd_connection *connection = mpdclient_get_connection(mpd);
207 if (mpd_connection_cmp_server_version(connection, 0, 16, 0) < 0) {
208 const unsigned *version =
209 mpd_connection_get_server_version(connection);
210 screen_status_printf(_("Error: MPD version %d.%d.%d is too old (%s needed)"),
211 version[0], version[1], version[2],
212 "0.16.0");
213 mpdclient_disconnect(mpd);
214 doupdate();
216 /* try again after 30 seconds */
217 reconnect_source_id = g_timeout_add(30000,
218 timer_reconnect, NULL);
219 return;
220 }
221 #endif
223 screen_status_clear_message();
224 doupdate();
226 /* update immediately */
227 mpd->events = MPD_IDLE_ALL;
229 do_mpd_update();
231 auto_update_timer();
232 }
234 void
235 mpdclient_failed_callback(void)
236 {
237 assert(reconnect_source_id == 0);
239 /* try again in 5 seconds */
240 reconnect_source_id = g_timeout_add(5000,
241 timer_reconnect, NULL);
242 }
244 void
245 mpdclient_lost_callback(void)
246 {
247 assert(reconnect_source_id == 0);
249 screen_update(mpd);
251 reconnect_source_id = g_timeout_add(1000, timer_reconnect, NULL);
252 }
254 /**
255 * This function is called by the gidle.c library when MPD sends us an
256 * idle event (or when the connection dies).
257 */
258 void
259 mpdclient_idle_callback(gcc_unused enum mpd_idle events)
260 {
261 #ifndef NCMPC_MINI
262 if (options.enable_xterm_title)
263 update_xterm_title();
264 #endif
266 screen_update(mpd);
267 auto_update_timer();
268 }
270 static gboolean
271 timer_mpd_update(gcc_unused gpointer data)
272 {
273 do_mpd_update();
275 if (should_enable_update_timer())
276 return true;
277 else {
278 update_source_id = 0;
279 return false;
280 }
281 }
283 void begin_input_event(void)
284 {
285 }
287 void end_input_event(void)
288 {
289 screen_update(mpd);
290 mpd->events = 0;
292 auto_update_timer();
293 }
295 bool
296 do_input_event(command_t cmd)
297 {
298 if (cmd == CMD_QUIT) {
299 g_main_loop_quit(main_loop);
300 return false;
301 }
303 screen_cmd(mpd, cmd);
305 if (cmd == CMD_VOLUME_UP || cmd == CMD_VOLUME_DOWN)
306 /* make sure we don't update the volume yet */
307 disable_update_timer();
309 return true;
310 }
312 #ifndef NCMPC_MINI
313 /**
314 * Check the configured key bindings for errors, and display a status
315 * message every 10 seconds.
316 */
317 static gboolean
318 timer_check_key_bindings(gcc_unused gpointer data)
319 {
320 char buf[256];
322 if (check_key_bindings(NULL, buf, sizeof(buf))) {
323 /* no error: disable this timer for the rest of this
324 process */
325 check_key_bindings_source_id = 0;
326 return FALSE;
327 }
329 #ifdef ENABLE_KEYDEF_SCREEN
330 g_strchomp(buf);
331 g_strlcat(buf, " (", sizeof(buf));
332 /* to translators: a key was bound twice in the key editor,
333 and this is a hint for the user what to press to correct
334 that */
335 char comment[64];
336 g_snprintf(comment, sizeof(comment), _("press %s for the key editor"),
337 get_key_names(CMD_SCREEN_KEYDEF, false));
338 g_strlcat(buf, comment, sizeof(buf));
339 g_strlcat(buf, ")", sizeof(buf));
340 #endif
342 screen_status_printf("%s", buf);
344 doupdate();
345 return TRUE;
346 }
347 #endif
349 int
350 main(int argc, const char *argv[])
351 {
352 #ifdef ENABLE_LOCALE
353 #ifndef ENABLE_NLS
354 gcc_unused
355 #endif
356 const char *charset = NULL;
357 /* time and date formatting */
358 setlocale(LC_TIME,"");
359 /* care about sorting order etc */
360 setlocale(LC_COLLATE,"");
361 /* charset */
362 setlocale(LC_CTYPE,"");
363 /* initialize charset conversions */
364 charset = charset_init();
366 /* initialize i18n support */
367 #endif
369 #ifdef ENABLE_NLS
370 setlocale(LC_MESSAGES, "");
371 bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
372 #ifdef ENABLE_LOCALE
373 bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
374 #endif
375 textdomain(GETTEXT_PACKAGE);
376 #endif
378 /* initialize options */
379 options_init();
381 /* parse command line options - 1 pass get configuration files */
382 options_parse(argc, argv);
384 #ifndef NCMPC_MINI
385 /* read configuration */
386 read_configuration();
388 /* check key bindings */
389 check_key_bindings(NULL, NULL, 0);
390 #endif
392 /* parse command line options - 2 pass */
393 options_parse(argc, argv);
395 ncu_init();
397 #ifdef ENABLE_LYRICS_SCREEN
398 lyrics_init();
399 #endif
401 /* create mpdclient instance */
402 mpd = mpdclient_new(options.host, options.port,
403 options.timeout_ms,
404 options.password);
406 /* initialize curses */
407 screen_init(mpd);
409 /* the main loop */
410 main_loop = g_main_loop_new(NULL, FALSE);
412 /* watch out for keyboard input */
413 keyboard_init();
415 /* watch out for lirc input */
416 ncmpc_lirc_init();
418 signals_init(main_loop, mpd);
420 /* attempt to connect */
421 reconnect_source_id = g_idle_add(timer_reconnect, NULL);
423 auto_update_timer();
425 #ifndef NCMPC_MINI
426 check_key_bindings_source_id = g_timeout_add(10000, timer_check_key_bindings, NULL);
427 #endif
429 screen_paint(mpd);
431 g_main_loop_run(main_loop);
432 g_main_loop_unref(main_loop);
434 /* cleanup */
436 cancel_seek_timer();
438 disable_update_timer();
440 if (reconnect_source_id != 0)
441 g_source_remove(reconnect_source_id);
443 #ifndef NCMPC_MINI
444 if (check_key_bindings_source_id != 0)
445 g_source_remove(check_key_bindings_source_id);
446 #endif
448 signals_deinit();
449 ncmpc_lirc_deinit();
451 screen_exit();
452 #ifndef NCMPC_MINI
453 set_xterm_title("");
454 #endif
455 printf("\n");
457 mpdclient_free(mpd);
459 #ifdef ENABLE_LYRICS_SCREEN
460 lyrics_deinit();
461 #endif
463 ncu_deinit();
464 options_deinit();
466 return 0;
467 }