Code

main: no delay for initial collect
[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 do_mpd_update(void)
136         if (mpdclient_is_connected(mpd) &&
137             (mpd->events != 0 || mpd->playing))
138                 mpdclient_update(mpd);
140 #ifndef NCMPC_MINI
141         if (options.enable_xterm_title)
142                 update_xterm_title();
143 #endif
145         screen_update(mpd);
146         mpd->events = 0;
149 static char *
150 settings_name(const struct mpd_settings *settings)
152         const char *host = mpd_settings_get_host(settings);
153         if (host == NULL)
154                 host = _("unknown");
156         if (host[0] == '/')
157                 return g_strdup(host);
159         unsigned port = mpd_settings_get_port(settings);
160         if (port == 0 || port == 6600)
161                 return g_strdup(host);
163         return g_strdup_printf("%s:%u", host, port);
166 static char *
167 default_settings_name(void)
169         struct mpd_settings *settings =
170                 mpd_settings_new(options.host, options.port, 0,
171                                  NULL, options.password);
172         if (settings == NULL)
173                 return g_strdup(_("unknown"));
175         char *name = settings_name(settings);
176         mpd_settings_free(settings);
178         return name;
181 /**
182  * This timer is installed when the connection to the MPD server is
183  * broken.  It tries to recover by reconnecting periodically.
184  */
185 static gboolean
186 timer_reconnect(gcc_unused gpointer data)
188         assert(mpdclient_is_dead(mpd));
190         reconnect_source_id = 0;
192         char *name = default_settings_name();
193         screen_status_printf(_("Connecting to %s...  [Press %s to abort]"),
194                              name, get_key_names(CMD_QUIT, false));
195         g_free(name);
196         doupdate();
198         mpdclient_connect(mpd);
200         return FALSE;
203 void
204 mpdclient_connected_callback(void)
206         assert(reconnect_source_id == 0);
208 #ifndef NCMPC_MINI
209         /* quit if mpd is pre 0.14 - song id not supported by mpd */
210         struct mpd_connection *connection = mpdclient_get_connection(mpd);
211         if (mpd_connection_cmp_server_version(connection, 0, 16, 0) < 0) {
212                 const unsigned *version =
213                         mpd_connection_get_server_version(connection);
214                 screen_status_printf(_("Error: MPD version %d.%d.%d is too old (%s needed)"),
215                                      version[0], version[1], version[2],
216                                      "0.16.0");
217                 mpdclient_disconnect(mpd);
218                 doupdate();
220                 /* try again after 30 seconds */
221                 reconnect_source_id = g_timeout_add(30000,
222                                                     timer_reconnect, NULL);
223                 return;
224         }
225 #endif
227         screen_status_clear_message();
228         doupdate();
230         /* update immediately */
231         mpd->events = MPD_IDLE_ALL;
233         do_mpd_update();
235         auto_update_timer();
238 void
239 mpdclient_failed_callback(void)
241         assert(reconnect_source_id == 0);
243         /* try again in 5 seconds */
244         reconnect_source_id = g_timeout_add(5000,
245                                             timer_reconnect, NULL);
248 void
249 mpdclient_lost_callback(void)
251         assert(reconnect_source_id == 0);
253         screen_update(mpd);
255         reconnect_source_id = g_timeout_add(1000, timer_reconnect, NULL);
258 /**
259  * This function is called by the gidle.c library when MPD sends us an
260  * idle event (or when the connection dies).
261  */
262 void
263 mpdclient_idle_callback(gcc_unused enum mpd_idle events)
265 #ifndef NCMPC_MINI
266         if (options.enable_xterm_title)
267                 update_xterm_title();
268 #endif
270         screen_update(mpd);
271         auto_update_timer();
274 static gboolean
275 timer_mpd_update(gcc_unused gpointer data)
277         do_mpd_update();
279         if (should_enable_update_timer())
280                 return true;
281         else {
282                 update_source_id = 0;
283                 return false;
284         }
287 void begin_input_event(void)
291 void end_input_event(void)
293         screen_update(mpd);
294         mpd->events = 0;
296         auto_update_timer();
299 bool
300 do_input_event(command_t cmd)
302         if (cmd == CMD_QUIT) {
303                 g_main_loop_quit(main_loop);
304                 return false;
305         }
307         screen_cmd(mpd, cmd);
309         if (cmd == CMD_VOLUME_UP || cmd == CMD_VOLUME_DOWN)
310                 /* make sure we don't update the volume yet */
311                 disable_update_timer();
313         return true;
316 #ifndef NCMPC_MINI
317 /**
318  * Check the configured key bindings for errors, and display a status
319  * message every 10 seconds.
320  */
321 static gboolean
322 timer_check_key_bindings(gcc_unused gpointer data)
324         char buf[256];
326         if (check_key_bindings(NULL, buf, sizeof(buf))) {
327                 /* no error: disable this timer for the rest of this
328                    process */
329                 check_key_bindings_source_id = 0;
330                 return FALSE;
331         }
333 #ifdef ENABLE_KEYDEF_SCREEN
334         g_strchomp(buf);
335         g_strlcat(buf, " (", sizeof(buf));
336         /* to translators: a key was bound twice in the key editor,
337            and this is a hint for the user what to press to correct
338            that */
339         char comment[64];
340         g_snprintf(comment, sizeof(comment), _("press %s for the key editor"),
341                    get_key_names(CMD_SCREEN_KEYDEF, false));
342         g_strlcat(buf, comment, sizeof(buf));
343         g_strlcat(buf, ")", sizeof(buf));
344 #endif
346         screen_status_printf("%s", buf);
348         doupdate();
349         return TRUE;
351 #endif
353 int
354 main(int argc, const char *argv[])
356 #ifdef ENABLE_LOCALE
357 #ifndef ENABLE_NLS
358         gcc_unused
359 #endif
360         const char *charset = NULL;
361         /* time and date formatting */
362         setlocale(LC_TIME,"");
363         /* care about sorting order etc */
364         setlocale(LC_COLLATE,"");
365         /* charset */
366         setlocale(LC_CTYPE,"");
367         /* initialize charset conversions */
368         charset = charset_init();
370         /* initialize i18n support */
371 #endif
373 #ifdef ENABLE_NLS
374         setlocale(LC_MESSAGES, "");
375         bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
376 #ifdef ENABLE_LOCALE
377         bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
378 #endif
379         textdomain(GETTEXT_PACKAGE);
380 #endif
382         /* initialize options */
383         options_init();
385         /* parse command line options - 1 pass get configuration files */
386         options_parse(argc, argv);
388 #ifndef NCMPC_MINI
389         /* read configuration */
390         read_configuration();
392         /* check key bindings */
393         check_key_bindings(NULL, NULL, 0);
394 #endif
396         /* parse command line options - 2 pass */
397         options_parse(argc, argv);
399         ncu_init();
401 #ifdef ENABLE_LYRICS_SCREEN
402         lyrics_init();
403 #endif
405         /* create mpdclient instance */
406         mpd = mpdclient_new(options.host, options.port,
407                             options.timeout_ms,
408                             options.password);
410         /* initialize curses */
411         screen_init(mpd);
413         /* the main loop */
414         main_loop = g_main_loop_new(NULL, FALSE);
416         /* watch out for keyboard input */
417         keyboard_init();
419         /* watch out for lirc input */
420         ncmpc_lirc_init();
422         signals_init(main_loop, mpd);
424         /* attempt to connect */
425         reconnect_source_id = g_idle_add(timer_reconnect, NULL);
427         auto_update_timer();
429 #ifndef NCMPC_MINI
430         check_key_bindings_source_id = g_timeout_add(10000, timer_check_key_bindings, NULL);
431 #endif
433         screen_paint(mpd);
435         g_main_loop_run(main_loop);
436         g_main_loop_unref(main_loop);
438         /* cleanup */
440         cancel_seek_timer();
442         disable_update_timer();
444         if (reconnect_source_id != 0)
445                 g_source_remove(reconnect_source_id);
447 #ifndef NCMPC_MINI
448         if (check_key_bindings_source_id != 0)
449                 g_source_remove(check_key_bindings_source_id);
450 #endif
452         signals_deinit();
453         ncmpc_lirc_deinit();
455         screen_exit();
456 #ifndef NCMPC_MINI
457         set_xterm_title("");
458 #endif
459         printf("\n");
461         mpdclient_free(mpd);
463 #ifdef ENABLE_LYRICS_SCREEN
464         lyrics_deinit();
465 #endif
467         ncu_deinit();
468         options_deinit();
470         return 0;