1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2009 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
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.
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.
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 "charset.h"
24 #include "options.h"
25 #include "command.h"
26 #include "ncu.h"
27 #include "screen.h"
28 #include "screen_utils.h"
29 #include "screen_message.h"
30 #include "strfsong.h"
31 #include "i18n.h"
32 #include "player_command.h"
34 #ifndef NCMPC_MINI
35 #include "conf.h"
36 #endif
38 #ifdef ENABLE_LYRICS_SCREEN
39 #include "lyrics.h"
40 #endif
42 #ifdef ENABLE_LIRC
43 #include "lirc.h"
44 #endif
46 #include <mpd/client.h>
48 #include <stdlib.h>
49 #include <unistd.h>
50 #include <signal.h>
51 #include <string.h>
53 #ifdef ENABLE_LOCALE
54 #include <locale.h>
55 #endif
57 /* time between mpd updates [s] */
58 static const guint update_interval = 500;
60 #define BUFSIZE 1024
62 static struct mpdclient *mpd = NULL;
63 static GMainLoop *main_loop;
64 static guint reconnect_source_id, update_source_id;
66 #ifndef NCMPC_MINI
67 static guint check_key_bindings_source_id;
68 #endif
70 #ifndef NCMPC_MINI
71 static void
72 update_xterm_title(void)
73 {
74 static char title[BUFSIZE];
75 char tmp[BUFSIZE];
76 struct mpd_status *status = NULL;
77 const struct mpd_song *song = NULL;
79 if (mpd) {
80 status = mpd->status;
81 song = mpd->song;
82 }
84 if (options.xterm_title_format && status && song &&
85 mpd_status_get_state(status) == MPD_STATE_PLAY)
86 strfsong(tmp, BUFSIZE, options.xterm_title_format, song);
87 else
88 g_strlcpy(tmp, PACKAGE " version " VERSION, BUFSIZE);
90 if (strncmp(title, tmp, BUFSIZE)) {
91 g_strlcpy(title, tmp, BUFSIZE);
92 set_xterm_title("%s", title);
93 }
94 }
95 #endif
97 static void
98 exit_and_cleanup(void)
99 {
100 screen_exit();
101 #ifndef NCMPC_MINI
102 set_xterm_title("");
103 #endif
104 printf("\n");
106 if (mpd) {
107 mpdclient_disconnect(mpd);
108 mpdclient_free(mpd);
109 }
110 }
112 static void
113 catch_sigint(G_GNUC_UNUSED int sig)
114 {
115 g_main_loop_quit(main_loop);
116 }
119 static void
120 catch_sigcont(G_GNUC_UNUSED int sig)
121 {
122 screen_resize(mpd);
123 }
125 void
126 sigstop(void)
127 {
128 def_prog_mode(); /* save the tty modes */
129 endwin(); /* end curses mode temporarily */
130 kill(0, SIGSTOP); /* issue SIGSTOP */
131 }
133 static guint timer_sigwinch_id;
135 static gboolean
136 timer_sigwinch(G_GNUC_UNUSED gpointer data)
137 {
138 /* the following causes the screen to flicker. There might be
139 better solutions, but I believe it isn't all that
140 important. */
142 endwin();
143 refresh();
144 screen_resize(mpd);
146 return FALSE;
147 }
149 static void
150 catch_sigwinch(G_GNUC_UNUSED int sig)
151 {
152 if (timer_sigwinch_id != 0)
153 g_source_remove(timer_sigwinch_id);
155 timer_sigwinch_id = g_timeout_add(100, timer_sigwinch, NULL);
156 }
158 static gboolean
159 timer_mpd_update(gpointer data);
161 static void
162 enable_update_timer(void)
163 {
164 if (update_source_id != 0)
165 return;
167 update_source_id = g_timeout_add(update_interval,
168 timer_mpd_update, NULL);
169 }
171 static void
172 disable_update_timer(void)
173 {
174 if (update_source_id == 0)
175 return;
177 g_source_remove(update_source_id);
178 update_source_id = 0;
179 }
181 static void
182 check_reconnect(void);
184 static void
185 do_mpd_update(void)
186 {
187 if (mpdclient_is_connected(mpd))
188 mpdclient_update(mpd);
190 #ifndef NCMPC_MINI
191 if (options.enable_xterm_title)
192 update_xterm_title();
193 #endif
195 screen_update(mpd);
196 mpd->events = 0;
198 check_reconnect();
199 }
201 /**
202 * This timer is installed when the connection to the MPD server is
203 * broken. It tries to recover by reconnecting periodically.
204 */
205 static gboolean
206 timer_reconnect(G_GNUC_UNUSED gpointer data)
207 {
208 bool success;
210 assert(!mpdclient_is_connected(mpd));
212 reconnect_source_id = 0;
214 screen_status_printf(_("Connecting to %s... [Press %s to abort]"),
215 options.host, get_key_names(CMD_QUIT,0) );
216 doupdate();
218 mpdclient_disconnect(mpd);
219 success = mpdclient_connect(mpd,
220 options.host, options.port,
221 1.5,
222 options.password);
223 if (!success) {
224 /* try again in 5 seconds */
225 reconnect_source_id = g_timeout_add(5000,
226 timer_reconnect, NULL);
227 return FALSE;
228 }
230 #ifndef NCMPC_MINI
231 /* quit if mpd is pre 0.11.0 - song id not supported by mpd */
232 if (mpd_connection_cmp_server_version(mpd->connection, 0, 12, 0) < 0) {
233 const unsigned *version =
234 mpd_connection_get_server_version(mpd->connection);
235 screen_status_printf(_("Error: MPD version %d.%d.%d is to old (%s needed)"),
236 version[0], version[1], version[2],
237 "0.12.0");
238 mpdclient_disconnect(mpd);
239 doupdate();
241 /* try again after 30 seconds */
242 reconnect_source_id = g_timeout_add(30000,
243 timer_reconnect, NULL);
244 return FALSE;
245 }
246 #endif
248 screen_status_printf(_("Connected to %s"),
249 options.host != NULL
250 ? options.host : "localhost");
251 doupdate();
253 /* update immediately */
254 mpd->events = MPD_IDLE_DATABASE|MPD_IDLE_STORED_PLAYLIST|
255 MPD_IDLE_QUEUE|MPD_IDLE_PLAYER|MPD_IDLE_MIXER|MPD_IDLE_OUTPUT|
256 MPD_IDLE_OPTIONS|MPD_IDLE_UPDATE;
258 do_mpd_update();
260 return FALSE;
261 }
263 static void
264 check_reconnect(void)
265 {
266 if (!mpdclient_is_connected(mpd) && reconnect_source_id == 0)
267 /* reconnect when the connection is lost */
268 reconnect_source_id = g_timeout_add(1000, timer_reconnect,
269 NULL);
270 }
272 static gboolean
273 timer_mpd_update(G_GNUC_UNUSED gpointer data)
274 {
275 do_mpd_update();
277 return true;
278 }
280 void begin_input_event(void)
281 {
282 }
284 void end_input_event(void)
285 {
286 screen_update(mpd);
287 mpd->events = 0;
289 check_reconnect();
290 }
292 int do_input_event(command_t cmd)
293 {
294 if (cmd == CMD_QUIT) {
295 g_main_loop_quit(main_loop);
296 return -1;
297 }
299 screen_cmd(mpd, cmd);
301 if (cmd == CMD_VOLUME_UP || cmd == CMD_VOLUME_DOWN) {
302 /* make sure we don't update the volume yet */
303 disable_update_timer();
304 enable_update_timer();
305 }
307 return 0;
308 }
310 static gboolean
311 keyboard_event(G_GNUC_UNUSED GIOChannel *source,
312 G_GNUC_UNUSED GIOCondition condition,
313 G_GNUC_UNUSED gpointer data)
314 {
315 command_t cmd;
317 begin_input_event();
319 if ((cmd=get_keyboard_command()) != CMD_NONE)
320 if (do_input_event(cmd) != 0)
321 return FALSE;
323 end_input_event();
324 return TRUE;
325 }
327 #ifndef NCMPC_MINI
328 /**
329 * Check the configured key bindings for errors, and display a status
330 * message every 10 seconds.
331 */
332 static gboolean
333 timer_check_key_bindings(G_GNUC_UNUSED gpointer data)
334 {
335 char buf[256];
336 #ifdef ENABLE_KEYDEF_SCREEN
337 char comment[64];
338 #endif
339 gboolean key_error;
341 key_error = check_key_bindings(NULL, buf, sizeof(buf));
342 if (!key_error) {
343 /* no error: disable this timer for the rest of this
344 process */
345 check_key_bindings_source_id = 0;
346 return FALSE;
347 }
349 #ifdef ENABLE_KEYDEF_SCREEN
350 g_strchomp(buf);
351 g_strlcat(buf, " (", sizeof(buf));
352 /* to translators: a key was bound twice in the key editor,
353 and this is a hint for the user what to press to correct
354 that */
355 g_snprintf(comment, sizeof(comment), _("press %s for the key editor"),
356 get_key_names(CMD_SCREEN_KEYDEF, 0));
357 g_strlcat(buf, comment, sizeof(buf));
358 g_strlcat(buf, ")", sizeof(buf));
359 #endif
361 screen_status_printf("%s", buf);
363 doupdate();
364 return TRUE;
365 }
366 #endif
368 int
369 main(int argc, const char *argv[])
370 {
371 struct sigaction act;
372 #ifdef ENABLE_LOCALE
373 const char *charset = NULL;
374 #endif
375 GIOChannel *keyboard_channel;
376 #ifdef ENABLE_LIRC
377 int lirc_socket;
378 GIOChannel *lirc_channel = NULL;
379 #endif
381 #ifdef ENABLE_LOCALE
382 /* time and date formatting */
383 setlocale(LC_TIME,"");
384 /* care about sorting order etc */
385 setlocale(LC_COLLATE,"");
386 /* charset */
387 setlocale(LC_CTYPE,"");
388 /* initialize charset conversions */
389 charset = charset_init();
391 /* initialize i18n support */
392 #endif
394 #ifdef ENABLE_NLS
395 setlocale(LC_MESSAGES, "");
396 bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
397 #ifdef ENABLE_LOCALE
398 bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
399 #endif
400 textdomain(GETTEXT_PACKAGE);
401 #endif
403 /* initialize options */
404 options_init();
406 /* parse command line options - 1 pass get configuration files */
407 options_parse(argc, argv);
409 #ifndef NCMPC_MINI
410 /* read configuration */
411 read_configuration();
413 /* check key bindings */
414 check_key_bindings(NULL, NULL, 0);
415 #endif
417 /* parse command line options - 2 pass */
418 options_parse(argc, argv);
420 /* setup signal behavior - SIGINT */
421 sigemptyset(&act.sa_mask);
422 act.sa_flags = 0;
423 act.sa_handler = catch_sigint;
424 if (sigaction(SIGINT, &act, NULL) < 0) {
425 perror("signal");
426 exit(EXIT_FAILURE);
427 }
429 /* setup signal behavior - SIGTERM */
431 act.sa_handler = catch_sigint;
432 if (sigaction(SIGTERM, &act, NULL) < 0) {
433 perror("sigaction()");
434 exit(EXIT_FAILURE);
435 }
437 /* setup signal behavior - SIGCONT */
439 act.sa_handler = catch_sigcont;
440 if (sigaction(SIGCONT, &act, NULL) < 0) {
441 perror("sigaction(SIGCONT)");
442 exit(EXIT_FAILURE);
443 }
445 /* setup signal behaviour - SIGHUP*/
447 act.sa_handler = catch_sigint;
448 if (sigaction(SIGHUP, &act, NULL) < 0) {
449 perror("sigaction(SIGHUP)");
450 exit(EXIT_FAILURE);
451 }
453 /* setup SIGWINCH */
455 act.sa_flags = SA_RESTART;
456 act.sa_handler = catch_sigwinch;
457 if (sigaction(SIGWINCH, &act, NULL) < 0) {
458 perror("sigaction(SIGWINCH)");
459 exit(EXIT_FAILURE);
460 }
462 /* ignore SIGPIPE */
464 act.sa_handler = SIG_IGN;
465 if (sigaction(SIGPIPE, &act, NULL) < 0) {
466 perror("sigaction(SIGPIPE)");
467 exit(EXIT_FAILURE);
468 }
470 ncu_init();
472 #ifdef ENABLE_LYRICS_SCREEN
473 lyrics_init();
474 #endif
476 /* create mpdclient instance */
477 mpd = mpdclient_new();
479 /* initialize curses */
480 screen_init(mpd);
482 /* the main loop */
483 main_loop = g_main_loop_new(NULL, FALSE);
485 /* watch out for keyboard input */
486 keyboard_channel = g_io_channel_unix_new(STDIN_FILENO);
487 g_io_add_watch(keyboard_channel, G_IO_IN, keyboard_event, NULL);
489 #ifdef ENABLE_LIRC
490 /* watch out for lirc input */
491 lirc_socket = ncmpc_lirc_open();
492 if (lirc_socket >= 0) {
493 lirc_channel = g_io_channel_unix_new(lirc_socket);
494 g_io_add_watch(lirc_channel, G_IO_IN, lirc_event, NULL);
495 }
496 #endif
498 /* attempt to connect */
499 reconnect_source_id = g_timeout_add(1, timer_reconnect, NULL);
501 enable_update_timer();
503 #ifndef NCMPC_MINI
504 check_key_bindings_source_id = g_timeout_add(10000, timer_check_key_bindings, NULL);
505 #endif
507 screen_paint(mpd);
509 g_main_loop_run(main_loop);
511 /* cleanup */
513 cancel_seek_timer();
515 disable_update_timer();
517 if (reconnect_source_id != 0)
518 g_source_remove(reconnect_source_id);
520 #ifndef NCMPC_MINI
521 if (check_key_bindings_source_id != 0)
522 g_source_remove(check_key_bindings_source_id);
523 #endif
525 g_main_loop_unref(main_loop);
526 g_io_channel_unref(keyboard_channel);
528 #ifdef ENABLE_LIRC
529 if (lirc_socket >= 0)
530 g_io_channel_unref(lirc_channel);
531 ncmpc_lirc_close();
532 #endif
534 exit_and_cleanup();
536 #ifdef ENABLE_LYRICS_SCREEN
537 lyrics_deinit();
538 #endif
540 ncu_deinit();
541 options_deinit();
543 return 0;
544 }