Code

398a46f78af3239b6a5f6c61190401433c66d056
[ncmpc.git] / src / main.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2009 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.
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"
31 #include "player_command.h"
33 #ifndef NCMPC_MINI
34 #include "conf.h"
35 #endif
37 #ifdef ENABLE_LYRICS_SCREEN
38 #include "lyrics.h"
39 #endif
41 #ifdef ENABLE_LIRC
42 #include "lirc.h"
43 #endif
45 #include <mpd/client.h>
47 #include <stdlib.h>
48 #include <unistd.h>
49 #include <signal.h>
50 #include <string.h>
52 #ifdef ENABLE_LOCALE
53 #include <locale.h>
54 #endif
56 /* time between mpd updates [s] */
57 static const guint update_interval = 500;
59 #define BUFSIZE 1024
61 static struct mpdclient *mpd = NULL;
62 static GMainLoop *main_loop;
63 static guint reconnect_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 struct mpdclient *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_SERVER:
92                 screen_status_printf("%s", error_msg(msg));
93                 screen_bell();
94                 break;
95         default:
96                 screen_status_printf("%s", msg);
97                 screen_bell();
98                 doupdate();
99         }
101         g_free(msg);
104 #ifndef NCMPC_MINI
105 static void
106 update_xterm_title(void)
108         static char title[BUFSIZE];
109         char tmp[BUFSIZE];
110         struct mpd_status *status = NULL;
111         struct 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(mpd_status_get_state(status)))
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         }
129 #endif
131 static void
132 exit_and_cleanup(void)
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         }
146 static void
147 catch_sigint(G_GNUC_UNUSED int sig)
149         g_main_loop_quit(main_loop);
153 static void
154 catch_sigcont(G_GNUC_UNUSED int sig)
156         screen_resize(mpd);
159 void
160 sigstop(void)
162   def_prog_mode();  /* save the tty modes */
163   endwin();         /* end curses mode temporarily */
164   kill(0, SIGSTOP); /* issue SIGSTOP */
167 static guint timer_sigwinch_id;
169 static gboolean
170 timer_sigwinch(G_GNUC_UNUSED gpointer data)
172         /* the following causes the screen to flicker.  There might be
173            better solutions, but I believe it isn't all that
174            important. */
176         endwin();
177         refresh();
178         screen_resize(mpd);
180         return FALSE;
183 static void
184 catch_sigwinch(G_GNUC_UNUSED int sig)
186         if (timer_sigwinch_id != 0)
187                 g_source_remove(timer_sigwinch_id);
189         timer_sigwinch_id = g_timeout_add(100, timer_sigwinch, NULL);
192 static gboolean
193 timer_mpd_update(gpointer data);
195 /**
196  * This timer is installed when the connection to the MPD server is
197  * broken.  It tries to recover by reconnecting periodically.
198  */
199 static gboolean
200 timer_reconnect(G_GNUC_UNUSED gpointer data)
202         int ret;
204         assert(!mpdclient_is_connected(mpd));
206         screen_status_printf(_("Connecting to %s...  [Press %s to abort]"),
207                              options.host, get_key_names(CMD_QUIT,0) );
208         doupdate();
210         mpdclient_disconnect(mpd);
211         ret = mpdclient_connect(mpd,
212                                 options.host, options.port,
213                                 1.5,
214                                 options.password);
215         if (ret != 0) {
216                 /* try again in 5 seconds */
217                 g_timeout_add(5000, timer_reconnect, NULL);
218                 return FALSE;
219         }
221 #ifndef NCMPC_MINI
222         /* quit if mpd is pre 0.11.0 - song id not supported by mpd */
223         if (mpd_connection_cmp_server_version(mpd->connection, 0, 11, 0) < 0) {
224                 const unsigned *version =
225                         mpd_connection_get_server_version(mpd->connection);
226                 screen_status_printf(_("Error: MPD version %d.%d.%d is to old (%s needed)"),
227                                      version[0], version[1], version[2],
228                                      "0.11.0");
229                 mpdclient_disconnect(mpd);
230                 doupdate();
232                 /* try again after 30 seconds */
233                 g_timeout_add(30000, timer_reconnect, NULL);
234                 return FALSE;
235         }
236 #endif
238         screen_status_printf(_("Connected to %s"),
239                              options.host != NULL
240                              ? options.host : "localhost");
241         doupdate();
243         /* update immediately */
244         g_timeout_add(1, timer_mpd_update, GINT_TO_POINTER(FALSE));
246         reconnect_source_id = 0;
247         return FALSE;
251 static gboolean
252 timer_mpd_update(gpointer data)
254         if (mpdclient_is_connected(mpd))
255                 mpdclient_update(mpd);
256         else if (reconnect_source_id == 0)
257                 reconnect_source_id = g_timeout_add(1000, timer_reconnect,
258                                                     NULL);
260 #ifndef NCMPC_MINI
261         if (options.enable_xterm_title)
262                 update_xterm_title();
263 #endif
265         screen_update(mpd);
267         return GPOINTER_TO_INT(data);
270 void begin_input_event(void)
274 void end_input_event(void)
276         screen_update(mpd);
279 int do_input_event(command_t cmd)
281         if (cmd == CMD_QUIT) {
282                 g_main_loop_quit(main_loop);
283                 return -1;
284         }
286         screen_cmd(mpd, cmd);
288         if (cmd == CMD_VOLUME_UP || cmd == CMD_VOLUME_DOWN) {
289                 /* make sure we don't update the volume yet */
290                 g_source_remove(update_source_id);
291                 update_source_id = g_timeout_add(update_interval,
292                                                  timer_mpd_update,
293                                                  GINT_TO_POINTER(TRUE));
294         }
296         return 0;
299 static gboolean
300 keyboard_event(G_GNUC_UNUSED GIOChannel *source,
301                G_GNUC_UNUSED GIOCondition condition,
302                G_GNUC_UNUSED gpointer data)
304         command_t cmd;
306         begin_input_event();
308         if ((cmd=get_keyboard_command()) != CMD_NONE)
309                 if (do_input_event(cmd) != 0)
310                         return FALSE;
312         end_input_event();
313         return TRUE;
316 #ifndef NCMPC_MINI
317 /**
318  * Check the configured key bindings for errors, and display a status
319  * message every 10 seconds.
320  */
321 static gboolean
322 timer_check_key_bindings(G_GNUC_UNUSED gpointer data)
324         char buf[256];
325 #ifdef ENABLE_KEYDEF_SCREEN
326         char comment[64];
327 #endif
328         gboolean key_error;
330         key_error = check_key_bindings(NULL, buf, sizeof(buf));
331         if (!key_error) {
332                 /* no error: disable this timer for the rest of this
333                    process */
334                 check_key_bindings_source_id = 0;
335                 return FALSE;
336         }
338 #ifdef ENABLE_KEYDEF_SCREEN
339         g_strchomp(buf);
340         g_strlcat(buf, " (", sizeof(buf));
341         /* to translators: a key was bound twice in the key editor,
342            and this is a hint for the user what to press to correct
343            that */
344         g_snprintf(comment, sizeof(comment), _("press %s for the key editor"),
345                    get_key_names(CMD_SCREEN_KEYDEF, 0));
346         g_strlcat(buf, comment, sizeof(buf));
347         g_strlcat(buf, ")", sizeof(buf));
348 #endif
350         screen_status_printf("%s", buf);
352         doupdate();
353         return TRUE;
355 #endif
357 int
358 main(int argc, const char *argv[])
360         struct sigaction act;
361 #ifdef ENABLE_LOCALE
362         const char *charset = NULL;
363 #endif
364         GIOChannel *keyboard_channel;
365 #ifdef ENABLE_LIRC
366         int lirc_socket;
367         GIOChannel *lirc_channel = NULL;
368 #endif
370 #ifdef ENABLE_LOCALE
371         /* time and date formatting */
372         setlocale(LC_TIME,"");
373         /* care about sorting order etc */
374         setlocale(LC_COLLATE,"");
375         /* charset */
376         setlocale(LC_CTYPE,"");
377         /* initialize charset conversions */
378         charset = charset_init();
380         /* initialize i18n support */
381 #endif
383 #ifdef ENABLE_NLS
384         setlocale(LC_MESSAGES, "");
385         bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
386 #ifdef ENABLE_LOCALE
387         bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
388 #endif
389         textdomain(GETTEXT_PACKAGE);
390 #endif
392         /* initialize options */
393         options_init();
395         /* parse command line options - 1 pass get configuration files */
396         options_parse(argc, argv);
398 #ifndef NCMPC_MINI
399         /* read configuration */
400         read_configuration();
402         /* check key bindings */
403         check_key_bindings(NULL, NULL, 0);
404 #endif
406         /* parse command line options - 2 pass */
407         options_parse(argc, argv);
409         /* setup signal behavior - SIGINT */
410         sigemptyset(&act.sa_mask);
411         act.sa_flags = 0;
412         act.sa_handler = catch_sigint;
413         if (sigaction(SIGINT, &act, NULL) < 0) {
414                 perror("signal");
415                 exit(EXIT_FAILURE);
416         }
418         /* setup signal behavior - SIGTERM */
420         act.sa_handler = catch_sigint;
421         if (sigaction(SIGTERM, &act, NULL) < 0) {
422                 perror("sigaction()");
423                 exit(EXIT_FAILURE);
424         }
426         /* setup signal behavior - SIGCONT */
428         act.sa_handler = catch_sigcont;
429         if (sigaction(SIGCONT, &act, NULL) < 0) {
430                 perror("sigaction(SIGCONT)");
431                 exit(EXIT_FAILURE);
432         }
434         /* setup signal behaviour - SIGHUP*/
436         act.sa_handler = catch_sigint;
437         if (sigaction(SIGHUP, &act, NULL) < 0) {
438                 perror("sigaction(SIGHUP)");
439                 exit(EXIT_FAILURE);
440         }
442         /* setup SIGWINCH */
444         act.sa_flags = SA_RESTART;
445         act.sa_handler = catch_sigwinch;
446         if (sigaction(SIGWINCH, &act, NULL) < 0) {
447                 perror("sigaction(SIGWINCH)");
448                 exit(EXIT_FAILURE);
449         }
451         /* ignore SIGPIPE */
453         act.sa_handler = SIG_IGN;
454         if (sigaction(SIGPIPE, &act, NULL) < 0) {
455                 perror("sigaction(SIGPIPE)");
456                 exit(EXIT_FAILURE);
457         }
459         ncu_init();
461 #ifdef ENABLE_LYRICS_SCREEN
462         lyrics_init();
463 #endif
465         /* create mpdclient instance */
466         mpd = mpdclient_new();
467         mpdclient_install_error_callback(mpd, error_callback);
469         /* initialize curses */
470         screen_init(mpd);
472         /* the main loop */
473         main_loop = g_main_loop_new(NULL, FALSE);
475         /* watch out for keyboard input */
476         keyboard_channel = g_io_channel_unix_new(STDIN_FILENO);
477         g_io_add_watch(keyboard_channel, G_IO_IN, keyboard_event, NULL);
479 #ifdef ENABLE_LIRC
480         /* watch out for lirc input */
481         lirc_socket = ncmpc_lirc_open();
482         if (lirc_socket >= 0) {
483                 lirc_channel = g_io_channel_unix_new(lirc_socket);
484                 g_io_add_watch(lirc_channel, G_IO_IN, lirc_event, NULL);
485         }
486 #endif
488         /* attempt to connect */
489         reconnect_source_id = g_timeout_add(1, timer_reconnect, NULL);
491         update_source_id = g_timeout_add(update_interval,
492                                          timer_mpd_update,
493                                          GINT_TO_POINTER(TRUE));
494 #ifndef NCMPC_MINI
495         check_key_bindings_source_id = g_timeout_add(10000, timer_check_key_bindings, NULL);
496 #endif
498         screen_paint(mpd);
500         g_main_loop_run(main_loop);
502         /* cleanup */
504         cancel_seek_timer();
506         g_source_remove(update_source_id);
508 #ifndef NCMPC_MINI
509         if (check_key_bindings_source_id != 0)
510                 g_source_remove(check_key_bindings_source_id);
511 #endif
513         g_main_loop_unref(main_loop);
514         g_io_channel_unref(keyboard_channel);
516 #ifdef ENABLE_LIRC
517         if (lirc_socket >= 0)
518                 g_io_channel_unref(lirc_channel);
519         ncmpc_lirc_close();
520 #endif
522         exit_and_cleanup();
524 #ifdef ENABLE_LYRICS_SCREEN
525         lyrics_deinit();
526 #endif
528         ncu_deinit();
529         options_deinit();
531         return 0;