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 /* read configuration */
80 read_configuration(options);
82 /* check key bindings */
83 if( check_key_bindings() )
84 {
85 fprintf(stderr, "Confusing key bindings - exiting!\n");
86 exit(EXIT_FAILURE);
87 }
89 /* parse command line options */
90 options_parse(argc, argv);
92 /* initialize local charset */
93 if( charset_init() )
94 exit(EXIT_FAILURE);
96 /* setup signal behavior - SIGINT */
97 sigemptyset( &act.sa_mask );
98 act.sa_flags = 0;
99 act.sa_handler = catch_sigint;
100 if( sigaction( SIGINT, &act, NULL )<0 )
101 {
102 perror("signal");
103 exit(EXIT_FAILURE);
104 }
105 /* setup signal behavior - SIGTERM */
106 sigemptyset( &act.sa_mask );
107 act.sa_flags = 0;
108 act.sa_handler = catch_sigint;
109 if( sigaction( SIGTERM, &act, NULL )<0 )
110 {
111 perror("sigaction()");
112 exit(EXIT_FAILURE);
113 }
115 /* set xterm title */
116 if( g_getenv("DISPLAY") )
117 printf("%c]0;%s%c", '\033', PACKAGE " version " VERSION, '\007');
119 /* install exit function */
120 atexit(exit_and_cleanup);
122 /* connect to our music player daemon */
123 mpc = mpc_connect(options->host, options->port, options->password);
124 if( mpc_error(mpc) )
125 exit(EXIT_FAILURE);
127 /* initialize curses */
128 screen_init();
130 /* initialize timer */
131 timer = g_timer_new();
133 connected = TRUE;
134 while( connected || options->reconnect )
135 {
136 static gdouble t = G_MAXDOUBLE;
138 if( connected && t>=MPD_UPDATE_TIME )
139 {
140 mpc_update(mpc);
141 if( mpc_error(mpc) == MPD_ERROR_ACK )
142 {
143 screen_status_printf("%s", mpc_error_str(mpc));
144 mpd_clearError(mpc->connection);
145 mpd_finishCommand(mpc->connection);
146 }
147 else if( mpc_error(mpc) )
148 {
149 screen_status_printf("Lost connection to %s", options->host);
150 connected = FALSE;
151 doupdate();
152 mpd_clearError(mpc->connection);
153 mpd_closeConnection(mpc->connection);
154 mpc->connection = NULL;
155 }
156 else
157 mpd_finishCommand(mpc->connection);
158 g_timer_start(timer);
159 }
161 if( connected )
162 {
163 command_t cmd;
165 screen_update(mpc);
166 if( (cmd=get_keyboard_command()) != CMD_NONE )
167 {
168 screen_cmd(mpc, cmd);
169 if( cmd==CMD_VOLUME_UP || cmd==CMD_VOLUME_DOWN)
170 /* make shure we dont update the volume yet */
171 g_timer_start(timer);
172 }
173 else
174 screen_idle(mpc);
175 }
176 else if( options->reconnect )
177 {
178 sleep(MPD_RECONNECT_TIMEOUT);
179 screen_status_printf("Connecting to %s... [Press Ctrl-C to abort]",
180 options->host);
181 if( mpc_reconnect(mpc,
182 options->host,
183 options->port,
184 options->password) == 0 )
185 {
186 screen_status_printf("Connected to %s!", options->host);
187 connected = TRUE;
188 }
189 doupdate();
190 }
192 t = g_timer_elapsed(timer, NULL);
193 }
195 exit(EXIT_FAILURE);
196 }