3dd7b6b94b338948b149fb0afe37ae43d39112fd
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"
40 #define BUFSIZE 1024
42 static mpdclient_t *mpd = NULL;
43 static gboolean connected = FALSE;
44 static GTimer *timer = NULL;
46 static gchar *
47 error_msg(gchar *msg)
48 {
49 gchar *p;
51 if( (p=strchr(msg, '}' )) == NULL )
52 return msg;
53 while( p && *p && (*p=='}' || *p==' ') )
54 p++;
56 return p;
57 }
59 static void
60 error_callback(mpdclient_t *c, gint error, gchar *msg)
61 {
62 gint code = GET_ACK_ERROR_CODE(error);
64 error = error & 0xFF;
65 D("Error [%d:%d]> \"%s\"\n", error, code, msg);
66 switch(error)
67 {
68 case MPD_ERROR_CONNPORT:
69 case MPD_ERROR_NORESPONSE:
70 break;
71 case MPD_ERROR_ACK:
72 screen_status_printf("%s", error_msg(msg));
73 screen_bell();
74 break;
75 default:
76 screen_status_printf("%s", msg);
77 screen_bell();
78 doupdate();
79 connected = FALSE;
80 }
81 }
83 static void
84 update_xterm_title(void)
85 {
86 static char title[BUFSIZE];
87 char tmp[BUFSIZE];
88 mpd_Status *status = NULL;
89 mpd_Song *song = NULL;
91 if( mpd )
92 {
93 status = mpd->status;
94 song = mpd->song;
95 }
97 if(options.xterm_title_format && status && song && IS_PLAYING(status->state))
98 {
99 strfsong(tmp, BUFSIZE, options.xterm_title_format, song);
100 }
101 else
102 g_strlcpy(tmp, PACKAGE " version " VERSION, BUFSIZE);
104 if( strncmp(title,tmp,BUFSIZE) )
105 {
106 g_strlcpy(title, tmp, BUFSIZE);
107 set_xterm_title("%s", title);
108 }
109 }
111 void
112 exit_and_cleanup(void)
113 {
114 screen_exit();
115 set_xterm_title("");
116 printf("\n");
117 if( mpd )
118 {
119 mpdclient_disconnect(mpd);
120 mpd = mpdclient_free(mpd);
121 }
122 g_free(options.host);
123 g_free(options.password);
124 g_free(options.list_format);
125 g_free(options.status_format);
126 if( timer )
127 g_timer_destroy(timer);
128 }
130 void
131 catch_sigint( int sig )
132 {
133 printf("\n%s\n", _("Exiting..."));
134 exit(EXIT_SUCCESS);
135 }
137 void
138 catch_sigcont( int sig )
139 {
140 D("catch_sigcont()\n");
141 #ifdef ENABLE_RAW_MODE
142 reset_prog_mode(); /* restore tty modes */
143 refresh();
144 #endif
145 screen_resize();
146 }
148 void
149 sigstop(void)
150 {
151 def_prog_mode(); /* save the tty modes */
152 endwin(); /* end curses mode temporarily */
153 kill(0, SIGSTOP); /* issue SIGSTOP */
154 }
156 #ifdef DEBUG
157 void
158 D(char *format, ...)
159 {
160 if( options.debug )
161 {
162 gchar *msg;
163 va_list ap;
165 va_start(ap,format);
166 msg = g_strdup_vprintf(format,ap);
167 va_end(ap);
168 fprintf(stderr, "%s", msg);
169 g_free(msg);
170 }
171 }
172 #endif
174 int
175 main(int argc, const char *argv[])
176 {
177 options_t *options;
178 struct sigaction act;
179 const char *charset = NULL;
180 gboolean key_error;
182 #ifdef HAVE_LOCALE_H
183 /* time and date formatting */
184 setlocale(LC_TIME,"");
185 /* charset */
186 setlocale(LC_CTYPE,"");
187 /* initialize charset conversions */
188 charset_init(g_get_charset(&charset));
189 D("charset: %s\n", charset);
190 #endif
192 /* initialize i18n support */
193 #ifdef ENABLE_NLS
194 setlocale(LC_MESSAGES, "");
195 bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
196 bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
197 textdomain(GETTEXT_PACKAGE);
198 #endif
200 /* initialize options */
201 options = options_init();
203 /* parse command line options - 1 pass get configuration files */
204 options_parse(argc, argv);
206 /* read configuration */
207 read_configuration(options);
209 /* check key bindings */
210 key_error = check_key_bindings(NULL, NULL, 0);
212 /* parse command line options - 2 pass */
213 options_parse(argc, argv);
215 /* setup signal behavior - SIGINT */
216 sigemptyset( &act.sa_mask );
217 act.sa_flags = 0;
218 act.sa_handler = catch_sigint;
219 if( sigaction(SIGINT, &act, NULL)<0 )
220 {
221 perror("signal");
222 exit(EXIT_FAILURE);
223 }
224 /* setup signal behavior - SIGTERM */
225 sigemptyset( &act.sa_mask );
226 act.sa_flags = 0;
227 act.sa_handler = catch_sigint;
228 if( sigaction(SIGTERM, &act, NULL)<0 )
229 {
230 perror("sigaction()");
231 exit(EXIT_FAILURE);
232 }
233 /* setup signal behavior - SIGCONT */
234 sigemptyset( &act.sa_mask );
235 act.sa_flags = 0;
236 act.sa_handler = catch_sigcont;
237 if( sigaction(SIGCONT, &act, NULL)<0 )
238 {
239 perror("sigaction(SIGCONT)");
240 exit(EXIT_FAILURE);
241 }
243 /* install exit function */
244 atexit(exit_and_cleanup);
246 /* connect to our music player daemon */
247 mpd = mpdclient_new();
248 if( mpdclient_connect(mpd,
249 options->host,
250 options->port,
251 10.0,
252 options->password) )
253 {
254 exit(EXIT_FAILURE);
255 }
257 /* if no password is used, but the mpd wants one, the connection
258 might be established but no status information is avaiable */
259 mpdclient_update(mpd);
260 if(!mpd->status)
261 exit(EXIT_FAILURE);
263 connected = TRUE;
264 D("Connected to MPD version %d.%d.%d\n",
265 mpd->connection->version[0],
266 mpd->connection->version[1],
267 mpd->connection->version[2]);
269 /* quit if mpd is pre 0.11.0 - song id not supported by mpd */
270 if( MPD_VERSION_LT(mpd, 0,11,0) )
271 {
272 fprintf(stderr,
273 _("Error: MPD version %d.%d.%d is to old (0.11.0 needed).\n"),
274 mpd->connection->version[0],
275 mpd->connection->version[1],
276 mpd->connection->version[2]);
277 exit(EXIT_FAILURE);
278 }
280 /* initialize curses */
281 screen_init(mpd);
283 /* install error callback function */
284 mpdclient_install_error_callback(mpd, error_callback);
286 /* initialize timer */
287 timer = g_timer_new();
289 connected = TRUE;
290 while( connected || options->reconnect )
291 {
292 static gdouble t = G_MAXDOUBLE;
294 if( key_error )
295 {
296 char buf[BUFSIZE];
298 key_error=check_key_bindings(NULL, buf, BUFSIZE);
299 screen_status_printf("%s", buf);
300 }
302 if( connected && (t>=MPD_UPDATE_TIME || mpd->need_update) )
303 {
304 mpdclient_update(mpd);
305 g_timer_start(timer);
306 }
308 if( connected )
309 {
310 command_t cmd;
312 screen_update(mpd);
313 if( (cmd=get_keyboard_command()) != CMD_NONE )
314 {
315 screen_cmd(mpd, cmd);
316 if( cmd==CMD_VOLUME_UP || cmd==CMD_VOLUME_DOWN)
317 /* make shure we dont update the volume yet */
318 g_timer_start(timer);
319 }
320 else
321 screen_idle(mpd);
322 }
323 else if( options->reconnect )
324 {
325 screen_status_printf(_("Connecting to %s... [Press %s to abort]"),
326 options->host, get_key_names(CMD_QUIT,0) );
328 if( get_keyboard_command_with_timeout(MPD_RECONNECT_TIME)==CMD_QUIT)
329 exit(EXIT_SUCCESS);
331 if( mpdclient_connect(mpd,
332 options->host,
333 options->port,
334 1.5,
335 options->password) == 0 )
336 {
337 screen_status_printf(_("Connected to %s!"), options->host);
338 connected = TRUE;
339 }
340 doupdate();
341 }
342 if( options->enable_xterm_title )
343 update_xterm_title();
344 t = g_timer_elapsed(timer, NULL);
345 }
346 exit(EXIT_FAILURE);
347 }