Code

main: reconnect at the end of timer_mpd_update()
[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 "screen_message.h"
30 #include "strfsong.h"
31 #include "i18n.h"
32 #include "player_command.h"
34 #ifndef NCMPC_MINI
35 #include "conf.h"
36 #endif
38 #ifdef ENABLE_LYRICS_SCREEN
39 #include "lyrics.h"
40 #endif
42 #ifdef ENABLE_LIRC
43 #include "lirc.h"
44 #endif
46 #include <mpd/client.h>
48 #include <stdlib.h>
49 #include <unistd.h>
50 #include <signal.h>
51 #include <string.h>
53 #ifdef ENABLE_LOCALE
54 #include <locale.h>
55 #endif
57 /* time between mpd updates [s] */
58 static const guint update_interval = 500;
60 #define BUFSIZE 1024
62 static struct mpdclient *mpd = NULL;
63 static GMainLoop *main_loop;
64 static guint reconnect_source_id, update_source_id;
66 #ifndef NCMPC_MINI
67 static guint check_key_bindings_source_id;
68 #endif
70 #ifndef NCMPC_MINI
71 static void
72 update_xterm_title(void)
73 {
74         static char title[BUFSIZE];
75         char tmp[BUFSIZE];
76         struct mpd_status *status = NULL;
77         const struct mpd_song *song = NULL;
79         if (mpd) {
80                 status = mpd->status;
81                 song = mpd->song;
82         }
84         if (options.xterm_title_format && status && song &&
85             mpd_status_get_state(status) == MPD_STATE_PLAY)
86                 strfsong(tmp, BUFSIZE, options.xterm_title_format, song);
87         else
88                 g_strlcpy(tmp, PACKAGE " version " VERSION, BUFSIZE);
90         if (strncmp(title, tmp, BUFSIZE)) {
91                 g_strlcpy(title, tmp, BUFSIZE);
92                 set_xterm_title("%s", title);
93         }
94 }
95 #endif
97 static void
98 exit_and_cleanup(void)
99 {
100         screen_exit();
101 #ifndef NCMPC_MINI
102         set_xterm_title("");
103 #endif
104         printf("\n");
106         if (mpd) {
107                 mpdclient_disconnect(mpd);
108                 mpdclient_free(mpd);
109         }
112 static void
113 catch_sigint(G_GNUC_UNUSED int sig)
115         g_main_loop_quit(main_loop);
119 static void
120 catch_sigcont(G_GNUC_UNUSED int sig)
122         screen_resize(mpd);
125 void
126 sigstop(void)
128   def_prog_mode();  /* save the tty modes */
129   endwin();         /* end curses mode temporarily */
130   kill(0, SIGSTOP); /* issue SIGSTOP */
133 static guint timer_sigwinch_id;
135 static gboolean
136 timer_sigwinch(G_GNUC_UNUSED gpointer data)
138         /* the following causes the screen to flicker.  There might be
139            better solutions, but I believe it isn't all that
140            important. */
142         endwin();
143         refresh();
144         screen_resize(mpd);
146         return FALSE;
149 static void
150 catch_sigwinch(G_GNUC_UNUSED int sig)
152         if (timer_sigwinch_id != 0)
153                 g_source_remove(timer_sigwinch_id);
155         timer_sigwinch_id = g_timeout_add(100, timer_sigwinch, NULL);
158 static gboolean
159 timer_mpd_update(gpointer data);
161 static void
162 enable_update_timer(void)
164         if (update_source_id != 0)
165                 return;
167         update_source_id = g_timeout_add(update_interval,
168                                          timer_mpd_update,
169                                          GINT_TO_POINTER(TRUE));
172 static void
173 disable_update_timer(void)
175         if (update_source_id == 0)
176                 return;
178         g_source_remove(update_source_id);
179         update_source_id = 0;
182 /**
183  * This timer is installed when the connection to the MPD server is
184  * broken.  It tries to recover by reconnecting periodically.
185  */
186 static gboolean
187 timer_reconnect(G_GNUC_UNUSED gpointer data)
189         bool success;
191         assert(!mpdclient_is_connected(mpd));
193         reconnect_source_id = 0;
195         screen_status_printf(_("Connecting to %s...  [Press %s to abort]"),
196                              options.host, get_key_names(CMD_QUIT,0) );
197         doupdate();
199         mpdclient_disconnect(mpd);
200         success = mpdclient_connect(mpd,
201                                     options.host, options.port,
202                                     1.5,
203                                     options.password);
204         if (!success) {
205                 /* try again in 5 seconds */
206                 reconnect_source_id = g_timeout_add(5000,
207                                                     timer_reconnect, NULL);
208                 return FALSE;
209         }
211 #ifndef NCMPC_MINI
212         /* quit if mpd is pre 0.11.0 - song id not supported by mpd */
213         if (mpd_connection_cmp_server_version(mpd->connection, 0, 12, 0) < 0) {
214                 const unsigned *version =
215                         mpd_connection_get_server_version(mpd->connection);
216                 screen_status_printf(_("Error: MPD version %d.%d.%d is to old (%s needed)"),
217                                      version[0], version[1], version[2],
218                                      "0.12.0");
219                 mpdclient_disconnect(mpd);
220                 doupdate();
222                 /* try again after 30 seconds */
223                 reconnect_source_id = g_timeout_add(30000,
224                                                     timer_reconnect, NULL);
225                 return FALSE;
226         }
227 #endif
229         screen_status_printf(_("Connected to %s"),
230                              options.host != NULL
231                              ? options.host : "localhost");
232         doupdate();
234         /* update immediately */
235         g_timeout_add(1, timer_mpd_update, GINT_TO_POINTER(FALSE));
237         return FALSE;
240 static gboolean
241 timer_mpd_update(gpointer data)
243         if (mpdclient_is_connected(mpd))
244                 mpdclient_update(mpd);
246 #ifndef NCMPC_MINI
247         if (options.enable_xterm_title)
248                 update_xterm_title();
249 #endif
251         screen_update(mpd);
252         mpd->events = 0;
254         if (!mpdclient_is_connected(mpd) && reconnect_source_id == 0)
255                 /* reconnect when the connection is lost */
256                 reconnect_source_id = g_timeout_add(1000, timer_reconnect,
257                                                     NULL);
259         return GPOINTER_TO_INT(data);
262 void begin_input_event(void)
266 void end_input_event(void)
268         screen_update(mpd);
269         mpd->events = 0;
272 int do_input_event(command_t cmd)
274         if (cmd == CMD_QUIT) {
275                 g_main_loop_quit(main_loop);
276                 return -1;
277         }
279         screen_cmd(mpd, cmd);
281         if (cmd == CMD_VOLUME_UP || cmd == CMD_VOLUME_DOWN) {
282                 /* make sure we don't update the volume yet */
283                 disable_update_timer();
284                 enable_update_timer();
285         }
287         return 0;
290 static gboolean
291 keyboard_event(G_GNUC_UNUSED GIOChannel *source,
292                G_GNUC_UNUSED GIOCondition condition,
293                G_GNUC_UNUSED gpointer data)
295         command_t cmd;
297         begin_input_event();
299         if ((cmd=get_keyboard_command()) != CMD_NONE)
300                 if (do_input_event(cmd) != 0)
301                         return FALSE;
303         end_input_event();
304         return TRUE;
307 #ifndef NCMPC_MINI
308 /**
309  * Check the configured key bindings for errors, and display a status
310  * message every 10 seconds.
311  */
312 static gboolean
313 timer_check_key_bindings(G_GNUC_UNUSED gpointer data)
315         char buf[256];
316 #ifdef ENABLE_KEYDEF_SCREEN
317         char comment[64];
318 #endif
319         gboolean key_error;
321         key_error = check_key_bindings(NULL, buf, sizeof(buf));
322         if (!key_error) {
323                 /* no error: disable this timer for the rest of this
324                    process */
325                 check_key_bindings_source_id = 0;
326                 return FALSE;
327         }
329 #ifdef ENABLE_KEYDEF_SCREEN
330         g_strchomp(buf);
331         g_strlcat(buf, " (", sizeof(buf));
332         /* to translators: a key was bound twice in the key editor,
333            and this is a hint for the user what to press to correct
334            that */
335         g_snprintf(comment, sizeof(comment), _("press %s for the key editor"),
336                    get_key_names(CMD_SCREEN_KEYDEF, 0));
337         g_strlcat(buf, comment, sizeof(buf));
338         g_strlcat(buf, ")", sizeof(buf));
339 #endif
341         screen_status_printf("%s", buf);
343         doupdate();
344         return TRUE;
346 #endif
348 int
349 main(int argc, const char *argv[])
351         struct sigaction act;
352 #ifdef ENABLE_LOCALE
353         const char *charset = NULL;
354 #endif
355         GIOChannel *keyboard_channel;
356 #ifdef ENABLE_LIRC
357         int lirc_socket;
358         GIOChannel *lirc_channel = NULL;
359 #endif
361 #ifdef ENABLE_LOCALE
362         /* time and date formatting */
363         setlocale(LC_TIME,"");
364         /* care about sorting order etc */
365         setlocale(LC_COLLATE,"");
366         /* charset */
367         setlocale(LC_CTYPE,"");
368         /* initialize charset conversions */
369         charset = charset_init();
371         /* initialize i18n support */
372 #endif
374 #ifdef ENABLE_NLS
375         setlocale(LC_MESSAGES, "");
376         bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
377 #ifdef ENABLE_LOCALE
378         bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
379 #endif
380         textdomain(GETTEXT_PACKAGE);
381 #endif
383         /* initialize options */
384         options_init();
386         /* parse command line options - 1 pass get configuration files */
387         options_parse(argc, argv);
389 #ifndef NCMPC_MINI
390         /* read configuration */
391         read_configuration();
393         /* check key bindings */
394         check_key_bindings(NULL, NULL, 0);
395 #endif
397         /* parse command line options - 2 pass */
398         options_parse(argc, argv);
400         /* setup signal behavior - SIGINT */
401         sigemptyset(&act.sa_mask);
402         act.sa_flags = 0;
403         act.sa_handler = catch_sigint;
404         if (sigaction(SIGINT, &act, NULL) < 0) {
405                 perror("signal");
406                 exit(EXIT_FAILURE);
407         }
409         /* setup signal behavior - SIGTERM */
411         act.sa_handler = catch_sigint;
412         if (sigaction(SIGTERM, &act, NULL) < 0) {
413                 perror("sigaction()");
414                 exit(EXIT_FAILURE);
415         }
417         /* setup signal behavior - SIGCONT */
419         act.sa_handler = catch_sigcont;
420         if (sigaction(SIGCONT, &act, NULL) < 0) {
421                 perror("sigaction(SIGCONT)");
422                 exit(EXIT_FAILURE);
423         }
425         /* setup signal behaviour - SIGHUP*/
427         act.sa_handler = catch_sigint;
428         if (sigaction(SIGHUP, &act, NULL) < 0) {
429                 perror("sigaction(SIGHUP)");
430                 exit(EXIT_FAILURE);
431         }
433         /* setup SIGWINCH */
435         act.sa_flags = SA_RESTART;
436         act.sa_handler = catch_sigwinch;
437         if (sigaction(SIGWINCH, &act, NULL) < 0) {
438                 perror("sigaction(SIGWINCH)");
439                 exit(EXIT_FAILURE);
440         }
442         /* ignore SIGPIPE */
444         act.sa_handler = SIG_IGN;
445         if (sigaction(SIGPIPE, &act, NULL) < 0) {
446                 perror("sigaction(SIGPIPE)");
447                 exit(EXIT_FAILURE);
448         }
450         ncu_init();
452 #ifdef ENABLE_LYRICS_SCREEN
453         lyrics_init();
454 #endif
456         /* create mpdclient instance */
457         mpd = mpdclient_new();
459         /* initialize curses */
460         screen_init(mpd);
462         /* the main loop */
463         main_loop = g_main_loop_new(NULL, FALSE);
465         /* watch out for keyboard input */
466         keyboard_channel = g_io_channel_unix_new(STDIN_FILENO);
467         g_io_add_watch(keyboard_channel, G_IO_IN, keyboard_event, NULL);
469 #ifdef ENABLE_LIRC
470         /* watch out for lirc input */
471         lirc_socket = ncmpc_lirc_open();
472         if (lirc_socket >= 0) {
473                 lirc_channel = g_io_channel_unix_new(lirc_socket);
474                 g_io_add_watch(lirc_channel, G_IO_IN, lirc_event, NULL);
475         }
476 #endif
478         /* attempt to connect */
479         reconnect_source_id = g_timeout_add(1, timer_reconnect, NULL);
481         enable_update_timer();
483 #ifndef NCMPC_MINI
484         check_key_bindings_source_id = g_timeout_add(10000, timer_check_key_bindings, NULL);
485 #endif
487         screen_paint(mpd);
489         g_main_loop_run(main_loop);
491         /* cleanup */
493         cancel_seek_timer();
495         disable_update_timer();
497         if (reconnect_source_id != 0)
498                 g_source_remove(reconnect_source_id);
500 #ifndef NCMPC_MINI
501         if (check_key_bindings_source_id != 0)
502                 g_source_remove(check_key_bindings_source_id);
503 #endif
505         g_main_loop_unref(main_loop);
506         g_io_channel_unref(keyboard_channel);
508 #ifdef ENABLE_LIRC
509         if (lirc_socket >= 0)
510                 g_io_channel_unref(lirc_channel);
511         ncmpc_lirc_close();
512 #endif
514         exit_and_cleanup();
516 #ifdef ENABLE_LYRICS_SCREEN
517         lyrics_deinit();
518 #endif
520         ncu_deinit();
521         options_deinit();
523         return 0;