72a9362e53e2cee8f3766484e32a9739765b1dfc
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_status.h"
30 #include "xterm_title.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 *tmp = 0;
83 if (*tmp == 0)
84 g_strlcpy(tmp, PACKAGE " version " VERSION, BUFSIZE);
86 static char title[BUFSIZE];
87 if (strncmp(title, tmp, BUFSIZE)) {
88 g_strlcpy(title, tmp, BUFSIZE);
89 set_xterm_title(title);
90 }
91 }
92 #endif
94 static gboolean
95 timer_mpd_update(gpointer data);
97 static void
98 enable_update_timer(void)
99 {
100 if (update_source_id != 0)
101 return;
103 update_source_id = g_timeout_add(update_interval,
104 timer_mpd_update, NULL);
105 }
107 static void
108 disable_update_timer(void)
109 {
110 if (update_source_id == 0)
111 return;
113 g_source_remove(update_source_id);
114 update_source_id = 0;
115 }
117 static bool
118 should_enable_update_timer(void)
119 {
120 return mpd->playing;
121 }
123 static void
124 auto_update_timer(void)
125 {
126 if (should_enable_update_timer())
127 enable_update_timer();
128 else
129 disable_update_timer();
130 }
132 static void
133 do_mpd_update(void)
134 {
135 if (mpdclient_is_connected(mpd) &&
136 (mpd->events != 0 || mpd->playing))
137 mpdclient_update(mpd);
139 #ifndef NCMPC_MINI
140 if (options.enable_xterm_title)
141 update_xterm_title();
142 #endif
144 screen_update(mpd);
145 mpd->events = 0;
146 }
148 static char *
149 settings_name(const struct mpd_settings *settings)
150 {
151 const char *host = mpd_settings_get_host(settings);
152 if (host == NULL)
153 host = _("unknown");
155 if (host[0] == '/')
156 return g_strdup(host);
158 unsigned port = mpd_settings_get_port(settings);
159 if (port == 0 || port == 6600)
160 return g_strdup(host);
162 return g_strdup_printf("%s:%u", host, port);
163 }
165 static char *
166 default_settings_name(void)
167 {
168 struct mpd_settings *settings =
169 mpd_settings_new(options.host, options.port, 0,
170 NULL, options.password);
171 if (settings == NULL)
172 return g_strdup(_("unknown"));
174 char *name = settings_name(settings);
175 mpd_settings_free(settings);
177 return name;
178 }
180 /**
181 * This timer is installed when the connection to the MPD server is
182 * broken. It tries to recover by reconnecting periodically.
183 */
184 static gboolean
185 timer_reconnect(gcc_unused gpointer data)
186 {
187 assert(mpdclient_is_dead(mpd));
189 reconnect_source_id = 0;
191 char *name = default_settings_name();
192 screen_status_printf(_("Connecting to %s... [Press %s to abort]"),
193 name, get_key_names(CMD_QUIT, false));
194 g_free(name);
195 doupdate();
197 mpdclient_connect(mpd);
199 return FALSE;
200 }
202 void
203 mpdclient_connected_callback(void)
204 {
205 assert(reconnect_source_id == 0);
207 #ifndef NCMPC_MINI
208 /* quit if mpd is pre 0.14 - song id not supported by mpd */
209 struct mpd_connection *connection = mpdclient_get_connection(mpd);
210 if (mpd_connection_cmp_server_version(connection, 0, 16, 0) < 0) {
211 const unsigned *version =
212 mpd_connection_get_server_version(connection);
213 screen_status_printf(_("Error: MPD version %d.%d.%d is too old (%s needed)"),
214 version[0], version[1], version[2],
215 "0.16.0");
216 mpdclient_disconnect(mpd);
217 doupdate();
219 /* try again after 30 seconds */
220 reconnect_source_id = g_timeout_add(30000,
221 timer_reconnect, NULL);
222 return;
223 }
224 #endif
226 screen_status_clear_message();
227 doupdate();
229 /* update immediately */
230 mpd->events = MPD_IDLE_ALL;
232 do_mpd_update();
234 auto_update_timer();
235 }
237 void
238 mpdclient_failed_callback(void)
239 {
240 assert(reconnect_source_id == 0);
242 /* try again in 5 seconds */
243 reconnect_source_id = g_timeout_add(5000,
244 timer_reconnect, NULL);
245 }
247 void
248 mpdclient_lost_callback(void)
249 {
250 assert(reconnect_source_id == 0);
252 screen_update(mpd);
254 reconnect_source_id = g_timeout_add(1000, timer_reconnect, NULL);
255 }
257 /**
258 * This function is called by the gidle.c library when MPD sends us an
259 * idle event (or when the connection dies).
260 */
261 void
262 mpdclient_idle_callback(gcc_unused enum mpd_idle events)
263 {
264 #ifndef NCMPC_MINI
265 if (options.enable_xterm_title)
266 update_xterm_title();
267 #endif
269 screen_update(mpd);
270 auto_update_timer();
271 }
273 static gboolean
274 timer_mpd_update(gcc_unused gpointer data)
275 {
276 do_mpd_update();
278 if (should_enable_update_timer())
279 return true;
280 else {
281 update_source_id = 0;
282 return false;
283 }
284 }
286 void begin_input_event(void)
287 {
288 }
290 void end_input_event(void)
291 {
292 screen_update(mpd);
293 mpd->events = 0;
295 auto_update_timer();
296 }
298 bool
299 do_input_event(command_t cmd)
300 {
301 if (cmd == CMD_QUIT) {
302 g_main_loop_quit(main_loop);
303 return false;
304 }
306 screen_cmd(mpd, cmd);
308 if (cmd == CMD_VOLUME_UP || cmd == CMD_VOLUME_DOWN)
309 /* make sure we don't update the volume yet */
310 disable_update_timer();
312 return true;
313 }
315 #ifndef NCMPC_MINI
316 /**
317 * Check the configured key bindings for errors, and display a status
318 * message every 10 seconds.
319 */
320 static gboolean
321 timer_check_key_bindings(gcc_unused gpointer data)
322 {
323 char buf[256];
325 if (check_key_bindings(NULL, buf, sizeof(buf))) {
326 /* no error: disable this timer for the rest of this
327 process */
328 check_key_bindings_source_id = 0;
329 return FALSE;
330 }
332 #ifdef ENABLE_KEYDEF_SCREEN
333 g_strchomp(buf);
334 g_strlcat(buf, " (", sizeof(buf));
335 /* to translators: a key was bound twice in the key editor,
336 and this is a hint for the user what to press to correct
337 that */
338 char comment[64];
339 g_snprintf(comment, sizeof(comment), _("press %s for the key editor"),
340 get_key_names(CMD_SCREEN_KEYDEF, false));
341 g_strlcat(buf, comment, sizeof(buf));
342 g_strlcat(buf, ")", sizeof(buf));
343 #endif
345 screen_status_printf("%s", buf);
347 doupdate();
348 return TRUE;
349 }
350 #endif
352 int
353 main(int argc, const char *argv[])
354 {
355 #ifdef ENABLE_LOCALE
356 #ifndef ENABLE_NLS
357 gcc_unused
358 #endif
359 const char *charset = NULL;
360 /* time and date formatting */
361 setlocale(LC_TIME,"");
362 /* care about sorting order etc */
363 setlocale(LC_COLLATE,"");
364 /* charset */
365 setlocale(LC_CTYPE,"");
366 /* initialize charset conversions */
367 charset = charset_init();
369 /* initialize i18n support */
370 #endif
372 #ifdef ENABLE_NLS
373 setlocale(LC_MESSAGES, "");
374 bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
375 #ifdef ENABLE_LOCALE
376 bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
377 #endif
378 textdomain(GETTEXT_PACKAGE);
379 #endif
381 /* initialize options */
382 options_init();
384 /* parse command line options - 1 pass get configuration files */
385 options_parse(argc, argv);
387 #ifndef NCMPC_MINI
388 /* read configuration */
389 read_configuration();
391 /* check key bindings */
392 check_key_bindings(NULL, NULL, 0);
393 #endif
395 /* parse command line options - 2 pass */
396 options_parse(argc, argv);
398 ncu_init();
400 #ifdef ENABLE_LYRICS_SCREEN
401 lyrics_init();
402 #endif
404 /* create mpdclient instance */
405 mpd = mpdclient_new(options.host, options.port,
406 options.timeout_ms,
407 options.password);
409 /* initialize curses */
410 screen_init(mpd);
412 /* the main loop */
413 main_loop = g_main_loop_new(NULL, FALSE);
415 /* watch out for keyboard input */
416 keyboard_init();
418 /* watch out for lirc input */
419 ncmpc_lirc_init();
421 signals_init(main_loop, mpd);
423 /* attempt to connect */
424 reconnect_source_id = g_idle_add(timer_reconnect, NULL);
426 auto_update_timer();
428 #ifndef NCMPC_MINI
429 check_key_bindings_source_id = g_timeout_add(10000, timer_check_key_bindings, NULL);
430 #endif
432 screen_paint(mpd);
434 g_main_loop_run(main_loop);
435 g_main_loop_unref(main_loop);
437 /* cleanup */
439 cancel_seek_timer();
441 disable_update_timer();
443 if (reconnect_source_id != 0)
444 g_source_remove(reconnect_source_id);
446 #ifndef NCMPC_MINI
447 if (check_key_bindings_source_id != 0)
448 g_source_remove(check_key_bindings_source_id);
449 #endif
451 signals_deinit();
452 ncmpc_lirc_deinit();
454 screen_exit();
455 #ifndef NCMPC_MINI
456 set_xterm_title("");
457 #endif
458 printf("\n");
460 mpdclient_free(mpd);
462 #ifdef ENABLE_LYRICS_SCREEN
463 lyrics_deinit();
464 #endif
466 ncu_deinit();
467 options_deinit();
469 return 0;
470 }