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 <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <signal.h>
25 #include <string.h>
26 #include <ncurses.h>
27 #include <glib.h>
29 #include "config.h"
30 #include "ncmpc.h"
31 #include "mpdclient.h"
32 #include "support.h"
33 #include "options.h"
34 #include "conf.h"
35 #include "command.h"
36 #include "screen.h"
37 #include "screen_utils.h"
38 #include "strfsong.h"
39 #include "splash.h"
41 #define BUFSIZE 1024
43 static mpdclient_t *mpd = NULL;
44 static gboolean connected = FALSE;
45 static GTimer *timer = NULL;
47 static gchar *
48 error_msg(gchar *msg)
49 {
50 gchar *p;
52 if( (p=strchr(msg, '}' )) == NULL )
53 return msg;
54 while( p && *p && (*p=='}' || *p==' ') )
55 p++;
57 return p;
58 }
60 static void
61 error_callback(mpdclient_t *c, gint error, gchar *msg)
62 {
63 gint code = GET_ACK_ERROR_CODE(error);
65 error = error & 0xFF;
66 D("Error [%d:%d]> \"%s\"\n", error, code, msg);
67 switch(error)
68 {
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 {
94 status = mpd->status;
95 song = mpd->song;
96 }
98 if(options.xterm_title_format && status && song && IS_PLAYING(status->state))
99 {
100 strfsong(tmp, BUFSIZE, options.xterm_title_format, song);
101 }
102 else
103 g_strlcpy(tmp, PACKAGE " version " VERSION, BUFSIZE);
105 if( strncmp(title,tmp,BUFSIZE) )
106 {
107 g_strlcpy(title, tmp, BUFSIZE);
108 set_xterm_title("%s", title);
109 }
110 }
112 void
113 exit_and_cleanup(void)
114 {
115 screen_exit();
116 set_xterm_title("");
117 printf("\n");
118 if( mpd )
119 {
120 mpdclient_disconnect(mpd);
121 mpd = mpdclient_free(mpd);
122 }
123 g_free(options.host);
124 g_free(options.password);
125 g_free(options.list_format);
126 g_free(options.status_format);
127 if( timer )
128 g_timer_destroy(timer);
129 }
131 void
132 catch_sigint( int sig )
133 {
134 printf("\n%s\n", _("Exiting..."));
135 exit(EXIT_SUCCESS);
136 }
138 void
139 catch_sighup( int sig)
140 {
141 printf("\n%s\n", _("Exiting..."));
142 exit(EXIT_SUCCESS);
143 }
145 void
146 catch_sigcont( int sig )
147 {
148 D("catch_sigcont()\n");
149 #ifdef ENABLE_RAW_MODE
150 reset_prog_mode(); /* restore tty modes */
151 refresh();
152 #endif
153 screen_resize();
154 }
156 void
157 sigstop(void)
158 {
159 def_prog_mode(); /* save the tty modes */
160 endwin(); /* end curses mode temporarily */
161 kill(0, SIGSTOP); /* issue SIGSTOP */
162 }
164 #ifdef DEBUG
165 void
166 D(char *format, ...)
167 {
168 if( options.debug )
169 {
170 gchar *msg;
171 va_list ap;
173 va_start(ap,format);
174 msg = g_strdup_vprintf(format,ap);
175 va_end(ap);
176 fprintf(stderr, "%s", msg);
177 g_free(msg);
178 }
179 }
180 #endif
182 int
183 main(int argc, const char *argv[])
184 {
185 options_t *options;
186 struct sigaction act;
187 const char *charset = NULL;
188 gboolean key_error;
190 #ifdef HAVE_LOCALE_H
191 /* time and date formatting */
192 setlocale(LC_TIME,"");
193 /* charset */
194 setlocale(LC_CTYPE,"");
195 /* initialize charset conversions */
196 charset_init(g_get_charset(&charset));
197 D("charset: %s\n", charset);
198 #endif
200 /* initialize i18n support */
201 #ifdef ENABLE_NLS
202 setlocale(LC_MESSAGES, "");
203 bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
204 bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
205 textdomain(GETTEXT_PACKAGE);
206 #endif
208 /* initialize options */
209 options = options_init();
211 /* parse command line options - 1 pass get configuration files */
212 options_parse(argc, argv);
214 /* read configuration */
215 read_configuration(options);
217 /* check key bindings */
218 key_error = check_key_bindings(NULL, NULL, 0);
220 /* parse command line options - 2 pass */
221 options_parse(argc, argv);
223 /* setup signal behavior - SIGINT */
224 sigemptyset( &act.sa_mask );
225 act.sa_flags = 0;
226 act.sa_handler = catch_sigint;
227 if( sigaction(SIGINT, &act, NULL)<0 )
228 {
229 perror("signal");
230 exit(EXIT_FAILURE);
231 }
232 /* setup signal behavior - SIGTERM */
233 sigemptyset( &act.sa_mask );
234 act.sa_flags = 0;
235 act.sa_handler = catch_sigint;
236 if( sigaction(SIGTERM, &act, NULL)<0 )
237 {
238 perror("sigaction()");
239 exit(EXIT_FAILURE);
240 }
241 /* setup signal behavior - SIGCONT */
242 sigemptyset( &act.sa_mask );
243 act.sa_flags = 0;
244 act.sa_handler = catch_sigcont;
245 if( sigaction(SIGCONT, &act, NULL)<0 )
246 {
247 perror("sigaction(SIGCONT)");
248 exit(EXIT_FAILURE);
249 }
251 /* setup signal behaviour - SIGHUP*/
252 sigemptyset( &act.sa_mask );
253 act.sa_flags = 0;
254 act.sa_handler = catch_sigint;
255 if( sigaction(SIGHUP, &act, NULL)<0 )
256 {
257 perror("sigaction(SIGHUP)");
258 exit(EXIT_FAILURE);
259 }
261 /* install exit function */
262 atexit(exit_and_cleanup);
264 ncurses_init();
265 if(options->show_splash == TRUE) draw_splash();
267 /* connect to our music player daemon */
268 mpd = mpdclient_new();
270 if( mpdclient_connect(mpd,
271 options->host,
272 options->port,
273 10.0,
274 options->password) )
275 {
276 exit(EXIT_FAILURE);
277 }
279 /* if no password is used, but the mpd wants one, the connection
280 might be established but no status information is avaiable */
281 mpdclient_update(mpd);
282 if(!mpd->status)
283 {
284 screen_auth(mpd);
285 }
286 if(!mpd->status) exit(EXIT_FAILURE);
288 connected = TRUE;
289 D("Connected to MPD version %d.%d.%d\n",
290 mpd->connection->version[0],
291 mpd->connection->version[1],
292 mpd->connection->version[2]);
294 /* quit if mpd is pre 0.11.0 - song id not supported by mpd */
295 if( MPD_VERSION_LT(mpd, 0,11,0) )
296 {
297 fprintf(stderr,
298 _("Error: MPD version %d.%d.%d is to old (0.11.0 needed).\n"),
299 mpd->connection->version[0],
300 mpd->connection->version[1],
301 mpd->connection->version[2]);
302 exit(EXIT_FAILURE);
303 }
305 /* initialize curses */
306 screen_init(mpd);
307 /* install error callback function */
308 mpdclient_install_error_callback(mpd, error_callback);
310 /* initialize timer */
311 timer = g_timer_new();
313 connected = TRUE;
314 while( connected || options->reconnect )
315 {
316 static gdouble t = G_MAXDOUBLE;
318 if( key_error )
319 {
320 char buf[BUFSIZE];
322 key_error=check_key_bindings(NULL, buf, BUFSIZE);
323 screen_status_printf("%s", buf);
324 }
326 if( connected && (t>=MPD_UPDATE_TIME || mpd->need_update) )
327 {
328 mpdclient_update(mpd);
329 g_timer_start(timer);
330 }
332 if( connected )
333 {
334 command_t cmd;
336 screen_update(mpd);
337 if( (cmd=get_keyboard_command()) != CMD_NONE )
338 {
339 screen_cmd(mpd, cmd);
340 if( cmd==CMD_VOLUME_UP || cmd==CMD_VOLUME_DOWN)
341 /* make shure we dont update the volume yet */
342 g_timer_start(timer);
343 }
344 else
345 screen_idle(mpd);
346 }
347 else if( options->reconnect )
348 {
349 screen_status_printf(_("Connecting to %s... [Press %s to abort]"),
350 options->host, get_key_names(CMD_QUIT,0) );
352 if( get_keyboard_command_with_timeout(MPD_RECONNECT_TIME)==CMD_QUIT)
353 exit(EXIT_SUCCESS);
355 if( mpdclient_connect(mpd,
356 options->host,
357 options->port,
358 1.5,
359 options->password) == 0 )
360 {
361 screen_status_printf(_("Connected to %s!"), options->host);
362 connected = TRUE;
363 }
364 doupdate();
365 }
366 if( options->enable_xterm_title )
367 update_xterm_title();
368 t = g_timer_elapsed(timer, NULL);
369 }
370 exit(EXIT_FAILURE);
371 }