Code

af6732737d2fd26079057501819b930b4b2a6095
[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"
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         const struct mpd_song *song = mpd->song;
79         char tmp[BUFSIZE];
80         if (options.xterm_title_format && mpd->playing && song)
81                 strfsong(tmp, BUFSIZE, options.xterm_title_format, song);
82         else
83                 g_strlcpy(tmp, PACKAGE " version " VERSION, BUFSIZE);
85         static char title[BUFSIZE];
86         if (strncmp(title, tmp, BUFSIZE)) {
87                 g_strlcpy(title, tmp, BUFSIZE);
88                 set_xterm_title("%s", title);
89         }
90 }
91 #endif
93 #ifndef WIN32
94 static void
95 catch_sigint(gcc_unused int sig)
96 {
97         g_main_loop_quit(main_loop);
98 }
101 static void
102 catch_sigcont(gcc_unused int sig)
104         char irrelevant = 'a';
105         if (1 != write(sigwinch_pipes[1], &irrelevant, 1))
106                 exit(EXIT_FAILURE);
109 void
110 sigstop(void)
112         def_prog_mode();  /* save the tty modes */
113         endwin();         /* end curses mode temporarily */
114         kill(0, SIGSTOP); /* issue SIGSTOP */
117 static gboolean
118 sigwinch_event(gcc_unused GIOChannel *source,
119                gcc_unused GIOCondition condition, gcc_unused gpointer data)
121         char ignoreme[64];
122         if (1 > read(sigwinch_pipes[0], ignoreme, 64))
123                 exit(EXIT_FAILURE);
125         endwin();
126         refresh();
127         screen_resize(mpd);
129         return TRUE;
132 static void
133 catch_sigwinch(gcc_unused int sig)
135         if (1 != write(sigwinch_pipes[1], "", 1))
136                 exit(EXIT_FAILURE);
138 #endif /* WIN32 */
140 static gboolean
141 timer_mpd_update(gpointer data);
143 static void
144 enable_update_timer(void)
146         if (update_source_id != 0)
147                 return;
149         update_source_id = g_timeout_add(update_interval,
150                                          timer_mpd_update, NULL);
153 static void
154 disable_update_timer(void)
156         if (update_source_id == 0)
157                 return;
159         g_source_remove(update_source_id);
160         update_source_id = 0;
163 static bool
164 should_enable_update_timer(void)
166         return mpd->playing
167 #ifndef NCMPC_MINI
168                 || options.display_time
169 #endif
170                 ;
173 static void
174 auto_update_timer(void)
176         if (should_enable_update_timer())
177                 enable_update_timer();
178         else
179                 disable_update_timer();
182 static void
183 check_reconnect(void);
185 static void
186 do_mpd_update(void)
188         if (mpdclient_is_connected(mpd) &&
189             (mpd->events != 0 || mpd->playing))
190                 mpdclient_update(mpd);
192 #ifndef NCMPC_MINI
193         if (options.enable_xterm_title)
194                 update_xterm_title();
195 #endif
197         screen_update(mpd);
198         mpd->events = 0;
200         mpdclient_put_connection(mpd);
201         check_reconnect();
204 static char *
205 settings_name(const struct mpd_settings *settings)
207         const char *host = mpd_settings_get_host(settings);
208         if (host == NULL)
209                 host = _("unknown");
211         if (host[0] == '/')
212                 return g_strdup(host);
214         unsigned port = mpd_settings_get_port(settings);
215         if (port == 0 || port == 6600)
216                 return g_strdup(host);
218         return g_strdup_printf("%s:%u", host, port);
221 static char *
222 default_settings_name(void)
224         struct mpd_settings *settings =
225                 mpd_settings_new(options.host, options.port, 0,
226                                  NULL, options.password);
227         if (settings == NULL)
228                 return g_strdup(_("unknown"));
230         char *name = settings_name(settings);
231         mpd_settings_free(settings);
233         return name;
236 /**
237  * This timer is installed when the connection to the MPD server is
238  * broken.  It tries to recover by reconnecting periodically.
239  */
240 static gboolean
241 timer_reconnect(gcc_unused gpointer data)
243         assert(mpdclient_is_dead(mpd));
245         reconnect_source_id = 0;
247         char *name = default_settings_name();
248         screen_status_printf(_("Connecting to %s...  [Press %s to abort]"),
249                              name, get_key_names(CMD_QUIT, false));
250         g_free(name);
251         doupdate();
253         mpdclient_disconnect(mpd);
254         mpdclient_connect(mpd, options.host, options.port,
255                           options.timeout_ms,
256                           options.password);
258         return FALSE;
261 static void
262 check_reconnect(void)
264         if (mpdclient_is_dead(mpd) && reconnect_source_id == 0)
265                 /* reconnect when the connection is lost */
266                 reconnect_source_id = g_timeout_add(1000, timer_reconnect,
267                                                     NULL);
270 void
271 mpdclient_connected_callback(void)
273         assert(reconnect_source_id == 0);
275 #ifndef NCMPC_MINI
276         /* quit if mpd is pre 0.14 - song id not supported by mpd */
277         struct mpd_connection *connection = mpdclient_get_connection(mpd);
278         if (mpd_connection_cmp_server_version(connection, 0, 16, 0) < 0) {
279                 const unsigned *version =
280                         mpd_connection_get_server_version(connection);
281                 screen_status_printf(_("Error: MPD version %d.%d.%d is too old (%s needed)"),
282                                      version[0], version[1], version[2],
283                                      "0.16.0");
284                 mpdclient_disconnect(mpd);
285                 doupdate();
287                 /* try again after 30 seconds */
288                 reconnect_source_id = g_timeout_add(30000,
289                                                     timer_reconnect, NULL);
290                 return;
291         }
292 #endif
294         screen_status_clear_message();
295         doupdate();
297         /* update immediately */
298         mpd->events = MPD_IDLE_ALL;
300         do_mpd_update();
302         auto_update_timer();
305 void
306 mpdclient_failed_callback(void)
308         assert(reconnect_source_id == 0);
310         /* try again in 5 seconds */
311         reconnect_source_id = g_timeout_add(5000,
312                                             timer_reconnect, NULL);
315 void
316 mpdclient_lost_callback(void)
318         assert(reconnect_source_id == 0);
320         screen_update(mpd);
322         reconnect_source_id = g_timeout_add(1000, timer_reconnect, NULL);
325 /**
326  * This function is called by the gidle.c library when MPD sends us an
327  * idle event (or when the connection dies).
328  */
329 void
330 mpdclient_idle_callback(gcc_unused enum mpd_idle events)
332 #ifndef NCMPC_MINI
333         if (options.enable_xterm_title)
334                 update_xterm_title();
335 #endif
337         screen_update(mpd);
338         auto_update_timer();
341 static gboolean
342 timer_mpd_update(gcc_unused gpointer data)
344         do_mpd_update();
346         if (should_enable_update_timer())
347                 return true;
348         else {
349                 update_source_id = 0;
350                 return false;
351         }
354 void begin_input_event(void)
358 void end_input_event(void)
360         screen_update(mpd);
361         mpd->events = 0;
363         mpdclient_put_connection(mpd);
364         check_reconnect();
365         auto_update_timer();
368 int do_input_event(command_t cmd)
370         if (cmd == CMD_QUIT) {
371                 g_main_loop_quit(main_loop);
372                 return -1;
373         }
375         screen_cmd(mpd, cmd);
377         if (cmd == CMD_VOLUME_UP || cmd == CMD_VOLUME_DOWN)
378                 /* make sure we don't update the volume yet */
379                 disable_update_timer();
381         return 0;
384 static gboolean
385 keyboard_event(gcc_unused GIOChannel *source,
386                gcc_unused GIOCondition condition,
387                gcc_unused gpointer data)
389         begin_input_event();
391         command_t cmd = get_keyboard_command();
392         if (cmd != CMD_NONE)
393                 if (do_input_event(cmd) != 0)
394                         return FALSE;
396         end_input_event();
397         return TRUE;
400 #ifndef NCMPC_MINI
401 /**
402  * Check the configured key bindings for errors, and display a status
403  * message every 10 seconds.
404  */
405 static gboolean
406 timer_check_key_bindings(gcc_unused gpointer data)
408         char buf[256];
410         if (check_key_bindings(NULL, buf, sizeof(buf))) {
411                 /* no error: disable this timer for the rest of this
412                    process */
413                 check_key_bindings_source_id = 0;
414                 return FALSE;
415         }
417 #ifdef ENABLE_KEYDEF_SCREEN
418         g_strchomp(buf);
419         g_strlcat(buf, " (", sizeof(buf));
420         /* to translators: a key was bound twice in the key editor,
421            and this is a hint for the user what to press to correct
422            that */
423         char comment[64];
424         g_snprintf(comment, sizeof(comment), _("press %s for the key editor"),
425                    get_key_names(CMD_SCREEN_KEYDEF, false));
426         g_strlcat(buf, comment, sizeof(buf));
427         g_strlcat(buf, ")", sizeof(buf));
428 #endif
430         screen_status_printf("%s", buf);
432         doupdate();
433         return TRUE;
435 #endif
437 int
438 main(int argc, const char *argv[])
440 #ifdef ENABLE_LOCALE
441 #ifndef ENABLE_NLS
442         gcc_unused
443 #endif
444         const char *charset = NULL;
445         /* time and date formatting */
446         setlocale(LC_TIME,"");
447         /* care about sorting order etc */
448         setlocale(LC_COLLATE,"");
449         /* charset */
450         setlocale(LC_CTYPE,"");
451         /* initialize charset conversions */
452         charset = charset_init();
454         /* initialize i18n support */
455 #endif
457 #ifdef ENABLE_NLS
458         setlocale(LC_MESSAGES, "");
459         bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
460 #ifdef ENABLE_LOCALE
461         bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
462 #endif
463         textdomain(GETTEXT_PACKAGE);
464 #endif
466         /* initialize options */
467         options_init();
469         /* parse command line options - 1 pass get configuration files */
470         options_parse(argc, argv);
472 #ifndef NCMPC_MINI
473         /* read configuration */
474         read_configuration();
476         /* check key bindings */
477         check_key_bindings(NULL, NULL, 0);
478 #endif
480         /* parse command line options - 2 pass */
481         options_parse(argc, argv);
483 #ifndef WIN32
484         /* setup signal behavior - SIGINT */
485         struct sigaction act;
486         sigemptyset(&act.sa_mask);
487         act.sa_flags = 0;
488         act.sa_handler = catch_sigint;
489         if (sigaction(SIGINT, &act, NULL) < 0) {
490                 perror("signal");
491                 exit(EXIT_FAILURE);
492         }
494         /* setup signal behavior - SIGTERM */
496         act.sa_handler = catch_sigint;
497         if (sigaction(SIGTERM, &act, NULL) < 0) {
498                 perror("sigaction()");
499                 exit(EXIT_FAILURE);
500         }
502         /* setup signal behavior - SIGCONT */
504         act.sa_handler = catch_sigcont;
505         if (sigaction(SIGCONT, &act, NULL) < 0) {
506                 perror("sigaction(SIGCONT)");
507                 exit(EXIT_FAILURE);
508         }
510         /* setup signal behaviour - SIGHUP*/
512         act.sa_handler = catch_sigint;
513         if (sigaction(SIGHUP, &act, NULL) < 0) {
514                 perror("sigaction(SIGHUP)");
515                 exit(EXIT_FAILURE);
516         }
518         /* setup SIGWINCH */
520         act.sa_flags = SA_RESTART;
521         act.sa_handler = catch_sigwinch;
522         if (sigaction(SIGWINCH, &act, NULL) < 0) {
523                 perror("sigaction(SIGWINCH)");
524                 exit(EXIT_FAILURE);
525         }
527         /* ignore SIGPIPE */
529         act.sa_handler = SIG_IGN;
530         if (sigaction(SIGPIPE, &act, NULL) < 0) {
531                 perror("sigaction(SIGPIPE)");
532                 exit(EXIT_FAILURE);
533         }
534 #endif
536         ncu_init();
538 #ifdef ENABLE_LYRICS_SCREEN
539         lyrics_init();
540 #endif
542         /* create mpdclient instance */
543         mpd = mpdclient_new();
545         /* initialize curses */
546         screen_init(mpd);
548         /* the main loop */
549         main_loop = g_main_loop_new(NULL, FALSE);
551         /* watch out for keyboard input */
552         GIOChannel *keyboard_channel = g_io_channel_unix_new(STDIN_FILENO);
553         g_io_add_watch(keyboard_channel, G_IO_IN, keyboard_event, NULL);
555 #ifdef ENABLE_LIRC
556         /* watch out for lirc input */
557         int lirc_socket = ncmpc_lirc_open();
558         GIOChannel *lirc_channel = NULL;
559         if (lirc_socket >= 0) {
560                 lirc_channel = g_io_channel_unix_new(lirc_socket);
561                 g_io_add_watch(lirc_channel, G_IO_IN, lirc_event, NULL);
562         }
563 #endif
565 #ifndef WIN32
566         GIOChannel *sigwinch_channel = NULL;
567         if (!pipe(sigwinch_pipes) &&
568                 !fcntl(sigwinch_pipes[1], F_SETFL, O_NONBLOCK)) {
569                 sigwinch_channel = g_io_channel_unix_new(sigwinch_pipes[0]);
570                 g_io_add_watch(sigwinch_channel, G_IO_IN, sigwinch_event, NULL);
571         }
572         else {
573                 perror("sigwinch pipe creation failed");
574                 exit(EXIT_FAILURE);
575         }
576 #endif
578         /* attempt to connect */
579         reconnect_source_id = g_timeout_add(1, timer_reconnect, NULL);
581         auto_update_timer();
583 #ifndef NCMPC_MINI
584         check_key_bindings_source_id = g_timeout_add(10000, timer_check_key_bindings, NULL);
585 #endif
587         screen_paint(mpd);
589         g_main_loop_run(main_loop);
591         /* cleanup */
593         cancel_seek_timer();
595         disable_update_timer();
597         if (reconnect_source_id != 0)
598                 g_source_remove(reconnect_source_id);
600 #ifndef NCMPC_MINI
601         if (check_key_bindings_source_id != 0)
602                 g_source_remove(check_key_bindings_source_id);
603 #endif
605         g_main_loop_unref(main_loop);
606         g_io_channel_unref(keyboard_channel);
607         g_io_channel_unref(sigwinch_channel);
608         close(sigwinch_pipes[0]);
609         close(sigwinch_pipes[1]);
611 #ifdef ENABLE_LIRC
612         if (lirc_socket >= 0)
613                 g_io_channel_unref(lirc_channel);
614         ncmpc_lirc_close();
615 #endif
617         screen_exit();
618 #ifndef NCMPC_MINI
619         set_xterm_title("");
620 #endif
621         printf("\n");
623         mpdclient_free(mpd);
625 #ifdef ENABLE_LYRICS_SCREEN
626         lyrics_deinit();
627 #endif
629         ncu_deinit();
630         options_deinit();
632         return 0;