Code

main: merge catch_sigcont() and catch_sigwinch()
[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"
37 #ifndef NCMPC_MINI
38 #include "conf.h"
39 #endif
41 #ifdef ENABLE_LYRICS_SCREEN
42 #include "lyrics.h"
43 #endif
45 #include <mpd/client.h>
47 #include <stdlib.h>
48 #include <unistd.h>
49 #include <fcntl.h>
50 #include <signal.h>
51 #include <string.h>
53 #ifdef ENABLE_LOCALE
54 #include <locale.h>
55 #endif
57 /* time between mpd updates [ms] */
58 static const guint update_interval = 500;
60 #define BUFSIZE 1024
62 static struct mpdclient *mpd = NULL;
63 static GMainLoop *main_loop;
64 static guint reconnect_source_id, update_source_id;
65 static int sigwinch_pipes[2];
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 #ifndef WIN32
92 static void
93 catch_sigint(gcc_unused int sig)
94 {
95         g_main_loop_quit(main_loop);
96 }
98 static gboolean
99 sigwinch_event(gcc_unused GIOChannel *source,
100                gcc_unused GIOCondition condition, gcc_unused gpointer data)
102         char ignoreme[64];
103         if (1 > read(sigwinch_pipes[0], ignoreme, 64))
104                 exit(EXIT_FAILURE);
106         endwin();
107         refresh();
108         screen_resize(mpd);
110         return TRUE;
113 static void
114 catch_sigwinch(gcc_unused int sig)
116         if (1 != write(sigwinch_pipes[1], "", 1))
117                 exit(EXIT_FAILURE);
119 #endif /* WIN32 */
121 static gboolean
122 timer_mpd_update(gpointer data);
124 static void
125 enable_update_timer(void)
127         if (update_source_id != 0)
128                 return;
130         update_source_id = g_timeout_add(update_interval,
131                                          timer_mpd_update, NULL);
134 static void
135 disable_update_timer(void)
137         if (update_source_id == 0)
138                 return;
140         g_source_remove(update_source_id);
141         update_source_id = 0;
144 static bool
145 should_enable_update_timer(void)
147         return mpd->playing
148 #ifndef NCMPC_MINI
149                 || options.display_time
150 #endif
151                 ;
154 static void
155 auto_update_timer(void)
157         if (should_enable_update_timer())
158                 enable_update_timer();
159         else
160                 disable_update_timer();
163 static void
164 check_reconnect(void);
166 static void
167 do_mpd_update(void)
169         if (mpdclient_is_connected(mpd) &&
170             (mpd->events != 0 || mpd->playing))
171                 mpdclient_update(mpd);
173 #ifndef NCMPC_MINI
174         if (options.enable_xterm_title)
175                 update_xterm_title();
176 #endif
178         screen_update(mpd);
179         mpd->events = 0;
181         mpdclient_put_connection(mpd);
182         check_reconnect();
185 static char *
186 settings_name(const struct mpd_settings *settings)
188         const char *host = mpd_settings_get_host(settings);
189         if (host == NULL)
190                 host = _("unknown");
192         if (host[0] == '/')
193                 return g_strdup(host);
195         unsigned port = mpd_settings_get_port(settings);
196         if (port == 0 || port == 6600)
197                 return g_strdup(host);
199         return g_strdup_printf("%s:%u", host, port);
202 static char *
203 default_settings_name(void)
205         struct mpd_settings *settings =
206                 mpd_settings_new(options.host, options.port, 0,
207                                  NULL, options.password);
208         if (settings == NULL)
209                 return g_strdup(_("unknown"));
211         char *name = settings_name(settings);
212         mpd_settings_free(settings);
214         return name;
217 /**
218  * This timer is installed when the connection to the MPD server is
219  * broken.  It tries to recover by reconnecting periodically.
220  */
221 static gboolean
222 timer_reconnect(gcc_unused gpointer data)
224         assert(mpdclient_is_dead(mpd));
226         reconnect_source_id = 0;
228         char *name = default_settings_name();
229         screen_status_printf(_("Connecting to %s...  [Press %s to abort]"),
230                              name, get_key_names(CMD_QUIT, false));
231         g_free(name);
232         doupdate();
234         mpdclient_disconnect(mpd);
235         mpdclient_connect(mpd, options.host, options.port,
236                           options.timeout_ms,
237                           options.password);
239         return FALSE;
242 static void
243 check_reconnect(void)
245         if (mpdclient_is_dead(mpd) && reconnect_source_id == 0)
246                 /* reconnect when the connection is lost */
247                 reconnect_source_id = g_timeout_add(1000, timer_reconnect,
248                                                     NULL);
251 void
252 mpdclient_connected_callback(void)
254         assert(reconnect_source_id == 0);
256 #ifndef NCMPC_MINI
257         /* quit if mpd is pre 0.14 - song id not supported by mpd */
258         struct mpd_connection *connection = mpdclient_get_connection(mpd);
259         if (mpd_connection_cmp_server_version(connection, 0, 16, 0) < 0) {
260                 const unsigned *version =
261                         mpd_connection_get_server_version(connection);
262                 screen_status_printf(_("Error: MPD version %d.%d.%d is too old (%s needed)"),
263                                      version[0], version[1], version[2],
264                                      "0.16.0");
265                 mpdclient_disconnect(mpd);
266                 doupdate();
268                 /* try again after 30 seconds */
269                 reconnect_source_id = g_timeout_add(30000,
270                                                     timer_reconnect, NULL);
271                 return;
272         }
273 #endif
275         screen_status_clear_message();
276         doupdate();
278         /* update immediately */
279         mpd->events = MPD_IDLE_ALL;
281         do_mpd_update();
283         auto_update_timer();
286 void
287 mpdclient_failed_callback(void)
289         assert(reconnect_source_id == 0);
291         /* try again in 5 seconds */
292         reconnect_source_id = g_timeout_add(5000,
293                                             timer_reconnect, NULL);
296 void
297 mpdclient_lost_callback(void)
299         assert(reconnect_source_id == 0);
301         screen_update(mpd);
303         reconnect_source_id = g_timeout_add(1000, timer_reconnect, NULL);
306 /**
307  * This function is called by the gidle.c library when MPD sends us an
308  * idle event (or when the connection dies).
309  */
310 void
311 mpdclient_idle_callback(gcc_unused enum mpd_idle events)
313 #ifndef NCMPC_MINI
314         if (options.enable_xterm_title)
315                 update_xterm_title();
316 #endif
318         screen_update(mpd);
319         auto_update_timer();
322 static gboolean
323 timer_mpd_update(gcc_unused gpointer data)
325         do_mpd_update();
327         if (should_enable_update_timer())
328                 return true;
329         else {
330                 update_source_id = 0;
331                 return false;
332         }
335 void begin_input_event(void)
339 void end_input_event(void)
341         screen_update(mpd);
342         mpd->events = 0;
344         mpdclient_put_connection(mpd);
345         check_reconnect();
346         auto_update_timer();
349 bool
350 do_input_event(command_t cmd)
352         if (cmd == CMD_QUIT) {
353                 g_main_loop_quit(main_loop);
354                 return false;
355         }
357         screen_cmd(mpd, cmd);
359         if (cmd == CMD_VOLUME_UP || cmd == CMD_VOLUME_DOWN)
360                 /* make sure we don't update the volume yet */
361                 disable_update_timer();
363         return true;
366 #ifndef NCMPC_MINI
367 /**
368  * Check the configured key bindings for errors, and display a status
369  * message every 10 seconds.
370  */
371 static gboolean
372 timer_check_key_bindings(gcc_unused gpointer data)
374         char buf[256];
376         if (check_key_bindings(NULL, buf, sizeof(buf))) {
377                 /* no error: disable this timer for the rest of this
378                    process */
379                 check_key_bindings_source_id = 0;
380                 return FALSE;
381         }
383 #ifdef ENABLE_KEYDEF_SCREEN
384         g_strchomp(buf);
385         g_strlcat(buf, " (", sizeof(buf));
386         /* to translators: a key was bound twice in the key editor,
387            and this is a hint for the user what to press to correct
388            that */
389         char comment[64];
390         g_snprintf(comment, sizeof(comment), _("press %s for the key editor"),
391                    get_key_names(CMD_SCREEN_KEYDEF, false));
392         g_strlcat(buf, comment, sizeof(buf));
393         g_strlcat(buf, ")", sizeof(buf));
394 #endif
396         screen_status_printf("%s", buf);
398         doupdate();
399         return TRUE;
401 #endif
403 int
404 main(int argc, const char *argv[])
406 #ifdef ENABLE_LOCALE
407 #ifndef ENABLE_NLS
408         gcc_unused
409 #endif
410         const char *charset = NULL;
411         /* time and date formatting */
412         setlocale(LC_TIME,"");
413         /* care about sorting order etc */
414         setlocale(LC_COLLATE,"");
415         /* charset */
416         setlocale(LC_CTYPE,"");
417         /* initialize charset conversions */
418         charset = charset_init();
420         /* initialize i18n support */
421 #endif
423 #ifdef ENABLE_NLS
424         setlocale(LC_MESSAGES, "");
425         bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
426 #ifdef ENABLE_LOCALE
427         bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
428 #endif
429         textdomain(GETTEXT_PACKAGE);
430 #endif
432         /* initialize options */
433         options_init();
435         /* parse command line options - 1 pass get configuration files */
436         options_parse(argc, argv);
438 #ifndef NCMPC_MINI
439         /* read configuration */
440         read_configuration();
442         /* check key bindings */
443         check_key_bindings(NULL, NULL, 0);
444 #endif
446         /* parse command line options - 2 pass */
447         options_parse(argc, argv);
449 #ifndef WIN32
450         /* setup signal behavior - SIGINT */
451         struct sigaction act;
452         sigemptyset(&act.sa_mask);
453         act.sa_flags = 0;
454         act.sa_handler = catch_sigint;
455         if (sigaction(SIGINT, &act, NULL) < 0) {
456                 perror("signal");
457                 exit(EXIT_FAILURE);
458         }
460         /* setup signal behavior - SIGTERM */
462         act.sa_handler = catch_sigint;
463         if (sigaction(SIGTERM, &act, NULL) < 0) {
464                 perror("sigaction()");
465                 exit(EXIT_FAILURE);
466         }
468         /* setup signal behavior - SIGCONT */
470         act.sa_handler = catch_sigwinch;
471         if (sigaction(SIGCONT, &act, NULL) < 0) {
472                 perror("sigaction(SIGCONT)");
473                 exit(EXIT_FAILURE);
474         }
476         /* setup signal behaviour - SIGHUP*/
478         act.sa_handler = catch_sigint;
479         if (sigaction(SIGHUP, &act, NULL) < 0) {
480                 perror("sigaction(SIGHUP)");
481                 exit(EXIT_FAILURE);
482         }
484         /* setup SIGWINCH */
486         act.sa_flags = SA_RESTART;
487         act.sa_handler = catch_sigwinch;
488         if (sigaction(SIGWINCH, &act, NULL) < 0) {
489                 perror("sigaction(SIGWINCH)");
490                 exit(EXIT_FAILURE);
491         }
493         /* ignore SIGPIPE */
495         act.sa_handler = SIG_IGN;
496         if (sigaction(SIGPIPE, &act, NULL) < 0) {
497                 perror("sigaction(SIGPIPE)");
498                 exit(EXIT_FAILURE);
499         }
500 #endif
502         ncu_init();
504 #ifdef ENABLE_LYRICS_SCREEN
505         lyrics_init();
506 #endif
508         /* create mpdclient instance */
509         mpd = mpdclient_new();
511         /* initialize curses */
512         screen_init(mpd);
514         /* the main loop */
515         main_loop = g_main_loop_new(NULL, FALSE);
517         /* watch out for keyboard input */
518         keyboard_init();
520         /* watch out for lirc input */
521         ncmpc_lirc_init();
523 #ifndef WIN32
524         if (!pipe(sigwinch_pipes) &&
525                 !fcntl(sigwinch_pipes[1], F_SETFL, O_NONBLOCK)) {
526                 GIOChannel *sigwinch_channel = g_io_channel_unix_new(sigwinch_pipes[0]);
527                 g_io_add_watch(sigwinch_channel, G_IO_IN, sigwinch_event, NULL);
528                 g_io_channel_unref(sigwinch_channel);
529         }
530         else {
531                 perror("sigwinch pipe creation failed");
532                 exit(EXIT_FAILURE);
533         }
534 #endif
536         /* attempt to connect */
537         reconnect_source_id = g_timeout_add(1, timer_reconnect, NULL);
539         auto_update_timer();
541 #ifndef NCMPC_MINI
542         check_key_bindings_source_id = g_timeout_add(10000, timer_check_key_bindings, NULL);
543 #endif
545         screen_paint(mpd);
547         g_main_loop_run(main_loop);
548         g_main_loop_unref(main_loop);
550         /* cleanup */
552         cancel_seek_timer();
554         disable_update_timer();
556         if (reconnect_source_id != 0)
557                 g_source_remove(reconnect_source_id);
559 #ifndef NCMPC_MINI
560         if (check_key_bindings_source_id != 0)
561                 g_source_remove(check_key_bindings_source_id);
562 #endif
564         close(sigwinch_pipes[0]);
565         close(sigwinch_pipes[1]);
567         ncmpc_lirc_deinit();
569         screen_exit();
570 #ifndef NCMPC_MINI
571         set_xterm_title("");
572 #endif
573         printf("\n");
575         mpdclient_free(mpd);
577 #ifdef ENABLE_LYRICS_SCREEN
578         lyrics_deinit();
579 #endif
581         ncu_deinit();
582         options_deinit();
584         return 0;