Code

options: added options_deinit()
[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         }
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         if (connected)
205                 return FALSE;
207         screen_status_printf(_("Connecting to %s...  [Press %s to abort]"),
208                              options.host, get_key_names(CMD_QUIT,0) );
209         doupdate();
211         mpdclient_disconnect(mpd);
212         ret = mpdclient_connect(mpd,
213                                 options.host, options.port,
214                                 1.5,
215                                 options.password);
216         if (ret != 0) {
217                 /* try again in 5 seconds */
218                 g_timeout_add(5000, timer_reconnect, NULL);
219                 return FALSE;
220         }
222 #ifndef NCMPC_MINI
223         /* quit if mpd is pre 0.11.0 - song id not supported by mpd */
224         if (MPD_VERSION_LT(mpd, 0, 11, 0)) {
225                 screen_status_printf(_("Error: MPD version %d.%d.%d is to old (%s needed)"),
226                                      mpd->connection->version[0],
227                                      mpd->connection->version[1],
228                                      mpd->connection->version[2],
229                                      "0.11.0");
230                 mpdclient_disconnect(mpd);
231                 doupdate();
233                 /* try again after 30 seconds */
234                 g_timeout_add(30000, timer_reconnect, NULL);
235                 return FALSE;
236         }
237 #endif
239         screen_status_printf(_("Connected to %s"), options.host);
240         doupdate();
242         connected = TRUE;
244         /* update immediately */
245         g_timeout_add(1, timer_mpd_update, GINT_TO_POINTER(FALSE));
247         reconnect_source_id = 0;
248         return FALSE;
252 static gboolean
253 timer_mpd_update(gpointer data)
255         if (connected)
256                 mpdclient_update(mpd);
257         else if (reconnect_source_id == 0)
258                 reconnect_source_id = g_timeout_add(1000, timer_reconnect,
259                                                     NULL);
261 #ifndef NCMPC_MINI
262         if (options.enable_xterm_title)
263                 update_xterm_title();
264 #endif
266         screen_update(mpd);
268         return GPOINTER_TO_INT(data);
271 /**
272  * This idle timer is invoked when the user hasn't typed a key for
273  * 500ms.  It is used for delayed seeking.
274  */
275 static gboolean
276 timer_idle(G_GNUC_UNUSED gpointer data)
278         screen_idle(mpd);
279         return TRUE;
282 void begin_input_event(void)
284         /* remove the idle timeout; add it later with fresh interval */
285         g_source_remove(idle_source_id);
288 void end_input_event(void)
290         screen_update(mpd);
292         idle_source_id = g_timeout_add(idle_interval, timer_idle, NULL);
295 int do_input_event(command_t cmd)
297         if (cmd == CMD_QUIT) {
298                 g_main_loop_quit(main_loop);
299                 return -1;
300         }
302         screen_cmd(mpd, cmd);
304         if (cmd == CMD_VOLUME_UP || cmd == CMD_VOLUME_DOWN) {
305                 /* make sure we don't update the volume yet */
306                 g_source_remove(update_source_id);
307                 update_source_id = g_timeout_add(update_interval,
308                                                  timer_mpd_update,
309                                                  GINT_TO_POINTER(TRUE));
310         }
312         return 0;
315 static gboolean
316 keyboard_event(G_GNUC_UNUSED GIOChannel *source,
317                G_GNUC_UNUSED GIOCondition condition,
318                G_GNUC_UNUSED gpointer data)
320         command_t cmd;
322         begin_input_event();
324         if ((cmd=get_keyboard_command()) != CMD_NONE)
325                 if (do_input_event(cmd) != 0)
326                         return FALSE;
328         end_input_event();
329         return TRUE;
332 #ifndef NCMPC_MINI
333 /**
334  * Check the configured key bindings for errors, and display a status
335  * message every 10 seconds.
336  */
337 static gboolean
338 timer_check_key_bindings(G_GNUC_UNUSED gpointer data)
340         char buf[256];
341 #ifdef ENABLE_KEYDEF_SCREEN
342         char comment[64];
343 #endif
344         gboolean key_error;
346         key_error = check_key_bindings(NULL, buf, sizeof(buf));
347         if (!key_error)
348                 /* no error: disable this timer for the rest of this
349                    process */
350                 return FALSE;
352 #ifdef ENABLE_KEYDEF_SCREEN
353         g_strchomp(buf);
354         g_strlcat(buf, " (", sizeof(buf));
355         /* to translators: a key was bound twice in the key editor,
356            and this is a hint for the user what to press to correct
357            that */
358         g_snprintf(comment, sizeof(comment), _("press %s for the key editor"),
359                    get_key_names(CMD_SCREEN_KEYDEF, 0));
360         g_strlcat(buf, comment, sizeof(buf));
361         g_strlcat(buf, ")", sizeof(buf));
362 #endif
364         screen_status_printf("%s", buf);
366         doupdate();
367         return TRUE;
369 #endif
371 int
372 main(int argc, const char *argv[])
374         struct sigaction act;
375 #ifdef ENABLE_LOCALE
376         const char *charset = NULL;
377 #endif
378         GIOChannel *keyboard_channel;
379 #ifdef ENABLE_LIRC
380         int lirc_socket;
381         GIOChannel *lirc_channel = NULL;
382 #endif
384 #ifdef ENABLE_LOCALE
385         /* time and date formatting */
386         setlocale(LC_TIME,"");
387         /* care about sorting order etc */
388         setlocale(LC_COLLATE,"");
389         /* charset */
390         setlocale(LC_CTYPE,"");
391         /* initialize charset conversions */
392         charset = charset_init();
394         /* initialize i18n support */
395 #endif
397 #ifdef ENABLE_NLS
398         setlocale(LC_MESSAGES, "");
399         bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
400 #ifdef ENABLE_LOCALE
401         bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
402 #endif
403         textdomain(GETTEXT_PACKAGE);
404 #endif
406         /* initialize options */
407         options_init();
409         /* parse command line options - 1 pass get configuration files */
410         options_parse(argc, argv);
412 #ifndef NCMPC_MINI
413         /* read configuration */
414         read_configuration();
416         /* check key bindings */
417         check_key_bindings(NULL, NULL, 0);
418 #endif
420         /* parse command line options - 2 pass */
421         options_parse(argc, argv);
423         /* setup signal behavior - SIGINT */
424         sigemptyset(&act.sa_mask);
425         act.sa_flags = 0;
426         act.sa_handler = catch_sigint;
427         if (sigaction(SIGINT, &act, NULL) < 0) {
428                 perror("signal");
429                 exit(EXIT_FAILURE);
430         }
432         /* setup signal behavior - SIGTERM */
434         act.sa_handler = catch_sigint;
435         if (sigaction(SIGTERM, &act, NULL) < 0) {
436                 perror("sigaction()");
437                 exit(EXIT_FAILURE);
438         }
440         /* setup signal behavior - SIGCONT */
442         act.sa_handler = catch_sigcont;
443         if (sigaction(SIGCONT, &act, NULL) < 0) {
444                 perror("sigaction(SIGCONT)");
445                 exit(EXIT_FAILURE);
446         }
448         /* setup signal behaviour - SIGHUP*/
450         act.sa_handler = catch_sigint;
451         if (sigaction(SIGHUP, &act, NULL) < 0) {
452                 perror("sigaction(SIGHUP)");
453                 exit(EXIT_FAILURE);
454         }
456         /* setup SIGWINCH */
458         act.sa_flags = SA_RESTART;
459         act.sa_handler = catch_sigwinch;
460         if (sigaction(SIGWINCH, &act, NULL) < 0) {
461                 perror("sigaction(SIGWINCH)");
462                 exit(EXIT_FAILURE);
463         }
465         /* ignore SIGPIPE */
467         act.sa_handler = SIG_IGN;
468         if (sigaction(SIGPIPE, &act, NULL) < 0) {
469                 perror("sigaction(SIGPIPE)");
470                 exit(EXIT_FAILURE);
471         }
473         ncu_init();
475 #ifdef ENABLE_LYRICS_SCREEN
476         lyrics_init();
477 #endif
479         /* create mpdclient instance */
480         mpd = mpdclient_new();
481         mpdclient_install_error_callback(mpd, error_callback);
483         /* initialize curses */
484         screen_init(mpd);
486         /* the main loop */
487         main_loop = g_main_loop_new(NULL, FALSE);
489         /* watch out for keyboard input */
490         keyboard_channel = g_io_channel_unix_new(STDIN_FILENO);
491         g_io_add_watch(keyboard_channel, G_IO_IN, keyboard_event, NULL);
493 #ifdef ENABLE_LIRC
494         /* watch out for lirc input */
495         lirc_socket = ncmpc_lirc_open();
496         if (lirc_socket >= 0) {
497                 lirc_channel = g_io_channel_unix_new(lirc_socket);
498                 g_io_add_watch(lirc_channel, G_IO_IN, lirc_event, NULL);
499         }
500 #endif
502         /* attempt to connect */
503         reconnect_source_id = g_timeout_add(1, timer_reconnect, NULL);
505         update_source_id = g_timeout_add(update_interval,
506                                          timer_mpd_update,
507                                          GINT_TO_POINTER(TRUE));
508 #ifndef NCMPC_MINI
509         g_timeout_add(10000, timer_check_key_bindings, NULL);
510 #endif
511         idle_source_id = g_timeout_add(idle_interval, timer_idle, NULL);
513         screen_paint(mpd);
515         g_main_loop_run(main_loop);
517         /* cleanup */
519         g_main_loop_unref(main_loop);
520         g_io_channel_unref(keyboard_channel);
522 #ifdef ENABLE_LIRC
523         if (lirc_socket >= 0)
524                 g_io_channel_unref(lirc_channel);
525         ncmpc_lirc_close();
526 #endif
528         exit_and_cleanup();
530 #ifdef ENABLE_LYRICS_SCREEN
531         lyrics_deinit();
532 #endif
534         ncu_deinit();
535         options_deinit();
537         return 0;