Code

main: enable the update timer on demand
[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, NULL);
171 static void
172 disable_update_timer(void)
174         if (update_source_id == 0)
175                 return;
177         g_source_remove(update_source_id);
178         update_source_id = 0;
181 static bool
182 should_enable_update_timer(void)
184         return mpdclient_is_connected(mpd)
185 #ifndef NCMPC_MINI
186                 || options.display_time
187 #endif
188                 ;
191 static void
192 auto_update_timer(void)
194         if (should_enable_update_timer())
195                 enable_update_timer();
196         else
197                 disable_update_timer();
200 static void
201 check_reconnect(void);
203 static void
204 do_mpd_update(void)
206         if (mpdclient_is_connected(mpd))
207                 mpdclient_update(mpd);
209 #ifndef NCMPC_MINI
210         if (options.enable_xterm_title)
211                 update_xterm_title();
212 #endif
214         screen_update(mpd);
215         mpd->events = 0;
217         check_reconnect();
220 /**
221  * This timer is installed when the connection to the MPD server is
222  * broken.  It tries to recover by reconnecting periodically.
223  */
224 static gboolean
225 timer_reconnect(G_GNUC_UNUSED gpointer data)
227         bool success;
229         assert(!mpdclient_is_connected(mpd));
231         reconnect_source_id = 0;
233         screen_status_printf(_("Connecting to %s...  [Press %s to abort]"),
234                              options.host, get_key_names(CMD_QUIT,0) );
235         doupdate();
237         mpdclient_disconnect(mpd);
238         success = mpdclient_connect(mpd,
239                                     options.host, options.port,
240                                     1.5,
241                                     options.password);
242         if (!success) {
243                 /* try again in 5 seconds */
244                 reconnect_source_id = g_timeout_add(5000,
245                                                     timer_reconnect, NULL);
246                 return FALSE;
247         }
249 #ifndef NCMPC_MINI
250         /* quit if mpd is pre 0.11.0 - song id not supported by mpd */
251         if (mpd_connection_cmp_server_version(mpd->connection, 0, 12, 0) < 0) {
252                 const unsigned *version =
253                         mpd_connection_get_server_version(mpd->connection);
254                 screen_status_printf(_("Error: MPD version %d.%d.%d is to old (%s needed)"),
255                                      version[0], version[1], version[2],
256                                      "0.12.0");
257                 mpdclient_disconnect(mpd);
258                 doupdate();
260                 /* try again after 30 seconds */
261                 reconnect_source_id = g_timeout_add(30000,
262                                                     timer_reconnect, NULL);
263                 return FALSE;
264         }
265 #endif
267         screen_status_printf(_("Connected to %s"),
268                              options.host != NULL
269                              ? options.host : "localhost");
270         doupdate();
272         /* update immediately */
273         mpd->events = MPD_IDLE_DATABASE|MPD_IDLE_STORED_PLAYLIST|
274                 MPD_IDLE_QUEUE|MPD_IDLE_PLAYER|MPD_IDLE_MIXER|MPD_IDLE_OUTPUT|
275                 MPD_IDLE_OPTIONS|MPD_IDLE_UPDATE;
277         do_mpd_update();
279         auto_update_timer();
281         return FALSE;
284 static void
285 check_reconnect(void)
287         if (!mpdclient_is_connected(mpd) && reconnect_source_id == 0)
288                 /* reconnect when the connection is lost */
289                 reconnect_source_id = g_timeout_add(1000, timer_reconnect,
290                                                     NULL);
293 static gboolean
294 timer_mpd_update(G_GNUC_UNUSED gpointer data)
296         do_mpd_update();
298         if (should_enable_update_timer())
299                 return true;
300         else {
301                 update_source_id = 0;
302                 return false;
303         }
306 void begin_input_event(void)
310 void end_input_event(void)
312         screen_update(mpd);
313         mpd->events = 0;
315         check_reconnect();
316         auto_update_timer();
319 int do_input_event(command_t cmd)
321         if (cmd == CMD_QUIT) {
322                 g_main_loop_quit(main_loop);
323                 return -1;
324         }
326         screen_cmd(mpd, cmd);
328         if (cmd == CMD_VOLUME_UP || cmd == CMD_VOLUME_DOWN)
329                 /* make sure we don't update the volume yet */
330                 disable_update_timer();
332         return 0;
335 static gboolean
336 keyboard_event(G_GNUC_UNUSED GIOChannel *source,
337                G_GNUC_UNUSED GIOCondition condition,
338                G_GNUC_UNUSED gpointer data)
340         command_t cmd;
342         begin_input_event();
344         if ((cmd=get_keyboard_command()) != CMD_NONE)
345                 if (do_input_event(cmd) != 0)
346                         return FALSE;
348         end_input_event();
349         return TRUE;
352 #ifndef NCMPC_MINI
353 /**
354  * Check the configured key bindings for errors, and display a status
355  * message every 10 seconds.
356  */
357 static gboolean
358 timer_check_key_bindings(G_GNUC_UNUSED gpointer data)
360         char buf[256];
361 #ifdef ENABLE_KEYDEF_SCREEN
362         char comment[64];
363 #endif
364         gboolean key_error;
366         key_error = check_key_bindings(NULL, buf, sizeof(buf));
367         if (!key_error) {
368                 /* no error: disable this timer for the rest of this
369                    process */
370                 check_key_bindings_source_id = 0;
371                 return FALSE;
372         }
374 #ifdef ENABLE_KEYDEF_SCREEN
375         g_strchomp(buf);
376         g_strlcat(buf, " (", sizeof(buf));
377         /* to translators: a key was bound twice in the key editor,
378            and this is a hint for the user what to press to correct
379            that */
380         g_snprintf(comment, sizeof(comment), _("press %s for the key editor"),
381                    get_key_names(CMD_SCREEN_KEYDEF, 0));
382         g_strlcat(buf, comment, sizeof(buf));
383         g_strlcat(buf, ")", sizeof(buf));
384 #endif
386         screen_status_printf("%s", buf);
388         doupdate();
389         return TRUE;
391 #endif
393 int
394 main(int argc, const char *argv[])
396         struct sigaction act;
397 #ifdef ENABLE_LOCALE
398         const char *charset = NULL;
399 #endif
400         GIOChannel *keyboard_channel;
401 #ifdef ENABLE_LIRC
402         int lirc_socket;
403         GIOChannel *lirc_channel = NULL;
404 #endif
406 #ifdef ENABLE_LOCALE
407         /* time and date formatting */
408         setlocale(LC_TIME,"");
409         /* care about sorting order etc */
410         setlocale(LC_COLLATE,"");
411         /* charset */
412         setlocale(LC_CTYPE,"");
413         /* initialize charset conversions */
414         charset = charset_init();
416         /* initialize i18n support */
417 #endif
419 #ifdef ENABLE_NLS
420         setlocale(LC_MESSAGES, "");
421         bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
422 #ifdef ENABLE_LOCALE
423         bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
424 #endif
425         textdomain(GETTEXT_PACKAGE);
426 #endif
428         /* initialize options */
429         options_init();
431         /* parse command line options - 1 pass get configuration files */
432         options_parse(argc, argv);
434 #ifndef NCMPC_MINI
435         /* read configuration */
436         read_configuration();
438         /* check key bindings */
439         check_key_bindings(NULL, NULL, 0);
440 #endif
442         /* parse command line options - 2 pass */
443         options_parse(argc, argv);
445         /* setup signal behavior - SIGINT */
446         sigemptyset(&act.sa_mask);
447         act.sa_flags = 0;
448         act.sa_handler = catch_sigint;
449         if (sigaction(SIGINT, &act, NULL) < 0) {
450                 perror("signal");
451                 exit(EXIT_FAILURE);
452         }
454         /* setup signal behavior - SIGTERM */
456         act.sa_handler = catch_sigint;
457         if (sigaction(SIGTERM, &act, NULL) < 0) {
458                 perror("sigaction()");
459                 exit(EXIT_FAILURE);
460         }
462         /* setup signal behavior - SIGCONT */
464         act.sa_handler = catch_sigcont;
465         if (sigaction(SIGCONT, &act, NULL) < 0) {
466                 perror("sigaction(SIGCONT)");
467                 exit(EXIT_FAILURE);
468         }
470         /* setup signal behaviour - SIGHUP*/
472         act.sa_handler = catch_sigint;
473         if (sigaction(SIGHUP, &act, NULL) < 0) {
474                 perror("sigaction(SIGHUP)");
475                 exit(EXIT_FAILURE);
476         }
478         /* setup SIGWINCH */
480         act.sa_flags = SA_RESTART;
481         act.sa_handler = catch_sigwinch;
482         if (sigaction(SIGWINCH, &act, NULL) < 0) {
483                 perror("sigaction(SIGWINCH)");
484                 exit(EXIT_FAILURE);
485         }
487         /* ignore SIGPIPE */
489         act.sa_handler = SIG_IGN;
490         if (sigaction(SIGPIPE, &act, NULL) < 0) {
491                 perror("sigaction(SIGPIPE)");
492                 exit(EXIT_FAILURE);
493         }
495         ncu_init();
497 #ifdef ENABLE_LYRICS_SCREEN
498         lyrics_init();
499 #endif
501         /* create mpdclient instance */
502         mpd = mpdclient_new();
504         /* initialize curses */
505         screen_init(mpd);
507         /* the main loop */
508         main_loop = g_main_loop_new(NULL, FALSE);
510         /* watch out for keyboard input */
511         keyboard_channel = g_io_channel_unix_new(STDIN_FILENO);
512         g_io_add_watch(keyboard_channel, G_IO_IN, keyboard_event, NULL);
514 #ifdef ENABLE_LIRC
515         /* watch out for lirc input */
516         lirc_socket = ncmpc_lirc_open();
517         if (lirc_socket >= 0) {
518                 lirc_channel = g_io_channel_unix_new(lirc_socket);
519                 g_io_add_watch(lirc_channel, G_IO_IN, lirc_event, NULL);
520         }
521 #endif
523         /* attempt to connect */
524         reconnect_source_id = g_timeout_add(1, timer_reconnect, NULL);
526         auto_update_timer();
528 #ifndef NCMPC_MINI
529         check_key_bindings_source_id = g_timeout_add(10000, timer_check_key_bindings, NULL);
530 #endif
532         screen_paint(mpd);
534         g_main_loop_run(main_loop);
536         /* cleanup */
538         cancel_seek_timer();
540         disable_update_timer();
542         if (reconnect_source_id != 0)
543                 g_source_remove(reconnect_source_id);
545 #ifndef NCMPC_MINI
546         if (check_key_bindings_source_id != 0)
547                 g_source_remove(check_key_bindings_source_id);
548 #endif
550         g_main_loop_unref(main_loop);
551         g_io_channel_unref(keyboard_channel);
553 #ifdef ENABLE_LIRC
554         if (lirc_socket >= 0)
555                 g_io_channel_unref(lirc_channel);
556         ncmpc_lirc_close();
557 #endif
559         exit_and_cleanup();
561 #ifdef ENABLE_LYRICS_SCREEN
562         lyrics_deinit();
563 #endif
565         ncu_deinit();
566         options_deinit();
568         return 0;