Code

main: move signal handlers to signals.c
[ncmpc.git] / src / main.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2017 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 "callbacks.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"
34 #include "keyboard.h"
35 #include "lirc.h"
36 #include "signals.h"
38 #ifndef NCMPC_MINI
39 #include "conf.h"
40 #endif
42 #ifdef ENABLE_LYRICS_SCREEN
43 #include "lyrics.h"
44 #endif
46 #include <mpd/client.h>
48 #include <stdlib.h>
49 #include <unistd.h>
50 #include <fcntl.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         const struct mpd_song *song = mpd->song;
77         char tmp[BUFSIZE];
78         if (options.xterm_title_format && mpd->playing && song)
79                 strfsong(tmp, BUFSIZE, options.xterm_title_format, song);
80         else
81                 g_strlcpy(tmp, PACKAGE " version " VERSION, BUFSIZE);
83         static char title[BUFSIZE];
84         if (strncmp(title, tmp, BUFSIZE)) {
85                 g_strlcpy(title, tmp, BUFSIZE);
86                 set_xterm_title("%s", title);
87         }
88 }
89 #endif
91 static gboolean
92 timer_mpd_update(gpointer data);
94 static void
95 enable_update_timer(void)
96 {
97         if (update_source_id != 0)
98                 return;
100         update_source_id = g_timeout_add(update_interval,
101                                          timer_mpd_update, NULL);
104 static void
105 disable_update_timer(void)
107         if (update_source_id == 0)
108                 return;
110         g_source_remove(update_source_id);
111         update_source_id = 0;
114 static bool
115 should_enable_update_timer(void)
117         return mpd->playing
118 #ifndef NCMPC_MINI
119                 || options.display_time
120 #endif
121                 ;
124 static void
125 auto_update_timer(void)
127         if (should_enable_update_timer())
128                 enable_update_timer();
129         else
130                 disable_update_timer();
133 static void
134 check_reconnect(void);
136 static void
137 do_mpd_update(void)
139         if (mpdclient_is_connected(mpd) &&
140             (mpd->events != 0 || mpd->playing))
141                 mpdclient_update(mpd);
143 #ifndef NCMPC_MINI
144         if (options.enable_xterm_title)
145                 update_xterm_title();
146 #endif
148         screen_update(mpd);
149         mpd->events = 0;
151         mpdclient_put_connection(mpd);
152         check_reconnect();
155 static char *
156 settings_name(const struct mpd_settings *settings)
158         const char *host = mpd_settings_get_host(settings);
159         if (host == NULL)
160                 host = _("unknown");
162         if (host[0] == '/')
163                 return g_strdup(host);
165         unsigned port = mpd_settings_get_port(settings);
166         if (port == 0 || port == 6600)
167                 return g_strdup(host);
169         return g_strdup_printf("%s:%u", host, port);
172 static char *
173 default_settings_name(void)
175         struct mpd_settings *settings =
176                 mpd_settings_new(options.host, options.port, 0,
177                                  NULL, options.password);
178         if (settings == NULL)
179                 return g_strdup(_("unknown"));
181         char *name = settings_name(settings);
182         mpd_settings_free(settings);
184         return name;
187 /**
188  * This timer is installed when the connection to the MPD server is
189  * broken.  It tries to recover by reconnecting periodically.
190  */
191 static gboolean
192 timer_reconnect(gcc_unused gpointer data)
194         assert(mpdclient_is_dead(mpd));
196         reconnect_source_id = 0;
198         char *name = default_settings_name();
199         screen_status_printf(_("Connecting to %s...  [Press %s to abort]"),
200                              name, get_key_names(CMD_QUIT, false));
201         g_free(name);
202         doupdate();
204         mpdclient_disconnect(mpd);
205         mpdclient_connect(mpd, options.host, options.port,
206                           options.timeout_ms,
207                           options.password);
209         return FALSE;
212 static void
213 check_reconnect(void)
215         if (mpdclient_is_dead(mpd) && reconnect_source_id == 0)
216                 /* reconnect when the connection is lost */
217                 reconnect_source_id = g_timeout_add(1000, timer_reconnect,
218                                                     NULL);
221 void
222 mpdclient_connected_callback(void)
224         assert(reconnect_source_id == 0);
226 #ifndef NCMPC_MINI
227         /* quit if mpd is pre 0.14 - song id not supported by mpd */
228         struct mpd_connection *connection = mpdclient_get_connection(mpd);
229         if (mpd_connection_cmp_server_version(connection, 0, 16, 0) < 0) {
230                 const unsigned *version =
231                         mpd_connection_get_server_version(connection);
232                 screen_status_printf(_("Error: MPD version %d.%d.%d is too old (%s needed)"),
233                                      version[0], version[1], version[2],
234                                      "0.16.0");
235                 mpdclient_disconnect(mpd);
236                 doupdate();
238                 /* try again after 30 seconds */
239                 reconnect_source_id = g_timeout_add(30000,
240                                                     timer_reconnect, NULL);
241                 return;
242         }
243 #endif
245         screen_status_clear_message();
246         doupdate();
248         /* update immediately */
249         mpd->events = MPD_IDLE_ALL;
251         do_mpd_update();
253         auto_update_timer();
256 void
257 mpdclient_failed_callback(void)
259         assert(reconnect_source_id == 0);
261         /* try again in 5 seconds */
262         reconnect_source_id = g_timeout_add(5000,
263                                             timer_reconnect, NULL);
266 void
267 mpdclient_lost_callback(void)
269         assert(reconnect_source_id == 0);
271         screen_update(mpd);
273         reconnect_source_id = g_timeout_add(1000, timer_reconnect, NULL);
276 /**
277  * This function is called by the gidle.c library when MPD sends us an
278  * idle event (or when the connection dies).
279  */
280 void
281 mpdclient_idle_callback(gcc_unused enum mpd_idle events)
283 #ifndef NCMPC_MINI
284         if (options.enable_xterm_title)
285                 update_xterm_title();
286 #endif
288         screen_update(mpd);
289         auto_update_timer();
292 static gboolean
293 timer_mpd_update(gcc_unused gpointer data)
295         do_mpd_update();
297         if (should_enable_update_timer())
298                 return true;
299         else {
300                 update_source_id = 0;
301                 return false;
302         }
305 void begin_input_event(void)
309 void end_input_event(void)
311         screen_update(mpd);
312         mpd->events = 0;
314         mpdclient_put_connection(mpd);
315         check_reconnect();
316         auto_update_timer();
319 bool
320 do_input_event(command_t cmd)
322         if (cmd == CMD_QUIT) {
323                 g_main_loop_quit(main_loop);
324                 return false;
325         }
327         screen_cmd(mpd, cmd);
329         if (cmd == CMD_VOLUME_UP || cmd == CMD_VOLUME_DOWN)
330                 /* make sure we don't update the volume yet */
331                 disable_update_timer();
333         return true;
336 #ifndef NCMPC_MINI
337 /**
338  * Check the configured key bindings for errors, and display a status
339  * message every 10 seconds.
340  */
341 static gboolean
342 timer_check_key_bindings(gcc_unused gpointer data)
344         char buf[256];
346         if (check_key_bindings(NULL, buf, sizeof(buf))) {
347                 /* no error: disable this timer for the rest of this
348                    process */
349                 check_key_bindings_source_id = 0;
350                 return FALSE;
351         }
353 #ifdef ENABLE_KEYDEF_SCREEN
354         g_strchomp(buf);
355         g_strlcat(buf, " (", sizeof(buf));
356         /* to translators: a key was bound twice in the key editor,
357            and this is a hint for the user what to press to correct
358            that */
359         char comment[64];
360         g_snprintf(comment, sizeof(comment), _("press %s for the key editor"),
361                    get_key_names(CMD_SCREEN_KEYDEF, false));
362         g_strlcat(buf, comment, sizeof(buf));
363         g_strlcat(buf, ")", sizeof(buf));
364 #endif
366         screen_status_printf("%s", buf);
368         doupdate();
369         return TRUE;
371 #endif
373 int
374 main(int argc, const char *argv[])
376 #ifdef ENABLE_LOCALE
377 #ifndef ENABLE_NLS
378         gcc_unused
379 #endif
380         const char *charset = NULL;
381         /* time and date formatting */
382         setlocale(LC_TIME,"");
383         /* care about sorting order etc */
384         setlocale(LC_COLLATE,"");
385         /* charset */
386         setlocale(LC_CTYPE,"");
387         /* initialize charset conversions */
388         charset = charset_init();
390         /* initialize i18n support */
391 #endif
393 #ifdef ENABLE_NLS
394         setlocale(LC_MESSAGES, "");
395         bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
396 #ifdef ENABLE_LOCALE
397         bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
398 #endif
399         textdomain(GETTEXT_PACKAGE);
400 #endif
402         /* initialize options */
403         options_init();
405         /* parse command line options - 1 pass get configuration files */
406         options_parse(argc, argv);
408 #ifndef NCMPC_MINI
409         /* read configuration */
410         read_configuration();
412         /* check key bindings */
413         check_key_bindings(NULL, NULL, 0);
414 #endif
416         /* parse command line options - 2 pass */
417         options_parse(argc, argv);
419         ncu_init();
421 #ifdef ENABLE_LYRICS_SCREEN
422         lyrics_init();
423 #endif
425         /* create mpdclient instance */
426         mpd = mpdclient_new();
428         /* initialize curses */
429         screen_init(mpd);
431         /* the main loop */
432         main_loop = g_main_loop_new(NULL, FALSE);
434         /* watch out for keyboard input */
435         keyboard_init();
437         /* watch out for lirc input */
438         ncmpc_lirc_init();
440         signals_init(main_loop, mpd);
442         /* attempt to connect */
443         reconnect_source_id = g_timeout_add(1, timer_reconnect, NULL);
445         auto_update_timer();
447 #ifndef NCMPC_MINI
448         check_key_bindings_source_id = g_timeout_add(10000, timer_check_key_bindings, NULL);
449 #endif
451         screen_paint(mpd);
453         g_main_loop_run(main_loop);
454         g_main_loop_unref(main_loop);
456         /* cleanup */
458         cancel_seek_timer();
460         disable_update_timer();
462         if (reconnect_source_id != 0)
463                 g_source_remove(reconnect_source_id);
465 #ifndef NCMPC_MINI
466         if (check_key_bindings_source_id != 0)
467                 g_source_remove(check_key_bindings_source_id);
468 #endif
470         signals_deinit();
471         ncmpc_lirc_deinit();
473         screen_exit();
474 #ifndef NCMPC_MINI
475         set_xterm_title("");
476 #endif
477         printf("\n");
479         mpdclient_free(mpd);
481 #ifdef ENABLE_LYRICS_SCREEN
482         lyrics_deinit();
483 #endif
485         ncu_deinit();
486         options_deinit();
488         return 0;