Code

mpdclient: automatically re-enter idle mode, eliminate mpdclient_put_connection()
[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         check_reconnect();
154 static char *
155 settings_name(const struct mpd_settings *settings)
157         const char *host = mpd_settings_get_host(settings);
158         if (host == NULL)
159                 host = _("unknown");
161         if (host[0] == '/')
162                 return g_strdup(host);
164         unsigned port = mpd_settings_get_port(settings);
165         if (port == 0 || port == 6600)
166                 return g_strdup(host);
168         return g_strdup_printf("%s:%u", host, port);
171 static char *
172 default_settings_name(void)
174         struct mpd_settings *settings =
175                 mpd_settings_new(options.host, options.port, 0,
176                                  NULL, options.password);
177         if (settings == NULL)
178                 return g_strdup(_("unknown"));
180         char *name = settings_name(settings);
181         mpd_settings_free(settings);
183         return name;
186 /**
187  * This timer is installed when the connection to the MPD server is
188  * broken.  It tries to recover by reconnecting periodically.
189  */
190 static gboolean
191 timer_reconnect(gcc_unused gpointer data)
193         assert(mpdclient_is_dead(mpd));
195         reconnect_source_id = 0;
197         char *name = default_settings_name();
198         screen_status_printf(_("Connecting to %s...  [Press %s to abort]"),
199                              name, get_key_names(CMD_QUIT, false));
200         g_free(name);
201         doupdate();
203         mpdclient_connect(mpd);
205         return FALSE;
208 static void
209 check_reconnect(void)
211         if (mpdclient_is_dead(mpd) && reconnect_source_id == 0)
212                 /* reconnect when the connection is lost */
213                 reconnect_source_id = g_timeout_add(1000, timer_reconnect,
214                                                     NULL);
217 void
218 mpdclient_connected_callback(void)
220         assert(reconnect_source_id == 0);
222 #ifndef NCMPC_MINI
223         /* quit if mpd is pre 0.14 - song id not supported by mpd */
224         struct mpd_connection *connection = mpdclient_get_connection(mpd);
225         if (mpd_connection_cmp_server_version(connection, 0, 16, 0) < 0) {
226                 const unsigned *version =
227                         mpd_connection_get_server_version(connection);
228                 screen_status_printf(_("Error: MPD version %d.%d.%d is too old (%s needed)"),
229                                      version[0], version[1], version[2],
230                                      "0.16.0");
231                 mpdclient_disconnect(mpd);
232                 doupdate();
234                 /* try again after 30 seconds */
235                 reconnect_source_id = g_timeout_add(30000,
236                                                     timer_reconnect, NULL);
237                 return;
238         }
239 #endif
241         screen_status_clear_message();
242         doupdate();
244         /* update immediately */
245         mpd->events = MPD_IDLE_ALL;
247         do_mpd_update();
249         auto_update_timer();
252 void
253 mpdclient_failed_callback(void)
255         assert(reconnect_source_id == 0);
257         /* try again in 5 seconds */
258         reconnect_source_id = g_timeout_add(5000,
259                                             timer_reconnect, NULL);
262 void
263 mpdclient_lost_callback(void)
265         assert(reconnect_source_id == 0);
267         screen_update(mpd);
269         reconnect_source_id = g_timeout_add(1000, timer_reconnect, NULL);
272 /**
273  * This function is called by the gidle.c library when MPD sends us an
274  * idle event (or when the connection dies).
275  */
276 void
277 mpdclient_idle_callback(gcc_unused enum mpd_idle events)
279 #ifndef NCMPC_MINI
280         if (options.enable_xterm_title)
281                 update_xterm_title();
282 #endif
284         screen_update(mpd);
285         auto_update_timer();
288 static gboolean
289 timer_mpd_update(gcc_unused gpointer data)
291         do_mpd_update();
293         if (should_enable_update_timer())
294                 return true;
295         else {
296                 update_source_id = 0;
297                 return false;
298         }
301 void begin_input_event(void)
305 void end_input_event(void)
307         screen_update(mpd);
308         mpd->events = 0;
310         check_reconnect();
311         auto_update_timer();
314 bool
315 do_input_event(command_t cmd)
317         if (cmd == CMD_QUIT) {
318                 g_main_loop_quit(main_loop);
319                 return false;
320         }
322         screen_cmd(mpd, cmd);
324         if (cmd == CMD_VOLUME_UP || cmd == CMD_VOLUME_DOWN)
325                 /* make sure we don't update the volume yet */
326                 disable_update_timer();
328         return true;
331 #ifndef NCMPC_MINI
332 /**
333  * Check the configured key bindings for errors, and display a status
334  * message every 10 seconds.
335  */
336 static gboolean
337 timer_check_key_bindings(gcc_unused gpointer data)
339         char buf[256];
341         if (check_key_bindings(NULL, buf, sizeof(buf))) {
342                 /* no error: disable this timer for the rest of this
343                    process */
344                 check_key_bindings_source_id = 0;
345                 return FALSE;
346         }
348 #ifdef ENABLE_KEYDEF_SCREEN
349         g_strchomp(buf);
350         g_strlcat(buf, " (", sizeof(buf));
351         /* to translators: a key was bound twice in the key editor,
352            and this is a hint for the user what to press to correct
353            that */
354         char comment[64];
355         g_snprintf(comment, sizeof(comment), _("press %s for the key editor"),
356                    get_key_names(CMD_SCREEN_KEYDEF, false));
357         g_strlcat(buf, comment, sizeof(buf));
358         g_strlcat(buf, ")", sizeof(buf));
359 #endif
361         screen_status_printf("%s", buf);
363         doupdate();
364         return TRUE;
366 #endif
368 int
369 main(int argc, const char *argv[])
371 #ifdef ENABLE_LOCALE
372 #ifndef ENABLE_NLS
373         gcc_unused
374 #endif
375         const char *charset = NULL;
376         /* time and date formatting */
377         setlocale(LC_TIME,"");
378         /* care about sorting order etc */
379         setlocale(LC_COLLATE,"");
380         /* charset */
381         setlocale(LC_CTYPE,"");
382         /* initialize charset conversions */
383         charset = charset_init();
385         /* initialize i18n support */
386 #endif
388 #ifdef ENABLE_NLS
389         setlocale(LC_MESSAGES, "");
390         bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
391 #ifdef ENABLE_LOCALE
392         bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
393 #endif
394         textdomain(GETTEXT_PACKAGE);
395 #endif
397         /* initialize options */
398         options_init();
400         /* parse command line options - 1 pass get configuration files */
401         options_parse(argc, argv);
403 #ifndef NCMPC_MINI
404         /* read configuration */
405         read_configuration();
407         /* check key bindings */
408         check_key_bindings(NULL, NULL, 0);
409 #endif
411         /* parse command line options - 2 pass */
412         options_parse(argc, argv);
414         ncu_init();
416 #ifdef ENABLE_LYRICS_SCREEN
417         lyrics_init();
418 #endif
420         /* create mpdclient instance */
421         mpd = mpdclient_new(options.host, options.port,
422                             options.timeout_ms,
423                             options.password);
425         /* initialize curses */
426         screen_init(mpd);
428         /* the main loop */
429         main_loop = g_main_loop_new(NULL, FALSE);
431         /* watch out for keyboard input */
432         keyboard_init();
434         /* watch out for lirc input */
435         ncmpc_lirc_init();
437         signals_init(main_loop, mpd);
439         /* attempt to connect */
440         reconnect_source_id = g_timeout_add(1, timer_reconnect, NULL);
442         auto_update_timer();
444 #ifndef NCMPC_MINI
445         check_key_bindings_source_id = g_timeout_add(10000, timer_check_key_bindings, NULL);
446 #endif
448         screen_paint(mpd);
450         g_main_loop_run(main_loop);
451         g_main_loop_unref(main_loop);
453         /* cleanup */
455         cancel_seek_timer();
457         disable_update_timer();
459         if (reconnect_source_id != 0)
460                 g_source_remove(reconnect_source_id);
462 #ifndef NCMPC_MINI
463         if (check_key_bindings_source_id != 0)
464                 g_source_remove(check_key_bindings_source_id);
465 #endif
467         signals_deinit();
468         ncmpc_lirc_deinit();
470         screen_exit();
471 #ifndef NCMPC_MINI
472         set_xterm_title("");
473 #endif
474         printf("\n");
476         mpdclient_free(mpd);
478 #ifdef ENABLE_LYRICS_SCREEN
479         lyrics_deinit();
480 #endif
482         ncu_deinit();
483         options_deinit();
485         return 0;