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 "gidle.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 struct mpd_status *status = NULL;
78 const struct mpd_song *song = NULL;
79 if (mpd) {
80 status = mpd->status;
81 song = mpd->song;
82 }
84 char tmp[BUFSIZE];
85 if (options.xterm_title_format && status && song &&
86 mpd_status_get_state(status) == MPD_STATE_PLAY)
87 strfsong(tmp, BUFSIZE, options.xterm_title_format, song);
88 else
89 g_strlcpy(tmp, PACKAGE " version " VERSION, BUFSIZE);
91 static char title[BUFSIZE];
92 if (strncmp(title, tmp, BUFSIZE)) {
93 g_strlcpy(title, tmp, BUFSIZE);
94 set_xterm_title("%s", title);
95 }
96 }
97 #endif
99 static void
100 exit_and_cleanup(void)
101 {
102 screen_exit();
103 #ifndef NCMPC_MINI
104 set_xterm_title("");
105 #endif
106 printf("\n");
108 if (mpd) {
109 mpdclient_disconnect(mpd);
110 mpdclient_free(mpd);
111 }
112 }
114 #ifndef WIN32
115 static void
116 catch_sigint(gcc_unused int sig)
117 {
118 g_main_loop_quit(main_loop);
119 }
122 static void
123 catch_sigcont(gcc_unused int sig)
124 {
125 char irrelevant = 'a';
126 if (1 != write(sigwinch_pipes[1], &irrelevant, 1))
127 exit(EXIT_FAILURE);
128 }
130 void
131 sigstop(void)
132 {
133 def_prog_mode(); /* save the tty modes */
134 endwin(); /* end curses mode temporarily */
135 kill(0, SIGSTOP); /* issue SIGSTOP */
136 }
138 static gboolean
139 sigwinch_event(gcc_unused GIOChannel *source,
140 gcc_unused GIOCondition condition, gcc_unused gpointer data)
141 {
142 char ignoreme[64];
143 if (1 > read(sigwinch_pipes[0], ignoreme, 64))
144 exit(EXIT_FAILURE);
146 endwin();
147 refresh();
148 screen_resize(mpd);
150 return TRUE;
151 }
153 static void
154 catch_sigwinch(gcc_unused int sig)
155 {
156 if (1 != write(sigwinch_pipes[1], "", 1))
157 exit(EXIT_FAILURE);
158 }
159 #endif /* WIN32 */
161 static void
162 idle_callback(enum mpd_error error,
163 gcc_unused enum mpd_server_error server_error,
164 const char *message, enum mpd_idle events,
165 gcc_unused void *ctx);
167 static gboolean
168 timer_mpd_update(gpointer data);
170 static void
171 enable_update_timer(void)
172 {
173 if (update_source_id != 0)
174 return;
176 update_source_id = g_timeout_add(update_interval,
177 timer_mpd_update, NULL);
178 }
180 static void
181 disable_update_timer(void)
182 {
183 if (update_source_id == 0)
184 return;
186 g_source_remove(update_source_id);
187 update_source_id = 0;
188 }
190 static bool
191 should_enable_update_timer(void)
192 {
193 return (mpdclient_is_connected(mpd) &&
194 mpd->status != NULL &&
195 mpd_status_get_state(mpd->status) == MPD_STATE_PLAY)
196 #ifndef NCMPC_MINI
197 || options.display_time
198 #endif
199 ;
200 }
202 static void
203 auto_update_timer(void)
204 {
205 if (should_enable_update_timer())
206 enable_update_timer();
207 else
208 disable_update_timer();
209 }
211 static void
212 check_reconnect(void);
214 static void
215 do_mpd_update(void)
216 {
217 if (mpdclient_is_connected(mpd) &&
218 (mpd->events != 0 ||
219 (mpd->status != NULL &&
220 mpd_status_get_state(mpd->status) == MPD_STATE_PLAY)))
221 mpdclient_update(mpd);
223 #ifndef NCMPC_MINI
224 if (options.enable_xterm_title)
225 update_xterm_title();
226 #endif
228 screen_update(mpd);
229 mpd->events = 0;
231 mpdclient_put_connection(mpd);
232 check_reconnect();
233 }
235 static char *
236 settings_name(const struct mpd_settings *settings)
237 {
238 const char *host = mpd_settings_get_host(settings);
239 if (host == NULL)
240 host = _("unknown");
242 if (host[0] == '/')
243 return g_strdup(host);
245 unsigned port = mpd_settings_get_port(settings);
246 if (port == 0 || port == 6600)
247 return g_strdup(host);
249 return g_strdup_printf("%s:%u", host, port);
250 }
252 static char *
253 default_settings_name(void)
254 {
255 struct mpd_settings *settings =
256 mpd_settings_new(options.host, options.port, 0,
257 NULL, options.password);
258 if (settings == NULL)
259 return g_strdup(_("unknown"));
261 char *name = settings_name(settings);
262 mpd_settings_free(settings);
264 return name;
265 }
267 /**
268 * This timer is installed when the connection to the MPD server is
269 * broken. It tries to recover by reconnecting periodically.
270 */
271 static gboolean
272 timer_reconnect(gcc_unused gpointer data)
273 {
274 assert(!mpdclient_is_connected(mpd));
276 reconnect_source_id = 0;
278 char *name = default_settings_name();
279 screen_status_printf(_("Connecting to %s... [Press %s to abort]"),
280 name, get_key_names(CMD_QUIT, false));
281 g_free(name);
282 doupdate();
284 mpdclient_disconnect(mpd);
285 if (!mpdclient_connect(mpd, options.host, options.port,
286 options.timeout_ms,
287 options.password)) {
288 /* try again in 5 seconds */
289 reconnect_source_id = g_timeout_add(5000,
290 timer_reconnect, NULL);
291 return FALSE;
292 }
294 struct mpd_connection *connection = mpdclient_get_connection(mpd);
296 #ifndef NCMPC_MINI
297 /* quit if mpd is pre 0.14 - song id not supported by mpd */
298 if (mpd_connection_cmp_server_version(connection, 0, 16, 0) < 0) {
299 const unsigned *version =
300 mpd_connection_get_server_version(connection);
301 screen_status_printf(_("Error: MPD version %d.%d.%d is too old (%s needed)"),
302 version[0], version[1], version[2],
303 "0.16.0");
304 mpdclient_disconnect(mpd);
305 doupdate();
307 /* try again after 30 seconds */
308 reconnect_source_id = g_timeout_add(30000,
309 timer_reconnect, NULL);
310 return FALSE;
311 }
312 #endif
314 mpd->source = mpd_glib_new(connection,
315 idle_callback, mpd);
317 screen_status_clear_message();
318 doupdate();
320 /* update immediately */
321 mpd->events = MPD_IDLE_ALL;
323 do_mpd_update();
325 auto_update_timer();
327 return FALSE;
328 }
330 static void
331 check_reconnect(void)
332 {
333 if (!mpdclient_is_connected(mpd) && reconnect_source_id == 0)
334 /* reconnect when the connection is lost */
335 reconnect_source_id = g_timeout_add(1000, timer_reconnect,
336 NULL);
337 }
339 /**
340 * This function is called by the gidle.c library when MPD sends us an
341 * idle event (or when the connection dies).
342 */
343 static void
344 idle_callback(enum mpd_error error,
345 gcc_unused enum mpd_server_error server_error,
346 const char *message, enum mpd_idle events,
347 void *ctx)
348 {
349 struct mpdclient *c = ctx;
351 c->idle = false;
353 assert(mpdclient_is_connected(c));
355 if (error != MPD_ERROR_SUCCESS) {
356 char *allocated;
357 if (error == MPD_ERROR_SERVER)
358 message = allocated = utf8_to_locale(message);
359 else
360 allocated = NULL;
361 screen_status_message(message);
362 g_free(allocated);
363 screen_bell();
364 doupdate();
366 mpdclient_disconnect(c);
367 screen_update(mpd);
368 reconnect_source_id = g_timeout_add(1000, timer_reconnect,
369 NULL);
370 return;
371 }
373 c->events |= events;
374 mpdclient_update(c);
376 #ifndef NCMPC_MINI
377 if (options.enable_xterm_title)
378 update_xterm_title();
379 #endif
381 screen_update(mpd);
382 c->events = 0;
384 mpdclient_put_connection(c);
385 check_reconnect();
386 auto_update_timer();
387 }
389 static gboolean
390 timer_mpd_update(gcc_unused gpointer data)
391 {
392 do_mpd_update();
394 if (should_enable_update_timer())
395 return true;
396 else {
397 update_source_id = 0;
398 return false;
399 }
400 }
402 void begin_input_event(void)
403 {
404 }
406 void end_input_event(void)
407 {
408 screen_update(mpd);
409 mpd->events = 0;
411 mpdclient_put_connection(mpd);
412 check_reconnect();
413 auto_update_timer();
414 }
416 int do_input_event(command_t cmd)
417 {
418 if (cmd == CMD_QUIT) {
419 g_main_loop_quit(main_loop);
420 return -1;
421 }
423 screen_cmd(mpd, cmd);
425 if (cmd == CMD_VOLUME_UP || cmd == CMD_VOLUME_DOWN)
426 /* make sure we don't update the volume yet */
427 disable_update_timer();
429 return 0;
430 }
432 static gboolean
433 keyboard_event(gcc_unused GIOChannel *source,
434 gcc_unused GIOCondition condition,
435 gcc_unused gpointer data)
436 {
437 begin_input_event();
439 command_t cmd = get_keyboard_command();
440 if (cmd != CMD_NONE)
441 if (do_input_event(cmd) != 0)
442 return FALSE;
444 end_input_event();
445 return TRUE;
446 }
448 #ifndef NCMPC_MINI
449 /**
450 * Check the configured key bindings for errors, and display a status
451 * message every 10 seconds.
452 */
453 static gboolean
454 timer_check_key_bindings(gcc_unused gpointer data)
455 {
456 char buf[256];
458 if (check_key_bindings(NULL, buf, sizeof(buf))) {
459 /* no error: disable this timer for the rest of this
460 process */
461 check_key_bindings_source_id = 0;
462 return FALSE;
463 }
465 #ifdef ENABLE_KEYDEF_SCREEN
466 g_strchomp(buf);
467 g_strlcat(buf, " (", sizeof(buf));
468 /* to translators: a key was bound twice in the key editor,
469 and this is a hint for the user what to press to correct
470 that */
471 char comment[64];
472 g_snprintf(comment, sizeof(comment), _("press %s for the key editor"),
473 get_key_names(CMD_SCREEN_KEYDEF, false));
474 g_strlcat(buf, comment, sizeof(buf));
475 g_strlcat(buf, ")", sizeof(buf));
476 #endif
478 screen_status_printf("%s", buf);
480 doupdate();
481 return TRUE;
482 }
483 #endif
485 int
486 main(int argc, const char *argv[])
487 {
488 #ifdef ENABLE_LOCALE
489 #ifndef ENABLE_NLS
490 gcc_unused
491 #endif
492 const char *charset = NULL;
493 /* time and date formatting */
494 setlocale(LC_TIME,"");
495 /* care about sorting order etc */
496 setlocale(LC_COLLATE,"");
497 /* charset */
498 setlocale(LC_CTYPE,"");
499 /* initialize charset conversions */
500 charset = charset_init();
502 /* initialize i18n support */
503 #endif
505 #ifdef ENABLE_NLS
506 setlocale(LC_MESSAGES, "");
507 bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
508 #ifdef ENABLE_LOCALE
509 bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
510 #endif
511 textdomain(GETTEXT_PACKAGE);
512 #endif
514 /* initialize options */
515 options_init();
517 /* parse command line options - 1 pass get configuration files */
518 options_parse(argc, argv);
520 #ifndef NCMPC_MINI
521 /* read configuration */
522 read_configuration();
524 /* check key bindings */
525 check_key_bindings(NULL, NULL, 0);
526 #endif
528 /* parse command line options - 2 pass */
529 options_parse(argc, argv);
531 #ifndef WIN32
532 /* setup signal behavior - SIGINT */
533 struct sigaction act;
534 sigemptyset(&act.sa_mask);
535 act.sa_flags = 0;
536 act.sa_handler = catch_sigint;
537 if (sigaction(SIGINT, &act, NULL) < 0) {
538 perror("signal");
539 exit(EXIT_FAILURE);
540 }
542 /* setup signal behavior - SIGTERM */
544 act.sa_handler = catch_sigint;
545 if (sigaction(SIGTERM, &act, NULL) < 0) {
546 perror("sigaction()");
547 exit(EXIT_FAILURE);
548 }
550 /* setup signal behavior - SIGCONT */
552 act.sa_handler = catch_sigcont;
553 if (sigaction(SIGCONT, &act, NULL) < 0) {
554 perror("sigaction(SIGCONT)");
555 exit(EXIT_FAILURE);
556 }
558 /* setup signal behaviour - SIGHUP*/
560 act.sa_handler = catch_sigint;
561 if (sigaction(SIGHUP, &act, NULL) < 0) {
562 perror("sigaction(SIGHUP)");
563 exit(EXIT_FAILURE);
564 }
566 /* setup SIGWINCH */
568 act.sa_flags = SA_RESTART;
569 act.sa_handler = catch_sigwinch;
570 if (sigaction(SIGWINCH, &act, NULL) < 0) {
571 perror("sigaction(SIGWINCH)");
572 exit(EXIT_FAILURE);
573 }
575 /* ignore SIGPIPE */
577 act.sa_handler = SIG_IGN;
578 if (sigaction(SIGPIPE, &act, NULL) < 0) {
579 perror("sigaction(SIGPIPE)");
580 exit(EXIT_FAILURE);
581 }
582 #endif
584 ncu_init();
586 #ifdef ENABLE_LYRICS_SCREEN
587 lyrics_init();
588 #endif
590 /* create mpdclient instance */
591 mpd = mpdclient_new();
593 /* initialize curses */
594 screen_init(mpd);
596 /* the main loop */
597 main_loop = g_main_loop_new(NULL, FALSE);
599 /* watch out for keyboard input */
600 GIOChannel *keyboard_channel = g_io_channel_unix_new(STDIN_FILENO);
601 g_io_add_watch(keyboard_channel, G_IO_IN, keyboard_event, NULL);
603 #ifdef ENABLE_LIRC
604 /* watch out for lirc input */
605 int lirc_socket = ncmpc_lirc_open();
606 GIOChannel *lirc_channel = NULL;
607 if (lirc_socket >= 0) {
608 lirc_channel = g_io_channel_unix_new(lirc_socket);
609 g_io_add_watch(lirc_channel, G_IO_IN, lirc_event, NULL);
610 }
611 #endif
613 #ifndef WIN32
614 GIOChannel *sigwinch_channel = NULL;
615 if (!pipe(sigwinch_pipes) &&
616 !fcntl(sigwinch_pipes[1], F_SETFL, O_NONBLOCK)) {
617 sigwinch_channel = g_io_channel_unix_new(sigwinch_pipes[0]);
618 g_io_add_watch(sigwinch_channel, G_IO_IN, sigwinch_event, NULL);
619 }
620 else {
621 perror("sigwinch pipe creation failed");
622 exit(EXIT_FAILURE);
623 }
624 #endif
626 /* attempt to connect */
627 reconnect_source_id = g_timeout_add(1, timer_reconnect, NULL);
629 auto_update_timer();
631 #ifndef NCMPC_MINI
632 check_key_bindings_source_id = g_timeout_add(10000, timer_check_key_bindings, NULL);
633 #endif
635 screen_paint(mpd);
637 g_main_loop_run(main_loop);
639 /* cleanup */
641 cancel_seek_timer();
643 disable_update_timer();
645 if (reconnect_source_id != 0)
646 g_source_remove(reconnect_source_id);
648 #ifndef NCMPC_MINI
649 if (check_key_bindings_source_id != 0)
650 g_source_remove(check_key_bindings_source_id);
651 #endif
653 g_main_loop_unref(main_loop);
654 g_io_channel_unref(keyboard_channel);
655 g_io_channel_unref(sigwinch_channel);
656 close(sigwinch_pipes[0]);
657 close(sigwinch_pipes[1]);
659 #ifdef ENABLE_LIRC
660 if (lirc_socket >= 0)
661 g_io_channel_unref(lirc_channel);
662 ncmpc_lirc_close();
663 #endif
665 exit_and_cleanup();
667 #ifdef ENABLE_LYRICS_SCREEN
668 lyrics_deinit();
669 #endif
671 ncu_deinit();
672 options_deinit();
674 return 0;
675 }