1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2009 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
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.
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.
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 <signal.h>
52 #include <string.h>
54 #ifdef ENABLE_LOCALE
55 #include <locale.h>
56 #endif
58 /* time between mpd updates [s] */
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 static char title[BUFSIZE];
76 char tmp[BUFSIZE];
77 struct mpd_status *status = NULL;
78 const struct mpd_song *song = NULL;
80 if (mpd) {
81 status = mpd->status;
82 song = mpd->song;
83 }
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 if (strncmp(title, tmp, BUFSIZE)) {
92 g_strlcpy(title, tmp, BUFSIZE);
93 set_xterm_title("%s", title);
94 }
95 }
96 #endif
98 static void
99 exit_and_cleanup(void)
100 {
101 screen_exit();
102 #ifndef NCMPC_MINI
103 set_xterm_title("");
104 #endif
105 printf("\n");
107 if (mpd) {
108 mpdclient_disconnect(mpd);
109 mpdclient_free(mpd);
110 }
111 }
113 static void
114 catch_sigint(G_GNUC_UNUSED int sig)
115 {
116 g_main_loop_quit(main_loop);
117 }
120 static void
121 catch_sigcont(G_GNUC_UNUSED int sig)
122 {
123 screen_resize(mpd);
124 }
126 void
127 sigstop(void)
128 {
129 def_prog_mode(); /* save the tty modes */
130 endwin(); /* end curses mode temporarily */
131 kill(0, SIGSTOP); /* issue SIGSTOP */
132 }
134 static guint timer_sigwinch_id;
136 static gboolean
137 timer_sigwinch(G_GNUC_UNUSED gpointer data)
138 {
139 /* the following causes the screen to flicker. There might be
140 better solutions, but I believe it isn't all that
141 important. */
143 endwin();
144 refresh();
145 screen_resize(mpd);
147 return FALSE;
148 }
150 static void
151 catch_sigwinch(G_GNUC_UNUSED int sig)
152 {
153 if (timer_sigwinch_id != 0)
154 g_source_remove(timer_sigwinch_id);
156 timer_sigwinch_id = g_timeout_add(100, timer_sigwinch, NULL);
157 }
159 static void
160 idle_callback(enum mpd_error error,
161 G_GNUC_UNUSED enum mpd_server_error server_error,
162 const char *message, enum mpd_idle events,
163 G_GNUC_UNUSED void *ctx);
165 static gboolean
166 timer_mpd_update(gpointer data);
168 static void
169 enable_update_timer(void)
170 {
171 if (update_source_id != 0)
172 return;
174 update_source_id = g_timeout_add(update_interval,
175 timer_mpd_update, NULL);
176 }
178 static void
179 disable_update_timer(void)
180 {
181 if (update_source_id == 0)
182 return;
184 g_source_remove(update_source_id);
185 update_source_id = 0;
186 }
188 static bool
189 should_enable_update_timer(void)
190 {
191 return (mpdclient_is_connected(mpd) &&
192 (mpd->source == NULL || /* "idle" not supported */
193 (mpd->status != NULL &&
194 mpd_status_get_state(mpd->status) == MPD_STATE_PLAY)))
195 #ifndef NCMPC_MINI
196 || options.display_time
197 #endif
198 ;
199 }
201 static void
202 auto_update_timer(void)
203 {
204 if (should_enable_update_timer())
205 enable_update_timer();
206 else
207 disable_update_timer();
208 }
210 static void
211 check_reconnect(void);
213 static void
214 do_mpd_update(void)
215 {
216 if (mpdclient_is_connected(mpd))
217 mpdclient_update(mpd);
219 #ifndef NCMPC_MINI
220 if (options.enable_xterm_title)
221 update_xterm_title();
222 #endif
224 screen_update(mpd);
225 mpd->events = 0;
227 mpdclient_put_connection(mpd);
228 check_reconnect();
229 }
231 /**
232 * This timer is installed when the connection to the MPD server is
233 * broken. It tries to recover by reconnecting periodically.
234 */
235 static gboolean
236 timer_reconnect(G_GNUC_UNUSED gpointer data)
237 {
238 bool success;
239 struct mpd_connection *connection;
241 assert(!mpdclient_is_connected(mpd));
243 reconnect_source_id = 0;
245 screen_status_printf(_("Connecting to %s... [Press %s to abort]"),
246 options.host, get_key_names(CMD_QUIT,0) );
247 doupdate();
249 mpdclient_disconnect(mpd);
250 success = mpdclient_connect(mpd,
251 options.host, options.port,
252 1.5,
253 options.password);
254 if (!success) {
255 /* try again in 5 seconds */
256 reconnect_source_id = g_timeout_add(5000,
257 timer_reconnect, NULL);
258 return FALSE;
259 }
261 connection = mpdclient_get_connection(mpd);
263 #ifndef NCMPC_MINI
264 /* quit if mpd is pre 0.11.0 - song id not supported by mpd */
265 if (mpd_connection_cmp_server_version(connection, 0, 12, 0) < 0) {
266 const unsigned *version =
267 mpd_connection_get_server_version(connection);
268 screen_status_printf(_("Error: MPD version %d.%d.%d is to old (%s needed)"),
269 version[0], version[1], version[2],
270 "0.12.0");
271 mpdclient_disconnect(mpd);
272 doupdate();
274 /* try again after 30 seconds */
275 reconnect_source_id = g_timeout_add(30000,
276 timer_reconnect, NULL);
277 return FALSE;
278 }
279 #endif
281 if (mpd_connection_cmp_server_version(connection,
282 0, 14, 0) >= 0)
283 mpd->source = mpd_glib_new(connection,
284 idle_callback, mpd);
286 screen_status_printf(_("Connected to %s"),
287 options.host != NULL
288 ? options.host : "localhost");
289 doupdate();
291 /* update immediately */
292 mpd->events = MPD_IDLE_DATABASE|MPD_IDLE_STORED_PLAYLIST|
293 MPD_IDLE_QUEUE|MPD_IDLE_PLAYER|MPD_IDLE_MIXER|MPD_IDLE_OUTPUT|
294 MPD_IDLE_OPTIONS|MPD_IDLE_UPDATE;
296 do_mpd_update();
298 auto_update_timer();
300 return FALSE;
301 }
303 static void
304 check_reconnect(void)
305 {
306 if (!mpdclient_is_connected(mpd) && reconnect_source_id == 0)
307 /* reconnect when the connection is lost */
308 reconnect_source_id = g_timeout_add(1000, timer_reconnect,
309 NULL);
310 }
312 /**
313 * This function is called by the gidle.c library when MPD sends us an
314 * idle event (or when the connectiond dies).
315 */
316 static void
317 idle_callback(enum mpd_error error, enum mpd_server_error server_error,
318 const char *message, enum mpd_idle events,
319 void *ctx)
320 {
321 struct mpdclient *c = ctx;
322 struct mpd_connection *connection;
324 c->idle = false;
326 connection = mpdclient_get_connection(c);
327 assert(connection != NULL);
329 if (error != MPD_ERROR_SUCCESS) {
330 char *allocated;
332 if (error == MPD_ERROR_SERVER &&
333 server_error == MPD_SERVER_ERROR_UNKNOWN_CMD) {
334 /* the "idle" command is not supported - fall
335 back to timer based polling */
336 mpd_glib_free(c->source);
337 c->source = NULL;
338 auto_update_timer();
339 return;
340 }
342 if (error == MPD_ERROR_SERVER)
343 message = allocated = utf8_to_locale(message);
344 else
345 allocated = NULL;
346 screen_status_message(message);
347 g_free(allocated);
348 screen_bell();
349 doupdate();
351 mpdclient_disconnect(c);
352 reconnect_source_id = g_timeout_add(1000, timer_reconnect,
353 NULL);
354 return;
355 }
357 c->events |= events;
358 mpdclient_update(c);
360 #ifndef NCMPC_MINI
361 if (options.enable_xterm_title)
362 update_xterm_title();
363 #endif
365 screen_update(mpd);
366 c->events = 0;
368 mpdclient_put_connection(c);
369 check_reconnect();
370 auto_update_timer();
371 }
373 static gboolean
374 timer_mpd_update(G_GNUC_UNUSED gpointer data)
375 {
376 do_mpd_update();
378 if (should_enable_update_timer())
379 return true;
380 else {
381 update_source_id = 0;
382 return false;
383 }
384 }
386 void begin_input_event(void)
387 {
388 }
390 void end_input_event(void)
391 {
392 screen_update(mpd);
393 mpd->events = 0;
395 mpdclient_put_connection(mpd);
396 check_reconnect();
397 auto_update_timer();
398 }
400 int do_input_event(command_t cmd)
401 {
402 if (cmd == CMD_QUIT) {
403 g_main_loop_quit(main_loop);
404 return -1;
405 }
407 screen_cmd(mpd, cmd);
409 if (cmd == CMD_VOLUME_UP || cmd == CMD_VOLUME_DOWN)
410 /* make sure we don't update the volume yet */
411 disable_update_timer();
413 return 0;
414 }
416 static gboolean
417 keyboard_event(G_GNUC_UNUSED GIOChannel *source,
418 G_GNUC_UNUSED GIOCondition condition,
419 G_GNUC_UNUSED gpointer data)
420 {
421 command_t cmd;
423 begin_input_event();
425 if ((cmd=get_keyboard_command()) != CMD_NONE)
426 if (do_input_event(cmd) != 0)
427 return FALSE;
429 end_input_event();
430 return TRUE;
431 }
433 #ifndef NCMPC_MINI
434 /**
435 * Check the configured key bindings for errors, and display a status
436 * message every 10 seconds.
437 */
438 static gboolean
439 timer_check_key_bindings(G_GNUC_UNUSED gpointer data)
440 {
441 char buf[256];
442 #ifdef ENABLE_KEYDEF_SCREEN
443 char comment[64];
444 #endif
445 gboolean key_error;
447 key_error = check_key_bindings(NULL, buf, sizeof(buf));
448 if (!key_error) {
449 /* no error: disable this timer for the rest of this
450 process */
451 check_key_bindings_source_id = 0;
452 return FALSE;
453 }
455 #ifdef ENABLE_KEYDEF_SCREEN
456 g_strchomp(buf);
457 g_strlcat(buf, " (", sizeof(buf));
458 /* to translators: a key was bound twice in the key editor,
459 and this is a hint for the user what to press to correct
460 that */
461 g_snprintf(comment, sizeof(comment), _("press %s for the key editor"),
462 get_key_names(CMD_SCREEN_KEYDEF, 0));
463 g_strlcat(buf, comment, sizeof(buf));
464 g_strlcat(buf, ")", sizeof(buf));
465 #endif
467 screen_status_printf("%s", buf);
469 doupdate();
470 return TRUE;
471 }
472 #endif
474 int
475 main(int argc, const char *argv[])
476 {
477 struct sigaction act;
478 #ifdef ENABLE_LOCALE
479 const char *charset = NULL;
480 #endif
481 GIOChannel *keyboard_channel;
482 #ifdef ENABLE_LIRC
483 int lirc_socket;
484 GIOChannel *lirc_channel = NULL;
485 #endif
487 #ifdef ENABLE_LOCALE
488 /* time and date formatting */
489 setlocale(LC_TIME,"");
490 /* care about sorting order etc */
491 setlocale(LC_COLLATE,"");
492 /* charset */
493 setlocale(LC_CTYPE,"");
494 /* initialize charset conversions */
495 charset = charset_init();
497 /* initialize i18n support */
498 #endif
500 #ifdef ENABLE_NLS
501 setlocale(LC_MESSAGES, "");
502 bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
503 #ifdef ENABLE_LOCALE
504 bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
505 #endif
506 textdomain(GETTEXT_PACKAGE);
507 #endif
509 /* initialize options */
510 options_init();
512 /* parse command line options - 1 pass get configuration files */
513 options_parse(argc, argv);
515 #ifndef NCMPC_MINI
516 /* read configuration */
517 read_configuration();
519 /* check key bindings */
520 check_key_bindings(NULL, NULL, 0);
521 #endif
523 /* parse command line options - 2 pass */
524 options_parse(argc, argv);
526 /* setup signal behavior - SIGINT */
527 sigemptyset(&act.sa_mask);
528 act.sa_flags = 0;
529 act.sa_handler = catch_sigint;
530 if (sigaction(SIGINT, &act, NULL) < 0) {
531 perror("signal");
532 exit(EXIT_FAILURE);
533 }
535 /* setup signal behavior - SIGTERM */
537 act.sa_handler = catch_sigint;
538 if (sigaction(SIGTERM, &act, NULL) < 0) {
539 perror("sigaction()");
540 exit(EXIT_FAILURE);
541 }
543 /* setup signal behavior - SIGCONT */
545 act.sa_handler = catch_sigcont;
546 if (sigaction(SIGCONT, &act, NULL) < 0) {
547 perror("sigaction(SIGCONT)");
548 exit(EXIT_FAILURE);
549 }
551 /* setup signal behaviour - SIGHUP*/
553 act.sa_handler = catch_sigint;
554 if (sigaction(SIGHUP, &act, NULL) < 0) {
555 perror("sigaction(SIGHUP)");
556 exit(EXIT_FAILURE);
557 }
559 /* setup SIGWINCH */
561 act.sa_flags = SA_RESTART;
562 act.sa_handler = catch_sigwinch;
563 if (sigaction(SIGWINCH, &act, NULL) < 0) {
564 perror("sigaction(SIGWINCH)");
565 exit(EXIT_FAILURE);
566 }
568 /* ignore SIGPIPE */
570 act.sa_handler = SIG_IGN;
571 if (sigaction(SIGPIPE, &act, NULL) < 0) {
572 perror("sigaction(SIGPIPE)");
573 exit(EXIT_FAILURE);
574 }
576 ncu_init();
578 #ifdef ENABLE_LYRICS_SCREEN
579 lyrics_init();
580 #endif
582 /* create mpdclient instance */
583 mpd = mpdclient_new();
585 /* initialize curses */
586 screen_init(mpd);
588 /* the main loop */
589 main_loop = g_main_loop_new(NULL, FALSE);
591 /* watch out for keyboard input */
592 keyboard_channel = g_io_channel_unix_new(STDIN_FILENO);
593 g_io_add_watch(keyboard_channel, G_IO_IN, keyboard_event, NULL);
595 #ifdef ENABLE_LIRC
596 /* watch out for lirc input */
597 lirc_socket = ncmpc_lirc_open();
598 if (lirc_socket >= 0) {
599 lirc_channel = g_io_channel_unix_new(lirc_socket);
600 g_io_add_watch(lirc_channel, G_IO_IN, lirc_event, NULL);
601 }
602 #endif
604 /* attempt to connect */
605 reconnect_source_id = g_timeout_add(1, timer_reconnect, NULL);
607 auto_update_timer();
609 #ifndef NCMPC_MINI
610 check_key_bindings_source_id = g_timeout_add(10000, timer_check_key_bindings, NULL);
611 #endif
613 screen_paint(mpd);
615 g_main_loop_run(main_loop);
617 /* cleanup */
619 cancel_seek_timer();
621 disable_update_timer();
623 if (reconnect_source_id != 0)
624 g_source_remove(reconnect_source_id);
626 #ifndef NCMPC_MINI
627 if (check_key_bindings_source_id != 0)
628 g_source_remove(check_key_bindings_source_id);
629 #endif
631 g_main_loop_unref(main_loop);
632 g_io_channel_unref(keyboard_channel);
634 #ifdef ENABLE_LIRC
635 if (lirc_socket >= 0)
636 g_io_channel_unref(lirc_channel);
637 ncmpc_lirc_close();
638 #endif
640 exit_and_cleanup();
642 #ifdef ENABLE_LYRICS_SCREEN
643 lyrics_deinit();
644 #endif
646 ncu_deinit();
647 options_deinit();
649 return 0;
650 }