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 <signal.h>
52 #include <string.h>
54 #ifdef ENABLE_LOCALE
55 #include <locale.h>
56 #endif
58 /* time between mpd updates [ms] */
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 (mpd->source == NULL || mpd->events != 0 ||
218 (mpd->status != NULL &&
219 mpd_status_get_state(mpd->status) == MPD_STATE_PLAY)))
220 mpdclient_update(mpd);
222 #ifndef NCMPC_MINI
223 if (options.enable_xterm_title)
224 update_xterm_title();
225 #endif
227 screen_update(mpd);
228 mpd->events = 0;
230 mpdclient_put_connection(mpd);
231 check_reconnect();
232 }
234 /**
235 * This timer is installed when the connection to the MPD server is
236 * broken. It tries to recover by reconnecting periodically.
237 */
238 static gboolean
239 timer_reconnect(G_GNUC_UNUSED gpointer data)
240 {
241 bool success;
242 struct mpd_connection *connection;
244 assert(!mpdclient_is_connected(mpd));
246 reconnect_source_id = 0;
248 screen_status_printf(_("Connecting to %s... [Press %s to abort]"),
249 options.host, get_key_names(CMD_QUIT,0) );
250 doupdate();
252 mpdclient_disconnect(mpd);
253 success = mpdclient_connect(mpd,
254 options.host, options.port,
255 1.5,
256 options.password);
257 if (!success) {
258 /* try again in 5 seconds */
259 reconnect_source_id = g_timeout_add(5000,
260 timer_reconnect, NULL);
261 return FALSE;
262 }
264 connection = mpdclient_get_connection(mpd);
266 #ifndef NCMPC_MINI
267 /* quit if mpd is pre 0.11.0 - song id not supported by mpd */
268 if (mpd_connection_cmp_server_version(connection, 0, 12, 0) < 0) {
269 const unsigned *version =
270 mpd_connection_get_server_version(connection);
271 screen_status_printf(_("Error: MPD version %d.%d.%d is to old (%s needed)"),
272 version[0], version[1], version[2],
273 "0.12.0");
274 mpdclient_disconnect(mpd);
275 doupdate();
277 /* try again after 30 seconds */
278 reconnect_source_id = g_timeout_add(30000,
279 timer_reconnect, NULL);
280 return FALSE;
281 }
282 #endif
284 if (mpd_connection_cmp_server_version(connection,
285 0, 14, 0) >= 0)
286 mpd->source = mpd_glib_new(connection,
287 idle_callback, mpd);
289 screen_status_printf(_("Connected to %s"),
290 options.host != NULL
291 ? options.host : "localhost");
292 doupdate();
294 /* update immediately */
295 mpd->events = MPD_IDLE_DATABASE|MPD_IDLE_STORED_PLAYLIST|
296 MPD_IDLE_QUEUE|MPD_IDLE_PLAYER|MPD_IDLE_MIXER|MPD_IDLE_OUTPUT|
297 MPD_IDLE_OPTIONS|MPD_IDLE_UPDATE;
299 do_mpd_update();
301 auto_update_timer();
303 return FALSE;
304 }
306 static void
307 check_reconnect(void)
308 {
309 if (!mpdclient_is_connected(mpd) && reconnect_source_id == 0)
310 /* reconnect when the connection is lost */
311 reconnect_source_id = g_timeout_add(1000, timer_reconnect,
312 NULL);
313 }
315 /**
316 * This function is called by the gidle.c library when MPD sends us an
317 * idle event (or when the connectiond dies).
318 */
319 static void
320 idle_callback(enum mpd_error error, enum mpd_server_error server_error,
321 const char *message, enum mpd_idle events,
322 void *ctx)
323 {
324 struct mpdclient *c = ctx;
325 struct mpd_connection *connection;
327 c->idle = false;
329 connection = mpdclient_get_connection(c);
330 assert(connection != NULL);
332 if (error != MPD_ERROR_SUCCESS) {
333 char *allocated;
335 if (error == MPD_ERROR_SERVER &&
336 server_error == MPD_SERVER_ERROR_UNKNOWN_CMD) {
337 /* the "idle" command is not supported - fall
338 back to timer based polling */
339 mpd_glib_free(c->source);
340 c->source = NULL;
341 auto_update_timer();
342 return;
343 }
345 if (error == MPD_ERROR_SERVER)
346 message = allocated = utf8_to_locale(message);
347 else
348 allocated = NULL;
349 screen_status_message(message);
350 g_free(allocated);
351 screen_bell();
352 doupdate();
354 mpdclient_disconnect(c);
355 screen_update(mpd);
356 reconnect_source_id = g_timeout_add(1000, timer_reconnect,
357 NULL);
358 return;
359 }
361 c->events |= events;
362 mpdclient_update(c);
364 #ifndef NCMPC_MINI
365 if (options.enable_xterm_title)
366 update_xterm_title();
367 #endif
369 screen_update(mpd);
370 c->events = 0;
372 mpdclient_put_connection(c);
373 check_reconnect();
374 auto_update_timer();
375 }
377 static gboolean
378 timer_mpd_update(G_GNUC_UNUSED gpointer data)
379 {
380 do_mpd_update();
382 if (should_enable_update_timer())
383 return true;
384 else {
385 update_source_id = 0;
386 return false;
387 }
388 }
390 void begin_input_event(void)
391 {
392 }
394 void end_input_event(void)
395 {
396 screen_update(mpd);
397 mpd->events = 0;
399 mpdclient_put_connection(mpd);
400 check_reconnect();
401 auto_update_timer();
402 }
404 int do_input_event(command_t cmd)
405 {
406 if (cmd == CMD_QUIT) {
407 g_main_loop_quit(main_loop);
408 return -1;
409 }
411 screen_cmd(mpd, cmd);
413 if (cmd == CMD_VOLUME_UP || cmd == CMD_VOLUME_DOWN)
414 /* make sure we don't update the volume yet */
415 disable_update_timer();
417 return 0;
418 }
420 static gboolean
421 keyboard_event(G_GNUC_UNUSED GIOChannel *source,
422 G_GNUC_UNUSED GIOCondition condition,
423 G_GNUC_UNUSED gpointer data)
424 {
425 command_t cmd;
427 begin_input_event();
429 if ((cmd=get_keyboard_command()) != CMD_NONE)
430 if (do_input_event(cmd) != 0)
431 return FALSE;
433 end_input_event();
434 return TRUE;
435 }
437 #ifndef NCMPC_MINI
438 /**
439 * Check the configured key bindings for errors, and display a status
440 * message every 10 seconds.
441 */
442 static gboolean
443 timer_check_key_bindings(G_GNUC_UNUSED gpointer data)
444 {
445 char buf[256];
446 #ifdef ENABLE_KEYDEF_SCREEN
447 char comment[64];
448 #endif
449 gboolean key_error;
451 key_error = check_key_bindings(NULL, buf, sizeof(buf));
452 if (!key_error) {
453 /* no error: disable this timer for the rest of this
454 process */
455 check_key_bindings_source_id = 0;
456 return FALSE;
457 }
459 #ifdef ENABLE_KEYDEF_SCREEN
460 g_strchomp(buf);
461 g_strlcat(buf, " (", sizeof(buf));
462 /* to translators: a key was bound twice in the key editor,
463 and this is a hint for the user what to press to correct
464 that */
465 g_snprintf(comment, sizeof(comment), _("press %s for the key editor"),
466 get_key_names(CMD_SCREEN_KEYDEF, 0));
467 g_strlcat(buf, comment, sizeof(buf));
468 g_strlcat(buf, ")", sizeof(buf));
469 #endif
471 screen_status_printf("%s", buf);
473 doupdate();
474 return TRUE;
475 }
476 #endif
478 int
479 main(int argc, const char *argv[])
480 {
481 struct sigaction act;
482 #ifdef ENABLE_LOCALE
483 const char *charset = NULL;
484 #endif
485 GIOChannel *keyboard_channel;
486 #ifdef ENABLE_LIRC
487 int lirc_socket;
488 GIOChannel *lirc_channel = NULL;
489 #endif
491 #ifdef ENABLE_LOCALE
492 /* time and date formatting */
493 setlocale(LC_TIME,"");
494 /* care about sorting order etc */
495 setlocale(LC_COLLATE,"");
496 /* charset */
497 setlocale(LC_CTYPE,"");
498 /* initialize charset conversions */
499 charset = charset_init();
501 /* initialize i18n support */
502 #endif
504 #ifdef ENABLE_NLS
505 setlocale(LC_MESSAGES, "");
506 bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
507 #ifdef ENABLE_LOCALE
508 bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
509 #endif
510 textdomain(GETTEXT_PACKAGE);
511 #endif
513 /* initialize options */
514 options_init();
516 /* parse command line options - 1 pass get configuration files */
517 options_parse(argc, argv);
519 #ifndef NCMPC_MINI
520 /* read configuration */
521 read_configuration();
523 /* check key bindings */
524 check_key_bindings(NULL, NULL, 0);
525 #endif
527 /* parse command line options - 2 pass */
528 options_parse(argc, argv);
530 /* setup signal behavior - SIGINT */
531 sigemptyset(&act.sa_mask);
532 act.sa_flags = 0;
533 act.sa_handler = catch_sigint;
534 if (sigaction(SIGINT, &act, NULL) < 0) {
535 perror("signal");
536 exit(EXIT_FAILURE);
537 }
539 /* setup signal behavior - SIGTERM */
541 act.sa_handler = catch_sigint;
542 if (sigaction(SIGTERM, &act, NULL) < 0) {
543 perror("sigaction()");
544 exit(EXIT_FAILURE);
545 }
547 /* setup signal behavior - SIGCONT */
549 act.sa_handler = catch_sigcont;
550 if (sigaction(SIGCONT, &act, NULL) < 0) {
551 perror("sigaction(SIGCONT)");
552 exit(EXIT_FAILURE);
553 }
555 /* setup signal behaviour - SIGHUP*/
557 act.sa_handler = catch_sigint;
558 if (sigaction(SIGHUP, &act, NULL) < 0) {
559 perror("sigaction(SIGHUP)");
560 exit(EXIT_FAILURE);
561 }
563 /* setup SIGWINCH */
565 act.sa_flags = SA_RESTART;
566 act.sa_handler = catch_sigwinch;
567 if (sigaction(SIGWINCH, &act, NULL) < 0) {
568 perror("sigaction(SIGWINCH)");
569 exit(EXIT_FAILURE);
570 }
572 /* ignore SIGPIPE */
574 act.sa_handler = SIG_IGN;
575 if (sigaction(SIGPIPE, &act, NULL) < 0) {
576 perror("sigaction(SIGPIPE)");
577 exit(EXIT_FAILURE);
578 }
580 ncu_init();
582 #ifdef ENABLE_LYRICS_SCREEN
583 lyrics_init();
584 #endif
586 /* create mpdclient instance */
587 mpd = mpdclient_new();
589 /* initialize curses */
590 screen_init(mpd);
592 /* the main loop */
593 main_loop = g_main_loop_new(NULL, FALSE);
595 /* watch out for keyboard input */
596 keyboard_channel = g_io_channel_unix_new(STDIN_FILENO);
597 g_io_add_watch(keyboard_channel, G_IO_IN, keyboard_event, NULL);
599 #ifdef ENABLE_LIRC
600 /* watch out for lirc input */
601 lirc_socket = ncmpc_lirc_open();
602 if (lirc_socket >= 0) {
603 lirc_channel = g_io_channel_unix_new(lirc_socket);
604 g_io_add_watch(lirc_channel, G_IO_IN, lirc_event, NULL);
605 }
606 #endif
608 /* attempt to connect */
609 reconnect_source_id = g_timeout_add(1, timer_reconnect, NULL);
611 auto_update_timer();
613 #ifndef NCMPC_MINI
614 check_key_bindings_source_id = g_timeout_add(10000, timer_check_key_bindings, NULL);
615 #endif
617 screen_paint(mpd);
619 g_main_loop_run(main_loop);
621 /* cleanup */
623 cancel_seek_timer();
625 disable_update_timer();
627 if (reconnect_source_id != 0)
628 g_source_remove(reconnect_source_id);
630 #ifndef NCMPC_MINI
631 if (check_key_bindings_source_id != 0)
632 g_source_remove(check_key_bindings_source_id);
633 #endif
635 g_main_loop_unref(main_loop);
636 g_io_channel_unref(keyboard_channel);
638 #ifdef ENABLE_LIRC
639 if (lirc_socket >= 0)
640 g_io_channel_unref(lirc_channel);
641 ncmpc_lirc_close();
642 #endif
644 exit_and_cleanup();
646 #ifdef ENABLE_LYRICS_SCREEN
647 lyrics_deinit();
648 #endif
650 ncu_deinit();
651 options_deinit();
653 return 0;
654 }