Code

8bc4d597a7b83175e8d40a8fb404b8f06ed36f44
[ncmpc.git] / src / main.c
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_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)
76 {
77         struct mpd_status *status = NULL;
78         const struct mpd_song *song = NULL;
79         if (mpd) {
80                 status = mpd->status;
81                 song = mpd->song;
82         }
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);
95         }
96 }
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);
111         }
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         if (1 != write(sigwinch_pipes[1], "", 1))
157                 exit(EXIT_FAILURE);
159 #endif /* WIN32 */
161 static void
162 idle_callback(enum mpd_error error,
163               gcc_unused enum mpd_server_error server_error,
164               const char *message, enum mpd_idle events,
165               gcc_unused void *ctx);
167 static gboolean
168 timer_mpd_update(gpointer data);
170 static void
171 enable_update_timer(void)
173         if (update_source_id != 0)
174                 return;
176         update_source_id = g_timeout_add(update_interval,
177                                          timer_mpd_update, NULL);
180 static void
181 disable_update_timer(void)
183         if (update_source_id == 0)
184                 return;
186         g_source_remove(update_source_id);
187         update_source_id = 0;
190 static bool
191 should_enable_update_timer(void)
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                 ;
203 static void
204 auto_update_timer(void)
206         if (should_enable_update_timer())
207                 enable_update_timer();
208         else
209                 disable_update_timer();
212 static void
213 check_reconnect(void);
215 static void
216 do_mpd_update(void)
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();
236 static char *
237 settings_name(const struct mpd_settings *settings)
239         const char *host = mpd_settings_get_host(settings);
240         if (host == NULL)
241                 host = _("unknown");
243         if (host[0] == '/')
244                 return g_strdup(host);
246         unsigned port = mpd_settings_get_port(settings);
247         if (port == 0 || port == 6600)
248                 return g_strdup(host);
250         return g_strdup_printf("%s:%u", host, port);
253 static char *
254 default_settings_name(void)
256         struct mpd_settings *settings =
257                 mpd_settings_new(options.host, options.port, 0,
258                                  NULL, options.password);
259         if (settings == NULL)
260                 return g_strdup(_("unknown"));
262         char *name = settings_name(settings);
263         mpd_settings_free(settings);
265         return name;
268 /**
269  * This timer is installed when the connection to the MPD server is
270  * broken.  It tries to recover by reconnecting periodically.
271  */
272 static gboolean
273 timer_reconnect(gcc_unused gpointer data)
275         assert(!mpdclient_is_connected(mpd));
277         reconnect_source_id = 0;
279         char *name = default_settings_name();
280         screen_status_printf(_("Connecting to %s...  [Press %s to abort]"),
281                              name, get_key_names(CMD_QUIT, false));
282         g_free(name);
283         doupdate();
285         mpdclient_disconnect(mpd);
286         if (!mpdclient_connect(mpd, options.host, options.port,
287                                options.timeout_ms,
288                                options.password)) {
289                 /* try again in 5 seconds */
290                 reconnect_source_id = g_timeout_add(5000,
291                                                     timer_reconnect, NULL);
292                 return FALSE;
293         }
295         struct mpd_connection *connection = mpdclient_get_connection(mpd);
297 #ifndef NCMPC_MINI
298         /* quit if mpd is pre 0.14 - song id not supported by mpd */
299         if (mpd_connection_cmp_server_version(connection, 0, 16, 0) < 0) {
300                 const unsigned *version =
301                         mpd_connection_get_server_version(connection);
302                 screen_status_printf(_("Error: MPD version %d.%d.%d is too old (%s needed)"),
303                                      version[0], version[1], version[2],
304                                      "0.16.0");
305                 mpdclient_disconnect(mpd);
306                 doupdate();
308                 /* try again after 30 seconds */
309                 reconnect_source_id = g_timeout_add(30000,
310                                                     timer_reconnect, NULL);
311                 return FALSE;
312         }
313 #endif
315         mpd->source = mpd_glib_new(connection,
316                                    idle_callback, mpd);
318         screen_status_clear_message();
319         doupdate();
321         /* update immediately */
322         mpd->events = MPD_IDLE_ALL;
324         do_mpd_update();
326         auto_update_timer();
328         return FALSE;
331 static void
332 check_reconnect(void)
334         if (!mpdclient_is_connected(mpd) && reconnect_source_id == 0)
335                 /* reconnect when the connection is lost */
336                 reconnect_source_id = g_timeout_add(1000, timer_reconnect,
337                                                     NULL);
340 /**
341  * This function is called by the gidle.c library when MPD sends us an
342  * idle event (or when the connection dies).
343  */
344 static void
345 idle_callback(enum mpd_error error, enum mpd_server_error server_error,
346               const char *message, enum mpd_idle events,
347               void *ctx)
349         struct mpdclient *c = ctx;
351         c->idle = false;
353         assert(mpdclient_is_connected(c));
355         if (error != MPD_ERROR_SUCCESS) {
356                 if (error == MPD_ERROR_SERVER &&
357                     server_error == MPD_SERVER_ERROR_UNKNOWN_CMD) {
358                         /* the "idle" command is not supported - fall
359                            back to timer based polling */
360                         mpd_glib_free(c->source);
361                         c->source = NULL;
362                         auto_update_timer();
363                         return;
364                 }
366                 char *allocated;
367                 if (error == MPD_ERROR_SERVER)
368                         message = allocated = utf8_to_locale(message);
369                 else
370                         allocated = NULL;
371                 screen_status_message(message);
372                 g_free(allocated);
373                 screen_bell();
374                 doupdate();
376                 mpdclient_disconnect(c);
377                 screen_update(mpd);
378                 reconnect_source_id = g_timeout_add(1000, timer_reconnect,
379                                                     NULL);
380                 return;
381         }
383         c->events |= events;
384         mpdclient_update(c);
386 #ifndef NCMPC_MINI
387         if (options.enable_xterm_title)
388                 update_xterm_title();
389 #endif
391         screen_update(mpd);
392         c->events = 0;
394         mpdclient_put_connection(c);
395         check_reconnect();
396         auto_update_timer();
399 static gboolean
400 timer_mpd_update(gcc_unused gpointer data)
402         do_mpd_update();
404         if (should_enable_update_timer())
405                 return true;
406         else {
407                 update_source_id = 0;
408                 return false;
409         }
412 void begin_input_event(void)
416 void end_input_event(void)
418         screen_update(mpd);
419         mpd->events = 0;
421         mpdclient_put_connection(mpd);
422         check_reconnect();
423         auto_update_timer();
426 int do_input_event(command_t cmd)
428         if (cmd == CMD_QUIT) {
429                 g_main_loop_quit(main_loop);
430                 return -1;
431         }
433         screen_cmd(mpd, cmd);
435         if (cmd == CMD_VOLUME_UP || cmd == CMD_VOLUME_DOWN)
436                 /* make sure we don't update the volume yet */
437                 disable_update_timer();
439         return 0;
442 static gboolean
443 keyboard_event(gcc_unused GIOChannel *source,
444                gcc_unused GIOCondition condition,
445                gcc_unused gpointer data)
447         begin_input_event();
449         command_t cmd = get_keyboard_command();
450         if (cmd != CMD_NONE)
451                 if (do_input_event(cmd) != 0)
452                         return FALSE;
454         end_input_event();
455         return TRUE;
458 #ifndef NCMPC_MINI
459 /**
460  * Check the configured key bindings for errors, and display a status
461  * message every 10 seconds.
462  */
463 static gboolean
464 timer_check_key_bindings(gcc_unused gpointer data)
466         char buf[256];
468         if (check_key_bindings(NULL, buf, sizeof(buf))) {
469                 /* no error: disable this timer for the rest of this
470                    process */
471                 check_key_bindings_source_id = 0;
472                 return FALSE;
473         }
475 #ifdef ENABLE_KEYDEF_SCREEN
476         g_strchomp(buf);
477         g_strlcat(buf, " (", sizeof(buf));
478         /* to translators: a key was bound twice in the key editor,
479            and this is a hint for the user what to press to correct
480            that */
481         char comment[64];
482         g_snprintf(comment, sizeof(comment), _("press %s for the key editor"),
483                    get_key_names(CMD_SCREEN_KEYDEF, false));
484         g_strlcat(buf, comment, sizeof(buf));
485         g_strlcat(buf, ")", sizeof(buf));
486 #endif
488         screen_status_printf("%s", buf);
490         doupdate();
491         return TRUE;
493 #endif
495 int
496 main(int argc, const char *argv[])
498 #ifdef ENABLE_LOCALE
499 #ifndef ENABLE_NLS
500         gcc_unused
501 #endif
502         const char *charset = NULL;
503         /* time and date formatting */
504         setlocale(LC_TIME,"");
505         /* care about sorting order etc */
506         setlocale(LC_COLLATE,"");
507         /* charset */
508         setlocale(LC_CTYPE,"");
509         /* initialize charset conversions */
510         charset = charset_init();
512         /* initialize i18n support */
513 #endif
515 #ifdef ENABLE_NLS
516         setlocale(LC_MESSAGES, "");
517         bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
518 #ifdef ENABLE_LOCALE
519         bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
520 #endif
521         textdomain(GETTEXT_PACKAGE);
522 #endif
524         /* initialize options */
525         options_init();
527         /* parse command line options - 1 pass get configuration files */
528         options_parse(argc, argv);
530 #ifndef NCMPC_MINI
531         /* read configuration */
532         read_configuration();
534         /* check key bindings */
535         check_key_bindings(NULL, NULL, 0);
536 #endif
538         /* parse command line options - 2 pass */
539         options_parse(argc, argv);
541 #ifndef WIN32
542         /* setup signal behavior - SIGINT */
543         struct sigaction act;
544         sigemptyset(&act.sa_mask);
545         act.sa_flags = 0;
546         act.sa_handler = catch_sigint;
547         if (sigaction(SIGINT, &act, NULL) < 0) {
548                 perror("signal");
549                 exit(EXIT_FAILURE);
550         }
552         /* setup signal behavior - SIGTERM */
554         act.sa_handler = catch_sigint;
555         if (sigaction(SIGTERM, &act, NULL) < 0) {
556                 perror("sigaction()");
557                 exit(EXIT_FAILURE);
558         }
560         /* setup signal behavior - SIGCONT */
562         act.sa_handler = catch_sigcont;
563         if (sigaction(SIGCONT, &act, NULL) < 0) {
564                 perror("sigaction(SIGCONT)");
565                 exit(EXIT_FAILURE);
566         }
568         /* setup signal behaviour - SIGHUP*/
570         act.sa_handler = catch_sigint;
571         if (sigaction(SIGHUP, &act, NULL) < 0) {
572                 perror("sigaction(SIGHUP)");
573                 exit(EXIT_FAILURE);
574         }
576         /* setup SIGWINCH */
578         act.sa_flags = SA_RESTART;
579         act.sa_handler = catch_sigwinch;
580         if (sigaction(SIGWINCH, &act, NULL) < 0) {
581                 perror("sigaction(SIGWINCH)");
582                 exit(EXIT_FAILURE);
583         }
585         /* ignore SIGPIPE */
587         act.sa_handler = SIG_IGN;
588         if (sigaction(SIGPIPE, &act, NULL) < 0) {
589                 perror("sigaction(SIGPIPE)");
590                 exit(EXIT_FAILURE);
591         }
592 #endif
594         ncu_init();
596 #ifdef ENABLE_LYRICS_SCREEN
597         lyrics_init();
598 #endif
600         /* create mpdclient instance */
601         mpd = mpdclient_new();
603         /* initialize curses */
604         screen_init(mpd);
606         /* the main loop */
607         main_loop = g_main_loop_new(NULL, FALSE);
609         /* watch out for keyboard input */
610         GIOChannel *keyboard_channel = g_io_channel_unix_new(STDIN_FILENO);
611         g_io_add_watch(keyboard_channel, G_IO_IN, keyboard_event, NULL);
613 #ifdef ENABLE_LIRC
614         /* watch out for lirc input */
615         int lirc_socket = ncmpc_lirc_open();
616         GIOChannel *lirc_channel = NULL;
617         if (lirc_socket >= 0) {
618                 lirc_channel = g_io_channel_unix_new(lirc_socket);
619                 g_io_add_watch(lirc_channel, G_IO_IN, lirc_event, NULL);
620         }
621 #endif
623 #ifndef WIN32
624         GIOChannel *sigwinch_channel = NULL;
625         if (!pipe(sigwinch_pipes) &&
626                 !fcntl(sigwinch_pipes[1], F_SETFL, O_NONBLOCK)) {
627                 sigwinch_channel = g_io_channel_unix_new(sigwinch_pipes[0]);
628                 g_io_add_watch(sigwinch_channel, G_IO_IN, sigwinch_event, NULL);
629         }
630         else {
631                 perror("sigwinch pipe creation failed");
632                 exit(EXIT_FAILURE);
633         }
634 #endif
636         /* attempt to connect */
637         reconnect_source_id = g_timeout_add(1, timer_reconnect, NULL);
639         auto_update_timer();
641 #ifndef NCMPC_MINI
642         check_key_bindings_source_id = g_timeout_add(10000, timer_check_key_bindings, NULL);
643 #endif
645         screen_paint(mpd);
647         g_main_loop_run(main_loop);
649         /* cleanup */
651         cancel_seek_timer();
653         disable_update_timer();
655         if (reconnect_source_id != 0)
656                 g_source_remove(reconnect_source_id);
658 #ifndef NCMPC_MINI
659         if (check_key_bindings_source_id != 0)
660                 g_source_remove(check_key_bindings_source_id);
661 #endif
663         g_main_loop_unref(main_loop);
664         g_io_channel_unref(keyboard_channel);
665         g_io_channel_unref(sigwinch_channel);
666         close(sigwinch_pipes[0]);
667         close(sigwinch_pipes[1]);
669 #ifdef ENABLE_LIRC
670         if (lirc_socket >= 0)
671                 g_io_channel_unref(lirc_channel);
672         ncmpc_lirc_close();
673 #endif
675         exit_and_cleanup();
677 #ifdef ENABLE_LYRICS_SCREEN
678         lyrics_deinit();
679 #endif
681         ncu_deinit();
682         options_deinit();
684         return 0;