Code

abe60bf152ecd9951d23fc2bdb781fd0ed0a6d01
[ncmpc.git] / src / main.c
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     }
110 void
111 exit_and_cleanup(void)
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);
129 void
130 catch_sigint( int sig )
132   printf("\n%s\n", _("Exiting..."));
133   exit(EXIT_SUCCESS);
136 #ifdef DEBUG
137 void 
138 D(char *format, ...)
140   if( options.debug )
141     {
142       gchar *msg;
143       va_list ap;
144   
145       va_start(ap,format);
146       msg = g_strdup_vprintf(format,ap);
147       va_end(ap);
148       fprintf(stderr, "%s", msg);
149       g_free(msg);
150     }
152 #endif
154 int 
155 main(int argc, const char *argv[])
157   options_t *options;
158   struct sigaction act;
159   const char *charset = NULL;
160   gboolean key_error;
162 #ifdef HAVE_LOCALE_H
163   /* time and date formatting */
164   setlocale(LC_TIME,"");
165   /* charset */
166   setlocale(LC_CTYPE,"");
167   /* initialize charset conversions */
168   charset_init(g_get_charset(&charset));
169   D("charset: %s\n", charset);
170 #endif
172   /* initialize i18n support */
173 #ifdef ENABLE_NLS
174   setlocale(LC_MESSAGES, "");
175   bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
176   bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
177   textdomain(GETTEXT_PACKAGE);
178 #endif
180   /* initialize options */
181   options = options_init();
183   /* parse command line options - 1 pass get configuration files */
184   options_parse(argc, argv);
185   
186   /* read configuration */
187   read_configuration(options);
189   /* check key bindings */
190   key_error = check_key_bindings(NULL, NULL, 0);
191   
192   /* parse command line options - 2 pass */
193   options_parse(argc, argv);
195   /* setup signal behavior - SIGINT */
196   sigemptyset( &act.sa_mask );
197   act.sa_flags    = 0;
198   act.sa_handler = catch_sigint;
199   if( sigaction( SIGINT, &act, NULL )<0 )
200     {
201       perror("signal");
202       exit(EXIT_FAILURE);
203     }
204   /* setup signal behavior - SIGTERM */
205   sigemptyset( &act.sa_mask );
206   act.sa_flags    = 0;
207   act.sa_handler = catch_sigint;
208   if( sigaction( SIGTERM, &act, NULL )<0 )
209     {
210       perror("sigaction()");
211       exit(EXIT_FAILURE);
212     }
214   /* install exit function */
215   atexit(exit_and_cleanup);
217   /* connect to our music player daemon */
218   mpd = mpdclient_new();
219   if( mpdclient_connect(mpd, 
220                         options->host, 
221                         options->port, 
222                         10.0, 
223                         options->password) )
224     {
225       exit(EXIT_FAILURE);
226     }
227   connected = TRUE;
228   D("Connected to MPD version %d.%d.%d\n",
229     mpd->connection->version[0],
230     mpd->connection->version[1],
231     mpd->connection->version[2]);
233   /* quit if mpd is pre 0.11.0 - song id not supported by mpd */
234   if( MPD_VERSION_LT(mpd, 0,11,0) )
235     {
236       fprintf(stderr,
237               _("Error: MPD version %d.%d.%d is to old (0.11.0 needed).\n"),
238               mpd->connection->version[0],
239               mpd->connection->version[1],
240               mpd->connection->version[2]);
241       exit(EXIT_FAILURE);
242     }
244   /* initialize curses */
245   screen_init(mpd);
247   /* install error callback function */
248   mpdclient_install_error_callback(mpd, error_callback);
250   /* initialize timer */
251   timer = g_timer_new();
253   connected = TRUE;
254   while( connected || options->reconnect )
255     {
256       static gdouble t = G_MAXDOUBLE;
258       if( key_error )
259         {
260           char buf[BUFSIZE];
262           key_error=check_key_bindings(NULL, buf, BUFSIZE);
263           screen_status_printf("%s", buf);
264         }
266       if( connected && (t>=MPD_UPDATE_TIME || mpd->need_update) )
267         {
268           mpdclient_update(mpd);
269           g_timer_start(timer);
270         }
272       if( connected )
273         {
274           command_t cmd;
276           screen_update(mpd);
277           if( (cmd=get_keyboard_command()) != CMD_NONE )
278             {
279               screen_cmd(mpd, cmd);
280               if( cmd==CMD_VOLUME_UP || cmd==CMD_VOLUME_DOWN)
281                 /* make shure we dont update the volume yet */
282                 g_timer_start(timer);
283             }
284           else
285             screen_idle(mpd);
286         }
287       else if( options->reconnect )
288         {
289           screen_status_printf(_("Connecting to %s...  [Press %s to abort]"), 
290                                options->host, get_key_names(CMD_QUIT,0) );
292           if( get_keyboard_command_with_timeout(MPD_RECONNECT_TIME)==CMD_QUIT)
293             exit(EXIT_SUCCESS);
294           
295           if( mpdclient_connect(mpd,
296                                 options->host,
297                                 options->port, 
298                                 1.5,
299                                 options->password) == 0 )
300             {
301               screen_status_printf(_("Connected to %s!"), options->host);
302               connected = TRUE;
303             }     
304           doupdate();
305         }
306       if( options->enable_xterm_title )
307         update_xterm_title();
308       t = g_timer_elapsed(timer, NULL);
309     }
310   exit(EXIT_FAILURE);