Code

47a5670cef21e810a063d487e017678ddf93b306
[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 bool
366 do_input_event(command_t cmd)
368         if (cmd == CMD_QUIT) {
369                 g_main_loop_quit(main_loop);
370                 return false;
371         }
373         screen_cmd(mpd, cmd);
375         if (cmd == CMD_VOLUME_UP || cmd == CMD_VOLUME_DOWN)
376                 /* make sure we don't update the volume yet */
377                 disable_update_timer();
379         return true;
382 #ifndef NCMPC_MINI
383 /**
384  * Check the configured key bindings for errors, and display a status
385  * message every 10 seconds.
386  */
387 static gboolean
388 timer_check_key_bindings(gcc_unused gpointer data)
390         char buf[256];
392         if (check_key_bindings(NULL, buf, sizeof(buf))) {
393                 /* no error: disable this timer for the rest of this
394                    process */
395                 check_key_bindings_source_id = 0;
396                 return FALSE;
397         }
399 #ifdef ENABLE_KEYDEF_SCREEN
400         g_strchomp(buf);
401         g_strlcat(buf, " (", sizeof(buf));
402         /* to translators: a key was bound twice in the key editor,
403            and this is a hint for the user what to press to correct
404            that */
405         char comment[64];
406         g_snprintf(comment, sizeof(comment), _("press %s for the key editor"),
407                    get_key_names(CMD_SCREEN_KEYDEF, false));
408         g_strlcat(buf, comment, sizeof(buf));
409         g_strlcat(buf, ")", sizeof(buf));
410 #endif
412         screen_status_printf("%s", buf);
414         doupdate();
415         return TRUE;
417 #endif
419 int
420 main(int argc, const char *argv[])
422 #ifdef ENABLE_LOCALE
423 #ifndef ENABLE_NLS
424         gcc_unused
425 #endif
426         const char *charset = NULL;
427         /* time and date formatting */
428         setlocale(LC_TIME,"");
429         /* care about sorting order etc */
430         setlocale(LC_COLLATE,"");
431         /* charset */
432         setlocale(LC_CTYPE,"");
433         /* initialize charset conversions */
434         charset = charset_init();
436         /* initialize i18n support */
437 #endif
439 #ifdef ENABLE_NLS
440         setlocale(LC_MESSAGES, "");
441         bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
442 #ifdef ENABLE_LOCALE
443         bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
444 #endif
445         textdomain(GETTEXT_PACKAGE);
446 #endif
448         /* initialize options */
449         options_init();
451         /* parse command line options - 1 pass get configuration files */
452         options_parse(argc, argv);
454 #ifndef NCMPC_MINI
455         /* read configuration */
456         read_configuration();
458         /* check key bindings */
459         check_key_bindings(NULL, NULL, 0);
460 #endif
462         /* parse command line options - 2 pass */
463         options_parse(argc, argv);
465 #ifndef WIN32
466         /* setup signal behavior - SIGINT */
467         struct sigaction act;
468         sigemptyset(&act.sa_mask);
469         act.sa_flags = 0;
470         act.sa_handler = catch_sigint;
471         if (sigaction(SIGINT, &act, NULL) < 0) {
472                 perror("signal");
473                 exit(EXIT_FAILURE);
474         }
476         /* setup signal behavior - SIGTERM */
478         act.sa_handler = catch_sigint;
479         if (sigaction(SIGTERM, &act, NULL) < 0) {
480                 perror("sigaction()");
481                 exit(EXIT_FAILURE);
482         }
484         /* setup signal behavior - SIGCONT */
486         act.sa_handler = catch_sigcont;
487         if (sigaction(SIGCONT, &act, NULL) < 0) {
488                 perror("sigaction(SIGCONT)");
489                 exit(EXIT_FAILURE);
490         }
492         /* setup signal behaviour - SIGHUP*/
494         act.sa_handler = catch_sigint;
495         if (sigaction(SIGHUP, &act, NULL) < 0) {
496                 perror("sigaction(SIGHUP)");
497                 exit(EXIT_FAILURE);
498         }
500         /* setup SIGWINCH */
502         act.sa_flags = SA_RESTART;
503         act.sa_handler = catch_sigwinch;
504         if (sigaction(SIGWINCH, &act, NULL) < 0) {
505                 perror("sigaction(SIGWINCH)");
506                 exit(EXIT_FAILURE);
507         }
509         /* ignore SIGPIPE */
511         act.sa_handler = SIG_IGN;
512         if (sigaction(SIGPIPE, &act, NULL) < 0) {
513                 perror("sigaction(SIGPIPE)");
514                 exit(EXIT_FAILURE);
515         }
516 #endif
518         ncu_init();
520 #ifdef ENABLE_LYRICS_SCREEN
521         lyrics_init();
522 #endif
524         /* create mpdclient instance */
525         mpd = mpdclient_new();
527         /* initialize curses */
528         screen_init(mpd);
530         /* the main loop */
531         main_loop = g_main_loop_new(NULL, FALSE);
533         /* watch out for keyboard input */
534         keyboard_init();
536         /* watch out for lirc input */
537         ncmpc_lirc_init();
539 #ifndef WIN32
540         if (!pipe(sigwinch_pipes) &&
541                 !fcntl(sigwinch_pipes[1], F_SETFL, O_NONBLOCK)) {
542                 GIOChannel *sigwinch_channel = g_io_channel_unix_new(sigwinch_pipes[0]);
543                 g_io_add_watch(sigwinch_channel, G_IO_IN, sigwinch_event, NULL);
544                 g_io_channel_unref(sigwinch_channel);
545         }
546         else {
547                 perror("sigwinch pipe creation failed");
548                 exit(EXIT_FAILURE);
549         }
550 #endif
552         /* attempt to connect */
553         reconnect_source_id = g_timeout_add(1, timer_reconnect, NULL);
555         auto_update_timer();
557 #ifndef NCMPC_MINI
558         check_key_bindings_source_id = g_timeout_add(10000, timer_check_key_bindings, NULL);
559 #endif
561         screen_paint(mpd);
563         g_main_loop_run(main_loop);
564         g_main_loop_unref(main_loop);
566         /* cleanup */
568         cancel_seek_timer();
570         disable_update_timer();
572         if (reconnect_source_id != 0)
573                 g_source_remove(reconnect_source_id);
575 #ifndef NCMPC_MINI
576         if (check_key_bindings_source_id != 0)
577                 g_source_remove(check_key_bindings_source_id);
578 #endif
580         close(sigwinch_pipes[0]);
581         close(sigwinch_pipes[1]);
583         ncmpc_lirc_deinit();
585         screen_exit();
586 #ifndef NCMPC_MINI
587         set_xterm_title("");
588 #endif
589         printf("\n");
591         mpdclient_free(mpd);
593 #ifdef ENABLE_LYRICS_SCREEN
594         lyrics_deinit();
595 #endif
597         ncu_deinit();
598         options_deinit();
600         return 0;