Code

include ncursesw/ncurses.h if available
[ncmpc.git] / src / main.c
1 /*
2  * (c) 2004 by Kalle Wallin <kaw@linux.se>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  *
17  */
19 #include "config.h"
20 #include "ncmpc.h"
21 #include "mpdclient.h"
22 #include "charset.h"
23 #include "options.h"
24 #include "conf.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 "gcc.h"
33 #ifdef ENABLE_LYRICS_SCREEN
34 #include "lyrics.h"
35 #endif
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <signal.h>
40 #include <string.h>
42 /* time between mpd updates [s] */
43 static const guint update_interval = 500;
45 #define BUFSIZE 1024
47 static const guint idle_interval = 500;
49 static mpdclient_t *mpd = NULL;
50 static gboolean connected = FALSE;
51 static GMainLoop *main_loop;
52 static guint reconnect_source_id, idle_source_id, update_source_id;
54 static const gchar *
55 error_msg(const gchar *msg)
56 {
57         gchar *p;
59         if ((p = strchr(msg, '}')) == NULL)
60                 return msg;
62         while (p && *p && (*p=='}' || *p==' '))
63                 p++;
65         return p;
66 }
68 static void
69 error_callback(mpd_unused mpdclient_t *c, gint error, const gchar *msg)
70 {
71         error = error & 0xFF;
72         switch (error) {
73         case MPD_ERROR_CONNPORT:
74         case MPD_ERROR_NORESPONSE:
75                 break;
76         case MPD_ERROR_ACK:
77                 screen_status_printf("%s", error_msg(msg));
78                 screen_bell();
79                 break;
80         default:
81                 screen_status_printf("%s", msg);
82                 screen_bell();
83                 doupdate();
84                 connected = FALSE;
85         }
86 }
88 static void
89 update_xterm_title(void)
90 {
91         static char title[BUFSIZE];
92         char tmp[BUFSIZE];
93         mpd_Status *status = NULL;
94         mpd_Song *song = NULL;
96         if (mpd) {
97                 status = mpd->status;
98                 song = mpd->song;
99         }
101         if (options.xterm_title_format && status && song &&
102             IS_PLAYING(status->state))
103                 strfsong(tmp, BUFSIZE, options.xterm_title_format, song);
104         else
105                 g_strlcpy(tmp, PACKAGE " version " VERSION, BUFSIZE);
107         if (strncmp(title, tmp, BUFSIZE)) {
108                 g_strlcpy(title, tmp, BUFSIZE);
109                 set_xterm_title("%s", title);
110         }
113 static void
114 exit_and_cleanup(void)
116         screen_exit();
117         set_xterm_title("");
118         printf("\n");
120         if (mpd) {
121                 mpdclient_disconnect(mpd);
122                 mpdclient_free(mpd);
123         }
125         g_free(options.host);
126         g_free(options.password);
127         g_free(options.list_format);
128         g_free(options.status_format);
129         g_free(options.scroll_sep);
132 static void
133 catch_sigint(mpd_unused int sig)
135         g_main_loop_quit(main_loop);
139 static void
140 catch_sigcont(mpd_unused int sig)
142         screen_resize(mpd);
145 void
146 sigstop(void)
148   def_prog_mode();  /* save the tty modes */
149   endwin();         /* end curses mode temporarily */
150   kill(0, SIGSTOP); /* issue SIGSTOP */
153 static guint timer_sigwinch_id;
155 static gboolean
156 timer_sigwinch(mpd_unused gpointer data)
158         /* the following causes the screen to flicker.  There might be
159            better solutions, but I believe it isn't all that
160            important. */
162         endwin();
163         refresh();
164         screen_resize(mpd);
166         return FALSE;
169 static void
170 catch_sigwinch(mpd_unused int sig)
172         if (timer_sigwinch_id != 0)
173                 g_source_remove(timer_sigwinch_id);
175         timer_sigwinch_id = g_timeout_add(100, timer_sigwinch, NULL);
178 static gboolean
179 timer_mpd_update(gpointer data);
181 /**
182  * This timer is installed when the connection to the MPD server is
183  * broken.  It tries to recover by reconnecting periodically.
184  */
185 static gboolean
186 timer_reconnect(mpd_unused gpointer data)
188         int ret;
190         if (connected)
191                 return FALSE;
193         screen_status_printf(_("Connecting to %s...  [Press %s to abort]"),
194                              options.host, get_key_names(CMD_QUIT,0) );
195         doupdate();
197         mpdclient_disconnect(mpd);
198         ret = mpdclient_connect(mpd,
199                                 options.host, options.port,
200                                 1.5,
201                                 options.password);
202         if (ret != 0) {
203                 /* try again in 5 seconds */
204                 g_timeout_add(5000, timer_reconnect, NULL);
205                 return FALSE;
206         }
208         /* quit if mpd is pre 0.11.0 - song id not supported by mpd */
209         if (MPD_VERSION_LT(mpd, 0, 11, 0)) {
210                 screen_status_printf(_("Error: MPD version %d.%d.%d is to old (0.11.0 needed).\n"),
211                                      mpd->connection->version[0],
212                                      mpd->connection->version[1],
213                                      mpd->connection->version[2]);
214                 mpdclient_disconnect(mpd);
215                 doupdate();
217                 /* try again after 30 seconds */
218                 g_timeout_add(30000, timer_reconnect, NULL);
219                 return FALSE;
220         }
222         screen_status_printf(_("Connected to %s!"), options.host);
223         doupdate();
225         connected = TRUE;
227         /* update immediately */
228         g_timeout_add(1, timer_mpd_update, GINT_TO_POINTER(FALSE));
230         reconnect_source_id = 0;
231         return FALSE;
235 static gboolean
236 timer_mpd_update(gpointer data)
238         if (connected)
239                 mpdclient_update(mpd);
240         else if (reconnect_source_id == 0)
241                 reconnect_source_id = g_timeout_add(1000, timer_reconnect,
242                                                     NULL);
244         if (options.enable_xterm_title)
245                 update_xterm_title();
247         screen_update(mpd);
249         return GPOINTER_TO_INT(data);
252 /**
253  * This idle timer is invoked when the user hasn't typed a key for
254  * 500ms.  It is used for delayed seeking.
255  */
256 static gboolean
257 timer_idle(mpd_unused gpointer data)
259         screen_idle(mpd);
260         return TRUE;
263 static gboolean
264 keyboard_event(mpd_unused GIOChannel *source,
265                mpd_unused GIOCondition condition, mpd_unused gpointer data)
267         command_t cmd;
269         /* remove the idle timeout; add it later with fresh interval */
270         g_source_remove(idle_source_id);
272         if ((cmd=get_keyboard_command()) != CMD_NONE) {
273                 if (cmd == CMD_QUIT) {
274                         g_main_loop_quit(main_loop);
275                         return FALSE;
276                 }
278                 screen_cmd(mpd, cmd);
280                 if (cmd == CMD_VOLUME_UP || cmd == CMD_VOLUME_DOWN) {
281                         /* make sure we dont update the volume yet */
282                         g_source_remove(update_source_id);
283                         update_source_id = g_timeout_add(update_interval,
284                                                          timer_mpd_update,
285                                                          GINT_TO_POINTER(TRUE));
286                 }
287         }
289         screen_update(mpd);
291         idle_source_id = g_timeout_add(idle_interval, timer_idle, NULL);
292         return TRUE;
295 /**
296  * Check the configured key bindings for errors, and display a status
297  * message every 10 seconds.
298  */
299 static gboolean
300 timer_check_key_bindings(mpd_unused gpointer data)
302         char buf[256];
303         gboolean key_error;
305         key_error = check_key_bindings(NULL, buf, sizeof(buf));
306         if (!key_error)
307                 /* no error: disable this timer for the rest of this
308                    process */
309                 return FALSE;
311         screen_status_printf("%s", buf);
312         doupdate();
313         return TRUE;
316 int
317 main(int argc, const char *argv[])
319         struct sigaction act;
320 #ifdef HAVE_LOCALE_H
321         const char *charset = NULL;
322 #endif
323         GIOChannel *keyboard_channel;
325 #ifdef HAVE_LOCALE_H
326         /* time and date formatting */
327         setlocale(LC_TIME,"");
328         /* care about sorting order etc */
329         setlocale(LC_COLLATE,"");
330         /* charset */
331         setlocale(LC_CTYPE,"");
332         /* initialize charset conversions */
333         charset = charset_init();
335         /* initialize i18n support */
336 #ifdef ENABLE_NLS
337         setlocale(LC_MESSAGES, "");
338         bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
339         bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
340         textdomain(GETTEXT_PACKAGE);
341 #endif
342 #endif
344         /* initialize options */
345         options_init();
347         /* parse command line options - 1 pass get configuration files */
348         options_parse(argc, argv);
350         /* read configuration */
351         read_configuration();
353         /* check key bindings */
354         check_key_bindings(NULL, NULL, 0);
356         /* parse command line options - 2 pass */
357         options_parse(argc, argv);
359         /* setup signal behavior - SIGINT */
360         sigemptyset(&act.sa_mask);
361         act.sa_flags = 0;
362         act.sa_handler = catch_sigint;
363         if (sigaction(SIGINT, &act, NULL) < 0) {
364                 perror("signal");
365                 exit(EXIT_FAILURE);
366         }
368         /* setup signal behavior - SIGTERM */
370         act.sa_handler = catch_sigint;
371         if (sigaction(SIGTERM, &act, NULL) < 0) {
372                 perror("sigaction()");
373                 exit(EXIT_FAILURE);
374         }
376         /* setup signal behavior - SIGCONT */
378         act.sa_handler = catch_sigcont;
379         if (sigaction(SIGCONT, &act, NULL) < 0) {
380                 perror("sigaction(SIGCONT)");
381                 exit(EXIT_FAILURE);
382         }
384         /* setup signal behaviour - SIGHUP*/
386         act.sa_handler = catch_sigint;
387         if (sigaction(SIGHUP, &act, NULL) < 0) {
388                 perror("sigaction(SIGHUP)");
389                 exit(EXIT_FAILURE);
390         }
392         /* setup SIGWINCH */
394         act.sa_flags = SA_RESTART;
395         act.sa_handler = catch_sigwinch;
396         if (sigaction(SIGWINCH, &act, NULL) < 0) {
397                 perror("sigaction(SIGWINCH)");
398                 exit(EXIT_FAILURE);
399         }
401         /* ignore SIGPIPE */
403         act.sa_handler = SIG_IGN;
404         if (sigaction(SIGPIPE, &act, NULL) < 0) {
405                 perror("sigaction(SIGPIPE)");
406                 exit(EXIT_FAILURE);
407         }
409         ncu_init();
411 #ifdef ENABLE_LYRICS_SCREEN
412         lyrics_init();
413 #endif
415         /* create mpdclient instance */
416         mpd = mpdclient_new();
417         mpdclient_install_error_callback(mpd, error_callback);
419         /* initialize curses */
420         screen_init(mpd);
422         /* the main loop */
423         main_loop = g_main_loop_new(NULL, FALSE);
425         /* watch out for keyboard input */
426         keyboard_channel = g_io_channel_unix_new(STDIN_FILENO);
427         g_io_add_watch(keyboard_channel, G_IO_IN, keyboard_event, NULL);
429         /* attempt to connect */
430         reconnect_source_id = g_timeout_add(1, timer_reconnect, NULL);
432         update_source_id = g_timeout_add(update_interval,
433                                          timer_mpd_update,
434                                          GINT_TO_POINTER(TRUE));
435         g_timeout_add(10000, timer_check_key_bindings, NULL);
436         idle_source_id = g_timeout_add(idle_interval, timer_idle, NULL);
438         screen_paint(mpd);
440         g_main_loop_run(main_loop);
442         /* cleanup */
444         g_main_loop_unref(main_loop);
445         g_io_channel_unref(keyboard_channel);
447         exit_and_cleanup();
448         ncu_deinit();
450         return 0;