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 const char *new_title = NULL;
79 if (options.xterm_title_format && mpd->playing && song)
80 new_title = strfsong(tmp, BUFSIZE, options.xterm_title_format, song) > 0
81 ? tmp
82 : NULL;
84 if (new_title == NULL)
85 new_title = PACKAGE " version " VERSION;
87 static char title[BUFSIZE];
88 if (strncmp(title, new_title, BUFSIZE)) {
89 g_strlcpy(title, new_title, BUFSIZE);
90 set_xterm_title(title);
91 }
92 }
93 #endif
95 static gboolean
96 timer_mpd_update(gpointer data);
98 static void
99 enable_update_timer(void)
100 {
101 if (update_source_id != 0)
102 return;
104 update_source_id = g_timeout_add(update_interval,
105 timer_mpd_update, NULL);
106 }
108 static void
109 disable_update_timer(void)
110 {
111 if (update_source_id == 0)
112 return;
114 g_source_remove(update_source_id);
115 update_source_id = 0;
116 }
118 static bool
119 should_enable_update_timer(void)
120 {
121 return mpd->playing;
122 }
124 static void
125 auto_update_timer(void)
126 {
127 if (should_enable_update_timer())
128 enable_update_timer();
129 else
130 disable_update_timer();
131 }
133 static void
134 do_mpd_update(void)
135 {
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;
147 }
149 static char *
150 settings_name(const struct mpd_settings *settings)
151 {
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);
164 }
166 static char *
167 default_settings_name(void)
168 {
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;
179 }
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)
187 {
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;
201 }
203 void
204 mpdclient_connected_callback(void)
205 {
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 =
222 g_timeout_add_seconds(30, 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();
236 }
238 void
239 mpdclient_failed_callback(void)
240 {
241 assert(reconnect_source_id == 0);
243 /* try again in 5 seconds */
244 reconnect_source_id = g_timeout_add_seconds(5, 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_seconds(1, 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 =
430 g_timeout_add_seconds(10, timer_check_key_bindings, NULL);
431 #endif
433 screen_paint(mpd, true);
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;
471 }