Code

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