9497e3b61f80c81d87ea4b0850a47528f79400fa
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 <glib.h>
28 #include "config.h"
29 #include "ncmpc.h"
30 #include "mpdclient.h"
31 #include "support.h"
32 #include "options.h"
33 #include "conf.h"
34 #include "command.h"
35 #include "screen.h"
36 #include "screen_utils.h"
37 #include "strfsong.h"
39 #define BUFSIZE 1024
41 static mpdclient_t *mpd = NULL;
42 static gboolean connected = FALSE;
43 static GTimer *timer = NULL;
45 static gchar *
46 error_msg(gchar *msg)
47 {
48 gchar *p;
50 if( (p=strchr(msg, '}' )) == NULL )
51 return msg;
52 while( p && *p && (*p=='}' || *p==' ') )
53 p++;
55 return p;
56 }
58 static void
59 error_callback(mpdclient_t *c, gint error, gchar *msg)
60 {
61 gint code = GET_ACK_ERROR_CODE(error);
63 error = error & 0xFF;
64 D("Error [%d:%d]> \"%s\"\n", error, code, msg);
65 switch(error)
66 {
67 case MPD_ERROR_CONNPORT:
68 case MPD_ERROR_NORESPONSE:
69 break;
70 case MPD_ERROR_ACK:
71 screen_status_printf("%s", error_msg(msg));
72 screen_bell();
73 break;
74 default:
75 screen_status_printf("%s", msg);
76 screen_bell();
77 doupdate();
78 connected = FALSE;
79 }
80 }
82 static void
83 update_xterm_title(void)
84 {
85 static char title[BUFSIZE];
86 char tmp[BUFSIZE];
87 mpd_Status *status = NULL;
88 mpd_Song *song = NULL;
90 if( mpd )
91 {
92 status = mpd->status;
93 song = mpd->song;
94 }
96 if(options.xterm_title_format && status && song && IS_PLAYING(status->state))
97 {
98 strfsong(tmp, BUFSIZE, options.xterm_title_format, song);
99 }
100 else
101 strncpy(tmp, PACKAGE " version " VERSION, BUFSIZE);
103 if( strcmp(title,tmp) )
104 {
105 strncpy(title, tmp, BUFSIZE);
106 set_xterm_title(title);
107 }
108 }
110 void
111 exit_and_cleanup(void)
112 {
113 screen_exit();
114 set_xterm_title("");
115 printf("\n");
116 if( mpd )
117 {
118 mpdclient_disconnect(mpd);
119 mpd = mpdclient_free(mpd);
120 }
121 g_free(options.host);
122 g_free(options.password);
123 g_free(options.list_format);
124 g_free(options.status_format);
125 if( timer )
126 g_timer_destroy(timer);
127 }
129 void
130 catch_sigint( int sig )
131 {
132 printf("\n%s\n", _("Exiting..."));
133 exit(EXIT_SUCCESS);
134 }
136 void
137 catch_sigcont( int sig )
138 {
139 D("catch_sigcont()\n");
140 screen_resize();
141 }
143 #ifdef DEBUG
144 void
145 D(char *format, ...)
146 {
147 if( options.debug )
148 {
149 gchar *msg;
150 va_list ap;
152 va_start(ap,format);
153 msg = g_strdup_vprintf(format,ap);
154 va_end(ap);
155 fprintf(stderr, "%s", msg);
156 g_free(msg);
157 }
158 }
159 #endif
161 int
162 main(int argc, const char *argv[])
163 {
164 options_t *options;
165 struct sigaction act;
166 const char *charset = NULL;
167 gboolean key_error;
169 #ifdef HAVE_LOCALE_H
170 /* time and date formatting */
171 setlocale(LC_TIME,"");
172 /* charset */
173 setlocale(LC_CTYPE,"");
174 /* initialize charset conversions */
175 charset_init(g_get_charset(&charset));
176 D("charset: %s\n", charset);
177 #endif
179 /* initialize i18n support */
180 #ifdef ENABLE_NLS
181 setlocale(LC_MESSAGES, "");
182 bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
183 bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
184 textdomain(GETTEXT_PACKAGE);
185 #endif
187 /* initialize options */
188 options = options_init();
190 /* parse command line options - 1 pass get configuration files */
191 options_parse(argc, argv);
193 /* read configuration */
194 read_configuration(options);
196 /* check key bindings */
197 key_error = check_key_bindings(NULL, NULL, 0);
199 /* parse command line options - 2 pass */
200 options_parse(argc, argv);
202 /* setup signal behavior - SIGINT */
203 sigemptyset( &act.sa_mask );
204 act.sa_flags = 0;
205 act.sa_handler = catch_sigint;
206 if( sigaction(SIGINT, &act, NULL)<0 )
207 {
208 perror("signal");
209 exit(EXIT_FAILURE);
210 }
211 /* setup signal behavior - SIGTERM */
212 sigemptyset( &act.sa_mask );
213 act.sa_flags = 0;
214 act.sa_handler = catch_sigint;
215 if( sigaction(SIGTERM, &act, NULL)<0 )
216 {
217 perror("sigaction()");
218 exit(EXIT_FAILURE);
219 }
220 /* setup signal behavior - SIGCONT */
221 sigemptyset( &act.sa_mask );
222 act.sa_flags = 0;
223 act.sa_handler = catch_sigcont;
224 if( sigaction(SIGCONT, &act, NULL)<0 )
225 {
226 perror("sigaction(SIGCONT)");
227 exit(EXIT_FAILURE);
228 }
230 /* install exit function */
231 atexit(exit_and_cleanup);
233 /* connect to our music player daemon */
234 mpd = mpdclient_new();
235 if( mpdclient_connect(mpd,
236 options->host,
237 options->port,
238 10.0,
239 options->password) )
240 {
241 exit(EXIT_FAILURE);
242 }
243 connected = TRUE;
244 D("Connected to MPD version %d.%d.%d\n",
245 mpd->connection->version[0],
246 mpd->connection->version[1],
247 mpd->connection->version[2]);
249 /* quit if mpd is pre 0.11.0 - song id not supported by mpd */
250 if( MPD_VERSION_LT(mpd, 0,11,0) )
251 {
252 fprintf(stderr,
253 _("Error: MPD version %d.%d.%d is to old (0.11.0 needed).\n"),
254 mpd->connection->version[0],
255 mpd->connection->version[1],
256 mpd->connection->version[2]);
257 exit(EXIT_FAILURE);
258 }
260 /* initialize curses */
261 screen_init(mpd);
263 /* install error callback function */
264 mpdclient_install_error_callback(mpd, error_callback);
266 /* initialize timer */
267 timer = g_timer_new();
269 connected = TRUE;
270 while( connected || options->reconnect )
271 {
272 static gdouble t = G_MAXDOUBLE;
274 if( key_error )
275 {
276 char buf[BUFSIZE];
278 key_error=check_key_bindings(NULL, buf, BUFSIZE);
279 screen_status_printf("%s", buf);
280 }
282 if( connected && (t>=MPD_UPDATE_TIME || mpd->need_update) )
283 {
284 mpdclient_update(mpd);
285 g_timer_start(timer);
286 }
288 if( connected )
289 {
290 command_t cmd;
292 screen_update(mpd);
293 if( (cmd=get_keyboard_command()) != CMD_NONE )
294 {
295 screen_cmd(mpd, cmd);
296 if( cmd==CMD_VOLUME_UP || cmd==CMD_VOLUME_DOWN)
297 /* make shure we dont update the volume yet */
298 g_timer_start(timer);
299 }
300 else
301 screen_idle(mpd);
302 }
303 else if( options->reconnect )
304 {
305 screen_status_printf(_("Connecting to %s... [Press %s to abort]"),
306 options->host, get_key_names(CMD_QUIT,0) );
308 if( get_keyboard_command_with_timeout(MPD_RECONNECT_TIME)==CMD_QUIT)
309 exit(EXIT_SUCCESS);
311 if( mpdclient_connect(mpd,
312 options->host,
313 options->port,
314 1.5,
315 options->password) == 0 )
316 {
317 screen_status_printf(_("Connected to %s!"), options->host);
318 connected = TRUE;
319 }
320 doupdate();
321 }
322 if( options->enable_xterm_title )
323 update_xterm_title();
324 t = g_timer_elapsed(timer, NULL);
325 }
326 exit(EXIT_FAILURE);
327 }