Code

mpdclient: pass host, port etc. to constructor
[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_disconnect(mpd);
205         mpdclient_connect(mpd);
207         return FALSE;
210 static void
211 check_reconnect(void)
213         if (mpdclient_is_dead(mpd) && reconnect_source_id == 0)
214                 /* reconnect when the connection is lost */
215                 reconnect_source_id = g_timeout_add(1000, timer_reconnect,
216                                                     NULL);
219 void
220 mpdclient_connected_callback(void)
222         assert(reconnect_source_id == 0);
224 #ifndef NCMPC_MINI
225         /* quit if mpd is pre 0.14 - song id not supported by mpd */
226         struct mpd_connection *connection = mpdclient_get_connection(mpd);
227         if (mpd_connection_cmp_server_version(connection, 0, 16, 0) < 0) {
228                 const unsigned *version =
229                         mpd_connection_get_server_version(connection);
230                 screen_status_printf(_("Error: MPD version %d.%d.%d is too old (%s needed)"),
231                                      version[0], version[1], version[2],
232                                      "0.16.0");
233                 mpdclient_disconnect(mpd);
234                 doupdate();
236                 /* try again after 30 seconds */
237                 reconnect_source_id = g_timeout_add(30000,
238                                                     timer_reconnect, NULL);
239                 return;
240         }
241 #endif
243         screen_status_clear_message();
244         doupdate();
246         /* update immediately */
247         mpd->events = MPD_IDLE_ALL;
249         do_mpd_update();
251         auto_update_timer();
254 void
255 mpdclient_failed_callback(void)
257         assert(reconnect_source_id == 0);
259         /* try again in 5 seconds */
260         reconnect_source_id = g_timeout_add(5000,
261                                             timer_reconnect, NULL);
264 void
265 mpdclient_lost_callback(void)
267         assert(reconnect_source_id == 0);
269         screen_update(mpd);
271         reconnect_source_id = g_timeout_add(1000, timer_reconnect, NULL);
274 /**
275  * This function is called by the gidle.c library when MPD sends us an
276  * idle event (or when the connection dies).
277  */
278 void
279 mpdclient_idle_callback(gcc_unused enum mpd_idle events)
281 #ifndef NCMPC_MINI
282         if (options.enable_xterm_title)
283                 update_xterm_title();
284 #endif
286         screen_update(mpd);
287         auto_update_timer();
290 static gboolean
291 timer_mpd_update(gcc_unused gpointer data)
293         do_mpd_update();
295         if (should_enable_update_timer())
296                 return true;
297         else {
298                 update_source_id = 0;
299                 return false;
300         }
303 void begin_input_event(void)
307 void end_input_event(void)
309         screen_update(mpd);
310         mpd->events = 0;
312         mpdclient_put_connection(mpd);
313         check_reconnect();
314         auto_update_timer();
317 bool
318 do_input_event(command_t cmd)
320         if (cmd == CMD_QUIT) {
321                 g_main_loop_quit(main_loop);
322                 return false;
323         }
325         screen_cmd(mpd, cmd);
327         if (cmd == CMD_VOLUME_UP || cmd == CMD_VOLUME_DOWN)
328                 /* make sure we don't update the volume yet */
329                 disable_update_timer();
331         return true;
334 #ifndef NCMPC_MINI
335 /**
336  * Check the configured key bindings for errors, and display a status
337  * message every 10 seconds.
338  */
339 static gboolean
340 timer_check_key_bindings(gcc_unused gpointer data)
342         char buf[256];
344         if (check_key_bindings(NULL, buf, sizeof(buf))) {
345                 /* no error: disable this timer for the rest of this
346                    process */
347                 check_key_bindings_source_id = 0;
348                 return FALSE;
349         }
351 #ifdef ENABLE_KEYDEF_SCREEN
352         g_strchomp(buf);
353         g_strlcat(buf, " (", sizeof(buf));
354         /* to translators: a key was bound twice in the key editor,
355            and this is a hint for the user what to press to correct
356            that */
357         char comment[64];
358         g_snprintf(comment, sizeof(comment), _("press %s for the key editor"),
359                    get_key_names(CMD_SCREEN_KEYDEF, false));
360         g_strlcat(buf, comment, sizeof(buf));
361         g_strlcat(buf, ")", sizeof(buf));
362 #endif
364         screen_status_printf("%s", buf);
366         doupdate();
367         return TRUE;
369 #endif
371 int
372 main(int argc, const char *argv[])
374 #ifdef ENABLE_LOCALE
375 #ifndef ENABLE_NLS
376         gcc_unused
377 #endif
378         const char *charset = NULL;
379         /* time and date formatting */
380         setlocale(LC_TIME,"");
381         /* care about sorting order etc */
382         setlocale(LC_COLLATE,"");
383         /* charset */
384         setlocale(LC_CTYPE,"");
385         /* initialize charset conversions */
386         charset = charset_init();
388         /* initialize i18n support */
389 #endif
391 #ifdef ENABLE_NLS
392         setlocale(LC_MESSAGES, "");
393         bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
394 #ifdef ENABLE_LOCALE
395         bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
396 #endif
397         textdomain(GETTEXT_PACKAGE);
398 #endif
400         /* initialize options */
401         options_init();
403         /* parse command line options - 1 pass get configuration files */
404         options_parse(argc, argv);
406 #ifndef NCMPC_MINI
407         /* read configuration */
408         read_configuration();
410         /* check key bindings */
411         check_key_bindings(NULL, NULL, 0);
412 #endif
414         /* parse command line options - 2 pass */
415         options_parse(argc, argv);
417         ncu_init();
419 #ifdef ENABLE_LYRICS_SCREEN
420         lyrics_init();
421 #endif
423         /* create mpdclient instance */
424         mpd = mpdclient_new(options.host, options.port,
425                             options.timeout_ms,
426                             options.password);
428         /* initialize curses */
429         screen_init(mpd);
431         /* the main loop */
432         main_loop = g_main_loop_new(NULL, FALSE);
434         /* watch out for keyboard input */
435         keyboard_init();
437         /* watch out for lirc input */
438         ncmpc_lirc_init();
440         signals_init(main_loop, mpd);
442         /* attempt to connect */
443         reconnect_source_id = g_timeout_add(1, timer_reconnect, NULL);
445         auto_update_timer();
447 #ifndef NCMPC_MINI
448         check_key_bindings_source_id = g_timeout_add(10000, timer_check_key_bindings, NULL);
449 #endif
451         screen_paint(mpd);
453         g_main_loop_run(main_loop);
454         g_main_loop_unref(main_loop);
456         /* cleanup */
458         cancel_seek_timer();
460         disable_update_timer();
462         if (reconnect_source_id != 0)
463                 g_source_remove(reconnect_source_id);
465 #ifndef NCMPC_MINI
466         if (check_key_bindings_source_id != 0)
467                 g_source_remove(check_key_bindings_source_id);
468 #endif
470         signals_deinit();
471         ncmpc_lirc_deinit();
473         screen_exit();
474 #ifndef NCMPC_MINI
475         set_xterm_title("");
476 #endif
477         printf("\n");
479         mpdclient_free(mpd);
481 #ifdef ENABLE_LYRICS_SCREEN
482         lyrics_deinit();
483 #endif
485         ncu_deinit();
486         options_deinit();
488         return 0;