Code

214478e39ded84a53a3ac96591888e7f96c0d19c
[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 #ifndef WIN32
48 #include <glib-unix.h>
49 #endif
51 #include <stdlib.h>
52 #include <unistd.h>
53 #include <fcntl.h>
54 #include <signal.h>
55 #include <string.h>
57 #ifdef ENABLE_LOCALE
58 #include <locale.h>
59 #endif
61 /* time between mpd updates [ms] */
62 static const guint update_interval = 500;
64 #define BUFSIZE 1024
66 static struct mpdclient *mpd = NULL;
67 static GMainLoop *main_loop;
68 static guint reconnect_source_id, update_source_id;
69 static int sigwinch_pipes[2];
71 #ifndef NCMPC_MINI
72 static guint check_key_bindings_source_id;
73 #endif
75 #ifndef NCMPC_MINI
76 static void
77 update_xterm_title(void)
78 {
79         const struct mpd_song *song = mpd->song;
81         char tmp[BUFSIZE];
82         if (options.xterm_title_format && mpd->playing && song)
83                 strfsong(tmp, BUFSIZE, options.xterm_title_format, song);
84         else
85                 g_strlcpy(tmp, PACKAGE " version " VERSION, BUFSIZE);
87         static char title[BUFSIZE];
88         if (strncmp(title, tmp, BUFSIZE)) {
89                 g_strlcpy(title, tmp, BUFSIZE);
90                 set_xterm_title("%s", title);
91         }
92 }
93 #endif
95 #ifndef WIN32
96 static gboolean
97 handle_quit_signal(gcc_unused gpointer data)
98 {
99         g_main_loop_quit(main_loop);
100         return false;
103 static gboolean
104 sigwinch_event(gcc_unused GIOChannel *source,
105                gcc_unused GIOCondition condition, gcc_unused gpointer data)
107         char ignoreme[64];
108         if (1 > read(sigwinch_pipes[0], ignoreme, 64))
109                 exit(EXIT_FAILURE);
111         endwin();
112         refresh();
113         screen_resize(mpd);
115         return TRUE;
118 static void
119 catch_sigwinch(gcc_unused int sig)
121         if (1 != write(sigwinch_pipes[1], "", 1))
122                 exit(EXIT_FAILURE);
124 #endif /* WIN32 */
126 static gboolean
127 timer_mpd_update(gpointer data);
129 static void
130 enable_update_timer(void)
132         if (update_source_id != 0)
133                 return;
135         update_source_id = g_timeout_add(update_interval,
136                                          timer_mpd_update, NULL);
139 static void
140 disable_update_timer(void)
142         if (update_source_id == 0)
143                 return;
145         g_source_remove(update_source_id);
146         update_source_id = 0;
149 static bool
150 should_enable_update_timer(void)
152         return mpd->playing
153 #ifndef NCMPC_MINI
154                 || options.display_time
155 #endif
156                 ;
159 static void
160 auto_update_timer(void)
162         if (should_enable_update_timer())
163                 enable_update_timer();
164         else
165                 disable_update_timer();
168 static void
169 check_reconnect(void);
171 static void
172 do_mpd_update(void)
174         if (mpdclient_is_connected(mpd) &&
175             (mpd->events != 0 || mpd->playing))
176                 mpdclient_update(mpd);
178 #ifndef NCMPC_MINI
179         if (options.enable_xterm_title)
180                 update_xterm_title();
181 #endif
183         screen_update(mpd);
184         mpd->events = 0;
186         mpdclient_put_connection(mpd);
187         check_reconnect();
190 static char *
191 settings_name(const struct mpd_settings *settings)
193         const char *host = mpd_settings_get_host(settings);
194         if (host == NULL)
195                 host = _("unknown");
197         if (host[0] == '/')
198                 return g_strdup(host);
200         unsigned port = mpd_settings_get_port(settings);
201         if (port == 0 || port == 6600)
202                 return g_strdup(host);
204         return g_strdup_printf("%s:%u", host, port);
207 static char *
208 default_settings_name(void)
210         struct mpd_settings *settings =
211                 mpd_settings_new(options.host, options.port, 0,
212                                  NULL, options.password);
213         if (settings == NULL)
214                 return g_strdup(_("unknown"));
216         char *name = settings_name(settings);
217         mpd_settings_free(settings);
219         return name;
222 /**
223  * This timer is installed when the connection to the MPD server is
224  * broken.  It tries to recover by reconnecting periodically.
225  */
226 static gboolean
227 timer_reconnect(gcc_unused gpointer data)
229         assert(mpdclient_is_dead(mpd));
231         reconnect_source_id = 0;
233         char *name = default_settings_name();
234         screen_status_printf(_("Connecting to %s...  [Press %s to abort]"),
235                              name, get_key_names(CMD_QUIT, false));
236         g_free(name);
237         doupdate();
239         mpdclient_disconnect(mpd);
240         mpdclient_connect(mpd, options.host, options.port,
241                           options.timeout_ms,
242                           options.password);
244         return FALSE;
247 static void
248 check_reconnect(void)
250         if (mpdclient_is_dead(mpd) && reconnect_source_id == 0)
251                 /* reconnect when the connection is lost */
252                 reconnect_source_id = g_timeout_add(1000, timer_reconnect,
253                                                     NULL);
256 void
257 mpdclient_connected_callback(void)
259         assert(reconnect_source_id == 0);
261 #ifndef NCMPC_MINI
262         /* quit if mpd is pre 0.14 - song id not supported by mpd */
263         struct mpd_connection *connection = mpdclient_get_connection(mpd);
264         if (mpd_connection_cmp_server_version(connection, 0, 16, 0) < 0) {
265                 const unsigned *version =
266                         mpd_connection_get_server_version(connection);
267                 screen_status_printf(_("Error: MPD version %d.%d.%d is too old (%s needed)"),
268                                      version[0], version[1], version[2],
269                                      "0.16.0");
270                 mpdclient_disconnect(mpd);
271                 doupdate();
273                 /* try again after 30 seconds */
274                 reconnect_source_id = g_timeout_add(30000,
275                                                     timer_reconnect, NULL);
276                 return;
277         }
278 #endif
280         screen_status_clear_message();
281         doupdate();
283         /* update immediately */
284         mpd->events = MPD_IDLE_ALL;
286         do_mpd_update();
288         auto_update_timer();
291 void
292 mpdclient_failed_callback(void)
294         assert(reconnect_source_id == 0);
296         /* try again in 5 seconds */
297         reconnect_source_id = g_timeout_add(5000,
298                                             timer_reconnect, NULL);
301 void
302 mpdclient_lost_callback(void)
304         assert(reconnect_source_id == 0);
306         screen_update(mpd);
308         reconnect_source_id = g_timeout_add(1000, timer_reconnect, NULL);
311 /**
312  * This function is called by the gidle.c library when MPD sends us an
313  * idle event (or when the connection dies).
314  */
315 void
316 mpdclient_idle_callback(gcc_unused enum mpd_idle events)
318 #ifndef NCMPC_MINI
319         if (options.enable_xterm_title)
320                 update_xterm_title();
321 #endif
323         screen_update(mpd);
324         auto_update_timer();
327 static gboolean
328 timer_mpd_update(gcc_unused gpointer data)
330         do_mpd_update();
332         if (should_enable_update_timer())
333                 return true;
334         else {
335                 update_source_id = 0;
336                 return false;
337         }
340 void begin_input_event(void)
344 void end_input_event(void)
346         screen_update(mpd);
347         mpd->events = 0;
349         mpdclient_put_connection(mpd);
350         check_reconnect();
351         auto_update_timer();
354 bool
355 do_input_event(command_t cmd)
357         if (cmd == CMD_QUIT) {
358                 g_main_loop_quit(main_loop);
359                 return false;
360         }
362         screen_cmd(mpd, cmd);
364         if (cmd == CMD_VOLUME_UP || cmd == CMD_VOLUME_DOWN)
365                 /* make sure we don't update the volume yet */
366                 disable_update_timer();
368         return true;
371 #ifndef NCMPC_MINI
372 /**
373  * Check the configured key bindings for errors, and display a status
374  * message every 10 seconds.
375  */
376 static gboolean
377 timer_check_key_bindings(gcc_unused gpointer data)
379         char buf[256];
381         if (check_key_bindings(NULL, buf, sizeof(buf))) {
382                 /* no error: disable this timer for the rest of this
383                    process */
384                 check_key_bindings_source_id = 0;
385                 return FALSE;
386         }
388 #ifdef ENABLE_KEYDEF_SCREEN
389         g_strchomp(buf);
390         g_strlcat(buf, " (", sizeof(buf));
391         /* to translators: a key was bound twice in the key editor,
392            and this is a hint for the user what to press to correct
393            that */
394         char comment[64];
395         g_snprintf(comment, sizeof(comment), _("press %s for the key editor"),
396                    get_key_names(CMD_SCREEN_KEYDEF, false));
397         g_strlcat(buf, comment, sizeof(buf));
398         g_strlcat(buf, ")", sizeof(buf));
399 #endif
401         screen_status_printf("%s", buf);
403         doupdate();
404         return TRUE;
406 #endif
408 int
409 main(int argc, const char *argv[])
411 #ifdef ENABLE_LOCALE
412 #ifndef ENABLE_NLS
413         gcc_unused
414 #endif
415         const char *charset = NULL;
416         /* time and date formatting */
417         setlocale(LC_TIME,"");
418         /* care about sorting order etc */
419         setlocale(LC_COLLATE,"");
420         /* charset */
421         setlocale(LC_CTYPE,"");
422         /* initialize charset conversions */
423         charset = charset_init();
425         /* initialize i18n support */
426 #endif
428 #ifdef ENABLE_NLS
429         setlocale(LC_MESSAGES, "");
430         bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
431 #ifdef ENABLE_LOCALE
432         bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
433 #endif
434         textdomain(GETTEXT_PACKAGE);
435 #endif
437         /* initialize options */
438         options_init();
440         /* parse command line options - 1 pass get configuration files */
441         options_parse(argc, argv);
443 #ifndef NCMPC_MINI
444         /* read configuration */
445         read_configuration();
447         /* check key bindings */
448         check_key_bindings(NULL, NULL, 0);
449 #endif
451         /* parse command line options - 2 pass */
452         options_parse(argc, argv);
454 #ifndef WIN32
455         /* setup quit signals */
456         g_unix_signal_add(SIGTERM, handle_quit_signal, NULL);
457         g_unix_signal_add(SIGINT, handle_quit_signal, NULL);
458         g_unix_signal_add(SIGHUP, handle_quit_signal, NULL);
460         /* setup signal behavior - SIGCONT */
462         struct sigaction act;
463         sigemptyset(&act.sa_mask);
464         act.sa_flags = 0;
466         act.sa_handler = catch_sigwinch;
467         if (sigaction(SIGCONT, &act, NULL) < 0) {
468                 perror("sigaction(SIGCONT)");
469                 exit(EXIT_FAILURE);
470         }
472         /* setup SIGWINCH */
474         act.sa_flags = SA_RESTART;
475         act.sa_handler = catch_sigwinch;
476         if (sigaction(SIGWINCH, &act, NULL) < 0) {
477                 perror("sigaction(SIGWINCH)");
478                 exit(EXIT_FAILURE);
479         }
481         /* ignore SIGPIPE */
483         act.sa_handler = SIG_IGN;
484         if (sigaction(SIGPIPE, &act, NULL) < 0) {
485                 perror("sigaction(SIGPIPE)");
486                 exit(EXIT_FAILURE);
487         }
488 #endif
490         ncu_init();
492 #ifdef ENABLE_LYRICS_SCREEN
493         lyrics_init();
494 #endif
496         /* create mpdclient instance */
497         mpd = mpdclient_new();
499         /* initialize curses */
500         screen_init(mpd);
502         /* the main loop */
503         main_loop = g_main_loop_new(NULL, FALSE);
505         /* watch out for keyboard input */
506         keyboard_init();
508         /* watch out for lirc input */
509         ncmpc_lirc_init();
511 #ifndef WIN32
512         if (!pipe(sigwinch_pipes) &&
513                 !fcntl(sigwinch_pipes[1], F_SETFL, O_NONBLOCK)) {
514                 GIOChannel *sigwinch_channel = g_io_channel_unix_new(sigwinch_pipes[0]);
515                 g_io_add_watch(sigwinch_channel, G_IO_IN, sigwinch_event, NULL);
516                 g_io_channel_unref(sigwinch_channel);
517         }
518         else {
519                 perror("sigwinch pipe creation failed");
520                 exit(EXIT_FAILURE);
521         }
522 #endif
524         /* attempt to connect */
525         reconnect_source_id = g_timeout_add(1, timer_reconnect, NULL);
527         auto_update_timer();
529 #ifndef NCMPC_MINI
530         check_key_bindings_source_id = g_timeout_add(10000, timer_check_key_bindings, NULL);
531 #endif
533         screen_paint(mpd);
535         g_main_loop_run(main_loop);
536         g_main_loop_unref(main_loop);
538         /* cleanup */
540         cancel_seek_timer();
542         disable_update_timer();
544         if (reconnect_source_id != 0)
545                 g_source_remove(reconnect_source_id);
547 #ifndef NCMPC_MINI
548         if (check_key_bindings_source_id != 0)
549                 g_source_remove(check_key_bindings_source_id);
550 #endif
552         close(sigwinch_pipes[0]);
553         close(sigwinch_pipes[1]);
555         ncmpc_lirc_deinit();
557         screen_exit();
558 #ifndef NCMPC_MINI
559         set_xterm_title("");
560 #endif
561         printf("\n");
563         mpdclient_free(mpd);
565 #ifdef ENABLE_LYRICS_SCREEN
566         lyrics_deinit();
567 #endif
569         ncu_deinit();
570         options_deinit();
572         return 0;