5b31f41a410dcec78d3cb907e41264cd18addeb3
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_message.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 static void
116 catch_sigint(G_GNUC_UNUSED int sig)
117 {
118 g_main_loop_quit(main_loop);
119 }
122 static void
123 catch_sigcont(G_GNUC_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(G_GNUC_UNUSED GIOChannel *source,
140 G_GNUC_UNUSED GIOCondition condition, G_GNUC_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(G_GNUC_UNUSED int sig)
155 {
156 char irrelevant = 'a';
157 if (1 != write(sigwinch_pipes[1], &irrelevant, 1))
158 exit(EXIT_FAILURE);
159 }
161 static void
162 idle_callback(enum mpd_error error,
163 G_GNUC_UNUSED enum mpd_server_error server_error,
164 const char *message, enum mpd_idle events,
165 G_GNUC_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->source == NULL || /* "idle" not supported */
195 (mpd->status != NULL &&
196 mpd_status_get_state(mpd->status) == MPD_STATE_PLAY)))
197 #ifndef NCMPC_MINI
198 || options.display_time
199 #endif
200 ;
201 }
203 static void
204 auto_update_timer(void)
205 {
206 if (should_enable_update_timer())
207 enable_update_timer();
208 else
209 disable_update_timer();
210 }
212 static void
213 check_reconnect(void);
215 static void
216 do_mpd_update(void)
217 {
218 if (mpdclient_is_connected(mpd) &&
219 (mpd->source == NULL || mpd->events != 0 ||
220 (mpd->status != NULL &&
221 mpd_status_get_state(mpd->status) == MPD_STATE_PLAY)))
222 mpdclient_update(mpd);
224 #ifndef NCMPC_MINI
225 if (options.enable_xterm_title)
226 update_xterm_title();
227 #endif
229 screen_update(mpd);
230 mpd->events = 0;
232 mpdclient_put_connection(mpd);
233 check_reconnect();
234 }
236 #if LIBMPDCLIENT_CHECK_VERSION(2,4,0)
238 static char *
239 settings_name(const struct mpd_settings *settings)
240 {
241 const char *host = mpd_settings_get_host(settings);
242 if (host == NULL)
243 host = "unknown";
245 if (host[0] == '/')
246 return g_strdup(host);
248 unsigned port = mpd_settings_get_port(settings);
249 if (port == 0 || port == 6600)
250 return g_strdup(host);
252 return g_strdup_printf("%s:%u", host, port);
253 }
255 #endif
257 static char *
258 default_settings_name(void)
259 {
260 #if LIBMPDCLIENT_CHECK_VERSION(2,4,0)
261 struct mpd_settings *settings =
262 mpd_settings_new(options.host, options.port, 0,
263 NULL, options.password);
264 if (settings == NULL)
265 return g_strdup("unknown");
267 char *name = settings_name(settings);
268 mpd_settings_free(settings);
270 return name;
271 #else
272 return g_strdup(options.host);
273 #endif
274 }
276 static char *
277 connection_settings_name(const struct mpd_connection *connection)
278 {
279 #if LIBMPDCLIENT_CHECK_VERSION(2,4,0)
280 const struct mpd_settings *settings =
281 mpd_connection_get_settings(connection);
282 if (settings == NULL)
283 return g_strdup("unknown");
285 return settings_name(settings);
286 #else
287 (void)connection;
288 return g_strdup(options.host);
289 #endif
290 }
292 /**
293 * This timer is installed when the connection to the MPD server is
294 * broken. It tries to recover by reconnecting periodically.
295 */
296 static gboolean
297 timer_reconnect(G_GNUC_UNUSED gpointer data)
298 {
299 bool success;
300 struct mpd_connection *connection;
302 assert(!mpdclient_is_connected(mpd));
304 reconnect_source_id = 0;
306 char *name = default_settings_name();
307 screen_status_printf(_("Connecting to %s... [Press %s to abort]"),
308 name, get_key_names(CMD_QUIT,0) );
309 g_free(name);
310 doupdate();
312 mpdclient_disconnect(mpd);
313 success = mpdclient_connect(mpd,
314 options.host, options.port,
315 1.5,
316 options.password);
317 if (!success) {
318 /* try again in 5 seconds */
319 reconnect_source_id = g_timeout_add(5000,
320 timer_reconnect, NULL);
321 return FALSE;
322 }
324 connection = mpdclient_get_connection(mpd);
326 #ifndef NCMPC_MINI
327 /* quit if mpd is pre 0.11.0 - song id not supported by mpd */
328 if (mpd_connection_cmp_server_version(connection, 0, 12, 0) < 0) {
329 const unsigned *version =
330 mpd_connection_get_server_version(connection);
331 screen_status_printf(_("Error: MPD version %d.%d.%d is to old (%s needed)"),
332 version[0], version[1], version[2],
333 "0.12.0");
334 mpdclient_disconnect(mpd);
335 doupdate();
337 /* try again after 30 seconds */
338 reconnect_source_id = g_timeout_add(30000,
339 timer_reconnect, NULL);
340 return FALSE;
341 }
342 #endif
344 if (mpd_connection_cmp_server_version(connection,
345 0, 14, 0) >= 0)
346 mpd->source = mpd_glib_new(connection,
347 idle_callback, mpd);
349 name = connection_settings_name(connection);
350 screen_status_printf(_("Connected to %s"), name);
351 g_free(name);
352 doupdate();
354 /* update immediately */
355 mpd->events = MPD_IDLE_DATABASE|MPD_IDLE_STORED_PLAYLIST|
356 MPD_IDLE_QUEUE|MPD_IDLE_PLAYER|MPD_IDLE_MIXER|MPD_IDLE_OUTPUT|
357 MPD_IDLE_OPTIONS|MPD_IDLE_UPDATE;
359 do_mpd_update();
361 auto_update_timer();
363 return FALSE;
364 }
366 static void
367 check_reconnect(void)
368 {
369 if (!mpdclient_is_connected(mpd) && reconnect_source_id == 0)
370 /* reconnect when the connection is lost */
371 reconnect_source_id = g_timeout_add(1000, timer_reconnect,
372 NULL);
373 }
375 /**
376 * This function is called by the gidle.c library when MPD sends us an
377 * idle event (or when the connectiond dies).
378 */
379 static void
380 idle_callback(enum mpd_error error, enum mpd_server_error server_error,
381 const char *message, enum mpd_idle events,
382 void *ctx)
383 {
384 struct mpdclient *c = ctx;
386 c->idle = false;
388 assert(mpdclient_get_connection(c) != NULL);
390 if (error != MPD_ERROR_SUCCESS) {
391 char *allocated;
393 if (error == MPD_ERROR_SERVER &&
394 server_error == MPD_SERVER_ERROR_UNKNOWN_CMD) {
395 /* the "idle" command is not supported - fall
396 back to timer based polling */
397 mpd_glib_free(c->source);
398 c->source = NULL;
399 auto_update_timer();
400 return;
401 }
403 if (error == MPD_ERROR_SERVER)
404 message = allocated = utf8_to_locale(message);
405 else
406 allocated = NULL;
407 screen_status_message(message);
408 g_free(allocated);
409 screen_bell();
410 doupdate();
412 mpdclient_disconnect(c);
413 screen_update(mpd);
414 reconnect_source_id = g_timeout_add(1000, timer_reconnect,
415 NULL);
416 return;
417 }
419 c->events |= events;
420 mpdclient_update(c);
422 #ifndef NCMPC_MINI
423 if (options.enable_xterm_title)
424 update_xterm_title();
425 #endif
427 screen_update(mpd);
428 c->events = 0;
430 mpdclient_put_connection(c);
431 check_reconnect();
432 auto_update_timer();
433 }
435 static gboolean
436 timer_mpd_update(G_GNUC_UNUSED gpointer data)
437 {
438 do_mpd_update();
440 if (should_enable_update_timer())
441 return true;
442 else {
443 update_source_id = 0;
444 return false;
445 }
446 }
448 void begin_input_event(void)
449 {
450 }
452 void end_input_event(void)
453 {
454 screen_update(mpd);
455 mpd->events = 0;
457 mpdclient_put_connection(mpd);
458 check_reconnect();
459 auto_update_timer();
460 }
462 int do_input_event(command_t cmd)
463 {
464 if (cmd == CMD_QUIT) {
465 g_main_loop_quit(main_loop);
466 return -1;
467 }
469 screen_cmd(mpd, cmd);
471 if (cmd == CMD_VOLUME_UP || cmd == CMD_VOLUME_DOWN)
472 /* make sure we don't update the volume yet */
473 disable_update_timer();
475 return 0;
476 }
478 static gboolean
479 keyboard_event(G_GNUC_UNUSED GIOChannel *source,
480 G_GNUC_UNUSED GIOCondition condition,
481 G_GNUC_UNUSED gpointer data)
482 {
483 command_t cmd;
485 begin_input_event();
487 if ((cmd=get_keyboard_command()) != CMD_NONE)
488 if (do_input_event(cmd) != 0)
489 return FALSE;
491 end_input_event();
492 return TRUE;
493 }
495 #ifndef NCMPC_MINI
496 /**
497 * Check the configured key bindings for errors, and display a status
498 * message every 10 seconds.
499 */
500 static gboolean
501 timer_check_key_bindings(G_GNUC_UNUSED gpointer data)
502 {
503 char buf[256];
504 #ifdef ENABLE_KEYDEF_SCREEN
505 char comment[64];
506 #endif
507 gboolean key_error;
509 key_error = check_key_bindings(NULL, buf, sizeof(buf));
510 if (!key_error) {
511 /* no error: disable this timer for the rest of this
512 process */
513 check_key_bindings_source_id = 0;
514 return FALSE;
515 }
517 #ifdef ENABLE_KEYDEF_SCREEN
518 g_strchomp(buf);
519 g_strlcat(buf, " (", sizeof(buf));
520 /* to translators: a key was bound twice in the key editor,
521 and this is a hint for the user what to press to correct
522 that */
523 g_snprintf(comment, sizeof(comment), _("press %s for the key editor"),
524 get_key_names(CMD_SCREEN_KEYDEF, 0));
525 g_strlcat(buf, comment, sizeof(buf));
526 g_strlcat(buf, ")", sizeof(buf));
527 #endif
529 screen_status_printf("%s", buf);
531 doupdate();
532 return TRUE;
533 }
534 #endif
536 int
537 main(int argc, const char *argv[])
538 {
539 struct sigaction act;
540 #ifdef ENABLE_LOCALE
541 const char *charset = NULL;
542 #endif
543 GIOChannel *keyboard_channel;
544 #ifdef ENABLE_LIRC
545 int lirc_socket;
546 GIOChannel *lirc_channel = NULL;
547 #endif
548 GIOChannel *sigwinch_channel = NULL;
550 #ifdef ENABLE_LOCALE
551 /* time and date formatting */
552 setlocale(LC_TIME,"");
553 /* care about sorting order etc */
554 setlocale(LC_COLLATE,"");
555 /* charset */
556 setlocale(LC_CTYPE,"");
557 /* initialize charset conversions */
558 charset = charset_init();
560 /* initialize i18n support */
561 #endif
563 #ifdef ENABLE_NLS
564 setlocale(LC_MESSAGES, "");
565 bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
566 #ifdef ENABLE_LOCALE
567 bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
568 #endif
569 textdomain(GETTEXT_PACKAGE);
570 #endif
572 /* initialize options */
573 options_init();
575 /* parse command line options - 1 pass get configuration files */
576 options_parse(argc, argv);
578 #ifndef NCMPC_MINI
579 /* read configuration */
580 read_configuration();
582 /* check key bindings */
583 check_key_bindings(NULL, NULL, 0);
584 #endif
586 /* parse command line options - 2 pass */
587 options_parse(argc, argv);
589 /* setup signal behavior - SIGINT */
590 sigemptyset(&act.sa_mask);
591 act.sa_flags = 0;
592 act.sa_handler = catch_sigint;
593 if (sigaction(SIGINT, &act, NULL) < 0) {
594 perror("signal");
595 exit(EXIT_FAILURE);
596 }
598 /* setup signal behavior - SIGTERM */
600 act.sa_handler = catch_sigint;
601 if (sigaction(SIGTERM, &act, NULL) < 0) {
602 perror("sigaction()");
603 exit(EXIT_FAILURE);
604 }
606 /* setup signal behavior - SIGCONT */
608 act.sa_handler = catch_sigcont;
609 if (sigaction(SIGCONT, &act, NULL) < 0) {
610 perror("sigaction(SIGCONT)");
611 exit(EXIT_FAILURE);
612 }
614 /* setup signal behaviour - SIGHUP*/
616 act.sa_handler = catch_sigint;
617 if (sigaction(SIGHUP, &act, NULL) < 0) {
618 perror("sigaction(SIGHUP)");
619 exit(EXIT_FAILURE);
620 }
622 /* setup SIGWINCH */
624 act.sa_flags = SA_RESTART;
625 act.sa_handler = catch_sigwinch;
626 if (sigaction(SIGWINCH, &act, NULL) < 0) {
627 perror("sigaction(SIGWINCH)");
628 exit(EXIT_FAILURE);
629 }
631 /* ignore SIGPIPE */
633 act.sa_handler = SIG_IGN;
634 if (sigaction(SIGPIPE, &act, NULL) < 0) {
635 perror("sigaction(SIGPIPE)");
636 exit(EXIT_FAILURE);
637 }
639 ncu_init();
641 #ifdef ENABLE_LYRICS_SCREEN
642 lyrics_init();
643 #endif
645 /* create mpdclient instance */
646 mpd = mpdclient_new();
648 /* initialize curses */
649 screen_init(mpd);
651 /* the main loop */
652 main_loop = g_main_loop_new(NULL, FALSE);
654 /* watch out for keyboard input */
655 keyboard_channel = g_io_channel_unix_new(STDIN_FILENO);
656 g_io_add_watch(keyboard_channel, G_IO_IN, keyboard_event, NULL);
658 #ifdef ENABLE_LIRC
659 /* watch out for lirc input */
660 lirc_socket = ncmpc_lirc_open();
661 if (lirc_socket >= 0) {
662 lirc_channel = g_io_channel_unix_new(lirc_socket);
663 g_io_add_watch(lirc_channel, G_IO_IN, lirc_event, NULL);
664 }
665 #endif
667 if (!pipe(sigwinch_pipes) &&
668 !fcntl(sigwinch_pipes[1], F_SETFL, O_NONBLOCK)) {
669 sigwinch_channel = g_io_channel_unix_new(sigwinch_pipes[0]);
670 g_io_add_watch(sigwinch_channel, G_IO_IN, sigwinch_event, NULL);
671 }
672 else {
673 perror("sigwinch pipe creation failed");
674 exit(EXIT_FAILURE);
675 }
677 /* attempt to connect */
678 reconnect_source_id = g_timeout_add(1, timer_reconnect, NULL);
680 auto_update_timer();
682 #ifndef NCMPC_MINI
683 check_key_bindings_source_id = g_timeout_add(10000, timer_check_key_bindings, NULL);
684 #endif
686 screen_paint(mpd);
688 g_main_loop_run(main_loop);
690 /* cleanup */
692 cancel_seek_timer();
694 disable_update_timer();
696 if (reconnect_source_id != 0)
697 g_source_remove(reconnect_source_id);
699 #ifndef NCMPC_MINI
700 if (check_key_bindings_source_id != 0)
701 g_source_remove(check_key_bindings_source_id);
702 #endif
704 g_main_loop_unref(main_loop);
705 g_io_channel_unref(keyboard_channel);
706 g_io_channel_unref(sigwinch_channel);
707 close(sigwinch_pipes[0]);
708 close(sigwinch_pipes[1]);
710 #ifdef ENABLE_LIRC
711 if (lirc_socket >= 0)
712 g_io_channel_unref(lirc_channel);
713 ncmpc_lirc_close();
714 #endif
716 exit_and_cleanup();
718 #ifdef ENABLE_LYRICS_SCREEN
719 lyrics_deinit();
720 #endif
722 ncu_deinit();
723 options_deinit();
725 return 0;
726 }