Code

8d07b426725ef90420611309e47ceac7e07469c1
[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"
36 #include "signals.h"
38 #ifndef NCMPC_MINI
39 #include "conf.h"
40 #endif
42 #ifdef ENABLE_LYRICS_SCREEN
43 #include "lyrics.h"
44 #endif
46 #include <mpd/client.h>
48 #include <stdlib.h>
49 #include <unistd.h>
50 #include <fcntl.h>
51 #include <signal.h>
52 #include <string.h>
54 #ifdef ENABLE_LOCALE
55 #include <locale.h>
56 #endif
58 /* time between mpd updates [ms] */
59 static const guint update_interval = 500;
61 #define BUFSIZE 1024
63 static struct mpdclient *mpd = NULL;
64 static GMainLoop *main_loop;
65 static guint reconnect_source_id, update_source_id;
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 static gboolean
92 timer_mpd_update(gpointer data);
94 static void
95 enable_update_timer(void)
96 {
97         if (update_source_id != 0)
98                 return;
100         update_source_id = g_timeout_add(update_interval,
101                                          timer_mpd_update, NULL);
104 static void
105 disable_update_timer(void)
107         if (update_source_id == 0)
108                 return;
110         g_source_remove(update_source_id);
111         update_source_id = 0;
114 static bool
115 should_enable_update_timer(void)
117         return mpd->playing
118 #ifndef NCMPC_MINI
119                 || options.display_time
120 #endif
121                 ;
124 static void
125 auto_update_timer(void)
127         if (should_enable_update_timer())
128                 enable_update_timer();
129         else
130                 disable_update_timer();
133 static void
134 check_reconnect(void);
136 static void
137 do_mpd_update(void)
139         if (mpdclient_is_connected(mpd) &&
140             (mpd->events != 0 || mpd->playing))
141                 mpdclient_update(mpd);
143 #ifndef NCMPC_MINI
144         if (options.enable_xterm_title)
145                 update_xterm_title();
146 #endif
148         screen_update(mpd);
149         mpd->events = 0;
151         mpdclient_put_connection(mpd);
152         check_reconnect();
155 static char *
156 settings_name(const struct mpd_settings *settings)
158         const char *host = mpd_settings_get_host(settings);
159         if (host == NULL)
160                 host = _("unknown");
162         if (host[0] == '/')
163                 return g_strdup(host);
165         unsigned port = mpd_settings_get_port(settings);
166         if (port == 0 || port == 6600)
167                 return g_strdup(host);
169         return g_strdup_printf("%s:%u", host, port);
172 static char *
173 default_settings_name(void)
175         struct mpd_settings *settings =
176                 mpd_settings_new(options.host, options.port, 0,
177                                  NULL, options.password);
178         if (settings == NULL)
179                 return g_strdup(_("unknown"));
181         char *name = settings_name(settings);
182         mpd_settings_free(settings);
184         return name;
187 /**
188  * This timer is installed when the connection to the MPD server is
189  * broken.  It tries to recover by reconnecting periodically.
190  */
191 static gboolean
192 timer_reconnect(gcc_unused gpointer data)
194         assert(mpdclient_is_dead(mpd));
196         reconnect_source_id = 0;
198         char *name = default_settings_name();
199         screen_status_printf(_("Connecting to %s...  [Press %s to abort]"),
200                              name, get_key_names(CMD_QUIT, false));
201         g_free(name);
202         doupdate();
204         mpdclient_connect(mpd);
206         return FALSE;
209 static void
210 check_reconnect(void)
212         if (mpdclient_is_dead(mpd) && reconnect_source_id == 0)
213                 /* reconnect when the connection is lost */
214                 reconnect_source_id = g_timeout_add(1000, timer_reconnect,
215                                                     NULL);
218 void
219 mpdclient_connected_callback(void)
221         assert(reconnect_source_id == 0);
223 #ifndef NCMPC_MINI
224         /* quit if mpd is pre 0.14 - song id not supported by mpd */
225         struct mpd_connection *connection = mpdclient_get_connection(mpd);
226         if (mpd_connection_cmp_server_version(connection, 0, 16, 0) < 0) {
227                 const unsigned *version =
228                         mpd_connection_get_server_version(connection);
229                 screen_status_printf(_("Error: MPD version %d.%d.%d is too old (%s needed)"),
230                                      version[0], version[1], version[2],
231                                      "0.16.0");
232                 mpdclient_disconnect(mpd);
233                 doupdate();
235                 /* try again after 30 seconds */
236                 reconnect_source_id = g_timeout_add(30000,
237                                                     timer_reconnect, NULL);
238                 return;
239         }
240 #endif
242         screen_status_clear_message();
243         doupdate();
245         /* update immediately */
246         mpd->events = MPD_IDLE_ALL;
248         do_mpd_update();
250         auto_update_timer();
253 void
254 mpdclient_failed_callback(void)
256         assert(reconnect_source_id == 0);
258         /* try again in 5 seconds */
259         reconnect_source_id = g_timeout_add(5000,
260                                             timer_reconnect, NULL);
263 void
264 mpdclient_lost_callback(void)
266         assert(reconnect_source_id == 0);
268         screen_update(mpd);
270         reconnect_source_id = g_timeout_add(1000, timer_reconnect, NULL);
273 /**
274  * This function is called by the gidle.c library when MPD sends us an
275  * idle event (or when the connection dies).
276  */
277 void
278 mpdclient_idle_callback(gcc_unused enum mpd_idle events)
280 #ifndef NCMPC_MINI
281         if (options.enable_xterm_title)
282                 update_xterm_title();
283 #endif
285         screen_update(mpd);
286         auto_update_timer();
289 static gboolean
290 timer_mpd_update(gcc_unused gpointer data)
292         do_mpd_update();
294         if (should_enable_update_timer())
295                 return true;
296         else {
297                 update_source_id = 0;
298                 return false;
299         }
302 void begin_input_event(void)
306 void end_input_event(void)
308         screen_update(mpd);
309         mpd->events = 0;
311         mpdclient_put_connection(mpd);
312         check_reconnect();
313         auto_update_timer();
316 bool
317 do_input_event(command_t cmd)
319         if (cmd == CMD_QUIT) {
320                 g_main_loop_quit(main_loop);
321                 return false;
322         }
324         screen_cmd(mpd, cmd);
326         if (cmd == CMD_VOLUME_UP || cmd == CMD_VOLUME_DOWN)
327                 /* make sure we don't update the volume yet */
328                 disable_update_timer();
330         return true;
333 #ifndef NCMPC_MINI
334 /**
335  * Check the configured key bindings for errors, and display a status
336  * message every 10 seconds.
337  */
338 static gboolean
339 timer_check_key_bindings(gcc_unused gpointer data)
341         char buf[256];
343         if (check_key_bindings(NULL, buf, sizeof(buf))) {
344                 /* no error: disable this timer for the rest of this
345                    process */
346                 check_key_bindings_source_id = 0;
347                 return FALSE;
348         }
350 #ifdef ENABLE_KEYDEF_SCREEN
351         g_strchomp(buf);
352         g_strlcat(buf, " (", sizeof(buf));
353         /* to translators: a key was bound twice in the key editor,
354            and this is a hint for the user what to press to correct
355            that */
356         char comment[64];
357         g_snprintf(comment, sizeof(comment), _("press %s for the key editor"),
358                    get_key_names(CMD_SCREEN_KEYDEF, false));
359         g_strlcat(buf, comment, sizeof(buf));
360         g_strlcat(buf, ")", sizeof(buf));
361 #endif
363         screen_status_printf("%s", buf);
365         doupdate();
366         return TRUE;
368 #endif
370 int
371 main(int argc, const char *argv[])
373 #ifdef ENABLE_LOCALE
374 #ifndef ENABLE_NLS
375         gcc_unused
376 #endif
377         const char *charset = NULL;
378         /* time and date formatting */
379         setlocale(LC_TIME,"");
380         /* care about sorting order etc */
381         setlocale(LC_COLLATE,"");
382         /* charset */
383         setlocale(LC_CTYPE,"");
384         /* initialize charset conversions */
385         charset = charset_init();
387         /* initialize i18n support */
388 #endif
390 #ifdef ENABLE_NLS
391         setlocale(LC_MESSAGES, "");
392         bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
393 #ifdef ENABLE_LOCALE
394         bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
395 #endif
396         textdomain(GETTEXT_PACKAGE);
397 #endif
399         /* initialize options */
400         options_init();
402         /* parse command line options - 1 pass get configuration files */
403         options_parse(argc, argv);
405 #ifndef NCMPC_MINI
406         /* read configuration */
407         read_configuration();
409         /* check key bindings */
410         check_key_bindings(NULL, NULL, 0);
411 #endif
413         /* parse command line options - 2 pass */
414         options_parse(argc, argv);
416         ncu_init();
418 #ifdef ENABLE_LYRICS_SCREEN
419         lyrics_init();
420 #endif
422         /* create mpdclient instance */
423         mpd = mpdclient_new(options.host, options.port,
424                             options.timeout_ms,
425                             options.password);
427         /* initialize curses */
428         screen_init(mpd);
430         /* the main loop */
431         main_loop = g_main_loop_new(NULL, FALSE);
433         /* watch out for keyboard input */
434         keyboard_init();
436         /* watch out for lirc input */
437         ncmpc_lirc_init();
439         signals_init(main_loop, mpd);
441         /* attempt to connect */
442         reconnect_source_id = g_timeout_add(1, timer_reconnect, NULL);
444         auto_update_timer();
446 #ifndef NCMPC_MINI
447         check_key_bindings_source_id = g_timeout_add(10000, timer_check_key_bindings, NULL);
448 #endif
450         screen_paint(mpd);
452         g_main_loop_run(main_loop);
453         g_main_loop_unref(main_loop);
455         /* cleanup */
457         cancel_seek_timer();
459         disable_update_timer();
461         if (reconnect_source_id != 0)
462                 g_source_remove(reconnect_source_id);
464 #ifndef NCMPC_MINI
465         if (check_key_bindings_source_id != 0)
466                 g_source_remove(check_key_bindings_source_id);
467 #endif
469         signals_deinit();
470         ncmpc_lirc_deinit();
472         screen_exit();
473 #ifndef NCMPC_MINI
474         set_xterm_title("");
475 #endif
476         printf("\n");
478         mpdclient_free(mpd);
480 #ifdef ENABLE_LYRICS_SCREEN
481         lyrics_deinit();
482 #endif
484         ncu_deinit();
485         options_deinit();
487         return 0;