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"
35 #ifndef NCMPC_MINI
36 #include "conf.h"
37 #endif
39 #ifdef ENABLE_LYRICS_SCREEN
40 #include "lyrics.h"
41 #endif
43 #ifdef ENABLE_LIRC
44 #include "lirc.h"
45 #endif
47 #include <mpd/client.h>
49 #include <stdlib.h>
50 #include <unistd.h>
51 #include <fcntl.h>
52 #include <signal.h>
53 #include <string.h>
55 #ifdef ENABLE_LOCALE
56 #include <locale.h>
57 #endif
59 /* time between mpd updates [ms] */
60 static const guint update_interval = 500;
62 #define BUFSIZE 1024
64 static struct mpdclient *mpd = NULL;
65 static GMainLoop *main_loop;
66 static guint reconnect_source_id, update_source_id;
67 static int sigwinch_pipes[2];
69 #ifndef NCMPC_MINI
70 static guint check_key_bindings_source_id;
71 #endif
73 #ifndef NCMPC_MINI
74 static void
75 update_xterm_title(void)
76 {
77 struct mpd_status *status = NULL;
78 const struct mpd_song *song = NULL;
79 if (mpd) {
80 status = mpd->status;
81 song = mpd->song;
82 }
84 char tmp[BUFSIZE];
85 if (options.xterm_title_format && status && song &&
86 mpd_status_get_state(status) == MPD_STATE_PLAY)
87 strfsong(tmp, BUFSIZE, options.xterm_title_format, song);
88 else
89 g_strlcpy(tmp, PACKAGE " version " VERSION, BUFSIZE);
91 static char title[BUFSIZE];
92 if (strncmp(title, tmp, BUFSIZE)) {
93 g_strlcpy(title, tmp, BUFSIZE);
94 set_xterm_title("%s", title);
95 }
96 }
97 #endif
99 static void
100 exit_and_cleanup(void)
101 {
102 screen_exit();
103 #ifndef NCMPC_MINI
104 set_xterm_title("");
105 #endif
106 printf("\n");
108 if (mpd) {
109 mpdclient_free(mpd);
110 }
111 }
113 #ifndef WIN32
114 static void
115 catch_sigint(gcc_unused int sig)
116 {
117 g_main_loop_quit(main_loop);
118 }
121 static void
122 catch_sigcont(gcc_unused int sig)
123 {
124 char irrelevant = 'a';
125 if (1 != write(sigwinch_pipes[1], &irrelevant, 1))
126 exit(EXIT_FAILURE);
127 }
129 void
130 sigstop(void)
131 {
132 def_prog_mode(); /* save the tty modes */
133 endwin(); /* end curses mode temporarily */
134 kill(0, SIGSTOP); /* issue SIGSTOP */
135 }
137 static gboolean
138 sigwinch_event(gcc_unused GIOChannel *source,
139 gcc_unused GIOCondition condition, gcc_unused gpointer data)
140 {
141 char ignoreme[64];
142 if (1 > read(sigwinch_pipes[0], ignoreme, 64))
143 exit(EXIT_FAILURE);
145 endwin();
146 refresh();
147 screen_resize(mpd);
149 return TRUE;
150 }
152 static void
153 catch_sigwinch(gcc_unused int sig)
154 {
155 if (1 != write(sigwinch_pipes[1], "", 1))
156 exit(EXIT_FAILURE);
157 }
158 #endif /* WIN32 */
160 static gboolean
161 timer_mpd_update(gpointer data);
163 static void
164 enable_update_timer(void)
165 {
166 if (update_source_id != 0)
167 return;
169 update_source_id = g_timeout_add(update_interval,
170 timer_mpd_update, NULL);
171 }
173 static void
174 disable_update_timer(void)
175 {
176 if (update_source_id == 0)
177 return;
179 g_source_remove(update_source_id);
180 update_source_id = 0;
181 }
183 static bool
184 should_enable_update_timer(void)
185 {
186 return (mpdclient_is_connected(mpd) &&
187 mpd->status != NULL &&
188 mpd_status_get_state(mpd->status) == MPD_STATE_PLAY)
189 #ifndef NCMPC_MINI
190 || options.display_time
191 #endif
192 ;
193 }
195 static void
196 auto_update_timer(void)
197 {
198 if (should_enable_update_timer())
199 enable_update_timer();
200 else
201 disable_update_timer();
202 }
204 static void
205 check_reconnect(void);
207 static void
208 do_mpd_update(void)
209 {
210 if (mpdclient_is_connected(mpd) &&
211 (mpd->events != 0 ||
212 (mpd->status != NULL &&
213 mpd_status_get_state(mpd->status) == MPD_STATE_PLAY)))
214 mpdclient_update(mpd);
216 #ifndef NCMPC_MINI
217 if (options.enable_xterm_title)
218 update_xterm_title();
219 #endif
221 screen_update(mpd);
222 mpd->events = 0;
224 mpdclient_put_connection(mpd);
225 check_reconnect();
226 }
228 static char *
229 settings_name(const struct mpd_settings *settings)
230 {
231 const char *host = mpd_settings_get_host(settings);
232 if (host == NULL)
233 host = _("unknown");
235 if (host[0] == '/')
236 return g_strdup(host);
238 unsigned port = mpd_settings_get_port(settings);
239 if (port == 0 || port == 6600)
240 return g_strdup(host);
242 return g_strdup_printf("%s:%u", host, port);
243 }
245 static char *
246 default_settings_name(void)
247 {
248 struct mpd_settings *settings =
249 mpd_settings_new(options.host, options.port, 0,
250 NULL, options.password);
251 if (settings == NULL)
252 return g_strdup(_("unknown"));
254 char *name = settings_name(settings);
255 mpd_settings_free(settings);
257 return name;
258 }
260 /**
261 * This timer is installed when the connection to the MPD server is
262 * broken. It tries to recover by reconnecting periodically.
263 */
264 static gboolean
265 timer_reconnect(gcc_unused gpointer data)
266 {
267 assert(!mpdclient_is_connected(mpd));
269 reconnect_source_id = 0;
271 char *name = default_settings_name();
272 screen_status_printf(_("Connecting to %s... [Press %s to abort]"),
273 name, get_key_names(CMD_QUIT, false));
274 g_free(name);
275 doupdate();
277 mpdclient_disconnect(mpd);
278 if (!mpdclient_connect(mpd, options.host, options.port,
279 options.timeout_ms,
280 options.password)) {
281 /* try again in 5 seconds */
282 reconnect_source_id = g_timeout_add(5000,
283 timer_reconnect, NULL);
284 return FALSE;
285 }
287 #ifndef NCMPC_MINI
288 /* quit if mpd is pre 0.14 - song id not supported by mpd */
289 struct mpd_connection *connection = mpdclient_get_connection(mpd);
290 if (mpd_connection_cmp_server_version(connection, 0, 16, 0) < 0) {
291 const unsigned *version =
292 mpd_connection_get_server_version(connection);
293 screen_status_printf(_("Error: MPD version %d.%d.%d is too old (%s needed)"),
294 version[0], version[1], version[2],
295 "0.16.0");
296 mpdclient_disconnect(mpd);
297 doupdate();
299 /* try again after 30 seconds */
300 reconnect_source_id = g_timeout_add(30000,
301 timer_reconnect, NULL);
302 return FALSE;
303 }
304 #endif
306 screen_status_clear_message();
307 doupdate();
309 /* update immediately */
310 mpd->events = MPD_IDLE_ALL;
312 do_mpd_update();
314 auto_update_timer();
316 return FALSE;
317 }
319 static void
320 check_reconnect(void)
321 {
322 if (!mpdclient_is_connected(mpd) && reconnect_source_id == 0)
323 /* reconnect when the connection is lost */
324 reconnect_source_id = g_timeout_add(1000, timer_reconnect,
325 NULL);
326 }
328 void
329 mpdclient_lost_callback(void)
330 {
331 assert(reconnect_source_id == 0);
333 screen_update(mpd);
335 reconnect_source_id = g_timeout_add(1000, timer_reconnect, NULL);
336 }
338 /**
339 * This function is called by the gidle.c library when MPD sends us an
340 * idle event (or when the connection dies).
341 */
342 void
343 mpdclient_idle_callback(gcc_unused enum mpd_idle events)
344 {
345 #ifndef NCMPC_MINI
346 if (options.enable_xterm_title)
347 update_xterm_title();
348 #endif
350 screen_update(mpd);
351 auto_update_timer();
352 }
354 static gboolean
355 timer_mpd_update(gcc_unused gpointer data)
356 {
357 do_mpd_update();
359 if (should_enable_update_timer())
360 return true;
361 else {
362 update_source_id = 0;
363 return false;
364 }
365 }
367 void begin_input_event(void)
368 {
369 }
371 void end_input_event(void)
372 {
373 screen_update(mpd);
374 mpd->events = 0;
376 mpdclient_put_connection(mpd);
377 check_reconnect();
378 auto_update_timer();
379 }
381 int do_input_event(command_t cmd)
382 {
383 if (cmd == CMD_QUIT) {
384 g_main_loop_quit(main_loop);
385 return -1;
386 }
388 screen_cmd(mpd, cmd);
390 if (cmd == CMD_VOLUME_UP || cmd == CMD_VOLUME_DOWN)
391 /* make sure we don't update the volume yet */
392 disable_update_timer();
394 return 0;
395 }
397 static gboolean
398 keyboard_event(gcc_unused GIOChannel *source,
399 gcc_unused GIOCondition condition,
400 gcc_unused gpointer data)
401 {
402 begin_input_event();
404 command_t cmd = get_keyboard_command();
405 if (cmd != CMD_NONE)
406 if (do_input_event(cmd) != 0)
407 return FALSE;
409 end_input_event();
410 return TRUE;
411 }
413 #ifndef NCMPC_MINI
414 /**
415 * Check the configured key bindings for errors, and display a status
416 * message every 10 seconds.
417 */
418 static gboolean
419 timer_check_key_bindings(gcc_unused gpointer data)
420 {
421 char buf[256];
423 if (check_key_bindings(NULL, buf, sizeof(buf))) {
424 /* no error: disable this timer for the rest of this
425 process */
426 check_key_bindings_source_id = 0;
427 return FALSE;
428 }
430 #ifdef ENABLE_KEYDEF_SCREEN
431 g_strchomp(buf);
432 g_strlcat(buf, " (", sizeof(buf));
433 /* to translators: a key was bound twice in the key editor,
434 and this is a hint for the user what to press to correct
435 that */
436 char comment[64];
437 g_snprintf(comment, sizeof(comment), _("press %s for the key editor"),
438 get_key_names(CMD_SCREEN_KEYDEF, false));
439 g_strlcat(buf, comment, sizeof(buf));
440 g_strlcat(buf, ")", sizeof(buf));
441 #endif
443 screen_status_printf("%s", buf);
445 doupdate();
446 return TRUE;
447 }
448 #endif
450 int
451 main(int argc, const char *argv[])
452 {
453 #ifdef ENABLE_LOCALE
454 #ifndef ENABLE_NLS
455 gcc_unused
456 #endif
457 const char *charset = NULL;
458 /* time and date formatting */
459 setlocale(LC_TIME,"");
460 /* care about sorting order etc */
461 setlocale(LC_COLLATE,"");
462 /* charset */
463 setlocale(LC_CTYPE,"");
464 /* initialize charset conversions */
465 charset = charset_init();
467 /* initialize i18n support */
468 #endif
470 #ifdef ENABLE_NLS
471 setlocale(LC_MESSAGES, "");
472 bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
473 #ifdef ENABLE_LOCALE
474 bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
475 #endif
476 textdomain(GETTEXT_PACKAGE);
477 #endif
479 /* initialize options */
480 options_init();
482 /* parse command line options - 1 pass get configuration files */
483 options_parse(argc, argv);
485 #ifndef NCMPC_MINI
486 /* read configuration */
487 read_configuration();
489 /* check key bindings */
490 check_key_bindings(NULL, NULL, 0);
491 #endif
493 /* parse command line options - 2 pass */
494 options_parse(argc, argv);
496 #ifndef WIN32
497 /* setup signal behavior - SIGINT */
498 struct sigaction act;
499 sigemptyset(&act.sa_mask);
500 act.sa_flags = 0;
501 act.sa_handler = catch_sigint;
502 if (sigaction(SIGINT, &act, NULL) < 0) {
503 perror("signal");
504 exit(EXIT_FAILURE);
505 }
507 /* setup signal behavior - SIGTERM */
509 act.sa_handler = catch_sigint;
510 if (sigaction(SIGTERM, &act, NULL) < 0) {
511 perror("sigaction()");
512 exit(EXIT_FAILURE);
513 }
515 /* setup signal behavior - SIGCONT */
517 act.sa_handler = catch_sigcont;
518 if (sigaction(SIGCONT, &act, NULL) < 0) {
519 perror("sigaction(SIGCONT)");
520 exit(EXIT_FAILURE);
521 }
523 /* setup signal behaviour - SIGHUP*/
525 act.sa_handler = catch_sigint;
526 if (sigaction(SIGHUP, &act, NULL) < 0) {
527 perror("sigaction(SIGHUP)");
528 exit(EXIT_FAILURE);
529 }
531 /* setup SIGWINCH */
533 act.sa_flags = SA_RESTART;
534 act.sa_handler = catch_sigwinch;
535 if (sigaction(SIGWINCH, &act, NULL) < 0) {
536 perror("sigaction(SIGWINCH)");
537 exit(EXIT_FAILURE);
538 }
540 /* ignore SIGPIPE */
542 act.sa_handler = SIG_IGN;
543 if (sigaction(SIGPIPE, &act, NULL) < 0) {
544 perror("sigaction(SIGPIPE)");
545 exit(EXIT_FAILURE);
546 }
547 #endif
549 ncu_init();
551 #ifdef ENABLE_LYRICS_SCREEN
552 lyrics_init();
553 #endif
555 /* create mpdclient instance */
556 mpd = mpdclient_new();
558 /* initialize curses */
559 screen_init(mpd);
561 /* the main loop */
562 main_loop = g_main_loop_new(NULL, FALSE);
564 /* watch out for keyboard input */
565 GIOChannel *keyboard_channel = g_io_channel_unix_new(STDIN_FILENO);
566 g_io_add_watch(keyboard_channel, G_IO_IN, keyboard_event, NULL);
568 #ifdef ENABLE_LIRC
569 /* watch out for lirc input */
570 int lirc_socket = ncmpc_lirc_open();
571 GIOChannel *lirc_channel = NULL;
572 if (lirc_socket >= 0) {
573 lirc_channel = g_io_channel_unix_new(lirc_socket);
574 g_io_add_watch(lirc_channel, G_IO_IN, lirc_event, NULL);
575 }
576 #endif
578 #ifndef WIN32
579 GIOChannel *sigwinch_channel = NULL;
580 if (!pipe(sigwinch_pipes) &&
581 !fcntl(sigwinch_pipes[1], F_SETFL, O_NONBLOCK)) {
582 sigwinch_channel = g_io_channel_unix_new(sigwinch_pipes[0]);
583 g_io_add_watch(sigwinch_channel, G_IO_IN, sigwinch_event, NULL);
584 }
585 else {
586 perror("sigwinch pipe creation failed");
587 exit(EXIT_FAILURE);
588 }
589 #endif
591 /* attempt to connect */
592 reconnect_source_id = g_timeout_add(1, timer_reconnect, NULL);
594 auto_update_timer();
596 #ifndef NCMPC_MINI
597 check_key_bindings_source_id = g_timeout_add(10000, timer_check_key_bindings, NULL);
598 #endif
600 screen_paint(mpd);
602 g_main_loop_run(main_loop);
604 /* cleanup */
606 cancel_seek_timer();
608 disable_update_timer();
610 if (reconnect_source_id != 0)
611 g_source_remove(reconnect_source_id);
613 #ifndef NCMPC_MINI
614 if (check_key_bindings_source_id != 0)
615 g_source_remove(check_key_bindings_source_id);
616 #endif
618 g_main_loop_unref(main_loop);
619 g_io_channel_unref(keyboard_channel);
620 g_io_channel_unref(sigwinch_channel);
621 close(sigwinch_pipes[0]);
622 close(sigwinch_pipes[1]);
624 #ifdef ENABLE_LIRC
625 if (lirc_socket >= 0)
626 g_io_channel_unref(lirc_channel);
627 ncmpc_lirc_close();
628 #endif
630 exit_and_cleanup();
632 #ifdef ENABLE_LYRICS_SCREEN
633 lyrics_deinit();
634 #endif
636 ncu_deinit();
637 options_deinit();
639 return 0;
640 }