Code

main: assign reconnect_source_id when retrying to reconnect
[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);
245         else if (reconnect_source_id == 0)
246                 reconnect_source_id = g_timeout_add(1000, timer_reconnect,
247                                                     NULL);
249 #ifndef NCMPC_MINI
250         if (options.enable_xterm_title)
251                 update_xterm_title();
252 #endif
254         screen_update(mpd);
256         mpd->events = 0;
258         return GPOINTER_TO_INT(data);
261 void begin_input_event(void)
265 void end_input_event(void)
267         screen_update(mpd);
270 int do_input_event(command_t cmd)
272         if (cmd == CMD_QUIT) {
273                 g_main_loop_quit(main_loop);
274                 return -1;
275         }
277         screen_cmd(mpd, cmd);
279         if (cmd == CMD_VOLUME_UP || cmd == CMD_VOLUME_DOWN) {
280                 /* make sure we don't update the volume yet */
281                 disable_update_timer();
282                 enable_update_timer();
283         }
285         return 0;
288 static gboolean
289 keyboard_event(G_GNUC_UNUSED GIOChannel *source,
290                G_GNUC_UNUSED GIOCondition condition,
291                G_GNUC_UNUSED gpointer data)
293         command_t cmd;
295         begin_input_event();
297         if ((cmd=get_keyboard_command()) != CMD_NONE)
298                 if (do_input_event(cmd) != 0)
299                         return FALSE;
301         end_input_event();
302         return TRUE;
305 #ifndef NCMPC_MINI
306 /**
307  * Check the configured key bindings for errors, and display a status
308  * message every 10 seconds.
309  */
310 static gboolean
311 timer_check_key_bindings(G_GNUC_UNUSED gpointer data)
313         char buf[256];
314 #ifdef ENABLE_KEYDEF_SCREEN
315         char comment[64];
316 #endif
317         gboolean key_error;
319         key_error = check_key_bindings(NULL, buf, sizeof(buf));
320         if (!key_error) {
321                 /* no error: disable this timer for the rest of this
322                    process */
323                 check_key_bindings_source_id = 0;
324                 return FALSE;
325         }
327 #ifdef ENABLE_KEYDEF_SCREEN
328         g_strchomp(buf);
329         g_strlcat(buf, " (", sizeof(buf));
330         /* to translators: a key was bound twice in the key editor,
331            and this is a hint for the user what to press to correct
332            that */
333         g_snprintf(comment, sizeof(comment), _("press %s for the key editor"),
334                    get_key_names(CMD_SCREEN_KEYDEF, 0));
335         g_strlcat(buf, comment, sizeof(buf));
336         g_strlcat(buf, ")", sizeof(buf));
337 #endif
339         screen_status_printf("%s", buf);
341         doupdate();
342         return TRUE;
344 #endif
346 int
347 main(int argc, const char *argv[])
349         struct sigaction act;
350 #ifdef ENABLE_LOCALE
351         const char *charset = NULL;
352 #endif
353         GIOChannel *keyboard_channel;
354 #ifdef ENABLE_LIRC
355         int lirc_socket;
356         GIOChannel *lirc_channel = NULL;
357 #endif
359 #ifdef ENABLE_LOCALE
360         /* time and date formatting */
361         setlocale(LC_TIME,"");
362         /* care about sorting order etc */
363         setlocale(LC_COLLATE,"");
364         /* charset */
365         setlocale(LC_CTYPE,"");
366         /* initialize charset conversions */
367         charset = charset_init();
369         /* initialize i18n support */
370 #endif
372 #ifdef ENABLE_NLS
373         setlocale(LC_MESSAGES, "");
374         bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
375 #ifdef ENABLE_LOCALE
376         bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
377 #endif
378         textdomain(GETTEXT_PACKAGE);
379 #endif
381         /* initialize options */
382         options_init();
384         /* parse command line options - 1 pass get configuration files */
385         options_parse(argc, argv);
387 #ifndef NCMPC_MINI
388         /* read configuration */
389         read_configuration();
391         /* check key bindings */
392         check_key_bindings(NULL, NULL, 0);
393 #endif
395         /* parse command line options - 2 pass */
396         options_parse(argc, argv);
398         /* setup signal behavior - SIGINT */
399         sigemptyset(&act.sa_mask);
400         act.sa_flags = 0;
401         act.sa_handler = catch_sigint;
402         if (sigaction(SIGINT, &act, NULL) < 0) {
403                 perror("signal");
404                 exit(EXIT_FAILURE);
405         }
407         /* setup signal behavior - SIGTERM */
409         act.sa_handler = catch_sigint;
410         if (sigaction(SIGTERM, &act, NULL) < 0) {
411                 perror("sigaction()");
412                 exit(EXIT_FAILURE);
413         }
415         /* setup signal behavior - SIGCONT */
417         act.sa_handler = catch_sigcont;
418         if (sigaction(SIGCONT, &act, NULL) < 0) {
419                 perror("sigaction(SIGCONT)");
420                 exit(EXIT_FAILURE);
421         }
423         /* setup signal behaviour - SIGHUP*/
425         act.sa_handler = catch_sigint;
426         if (sigaction(SIGHUP, &act, NULL) < 0) {
427                 perror("sigaction(SIGHUP)");
428                 exit(EXIT_FAILURE);
429         }
431         /* setup SIGWINCH */
433         act.sa_flags = SA_RESTART;
434         act.sa_handler = catch_sigwinch;
435         if (sigaction(SIGWINCH, &act, NULL) < 0) {
436                 perror("sigaction(SIGWINCH)");
437                 exit(EXIT_FAILURE);
438         }
440         /* ignore SIGPIPE */
442         act.sa_handler = SIG_IGN;
443         if (sigaction(SIGPIPE, &act, NULL) < 0) {
444                 perror("sigaction(SIGPIPE)");
445                 exit(EXIT_FAILURE);
446         }
448         ncu_init();
450 #ifdef ENABLE_LYRICS_SCREEN
451         lyrics_init();
452 #endif
454         /* create mpdclient instance */
455         mpd = mpdclient_new();
457         /* initialize curses */
458         screen_init(mpd);
460         /* the main loop */
461         main_loop = g_main_loop_new(NULL, FALSE);
463         /* watch out for keyboard input */
464         keyboard_channel = g_io_channel_unix_new(STDIN_FILENO);
465         g_io_add_watch(keyboard_channel, G_IO_IN, keyboard_event, NULL);
467 #ifdef ENABLE_LIRC
468         /* watch out for lirc input */
469         lirc_socket = ncmpc_lirc_open();
470         if (lirc_socket >= 0) {
471                 lirc_channel = g_io_channel_unix_new(lirc_socket);
472                 g_io_add_watch(lirc_channel, G_IO_IN, lirc_event, NULL);
473         }
474 #endif
476         /* attempt to connect */
477         reconnect_source_id = g_timeout_add(1, timer_reconnect, NULL);
479         enable_update_timer();
481 #ifndef NCMPC_MINI
482         check_key_bindings_source_id = g_timeout_add(10000, timer_check_key_bindings, NULL);
483 #endif
485         screen_paint(mpd);
487         g_main_loop_run(main_loop);
489         /* cleanup */
491         cancel_seek_timer();
493         disable_update_timer();
495         if (reconnect_source_id != 0)
496                 g_source_remove(reconnect_source_id);
498 #ifndef NCMPC_MINI
499         if (check_key_bindings_source_id != 0)
500                 g_source_remove(check_key_bindings_source_id);
501 #endif
503         g_main_loop_unref(main_loop);
504         g_io_channel_unref(keyboard_channel);
506 #ifdef ENABLE_LIRC
507         if (lirc_socket >= 0)
508                 g_io_channel_unref(lirc_channel);
509         ncmpc_lirc_close();
510 #endif
512         exit_and_cleanup();
514 #ifdef ENABLE_LYRICS_SCREEN
515         lyrics_deinit();
516 #endif
518         ncu_deinit();
519         options_deinit();
521         return 0;