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