Code

Update copyright notices
[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"
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);
104 #ifndef NCMPC_MINI
105 static void
106 update_xterm_title(void)
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         }
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         }
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
154 static void
155 catch_sigint(G_GNUC_UNUSED int sig)
157         g_main_loop_quit(main_loop);
161 static void
162 catch_sigcont(G_GNUC_UNUSED int sig)
164         screen_resize(mpd);
167 void
168 sigstop(void)
170   def_prog_mode();  /* save the tty modes */
171   endwin();         /* end curses mode temporarily */
172   kill(0, SIGSTOP); /* issue SIGSTOP */
175 static guint timer_sigwinch_id;
177 static gboolean
178 timer_sigwinch(G_GNUC_UNUSED gpointer data)
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;
191 static void
192 catch_sigwinch(G_GNUC_UNUSED int sig)
194         if (timer_sigwinch_id != 0)
195                 g_source_remove(timer_sigwinch_id);
197         timer_sigwinch_id = g_timeout_add(100, timer_sigwinch, NULL);
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)
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;
260 static gboolean
261 timer_mpd_update(gpointer data)
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);
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)
286         screen_idle(mpd);
287         return TRUE;
290 void begin_input_event(void)
292         /* remove the idle timeout; add it later with fresh interval */
293         g_source_remove(idle_source_id);
296 void end_input_event(void)
298         screen_update(mpd);
300         idle_source_id = g_timeout_add(idle_interval, timer_idle, NULL);
303 int do_input_event(command_t cmd)
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 dont 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;
323 static gboolean
324 keyboard_event(G_GNUC_UNUSED GIOChannel *source,
325                G_GNUC_UNUSED GIOCondition condition,
326                G_GNUC_UNUSED gpointer data)
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;
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)
348         char buf[256];
349         gboolean key_error;
351         key_error = check_key_bindings(NULL, buf, sizeof(buf));
352         if (!key_error)
353                 /* no error: disable this timer for the rest of this
354                    process */
355                 return FALSE;
357         screen_status_printf("%s", buf);
358         doupdate();
359         return TRUE;
361 #endif
363 int
364 main(int argc, const char *argv[])
366         struct sigaction act;
367 #ifdef ENABLE_LOCALE
368         const char *charset = NULL;
369 #endif
370         GIOChannel *keyboard_channel;
371 #ifdef ENABLE_LIRC
372         int lirc_socket;
373         GIOChannel *lirc_channel = NULL;
374 #endif
376 #ifdef ENABLE_LOCALE
377         /* time and date formatting */
378         setlocale(LC_TIME,"");
379         /* care about sorting order etc */
380         setlocale(LC_COLLATE,"");
381         /* charset */
382         setlocale(LC_CTYPE,"");
383         /* initialize charset conversions */
384         charset = charset_init();
386         /* initialize i18n support */
387 #endif
389 #ifdef ENABLE_NLS
390         setlocale(LC_MESSAGES, "");
391         bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
392 #ifdef ENABLE_LOCALE
393         bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
394 #endif
395         textdomain(GETTEXT_PACKAGE);
396 #endif
398         /* initialize options */
399         options_init();
401         /* parse command line options - 1 pass get configuration files */
402         options_parse(argc, argv);
404 #ifndef NCMPC_MINI
405         /* read configuration */
406         read_configuration();
408         /* check key bindings */
409         check_key_bindings(NULL, NULL, 0);
410 #endif
412         /* parse command line options - 2 pass */
413         options_parse(argc, argv);
415         /* setup signal behavior - SIGINT */
416         sigemptyset(&act.sa_mask);
417         act.sa_flags = 0;
418         act.sa_handler = catch_sigint;
419         if (sigaction(SIGINT, &act, NULL) < 0) {
420                 perror("signal");
421                 exit(EXIT_FAILURE);
422         }
424         /* setup signal behavior - SIGTERM */
426         act.sa_handler = catch_sigint;
427         if (sigaction(SIGTERM, &act, NULL) < 0) {
428                 perror("sigaction()");
429                 exit(EXIT_FAILURE);
430         }
432         /* setup signal behavior - SIGCONT */
434         act.sa_handler = catch_sigcont;
435         if (sigaction(SIGCONT, &act, NULL) < 0) {
436                 perror("sigaction(SIGCONT)");
437                 exit(EXIT_FAILURE);
438         }
440         /* setup signal behaviour - SIGHUP*/
442         act.sa_handler = catch_sigint;
443         if (sigaction(SIGHUP, &act, NULL) < 0) {
444                 perror("sigaction(SIGHUP)");
445                 exit(EXIT_FAILURE);
446         }
448         /* setup SIGWINCH */
450         act.sa_flags = SA_RESTART;
451         act.sa_handler = catch_sigwinch;
452         if (sigaction(SIGWINCH, &act, NULL) < 0) {
453                 perror("sigaction(SIGWINCH)");
454                 exit(EXIT_FAILURE);
455         }
457         /* ignore SIGPIPE */
459         act.sa_handler = SIG_IGN;
460         if (sigaction(SIGPIPE, &act, NULL) < 0) {
461                 perror("sigaction(SIGPIPE)");
462                 exit(EXIT_FAILURE);
463         }
465         ncu_init();
467 #ifdef ENABLE_LYRICS_SCREEN
468         lyrics_init();
469 #endif
471         /* create mpdclient instance */
472         mpd = mpdclient_new();
473         mpdclient_install_error_callback(mpd, error_callback);
475         /* initialize curses */
476         screen_init(mpd);
478         /* the main loop */
479         main_loop = g_main_loop_new(NULL, FALSE);
481         /* watch out for keyboard input */
482         keyboard_channel = g_io_channel_unix_new(STDIN_FILENO);
483         g_io_add_watch(keyboard_channel, G_IO_IN, keyboard_event, NULL);
485 #ifdef ENABLE_LIRC
486         /* watch out for lirc input */
487         lirc_socket = ncmpc_lirc_open();
488         if (lirc_socket >= 0) {
489                 lirc_channel = g_io_channel_unix_new(lirc_socket);
490                 g_io_add_watch(lirc_channel, G_IO_IN, lirc_event, NULL);
491         }
492 #endif
494         /* attempt to connect */
495         reconnect_source_id = g_timeout_add(1, timer_reconnect, NULL);
497         update_source_id = g_timeout_add(update_interval,
498                                          timer_mpd_update,
499                                          GINT_TO_POINTER(TRUE));
500 #ifndef NCMPC_MINI
501         g_timeout_add(10000, timer_check_key_bindings, NULL);
502 #endif
503         idle_source_id = g_timeout_add(idle_interval, timer_idle, NULL);
505         screen_paint(mpd);
507         g_main_loop_run(main_loop);
509         /* cleanup */
511         g_main_loop_unref(main_loop);
512         g_io_channel_unref(keyboard_channel);
514 #ifdef ENABLE_LIRC
515         if (lirc_socket >= 0)
516                 g_io_channel_unref(lirc_channel);
517         ncmpc_lirc_close();
518 #endif
520         exit_and_cleanup();
522 #ifdef ENABLE_LYRICS_SCREEN
523         lyrics_deinit();
524 #endif
526         ncu_deinit();
528         return 0;