Code

f3cc155ed5edf0fb81e29f875d73bb43f036340a
[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 "src_lyrics.h"
37 #include "screen.h"
38 #include "screen_utils.h"
39 #include "strfsong.h"
41 #define BUFSIZE 1024
43 static mpdclient_t   *mpd = NULL;
44 static gboolean connected = FALSE;
45 static GTimer      *timer = NULL;
47 static gchar *
48 error_msg(gchar *msg)
49 {
50   gchar *p;
52   if( (p=strchr(msg, '}' )) == NULL )
53     return msg;
54   while( p && *p && (*p=='}' || *p==' ') )
55     p++;
57   return p;
58 }
60 static void
61 error_callback(mpdclient_t *c, gint error, gchar *msg)
62 {
63   gint code = GET_ACK_ERROR_CODE(error);
65   error = error & 0xFF;
66   D("Error [%d:%d]> \"%s\"\n", error, code, msg);
67   switch(error)
68     {
69     case MPD_ERROR_CONNPORT:
70     case MPD_ERROR_NORESPONSE:
71       break;
72     case MPD_ERROR_ACK:
73       screen_status_printf("%s", error_msg(msg));
74       screen_bell();
75       break;
76     default:
77       screen_status_printf("%s", msg);
78       screen_bell();
79       doupdate();
80       connected = FALSE;
81     }
82 }
84 static void
85 update_xterm_title(void)
86 {
87   static char title[BUFSIZE];
88   char tmp[BUFSIZE];
89   mpd_Status *status = NULL;
90   mpd_Song *song = NULL;
92   if( mpd )
93     {
94       status = mpd->status;
95       song = mpd->song;
96     }
98   if(options.xterm_title_format && status && song && IS_PLAYING(status->state))
99     {
100       strfsong(tmp, BUFSIZE, options.xterm_title_format, song);
101     }
102   else
103     g_strlcpy(tmp, PACKAGE " version " VERSION, BUFSIZE);
105   if( strncmp(title,tmp,BUFSIZE) )
106     {
107       g_strlcpy(title, tmp, BUFSIZE);
108       set_xterm_title("%s", title);
109     }
112 void
113 exit_and_cleanup(void)
115   screen_exit();
116   set_xterm_title("");
117   printf("\n");
118   if( mpd )
119     {
120       mpdclient_disconnect(mpd);
121       mpd = mpdclient_free(mpd);
122     }
123   g_free(options.host);
124   g_free(options.password);
125   g_free(options.list_format);
126   g_free(options.status_format);
127   g_free(options.scroll_sep);
128   if( timer )
129     g_timer_destroy(timer);
132 void
133 catch_sigint( int sig )
135   printf("\n%s\n", _("Exiting..."));
136   exit(EXIT_SUCCESS);
139 void
140 catch_sigcont( int sig )
142   D("catch_sigcont()\n");
143 #ifdef ENABLE_RAW_MODE
144   reset_prog_mode(); /* restore tty modes */
145   refresh();
146 #endif
147   screen_resize(); 
150 void
151 sigstop(void)
153   def_prog_mode();  /* save the tty modes */
154   endwin();         /* end curses mode temporarily */
155   kill(0, SIGSTOP); /* issue SIGSTOP */
158 #ifdef DEBUG
159 void 
160 D(char *format, ...)
162   if( options.debug )
163     {
164       gchar *msg;
165       va_list ap;
166   
167       va_start(ap,format);
168       msg = g_strdup_vprintf(format,ap);
169       va_end(ap);
170       fprintf(stderr, "%s", msg);
171       g_free(msg);
172     }
174 #endif
176 int 
177 main(int argc, const char *argv[])
179   options_t *options;
180   struct sigaction act;
181   const char *charset = NULL;
182   gboolean key_error;
184 #ifdef HAVE_LOCALE_H
185   /* time and date formatting */
186   setlocale(LC_TIME,"");
187   /* care about sorting order etc */
188   setlocale(LC_COLLATE,"");
189   /* charset */
190   setlocale(LC_CTYPE,"");
191   /* initialize charset conversions */
192   charset_init(g_get_charset(&charset));
193   D("charset: %s\n", charset);
194 #endif
196   /* initialize i18n support */
197 #ifdef ENABLE_NLS
198   setlocale(LC_MESSAGES, "");
199   bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
200   bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
201   textdomain(GETTEXT_PACKAGE);
202 #endif
204   /* initialize options */
205   options = options_init();
207   /* parse command line options - 1 pass get configuration files */
208   options_parse(argc, argv);
209   
210   /* read configuration */
211   read_configuration(options);
213   /* check key bindings */
214   key_error = check_key_bindings(NULL, NULL, 0);
215   
216   /* parse command line options - 2 pass */
217   options_parse(argc, argv);
219   /* setup signal behavior - SIGINT */
220   sigemptyset( &act.sa_mask );
221   act.sa_flags    = 0;
222   act.sa_handler = catch_sigint;
223   if( sigaction(SIGINT, &act, NULL)<0 )
224     {
225       perror("signal");
226       exit(EXIT_FAILURE);
227     }
228   /* setup signal behavior - SIGTERM */
229   sigemptyset( &act.sa_mask );
230   act.sa_flags    = 0;
231   act.sa_handler = catch_sigint;
232   if( sigaction(SIGTERM, &act, NULL)<0 )
233     {
234       perror("sigaction()");
235       exit(EXIT_FAILURE);
236     }
237   /* setup signal behavior - SIGCONT */
238   sigemptyset( &act.sa_mask );
239   act.sa_flags    = 0;
240   act.sa_handler = catch_sigcont;
241   if( sigaction(SIGCONT, &act, NULL)<0 )
242     {
243       perror("sigaction(SIGCONT)");
244       exit(EXIT_FAILURE);
245     }
246  
247   /* setup signal behaviour - SIGHUP*/ 
248   sigemptyset( &act.sa_mask );
249   act.sa_flags    = 0;
250   act.sa_handler = catch_sigint;
251   if( sigaction(SIGHUP, &act, NULL)<0 )
252         {
253         perror("sigaction(SIGHUP)");
254         exit(EXIT_FAILURE);
255         }
256         
257   /* install exit function */
258   atexit(exit_and_cleanup);
259   
260   ncurses_init();
262   src_lyr_init ();
264   /* connect to our music player daemon */
265   mpd = mpdclient_new();
266  
267   if( mpdclient_connect(mpd, 
268                         options->host, 
269                         options->port, 
270                         10.0, 
271                         options->password) )
272     {
273       exit(EXIT_FAILURE);
274     }
276   /* if no password is used, but the mpd wants one, the connection
277      might be established but no status information is avaiable */
278   mpdclient_update(mpd);
279   if(!mpd->status)
280     {
281       screen_auth(mpd);
282     }
283   if(!mpd->status) exit(EXIT_FAILURE);
284   
285   connected = TRUE;
286   D("Connected to MPD version %d.%d.%d\n",
287     mpd->connection->version[0],
288     mpd->connection->version[1],
289     mpd->connection->version[2]);
291   /* quit if mpd is pre 0.11.0 - song id not supported by mpd */
292   if( MPD_VERSION_LT(mpd, 0,11,0) )
293     {
294       fprintf(stderr,
295               _("Error: MPD version %d.%d.%d is to old (0.11.0 needed).\n"),
296               mpd->connection->version[0],
297               mpd->connection->version[1],
298               mpd->connection->version[2]);
299       exit(EXIT_FAILURE);
300     }
302   /* initialize curses */
303   screen_init(mpd);
304   /* install error callback function */
305   mpdclient_install_error_callback(mpd, error_callback);
307   /* initialize timer */
308   timer = g_timer_new();
310   connected = TRUE;
311   while( connected || options->reconnect )
312     {
313       static gdouble t = G_MAXDOUBLE;
315       if( key_error )
316         {
317           char buf[BUFSIZE];
319           key_error=check_key_bindings(NULL, buf, BUFSIZE);
320           screen_status_printf("%s", buf);
321         }
323       if( connected && (t>=MPD_UPDATE_TIME || mpd->need_update) )
324         {
325           mpdclient_update(mpd);
326           g_timer_start(timer);
327         }
329       if( connected )
330         {
331           command_t cmd;
333           screen_update(mpd);
334           if( (cmd=get_keyboard_command()) != CMD_NONE )
335             {
336               screen_cmd(mpd, cmd);
337               if( cmd==CMD_VOLUME_UP || cmd==CMD_VOLUME_DOWN)
338                 /* make shure we dont update the volume yet */
339                 g_timer_start(timer);
340             }
341           else
342             screen_idle(mpd);
343         }
344       else if( options->reconnect )
345         {
346           screen_status_printf(_("Connecting to %s...  [Press %s to abort]"), 
347                                options->host, get_key_names(CMD_QUIT,0) );
349           if( get_keyboard_command_with_timeout(MPD_RECONNECT_TIME)==CMD_QUIT)
350             exit(EXIT_SUCCESS);
351           
352           if( mpdclient_connect(mpd,
353                                 options->host,
354                                 options->port, 
355                                 1.5,
356                                 options->password) == 0 )
357             {
358               screen_status_printf(_("Connected to %s!"), options->host);
359               connected = TRUE;
360             }     
361           doupdate();
362         }
363       if( options->enable_xterm_title )
364         update_xterm_title();
365       t = g_timer_elapsed(timer, NULL);
366     }
367   exit(EXIT_FAILURE);