lyrics: lyrics area regex fix for LyricWikia
[ncmpc.git] / src / main.c
blob044221024ff11bf1f78947a4ebe7e402edb8670b
1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2010 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.
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)
77 struct mpd_status *status = NULL;
78 const struct mpd_song *song = NULL;
79 if (mpd) {
80 status = mpd->status;
81 song = mpd->song;
84 char tmp[BUFSIZE];
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 static char title[BUFSIZE];
92 if (strncmp(title, tmp, BUFSIZE)) {
93 g_strlcpy(title, tmp, BUFSIZE);
94 set_xterm_title("%s", title);
97 #endif
99 static void
100 exit_and_cleanup(void)
102 screen_exit();
103 #ifndef NCMPC_MINI
104 set_xterm_title("");
105 #endif
106 printf("\n");
108 if (mpd) {
109 mpdclient_disconnect(mpd);
110 mpdclient_free(mpd);
114 #ifndef WIN32
115 static void
116 catch_sigint(gcc_unused int sig)
118 g_main_loop_quit(main_loop);
122 static void
123 catch_sigcont(gcc_unused int sig)
125 char irrelevant = 'a';
126 if (1 != write(sigwinch_pipes[1], &irrelevant, 1))
127 exit(EXIT_FAILURE);
130 void
131 sigstop(void)
133 def_prog_mode(); /* save the tty modes */
134 endwin(); /* end curses mode temporarily */
135 kill(0, SIGSTOP); /* issue SIGSTOP */
138 static gboolean
139 sigwinch_event(gcc_unused GIOChannel *source,
140 gcc_unused GIOCondition condition, gcc_unused gpointer data)
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;
153 static void
154 catch_sigwinch(gcc_unused int sig)
156 char irrelevant = 'a';
157 if (1 != write(sigwinch_pipes[1], &irrelevant, 1))
158 exit(EXIT_FAILURE);
160 #endif /* WIN32 */
162 static void
163 idle_callback(enum mpd_error error,
164 gcc_unused enum mpd_server_error server_error,
165 const char *message, enum mpd_idle events,
166 gcc_unused void *ctx);
168 static gboolean
169 timer_mpd_update(gpointer data);
171 static void
172 enable_update_timer(void)
174 if (update_source_id != 0)
175 return;
177 update_source_id = g_timeout_add(update_interval,
178 timer_mpd_update, NULL);
181 static void
182 disable_update_timer(void)
184 if (update_source_id == 0)
185 return;
187 g_source_remove(update_source_id);
188 update_source_id = 0;
191 static bool
192 should_enable_update_timer(void)
194 return (mpdclient_is_connected(mpd) &&
195 (mpd->source == NULL || /* "idle" not supported */
196 (mpd->status != NULL &&
197 mpd_status_get_state(mpd->status) == MPD_STATE_PLAY)))
198 #ifndef NCMPC_MINI
199 || options.display_time
200 #endif
204 static void
205 auto_update_timer(void)
207 if (should_enable_update_timer())
208 enable_update_timer();
209 else
210 disable_update_timer();
213 static void
214 check_reconnect(void);
216 static void
217 do_mpd_update(void)
219 if (mpdclient_is_connected(mpd) &&
220 (mpd->source == NULL || mpd->events != 0 ||
221 (mpd->status != NULL &&
222 mpd_status_get_state(mpd->status) == MPD_STATE_PLAY)))
223 mpdclient_update(mpd);
225 #ifndef NCMPC_MINI
226 if (options.enable_xterm_title)
227 update_xterm_title();
228 #endif
230 screen_update(mpd);
231 mpd->events = 0;
233 mpdclient_put_connection(mpd);
234 check_reconnect();
237 #if LIBMPDCLIENT_CHECK_VERSION(2,4,0)
239 static char *
240 settings_name(const struct mpd_settings *settings)
242 const char *host = mpd_settings_get_host(settings);
243 if (host == NULL)
244 host = _("unknown");
246 if (host[0] == '/')
247 return g_strdup(host);
249 unsigned port = mpd_settings_get_port(settings);
250 if (port == 0 || port == 6600)
251 return g_strdup(host);
253 return g_strdup_printf("%s:%u", host, port);
256 #endif
258 static char *
259 default_settings_name(void)
261 #if LIBMPDCLIENT_CHECK_VERSION(2,4,0)
262 struct mpd_settings *settings =
263 mpd_settings_new(options.host, options.port, 0,
264 NULL, options.password);
265 if (settings == NULL)
266 return g_strdup(_("unknown"));
268 char *name = settings_name(settings);
269 mpd_settings_free(settings);
271 return name;
272 #else
274 * localhost is actually not correct, we only know that
275 * mpd_connection_new() has connected to the "default host".
277 const char *name = options.host ?: "localhost";
278 return g_strdup(name);
279 #endif
283 * This timer is installed when the connection to the MPD server is
284 * broken. It tries to recover by reconnecting periodically.
286 static gboolean
287 timer_reconnect(gcc_unused gpointer data)
289 assert(!mpdclient_is_connected(mpd));
291 reconnect_source_id = 0;
293 char *name = default_settings_name();
294 screen_status_printf(_("Connecting to %s... [Press %s to abort]"),
295 name, get_key_names(CMD_QUIT, false));
296 g_free(name);
297 doupdate();
299 mpdclient_disconnect(mpd);
300 if (!mpdclient_connect(mpd, options.host, options.port,
301 options.timeout_ms,
302 options.password)) {
303 /* try again in 5 seconds */
304 reconnect_source_id = g_timeout_add(5000,
305 timer_reconnect, NULL);
306 return FALSE;
309 struct mpd_connection *connection = mpdclient_get_connection(mpd);
311 #ifndef NCMPC_MINI
312 /* quit if mpd is pre 0.14 - song id not supported by mpd */
313 if (mpd_connection_cmp_server_version(connection, 0, 16, 0) < 0) {
314 const unsigned *version =
315 mpd_connection_get_server_version(connection);
316 screen_status_printf(_("Error: MPD version %d.%d.%d is too old (%s needed)"),
317 version[0], version[1], version[2],
318 "0.16.0");
319 mpdclient_disconnect(mpd);
320 doupdate();
322 /* try again after 30 seconds */
323 reconnect_source_id = g_timeout_add(30000,
324 timer_reconnect, NULL);
325 return FALSE;
327 #endif
329 mpd->source = mpd_glib_new(connection,
330 idle_callback, mpd);
332 screen_status_clear_message();
333 doupdate();
335 /* update immediately */
336 mpd->events = MPD_IDLE_ALL;
338 do_mpd_update();
340 auto_update_timer();
342 return FALSE;
345 static void
346 check_reconnect(void)
348 if (!mpdclient_is_connected(mpd) && reconnect_source_id == 0)
349 /* reconnect when the connection is lost */
350 reconnect_source_id = g_timeout_add(1000, timer_reconnect,
351 NULL);
355 * This function is called by the gidle.c library when MPD sends us an
356 * idle event (or when the connection dies).
358 static void
359 idle_callback(enum mpd_error error, enum mpd_server_error server_error,
360 const char *message, enum mpd_idle events,
361 void *ctx)
363 struct mpdclient *c = ctx;
365 c->idle = false;
367 assert(mpdclient_is_connected(c));
369 if (error != MPD_ERROR_SUCCESS) {
370 if (error == MPD_ERROR_SERVER &&
371 server_error == MPD_SERVER_ERROR_UNKNOWN_CMD) {
372 /* the "idle" command is not supported - fall
373 back to timer based polling */
374 mpd_glib_free(c->source);
375 c->source = NULL;
376 auto_update_timer();
377 return;
380 char *allocated;
381 if (error == MPD_ERROR_SERVER)
382 message = allocated = utf8_to_locale(message);
383 else
384 allocated = NULL;
385 screen_status_message(message);
386 g_free(allocated);
387 screen_bell();
388 doupdate();
390 mpdclient_disconnect(c);
391 screen_update(mpd);
392 reconnect_source_id = g_timeout_add(1000, timer_reconnect,
393 NULL);
394 return;
397 c->events |= events;
398 mpdclient_update(c);
400 #ifndef NCMPC_MINI
401 if (options.enable_xterm_title)
402 update_xterm_title();
403 #endif
405 screen_update(mpd);
406 c->events = 0;
408 mpdclient_put_connection(c);
409 check_reconnect();
410 auto_update_timer();
413 static gboolean
414 timer_mpd_update(gcc_unused gpointer data)
416 do_mpd_update();
418 if (should_enable_update_timer())
419 return true;
420 else {
421 update_source_id = 0;
422 return false;
426 void begin_input_event(void)
430 void end_input_event(void)
432 screen_update(mpd);
433 mpd->events = 0;
435 mpdclient_put_connection(mpd);
436 check_reconnect();
437 auto_update_timer();
440 int do_input_event(command_t cmd)
442 if (cmd == CMD_QUIT) {
443 g_main_loop_quit(main_loop);
444 return -1;
447 screen_cmd(mpd, cmd);
449 if (cmd == CMD_VOLUME_UP || cmd == CMD_VOLUME_DOWN)
450 /* make sure we don't update the volume yet */
451 disable_update_timer();
453 return 0;
456 static gboolean
457 keyboard_event(gcc_unused GIOChannel *source,
458 gcc_unused GIOCondition condition,
459 gcc_unused gpointer data)
461 begin_input_event();
463 command_t cmd = get_keyboard_command();
464 if (cmd != CMD_NONE)
465 if (do_input_event(cmd) != 0)
466 return FALSE;
468 end_input_event();
469 return TRUE;
472 #ifndef NCMPC_MINI
474 * Check the configured key bindings for errors, and display a status
475 * message every 10 seconds.
477 static gboolean
478 timer_check_key_bindings(gcc_unused gpointer data)
480 char buf[256];
482 if (check_key_bindings(NULL, buf, sizeof(buf))) {
483 /* no error: disable this timer for the rest of this
484 process */
485 check_key_bindings_source_id = 0;
486 return FALSE;
489 #ifdef ENABLE_KEYDEF_SCREEN
490 g_strchomp(buf);
491 g_strlcat(buf, " (", sizeof(buf));
492 /* to translators: a key was bound twice in the key editor,
493 and this is a hint for the user what to press to correct
494 that */
495 char comment[64];
496 g_snprintf(comment, sizeof(comment), _("press %s for the key editor"),
497 get_key_names(CMD_SCREEN_KEYDEF, false));
498 g_strlcat(buf, comment, sizeof(buf));
499 g_strlcat(buf, ")", sizeof(buf));
500 #endif
502 screen_status_printf("%s", buf);
504 doupdate();
505 return TRUE;
507 #endif
510 main(int argc, const char *argv[])
512 #ifdef ENABLE_LOCALE
513 #ifndef ENABLE_NLS
514 gcc_unused
515 #endif
516 const char *charset = NULL;
517 /* time and date formatting */
518 setlocale(LC_TIME,"");
519 /* care about sorting order etc */
520 setlocale(LC_COLLATE,"");
521 /* charset */
522 setlocale(LC_CTYPE,"");
523 /* initialize charset conversions */
524 charset = charset_init();
526 /* initialize i18n support */
527 #endif
529 #ifdef ENABLE_NLS
530 setlocale(LC_MESSAGES, "");
531 bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
532 #ifdef ENABLE_LOCALE
533 bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
534 #endif
535 textdomain(GETTEXT_PACKAGE);
536 #endif
538 /* initialize options */
539 options_init();
541 /* parse command line options - 1 pass get configuration files */
542 options_parse(argc, argv);
544 #ifndef NCMPC_MINI
545 /* read configuration */
546 read_configuration();
548 /* check key bindings */
549 check_key_bindings(NULL, NULL, 0);
550 #endif
552 /* parse command line options - 2 pass */
553 options_parse(argc, argv);
555 #ifndef WIN32
556 /* setup signal behavior - SIGINT */
557 struct sigaction act;
558 sigemptyset(&act.sa_mask);
559 act.sa_flags = 0;
560 act.sa_handler = catch_sigint;
561 if (sigaction(SIGINT, &act, NULL) < 0) {
562 perror("signal");
563 exit(EXIT_FAILURE);
566 /* setup signal behavior - SIGTERM */
568 act.sa_handler = catch_sigint;
569 if (sigaction(SIGTERM, &act, NULL) < 0) {
570 perror("sigaction()");
571 exit(EXIT_FAILURE);
574 /* setup signal behavior - SIGCONT */
576 act.sa_handler = catch_sigcont;
577 if (sigaction(SIGCONT, &act, NULL) < 0) {
578 perror("sigaction(SIGCONT)");
579 exit(EXIT_FAILURE);
582 /* setup signal behaviour - SIGHUP*/
584 act.sa_handler = catch_sigint;
585 if (sigaction(SIGHUP, &act, NULL) < 0) {
586 perror("sigaction(SIGHUP)");
587 exit(EXIT_FAILURE);
590 /* setup SIGWINCH */
592 act.sa_flags = SA_RESTART;
593 act.sa_handler = catch_sigwinch;
594 if (sigaction(SIGWINCH, &act, NULL) < 0) {
595 perror("sigaction(SIGWINCH)");
596 exit(EXIT_FAILURE);
599 /* ignore SIGPIPE */
601 act.sa_handler = SIG_IGN;
602 if (sigaction(SIGPIPE, &act, NULL) < 0) {
603 perror("sigaction(SIGPIPE)");
604 exit(EXIT_FAILURE);
606 #endif
608 ncu_init();
610 #ifdef ENABLE_LYRICS_SCREEN
611 lyrics_init();
612 #endif
614 /* create mpdclient instance */
615 mpd = mpdclient_new();
617 /* initialize curses */
618 screen_init(mpd);
620 /* the main loop */
621 main_loop = g_main_loop_new(NULL, FALSE);
623 /* watch out for keyboard input */
624 GIOChannel *keyboard_channel = g_io_channel_unix_new(STDIN_FILENO);
625 g_io_add_watch(keyboard_channel, G_IO_IN, keyboard_event, NULL);
627 #ifdef ENABLE_LIRC
628 /* watch out for lirc input */
629 int lirc_socket = ncmpc_lirc_open();
630 GIOChannel *lirc_channel = NULL;
631 if (lirc_socket >= 0) {
632 lirc_channel = g_io_channel_unix_new(lirc_socket);
633 g_io_add_watch(lirc_channel, G_IO_IN, lirc_event, NULL);
635 #endif
637 #ifndef WIN32
638 GIOChannel *sigwinch_channel = NULL;
639 if (!pipe(sigwinch_pipes) &&
640 !fcntl(sigwinch_pipes[1], F_SETFL, O_NONBLOCK)) {
641 sigwinch_channel = g_io_channel_unix_new(sigwinch_pipes[0]);
642 g_io_add_watch(sigwinch_channel, G_IO_IN, sigwinch_event, NULL);
644 else {
645 perror("sigwinch pipe creation failed");
646 exit(EXIT_FAILURE);
648 #endif
650 /* attempt to connect */
651 reconnect_source_id = g_timeout_add(1, timer_reconnect, NULL);
653 auto_update_timer();
655 #ifndef NCMPC_MINI
656 check_key_bindings_source_id = g_timeout_add(10000, timer_check_key_bindings, NULL);
657 #endif
659 screen_paint(mpd);
661 g_main_loop_run(main_loop);
663 /* cleanup */
665 cancel_seek_timer();
667 disable_update_timer();
669 if (reconnect_source_id != 0)
670 g_source_remove(reconnect_source_id);
672 #ifndef NCMPC_MINI
673 if (check_key_bindings_source_id != 0)
674 g_source_remove(check_key_bindings_source_id);
675 #endif
677 g_main_loop_unref(main_loop);
678 g_io_channel_unref(keyboard_channel);
679 g_io_channel_unref(sigwinch_channel);
680 close(sigwinch_pipes[0]);
681 close(sigwinch_pipes[1]);
683 #ifdef ENABLE_LIRC
684 if (lirc_socket >= 0)
685 g_io_channel_unref(lirc_channel);
686 ncmpc_lirc_close();
687 #endif
689 exit_and_cleanup();
691 #ifdef ENABLE_LYRICS_SCREEN
692 lyrics_deinit();
693 #endif
695 ncu_deinit();
696 options_deinit();
698 return 0;