Code

moved the idle timer to player_command.c
[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"
31 #include "player_command.h"
33 #ifndef NCMPC_MINI
34 #include "conf.h"
35 #endif
37 #ifdef ENABLE_LYRICS_SCREEN
38 #include "lyrics.h"
39 #endif
41 #ifdef ENABLE_LIRC
42 #include "lirc.h"
43 #endif
45 #include <mpd/client.h>
47 #include <stdlib.h>
48 #include <unistd.h>
49 #include <signal.h>
50 #include <string.h>
52 #ifdef ENABLE_LOCALE
53 #include <locale.h>
54 #endif
56 /* time between mpd updates [s] */
57 static const guint update_interval = 500;
59 #define BUFSIZE 1024
61 static struct mpdclient *mpd = NULL;
62 static gboolean connected = FALSE;
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 static const gchar *
71 error_msg(const gchar *msg)
72 {
73         gchar *p;
75         if ((p = strchr(msg, '}')) == NULL)
76                 return msg;
78         do {
79                 p++;
80         } while (*p == '}' || * p== ' ');
82         return p;
83 }
85 static void
86 error_callback(G_GNUC_UNUSED struct mpdclient *c, gint error, const gchar *_msg)
87 {
88         char *msg = utf8_to_locale(_msg);
90         error = error & 0xFF;
91         switch (error) {
92         case MPD_ERROR_SERVER:
93                 screen_status_printf("%s", error_msg(msg));
94                 screen_bell();
95                 break;
96         default:
97                 screen_status_printf("%s", msg);
98                 screen_bell();
99                 doupdate();
100                 connected = FALSE;
101         }
103         g_free(msg);
106 #ifndef NCMPC_MINI
107 static void
108 update_xterm_title(void)
110         static char title[BUFSIZE];
111         char tmp[BUFSIZE];
112         struct mpd_status *status = NULL;
113         struct mpd_song *song = NULL;
115         if (mpd) {
116                 status = mpd->status;
117                 song = mpd->song;
118         }
120         if (options.xterm_title_format && status && song &&
121             IS_PLAYING(mpd_status_get_state(status)))
122                 strfsong(tmp, BUFSIZE, options.xterm_title_format, song);
123         else
124                 g_strlcpy(tmp, PACKAGE " version " VERSION, BUFSIZE);
126         if (strncmp(title, tmp, BUFSIZE)) {
127                 g_strlcpy(title, tmp, BUFSIZE);
128                 set_xterm_title("%s", title);
129         }
131 #endif
133 static void
134 exit_and_cleanup(void)
136         screen_exit();
137 #ifndef NCMPC_MINI
138         set_xterm_title("");
139 #endif
140         printf("\n");
142         if (mpd) {
143                 mpdclient_disconnect(mpd);
144                 mpdclient_free(mpd);
145         }
148 static void
149 catch_sigint(G_GNUC_UNUSED int sig)
151         g_main_loop_quit(main_loop);
155 static void
156 catch_sigcont(G_GNUC_UNUSED int sig)
158         screen_resize(mpd);
161 void
162 sigstop(void)
164   def_prog_mode();  /* save the tty modes */
165   endwin();         /* end curses mode temporarily */
166   kill(0, SIGSTOP); /* issue SIGSTOP */
169 static guint timer_sigwinch_id;
171 static gboolean
172 timer_sigwinch(G_GNUC_UNUSED gpointer data)
174         /* the following causes the screen to flicker.  There might be
175            better solutions, but I believe it isn't all that
176            important. */
178         endwin();
179         refresh();
180         screen_resize(mpd);
182         return FALSE;
185 static void
186 catch_sigwinch(G_GNUC_UNUSED int sig)
188         if (timer_sigwinch_id != 0)
189                 g_source_remove(timer_sigwinch_id);
191         timer_sigwinch_id = g_timeout_add(100, timer_sigwinch, NULL);
194 static gboolean
195 timer_mpd_update(gpointer data);
197 /**
198  * This timer is installed when the connection to the MPD server is
199  * broken.  It tries to recover by reconnecting periodically.
200  */
201 static gboolean
202 timer_reconnect(G_GNUC_UNUSED gpointer data)
204         int ret;
206         if (connected)
207                 return FALSE;
209         screen_status_printf(_("Connecting to %s...  [Press %s to abort]"),
210                              options.host, get_key_names(CMD_QUIT,0) );
211         doupdate();
213         mpdclient_disconnect(mpd);
214         ret = mpdclient_connect(mpd,
215                                 options.host, options.port,
216                                 1.5,
217                                 options.password);
218         if (ret != 0) {
219                 /* try again in 5 seconds */
220                 g_timeout_add(5000, timer_reconnect, NULL);
221                 return FALSE;
222         }
224 #ifndef NCMPC_MINI
225         /* quit if mpd is pre 0.11.0 - song id not supported by mpd */
226         if (mpd_connection_cmp_server_version(mpd->connection, 0, 11, 0) < 0) {
227                 const unsigned *version =
228                         mpd_connection_get_server_version(mpd->connection);
229                 screen_status_printf(_("Error: MPD version %d.%d.%d is to old (%s needed)"),
230                                      version[0], version[1], version[2],
231                                      "0.11.0");
232                 mpdclient_disconnect(mpd);
233                 doupdate();
235                 /* try again after 30 seconds */
236                 g_timeout_add(30000, timer_reconnect, NULL);
237                 return FALSE;
238         }
239 #endif
241         screen_status_printf(_("Connected to %s"),
242                              options.host != NULL
243                              ? options.host : "localhost");
244         doupdate();
246         connected = TRUE;
248         /* update immediately */
249         g_timeout_add(1, timer_mpd_update, GINT_TO_POINTER(FALSE));
251         reconnect_source_id = 0;
252         return FALSE;
256 static gboolean
257 timer_mpd_update(gpointer data)
259         if (connected)
260                 mpdclient_update(mpd);
261         else if (reconnect_source_id == 0)
262                 reconnect_source_id = g_timeout_add(1000, timer_reconnect,
263                                                     NULL);
265 #ifndef NCMPC_MINI
266         if (options.enable_xterm_title)
267                 update_xterm_title();
268 #endif
270         screen_update(mpd);
272         return GPOINTER_TO_INT(data);
275 void begin_input_event(void)
279 void end_input_event(void)
281         screen_update(mpd);
284 int do_input_event(command_t cmd)
286         if (cmd == CMD_QUIT) {
287                 g_main_loop_quit(main_loop);
288                 return -1;
289         }
291         screen_cmd(mpd, cmd);
293         if (cmd == CMD_VOLUME_UP || cmd == CMD_VOLUME_DOWN) {
294                 /* make sure we don't update the volume yet */
295                 g_source_remove(update_source_id);
296                 update_source_id = g_timeout_add(update_interval,
297                                                  timer_mpd_update,
298                                                  GINT_TO_POINTER(TRUE));
299         }
301         return 0;
304 static gboolean
305 keyboard_event(G_GNUC_UNUSED GIOChannel *source,
306                G_GNUC_UNUSED GIOCondition condition,
307                G_GNUC_UNUSED gpointer data)
309         command_t cmd;
311         begin_input_event();
313         if ((cmd=get_keyboard_command()) != CMD_NONE)
314                 if (do_input_event(cmd) != 0)
315                         return FALSE;
317         end_input_event();
318         return TRUE;
321 #ifndef NCMPC_MINI
322 /**
323  * Check the configured key bindings for errors, and display a status
324  * message every 10 seconds.
325  */
326 static gboolean
327 timer_check_key_bindings(G_GNUC_UNUSED gpointer data)
329         char buf[256];
330 #ifdef ENABLE_KEYDEF_SCREEN
331         char comment[64];
332 #endif
333         gboolean key_error;
335         key_error = check_key_bindings(NULL, buf, sizeof(buf));
336         if (!key_error) {
337                 /* no error: disable this timer for the rest of this
338                    process */
339                 check_key_bindings_source_id = 0;
340                 return FALSE;
341         }
343 #ifdef ENABLE_KEYDEF_SCREEN
344         g_strchomp(buf);
345         g_strlcat(buf, " (", sizeof(buf));
346         /* to translators: a key was bound twice in the key editor,
347            and this is a hint for the user what to press to correct
348            that */
349         g_snprintf(comment, sizeof(comment), _("press %s for the key editor"),
350                    get_key_names(CMD_SCREEN_KEYDEF, 0));
351         g_strlcat(buf, comment, sizeof(buf));
352         g_strlcat(buf, ")", sizeof(buf));
353 #endif
355         screen_status_printf("%s", buf);
357         doupdate();
358         return TRUE;
360 #endif
362 int
363 main(int argc, const char *argv[])
365         struct sigaction act;
366 #ifdef ENABLE_LOCALE
367         const char *charset = NULL;
368 #endif
369         GIOChannel *keyboard_channel;
370 #ifdef ENABLE_LIRC
371         int lirc_socket;
372         GIOChannel *lirc_channel = NULL;
373 #endif
375 #ifdef ENABLE_LOCALE
376         /* time and date formatting */
377         setlocale(LC_TIME,"");
378         /* care about sorting order etc */
379         setlocale(LC_COLLATE,"");
380         /* charset */
381         setlocale(LC_CTYPE,"");
382         /* initialize charset conversions */
383         charset = charset_init();
385         /* initialize i18n support */
386 #endif
388 #ifdef ENABLE_NLS
389         setlocale(LC_MESSAGES, "");
390         bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
391 #ifdef ENABLE_LOCALE
392         bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
393 #endif
394         textdomain(GETTEXT_PACKAGE);
395 #endif
397         /* initialize options */
398         options_init();
400         /* parse command line options - 1 pass get configuration files */
401         options_parse(argc, argv);
403 #ifndef NCMPC_MINI
404         /* read configuration */
405         read_configuration();
407         /* check key bindings */
408         check_key_bindings(NULL, NULL, 0);
409 #endif
411         /* parse command line options - 2 pass */
412         options_parse(argc, argv);
414         /* setup signal behavior - SIGINT */
415         sigemptyset(&act.sa_mask);
416         act.sa_flags = 0;
417         act.sa_handler = catch_sigint;
418         if (sigaction(SIGINT, &act, NULL) < 0) {
419                 perror("signal");
420                 exit(EXIT_FAILURE);
421         }
423         /* setup signal behavior - SIGTERM */
425         act.sa_handler = catch_sigint;
426         if (sigaction(SIGTERM, &act, NULL) < 0) {
427                 perror("sigaction()");
428                 exit(EXIT_FAILURE);
429         }
431         /* setup signal behavior - SIGCONT */
433         act.sa_handler = catch_sigcont;
434         if (sigaction(SIGCONT, &act, NULL) < 0) {
435                 perror("sigaction(SIGCONT)");
436                 exit(EXIT_FAILURE);
437         }
439         /* setup signal behaviour - SIGHUP*/
441         act.sa_handler = catch_sigint;
442         if (sigaction(SIGHUP, &act, NULL) < 0) {
443                 perror("sigaction(SIGHUP)");
444                 exit(EXIT_FAILURE);
445         }
447         /* setup SIGWINCH */
449         act.sa_flags = SA_RESTART;
450         act.sa_handler = catch_sigwinch;
451         if (sigaction(SIGWINCH, &act, NULL) < 0) {
452                 perror("sigaction(SIGWINCH)");
453                 exit(EXIT_FAILURE);
454         }
456         /* ignore SIGPIPE */
458         act.sa_handler = SIG_IGN;
459         if (sigaction(SIGPIPE, &act, NULL) < 0) {
460                 perror("sigaction(SIGPIPE)");
461                 exit(EXIT_FAILURE);
462         }
464         ncu_init();
466 #ifdef ENABLE_LYRICS_SCREEN
467         lyrics_init();
468 #endif
470         /* create mpdclient instance */
471         mpd = mpdclient_new();
472         mpdclient_install_error_callback(mpd, error_callback);
474         /* initialize curses */
475         screen_init(mpd);
477         /* the main loop */
478         main_loop = g_main_loop_new(NULL, FALSE);
480         /* watch out for keyboard input */
481         keyboard_channel = g_io_channel_unix_new(STDIN_FILENO);
482         g_io_add_watch(keyboard_channel, G_IO_IN, keyboard_event, NULL);
484 #ifdef ENABLE_LIRC
485         /* watch out for lirc input */
486         lirc_socket = ncmpc_lirc_open();
487         if (lirc_socket >= 0) {
488                 lirc_channel = g_io_channel_unix_new(lirc_socket);
489                 g_io_add_watch(lirc_channel, G_IO_IN, lirc_event, NULL);
490         }
491 #endif
493         /* attempt to connect */
494         reconnect_source_id = g_timeout_add(1, timer_reconnect, NULL);
496         update_source_id = g_timeout_add(update_interval,
497                                          timer_mpd_update,
498                                          GINT_TO_POINTER(TRUE));
499 #ifndef NCMPC_MINI
500         check_key_bindings_source_id = g_timeout_add(10000, timer_check_key_bindings, NULL);
501 #endif
503         screen_paint(mpd);
505         g_main_loop_run(main_loop);
507         /* cleanup */
509         cancel_seek_timer();
511         g_source_remove(update_source_id);
513 #ifndef NCMPC_MINI
514         if (check_key_bindings_source_id != 0)
515                 g_source_remove(check_key_bindings_source_id);
516 #endif
518         g_main_loop_unref(main_loop);
519         g_io_channel_unref(keyboard_channel);
521 #ifdef ENABLE_LIRC
522         if (lirc_socket >= 0)
523                 g_io_channel_unref(lirc_channel);
524         ncmpc_lirc_close();
525 #endif
527         exit_and_cleanup();
529 #ifdef ENABLE_LYRICS_SCREEN
530         lyrics_deinit();
531 #endif
533         ncu_deinit();
534         options_deinit();
536         return 0;