Code

Added wreadln.c, wreadln.h a simple line editor
[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 <glib.h>
27 #include "config.h"
28 #include "ncmpc.h"
29 #include "libmpdclient.h"
30 #include "support.h"
31 #include "mpc.h"
32 #include "options.h"
33 #include "command.h"
34 #include "screen.h"
35 #include "conf.h"
37 static mpd_client_t *mpc = NULL;
38 static GTimer       *timer = NULL;
40 void
41 exit_and_cleanup(void)
42 {
43   screen_exit();
44   printf("\n");
45   if( mpc )
46     {
47       if( mpc_error(mpc) )
48         fprintf(stderr,"Error: %s\n", mpc_error_str(mpc));
49       mpc_close(mpc);
50     }
51   g_free(options.host);
52   g_free(options.password);
53   if( timer )
54     g_timer_destroy(timer);
55 }
57 void
58 catch_sigint( int sig )
59 {
60   printf( _("\nExiting...\n"));
61   exit(EXIT_SUCCESS);
62 }
64 int 
65 main(int argc, const char *argv[])
66 {
67   options_t *options;
68   struct sigaction act;
69   gboolean connected;
70   const char *charset = NULL;
72 #ifdef HAVE_LOCALE_H
73   /* time and date formatting */
74   setlocale(LC_TIME,"");
75   /* charset */
76   setlocale(LC_CTYPE,"");
77   /* initialize charset conversions */
78   charset_init(g_get_charset(&charset));
79   D(printf("charset: %s\n", charset));
80 #endif
82   /* initialize i18n support */
83 #ifdef ENABLE_NLS
84   setlocale(LC_MESSAGES, "");
85   bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
86   bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
87   textdomain(GETTEXT_PACKAGE);
88 #endif
90   /* initialize options */
91   options = options_init();
93   /* parse command line options - 1 pass get configuration files */
94   options_parse(argc, argv);
95   
96   /* read configuration */
97   read_configuration(options);
98   
99   /* check key bindings */
100   if( check_key_bindings() )
101     {
102       fprintf(stderr, _("Confusing key bindings - exiting!\n"));
103       exit(EXIT_FAILURE);
104     }
106   /* parse command line options - 2 pass */
107   options_parse(argc, argv);
109   /* setup signal behavior - SIGINT */
110   sigemptyset( &act.sa_mask );
111   act.sa_flags    = 0;
112   act.sa_handler = catch_sigint;
113   if( sigaction( SIGINT, &act, NULL )<0 )
114     {
115       perror("signal");
116       exit(EXIT_FAILURE);
117     }
118   /* setup signal behavior - SIGTERM */
119   sigemptyset( &act.sa_mask );
120   act.sa_flags    = 0;
121   act.sa_handler = catch_sigint;
122   if( sigaction( SIGTERM, &act, NULL )<0 )
123     {
124       perror("sigaction()");
125       exit(EXIT_FAILURE);
126     }
128   /* set xterm title */
129   if( g_getenv("DISPLAY") )
130     printf("%c]0;%s%c", '\033', PACKAGE " version " VERSION, '\007');
132   /* install exit function */
133   atexit(exit_and_cleanup);
135   /* connect to our music player daemon */
136   mpc = mpc_connect(options->host, options->port, options->password);
137   if( mpc_error(mpc) )
138     exit(EXIT_FAILURE);
140   /* initialize curses */
141   screen_init();
143   /* initialize timer */
144   timer = g_timer_new();
146   connected = TRUE;
147   while( connected || options->reconnect )
148     {
149       static gdouble t = G_MAXDOUBLE;
151       if( connected && t>=MPD_UPDATE_TIME )
152         {
153           mpc_update(mpc);
154           if( mpc_error(mpc) == MPD_ERROR_ACK )
155             {
156               screen_status_printf("%s", mpc_error_str(mpc));
157               mpd_clearError(mpc->connection);
158               mpd_finishCommand(mpc->connection);
159             }
160           else if( mpc_error(mpc) )
161             {
162               screen_status_printf(_("Lost connection to %s"), options->host);
163               connected = FALSE;         
164               doupdate();
165               mpd_clearError(mpc->connection);
166               mpd_closeConnection(mpc->connection);
167               mpc->connection = NULL;
168             }
169           else  
170             mpd_finishCommand(mpc->connection);
171           g_timer_start(timer);
172         }
174       if( connected )
175         {
176           command_t cmd;
178           screen_update(mpc);
179           if( (cmd=get_keyboard_command()) != CMD_NONE )
180             {
181               screen_cmd(mpc, cmd);
182               if( cmd==CMD_VOLUME_UP || cmd==CMD_VOLUME_DOWN)
183                 /* make shure we dont update the volume yet */
184                 g_timer_start(timer);
185             }
186           else
187             screen_idle(mpc);
188         }
189       else if( options->reconnect )
190         {
191           sleep(MPD_RECONNECT_TIMEOUT);
192           screen_status_printf(_("Connecting to %s...  [Press Ctrl-C to abort]"), 
193                                options->host);
194           if( mpc_reconnect(mpc, 
195                             options->host, 
196                             options->port, 
197                             options->password) == 0 )
198             {
199               screen_status_printf(_("Connected to %s!"), options->host);
200               connected = TRUE;
201             }
202           doupdate();
203         }
205       t = g_timer_elapsed(timer, NULL);
206     }
208   exit(EXIT_FAILURE);