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 #ifndef NCMPC_MINI
66 static guint check_key_bindings_source_id;
67 #endif
69 static const gchar *
70 error_msg(const gchar *msg)
71 {
72 gchar *p;
74 if ((p = strchr(msg, '}')) == NULL)
75 return msg;
77 do {
78 p++;
79 } while (*p == '}' || * p== ' ');
81 return p;
82 }
84 static void
85 error_callback(G_GNUC_UNUSED mpdclient_t *c, gint error, const gchar *_msg)
86 {
87 char *msg = utf8_to_locale(_msg);
89 error = error & 0xFF;
90 switch (error) {
91 case MPD_ERROR_CONNPORT:
92 case MPD_ERROR_NORESPONSE:
93 break;
94 case MPD_ERROR_ACK:
95 screen_status_printf("%s", error_msg(msg));
96 screen_bell();
97 break;
98 default:
99 screen_status_printf("%s", msg);
100 screen_bell();
101 doupdate();
102 connected = FALSE;
103 }
105 g_free(msg);
106 }
108 #ifndef NCMPC_MINI
109 static void
110 update_xterm_title(void)
111 {
112 static char title[BUFSIZE];
113 char tmp[BUFSIZE];
114 mpd_Status *status = NULL;
115 mpd_Song *song = NULL;
117 if (mpd) {
118 status = mpd->status;
119 song = mpd->song;
120 }
122 if (options.xterm_title_format && status && song &&
123 IS_PLAYING(status->state))
124 strfsong(tmp, BUFSIZE, options.xterm_title_format, song);
125 else
126 g_strlcpy(tmp, PACKAGE " version " VERSION, BUFSIZE);
128 if (strncmp(title, tmp, BUFSIZE)) {
129 g_strlcpy(title, tmp, BUFSIZE);
130 set_xterm_title("%s", title);
131 }
132 }
133 #endif
135 static void
136 exit_and_cleanup(void)
137 {
138 screen_exit();
139 #ifndef NCMPC_MINI
140 set_xterm_title("");
141 #endif
142 printf("\n");
144 if (mpd) {
145 mpdclient_disconnect(mpd);
146 mpdclient_free(mpd);
147 }
148 }
150 static void
151 catch_sigint(G_GNUC_UNUSED int sig)
152 {
153 g_main_loop_quit(main_loop);
154 }
157 static void
158 catch_sigcont(G_GNUC_UNUSED int sig)
159 {
160 screen_resize(mpd);
161 }
163 void
164 sigstop(void)
165 {
166 def_prog_mode(); /* save the tty modes */
167 endwin(); /* end curses mode temporarily */
168 kill(0, SIGSTOP); /* issue SIGSTOP */
169 }
171 static guint timer_sigwinch_id;
173 static gboolean
174 timer_sigwinch(G_GNUC_UNUSED gpointer data)
175 {
176 /* the following causes the screen to flicker. There might be
177 better solutions, but I believe it isn't all that
178 important. */
180 endwin();
181 refresh();
182 screen_resize(mpd);
184 return FALSE;
185 }
187 static void
188 catch_sigwinch(G_GNUC_UNUSED int sig)
189 {
190 if (timer_sigwinch_id != 0)
191 g_source_remove(timer_sigwinch_id);
193 timer_sigwinch_id = g_timeout_add(100, timer_sigwinch, NULL);
194 }
196 static gboolean
197 timer_mpd_update(gpointer data);
199 /**
200 * This timer is installed when the connection to the MPD server is
201 * broken. It tries to recover by reconnecting periodically.
202 */
203 static gboolean
204 timer_reconnect(G_GNUC_UNUSED gpointer data)
205 {
206 int ret;
208 if (connected)
209 return FALSE;
211 screen_status_printf(_("Connecting to %s... [Press %s to abort]"),
212 options.host, get_key_names(CMD_QUIT,0) );
213 doupdate();
215 mpdclient_disconnect(mpd);
216 ret = mpdclient_connect(mpd,
217 options.host, options.port,
218 1.5,
219 options.password);
220 if (ret != 0) {
221 /* try again in 5 seconds */
222 g_timeout_add(5000, timer_reconnect, NULL);
223 return FALSE;
224 }
226 #ifndef NCMPC_MINI
227 /* quit if mpd is pre 0.11.0 - song id not supported by mpd */
228 if (MPD_VERSION_LT(mpd, 0, 11, 0)) {
229 screen_status_printf(_("Error: MPD version %d.%d.%d is to old (%s needed)"),
230 mpd->connection->version[0],
231 mpd->connection->version[1],
232 mpd->connection->version[2],
233 "0.11.0");
234 mpdclient_disconnect(mpd);
235 doupdate();
237 /* try again after 30 seconds */
238 g_timeout_add(30000, timer_reconnect, NULL);
239 return FALSE;
240 }
241 #endif
243 screen_status_printf(_("Connected to %s"), options.host);
244 doupdate();
246 connected = TRUE;
248 /* update immediately */
249 g_timeout_add(1, timer_mpd_update, GINT_TO_POINTER(FALSE));
251 reconnect_source_id = 0;
252 return FALSE;
254 }
256 static gboolean
257 timer_mpd_update(gpointer data)
258 {
259 if (connected)
260 mpdclient_update(mpd);
261 else if (reconnect_source_id == 0)
262 reconnect_source_id = g_timeout_add(1000, timer_reconnect,
263 NULL);
265 #ifndef NCMPC_MINI
266 if (options.enable_xterm_title)
267 update_xterm_title();
268 #endif
270 screen_update(mpd);
272 return GPOINTER_TO_INT(data);
273 }
275 /**
276 * This idle timer is invoked when the user hasn't typed a key for
277 * 500ms. It is used for delayed seeking.
278 */
279 static gboolean
280 timer_idle(G_GNUC_UNUSED gpointer data)
281 {
282 screen_idle(mpd);
283 return TRUE;
284 }
286 void begin_input_event(void)
287 {
288 /* remove the idle timeout; add it later with fresh interval */
289 g_source_remove(idle_source_id);
290 }
292 void end_input_event(void)
293 {
294 screen_update(mpd);
296 idle_source_id = g_timeout_add(idle_interval, timer_idle, NULL);
297 }
299 int do_input_event(command_t cmd)
300 {
301 if (cmd == CMD_QUIT) {
302 g_main_loop_quit(main_loop);
303 return -1;
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 g_source_remove(update_source_id);
311 update_source_id = g_timeout_add(update_interval,
312 timer_mpd_update,
313 GINT_TO_POINTER(TRUE));
314 }
316 return 0;
317 }
319 static gboolean
320 keyboard_event(G_GNUC_UNUSED GIOChannel *source,
321 G_GNUC_UNUSED GIOCondition condition,
322 G_GNUC_UNUSED gpointer data)
323 {
324 command_t cmd;
326 begin_input_event();
328 if ((cmd=get_keyboard_command()) != CMD_NONE)
329 if (do_input_event(cmd) != 0)
330 return FALSE;
332 end_input_event();
333 return TRUE;
334 }
336 #ifndef NCMPC_MINI
337 /**
338 * Check the configured key bindings for errors, and display a status
339 * message every 10 seconds.
340 */
341 static gboolean
342 timer_check_key_bindings(G_GNUC_UNUSED gpointer data)
343 {
344 char buf[256];
345 #ifdef ENABLE_KEYDEF_SCREEN
346 char comment[64];
347 #endif
348 gboolean key_error;
350 key_error = check_key_bindings(NULL, buf, sizeof(buf));
351 if (!key_error) {
352 /* no error: disable this timer for the rest of this
353 process */
354 check_key_bindings_source_id = 0;
355 return FALSE;
356 }
358 #ifdef ENABLE_KEYDEF_SCREEN
359 g_strchomp(buf);
360 g_strlcat(buf, " (", sizeof(buf));
361 /* to translators: a key was bound twice in the key editor,
362 and this is a hint for the user what to press to correct
363 that */
364 g_snprintf(comment, sizeof(comment), _("press %s for the key editor"),
365 get_key_names(CMD_SCREEN_KEYDEF, 0));
366 g_strlcat(buf, comment, sizeof(buf));
367 g_strlcat(buf, ")", sizeof(buf));
368 #endif
370 screen_status_printf("%s", buf);
372 doupdate();
373 return TRUE;
374 }
375 #endif
377 int
378 main(int argc, const char *argv[])
379 {
380 struct sigaction act;
381 #ifdef ENABLE_LOCALE
382 const char *charset = NULL;
383 #endif
384 GIOChannel *keyboard_channel;
385 #ifdef ENABLE_LIRC
386 int lirc_socket;
387 GIOChannel *lirc_channel = NULL;
388 #endif
390 #ifdef ENABLE_LOCALE
391 /* time and date formatting */
392 setlocale(LC_TIME,"");
393 /* care about sorting order etc */
394 setlocale(LC_COLLATE,"");
395 /* charset */
396 setlocale(LC_CTYPE,"");
397 /* initialize charset conversions */
398 charset = charset_init();
400 /* initialize i18n support */
401 #endif
403 #ifdef ENABLE_NLS
404 setlocale(LC_MESSAGES, "");
405 bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
406 #ifdef ENABLE_LOCALE
407 bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
408 #endif
409 textdomain(GETTEXT_PACKAGE);
410 #endif
412 /* initialize options */
413 options_init();
415 /* parse command line options - 1 pass get configuration files */
416 options_parse(argc, argv);
418 #ifndef NCMPC_MINI
419 /* read configuration */
420 read_configuration();
422 /* check key bindings */
423 check_key_bindings(NULL, NULL, 0);
424 #endif
426 /* parse command line options - 2 pass */
427 options_parse(argc, argv);
429 /* setup signal behavior - SIGINT */
430 sigemptyset(&act.sa_mask);
431 act.sa_flags = 0;
432 act.sa_handler = catch_sigint;
433 if (sigaction(SIGINT, &act, NULL) < 0) {
434 perror("signal");
435 exit(EXIT_FAILURE);
436 }
438 /* setup signal behavior - SIGTERM */
440 act.sa_handler = catch_sigint;
441 if (sigaction(SIGTERM, &act, NULL) < 0) {
442 perror("sigaction()");
443 exit(EXIT_FAILURE);
444 }
446 /* setup signal behavior - SIGCONT */
448 act.sa_handler = catch_sigcont;
449 if (sigaction(SIGCONT, &act, NULL) < 0) {
450 perror("sigaction(SIGCONT)");
451 exit(EXIT_FAILURE);
452 }
454 /* setup signal behaviour - SIGHUP*/
456 act.sa_handler = catch_sigint;
457 if (sigaction(SIGHUP, &act, NULL) < 0) {
458 perror("sigaction(SIGHUP)");
459 exit(EXIT_FAILURE);
460 }
462 /* setup SIGWINCH */
464 act.sa_flags = SA_RESTART;
465 act.sa_handler = catch_sigwinch;
466 if (sigaction(SIGWINCH, &act, NULL) < 0) {
467 perror("sigaction(SIGWINCH)");
468 exit(EXIT_FAILURE);
469 }
471 /* ignore SIGPIPE */
473 act.sa_handler = SIG_IGN;
474 if (sigaction(SIGPIPE, &act, NULL) < 0) {
475 perror("sigaction(SIGPIPE)");
476 exit(EXIT_FAILURE);
477 }
479 ncu_init();
481 #ifdef ENABLE_LYRICS_SCREEN
482 lyrics_init();
483 #endif
485 /* create mpdclient instance */
486 mpd = mpdclient_new();
487 mpdclient_install_error_callback(mpd, error_callback);
489 /* initialize curses */
490 screen_init(mpd);
492 /* the main loop */
493 main_loop = g_main_loop_new(NULL, FALSE);
495 /* watch out for keyboard input */
496 keyboard_channel = g_io_channel_unix_new(STDIN_FILENO);
497 g_io_add_watch(keyboard_channel, G_IO_IN, keyboard_event, NULL);
499 #ifdef ENABLE_LIRC
500 /* watch out for lirc input */
501 lirc_socket = ncmpc_lirc_open();
502 if (lirc_socket >= 0) {
503 lirc_channel = g_io_channel_unix_new(lirc_socket);
504 g_io_add_watch(lirc_channel, G_IO_IN, lirc_event, NULL);
505 }
506 #endif
508 /* attempt to connect */
509 reconnect_source_id = g_timeout_add(1, timer_reconnect, NULL);
511 update_source_id = g_timeout_add(update_interval,
512 timer_mpd_update,
513 GINT_TO_POINTER(TRUE));
514 #ifndef NCMPC_MINI
515 check_key_bindings_source_id = g_timeout_add(10000, timer_check_key_bindings, NULL);
516 #endif
517 idle_source_id = g_timeout_add(idle_interval, timer_idle, NULL);
519 screen_paint(mpd);
521 g_main_loop_run(main_loop);
523 /* cleanup */
525 g_source_remove(update_source_id);
526 g_source_remove(idle_source_id);
528 #ifndef NCMPC_MINI
529 if (check_key_bindings_source_id != 0)
530 g_source_remove(check_key_bindings_source_id);
531 #endif
533 g_main_loop_unref(main_loop);
534 g_io_channel_unref(keyboard_channel);
536 #ifdef ENABLE_LIRC
537 if (lirc_socket >= 0)
538 g_io_channel_unref(lirc_channel);
539 ncmpc_lirc_close();
540 #endif
542 exit_and_cleanup();
544 #ifdef ENABLE_LYRICS_SCREEN
545 lyrics_deinit();
546 #endif
548 ncu_deinit();
549 options_deinit();
551 return 0;
552 }