ec051f4a8c83de04a54b1e9983910a3847614662
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_utils.h"
30 #include "screen_status.h"
31 #include "strfsong.h"
32 #include "i18n.h"
33 #include "player_command.h"
35 #ifndef NCMPC_MINI
36 #include "conf.h"
37 #endif
39 #ifdef ENABLE_LYRICS_SCREEN
40 #include "lyrics.h"
41 #endif
43 #ifdef ENABLE_LIRC
44 #include "lirc.h"
45 #endif
47 #include <mpd/client.h>
49 #include <stdlib.h>
50 #include <unistd.h>
51 #include <fcntl.h>
52 #include <signal.h>
53 #include <string.h>
55 #ifdef ENABLE_LOCALE
56 #include <locale.h>
57 #endif
59 /* time between mpd updates [ms] */
60 static const guint update_interval = 500;
62 #define BUFSIZE 1024
64 static struct mpdclient *mpd = NULL;
65 static GMainLoop *main_loop;
66 static guint reconnect_source_id, update_source_id;
67 static int sigwinch_pipes[2];
69 #ifndef NCMPC_MINI
70 static guint check_key_bindings_source_id;
71 #endif
73 #ifndef NCMPC_MINI
74 static void
75 update_xterm_title(void)
76 {
77 const struct mpd_song *song = mpd->song;
79 char tmp[BUFSIZE];
80 if (options.xterm_title_format && mpd->playing && song)
81 strfsong(tmp, BUFSIZE, options.xterm_title_format, song);
82 else
83 g_strlcpy(tmp, PACKAGE " version " VERSION, BUFSIZE);
85 static char title[BUFSIZE];
86 if (strncmp(title, tmp, BUFSIZE)) {
87 g_strlcpy(title, tmp, BUFSIZE);
88 set_xterm_title("%s", title);
89 }
90 }
91 #endif
93 static void
94 exit_and_cleanup(void)
95 {
96 screen_exit();
97 #ifndef NCMPC_MINI
98 set_xterm_title("");
99 #endif
100 printf("\n");
102 mpdclient_free(mpd);
103 }
105 #ifndef WIN32
106 static void
107 catch_sigint(gcc_unused int sig)
108 {
109 g_main_loop_quit(main_loop);
110 }
113 static void
114 catch_sigcont(gcc_unused int sig)
115 {
116 char irrelevant = 'a';
117 if (1 != write(sigwinch_pipes[1], &irrelevant, 1))
118 exit(EXIT_FAILURE);
119 }
121 void
122 sigstop(void)
123 {
124 def_prog_mode(); /* save the tty modes */
125 endwin(); /* end curses mode temporarily */
126 kill(0, SIGSTOP); /* issue SIGSTOP */
127 }
129 static gboolean
130 sigwinch_event(gcc_unused GIOChannel *source,
131 gcc_unused GIOCondition condition, gcc_unused gpointer data)
132 {
133 char ignoreme[64];
134 if (1 > read(sigwinch_pipes[0], ignoreme, 64))
135 exit(EXIT_FAILURE);
137 endwin();
138 refresh();
139 screen_resize(mpd);
141 return TRUE;
142 }
144 static void
145 catch_sigwinch(gcc_unused int sig)
146 {
147 if (1 != write(sigwinch_pipes[1], "", 1))
148 exit(EXIT_FAILURE);
149 }
150 #endif /* WIN32 */
152 static gboolean
153 timer_mpd_update(gpointer data);
155 static void
156 enable_update_timer(void)
157 {
158 if (update_source_id != 0)
159 return;
161 update_source_id = g_timeout_add(update_interval,
162 timer_mpd_update, NULL);
163 }
165 static void
166 disable_update_timer(void)
167 {
168 if (update_source_id == 0)
169 return;
171 g_source_remove(update_source_id);
172 update_source_id = 0;
173 }
175 static bool
176 should_enable_update_timer(void)
177 {
178 return mpd->playing
179 #ifndef NCMPC_MINI
180 || options.display_time
181 #endif
182 ;
183 }
185 static void
186 auto_update_timer(void)
187 {
188 if (should_enable_update_timer())
189 enable_update_timer();
190 else
191 disable_update_timer();
192 }
194 static void
195 check_reconnect(void);
197 static void
198 do_mpd_update(void)
199 {
200 if (mpdclient_is_connected(mpd) &&
201 (mpd->events != 0 || mpd->playing))
202 mpdclient_update(mpd);
204 #ifndef NCMPC_MINI
205 if (options.enable_xterm_title)
206 update_xterm_title();
207 #endif
209 screen_update(mpd);
210 mpd->events = 0;
212 mpdclient_put_connection(mpd);
213 check_reconnect();
214 }
216 static char *
217 settings_name(const struct mpd_settings *settings)
218 {
219 const char *host = mpd_settings_get_host(settings);
220 if (host == NULL)
221 host = _("unknown");
223 if (host[0] == '/')
224 return g_strdup(host);
226 unsigned port = mpd_settings_get_port(settings);
227 if (port == 0 || port == 6600)
228 return g_strdup(host);
230 return g_strdup_printf("%s:%u", host, port);
231 }
233 static char *
234 default_settings_name(void)
235 {
236 struct mpd_settings *settings =
237 mpd_settings_new(options.host, options.port, 0,
238 NULL, options.password);
239 if (settings == NULL)
240 return g_strdup(_("unknown"));
242 char *name = settings_name(settings);
243 mpd_settings_free(settings);
245 return name;
246 }
248 /**
249 * This timer is installed when the connection to the MPD server is
250 * broken. It tries to recover by reconnecting periodically.
251 */
252 static gboolean
253 timer_reconnect(gcc_unused gpointer data)
254 {
255 assert(mpdclient_is_dead(mpd));
257 reconnect_source_id = 0;
259 char *name = default_settings_name();
260 screen_status_printf(_("Connecting to %s... [Press %s to abort]"),
261 name, get_key_names(CMD_QUIT, false));
262 g_free(name);
263 doupdate();
265 mpdclient_disconnect(mpd);
266 if (!mpdclient_connect(mpd, options.host, options.port,
267 options.timeout_ms,
268 options.password)) {
269 /* try again in 5 seconds */
270 reconnect_source_id = g_timeout_add(5000,
271 timer_reconnect, NULL);
272 return FALSE;
273 }
275 #ifndef NCMPC_MINI
276 /* quit if mpd is pre 0.14 - song id not supported by mpd */
277 struct mpd_connection *connection = mpdclient_get_connection(mpd);
278 if (mpd_connection_cmp_server_version(connection, 0, 16, 0) < 0) {
279 const unsigned *version =
280 mpd_connection_get_server_version(connection);
281 screen_status_printf(_("Error: MPD version %d.%d.%d is too old (%s needed)"),
282 version[0], version[1], version[2],
283 "0.16.0");
284 mpdclient_disconnect(mpd);
285 doupdate();
287 /* try again after 30 seconds */
288 reconnect_source_id = g_timeout_add(30000,
289 timer_reconnect, NULL);
290 return FALSE;
291 }
292 #endif
294 screen_status_clear_message();
295 doupdate();
297 /* update immediately */
298 mpd->events = MPD_IDLE_ALL;
300 do_mpd_update();
302 auto_update_timer();
304 return FALSE;
305 }
307 static void
308 check_reconnect(void)
309 {
310 if (mpdclient_is_dead(mpd) && reconnect_source_id == 0)
311 /* reconnect when the connection is lost */
312 reconnect_source_id = g_timeout_add(1000, timer_reconnect,
313 NULL);
314 }
316 void
317 mpdclient_lost_callback(void)
318 {
319 assert(reconnect_source_id == 0);
321 screen_update(mpd);
323 reconnect_source_id = g_timeout_add(1000, timer_reconnect, NULL);
324 }
326 /**
327 * This function is called by the gidle.c library when MPD sends us an
328 * idle event (or when the connection dies).
329 */
330 void
331 mpdclient_idle_callback(gcc_unused enum mpd_idle events)
332 {
333 #ifndef NCMPC_MINI
334 if (options.enable_xterm_title)
335 update_xterm_title();
336 #endif
338 screen_update(mpd);
339 auto_update_timer();
340 }
342 static gboolean
343 timer_mpd_update(gcc_unused gpointer data)
344 {
345 do_mpd_update();
347 if (should_enable_update_timer())
348 return true;
349 else {
350 update_source_id = 0;
351 return false;
352 }
353 }
355 void begin_input_event(void)
356 {
357 }
359 void end_input_event(void)
360 {
361 screen_update(mpd);
362 mpd->events = 0;
364 mpdclient_put_connection(mpd);
365 check_reconnect();
366 auto_update_timer();
367 }
369 int do_input_event(command_t cmd)
370 {
371 if (cmd == CMD_QUIT) {
372 g_main_loop_quit(main_loop);
373 return -1;
374 }
376 screen_cmd(mpd, cmd);
378 if (cmd == CMD_VOLUME_UP || cmd == CMD_VOLUME_DOWN)
379 /* make sure we don't update the volume yet */
380 disable_update_timer();
382 return 0;
383 }
385 static gboolean
386 keyboard_event(gcc_unused GIOChannel *source,
387 gcc_unused GIOCondition condition,
388 gcc_unused gpointer data)
389 {
390 begin_input_event();
392 command_t cmd = get_keyboard_command();
393 if (cmd != CMD_NONE)
394 if (do_input_event(cmd) != 0)
395 return FALSE;
397 end_input_event();
398 return TRUE;
399 }
401 #ifndef NCMPC_MINI
402 /**
403 * Check the configured key bindings for errors, and display a status
404 * message every 10 seconds.
405 */
406 static gboolean
407 timer_check_key_bindings(gcc_unused gpointer data)
408 {
409 char buf[256];
411 if (check_key_bindings(NULL, buf, sizeof(buf))) {
412 /* no error: disable this timer for the rest of this
413 process */
414 check_key_bindings_source_id = 0;
415 return FALSE;
416 }
418 #ifdef ENABLE_KEYDEF_SCREEN
419 g_strchomp(buf);
420 g_strlcat(buf, " (", sizeof(buf));
421 /* to translators: a key was bound twice in the key editor,
422 and this is a hint for the user what to press to correct
423 that */
424 char comment[64];
425 g_snprintf(comment, sizeof(comment), _("press %s for the key editor"),
426 get_key_names(CMD_SCREEN_KEYDEF, false));
427 g_strlcat(buf, comment, sizeof(buf));
428 g_strlcat(buf, ")", sizeof(buf));
429 #endif
431 screen_status_printf("%s", buf);
433 doupdate();
434 return TRUE;
435 }
436 #endif
438 int
439 main(int argc, const char *argv[])
440 {
441 #ifdef ENABLE_LOCALE
442 #ifndef ENABLE_NLS
443 gcc_unused
444 #endif
445 const char *charset = NULL;
446 /* time and date formatting */
447 setlocale(LC_TIME,"");
448 /* care about sorting order etc */
449 setlocale(LC_COLLATE,"");
450 /* charset */
451 setlocale(LC_CTYPE,"");
452 /* initialize charset conversions */
453 charset = charset_init();
455 /* initialize i18n support */
456 #endif
458 #ifdef ENABLE_NLS
459 setlocale(LC_MESSAGES, "");
460 bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
461 #ifdef ENABLE_LOCALE
462 bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
463 #endif
464 textdomain(GETTEXT_PACKAGE);
465 #endif
467 /* initialize options */
468 options_init();
470 /* parse command line options - 1 pass get configuration files */
471 options_parse(argc, argv);
473 #ifndef NCMPC_MINI
474 /* read configuration */
475 read_configuration();
477 /* check key bindings */
478 check_key_bindings(NULL, NULL, 0);
479 #endif
481 /* parse command line options - 2 pass */
482 options_parse(argc, argv);
484 #ifndef WIN32
485 /* setup signal behavior - SIGINT */
486 struct sigaction act;
487 sigemptyset(&act.sa_mask);
488 act.sa_flags = 0;
489 act.sa_handler = catch_sigint;
490 if (sigaction(SIGINT, &act, NULL) < 0) {
491 perror("signal");
492 exit(EXIT_FAILURE);
493 }
495 /* setup signal behavior - SIGTERM */
497 act.sa_handler = catch_sigint;
498 if (sigaction(SIGTERM, &act, NULL) < 0) {
499 perror("sigaction()");
500 exit(EXIT_FAILURE);
501 }
503 /* setup signal behavior - SIGCONT */
505 act.sa_handler = catch_sigcont;
506 if (sigaction(SIGCONT, &act, NULL) < 0) {
507 perror("sigaction(SIGCONT)");
508 exit(EXIT_FAILURE);
509 }
511 /* setup signal behaviour - SIGHUP*/
513 act.sa_handler = catch_sigint;
514 if (sigaction(SIGHUP, &act, NULL) < 0) {
515 perror("sigaction(SIGHUP)");
516 exit(EXIT_FAILURE);
517 }
519 /* setup SIGWINCH */
521 act.sa_flags = SA_RESTART;
522 act.sa_handler = catch_sigwinch;
523 if (sigaction(SIGWINCH, &act, NULL) < 0) {
524 perror("sigaction(SIGWINCH)");
525 exit(EXIT_FAILURE);
526 }
528 /* ignore SIGPIPE */
530 act.sa_handler = SIG_IGN;
531 if (sigaction(SIGPIPE, &act, NULL) < 0) {
532 perror("sigaction(SIGPIPE)");
533 exit(EXIT_FAILURE);
534 }
535 #endif
537 ncu_init();
539 #ifdef ENABLE_LYRICS_SCREEN
540 lyrics_init();
541 #endif
543 /* create mpdclient instance */
544 mpd = mpdclient_new();
546 /* initialize curses */
547 screen_init(mpd);
549 /* the main loop */
550 main_loop = g_main_loop_new(NULL, FALSE);
552 /* watch out for keyboard input */
553 GIOChannel *keyboard_channel = g_io_channel_unix_new(STDIN_FILENO);
554 g_io_add_watch(keyboard_channel, G_IO_IN, keyboard_event, NULL);
556 #ifdef ENABLE_LIRC
557 /* watch out for lirc input */
558 int lirc_socket = ncmpc_lirc_open();
559 GIOChannel *lirc_channel = NULL;
560 if (lirc_socket >= 0) {
561 lirc_channel = g_io_channel_unix_new(lirc_socket);
562 g_io_add_watch(lirc_channel, G_IO_IN, lirc_event, NULL);
563 }
564 #endif
566 #ifndef WIN32
567 GIOChannel *sigwinch_channel = NULL;
568 if (!pipe(sigwinch_pipes) &&
569 !fcntl(sigwinch_pipes[1], F_SETFL, O_NONBLOCK)) {
570 sigwinch_channel = g_io_channel_unix_new(sigwinch_pipes[0]);
571 g_io_add_watch(sigwinch_channel, G_IO_IN, sigwinch_event, NULL);
572 }
573 else {
574 perror("sigwinch pipe creation failed");
575 exit(EXIT_FAILURE);
576 }
577 #endif
579 /* attempt to connect */
580 reconnect_source_id = g_timeout_add(1, timer_reconnect, NULL);
582 auto_update_timer();
584 #ifndef NCMPC_MINI
585 check_key_bindings_source_id = g_timeout_add(10000, timer_check_key_bindings, NULL);
586 #endif
588 screen_paint(mpd);
590 g_main_loop_run(main_loop);
592 /* cleanup */
594 cancel_seek_timer();
596 disable_update_timer();
598 if (reconnect_source_id != 0)
599 g_source_remove(reconnect_source_id);
601 #ifndef NCMPC_MINI
602 if (check_key_bindings_source_id != 0)
603 g_source_remove(check_key_bindings_source_id);
604 #endif
606 g_main_loop_unref(main_loop);
607 g_io_channel_unref(keyboard_channel);
608 g_io_channel_unref(sigwinch_channel);
609 close(sigwinch_pipes[0]);
610 close(sigwinch_pipes[1]);
612 #ifdef ENABLE_LIRC
613 if (lirc_socket >= 0)
614 g_io_channel_unref(lirc_channel);
615 ncmpc_lirc_close();
616 #endif
618 exit_and_cleanup();
620 #ifdef ENABLE_LYRICS_SCREEN
621 lyrics_deinit();
622 #endif
624 ncu_deinit();
625 options_deinit();
627 return 0;
628 }