668d5aa3350a197feccaeb7ea488e08d4f9db9eb
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 }
113 }
115 static void
116 exit_and_cleanup(void)
117 {
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);
132 }
134 static void
135 catch_sigint(mpd_unused int sig)
136 {
137 g_main_loop_quit(main_loop);
138 }
141 static void
142 catch_sigcont(mpd_unused int sig)
143 {
144 #ifdef ENABLE_RAW_MODE
145 reset_prog_mode(); /* restore tty modes */
146 refresh();
147 #endif
148 screen_resize();
149 }
151 void
152 sigstop(void)
153 {
154 def_prog_mode(); /* save the tty modes */
155 endwin(); /* end curses mode temporarily */
156 kill(0, SIGSTOP); /* issue SIGSTOP */
157 }
159 static guint timer_sigwinch_id;
161 static gboolean
162 timer_sigwinch(mpd_unused gpointer data)
163 {
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;
173 }
175 static void
176 catch_sigwinch(mpd_unused int sig)
177 {
178 if (timer_sigwinch_id != 0)
179 g_source_remove(timer_sigwinch_id);
181 timer_sigwinch_id = g_timeout_add(100, timer_sigwinch, NULL);
182 }
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)
193 {
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;
239 }
241 static gboolean
242 timer_mpd_update(gpointer data)
243 {
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);
256 }
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)
264 {
265 screen_idle(mpd);
266 return TRUE;
267 }
269 static gboolean
270 keyboard_event(mpd_unused GIOChannel *source,
271 mpd_unused GIOCondition condition, mpd_unused gpointer data)
272 {
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;
299 }
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)
307 {
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;
320 }
322 int
323 main(int argc, const char *argv[])
324 {
325 struct sigaction act;
326 #ifdef HAVE_LOCALE_H
327 const char *charset = NULL;
328 #endif
329 GIOChannel *keyboard_channel;
331 #ifdef HAVE_LOCALE_H
332 /* time and date formatting */
333 setlocale(LC_TIME,"");
334 /* care about sorting order etc */
335 setlocale(LC_COLLATE,"");
336 /* charset */
337 setlocale(LC_CTYPE,"");
338 /* initialize charset conversions */
339 charset = charset_init();
341 /* initialize i18n support */
342 #ifdef ENABLE_NLS
343 setlocale(LC_MESSAGES, "");
344 bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
345 bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
346 textdomain(GETTEXT_PACKAGE);
347 #endif
348 #endif
350 /* initialize options */
351 options_init();
353 /* parse command line options - 1 pass get configuration files */
354 options_parse(argc, argv);
356 /* read configuration */
357 read_configuration(&options);
359 /* check key bindings */
360 check_key_bindings(NULL, NULL, 0);
362 /* parse command line options - 2 pass */
363 options_parse(argc, argv);
365 /* setup signal behavior - SIGINT */
366 sigemptyset(&act.sa_mask);
367 act.sa_flags = 0;
368 act.sa_handler = catch_sigint;
369 if (sigaction(SIGINT, &act, NULL) < 0) {
370 perror("signal");
371 exit(EXIT_FAILURE);
372 }
374 /* setup signal behavior - SIGTERM */
375 sigemptyset(&act.sa_mask);
376 act.sa_flags = 0;
377 act.sa_handler = catch_sigint;
378 if (sigaction(SIGTERM, &act, NULL) < 0) {
379 perror("sigaction()");
380 exit(EXIT_FAILURE);
381 }
383 /* setup signal behavior - SIGCONT */
384 sigemptyset(&act.sa_mask);
385 act.sa_flags = 0;
386 act.sa_handler = catch_sigcont;
387 if (sigaction(SIGCONT, &act, NULL) < 0) {
388 perror("sigaction(SIGCONT)");
389 exit(EXIT_FAILURE);
390 }
392 /* setup signal behaviour - SIGHUP*/
393 sigemptyset(&act.sa_mask);
394 act.sa_flags = 0;
395 act.sa_handler = catch_sigint;
396 if (sigaction(SIGHUP, &act, NULL) < 0) {
397 perror("sigaction(SIGHUP)");
398 exit(EXIT_FAILURE);
399 }
401 /* setup SIGWINCH */
403 act.sa_handler = catch_sigwinch;
404 if (sigaction(SIGWINCH, &act, NULL) < 0) {
405 perror("sigaction(SIGWINCH)");
406 exit(EXIT_FAILURE);
407 }
409 /* ignore SIGPIPE */
411 act.sa_flags = SA_RESTART;
412 act.sa_handler = SIG_IGN;
413 if (sigaction(SIGWINCH, &act, NULL) < 0) {
414 perror("sigaction(SIGWINCH)");
415 exit(EXIT_FAILURE);
416 }
418 ncu_init();
420 #ifdef ENABLE_LYRICS_SCREEN
421 lyrics_init();
422 #endif
424 /* create mpdclient instance */
425 mpd = mpdclient_new();
426 mpdclient_install_error_callback(mpd, error_callback);
428 /* initialize curses */
429 screen_init(mpd);
431 /* the main loop */
432 main_loop = g_main_loop_new(NULL, FALSE);
434 /* watch out for keyboard input */
435 keyboard_channel = g_io_channel_unix_new(STDIN_FILENO);
436 g_io_add_watch(keyboard_channel, G_IO_IN, keyboard_event, NULL);
438 /* attempt to connect */
439 reconnect_source_id = g_timeout_add(1, timer_reconnect, NULL);
441 update_source_id = g_timeout_add((guint)(MPD_UPDATE_TIME * 1000),
442 timer_mpd_update,
443 GINT_TO_POINTER(TRUE));
444 g_timeout_add(10000, timer_check_key_bindings, NULL);
445 idle_source_id = g_timeout_add(idle_interval, timer_idle, NULL);
447 g_main_loop_run(main_loop);
449 /* cleanup */
451 g_main_loop_unref(main_loop);
452 g_io_channel_unref(keyboard_channel);
454 exit_and_cleanup();
455 ncu_deinit();
457 return 0;
458 }