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 /**
150 * This timer is installed when the connection to the MPD server is
151 * broken. It tries to recover by reconnecting periodically.
152 */
153 static gboolean
154 timer_reconnect(gcc_unused gpointer data)
155 {
156 assert(mpdclient_is_dead(mpd));
158 reconnect_source_id = 0;
160 char *name = mpdclient_settings_name(mpd);
161 screen_status_printf(_("Connecting to %s... [Press %s to abort]"),
162 name, get_key_names(CMD_QUIT, false));
163 g_free(name);
164 doupdate();
166 mpdclient_connect(mpd);
168 return FALSE;
169 }
171 void
172 mpdclient_connected_callback(void)
173 {
174 assert(reconnect_source_id == 0);
176 #ifndef NCMPC_MINI
177 /* quit if mpd is pre 0.14 - song id not supported by mpd */
178 struct mpd_connection *connection = mpdclient_get_connection(mpd);
179 if (mpd_connection_cmp_server_version(connection, 0, 16, 0) < 0) {
180 const unsigned *version =
181 mpd_connection_get_server_version(connection);
182 screen_status_printf(_("Error: MPD version %d.%d.%d is too old (%s needed)"),
183 version[0], version[1], version[2],
184 "0.16.0");
185 mpdclient_disconnect(mpd);
186 doupdate();
188 /* try again after 30 seconds */
189 reconnect_source_id =
190 g_timeout_add_seconds(30, timer_reconnect, NULL);
191 return;
192 }
193 #endif
195 screen_status_clear_message();
196 doupdate();
198 /* update immediately */
199 mpd->events = MPD_IDLE_ALL;
201 do_mpd_update();
203 auto_update_timer();
204 }
206 void
207 mpdclient_failed_callback(void)
208 {
209 assert(reconnect_source_id == 0);
211 /* try again in 5 seconds */
212 reconnect_source_id = g_timeout_add_seconds(5, timer_reconnect, NULL);
213 }
215 void
216 mpdclient_lost_callback(void)
217 {
218 assert(reconnect_source_id == 0);
220 screen_update(mpd);
222 reconnect_source_id = g_timeout_add_seconds(1, timer_reconnect, NULL);
223 }
225 /**
226 * This function is called by the gidle.c library when MPD sends us an
227 * idle event (or when the connection dies).
228 */
229 void
230 mpdclient_idle_callback(gcc_unused enum mpd_idle events)
231 {
232 #ifndef NCMPC_MINI
233 if (options.enable_xterm_title)
234 update_xterm_title();
235 #endif
237 screen_update(mpd);
238 auto_update_timer();
239 }
241 static gboolean
242 timer_mpd_update(gcc_unused gpointer data)
243 {
244 do_mpd_update();
246 if (should_enable_update_timer())
247 return true;
248 else {
249 update_source_id = 0;
250 return false;
251 }
252 }
254 void begin_input_event(void)
255 {
256 }
258 void end_input_event(void)
259 {
260 screen_update(mpd);
261 mpd->events = 0;
263 auto_update_timer();
264 }
266 bool
267 do_input_event(command_t cmd)
268 {
269 if (cmd == CMD_QUIT) {
270 g_main_loop_quit(main_loop);
271 return false;
272 }
274 screen_cmd(mpd, cmd);
276 if (cmd == CMD_VOLUME_UP || cmd == CMD_VOLUME_DOWN)
277 /* make sure we don't update the volume yet */
278 disable_update_timer();
280 return true;
281 }
283 #ifdef HAVE_GETMOUSE
285 void
286 do_mouse_event(int x, int y, mmask_t bstate)
287 {
288 screen_mouse(mpd, x, y, bstate);
289 }
291 #endif
293 #ifndef NCMPC_MINI
294 /**
295 * Check the configured key bindings for errors, and display a status
296 * message every 10 seconds.
297 */
298 static gboolean
299 timer_check_key_bindings(gcc_unused gpointer data)
300 {
301 char buf[256];
303 if (check_key_bindings(NULL, buf, sizeof(buf))) {
304 /* no error: disable this timer for the rest of this
305 process */
306 check_key_bindings_source_id = 0;
307 return FALSE;
308 }
310 #ifdef ENABLE_KEYDEF_SCREEN
311 g_strchomp(buf);
312 g_strlcat(buf, " (", sizeof(buf));
313 /* to translators: a key was bound twice in the key editor,
314 and this is a hint for the user what to press to correct
315 that */
316 char comment[64];
317 g_snprintf(comment, sizeof(comment), _("press %s for the key editor"),
318 get_key_names(CMD_SCREEN_KEYDEF, false));
319 g_strlcat(buf, comment, sizeof(buf));
320 g_strlcat(buf, ")", sizeof(buf));
321 #endif
323 screen_status_printf("%s", buf);
325 doupdate();
326 return TRUE;
327 }
328 #endif
330 int
331 main(int argc, const char *argv[])
332 {
333 #ifdef ENABLE_LOCALE
334 #ifndef ENABLE_NLS
335 gcc_unused
336 #endif
337 const char *charset = NULL;
338 /* time and date formatting */
339 setlocale(LC_TIME,"");
340 /* care about sorting order etc */
341 setlocale(LC_COLLATE,"");
342 /* charset */
343 setlocale(LC_CTYPE,"");
344 /* initialize charset conversions */
345 charset = charset_init();
347 /* initialize i18n support */
348 #endif
350 #ifdef ENABLE_NLS
351 setlocale(LC_MESSAGES, "");
352 bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
353 #ifdef ENABLE_LOCALE
354 bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
355 #endif
356 textdomain(GETTEXT_PACKAGE);
357 #endif
359 /* initialize options */
360 options_init();
362 /* parse command line options - 1 pass get configuration files */
363 options_parse(argc, argv);
365 #ifndef NCMPC_MINI
366 /* read configuration */
367 read_configuration();
369 /* check key bindings */
370 check_key_bindings(NULL, NULL, 0);
371 #endif
373 /* parse command line options - 2 pass */
374 options_parse(argc, argv);
376 ncu_init();
378 #ifdef ENABLE_LYRICS_SCREEN
379 lyrics_init();
380 #endif
382 /* create mpdclient instance */
383 mpd = mpdclient_new(options.host, options.port,
384 options.timeout_ms,
385 options.password);
387 /* initialize curses */
388 screen_init(mpd);
390 /* the main loop */
391 main_loop = g_main_loop_new(NULL, FALSE);
393 /* watch out for keyboard input */
394 keyboard_init();
396 /* watch out for lirc input */
397 ncmpc_lirc_init();
399 signals_init(main_loop, mpd);
401 /* attempt to connect */
402 reconnect_source_id = g_idle_add(timer_reconnect, NULL);
404 auto_update_timer();
406 #ifndef NCMPC_MINI
407 check_key_bindings_source_id =
408 g_timeout_add_seconds(10, timer_check_key_bindings, NULL);
409 #endif
411 screen_paint(mpd, true);
413 g_main_loop_run(main_loop);
414 g_main_loop_unref(main_loop);
416 /* cleanup */
418 cancel_seek_timer();
420 disable_update_timer();
422 if (reconnect_source_id != 0)
423 g_source_remove(reconnect_source_id);
425 #ifndef NCMPC_MINI
426 if (check_key_bindings_source_id != 0)
427 g_source_remove(check_key_bindings_source_id);
428 #endif
430 signals_deinit();
431 ncmpc_lirc_deinit();
433 screen_exit();
434 #ifndef NCMPC_MINI
435 set_xterm_title("");
436 #endif
437 printf("\n");
439 mpdclient_free(mpd);
441 #ifdef ENABLE_LYRICS_SCREEN
442 lyrics_deinit();
443 #endif
445 ncu_deinit();
446 options_deinit();
448 return 0;
449 }