Code

fe696c400ce61d5e91d79f5283335b2fc1f04001
[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 }
99 static void
100 catch_sigcont(gcc_unused int sig)
102         if (1 != write(sigwinch_pipes[1], "", 1))
103                 exit(EXIT_FAILURE);
106 void
107 sigstop(void)
109         def_prog_mode();  /* save the tty modes */
110         endwin();         /* end curses mode temporarily */
111         kill(0, SIGSTOP); /* issue SIGSTOP */
114 static gboolean
115 sigwinch_event(gcc_unused GIOChannel *source,
116                gcc_unused GIOCondition condition, gcc_unused gpointer data)
118         char ignoreme[64];
119         if (1 > read(sigwinch_pipes[0], ignoreme, 64))
120                 exit(EXIT_FAILURE);
122         endwin();
123         refresh();
124         screen_resize(mpd);
126         return TRUE;
129 static void
130 catch_sigwinch(gcc_unused int sig)
132         if (1 != write(sigwinch_pipes[1], "", 1))
133                 exit(EXIT_FAILURE);
135 #endif /* WIN32 */
137 static gboolean
138 timer_mpd_update(gpointer data);
140 static void
141 enable_update_timer(void)
143         if (update_source_id != 0)
144                 return;
146         update_source_id = g_timeout_add(update_interval,
147                                          timer_mpd_update, NULL);
150 static void
151 disable_update_timer(void)
153         if (update_source_id == 0)
154                 return;
156         g_source_remove(update_source_id);
157         update_source_id = 0;
160 static bool
161 should_enable_update_timer(void)
163         return mpd->playing
164 #ifndef NCMPC_MINI
165                 || options.display_time
166 #endif
167                 ;
170 static void
171 auto_update_timer(void)
173         if (should_enable_update_timer())
174                 enable_update_timer();
175         else
176                 disable_update_timer();
179 static void
180 check_reconnect(void);
182 static void
183 do_mpd_update(void)
185         if (mpdclient_is_connected(mpd) &&
186             (mpd->events != 0 || mpd->playing))
187                 mpdclient_update(mpd);
189 #ifndef NCMPC_MINI
190         if (options.enable_xterm_title)
191                 update_xterm_title();
192 #endif
194         screen_update(mpd);
195         mpd->events = 0;
197         mpdclient_put_connection(mpd);
198         check_reconnect();
201 static char *
202 settings_name(const struct mpd_settings *settings)
204         const char *host = mpd_settings_get_host(settings);
205         if (host == NULL)
206                 host = _("unknown");
208         if (host[0] == '/')
209                 return g_strdup(host);
211         unsigned port = mpd_settings_get_port(settings);
212         if (port == 0 || port == 6600)
213                 return g_strdup(host);
215         return g_strdup_printf("%s:%u", host, port);
218 static char *
219 default_settings_name(void)
221         struct mpd_settings *settings =
222                 mpd_settings_new(options.host, options.port, 0,
223                                  NULL, options.password);
224         if (settings == NULL)
225                 return g_strdup(_("unknown"));
227         char *name = settings_name(settings);
228         mpd_settings_free(settings);
230         return name;
233 /**
234  * This timer is installed when the connection to the MPD server is
235  * broken.  It tries to recover by reconnecting periodically.
236  */
237 static gboolean
238 timer_reconnect(gcc_unused gpointer data)
240         assert(mpdclient_is_dead(mpd));
242         reconnect_source_id = 0;
244         char *name = default_settings_name();
245         screen_status_printf(_("Connecting to %s...  [Press %s to abort]"),
246                              name, get_key_names(CMD_QUIT, false));
247         g_free(name);
248         doupdate();
250         mpdclient_disconnect(mpd);
251         mpdclient_connect(mpd, options.host, options.port,
252                           options.timeout_ms,
253                           options.password);
255         return FALSE;
258 static void
259 check_reconnect(void)
261         if (mpdclient_is_dead(mpd) && reconnect_source_id == 0)
262                 /* reconnect when the connection is lost */
263                 reconnect_source_id = g_timeout_add(1000, timer_reconnect,
264                                                     NULL);
267 void
268 mpdclient_connected_callback(void)
270         assert(reconnect_source_id == 0);
272 #ifndef NCMPC_MINI
273         /* quit if mpd is pre 0.14 - song id not supported by mpd */
274         struct mpd_connection *connection = mpdclient_get_connection(mpd);
275         if (mpd_connection_cmp_server_version(connection, 0, 16, 0) < 0) {
276                 const unsigned *version =
277                         mpd_connection_get_server_version(connection);
278                 screen_status_printf(_("Error: MPD version %d.%d.%d is too old (%s needed)"),
279                                      version[0], version[1], version[2],
280                                      "0.16.0");
281                 mpdclient_disconnect(mpd);
282                 doupdate();
284                 /* try again after 30 seconds */
285                 reconnect_source_id = g_timeout_add(30000,
286                                                     timer_reconnect, NULL);
287                 return;
288         }
289 #endif
291         screen_status_clear_message();
292         doupdate();
294         /* update immediately */
295         mpd->events = MPD_IDLE_ALL;
297         do_mpd_update();
299         auto_update_timer();
302 void
303 mpdclient_failed_callback(void)
305         assert(reconnect_source_id == 0);
307         /* try again in 5 seconds */
308         reconnect_source_id = g_timeout_add(5000,
309                                             timer_reconnect, NULL);
312 void
313 mpdclient_lost_callback(void)
315         assert(reconnect_source_id == 0);
317         screen_update(mpd);
319         reconnect_source_id = g_timeout_add(1000, timer_reconnect, NULL);
322 /**
323  * This function is called by the gidle.c library when MPD sends us an
324  * idle event (or when the connection dies).
325  */
326 void
327 mpdclient_idle_callback(gcc_unused enum mpd_idle events)
329 #ifndef NCMPC_MINI
330         if (options.enable_xterm_title)
331                 update_xterm_title();
332 #endif
334         screen_update(mpd);
335         auto_update_timer();
338 static gboolean
339 timer_mpd_update(gcc_unused gpointer data)
341         do_mpd_update();
343         if (should_enable_update_timer())
344                 return true;
345         else {
346                 update_source_id = 0;
347                 return false;
348         }
351 void begin_input_event(void)
355 void end_input_event(void)
357         screen_update(mpd);
358         mpd->events = 0;
360         mpdclient_put_connection(mpd);
361         check_reconnect();
362         auto_update_timer();
365 int do_input_event(command_t cmd)
367         if (cmd == CMD_QUIT) {
368                 g_main_loop_quit(main_loop);
369                 return -1;
370         }
372         screen_cmd(mpd, cmd);
374         if (cmd == CMD_VOLUME_UP || cmd == CMD_VOLUME_DOWN)
375                 /* make sure we don't update the volume yet */
376                 disable_update_timer();
378         return 0;
381 #ifndef NCMPC_MINI
382 /**
383  * Check the configured key bindings for errors, and display a status
384  * message every 10 seconds.
385  */
386 static gboolean
387 timer_check_key_bindings(gcc_unused gpointer data)
389         char buf[256];
391         if (check_key_bindings(NULL, buf, sizeof(buf))) {
392                 /* no error: disable this timer for the rest of this
393                    process */
394                 check_key_bindings_source_id = 0;
395                 return FALSE;
396         }
398 #ifdef ENABLE_KEYDEF_SCREEN
399         g_strchomp(buf);
400         g_strlcat(buf, " (", sizeof(buf));
401         /* to translators: a key was bound twice in the key editor,
402            and this is a hint for the user what to press to correct
403            that */
404         char comment[64];
405         g_snprintf(comment, sizeof(comment), _("press %s for the key editor"),
406                    get_key_names(CMD_SCREEN_KEYDEF, false));
407         g_strlcat(buf, comment, sizeof(buf));
408         g_strlcat(buf, ")", sizeof(buf));
409 #endif
411         screen_status_printf("%s", buf);
413         doupdate();
414         return TRUE;
416 #endif
418 int
419 main(int argc, const char *argv[])
421 #ifdef ENABLE_LOCALE
422 #ifndef ENABLE_NLS
423         gcc_unused
424 #endif
425         const char *charset = NULL;
426         /* time and date formatting */
427         setlocale(LC_TIME,"");
428         /* care about sorting order etc */
429         setlocale(LC_COLLATE,"");
430         /* charset */
431         setlocale(LC_CTYPE,"");
432         /* initialize charset conversions */
433         charset = charset_init();
435         /* initialize i18n support */
436 #endif
438 #ifdef ENABLE_NLS
439         setlocale(LC_MESSAGES, "");
440         bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
441 #ifdef ENABLE_LOCALE
442         bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
443 #endif
444         textdomain(GETTEXT_PACKAGE);
445 #endif
447         /* initialize options */
448         options_init();
450         /* parse command line options - 1 pass get configuration files */
451         options_parse(argc, argv);
453 #ifndef NCMPC_MINI
454         /* read configuration */
455         read_configuration();
457         /* check key bindings */
458         check_key_bindings(NULL, NULL, 0);
459 #endif
461         /* parse command line options - 2 pass */
462         options_parse(argc, argv);
464 #ifndef WIN32
465         /* setup signal behavior - SIGINT */
466         struct sigaction act;
467         sigemptyset(&act.sa_mask);
468         act.sa_flags = 0;
469         act.sa_handler = catch_sigint;
470         if (sigaction(SIGINT, &act, NULL) < 0) {
471                 perror("signal");
472                 exit(EXIT_FAILURE);
473         }
475         /* setup signal behavior - SIGTERM */
477         act.sa_handler = catch_sigint;
478         if (sigaction(SIGTERM, &act, NULL) < 0) {
479                 perror("sigaction()");
480                 exit(EXIT_FAILURE);
481         }
483         /* setup signal behavior - SIGCONT */
485         act.sa_handler = catch_sigcont;
486         if (sigaction(SIGCONT, &act, NULL) < 0) {
487                 perror("sigaction(SIGCONT)");
488                 exit(EXIT_FAILURE);
489         }
491         /* setup signal behaviour - SIGHUP*/
493         act.sa_handler = catch_sigint;
494         if (sigaction(SIGHUP, &act, NULL) < 0) {
495                 perror("sigaction(SIGHUP)");
496                 exit(EXIT_FAILURE);
497         }
499         /* setup SIGWINCH */
501         act.sa_flags = SA_RESTART;
502         act.sa_handler = catch_sigwinch;
503         if (sigaction(SIGWINCH, &act, NULL) < 0) {
504                 perror("sigaction(SIGWINCH)");
505                 exit(EXIT_FAILURE);
506         }
508         /* ignore SIGPIPE */
510         act.sa_handler = SIG_IGN;
511         if (sigaction(SIGPIPE, &act, NULL) < 0) {
512                 perror("sigaction(SIGPIPE)");
513                 exit(EXIT_FAILURE);
514         }
515 #endif
517         ncu_init();
519 #ifdef ENABLE_LYRICS_SCREEN
520         lyrics_init();
521 #endif
523         /* create mpdclient instance */
524         mpd = mpdclient_new();
526         /* initialize curses */
527         screen_init(mpd);
529         /* the main loop */
530         main_loop = g_main_loop_new(NULL, FALSE);
532         /* watch out for keyboard input */
533         keyboard_init();
535         /* watch out for lirc input */
536         ncmpc_lirc_init();
538 #ifndef WIN32
539         if (!pipe(sigwinch_pipes) &&
540                 !fcntl(sigwinch_pipes[1], F_SETFL, O_NONBLOCK)) {
541                 GIOChannel *sigwinch_channel = g_io_channel_unix_new(sigwinch_pipes[0]);
542                 g_io_add_watch(sigwinch_channel, G_IO_IN, sigwinch_event, NULL);
543                 g_io_channel_unref(sigwinch_channel);
544         }
545         else {
546                 perror("sigwinch pipe creation failed");
547                 exit(EXIT_FAILURE);
548         }
549 #endif
551         /* attempt to connect */
552         reconnect_source_id = g_timeout_add(1, timer_reconnect, NULL);
554         auto_update_timer();
556 #ifndef NCMPC_MINI
557         check_key_bindings_source_id = g_timeout_add(10000, timer_check_key_bindings, NULL);
558 #endif
560         screen_paint(mpd);
562         g_main_loop_run(main_loop);
563         g_main_loop_unref(main_loop);
565         /* cleanup */
567         cancel_seek_timer();
569         disable_update_timer();
571         if (reconnect_source_id != 0)
572                 g_source_remove(reconnect_source_id);
574 #ifndef NCMPC_MINI
575         if (check_key_bindings_source_id != 0)
576                 g_source_remove(check_key_bindings_source_id);
577 #endif
579         close(sigwinch_pipes[0]);
580         close(sigwinch_pipes[1]);
582         ncmpc_lirc_deinit();
584         screen_exit();
585 #ifndef NCMPC_MINI
586         set_xterm_title("");
587 #endif
588         printf("\n");
590         mpdclient_free(mpd);
592 #ifdef ENABLE_LYRICS_SCREEN
593         lyrics_deinit();
594 #endif
596         ncu_deinit();
597         options_deinit();
599         return 0;