Code

fix function prototypes
[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 const gchar *
48 error_msg(const 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, const 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         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 static 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   g_free(options.scroll_sep);
127   if( timer )
128     g_timer_destroy(timer);
131 static void
132 catch_sigint( int sig )
134         printf("\n%s\n", _("Exiting..."));
135         exit(EXIT_SUCCESS);
139 static 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 #ifndef NDEBUG
159 void 
160 D(const 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);
210         /* read configuration */
211         read_configuration(options);
213         /* check key bindings */
214         key_error = check_key_bindings(NULL, NULL, 0);
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                 perror("signal");
225                 exit(EXIT_FAILURE);
226         }
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                 perror("sigaction()");
234                 exit(EXIT_FAILURE);
235         }
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                 perror("sigaction(SIGCONT)");
243                 exit(EXIT_FAILURE);
244         }
246         /* setup signal behaviour - SIGHUP*/
247         sigemptyset( &act.sa_mask );
248         act.sa_flags    = 0;
249         act.sa_handler = catch_sigint;
250         if( sigaction(SIGHUP, &act, NULL)<0 ) {
251                 perror("sigaction(SIGHUP)");
252                 exit(EXIT_FAILURE);
253         }
255         /* install exit function */
256         atexit(exit_and_cleanup);
258         ncurses_init();
260         src_lyr_init ();
262         /* connect to our music player daemon */
263         mpd = mpdclient_new();
265         if (mpdclient_connect(mpd,
266                               options->host,
267                               options->port,
268                               10.0,
269                               options->password))
270                 exit(EXIT_FAILURE);
272         /* if no password is used, but the mpd wants one, the connection
273            might be established but no status information is avaiable */
274         mpdclient_update(mpd);
275         if (!mpd->status)
276                 screen_auth(mpd);
278         if (!mpd->status)
279                 exit(EXIT_FAILURE);
281         connected = TRUE;
282         D("Connected to MPD version %d.%d.%d\n",
283           mpd->connection->version[0],
284           mpd->connection->version[1],
285           mpd->connection->version[2]);
287         /* quit if mpd is pre 0.11.0 - song id not supported by mpd */
288         if( MPD_VERSION_LT(mpd, 0,11,0) ) {
289                 fprintf(stderr,
290                         _("Error: MPD version %d.%d.%d is to old (0.11.0 needed).\n"),
291                         mpd->connection->version[0],
292                         mpd->connection->version[1],
293                         mpd->connection->version[2]);
294                 exit(EXIT_FAILURE);
295         }
297         /* initialize curses */
298         screen_init(mpd);
299         /* install error callback function */
300         mpdclient_install_error_callback(mpd, error_callback);
302         /* initialize timer */
303         timer = g_timer_new();
305         connected = TRUE;
306         while (connected || options->reconnect) {
307                 static gdouble t = G_MAXDOUBLE;
309                 if( key_error ) {
310                         char buf[BUFSIZE];
312                         key_error=check_key_bindings(NULL, buf, BUFSIZE);
313                         screen_status_printf("%s", buf);
314                 }
316                 if( connected && (t>=MPD_UPDATE_TIME || mpd->need_update) ) {
317                         mpdclient_update(mpd);
318                         g_timer_start(timer);
319                 }
321                 if( connected ) {
322                         command_t cmd;
324                         screen_update(mpd);
325                         if( (cmd=get_keyboard_command()) != CMD_NONE )
326                                 {
327                                         screen_cmd(mpd, cmd);
328                                         if( cmd==CMD_VOLUME_UP || cmd==CMD_VOLUME_DOWN)
329                                                 /* make shure we dont update the volume yet */
330                                                 g_timer_start(timer);
331                                 }
332                         else
333                                 screen_idle(mpd);
334                 } else if (options->reconnect) {
335                         screen_status_printf(_("Connecting to %s...  [Press %s to abort]"),
336                                              options->host, get_key_names(CMD_QUIT,0) );
338                         if( get_keyboard_command_with_timeout(MPD_RECONNECT_TIME)==CMD_QUIT)
339                                 exit(EXIT_SUCCESS);
341                         if (mpdclient_connect(mpd,
342                                               options->host, options->port,
343                                               1.5,
344                                               options->password) == 0) {
345                                 screen_status_printf(_("Connected to %s!"), options->host);
346                                 connected = TRUE;
347                         }
349                         doupdate();
350                 }
352                 if (options->enable_xterm_title)
353                         update_xterm_title();
355                 t = g_timer_elapsed(timer, NULL);
356         }
357         exit(EXIT_FAILURE);