1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2009 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 reconnect_source_id = g_timeout_add(1000, timer_reconnect,
356 NULL);
357 return;
358 }
360 c->events |= events;
361 mpdclient_update(c);
363 #ifndef NCMPC_MINI
364 if (options.enable_xterm_title)
365 update_xterm_title();
366 #endif
368 screen_update(mpd);
369 c->events = 0;
371 mpdclient_put_connection(c);
372 check_reconnect();
373 auto_update_timer();
374 }
376 static gboolean
377 timer_mpd_update(G_GNUC_UNUSED gpointer data)
378 {
379 do_mpd_update();
381 if (should_enable_update_timer())
382 return true;
383 else {
384 update_source_id = 0;
385 return false;
386 }
387 }
389 void begin_input_event(void)
390 {
391 }
393 void end_input_event(void)
394 {
395 screen_update(mpd);
396 mpd->events = 0;
398 mpdclient_put_connection(mpd);
399 check_reconnect();
400 auto_update_timer();
401 }
403 int do_input_event(command_t cmd)
404 {
405 if (cmd == CMD_QUIT) {
406 g_main_loop_quit(main_loop);
407 return -1;
408 }
410 screen_cmd(mpd, cmd);
412 if (cmd == CMD_VOLUME_UP || cmd == CMD_VOLUME_DOWN)
413 /* make sure we don't update the volume yet */
414 disable_update_timer();
416 return 0;
417 }
419 static gboolean
420 keyboard_event(G_GNUC_UNUSED GIOChannel *source,
421 G_GNUC_UNUSED GIOCondition condition,
422 G_GNUC_UNUSED gpointer data)
423 {
424 command_t cmd;
426 begin_input_event();
428 if ((cmd=get_keyboard_command()) != CMD_NONE)
429 if (do_input_event(cmd) != 0)
430 return FALSE;
432 end_input_event();
433 return TRUE;
434 }
436 #ifndef NCMPC_MINI
437 /**
438 * Check the configured key bindings for errors, and display a status
439 * message every 10 seconds.
440 */
441 static gboolean
442 timer_check_key_bindings(G_GNUC_UNUSED gpointer data)
443 {
444 char buf[256];
445 #ifdef ENABLE_KEYDEF_SCREEN
446 char comment[64];
447 #endif
448 gboolean key_error;
450 key_error = check_key_bindings(NULL, buf, sizeof(buf));
451 if (!key_error) {
452 /* no error: disable this timer for the rest of this
453 process */
454 check_key_bindings_source_id = 0;
455 return FALSE;
456 }
458 #ifdef ENABLE_KEYDEF_SCREEN
459 g_strchomp(buf);
460 g_strlcat(buf, " (", sizeof(buf));
461 /* to translators: a key was bound twice in the key editor,
462 and this is a hint for the user what to press to correct
463 that */
464 g_snprintf(comment, sizeof(comment), _("press %s for the key editor"),
465 get_key_names(CMD_SCREEN_KEYDEF, 0));
466 g_strlcat(buf, comment, sizeof(buf));
467 g_strlcat(buf, ")", sizeof(buf));
468 #endif
470 screen_status_printf("%s", buf);
472 doupdate();
473 return TRUE;
474 }
475 #endif
477 int
478 main(int argc, const char *argv[])
479 {
480 struct sigaction act;
481 #ifdef ENABLE_LOCALE
482 const char *charset = NULL;
483 #endif
484 GIOChannel *keyboard_channel;
485 #ifdef ENABLE_LIRC
486 int lirc_socket;
487 GIOChannel *lirc_channel = NULL;
488 #endif
490 #ifdef ENABLE_LOCALE
491 /* time and date formatting */
492 setlocale(LC_TIME,"");
493 /* care about sorting order etc */
494 setlocale(LC_COLLATE,"");
495 /* charset */
496 setlocale(LC_CTYPE,"");
497 /* initialize charset conversions */
498 charset = charset_init();
500 /* initialize i18n support */
501 #endif
503 #ifdef ENABLE_NLS
504 setlocale(LC_MESSAGES, "");
505 bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
506 #ifdef ENABLE_LOCALE
507 bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
508 #endif
509 textdomain(GETTEXT_PACKAGE);
510 #endif
512 /* initialize options */
513 options_init();
515 /* parse command line options - 1 pass get configuration files */
516 options_parse(argc, argv);
518 #ifndef NCMPC_MINI
519 /* read configuration */
520 read_configuration();
522 /* check key bindings */
523 check_key_bindings(NULL, NULL, 0);
524 #endif
526 /* parse command line options - 2 pass */
527 options_parse(argc, argv);
529 /* setup signal behavior - SIGINT */
530 sigemptyset(&act.sa_mask);
531 act.sa_flags = 0;
532 act.sa_handler = catch_sigint;
533 if (sigaction(SIGINT, &act, NULL) < 0) {
534 perror("signal");
535 exit(EXIT_FAILURE);
536 }
538 /* setup signal behavior - SIGTERM */
540 act.sa_handler = catch_sigint;
541 if (sigaction(SIGTERM, &act, NULL) < 0) {
542 perror("sigaction()");
543 exit(EXIT_FAILURE);
544 }
546 /* setup signal behavior - SIGCONT */
548 act.sa_handler = catch_sigcont;
549 if (sigaction(SIGCONT, &act, NULL) < 0) {
550 perror("sigaction(SIGCONT)");
551 exit(EXIT_FAILURE);
552 }
554 /* setup signal behaviour - SIGHUP*/
556 act.sa_handler = catch_sigint;
557 if (sigaction(SIGHUP, &act, NULL) < 0) {
558 perror("sigaction(SIGHUP)");
559 exit(EXIT_FAILURE);
560 }
562 /* setup SIGWINCH */
564 act.sa_flags = SA_RESTART;
565 act.sa_handler = catch_sigwinch;
566 if (sigaction(SIGWINCH, &act, NULL) < 0) {
567 perror("sigaction(SIGWINCH)");
568 exit(EXIT_FAILURE);
569 }
571 /* ignore SIGPIPE */
573 act.sa_handler = SIG_IGN;
574 if (sigaction(SIGPIPE, &act, NULL) < 0) {
575 perror("sigaction(SIGPIPE)");
576 exit(EXIT_FAILURE);
577 }
579 ncu_init();
581 #ifdef ENABLE_LYRICS_SCREEN
582 lyrics_init();
583 #endif
585 /* create mpdclient instance */
586 mpd = mpdclient_new();
588 /* initialize curses */
589 screen_init(mpd);
591 /* the main loop */
592 main_loop = g_main_loop_new(NULL, FALSE);
594 /* watch out for keyboard input */
595 keyboard_channel = g_io_channel_unix_new(STDIN_FILENO);
596 g_io_add_watch(keyboard_channel, G_IO_IN, keyboard_event, NULL);
598 #ifdef ENABLE_LIRC
599 /* watch out for lirc input */
600 lirc_socket = ncmpc_lirc_open();
601 if (lirc_socket >= 0) {
602 lirc_channel = g_io_channel_unix_new(lirc_socket);
603 g_io_add_watch(lirc_channel, G_IO_IN, lirc_event, NULL);
604 }
605 #endif
607 /* attempt to connect */
608 reconnect_source_id = g_timeout_add(1, timer_reconnect, NULL);
610 auto_update_timer();
612 #ifndef NCMPC_MINI
613 check_key_bindings_source_id = g_timeout_add(10000, timer_check_key_bindings, NULL);
614 #endif
616 screen_paint(mpd);
618 g_main_loop_run(main_loop);
620 /* cleanup */
622 cancel_seek_timer();
624 disable_update_timer();
626 if (reconnect_source_id != 0)
627 g_source_remove(reconnect_source_id);
629 #ifndef NCMPC_MINI
630 if (check_key_bindings_source_id != 0)
631 g_source_remove(check_key_bindings_source_id);
632 #endif
634 g_main_loop_unref(main_loop);
635 g_io_channel_unref(keyboard_channel);
637 #ifdef ENABLE_LIRC
638 if (lirc_socket >= 0)
639 g_io_channel_unref(lirc_channel);
640 ncmpc_lirc_close();
641 #endif
643 exit_and_cleanup();
645 #ifdef ENABLE_LYRICS_SCREEN
646 lyrics_deinit();
647 #endif
649 ncu_deinit();
650 options_deinit();
652 return 0;
653 }