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 "support.h"
25 #include "options.h"
26 #include "conf.h"
27 #include "command.h"
28 #include "lyrics.h"
29 #include "ncu.h"
30 #include "screen.h"
31 #include "screen_utils.h"
32 #include "strfsong.h"
33 #include "gcc.h"
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <signal.h>
38 #include <string.h>
40 #define BUFSIZE 1024
42 static const guint idle_interval = 500;
44 static mpdclient_t *mpd = NULL;
45 static gboolean connected = FALSE;
46 static GMainLoop *main_loop;
47 static guint reconnect_source_id, idle_source_id, update_source_id;
49 static const gchar *
50 error_msg(const gchar *msg)
51 {
52 gchar *p;
54 if ((p = strchr(msg, '}')) == NULL)
55 return msg;
57 while (p && *p && (*p=='}' || *p==' '))
58 p++;
60 return p;
61 }
63 static void
64 error_callback(mpd_unused mpdclient_t *c, gint error, const gchar *msg)
65 {
66 error = error & 0xFF;
67 D("Error [%d:%d]> \"%s\"\n", error, GET_ACK_ERROR_CODE(error), msg);
68 switch (error) {
69 case MPD_ERROR_CONNPORT:
70 case MPD_ERROR_NORESPONSE:
71 break;
72 case MPD_ERROR_ACK:
73 screen_status_printf("%s", error_msg(msg));
74 screen_bell();
75 break;
76 default:
77 screen_status_printf("%s", msg);
78 screen_bell();
79 doupdate();
80 connected = FALSE;
81 }
82 }
84 static void
85 update_xterm_title(void)
86 {
87 static char title[BUFSIZE];
88 char tmp[BUFSIZE];
89 mpd_Status *status = NULL;
90 mpd_Song *song = NULL;
92 if (mpd) {
93 status = mpd->status;
94 song = mpd->song;
95 }
97 if (options.xterm_title_format && status && song &&
98 IS_PLAYING(status->state))
99 strfsong(tmp, BUFSIZE, options.xterm_title_format, song);
100 else
101 g_strlcpy(tmp, PACKAGE " version " VERSION, BUFSIZE);
103 if (strncmp(title, tmp, BUFSIZE)) {
104 g_strlcpy(title, tmp, BUFSIZE);
105 set_xterm_title("%s", title);
106 }
107 }
109 static void
110 exit_and_cleanup(void)
111 {
112 screen_exit();
113 set_xterm_title("");
114 printf("\n");
116 if (mpd) {
117 mpdclient_disconnect(mpd);
118 mpdclient_free(mpd);
119 }
121 g_free(options.host);
122 g_free(options.password);
123 g_free(options.list_format);
124 g_free(options.status_format);
125 g_free(options.scroll_sep);
126 }
128 static void
129 catch_sigint(mpd_unused int sig)
130 {
131 g_main_loop_quit(main_loop);
132 }
135 static void
136 catch_sigcont(mpd_unused int sig)
137 {
138 D("catch_sigcont()\n");
139 #ifdef ENABLE_RAW_MODE
140 reset_prog_mode(); /* restore tty modes */
141 refresh();
142 #endif
143 screen_resize();
144 }
146 void
147 sigstop(void)
148 {
149 def_prog_mode(); /* save the tty modes */
150 endwin(); /* end curses mode temporarily */
151 kill(0, SIGSTOP); /* issue SIGSTOP */
152 }
154 static guint timer_sigwinch_id;
156 static gboolean
157 timer_sigwinch(mpd_unused gpointer data)
158 {
159 /* the following causes the screen to flicker. There might be
160 better solutions, but I believe it isn't all that
161 important. */
163 endwin();
164 refresh();
165 screen_resize();
167 return FALSE;
168 }
170 static void
171 catch_sigwinch(mpd_unused int sig)
172 {
173 if (timer_sigwinch_id != 0)
174 g_source_remove(timer_sigwinch_id);
176 timer_sigwinch_id = g_timeout_add(100, timer_sigwinch, NULL);
177 }
179 #ifndef NDEBUG
180 void
181 D(const char *format, ...)
182 {
183 if( options.debug )
184 {
185 gchar *msg;
186 va_list ap;
188 va_start(ap,format);
189 msg = g_strdup_vprintf(format,ap);
190 va_end(ap);
191 fprintf(stderr, "%s", msg);
192 g_free(msg);
193 }
194 }
195 #endif
197 static gboolean
198 timer_mpd_update(gpointer data);
200 /**
201 * This timer is installed when the connection to the MPD server is
202 * broken. It tries to recover by reconnecting periodically.
203 */
204 static gboolean
205 timer_reconnect(mpd_unused gpointer data)
206 {
207 int ret;
209 if (connected)
210 return FALSE;
212 screen_status_printf(_("Connecting to %s... [Press %s to abort]"),
213 options.host, get_key_names(CMD_QUIT,0) );
214 doupdate();
216 mpdclient_disconnect(mpd);
217 ret = mpdclient_connect(mpd,
218 options.host, options.port,
219 1.5,
220 options.password);
221 if (ret != 0) {
222 /* try again in 5 seconds */
223 g_timeout_add(5000, timer_reconnect, NULL);
224 return FALSE;
225 }
227 /* quit if mpd is pre 0.11.0 - song id not supported by mpd */
228 if (MPD_VERSION_LT(mpd, 0, 11, 0)) {
229 screen_status_printf(_("Error: MPD version %d.%d.%d is to old (0.11.0 needed).\n"),
230 mpd->connection->version[0],
231 mpd->connection->version[1],
232 mpd->connection->version[2]);
233 mpdclient_disconnect(mpd);
234 doupdate();
236 /* try again after 30 seconds */
237 g_timeout_add(30000, timer_reconnect, NULL);
238 return FALSE;
239 }
241 screen_status_printf(_("Connected to %s!"), options.host);
242 doupdate();
244 connected = TRUE;
246 /* update immediately */
247 g_timeout_add(1, timer_mpd_update, GINT_TO_POINTER(FALSE));
249 reconnect_source_id = 0;
250 return FALSE;
252 }
254 static gboolean
255 timer_mpd_update(gpointer data)
256 {
257 if (connected)
258 mpdclient_update(mpd);
259 else if (reconnect_source_id == 0)
260 reconnect_source_id = g_timeout_add(1000, timer_reconnect,
261 NULL);
263 if (options.enable_xterm_title)
264 update_xterm_title();
266 screen_update(mpd);
268 return GPOINTER_TO_INT(data);
269 }
271 /**
272 * This idle timer is invoked when the user hasn't typed a key for
273 * 500ms. It is used for delayed seeking.
274 */
275 static gboolean
276 timer_idle(mpd_unused gpointer data)
277 {
278 screen_idle(mpd);
279 return TRUE;
280 }
282 static gboolean
283 keyboard_event(mpd_unused GIOChannel *source,
284 mpd_unused GIOCondition condition, mpd_unused gpointer data)
285 {
286 command_t cmd;
288 /* remove the idle timeout; add it later with fresh interval */
289 g_source_remove(idle_source_id);
291 if ((cmd=get_keyboard_command()) != CMD_NONE) {
292 if (cmd == CMD_QUIT) {
293 g_main_loop_quit(main_loop);
294 return FALSE;
295 }
297 screen_cmd(mpd, cmd);
299 if (cmd == CMD_VOLUME_UP || cmd == CMD_VOLUME_DOWN) {
300 /* make sure we dont update the volume yet */
301 g_source_remove(update_source_id);
302 update_source_id = g_timeout_add((guint)(MPD_UPDATE_TIME * 1000),
303 timer_mpd_update,
304 GINT_TO_POINTER(TRUE));
305 }
306 }
308 screen_update(mpd);
310 idle_source_id = g_timeout_add(idle_interval, timer_idle, NULL);
311 return TRUE;
312 }
314 /**
315 * Check the configured key bindings for errors, and display a status
316 * message every 10 seconds.
317 */
318 static gboolean
319 timer_check_key_bindings(mpd_unused gpointer data)
320 {
321 char buf[256];
322 gboolean key_error;
324 key_error = check_key_bindings(NULL, buf, sizeof(buf));
325 if (!key_error)
326 /* no error: disable this timer for the rest of this
327 process */
328 return FALSE;
330 screen_status_printf("%s", buf);
331 doupdate();
332 return TRUE;
333 }
335 int
336 main(int argc, const char *argv[])
337 {
338 struct sigaction act;
339 const char *charset = NULL;
340 GIOChannel *keyboard_channel;
342 #ifdef HAVE_LOCALE_H
343 /* time and date formatting */
344 setlocale(LC_TIME,"");
345 /* care about sorting order etc */
346 setlocale(LC_COLLATE,"");
347 /* charset */
348 setlocale(LC_CTYPE,"");
349 /* initialize charset conversions */
350 charset_init(g_get_charset(&charset));
351 D("charset: %s\n", charset);
352 #endif
354 /* initialize i18n support */
355 #ifdef ENABLE_NLS
356 setlocale(LC_MESSAGES, "");
357 bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
358 bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
359 textdomain(GETTEXT_PACKAGE);
360 #endif
362 /* initialize options */
363 options_init();
365 /* parse command line options - 1 pass get configuration files */
366 options_parse(argc, argv);
368 /* read configuration */
369 read_configuration(&options);
371 /* check key bindings */
372 check_key_bindings(NULL, NULL, 0);
374 /* parse command line options - 2 pass */
375 options_parse(argc, argv);
377 /* setup signal behavior - SIGINT */
378 sigemptyset(&act.sa_mask);
379 act.sa_flags = 0;
380 act.sa_handler = catch_sigint;
381 if (sigaction(SIGINT, &act, NULL) < 0) {
382 perror("signal");
383 exit(EXIT_FAILURE);
384 }
386 /* setup signal behavior - SIGTERM */
387 sigemptyset(&act.sa_mask);
388 act.sa_flags = 0;
389 act.sa_handler = catch_sigint;
390 if (sigaction(SIGTERM, &act, NULL) < 0) {
391 perror("sigaction()");
392 exit(EXIT_FAILURE);
393 }
395 /* setup signal behavior - SIGCONT */
396 sigemptyset(&act.sa_mask);
397 act.sa_flags = 0;
398 act.sa_handler = catch_sigcont;
399 if (sigaction(SIGCONT, &act, NULL) < 0) {
400 perror("sigaction(SIGCONT)");
401 exit(EXIT_FAILURE);
402 }
404 /* setup signal behaviour - SIGHUP*/
405 sigemptyset(&act.sa_mask);
406 act.sa_flags = 0;
407 act.sa_handler = catch_sigint;
408 if (sigaction(SIGHUP, &act, NULL) < 0) {
409 perror("sigaction(SIGHUP)");
410 exit(EXIT_FAILURE);
411 }
413 /* setup SIGWINCH */
415 act.sa_handler = catch_sigwinch;
416 if (sigaction(SIGWINCH, &act, NULL) < 0) {
417 perror("sigaction(SIGWINCH)");
418 exit(EXIT_FAILURE);
419 }
421 ncu_init();
423 lyrics_init();
425 /* create mpdclient instance */
426 mpd = mpdclient_new();
427 mpdclient_install_error_callback(mpd, error_callback);
429 /* initialize curses */
430 screen_init(mpd);
432 /* the main loop */
433 main_loop = g_main_loop_new(NULL, FALSE);
435 /* watch out for keyboard input */
436 keyboard_channel = g_io_channel_unix_new(STDIN_FILENO);
437 g_io_add_watch(keyboard_channel, G_IO_IN, keyboard_event, NULL);
439 /* attempt to connect */
440 reconnect_source_id = g_timeout_add(1, timer_reconnect, NULL);
442 update_source_id = g_timeout_add((guint)(MPD_UPDATE_TIME * 1000),
443 timer_mpd_update,
444 GINT_TO_POINTER(TRUE));
445 g_timeout_add(10000, timer_check_key_bindings, NULL);
446 idle_source_id = g_timeout_add(idle_interval, timer_idle, NULL);
448 g_main_loop_run(main_loop);
450 /* cleanup */
452 g_main_loop_unref(main_loop);
453 g_io_channel_unref(keyboard_channel);
455 exit_and_cleanup();
456 ncu_deinit();
458 return 0;
459 }