bb3740b62aff1d50cd66159d9a81acf2321acc8d
1 /*
2 * (c) 2004 by Kalle Wallin (kaw@linux.se)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 */
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <signal.h>
23 #include <glib.h>
25 #include "config.h"
26 #include "libmpdclient.h"
27 #include "support.h"
28 #include "mpc.h"
29 #include "options.h"
30 #include "command.h"
31 #include "screen.h"
32 #include "conf.h"
34 /* time in seconds between mpd updates (double) */
35 #define MPD_UPDATE_TIME 0.5
37 /* timout in seconds before trying to reconnect (int) */
38 #define MPD_RECONNECT_TIMEOUT 3
41 static mpd_client_t *mpc = NULL;
42 static GTimer *timer = NULL;
44 void
45 exit_and_cleanup(void)
46 {
47 screen_exit();
48 printf("\n");
49 charset_close();
50 if( mpc )
51 {
52 if( mpc_error(mpc) )
53 fprintf(stderr,"Error: %s\n", mpc_error_str(mpc));
54 mpc_close(mpc);
55 }
56 g_free(options.host);
57 g_free(options.password);
58 if( timer )
59 g_timer_destroy(timer);
60 }
62 void
63 catch_sigint( int sig )
64 {
65 printf( "\nExiting...\n");
66 exit(EXIT_SUCCESS);
67 }
69 int
70 main(int argc, const char *argv[])
71 {
72 options_t *options;
73 struct sigaction act;
74 gboolean connected;
76 /* initialize options */
77 options = options_init();
79 /* parse command line options - 1 pass get configuration files */
80 options_parse(argc, argv);
82 /* read configuration */
83 read_configuration(options);
85 /* check key bindings */
86 if( check_key_bindings() )
87 {
88 fprintf(stderr, "Confusing key bindings - exiting!\n");
89 exit(EXIT_FAILURE);
90 }
92 /* parse command line options - 2 pass */
93 options_parse(argc, argv);
95 /* initialize local charset */
96 if( charset_init() )
97 exit(EXIT_FAILURE);
99 /* setup signal behavior - SIGINT */
100 sigemptyset( &act.sa_mask );
101 act.sa_flags = 0;
102 act.sa_handler = catch_sigint;
103 if( sigaction( SIGINT, &act, NULL )<0 )
104 {
105 perror("signal");
106 exit(EXIT_FAILURE);
107 }
108 /* setup signal behavior - SIGTERM */
109 sigemptyset( &act.sa_mask );
110 act.sa_flags = 0;
111 act.sa_handler = catch_sigint;
112 if( sigaction( SIGTERM, &act, NULL )<0 )
113 {
114 perror("sigaction()");
115 exit(EXIT_FAILURE);
116 }
118 /* set xterm title */
119 if( g_getenv("DISPLAY") )
120 printf("%c]0;%s%c", '\033', PACKAGE " version " VERSION, '\007');
122 /* install exit function */
123 atexit(exit_and_cleanup);
125 /* connect to our music player daemon */
126 mpc = mpc_connect(options->host, options->port, options->password);
127 if( mpc_error(mpc) )
128 exit(EXIT_FAILURE);
130 /* initialize curses */
131 screen_init();
133 /* initialize timer */
134 timer = g_timer_new();
136 connected = TRUE;
137 while( connected || options->reconnect )
138 {
139 static gdouble t = G_MAXDOUBLE;
141 if( connected && t>=MPD_UPDATE_TIME )
142 {
143 mpc_update(mpc);
144 if( mpc_error(mpc) == MPD_ERROR_ACK )
145 {
146 screen_status_printf("%s", mpc_error_str(mpc));
147 mpd_clearError(mpc->connection);
148 mpd_finishCommand(mpc->connection);
149 }
150 else if( mpc_error(mpc) )
151 {
152 screen_status_printf("Lost connection to %s", options->host);
153 connected = FALSE;
154 doupdate();
155 mpd_clearError(mpc->connection);
156 mpd_closeConnection(mpc->connection);
157 mpc->connection = NULL;
158 }
159 else
160 mpd_finishCommand(mpc->connection);
161 g_timer_start(timer);
162 }
164 if( connected )
165 {
166 command_t cmd;
168 screen_update(mpc);
169 if( (cmd=get_keyboard_command()) != CMD_NONE )
170 {
171 screen_cmd(mpc, cmd);
172 if( cmd==CMD_VOLUME_UP || cmd==CMD_VOLUME_DOWN)
173 /* make shure we dont update the volume yet */
174 g_timer_start(timer);
175 }
176 else
177 screen_idle(mpc);
178 }
179 else if( options->reconnect )
180 {
181 sleep(MPD_RECONNECT_TIMEOUT);
182 screen_status_printf("Connecting to %s... [Press Ctrl-C to abort]",
183 options->host);
184 if( mpc_reconnect(mpc,
185 options->host,
186 options->port,
187 options->password) == 0 )
188 {
189 screen_status_printf("Connected to %s!", options->host);
190 connected = TRUE;
191 }
192 doupdate();
193 }
195 t = g_timer_elapsed(timer, NULL);
196 }
198 exit(EXIT_FAILURE);
199 }