1a3573e7a4a3e55c79ca080d212482fe1d8b7d98
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 /* initialize charset */
73 #ifdef HAVE_LOCALE_H
74 if( setlocale(LC_CTYPE,"") == NULL )
75 {
76 g_printerr("setlocale() - failed!\n");
77 exit(EXIT_FAILURE);
78 }
79 charset_init(g_get_charset(&charset));
80 D(printf("charset: %s\n", charset));
81 #endif
83 /* initialize i18n support */
84 #ifdef ENABLE_NLS
85 setlocale(LC_MESSAGES, "");
86 bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
87 bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
88 textdomain(GETTEXT_PACKAGE);
89 #endif
91 /* initialize options */
92 options = options_init();
94 /* parse command line options - 1 pass get configuration files */
95 options_parse(argc, argv);
97 /* read configuration */
98 read_configuration(options);
100 /* check key bindings */
101 if( check_key_bindings() )
102 {
103 fprintf(stderr, _("Confusing key bindings - exiting!\n"));
104 exit(EXIT_FAILURE);
105 }
107 /* parse command line options - 2 pass */
108 options_parse(argc, argv);
110 /* setup signal behavior - SIGINT */
111 sigemptyset( &act.sa_mask );
112 act.sa_flags = 0;
113 act.sa_handler = catch_sigint;
114 if( sigaction( SIGINT, &act, NULL )<0 )
115 {
116 perror("signal");
117 exit(EXIT_FAILURE);
118 }
119 /* setup signal behavior - SIGTERM */
120 sigemptyset( &act.sa_mask );
121 act.sa_flags = 0;
122 act.sa_handler = catch_sigint;
123 if( sigaction( SIGTERM, &act, NULL )<0 )
124 {
125 perror("sigaction()");
126 exit(EXIT_FAILURE);
127 }
129 /* set xterm title */
130 if( g_getenv("DISPLAY") )
131 printf("%c]0;%s%c", '\033', PACKAGE " version " VERSION, '\007');
133 /* install exit function */
134 atexit(exit_and_cleanup);
136 /* connect to our music player daemon */
137 mpc = mpc_connect(options->host, options->port, options->password);
138 if( mpc_error(mpc) )
139 exit(EXIT_FAILURE);
141 /* initialize curses */
142 screen_init();
144 /* initialize timer */
145 timer = g_timer_new();
147 connected = TRUE;
148 while( connected || options->reconnect )
149 {
150 static gdouble t = G_MAXDOUBLE;
152 if( connected && t>=MPD_UPDATE_TIME )
153 {
154 mpc_update(mpc);
155 if( mpc_error(mpc) == MPD_ERROR_ACK )
156 {
157 screen_status_printf("%s", mpc_error_str(mpc));
158 mpd_clearError(mpc->connection);
159 mpd_finishCommand(mpc->connection);
160 }
161 else if( mpc_error(mpc) )
162 {
163 screen_status_printf(_("Lost connection to %s"), options->host);
164 connected = FALSE;
165 doupdate();
166 mpd_clearError(mpc->connection);
167 mpd_closeConnection(mpc->connection);
168 mpc->connection = NULL;
169 }
170 else
171 mpd_finishCommand(mpc->connection);
172 g_timer_start(timer);
173 }
175 if( connected )
176 {
177 command_t cmd;
179 screen_update(mpc);
180 if( (cmd=get_keyboard_command()) != CMD_NONE )
181 {
182 screen_cmd(mpc, cmd);
183 if( cmd==CMD_VOLUME_UP || cmd==CMD_VOLUME_DOWN)
184 /* make shure we dont update the volume yet */
185 g_timer_start(timer);
186 }
187 else
188 screen_idle(mpc);
189 }
190 else if( options->reconnect )
191 {
192 sleep(MPD_RECONNECT_TIMEOUT);
193 screen_status_printf(_("Connecting to %s... [Press Ctrl-C to abort]"),
194 options->host);
195 if( mpc_reconnect(mpc,
196 options->host,
197 options->port,
198 options->password) == 0 )
199 {
200 screen_status_printf(_("Connected to %s!"), options->host);
201 connected = TRUE;
202 }
203 doupdate();
204 }
206 t = g_timer_elapsed(timer, NULL);
207 }
209 exit(EXIT_FAILURE);
210 }