Code

8536cb67c77d8e7fa8258f9ccab837b46d4bb75d
[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_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)
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         }
113 static void
114 catch_sigint(G_GNUC_UNUSED int sig)
116         g_main_loop_quit(main_loop);
120 static void
121 catch_sigcont(G_GNUC_UNUSED int sig)
123         screen_resize(mpd);
126 void
127 sigstop(void)
129         def_prog_mode();  /* save the tty modes */
130         endwin();         /* end curses mode temporarily */
131         kill(0, SIGSTOP); /* issue SIGSTOP */
134 static guint timer_sigwinch_id;
136 static gboolean
137 timer_sigwinch(G_GNUC_UNUSED gpointer data)
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;
150 static void
151 catch_sigwinch(G_GNUC_UNUSED int sig)
153         if (timer_sigwinch_id != 0)
154                 g_source_remove(timer_sigwinch_id);
156         timer_sigwinch_id = g_timeout_add(100, timer_sigwinch, NULL);
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)
171         if (update_source_id != 0)
172                 return;
174         update_source_id = g_timeout_add(update_interval,
175                                          timer_mpd_update, NULL);
178 static void
179 disable_update_timer(void)
181         if (update_source_id == 0)
182                 return;
184         g_source_remove(update_source_id);
185         update_source_id = 0;
188 static bool
189 should_enable_update_timer(void)
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                 ;
201 static void
202 auto_update_timer(void)
204         if (should_enable_update_timer())
205                 enable_update_timer();
206         else
207                 disable_update_timer();
210 static void
211 check_reconnect(void);
213 static void
214 do_mpd_update(void)
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();
234 #if LIBMPDCLIENT_CHECK_VERSION(2,4,0)
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 #endif
255 static char *
256 default_settings_name(void)
258 #if LIBMPDCLIENT_CHECK_VERSION(2,4,0)
259         struct mpd_settings *settings =
260                 mpd_settings_new(options.host, options.port, 0,
261                                  NULL, options.password);
262         if (settings == NULL)
263                 return g_strdup("unknown");
265         char *name = settings_name(settings);
266         mpd_settings_free(settings);
268         return name;
269 #else
270         return g_strdup(options.host);
271 #endif
274 static char *
275 connection_settings_name(const struct mpd_connection *connection)
277 #if LIBMPDCLIENT_CHECK_VERSION(2,4,0)
278         const struct mpd_settings *settings =
279                 mpd_connection_get_settings(connection);
280         if (settings == NULL)
281                 return g_strdup("unknown");
283         return settings_name(settings);
284 #else
285         (void)connection;
286         return g_strdup(options.host);
287 #endif
290 /**
291  * This timer is installed when the connection to the MPD server is
292  * broken.  It tries to recover by reconnecting periodically.
293  */
294 static gboolean
295 timer_reconnect(G_GNUC_UNUSED gpointer data)
297         bool success;
298         struct mpd_connection *connection;
300         assert(!mpdclient_is_connected(mpd));
302         reconnect_source_id = 0;
304         char *name = default_settings_name();
305         screen_status_printf(_("Connecting to %s...  [Press %s to abort]"),
306                              name, get_key_names(CMD_QUIT,0) );
307         g_free(name);
308         doupdate();
310         mpdclient_disconnect(mpd);
311         success = mpdclient_connect(mpd,
312                                     options.host, options.port,
313                                     1.5,
314                                     options.password);
315         if (!success) {
316                 /* try again in 5 seconds */
317                 reconnect_source_id = g_timeout_add(5000,
318                                                     timer_reconnect, NULL);
319                 return FALSE;
320         }
322         connection = mpdclient_get_connection(mpd);
324 #ifndef NCMPC_MINI
325         /* quit if mpd is pre 0.11.0 - song id not supported by mpd */
326         if (mpd_connection_cmp_server_version(connection, 0, 12, 0) < 0) {
327                 const unsigned *version =
328                         mpd_connection_get_server_version(connection);
329                 screen_status_printf(_("Error: MPD version %d.%d.%d is to old (%s needed)"),
330                                      version[0], version[1], version[2],
331                                      "0.12.0");
332                 mpdclient_disconnect(mpd);
333                 doupdate();
335                 /* try again after 30 seconds */
336                 reconnect_source_id = g_timeout_add(30000,
337                                                     timer_reconnect, NULL);
338                 return FALSE;
339         }
340 #endif
342         if (mpd_connection_cmp_server_version(connection,
343                                               0, 14, 0) >= 0)
344                 mpd->source = mpd_glib_new(connection,
345                                            idle_callback, mpd);
347         name = connection_settings_name(connection);
348         screen_status_printf(_("Connected to %s"), name);
349         g_free(name);
350         doupdate();
352         /* update immediately */
353         mpd->events = MPD_IDLE_DATABASE|MPD_IDLE_STORED_PLAYLIST|
354                 MPD_IDLE_QUEUE|MPD_IDLE_PLAYER|MPD_IDLE_MIXER|MPD_IDLE_OUTPUT|
355                 MPD_IDLE_OPTIONS|MPD_IDLE_UPDATE;
357         do_mpd_update();
359         auto_update_timer();
361         return FALSE;
364 static void
365 check_reconnect(void)
367         if (!mpdclient_is_connected(mpd) && reconnect_source_id == 0)
368                 /* reconnect when the connection is lost */
369                 reconnect_source_id = g_timeout_add(1000, timer_reconnect,
370                                                     NULL);
373 /**
374  * This function is called by the gidle.c library when MPD sends us an
375  * idle event (or when the connectiond dies).
376  */
377 static void
378 idle_callback(enum mpd_error error, enum mpd_server_error server_error,
379               const char *message, enum mpd_idle events,
380               void *ctx)
382         struct mpdclient *c = ctx;
383         struct mpd_connection *connection;
385         c->idle = false;
387         connection = mpdclient_get_connection(c);
388         assert(connection != NULL);
390         if (error != MPD_ERROR_SUCCESS) {
391                 char *allocated;
393                 if (error == MPD_ERROR_SERVER &&
394                     server_error == MPD_SERVER_ERROR_UNKNOWN_CMD) {
395                         /* the "idle" command is not supported - fall
396                            back to timer based polling */
397                         mpd_glib_free(c->source);
398                         c->source = NULL;
399                         auto_update_timer();
400                         return;
401                 }
403                 if (error == MPD_ERROR_SERVER)
404                         message = allocated = utf8_to_locale(message);
405                 else
406                         allocated = NULL;
407                 screen_status_message(message);
408                 g_free(allocated);
409                 screen_bell();
410                 doupdate();
412                 mpdclient_disconnect(c);
413                 screen_update(mpd);
414                 reconnect_source_id = g_timeout_add(1000, timer_reconnect,
415                                                     NULL);
416                 return;
417         }
419         c->events |= events;
420         mpdclient_update(c);
422 #ifndef NCMPC_MINI
423         if (options.enable_xterm_title)
424                 update_xterm_title();
425 #endif
427         screen_update(mpd);
428         c->events = 0;
430         mpdclient_put_connection(c);
431         check_reconnect();
432         auto_update_timer();
435 static gboolean
436 timer_mpd_update(G_GNUC_UNUSED gpointer data)
438         do_mpd_update();
440         if (should_enable_update_timer())
441                 return true;
442         else {
443                 update_source_id = 0;
444                 return false;
445         }
448 void begin_input_event(void)
452 void end_input_event(void)
454         screen_update(mpd);
455         mpd->events = 0;
457         mpdclient_put_connection(mpd);
458         check_reconnect();
459         auto_update_timer();
462 int do_input_event(command_t cmd)
464         if (cmd == CMD_QUIT) {
465                 g_main_loop_quit(main_loop);
466                 return -1;
467         }
469         screen_cmd(mpd, cmd);
471         if (cmd == CMD_VOLUME_UP || cmd == CMD_VOLUME_DOWN)
472                 /* make sure we don't update the volume yet */
473                 disable_update_timer();
475         return 0;
478 static gboolean
479 keyboard_event(G_GNUC_UNUSED GIOChannel *source,
480                G_GNUC_UNUSED GIOCondition condition,
481                G_GNUC_UNUSED gpointer data)
483         command_t cmd;
485         begin_input_event();
487         if ((cmd=get_keyboard_command()) != CMD_NONE)
488                 if (do_input_event(cmd) != 0)
489                         return FALSE;
491         end_input_event();
492         return TRUE;
495 #ifndef NCMPC_MINI
496 /**
497  * Check the configured key bindings for errors, and display a status
498  * message every 10 seconds.
499  */
500 static gboolean
501 timer_check_key_bindings(G_GNUC_UNUSED gpointer data)
503         char buf[256];
504 #ifdef ENABLE_KEYDEF_SCREEN
505         char comment[64];
506 #endif
507         gboolean key_error;
509         key_error = check_key_bindings(NULL, buf, sizeof(buf));
510         if (!key_error) {
511                 /* no error: disable this timer for the rest of this
512                    process */
513                 check_key_bindings_source_id = 0;
514                 return FALSE;
515         }
517 #ifdef ENABLE_KEYDEF_SCREEN
518         g_strchomp(buf);
519         g_strlcat(buf, " (", sizeof(buf));
520         /* to translators: a key was bound twice in the key editor,
521            and this is a hint for the user what to press to correct
522            that */
523         g_snprintf(comment, sizeof(comment), _("press %s for the key editor"),
524                    get_key_names(CMD_SCREEN_KEYDEF, 0));
525         g_strlcat(buf, comment, sizeof(buf));
526         g_strlcat(buf, ")", sizeof(buf));
527 #endif
529         screen_status_printf("%s", buf);
531         doupdate();
532         return TRUE;
534 #endif
536 int
537 main(int argc, const char *argv[])
539         struct sigaction act;
540 #ifdef ENABLE_LOCALE
541         const char *charset = NULL;
542 #endif
543         GIOChannel *keyboard_channel;
544 #ifdef ENABLE_LIRC
545         int lirc_socket;
546         GIOChannel *lirc_channel = NULL;
547 #endif
549 #ifdef ENABLE_LOCALE
550         /* time and date formatting */
551         setlocale(LC_TIME,"");
552         /* care about sorting order etc */
553         setlocale(LC_COLLATE,"");
554         /* charset */
555         setlocale(LC_CTYPE,"");
556         /* initialize charset conversions */
557         charset = charset_init();
559         /* initialize i18n support */
560 #endif
562 #ifdef ENABLE_NLS
563         setlocale(LC_MESSAGES, "");
564         bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
565 #ifdef ENABLE_LOCALE
566         bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
567 #endif
568         textdomain(GETTEXT_PACKAGE);
569 #endif
571         /* initialize options */
572         options_init();
574         /* parse command line options - 1 pass get configuration files */
575         options_parse(argc, argv);
577 #ifndef NCMPC_MINI
578         /* read configuration */
579         read_configuration();
581         /* check key bindings */
582         check_key_bindings(NULL, NULL, 0);
583 #endif
585         /* parse command line options - 2 pass */
586         options_parse(argc, argv);
588         /* setup signal behavior - SIGINT */
589         sigemptyset(&act.sa_mask);
590         act.sa_flags = 0;
591         act.sa_handler = catch_sigint;
592         if (sigaction(SIGINT, &act, NULL) < 0) {
593                 perror("signal");
594                 exit(EXIT_FAILURE);
595         }
597         /* setup signal behavior - SIGTERM */
599         act.sa_handler = catch_sigint;
600         if (sigaction(SIGTERM, &act, NULL) < 0) {
601                 perror("sigaction()");
602                 exit(EXIT_FAILURE);
603         }
605         /* setup signal behavior - SIGCONT */
607         act.sa_handler = catch_sigcont;
608         if (sigaction(SIGCONT, &act, NULL) < 0) {
609                 perror("sigaction(SIGCONT)");
610                 exit(EXIT_FAILURE);
611         }
613         /* setup signal behaviour - SIGHUP*/
615         act.sa_handler = catch_sigint;
616         if (sigaction(SIGHUP, &act, NULL) < 0) {
617                 perror("sigaction(SIGHUP)");
618                 exit(EXIT_FAILURE);
619         }
621         /* setup SIGWINCH */
623         act.sa_flags = SA_RESTART;
624         act.sa_handler = catch_sigwinch;
625         if (sigaction(SIGWINCH, &act, NULL) < 0) {
626                 perror("sigaction(SIGWINCH)");
627                 exit(EXIT_FAILURE);
628         }
630         /* ignore SIGPIPE */
632         act.sa_handler = SIG_IGN;
633         if (sigaction(SIGPIPE, &act, NULL) < 0) {
634                 perror("sigaction(SIGPIPE)");
635                 exit(EXIT_FAILURE);
636         }
638         ncu_init();
640 #ifdef ENABLE_LYRICS_SCREEN
641         lyrics_init();
642 #endif
644         /* create mpdclient instance */
645         mpd = mpdclient_new();
647         /* initialize curses */
648         screen_init(mpd);
650         /* the main loop */
651         main_loop = g_main_loop_new(NULL, FALSE);
653         /* watch out for keyboard input */
654         keyboard_channel = g_io_channel_unix_new(STDIN_FILENO);
655         g_io_add_watch(keyboard_channel, G_IO_IN, keyboard_event, NULL);
657 #ifdef ENABLE_LIRC
658         /* watch out for lirc input */
659         lirc_socket = ncmpc_lirc_open();
660         if (lirc_socket >= 0) {
661                 lirc_channel = g_io_channel_unix_new(lirc_socket);
662                 g_io_add_watch(lirc_channel, G_IO_IN, lirc_event, NULL);
663         }
664 #endif
666         /* attempt to connect */
667         reconnect_source_id = g_timeout_add(1, timer_reconnect, NULL);
669         auto_update_timer();
671 #ifndef NCMPC_MINI
672         check_key_bindings_source_id = g_timeout_add(10000, timer_check_key_bindings, NULL);
673 #endif
675         screen_paint(mpd);
677         g_main_loop_run(main_loop);
679         /* cleanup */
681         cancel_seek_timer();
683         disable_update_timer();
685         if (reconnect_source_id != 0)
686                 g_source_remove(reconnect_source_id);
688 #ifndef NCMPC_MINI
689         if (check_key_bindings_source_id != 0)
690                 g_source_remove(check_key_bindings_source_id);
691 #endif
693         g_main_loop_unref(main_loop);
694         g_io_channel_unref(keyboard_channel);
696 #ifdef ENABLE_LIRC
697         if (lirc_socket >= 0)
698                 g_io_channel_unref(lirc_channel);
699         ncmpc_lirc_close();
700 #endif
702         exit_and_cleanup();
704 #ifdef ENABLE_LYRICS_SCREEN
705         lyrics_deinit();
706 #endif
708         ncu_deinit();
709         options_deinit();
711         return 0;