81522f9d79a2eb577adae43915b43dad3567fb11
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 "command.h"
34 #include "screen.h"
35 #include "conf.h"
37 static mpdclient_t *mpd = NULL;
38 static gboolean connected = FALSE;
39 static GTimer *timer = NULL;
41 static gchar *
42 error_msg(gchar *msg)
43 {
44 gchar *p;
46 if( (p=strchr(msg, '}' )) == NULL )
47 return msg;
48 while( p && *p && (*p=='}' || *p==' ') )
49 p++;
51 return p;
52 }
54 static void
55 error_callback(mpdclient_t *c, gint error, gchar *msg)
56 {
57 gint code = GET_ACK_ERROR_CODE(error);
59 error = error & 0xFF;
60 D("Error [%d:%d]> \"%s\"\n", error, code, msg);
61 switch(error)
62 {
63 case MPD_ERROR_CONNPORT:
64 case MPD_ERROR_NORESPONSE:
65 break;
66 case MPD_ERROR_ACK:
67 screen_status_printf("%s", error_msg(msg));
68 beep();
69 break;
70 default:
71 screen_status_printf("%s", msg);
72 beep();
73 doupdate();
74 connected = FALSE;
75 }
76 }
78 void
79 exit_and_cleanup(void)
80 {
81 screen_exit();
82 printf("\n");
83 if( mpd )
84 {
85 mpdclient_disconnect(mpd);
86 mpd = mpdclient_free(mpd);
87 }
88 g_free(options.host);
89 g_free(options.password);
90 g_free(options.list_format);
91 g_free(options.status_format);
92 if( timer )
93 g_timer_destroy(timer);
94 }
96 void
97 catch_sigint( int sig )
98 {
99 printf("\n%s\n", _("Exiting..."));
100 exit(EXIT_SUCCESS);
101 }
103 int
104 main(int argc, const char *argv[])
105 {
106 options_t *options;
107 struct sigaction act;
108 const char *charset = NULL;
110 #ifdef HAVE_LOCALE_H
111 /* time and date formatting */
112 setlocale(LC_TIME,"");
113 /* charset */
114 setlocale(LC_CTYPE,"");
115 /* initialize charset conversions */
116 charset_init(g_get_charset(&charset));
117 D("charset: %s\n", charset);
118 #endif
120 /* initialize i18n support */
121 #ifdef ENABLE_NLS
122 setlocale(LC_MESSAGES, "");
123 bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
124 bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
125 textdomain(GETTEXT_PACKAGE);
126 #endif
128 /* initialize options */
129 options = options_init();
131 /* parse command line options - 1 pass get configuration files */
132 options_parse(argc, argv);
134 /* read configuration */
135 read_configuration(options);
137 /* check key bindings */
138 if( check_key_bindings() )
139 {
140 fprintf(stderr, _("Confusing key bindings - exiting!\n"));
141 exit(EXIT_FAILURE);
142 }
144 /* parse command line options - 2 pass */
145 options_parse(argc, argv);
147 /* setup signal behavior - SIGINT */
148 sigemptyset( &act.sa_mask );
149 act.sa_flags = 0;
150 act.sa_handler = catch_sigint;
151 if( sigaction( SIGINT, &act, NULL )<0 )
152 {
153 perror("signal");
154 exit(EXIT_FAILURE);
155 }
156 /* setup signal behavior - SIGTERM */
157 sigemptyset( &act.sa_mask );
158 act.sa_flags = 0;
159 act.sa_handler = catch_sigint;
160 if( sigaction( SIGTERM, &act, NULL )<0 )
161 {
162 perror("sigaction()");
163 exit(EXIT_FAILURE);
164 }
166 /* set xterm title */
167 if( g_getenv("DISPLAY") )
168 printf("%c]0;%s%c", '\033', PACKAGE " version " VERSION, '\007');
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 if( !MPD_VERSION(mpd, 0,11,0) )
190 {
191 fprintf(stderr, "MPD version %d.%d.%d is to old (0.11.0 needed).\n",
192 mpd->connection->version[0],
193 mpd->connection->version[1],
194 mpd->connection->version[2]);
195 exit(EXIT_FAILURE);
196 }
198 /* initialize curses */
199 screen_init(mpd);
201 /* install error callback function */
202 mpdclient_install_error_callback(mpd, error_callback);
204 /* initialize timer */
205 timer = g_timer_new();
207 connected = TRUE;
208 while( connected || options->reconnect )
209 {
210 static gdouble t = G_MAXDOUBLE;
212 if( connected && (t>=MPD_UPDATE_TIME || mpd->need_update) )
213 {
214 mpdclient_update(mpd);
215 g_timer_start(timer);
216 }
218 if( connected )
219 {
220 command_t cmd;
222 screen_update(mpd);
223 if( (cmd=get_keyboard_command()) != CMD_NONE )
224 {
225 screen_cmd(mpd, cmd);
226 if( cmd==CMD_VOLUME_UP || cmd==CMD_VOLUME_DOWN)
227 /* make shure we dont update the volume yet */
228 g_timer_start(timer);
229 }
230 else
231 screen_idle(mpd);
232 }
233 else if( options->reconnect )
234 {
235 screen_status_printf(_("Connecting to %s... [Press %s to abort]"),
236 options->host, get_key_names(CMD_QUIT,0) );
238 if( get_keyboard_command_with_timeout(MPD_RECONNECT_TIME)==CMD_QUIT)
239 exit(EXIT_SUCCESS);
241 if( mpdclient_connect(mpd,
242 options->host,
243 options->port,
244 1.5,
245 options->password) == 0 )
246 {
247 screen_status_printf(_("Connected to %s!"), options->host);
248 connected = TRUE;
249 }
250 doupdate();
251 }
253 t = g_timer_elapsed(timer, NULL);
254 }
255 exit(EXIT_FAILURE);
256 }