Code

0a9f7cb59eebb57fad5ef9210fe2479cb73fb508
[ncmpc.git] / src / main.c
1 /* 
2  * $Id$
3  *
4  * (c) 2004 by Kalle Wallin <kaw@linux.se>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  */
21 #include "config.h"
22 #include "ncmpc.h"
23 #include "mpdclient.h"
24 #include "charset.h"
25 #include "options.h"
26 #include "conf.h"
27 #include "command.h"
28 #include "ncu.h"
29 #include "screen.h"
30 #include "screen_utils.h"
31 #include "strfsong.h"
32 #include "i18n.h"
33 #include "gcc.h"
35 #ifdef ENABLE_LYRICS_SCREEN
36 #include "lyrics.h"
37 #endif
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <signal.h>
42 #include <string.h>
44 /* time between mpd updates [s] */
45 static const float MPD_UPDATE_TIME = 0.5;
47 #define BUFSIZE 1024
49 static const guint idle_interval = 500;
51 static mpdclient_t *mpd = NULL;
52 static gboolean connected = FALSE;
53 static GMainLoop *main_loop;
54 static guint reconnect_source_id, idle_source_id, update_source_id;
56 static const gchar *
57 error_msg(const gchar *msg)
58 {
59         gchar *p;
61         if ((p = strchr(msg, '}')) == NULL)
62                 return msg;
64         while (p && *p && (*p=='}' || *p==' '))
65                 p++;
67         return p;
68 }
70 static void
71 error_callback(mpd_unused mpdclient_t *c, gint error, const gchar *msg)
72 {
73         error = error & 0xFF;
74         switch (error) {
75         case MPD_ERROR_CONNPORT:
76         case MPD_ERROR_NORESPONSE:
77                 break;
78         case MPD_ERROR_ACK:
79                 screen_status_printf("%s", error_msg(msg));
80                 screen_bell();
81                 break;
82         default:
83                 screen_status_printf("%s", msg);
84                 screen_bell();
85                 doupdate();
86                 connected = FALSE;
87         }
88 }
90 static void
91 update_xterm_title(void)
92 {
93         static char title[BUFSIZE];
94         char tmp[BUFSIZE];
95         mpd_Status *status = NULL;
96         mpd_Song *song = NULL;
98         if (mpd) {
99                 status = mpd->status;
100                 song = mpd->song;
101         }
103         if (options.xterm_title_format && status && song &&
104             IS_PLAYING(status->state))
105                 strfsong(tmp, BUFSIZE, options.xterm_title_format, song);
106         else
107                 g_strlcpy(tmp, PACKAGE " version " VERSION, BUFSIZE);
109         if (strncmp(title, tmp, BUFSIZE)) {
110                 g_strlcpy(title, tmp, BUFSIZE);
111                 set_xterm_title("%s", title);
112         }
115 static void
116 exit_and_cleanup(void)
118         screen_exit();
119         set_xterm_title("");
120         printf("\n");
122         if (mpd) {
123                 mpdclient_disconnect(mpd);
124                 mpdclient_free(mpd);
125         }
127         g_free(options.host);
128         g_free(options.password);
129         g_free(options.list_format);
130         g_free(options.status_format);
131         g_free(options.scroll_sep);
134 static void
135 catch_sigint(mpd_unused int sig)
137         g_main_loop_quit(main_loop);
141 static void
142 catch_sigcont(mpd_unused int sig)
144 #ifdef ENABLE_RAW_MODE
145         reset_prog_mode(); /* restore tty modes */
146         refresh();
147 #endif
148         screen_resize();
151 void
152 sigstop(void)
154   def_prog_mode();  /* save the tty modes */
155   endwin();         /* end curses mode temporarily */
156   kill(0, SIGSTOP); /* issue SIGSTOP */
159 static guint timer_sigwinch_id;
161 static gboolean
162 timer_sigwinch(mpd_unused gpointer data)
164         /* the following causes the screen to flicker.  There might be
165            better solutions, but I believe it isn't all that
166            important. */
168         endwin();
169         refresh();
170         screen_resize();
172         return FALSE;
175 static void
176 catch_sigwinch(mpd_unused int sig)
178         if (timer_sigwinch_id != 0)
179                 g_source_remove(timer_sigwinch_id);
181         timer_sigwinch_id = g_timeout_add(100, timer_sigwinch, NULL);
184 static gboolean
185 timer_mpd_update(gpointer data);
187 /**
188  * This timer is installed when the connection to the MPD server is
189  * broken.  It tries to recover by reconnecting periodically.
190  */
191 static gboolean
192 timer_reconnect(mpd_unused gpointer data)
194         int ret;
196         if (connected)
197                 return FALSE;
199         screen_status_printf(_("Connecting to %s...  [Press %s to abort]"),
200                              options.host, get_key_names(CMD_QUIT,0) );
201         doupdate();
203         mpdclient_disconnect(mpd);
204         ret = mpdclient_connect(mpd,
205                                 options.host, options.port,
206                                 1.5,
207                                 options.password);
208         if (ret != 0) {
209                 /* try again in 5 seconds */
210                 g_timeout_add(5000, timer_reconnect, NULL);
211                 return FALSE;
212         }
214         /* quit if mpd is pre 0.11.0 - song id not supported by mpd */
215         if (MPD_VERSION_LT(mpd, 0, 11, 0)) {
216                 screen_status_printf(_("Error: MPD version %d.%d.%d is to old (0.11.0 needed).\n"),
217                                      mpd->connection->version[0],
218                                      mpd->connection->version[1],
219                                      mpd->connection->version[2]);
220                 mpdclient_disconnect(mpd);
221                 doupdate();
223                 /* try again after 30 seconds */
224                 g_timeout_add(30000, timer_reconnect, NULL);
225                 return FALSE;
226         }
228         screen_status_printf(_("Connected to %s!"), options.host);
229         doupdate();
231         connected = TRUE;
233         /* update immediately */
234         g_timeout_add(1, timer_mpd_update, GINT_TO_POINTER(FALSE));
236         reconnect_source_id = 0;
237         return FALSE;
241 static gboolean
242 timer_mpd_update(gpointer data)
244         if (connected)
245                 mpdclient_update(mpd);
246         else if (reconnect_source_id == 0)
247                 reconnect_source_id = g_timeout_add(1000, timer_reconnect,
248                                                     NULL);
250         if (options.enable_xterm_title)
251                 update_xterm_title();
253         screen_update(mpd);
255         return GPOINTER_TO_INT(data);
258 /**
259  * This idle timer is invoked when the user hasn't typed a key for
260  * 500ms.  It is used for delayed seeking.
261  */
262 static gboolean
263 timer_idle(mpd_unused gpointer data)
265         screen_idle(mpd);
266         return TRUE;
269 static gboolean
270 keyboard_event(mpd_unused GIOChannel *source,
271                mpd_unused GIOCondition condition, mpd_unused gpointer data)
273         command_t cmd;
275         /* remove the idle timeout; add it later with fresh interval */
276         g_source_remove(idle_source_id);
278         if ((cmd=get_keyboard_command()) != CMD_NONE) {
279                 if (cmd == CMD_QUIT) {
280                         g_main_loop_quit(main_loop);
281                         return FALSE;
282                 }
284                 screen_cmd(mpd, cmd);
286                 if (cmd == CMD_VOLUME_UP || cmd == CMD_VOLUME_DOWN) {
287                         /* make sure we dont update the volume yet */
288                         g_source_remove(update_source_id);
289                         update_source_id = g_timeout_add((guint)(MPD_UPDATE_TIME * 1000),
290                                                          timer_mpd_update,
291                                                          GINT_TO_POINTER(TRUE));
292                 }
293         }
295         screen_update(mpd);
297         idle_source_id = g_timeout_add(idle_interval, timer_idle, NULL);
298         return TRUE;
301 /**
302  * Check the configured key bindings for errors, and display a status
303  * message every 10 seconds.
304  */
305 static gboolean
306 timer_check_key_bindings(mpd_unused gpointer data)
308         char buf[256];
309         gboolean key_error;
311         key_error = check_key_bindings(NULL, buf, sizeof(buf));
312         if (!key_error)
313                 /* no error: disable this timer for the rest of this
314                    process */
315                 return FALSE;
317         screen_status_printf("%s", buf);
318         doupdate();
319         return TRUE;
322 int
323 main(int argc, const char *argv[])
325         struct sigaction act;
326         const char *charset = NULL;
327         GIOChannel *keyboard_channel;
329 #ifdef HAVE_LOCALE_H
330         /* time and date formatting */
331         setlocale(LC_TIME,"");
332         /* care about sorting order etc */
333         setlocale(LC_COLLATE,"");
334         /* charset */
335         setlocale(LC_CTYPE,"");
336         /* initialize charset conversions */
337         charset_init(g_get_charset(&charset));
338 #endif
340         /* initialize i18n support */
341 #ifdef ENABLE_NLS
342         setlocale(LC_MESSAGES, "");
343         bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
344         bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
345         textdomain(GETTEXT_PACKAGE);
346 #endif
348         /* initialize options */
349         options_init();
351         /* parse command line options - 1 pass get configuration files */
352         options_parse(argc, argv);
354         /* read configuration */
355         read_configuration(&options);
357         /* check key bindings */
358         check_key_bindings(NULL, NULL, 0);
360         /* parse command line options - 2 pass */
361         options_parse(argc, argv);
363         /* setup signal behavior - SIGINT */
364         sigemptyset(&act.sa_mask);
365         act.sa_flags = 0;
366         act.sa_handler = catch_sigint;
367         if (sigaction(SIGINT, &act, NULL) < 0) {
368                 perror("signal");
369                 exit(EXIT_FAILURE);
370         }
372         /* setup signal behavior - SIGTERM */
373         sigemptyset(&act.sa_mask);
374         act.sa_flags = 0;
375         act.sa_handler = catch_sigint;
376         if (sigaction(SIGTERM, &act, NULL) < 0) {
377                 perror("sigaction()");
378                 exit(EXIT_FAILURE);
379         }
381         /* setup signal behavior - SIGCONT */
382         sigemptyset(&act.sa_mask);
383         act.sa_flags = 0;
384         act.sa_handler = catch_sigcont;
385         if (sigaction(SIGCONT, &act, NULL) < 0) {
386                 perror("sigaction(SIGCONT)");
387                 exit(EXIT_FAILURE);
388         }
390         /* setup signal behaviour - SIGHUP*/
391         sigemptyset(&act.sa_mask);
392         act.sa_flags = 0;
393         act.sa_handler = catch_sigint;
394         if (sigaction(SIGHUP, &act, NULL) < 0) {
395                 perror("sigaction(SIGHUP)");
396                 exit(EXIT_FAILURE);
397         }
399         /* setup SIGWINCH */
401         act.sa_handler = catch_sigwinch;
402         if (sigaction(SIGWINCH, &act, NULL) < 0) {
403                 perror("sigaction(SIGWINCH)");
404                 exit(EXIT_FAILURE);
405         }
407         /* ignore SIGPIPE */
409         act.sa_flags = SA_RESTART;
410         act.sa_handler = SIG_IGN;
411         if (sigaction(SIGWINCH, &act, NULL) < 0) {
412                 perror("sigaction(SIGWINCH)");
413                 exit(EXIT_FAILURE);
414         }
416         ncu_init();
418 #ifdef ENABLE_LYRICS_SCREEN
419         lyrics_init();
420 #endif
422         /* create mpdclient instance */
423         mpd = mpdclient_new();
424         mpdclient_install_error_callback(mpd, error_callback);
426         /* initialize curses */
427         screen_init(mpd);
429         /* the main loop */
430         main_loop = g_main_loop_new(NULL, FALSE);
432         /* watch out for keyboard input */
433         keyboard_channel = g_io_channel_unix_new(STDIN_FILENO);
434         g_io_add_watch(keyboard_channel, G_IO_IN, keyboard_event, NULL);
436         /* attempt to connect */
437         reconnect_source_id = g_timeout_add(1, timer_reconnect, NULL);
439         update_source_id = g_timeout_add((guint)(MPD_UPDATE_TIME * 1000),
440                                          timer_mpd_update,
441                                          GINT_TO_POINTER(TRUE));
442         g_timeout_add(10000, timer_check_key_bindings, NULL);
443         idle_source_id = g_timeout_add(idle_interval, timer_idle, NULL);
445         g_main_loop_run(main_loop);
447         /* cleanup */
449         g_main_loop_unref(main_loop);
450         g_io_channel_unref(keyboard_channel);
452         exit_and_cleanup();
453         ncu_deinit();
455         return 0;