33678eecb9c05c76367b76c23bc0459cbb7a8270
1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2010 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 static char title[BUFSIZE];
78 char tmp[BUFSIZE];
79 struct mpd_status *status = NULL;
80 const struct mpd_song *song = NULL;
82 if (mpd) {
83 status = mpd->status;
84 song = mpd->song;
85 }
87 if (options.xterm_title_format && status && song &&
88 mpd_status_get_state(status) == MPD_STATE_PLAY)
89 strfsong(tmp, BUFSIZE, options.xterm_title_format, song);
90 else
91 g_strlcpy(tmp, PACKAGE " version " VERSION, BUFSIZE);
93 if (strncmp(title, tmp, BUFSIZE)) {
94 g_strlcpy(title, tmp, BUFSIZE);
95 set_xterm_title("%s", title);
96 }
97 }
98 #endif
100 static void
101 exit_and_cleanup(void)
102 {
103 screen_exit();
104 #ifndef NCMPC_MINI
105 set_xterm_title("");
106 #endif
107 printf("\n");
109 if (mpd) {
110 mpdclient_disconnect(mpd);
111 mpdclient_free(mpd);
112 }
113 }
115 #ifndef WIN32
116 static void
117 catch_sigint(G_GNUC_UNUSED int sig)
118 {
119 g_main_loop_quit(main_loop);
120 }
123 static void
124 catch_sigcont(G_GNUC_UNUSED int sig)
125 {
126 char irrelevant = 'a';
127 if (1 != write(sigwinch_pipes[1], &irrelevant, 1))
128 exit(EXIT_FAILURE);
129 }
131 void
132 sigstop(void)
133 {
134 def_prog_mode(); /* save the tty modes */
135 endwin(); /* end curses mode temporarily */
136 kill(0, SIGSTOP); /* issue SIGSTOP */
137 }
139 static gboolean
140 sigwinch_event(G_GNUC_UNUSED GIOChannel *source,
141 G_GNUC_UNUSED GIOCondition condition, G_GNUC_UNUSED gpointer data)
142 {
143 char ignoreme[64];
144 if (1 > read(sigwinch_pipes[0], ignoreme, 64))
145 exit(EXIT_FAILURE);
147 endwin();
148 refresh();
149 screen_resize(mpd);
151 return TRUE;
152 }
154 static void
155 catch_sigwinch(G_GNUC_UNUSED int sig)
156 {
157 char irrelevant = 'a';
158 if (1 != write(sigwinch_pipes[1], &irrelevant, 1))
159 exit(EXIT_FAILURE);
160 }
161 #endif /* WIN32 */
163 static void
164 idle_callback(enum mpd_error error,
165 G_GNUC_UNUSED enum mpd_server_error server_error,
166 const char *message, enum mpd_idle events,
167 G_GNUC_UNUSED void *ctx);
169 static gboolean
170 timer_mpd_update(gpointer data);
172 static void
173 enable_update_timer(void)
174 {
175 if (update_source_id != 0)
176 return;
178 update_source_id = g_timeout_add(update_interval,
179 timer_mpd_update, NULL);
180 }
182 static void
183 disable_update_timer(void)
184 {
185 if (update_source_id == 0)
186 return;
188 g_source_remove(update_source_id);
189 update_source_id = 0;
190 }
192 static bool
193 should_enable_update_timer(void)
194 {
195 return (mpdclient_is_connected(mpd) &&
196 (mpd->source == NULL || /* "idle" not supported */
197 (mpd->status != NULL &&
198 mpd_status_get_state(mpd->status) == MPD_STATE_PLAY)))
199 #ifndef NCMPC_MINI
200 || options.display_time
201 #endif
202 ;
203 }
205 static void
206 auto_update_timer(void)
207 {
208 if (should_enable_update_timer())
209 enable_update_timer();
210 else
211 disable_update_timer();
212 }
214 static void
215 check_reconnect(void);
217 static void
218 do_mpd_update(void)
219 {
220 if (mpdclient_is_connected(mpd) &&
221 (mpd->source == NULL || mpd->events != 0 ||
222 (mpd->status != NULL &&
223 mpd_status_get_state(mpd->status) == MPD_STATE_PLAY)))
224 mpdclient_update(mpd);
226 #ifndef NCMPC_MINI
227 if (options.enable_xterm_title)
228 update_xterm_title();
229 #endif
231 screen_update(mpd);
232 mpd->events = 0;
234 mpdclient_put_connection(mpd);
235 check_reconnect();
236 }
238 #if LIBMPDCLIENT_CHECK_VERSION(2,4,0)
240 static char *
241 settings_name(const struct mpd_settings *settings)
242 {
243 const char *host = mpd_settings_get_host(settings);
244 if (host == NULL)
245 host = _("unknown");
247 if (host[0] == '/')
248 return g_strdup(host);
250 unsigned port = mpd_settings_get_port(settings);
251 if (port == 0 || port == 6600)
252 return g_strdup(host);
254 return g_strdup_printf("%s:%u", host, port);
255 }
257 #endif
259 static char *
260 default_settings_name(void)
261 {
262 #if LIBMPDCLIENT_CHECK_VERSION(2,4,0)
263 struct mpd_settings *settings =
264 mpd_settings_new(options.host, options.port, 0,
265 NULL, options.password);
266 if (settings == NULL)
267 return g_strdup(_("unknown"));
269 char *name = settings_name(settings);
270 mpd_settings_free(settings);
272 return name;
273 #else
274 /*
275 * localhost is actually not correct, we only know that
276 * mpd_connection_new() has connected to the "default host".
277 */
278 const char *name = options.host ?: "localhost";
279 return g_strdup(name);
280 #endif
281 }
283 static char *
284 connection_settings_name(const struct mpd_connection *connection)
285 {
286 #if LIBMPDCLIENT_CHECK_VERSION(2,4,0)
287 const struct mpd_settings *settings =
288 mpd_connection_get_settings(connection);
289 if (settings == NULL)
290 return g_strdup(_("unknown"));
292 return settings_name(settings);
293 #else
294 (void)connection;
296 return default_settings_name();
297 #endif
298 }
300 /**
301 * This timer is installed when the connection to the MPD server is
302 * broken. It tries to recover by reconnecting periodically.
303 */
304 static gboolean
305 timer_reconnect(G_GNUC_UNUSED gpointer data)
306 {
307 bool success;
308 struct mpd_connection *connection;
310 assert(!mpdclient_is_connected(mpd));
312 reconnect_source_id = 0;
314 char *name = default_settings_name();
315 screen_status_printf(_("Connecting to %s... [Press %s to abort]"),
316 name, get_key_names(CMD_QUIT, false));
317 g_free(name);
318 doupdate();
320 mpdclient_disconnect(mpd);
321 success = mpdclient_connect(mpd,
322 options.host, options.port,
323 options.timeout_ms,
324 options.password);
325 if (!success) {
326 /* try again in 5 seconds */
327 reconnect_source_id = g_timeout_add(5000,
328 timer_reconnect, NULL);
329 return FALSE;
330 }
332 connection = mpdclient_get_connection(mpd);
334 #ifndef NCMPC_MINI
335 /* quit if mpd is pre 0.14 - song id not supported by mpd */
336 if (mpd_connection_cmp_server_version(connection, 0, 16, 0) < 0) {
337 const unsigned *version =
338 mpd_connection_get_server_version(connection);
339 screen_status_printf(_("Error: MPD version %d.%d.%d is to old (%s needed)"),
340 version[0], version[1], version[2],
341 "0.16.0");
342 mpdclient_disconnect(mpd);
343 doupdate();
345 /* try again after 30 seconds */
346 reconnect_source_id = g_timeout_add(30000,
347 timer_reconnect, NULL);
348 return FALSE;
349 }
350 #endif
352 mpd->source = mpd_glib_new(connection,
353 idle_callback, mpd);
355 name = connection_settings_name(connection);
356 screen_status_printf(_("Connected to %s"), name);
357 g_free(name);
358 doupdate();
360 /* update immediately */
361 mpd->events = MPD_IDLE_ALL;
363 do_mpd_update();
365 auto_update_timer();
367 return FALSE;
368 }
370 static void
371 check_reconnect(void)
372 {
373 if (!mpdclient_is_connected(mpd) && reconnect_source_id == 0)
374 /* reconnect when the connection is lost */
375 reconnect_source_id = g_timeout_add(1000, timer_reconnect,
376 NULL);
377 }
379 /**
380 * This function is called by the gidle.c library when MPD sends us an
381 * idle event (or when the connection dies).
382 */
383 static void
384 idle_callback(enum mpd_error error, enum mpd_server_error server_error,
385 const char *message, enum mpd_idle events,
386 void *ctx)
387 {
388 struct mpdclient *c = ctx;
390 c->idle = false;
392 assert(mpdclient_is_connected(c));
394 if (error != MPD_ERROR_SUCCESS) {
395 char *allocated;
397 if (error == MPD_ERROR_SERVER &&
398 server_error == MPD_SERVER_ERROR_UNKNOWN_CMD) {
399 /* the "idle" command is not supported - fall
400 back to timer based polling */
401 mpd_glib_free(c->source);
402 c->source = NULL;
403 auto_update_timer();
404 return;
405 }
407 if (error == MPD_ERROR_SERVER)
408 message = allocated = utf8_to_locale(message);
409 else
410 allocated = NULL;
411 screen_status_message(message);
412 g_free(allocated);
413 screen_bell();
414 doupdate();
416 mpdclient_disconnect(c);
417 screen_update(mpd);
418 reconnect_source_id = g_timeout_add(1000, timer_reconnect,
419 NULL);
420 return;
421 }
423 c->events |= events;
424 mpdclient_update(c);
426 #ifndef NCMPC_MINI
427 if (options.enable_xterm_title)
428 update_xterm_title();
429 #endif
431 screen_update(mpd);
432 c->events = 0;
434 mpdclient_put_connection(c);
435 check_reconnect();
436 auto_update_timer();
437 }
439 static gboolean
440 timer_mpd_update(G_GNUC_UNUSED gpointer data)
441 {
442 do_mpd_update();
444 if (should_enable_update_timer())
445 return true;
446 else {
447 update_source_id = 0;
448 return false;
449 }
450 }
452 void begin_input_event(void)
453 {
454 }
456 void end_input_event(void)
457 {
458 screen_update(mpd);
459 mpd->events = 0;
461 mpdclient_put_connection(mpd);
462 check_reconnect();
463 auto_update_timer();
464 }
466 int do_input_event(command_t cmd)
467 {
468 if (cmd == CMD_QUIT) {
469 g_main_loop_quit(main_loop);
470 return -1;
471 }
473 screen_cmd(mpd, cmd);
475 if (cmd == CMD_VOLUME_UP || cmd == CMD_VOLUME_DOWN)
476 /* make sure we don't update the volume yet */
477 disable_update_timer();
479 return 0;
480 }
482 static gboolean
483 keyboard_event(G_GNUC_UNUSED GIOChannel *source,
484 G_GNUC_UNUSED GIOCondition condition,
485 G_GNUC_UNUSED gpointer data)
486 {
487 command_t cmd;
489 begin_input_event();
491 if ((cmd=get_keyboard_command()) != CMD_NONE)
492 if (do_input_event(cmd) != 0)
493 return FALSE;
495 end_input_event();
496 return TRUE;
497 }
499 #ifndef NCMPC_MINI
500 /**
501 * Check the configured key bindings for errors, and display a status
502 * message every 10 seconds.
503 */
504 static gboolean
505 timer_check_key_bindings(G_GNUC_UNUSED gpointer data)
506 {
507 char buf[256];
508 #ifdef ENABLE_KEYDEF_SCREEN
509 char comment[64];
510 #endif
511 gboolean key_error;
513 key_error = check_key_bindings(NULL, buf, sizeof(buf));
514 if (!key_error) {
515 /* no error: disable this timer for the rest of this
516 process */
517 check_key_bindings_source_id = 0;
518 return FALSE;
519 }
521 #ifdef ENABLE_KEYDEF_SCREEN
522 g_strchomp(buf);
523 g_strlcat(buf, " (", sizeof(buf));
524 /* to translators: a key was bound twice in the key editor,
525 and this is a hint for the user what to press to correct
526 that */
527 g_snprintf(comment, sizeof(comment), _("press %s for the key editor"),
528 get_key_names(CMD_SCREEN_KEYDEF, false));
529 g_strlcat(buf, comment, sizeof(buf));
530 g_strlcat(buf, ")", sizeof(buf));
531 #endif
533 screen_status_printf("%s", buf);
535 doupdate();
536 return TRUE;
537 }
538 #endif
540 int
541 main(int argc, const char *argv[])
542 {
543 #ifndef WIN32
544 struct sigaction act;
545 #endif
546 #ifdef ENABLE_LOCALE
547 #ifndef ENABLE_NLS
548 G_GNUC_UNUSED
549 #endif
550 const char *charset = NULL;
551 #endif
552 GIOChannel *keyboard_channel;
553 #ifdef ENABLE_LIRC
554 int lirc_socket;
555 GIOChannel *lirc_channel = NULL;
556 #endif
557 GIOChannel *sigwinch_channel = NULL;
559 #ifdef ENABLE_LOCALE
560 /* time and date formatting */
561 setlocale(LC_TIME,"");
562 /* care about sorting order etc */
563 setlocale(LC_COLLATE,"");
564 /* charset */
565 setlocale(LC_CTYPE,"");
566 /* initialize charset conversions */
567 charset = charset_init();
569 /* initialize i18n support */
570 #endif
572 #ifdef ENABLE_NLS
573 setlocale(LC_MESSAGES, "");
574 bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
575 #ifdef ENABLE_LOCALE
576 bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
577 #endif
578 textdomain(GETTEXT_PACKAGE);
579 #endif
581 /* initialize options */
582 options_init();
584 /* parse command line options - 1 pass get configuration files */
585 options_parse(argc, argv);
587 #ifndef NCMPC_MINI
588 /* read configuration */
589 read_configuration();
591 /* check key bindings */
592 check_key_bindings(NULL, NULL, 0);
593 #endif
595 /* parse command line options - 2 pass */
596 options_parse(argc, argv);
598 #ifndef WIN32
599 /* setup signal behavior - SIGINT */
600 sigemptyset(&act.sa_mask);
601 act.sa_flags = 0;
602 act.sa_handler = catch_sigint;
603 if (sigaction(SIGINT, &act, NULL) < 0) {
604 perror("signal");
605 exit(EXIT_FAILURE);
606 }
608 /* setup signal behavior - SIGTERM */
610 act.sa_handler = catch_sigint;
611 if (sigaction(SIGTERM, &act, NULL) < 0) {
612 perror("sigaction()");
613 exit(EXIT_FAILURE);
614 }
616 /* setup signal behavior - SIGCONT */
618 act.sa_handler = catch_sigcont;
619 if (sigaction(SIGCONT, &act, NULL) < 0) {
620 perror("sigaction(SIGCONT)");
621 exit(EXIT_FAILURE);
622 }
624 /* setup signal behaviour - SIGHUP*/
626 act.sa_handler = catch_sigint;
627 if (sigaction(SIGHUP, &act, NULL) < 0) {
628 perror("sigaction(SIGHUP)");
629 exit(EXIT_FAILURE);
630 }
632 /* setup SIGWINCH */
634 act.sa_flags = SA_RESTART;
635 act.sa_handler = catch_sigwinch;
636 if (sigaction(SIGWINCH, &act, NULL) < 0) {
637 perror("sigaction(SIGWINCH)");
638 exit(EXIT_FAILURE);
639 }
641 /* ignore SIGPIPE */
643 act.sa_handler = SIG_IGN;
644 if (sigaction(SIGPIPE, &act, NULL) < 0) {
645 perror("sigaction(SIGPIPE)");
646 exit(EXIT_FAILURE);
647 }
648 #endif
650 ncu_init();
652 #ifdef ENABLE_LYRICS_SCREEN
653 lyrics_init();
654 #endif
656 /* create mpdclient instance */
657 mpd = mpdclient_new();
659 /* initialize curses */
660 screen_init(mpd);
662 /* the main loop */
663 main_loop = g_main_loop_new(NULL, FALSE);
665 /* watch out for keyboard input */
666 keyboard_channel = g_io_channel_unix_new(STDIN_FILENO);
667 g_io_add_watch(keyboard_channel, G_IO_IN, keyboard_event, NULL);
669 #ifdef ENABLE_LIRC
670 /* watch out for lirc input */
671 lirc_socket = ncmpc_lirc_open();
672 if (lirc_socket >= 0) {
673 lirc_channel = g_io_channel_unix_new(lirc_socket);
674 g_io_add_watch(lirc_channel, G_IO_IN, lirc_event, NULL);
675 }
676 #endif
678 #ifndef WIN32
679 if (!pipe(sigwinch_pipes) &&
680 !fcntl(sigwinch_pipes[1], F_SETFL, O_NONBLOCK)) {
681 sigwinch_channel = g_io_channel_unix_new(sigwinch_pipes[0]);
682 g_io_add_watch(sigwinch_channel, G_IO_IN, sigwinch_event, NULL);
683 }
684 else {
685 perror("sigwinch pipe creation failed");
686 exit(EXIT_FAILURE);
687 }
688 #endif
690 /* attempt to connect */
691 reconnect_source_id = g_timeout_add(1, timer_reconnect, NULL);
693 auto_update_timer();
695 #ifndef NCMPC_MINI
696 check_key_bindings_source_id = g_timeout_add(10000, timer_check_key_bindings, NULL);
697 #endif
699 screen_paint(mpd);
701 g_main_loop_run(main_loop);
703 /* cleanup */
705 cancel_seek_timer();
707 disable_update_timer();
709 if (reconnect_source_id != 0)
710 g_source_remove(reconnect_source_id);
712 #ifndef NCMPC_MINI
713 if (check_key_bindings_source_id != 0)
714 g_source_remove(check_key_bindings_source_id);
715 #endif
717 g_main_loop_unref(main_loop);
718 g_io_channel_unref(keyboard_channel);
719 g_io_channel_unref(sigwinch_channel);
720 close(sigwinch_pipes[0]);
721 close(sigwinch_pipes[1]);
723 #ifdef ENABLE_LIRC
724 if (lirc_socket >= 0)
725 g_io_channel_unref(lirc_channel);
726 ncmpc_lirc_close();
727 #endif
729 exit_and_cleanup();
731 #ifdef ENABLE_LYRICS_SCREEN
732 lyrics_deinit();
733 #endif
735 ncu_deinit();
736 options_deinit();
738 return 0;
739 }