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"
38 #define BUFSIZE 256
40 static mpdclient_t *mpd = NULL;
41 static gboolean connected = FALSE;
42 static GTimer *timer = NULL;
44 static gchar *
45 error_msg(gchar *msg)
46 {
47 gchar *p;
49 if( (p=strchr(msg, '}' )) == NULL )
50 return msg;
51 while( p && *p && (*p=='}' || *p==' ') )
52 p++;
54 return p;
55 }
57 static void
58 error_callback(mpdclient_t *c, gint error, gchar *msg)
59 {
60 gint code = GET_ACK_ERROR_CODE(error);
62 error = error & 0xFF;
63 D("Error [%d:%d]> \"%s\"\n", error, code, msg);
64 switch(error)
65 {
66 case MPD_ERROR_CONNPORT:
67 case MPD_ERROR_NORESPONSE:
68 break;
69 case MPD_ERROR_ACK:
70 screen_status_printf("%s", error_msg(msg));
71 screen_bell();
72 break;
73 default:
74 screen_status_printf("%s", msg);
75 screen_bell();
76 doupdate();
77 connected = FALSE;
78 }
79 }
81 void
82 exit_and_cleanup(void)
83 {
84 screen_exit();
85 set_xterm_title("");
86 printf("\n");
87 if( mpd )
88 {
89 mpdclient_disconnect(mpd);
90 mpd = mpdclient_free(mpd);
91 }
92 g_free(options.host);
93 g_free(options.password);
94 g_free(options.list_format);
95 g_free(options.status_format);
96 if( timer )
97 g_timer_destroy(timer);
98 }
100 void
101 catch_sigint( int sig )
102 {
103 printf("\n%s\n", _("Exiting..."));
104 exit(EXIT_SUCCESS);
105 }
107 int
108 main(int argc, const char *argv[])
109 {
110 options_t *options;
111 struct sigaction act;
112 const char *charset = NULL;
113 gboolean key_error;
115 #ifdef HAVE_LOCALE_H
116 /* time and date formatting */
117 setlocale(LC_TIME,"");
118 /* charset */
119 setlocale(LC_CTYPE,"");
120 /* initialize charset conversions */
121 charset_init(g_get_charset(&charset));
122 D("charset: %s\n", charset);
123 #endif
125 /* initialize i18n support */
126 #ifdef ENABLE_NLS
127 setlocale(LC_MESSAGES, "");
128 bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
129 bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
130 textdomain(GETTEXT_PACKAGE);
131 #endif
133 /* initialize options */
134 options = options_init();
136 /* parse command line options - 1 pass get configuration files */
137 options_parse(argc, argv);
139 /* read configuration */
140 read_configuration(options);
142 /* check key bindings */
143 key_error = check_key_bindings(NULL, 0);
145 /* parse command line options - 2 pass */
146 options_parse(argc, argv);
148 /* setup signal behavior - SIGINT */
149 sigemptyset( &act.sa_mask );
150 act.sa_flags = 0;
151 act.sa_handler = catch_sigint;
152 if( sigaction( SIGINT, &act, NULL )<0 )
153 {
154 perror("signal");
155 exit(EXIT_FAILURE);
156 }
157 /* setup signal behavior - SIGTERM */
158 sigemptyset( &act.sa_mask );
159 act.sa_flags = 0;
160 act.sa_handler = catch_sigint;
161 if( sigaction( SIGTERM, &act, NULL )<0 )
162 {
163 perror("sigaction()");
164 exit(EXIT_FAILURE);
165 }
167 /* set xterm title */
168 set_xterm_title(PACKAGE " version " VERSION);
170 /* install exit function */
171 atexit(exit_and_cleanup);
173 /* connect to our music player daemon */
174 mpd = mpdclient_new();
175 if( mpdclient_connect(mpd,
176 options->host,
177 options->port,
178 10.0,
179 options->password) )
180 {
181 exit(EXIT_FAILURE);
182 }
183 connected = TRUE;
184 D("Connected to MPD version %d.%d.%d\n",
185 mpd->connection->version[0],
186 mpd->connection->version[1],
187 mpd->connection->version[2]);
189 /* quit if mpd is pre 0.11.0 - song id not supported by mpd */
190 if( MPD_VERSION_LT(mpd, 0,11,0) )
191 {
192 fprintf(stderr,
193 _("Error: MPD version %d.%d.%d is to old (0.11.0 needed).\n"),
194 mpd->connection->version[0],
195 mpd->connection->version[1],
196 mpd->connection->version[2]);
197 exit(EXIT_FAILURE);
198 }
200 /* initialize curses */
201 screen_init(mpd);
203 /* install error callback function */
204 mpdclient_install_error_callback(mpd, error_callback);
206 /* initialize timer */
207 timer = g_timer_new();
209 connected = TRUE;
210 while( connected || options->reconnect )
211 {
212 static gdouble t = G_MAXDOUBLE;
214 if( key_error )
215 {
216 char buf[BUFSIZE];
218 key_error=check_key_bindings(buf, BUFSIZE);
219 screen_status_printf("%s", buf);
220 }
222 if( connected && (t>=MPD_UPDATE_TIME || mpd->need_update) )
223 {
224 mpdclient_update(mpd);
225 g_timer_start(timer);
226 }
228 if( connected )
229 {
230 command_t cmd;
232 screen_update(mpd);
233 if( (cmd=get_keyboard_command()) != CMD_NONE )
234 {
235 screen_cmd(mpd, cmd);
236 if( cmd==CMD_VOLUME_UP || cmd==CMD_VOLUME_DOWN)
237 /* make shure we dont update the volume yet */
238 g_timer_start(timer);
239 }
240 else
241 screen_idle(mpd);
242 }
243 else if( options->reconnect )
244 {
245 screen_status_printf(_("Connecting to %s... [Press %s to abort]"),
246 options->host, get_key_names(CMD_QUIT,0) );
248 if( get_keyboard_command_with_timeout(MPD_RECONNECT_TIME)==CMD_QUIT)
249 exit(EXIT_SUCCESS);
251 if( mpdclient_connect(mpd,
252 options->host,
253 options->port,
254 1.5,
255 options->password) == 0 )
256 {
257 screen_status_printf(_("Connected to %s!"), options->host);
258 connected = TRUE;
259 }
260 doupdate();
261 }
263 t = g_timer_elapsed(timer, NULL);
264 }
265 exit(EXIT_FAILURE);
266 }