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 "strfsong.h"
30 #include "i18n.h"
32 #ifndef NCMPC_MINI
33 #include "conf.h"
34 #endif
36 #ifdef ENABLE_LYRICS_SCREEN
37 #include "lyrics.h"
38 #endif
40 #ifdef ENABLE_LIRC
41 #include "lirc.h"
42 #endif
44 #include <stdlib.h>
45 #include <unistd.h>
46 #include <signal.h>
47 #include <string.h>
49 #ifdef ENABLE_LOCALE
50 #include <locale.h>
51 #endif
53 /* time between mpd updates [s] */
54 static const guint update_interval = 500;
56 #define BUFSIZE 1024
58 static const guint idle_interval = 500;
60 static mpdclient_t *mpd = NULL;
61 static gboolean connected = FALSE;
62 static GMainLoop *main_loop;
63 static guint reconnect_source_id, idle_source_id, update_source_id;
65 static const gchar *
66 error_msg(const gchar *msg)
67 {
68 gchar *p;
70 if ((p = strchr(msg, '}')) == NULL)
71 return msg;
73 do {
74 p++;
75 } while (*p == '}' || * p== ' ');
77 return p;
78 }
80 static void
81 error_callback(G_GNUC_UNUSED mpdclient_t *c, gint error, const gchar *_msg)
82 {
83 char *msg = utf8_to_locale(_msg);
85 error = error & 0xFF;
86 switch (error) {
87 case MPD_ERROR_CONNPORT:
88 case MPD_ERROR_NORESPONSE:
89 break;
90 case MPD_ERROR_ACK:
91 screen_status_printf("%s", error_msg(msg));
92 screen_bell();
93 break;
94 default:
95 screen_status_printf("%s", msg);
96 screen_bell();
97 doupdate();
98 connected = FALSE;
99 }
101 g_free(msg);
102 }
104 #ifndef NCMPC_MINI
105 static void
106 update_xterm_title(void)
107 {
108 static char title[BUFSIZE];
109 char tmp[BUFSIZE];
110 mpd_Status *status = NULL;
111 mpd_Song *song = NULL;
113 if (mpd) {
114 status = mpd->status;
115 song = mpd->song;
116 }
118 if (options.xterm_title_format && status && song &&
119 IS_PLAYING(status->state))
120 strfsong(tmp, BUFSIZE, options.xterm_title_format, song);
121 else
122 g_strlcpy(tmp, PACKAGE " version " VERSION, BUFSIZE);
124 if (strncmp(title, tmp, BUFSIZE)) {
125 g_strlcpy(title, tmp, BUFSIZE);
126 set_xterm_title("%s", title);
127 }
128 }
129 #endif
131 static void
132 exit_and_cleanup(void)
133 {
134 screen_exit();
135 #ifndef NCMPC_MINI
136 set_xterm_title("");
137 #endif
138 printf("\n");
140 if (mpd) {
141 mpdclient_disconnect(mpd);
142 mpdclient_free(mpd);
143 }
145 g_free(options.host);
146 g_free(options.password);
147 g_free(options.list_format);
148 g_free(options.status_format);
149 #ifndef NCMPC_MINI
150 g_free(options.scroll_sep);
151 #endif
152 }
154 static void
155 catch_sigint(G_GNUC_UNUSED int sig)
156 {
157 g_main_loop_quit(main_loop);
158 }
161 static void
162 catch_sigcont(G_GNUC_UNUSED int sig)
163 {
164 screen_resize(mpd);
165 }
167 void
168 sigstop(void)
169 {
170 def_prog_mode(); /* save the tty modes */
171 endwin(); /* end curses mode temporarily */
172 kill(0, SIGSTOP); /* issue SIGSTOP */
173 }
175 static guint timer_sigwinch_id;
177 static gboolean
178 timer_sigwinch(G_GNUC_UNUSED gpointer data)
179 {
180 /* the following causes the screen to flicker. There might be
181 better solutions, but I believe it isn't all that
182 important. */
184 endwin();
185 refresh();
186 screen_resize(mpd);
188 return FALSE;
189 }
191 static void
192 catch_sigwinch(G_GNUC_UNUSED int sig)
193 {
194 if (timer_sigwinch_id != 0)
195 g_source_remove(timer_sigwinch_id);
197 timer_sigwinch_id = g_timeout_add(100, timer_sigwinch, NULL);
198 }
200 static gboolean
201 timer_mpd_update(gpointer data);
203 /**
204 * This timer is installed when the connection to the MPD server is
205 * broken. It tries to recover by reconnecting periodically.
206 */
207 static gboolean
208 timer_reconnect(G_GNUC_UNUSED gpointer data)
209 {
210 int ret;
212 if (connected)
213 return FALSE;
215 screen_status_printf(_("Connecting to %s... [Press %s to abort]"),
216 options.host, get_key_names(CMD_QUIT,0) );
217 doupdate();
219 mpdclient_disconnect(mpd);
220 ret = mpdclient_connect(mpd,
221 options.host, options.port,
222 1.5,
223 options.password);
224 if (ret != 0) {
225 /* try again in 5 seconds */
226 g_timeout_add(5000, 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_VERSION_LT(mpd, 0, 11, 0)) {
233 screen_status_printf(_("Error: MPD version %d.%d.%d is to old (%s needed)"),
234 mpd->connection->version[0],
235 mpd->connection->version[1],
236 mpd->connection->version[2],
237 "0.11.0");
238 mpdclient_disconnect(mpd);
239 doupdate();
241 /* try again after 30 seconds */
242 g_timeout_add(30000, timer_reconnect, NULL);
243 return FALSE;
244 }
245 #endif
247 screen_status_printf(_("Connected to %s"), options.host);
248 doupdate();
250 connected = TRUE;
252 /* update immediately */
253 g_timeout_add(1, timer_mpd_update, GINT_TO_POINTER(FALSE));
255 reconnect_source_id = 0;
256 return FALSE;
258 }
260 static gboolean
261 timer_mpd_update(gpointer data)
262 {
263 if (connected)
264 mpdclient_update(mpd);
265 else if (reconnect_source_id == 0)
266 reconnect_source_id = g_timeout_add(1000, timer_reconnect,
267 NULL);
269 #ifndef NCMPC_MINI
270 if (options.enable_xterm_title)
271 update_xterm_title();
272 #endif
274 screen_update(mpd);
276 return GPOINTER_TO_INT(data);
277 }
279 /**
280 * This idle timer is invoked when the user hasn't typed a key for
281 * 500ms. It is used for delayed seeking.
282 */
283 static gboolean
284 timer_idle(G_GNUC_UNUSED gpointer data)
285 {
286 screen_idle(mpd);
287 return TRUE;
288 }
290 void begin_input_event(void)
291 {
292 /* remove the idle timeout; add it later with fresh interval */
293 g_source_remove(idle_source_id);
294 }
296 void end_input_event(void)
297 {
298 screen_update(mpd);
300 idle_source_id = g_timeout_add(idle_interval, timer_idle, NULL);
301 }
303 int do_input_event(command_t cmd)
304 {
305 if (cmd == CMD_QUIT) {
306 g_main_loop_quit(main_loop);
307 return -1;
308 }
310 screen_cmd(mpd, cmd);
312 if (cmd == CMD_VOLUME_UP || cmd == CMD_VOLUME_DOWN) {
313 /* make sure we don't update the volume yet */
314 g_source_remove(update_source_id);
315 update_source_id = g_timeout_add(update_interval,
316 timer_mpd_update,
317 GINT_TO_POINTER(TRUE));
318 }
320 return 0;
321 }
323 static gboolean
324 keyboard_event(G_GNUC_UNUSED GIOChannel *source,
325 G_GNUC_UNUSED GIOCondition condition,
326 G_GNUC_UNUSED gpointer data)
327 {
328 command_t cmd;
330 begin_input_event();
332 if ((cmd=get_keyboard_command()) != CMD_NONE)
333 if (do_input_event(cmd) != 0)
334 return FALSE;
336 end_input_event();
337 return TRUE;
338 }
340 #ifndef NCMPC_MINI
341 /**
342 * Check the configured key bindings for errors, and display a status
343 * message every 10 seconds.
344 */
345 static gboolean
346 timer_check_key_bindings(G_GNUC_UNUSED gpointer data)
347 {
348 char buf[256];
349 #ifdef ENABLE_KEYDEF_SCREEN
350 char comment[64];
351 #endif
352 gboolean key_error;
354 key_error = check_key_bindings(NULL, buf, sizeof(buf));
355 if (!key_error)
356 /* no error: disable this timer for the rest of this
357 process */
358 return FALSE;
360 #ifdef ENABLE_KEYDEF_SCREEN
361 g_strchomp(buf);
362 g_strlcat(buf, " (", sizeof(buf));
363 /* to translators: a key was bound twice in the key editor,
364 and this is a hint for the user what to press to correct
365 that */
366 g_snprintf(comment, sizeof(comment), _("press %s for the key editor"),
367 get_key_names(CMD_SCREEN_KEYDEF, 0));
368 g_strlcat(buf, comment, sizeof(buf));
369 g_strlcat(buf, ")", sizeof(buf));
370 #endif
372 screen_status_printf("%s", buf);
374 doupdate();
375 return TRUE;
376 }
377 #endif
379 int
380 main(int argc, const char *argv[])
381 {
382 struct sigaction act;
383 #ifdef ENABLE_LOCALE
384 const char *charset = NULL;
385 #endif
386 GIOChannel *keyboard_channel;
387 #ifdef ENABLE_LIRC
388 int lirc_socket;
389 GIOChannel *lirc_channel = NULL;
390 #endif
392 #ifdef ENABLE_LOCALE
393 /* time and date formatting */
394 setlocale(LC_TIME,"");
395 /* care about sorting order etc */
396 setlocale(LC_COLLATE,"");
397 /* charset */
398 setlocale(LC_CTYPE,"");
399 /* initialize charset conversions */
400 charset = charset_init();
402 /* initialize i18n support */
403 #endif
405 #ifdef ENABLE_NLS
406 setlocale(LC_MESSAGES, "");
407 bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
408 #ifdef ENABLE_LOCALE
409 bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
410 #endif
411 textdomain(GETTEXT_PACKAGE);
412 #endif
414 /* initialize options */
415 options_init();
417 /* parse command line options - 1 pass get configuration files */
418 options_parse(argc, argv);
420 #ifndef NCMPC_MINI
421 /* read configuration */
422 read_configuration();
424 /* check key bindings */
425 check_key_bindings(NULL, NULL, 0);
426 #endif
428 /* parse command line options - 2 pass */
429 options_parse(argc, argv);
431 /* setup signal behavior - SIGINT */
432 sigemptyset(&act.sa_mask);
433 act.sa_flags = 0;
434 act.sa_handler = catch_sigint;
435 if (sigaction(SIGINT, &act, NULL) < 0) {
436 perror("signal");
437 exit(EXIT_FAILURE);
438 }
440 /* setup signal behavior - SIGTERM */
442 act.sa_handler = catch_sigint;
443 if (sigaction(SIGTERM, &act, NULL) < 0) {
444 perror("sigaction()");
445 exit(EXIT_FAILURE);
446 }
448 /* setup signal behavior - SIGCONT */
450 act.sa_handler = catch_sigcont;
451 if (sigaction(SIGCONT, &act, NULL) < 0) {
452 perror("sigaction(SIGCONT)");
453 exit(EXIT_FAILURE);
454 }
456 /* setup signal behaviour - SIGHUP*/
458 act.sa_handler = catch_sigint;
459 if (sigaction(SIGHUP, &act, NULL) < 0) {
460 perror("sigaction(SIGHUP)");
461 exit(EXIT_FAILURE);
462 }
464 /* setup SIGWINCH */
466 act.sa_flags = SA_RESTART;
467 act.sa_handler = catch_sigwinch;
468 if (sigaction(SIGWINCH, &act, NULL) < 0) {
469 perror("sigaction(SIGWINCH)");
470 exit(EXIT_FAILURE);
471 }
473 /* ignore SIGPIPE */
475 act.sa_handler = SIG_IGN;
476 if (sigaction(SIGPIPE, &act, NULL) < 0) {
477 perror("sigaction(SIGPIPE)");
478 exit(EXIT_FAILURE);
479 }
481 ncu_init();
483 #ifdef ENABLE_LYRICS_SCREEN
484 lyrics_init();
485 #endif
487 /* create mpdclient instance */
488 mpd = mpdclient_new();
489 mpdclient_install_error_callback(mpd, error_callback);
491 /* initialize curses */
492 screen_init(mpd);
494 /* the main loop */
495 main_loop = g_main_loop_new(NULL, FALSE);
497 /* watch out for keyboard input */
498 keyboard_channel = g_io_channel_unix_new(STDIN_FILENO);
499 g_io_add_watch(keyboard_channel, G_IO_IN, keyboard_event, NULL);
501 #ifdef ENABLE_LIRC
502 /* watch out for lirc input */
503 lirc_socket = ncmpc_lirc_open();
504 if (lirc_socket >= 0) {
505 lirc_channel = g_io_channel_unix_new(lirc_socket);
506 g_io_add_watch(lirc_channel, G_IO_IN, lirc_event, NULL);
507 }
508 #endif
510 /* attempt to connect */
511 reconnect_source_id = g_timeout_add(1, timer_reconnect, NULL);
513 update_source_id = g_timeout_add(update_interval,
514 timer_mpd_update,
515 GINT_TO_POINTER(TRUE));
516 #ifndef NCMPC_MINI
517 g_timeout_add(10000, timer_check_key_bindings, NULL);
518 #endif
519 idle_source_id = g_timeout_add(idle_interval, timer_idle, NULL);
521 screen_paint(mpd);
523 g_main_loop_run(main_loop);
525 /* cleanup */
527 g_main_loop_unref(main_loop);
528 g_io_channel_unref(keyboard_channel);
530 #ifdef ENABLE_LIRC
531 if (lirc_socket >= 0)
532 g_io_channel_unref(lirc_channel);
533 ncmpc_lirc_close();
534 #endif
536 exit_and_cleanup();
538 #ifdef ENABLE_LYRICS_SCREEN
539 lyrics_deinit();
540 #endif
542 ncu_deinit();
544 return 0;
545 }