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 float MPD_UPDATE_TIME = 0.5;
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 }
111 }
113 static void
114 exit_and_cleanup(void)
115 {
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);
130 }
132 static void
133 catch_sigint(mpd_unused int sig)
134 {
135 g_main_loop_quit(main_loop);
136 }
139 static void
140 catch_sigcont(mpd_unused int sig)
141 {
142 #ifdef ENABLE_RAW_MODE
143 reset_prog_mode(); /* restore tty modes */
144 refresh();
145 #endif
146 screen_resize();
147 }
149 void
150 sigstop(void)
151 {
152 def_prog_mode(); /* save the tty modes */
153 endwin(); /* end curses mode temporarily */
154 kill(0, SIGSTOP); /* issue SIGSTOP */
155 }
157 static guint timer_sigwinch_id;
159 static gboolean
160 timer_sigwinch(mpd_unused gpointer data)
161 {
162 /* the following causes the screen to flicker. There might be
163 better solutions, but I believe it isn't all that
164 important. */
166 endwin();
167 refresh();
168 screen_resize();
170 return FALSE;
171 }
173 static void
174 catch_sigwinch(mpd_unused int sig)
175 {
176 if (timer_sigwinch_id != 0)
177 g_source_remove(timer_sigwinch_id);
179 timer_sigwinch_id = g_timeout_add(100, timer_sigwinch, NULL);
180 }
182 static gboolean
183 timer_mpd_update(gpointer data);
185 /**
186 * This timer is installed when the connection to the MPD server is
187 * broken. It tries to recover by reconnecting periodically.
188 */
189 static gboolean
190 timer_reconnect(mpd_unused gpointer data)
191 {
192 int ret;
194 if (connected)
195 return FALSE;
197 screen_status_printf(_("Connecting to %s... [Press %s to abort]"),
198 options.host, get_key_names(CMD_QUIT,0) );
199 doupdate();
201 mpdclient_disconnect(mpd);
202 ret = mpdclient_connect(mpd,
203 options.host, options.port,
204 1.5,
205 options.password);
206 if (ret != 0) {
207 /* try again in 5 seconds */
208 g_timeout_add(5000, timer_reconnect, NULL);
209 return FALSE;
210 }
212 /* quit if mpd is pre 0.11.0 - song id not supported by mpd */
213 if (MPD_VERSION_LT(mpd, 0, 11, 0)) {
214 screen_status_printf(_("Error: MPD version %d.%d.%d is to old (0.11.0 needed).\n"),
215 mpd->connection->version[0],
216 mpd->connection->version[1],
217 mpd->connection->version[2]);
218 mpdclient_disconnect(mpd);
219 doupdate();
221 /* try again after 30 seconds */
222 g_timeout_add(30000, timer_reconnect, NULL);
223 return FALSE;
224 }
226 screen_status_printf(_("Connected to %s!"), options.host);
227 doupdate();
229 connected = TRUE;
231 /* update immediately */
232 g_timeout_add(1, timer_mpd_update, GINT_TO_POINTER(FALSE));
234 reconnect_source_id = 0;
235 return FALSE;
237 }
239 static gboolean
240 timer_mpd_update(gpointer data)
241 {
242 if (connected)
243 mpdclient_update(mpd);
244 else if (reconnect_source_id == 0)
245 reconnect_source_id = g_timeout_add(1000, timer_reconnect,
246 NULL);
248 if (options.enable_xterm_title)
249 update_xterm_title();
251 screen_update(mpd);
253 return GPOINTER_TO_INT(data);
254 }
256 /**
257 * This idle timer is invoked when the user hasn't typed a key for
258 * 500ms. It is used for delayed seeking.
259 */
260 static gboolean
261 timer_idle(mpd_unused gpointer data)
262 {
263 screen_idle(mpd);
264 return TRUE;
265 }
267 static gboolean
268 keyboard_event(mpd_unused GIOChannel *source,
269 mpd_unused GIOCondition condition, mpd_unused gpointer data)
270 {
271 command_t cmd;
273 /* remove the idle timeout; add it later with fresh interval */
274 g_source_remove(idle_source_id);
276 if ((cmd=get_keyboard_command()) != CMD_NONE) {
277 if (cmd == CMD_QUIT) {
278 g_main_loop_quit(main_loop);
279 return FALSE;
280 }
282 screen_cmd(mpd, cmd);
284 if (cmd == CMD_VOLUME_UP || cmd == CMD_VOLUME_DOWN) {
285 /* make sure we dont update the volume yet */
286 g_source_remove(update_source_id);
287 update_source_id = g_timeout_add((guint)(MPD_UPDATE_TIME * 1000),
288 timer_mpd_update,
289 GINT_TO_POINTER(TRUE));
290 }
291 }
293 screen_update(mpd);
295 idle_source_id = g_timeout_add(idle_interval, timer_idle, NULL);
296 return TRUE;
297 }
299 /**
300 * Check the configured key bindings for errors, and display a status
301 * message every 10 seconds.
302 */
303 static gboolean
304 timer_check_key_bindings(mpd_unused gpointer data)
305 {
306 char buf[256];
307 gboolean key_error;
309 key_error = check_key_bindings(NULL, buf, sizeof(buf));
310 if (!key_error)
311 /* no error: disable this timer for the rest of this
312 process */
313 return FALSE;
315 screen_status_printf("%s", buf);
316 doupdate();
317 return TRUE;
318 }
320 int
321 main(int argc, const char *argv[])
322 {
323 struct sigaction act;
324 #ifdef HAVE_LOCALE_H
325 const char *charset = NULL;
326 #endif
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 = charset_init();
339 /* initialize i18n support */
340 #ifdef ENABLE_NLS
341 setlocale(LC_MESSAGES, "");
342 bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
343 bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
344 textdomain(GETTEXT_PACKAGE);
345 #endif
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;
456 }