Code

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