Code

Compiler.h: add macro CLANG_OR_GCC_VERSION()
[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         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
201                 ;
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 static char *
238 settings_name(const struct mpd_settings *settings)
240         const char *host = mpd_settings_get_host(settings);
241         if (host == NULL)
242                 host = _("unknown");
244         if (host[0] == '/')
245                 return g_strdup(host);
247         unsigned port = mpd_settings_get_port(settings);
248         if (port == 0 || port == 6600)
249                 return g_strdup(host);
251         return g_strdup_printf("%s:%u", host, port);
254 static char *
255 default_settings_name(void)
257         struct mpd_settings *settings =
258                 mpd_settings_new(options.host, options.port, 0,
259                                  NULL, options.password);
260         if (settings == NULL)
261                 return g_strdup(_("unknown"));
263         char *name = settings_name(settings);
264         mpd_settings_free(settings);
266         return name;
269 /**
270  * This timer is installed when the connection to the MPD server is
271  * broken.  It tries to recover by reconnecting periodically.
272  */
273 static gboolean
274 timer_reconnect(gcc_unused gpointer data)
276         assert(!mpdclient_is_connected(mpd));
278         reconnect_source_id = 0;
280         char *name = default_settings_name();
281         screen_status_printf(_("Connecting to %s...  [Press %s to abort]"),
282                              name, get_key_names(CMD_QUIT, false));
283         g_free(name);
284         doupdate();
286         mpdclient_disconnect(mpd);
287         if (!mpdclient_connect(mpd, options.host, options.port,
288                                options.timeout_ms,
289                                options.password)) {
290                 /* try again in 5 seconds */
291                 reconnect_source_id = g_timeout_add(5000,
292                                                     timer_reconnect, NULL);
293                 return FALSE;
294         }
296         struct mpd_connection *connection = mpdclient_get_connection(mpd);
298 #ifndef NCMPC_MINI
299         /* quit if mpd is pre 0.14 - song id not supported by mpd */
300         if (mpd_connection_cmp_server_version(connection, 0, 16, 0) < 0) {
301                 const unsigned *version =
302                         mpd_connection_get_server_version(connection);
303                 screen_status_printf(_("Error: MPD version %d.%d.%d is too old (%s needed)"),
304                                      version[0], version[1], version[2],
305                                      "0.16.0");
306                 mpdclient_disconnect(mpd);
307                 doupdate();
309                 /* try again after 30 seconds */
310                 reconnect_source_id = g_timeout_add(30000,
311                                                     timer_reconnect, NULL);
312                 return FALSE;
313         }
314 #endif
316         mpd->source = mpd_glib_new(connection,
317                                    idle_callback, mpd);
319         screen_status_clear_message();
320         doupdate();
322         /* update immediately */
323         mpd->events = MPD_IDLE_ALL;
325         do_mpd_update();
327         auto_update_timer();
329         return FALSE;
332 static void
333 check_reconnect(void)
335         if (!mpdclient_is_connected(mpd) && reconnect_source_id == 0)
336                 /* reconnect when the connection is lost */
337                 reconnect_source_id = g_timeout_add(1000, timer_reconnect,
338                                                     NULL);
341 /**
342  * This function is called by the gidle.c library when MPD sends us an
343  * idle event (or when the connection dies).
344  */
345 static void
346 idle_callback(enum mpd_error error, enum mpd_server_error server_error,
347               const char *message, enum mpd_idle events,
348               void *ctx)
350         struct mpdclient *c = ctx;
352         c->idle = false;
354         assert(mpdclient_is_connected(c));
356         if (error != MPD_ERROR_SUCCESS) {
357                 if (error == MPD_ERROR_SERVER &&
358                     server_error == MPD_SERVER_ERROR_UNKNOWN_CMD) {
359                         /* the "idle" command is not supported - fall
360                            back to timer based polling */
361                         mpd_glib_free(c->source);
362                         c->source = NULL;
363                         auto_update_timer();
364                         return;
365                 }
367                 char *allocated;
368                 if (error == MPD_ERROR_SERVER)
369                         message = allocated = utf8_to_locale(message);
370                 else
371                         allocated = NULL;
372                 screen_status_message(message);
373                 g_free(allocated);
374                 screen_bell();
375                 doupdate();
377                 mpdclient_disconnect(c);
378                 screen_update(mpd);
379                 reconnect_source_id = g_timeout_add(1000, timer_reconnect,
380                                                     NULL);
381                 return;
382         }
384         c->events |= events;
385         mpdclient_update(c);
387 #ifndef NCMPC_MINI
388         if (options.enable_xterm_title)
389                 update_xterm_title();
390 #endif
392         screen_update(mpd);
393         c->events = 0;
395         mpdclient_put_connection(c);
396         check_reconnect();
397         auto_update_timer();
400 static gboolean
401 timer_mpd_update(gcc_unused gpointer data)
403         do_mpd_update();
405         if (should_enable_update_timer())
406                 return true;
407         else {
408                 update_source_id = 0;
409                 return false;
410         }
413 void begin_input_event(void)
417 void end_input_event(void)
419         screen_update(mpd);
420         mpd->events = 0;
422         mpdclient_put_connection(mpd);
423         check_reconnect();
424         auto_update_timer();
427 int do_input_event(command_t cmd)
429         if (cmd == CMD_QUIT) {
430                 g_main_loop_quit(main_loop);
431                 return -1;
432         }
434         screen_cmd(mpd, cmd);
436         if (cmd == CMD_VOLUME_UP || cmd == CMD_VOLUME_DOWN)
437                 /* make sure we don't update the volume yet */
438                 disable_update_timer();
440         return 0;
443 static gboolean
444 keyboard_event(gcc_unused GIOChannel *source,
445                gcc_unused GIOCondition condition,
446                gcc_unused gpointer data)
448         begin_input_event();
450         command_t cmd = get_keyboard_command();
451         if (cmd != CMD_NONE)
452                 if (do_input_event(cmd) != 0)
453                         return FALSE;
455         end_input_event();
456         return TRUE;
459 #ifndef NCMPC_MINI
460 /**
461  * Check the configured key bindings for errors, and display a status
462  * message every 10 seconds.
463  */
464 static gboolean
465 timer_check_key_bindings(gcc_unused gpointer data)
467         char buf[256];
469         if (check_key_bindings(NULL, buf, sizeof(buf))) {
470                 /* no error: disable this timer for the rest of this
471                    process */
472                 check_key_bindings_source_id = 0;
473                 return FALSE;
474         }
476 #ifdef ENABLE_KEYDEF_SCREEN
477         g_strchomp(buf);
478         g_strlcat(buf, " (", sizeof(buf));
479         /* to translators: a key was bound twice in the key editor,
480            and this is a hint for the user what to press to correct
481            that */
482         char comment[64];
483         g_snprintf(comment, sizeof(comment), _("press %s for the key editor"),
484                    get_key_names(CMD_SCREEN_KEYDEF, false));
485         g_strlcat(buf, comment, sizeof(buf));
486         g_strlcat(buf, ")", sizeof(buf));
487 #endif
489         screen_status_printf("%s", buf);
491         doupdate();
492         return TRUE;
494 #endif
496 int
497 main(int argc, const char *argv[])
499 #ifdef ENABLE_LOCALE
500 #ifndef ENABLE_NLS
501         gcc_unused
502 #endif
503         const char *charset = NULL;
504         /* time and date formatting */
505         setlocale(LC_TIME,"");
506         /* care about sorting order etc */
507         setlocale(LC_COLLATE,"");
508         /* charset */
509         setlocale(LC_CTYPE,"");
510         /* initialize charset conversions */
511         charset = charset_init();
513         /* initialize i18n support */
514 #endif
516 #ifdef ENABLE_NLS
517         setlocale(LC_MESSAGES, "");
518         bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
519 #ifdef ENABLE_LOCALE
520         bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
521 #endif
522         textdomain(GETTEXT_PACKAGE);
523 #endif
525         /* initialize options */
526         options_init();
528         /* parse command line options - 1 pass get configuration files */
529         options_parse(argc, argv);
531 #ifndef NCMPC_MINI
532         /* read configuration */
533         read_configuration();
535         /* check key bindings */
536         check_key_bindings(NULL, NULL, 0);
537 #endif
539         /* parse command line options - 2 pass */
540         options_parse(argc, argv);
542 #ifndef WIN32
543         /* setup signal behavior - SIGINT */
544         struct sigaction act;
545         sigemptyset(&act.sa_mask);
546         act.sa_flags = 0;
547         act.sa_handler = catch_sigint;
548         if (sigaction(SIGINT, &act, NULL) < 0) {
549                 perror("signal");
550                 exit(EXIT_FAILURE);
551         }
553         /* setup signal behavior - SIGTERM */
555         act.sa_handler = catch_sigint;
556         if (sigaction(SIGTERM, &act, NULL) < 0) {
557                 perror("sigaction()");
558                 exit(EXIT_FAILURE);
559         }
561         /* setup signal behavior - SIGCONT */
563         act.sa_handler = catch_sigcont;
564         if (sigaction(SIGCONT, &act, NULL) < 0) {
565                 perror("sigaction(SIGCONT)");
566                 exit(EXIT_FAILURE);
567         }
569         /* setup signal behaviour - SIGHUP*/
571         act.sa_handler = catch_sigint;
572         if (sigaction(SIGHUP, &act, NULL) < 0) {
573                 perror("sigaction(SIGHUP)");
574                 exit(EXIT_FAILURE);
575         }
577         /* setup SIGWINCH */
579         act.sa_flags = SA_RESTART;
580         act.sa_handler = catch_sigwinch;
581         if (sigaction(SIGWINCH, &act, NULL) < 0) {
582                 perror("sigaction(SIGWINCH)");
583                 exit(EXIT_FAILURE);
584         }
586         /* ignore SIGPIPE */
588         act.sa_handler = SIG_IGN;
589         if (sigaction(SIGPIPE, &act, NULL) < 0) {
590                 perror("sigaction(SIGPIPE)");
591                 exit(EXIT_FAILURE);
592         }
593 #endif
595         ncu_init();
597 #ifdef ENABLE_LYRICS_SCREEN
598         lyrics_init();
599 #endif
601         /* create mpdclient instance */
602         mpd = mpdclient_new();
604         /* initialize curses */
605         screen_init(mpd);
607         /* the main loop */
608         main_loop = g_main_loop_new(NULL, FALSE);
610         /* watch out for keyboard input */
611         GIOChannel *keyboard_channel = g_io_channel_unix_new(STDIN_FILENO);
612         g_io_add_watch(keyboard_channel, G_IO_IN, keyboard_event, NULL);
614 #ifdef ENABLE_LIRC
615         /* watch out for lirc input */
616         int lirc_socket = ncmpc_lirc_open();
617         GIOChannel *lirc_channel = NULL;
618         if (lirc_socket >= 0) {
619                 lirc_channel = g_io_channel_unix_new(lirc_socket);
620                 g_io_add_watch(lirc_channel, G_IO_IN, lirc_event, NULL);
621         }
622 #endif
624 #ifndef WIN32
625         GIOChannel *sigwinch_channel = NULL;
626         if (!pipe(sigwinch_pipes) &&
627                 !fcntl(sigwinch_pipes[1], F_SETFL, O_NONBLOCK)) {
628                 sigwinch_channel = g_io_channel_unix_new(sigwinch_pipes[0]);
629                 g_io_add_watch(sigwinch_channel, G_IO_IN, sigwinch_event, NULL);
630         }
631         else {
632                 perror("sigwinch pipe creation failed");
633                 exit(EXIT_FAILURE);
634         }
635 #endif
637         /* attempt to connect */
638         reconnect_source_id = g_timeout_add(1, timer_reconnect, NULL);
640         auto_update_timer();
642 #ifndef NCMPC_MINI
643         check_key_bindings_source_id = g_timeout_add(10000, timer_check_key_bindings, NULL);
644 #endif
646         screen_paint(mpd);
648         g_main_loop_run(main_loop);
650         /* cleanup */
652         cancel_seek_timer();
654         disable_update_timer();
656         if (reconnect_source_id != 0)
657                 g_source_remove(reconnect_source_id);
659 #ifndef NCMPC_MINI
660         if (check_key_bindings_source_id != 0)
661                 g_source_remove(check_key_bindings_source_id);
662 #endif
664         g_main_loop_unref(main_loop);
665         g_io_channel_unref(keyboard_channel);
666         g_io_channel_unref(sigwinch_channel);
667         close(sigwinch_pipes[0]);
668         close(sigwinch_pipes[1]);
670 #ifdef ENABLE_LIRC
671         if (lirc_socket >= 0)
672                 g_io_channel_unref(lirc_channel);
673         ncmpc_lirc_close();
674 #endif
676         exit_and_cleanup();
678 #ifdef ENABLE_LYRICS_SCREEN
679         lyrics_deinit();
680 #endif
682         ncu_deinit();
683         options_deinit();
685         return 0;