Code

Added flags for conflicting, modified key bindings - check_key_bindings()
[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 int 
137 main(int argc, const char *argv[])
139   options_t *options;
140   struct sigaction act;
141   const char *charset = NULL;
142   gboolean key_error;
144 #ifdef HAVE_LOCALE_H
145   /* time and date formatting */
146   setlocale(LC_TIME,"");
147   /* charset */
148   setlocale(LC_CTYPE,"");
149   /* initialize charset conversions */
150   charset_init(g_get_charset(&charset));
151   D("charset: %s\n", charset);
152 #endif
154   /* initialize i18n support */
155 #ifdef ENABLE_NLS
156   setlocale(LC_MESSAGES, "");
157   bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
158   bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
159   textdomain(GETTEXT_PACKAGE);
160 #endif
162   /* initialize options */
163   options = options_init();
165   /* parse command line options - 1 pass get configuration files */
166   options_parse(argc, argv);
167   
168   /* read configuration */
169   read_configuration(options);
171   /* check key bindings */
172   key_error = check_key_bindings(NULL, NULL, 0);
173   
174   /* parse command line options - 2 pass */
175   options_parse(argc, argv);
177   /* setup signal behavior - SIGINT */
178   sigemptyset( &act.sa_mask );
179   act.sa_flags    = 0;
180   act.sa_handler = catch_sigint;
181   if( sigaction( SIGINT, &act, NULL )<0 )
182     {
183       perror("signal");
184       exit(EXIT_FAILURE);
185     }
186   /* setup signal behavior - SIGTERM */
187   sigemptyset( &act.sa_mask );
188   act.sa_flags    = 0;
189   act.sa_handler = catch_sigint;
190   if( sigaction( SIGTERM, &act, NULL )<0 )
191     {
192       perror("sigaction()");
193       exit(EXIT_FAILURE);
194     }
196   /* install exit function */
197   atexit(exit_and_cleanup);
199   /* connect to our music player daemon */
200   mpd = mpdclient_new();
201   if( mpdclient_connect(mpd, 
202                         options->host, 
203                         options->port, 
204                         10.0, 
205                         options->password) )
206     {
207       exit(EXIT_FAILURE);
208     }
209   connected = TRUE;
210   D("Connected to MPD version %d.%d.%d\n",
211     mpd->connection->version[0],
212     mpd->connection->version[1],
213     mpd->connection->version[2]);
215   /* quit if mpd is pre 0.11.0 - song id not supported by mpd */
216   if( MPD_VERSION_LT(mpd, 0,11,0) )
217     {
218       fprintf(stderr,
219               _("Error: MPD version %d.%d.%d is to old (0.11.0 needed).\n"),
220               mpd->connection->version[0],
221               mpd->connection->version[1],
222               mpd->connection->version[2]);
223       exit(EXIT_FAILURE);
224     }
226   /* initialize curses */
227   screen_init(mpd);
229   /* install error callback function */
230   mpdclient_install_error_callback(mpd, error_callback);
232   /* initialize timer */
233   timer = g_timer_new();
235   connected = TRUE;
236   while( connected || options->reconnect )
237     {
238       static gdouble t = G_MAXDOUBLE;
240       if( key_error )
241         {
242           char buf[BUFSIZE];
244           key_error=check_key_bindings(NULL, buf, BUFSIZE);
245           screen_status_printf("%s", buf);
246         }
248       if( connected && (t>=MPD_UPDATE_TIME || mpd->need_update) )
249         {
250           mpdclient_update(mpd);
251           g_timer_start(timer);
252         }
254       if( connected )
255         {
256           command_t cmd;
258           screen_update(mpd);
259           if( (cmd=get_keyboard_command()) != CMD_NONE )
260             {
261               screen_cmd(mpd, cmd);
262               if( cmd==CMD_VOLUME_UP || cmd==CMD_VOLUME_DOWN)
263                 /* make shure we dont update the volume yet */
264                 g_timer_start(timer);
265             }
266           else
267             screen_idle(mpd);
268         }
269       else if( options->reconnect )
270         {
271           screen_status_printf(_("Connecting to %s...  [Press %s to abort]"), 
272                                options->host, get_key_names(CMD_QUIT,0) );
274           if( get_keyboard_command_with_timeout(MPD_RECONNECT_TIME)==CMD_QUIT)
275             exit(EXIT_SUCCESS);
276           
277           if( mpdclient_connect(mpd,
278                                 options->host,
279                                 options->port, 
280                                 1.5,
281                                 options->password) == 0 )
282             {
283               screen_status_printf(_("Connected to %s!"), options->host);
284               connected = TRUE;
285             }     
286           doupdate();
287         }
288       if( options->enable_xterm_title )
289         update_xterm_title();
290       t = g_timer_elapsed(timer, NULL);
291     }
292   exit(EXIT_FAILURE);