Commit | Line | Data |
---|---|---|
460b9539 | 1 | /* |
2 | * This file is part of DisOrder. | |
eb525fcd | 3 | * Copyright (C) 2006, 2007 Richard Kettlewell |
460b9539 | 4 | * |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * General Public License for more details. | |
14 | * | |
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 | |
18 | * USA | |
19 | */ | |
20 | ||
21 | #include "disobedience.h" | |
22 | ||
23 | #include <getopt.h> | |
24 | #include <locale.h> | |
fe8f8518 | 25 | #include <pcre.h> |
460b9539 | 26 | |
27 | /* Apologies for the numerous de-consting casts, but GLib et al do not seem to | |
28 | * have heard of const. */ | |
29 | ||
30 | #include "style.h" /* generated style */ | |
31 | ||
32 | /* Functions --------------------------------------------------------------- */ | |
33 | ||
34 | static void log_connected(void *v); | |
35 | static void log_completed(void *v, const char *track); | |
36 | static void log_failed(void *v, const char *track, const char *status); | |
37 | static void log_moved(void *v, const char *user); | |
38 | static void log_playing(void *v, const char *track, const char *user); | |
39 | static void log_queue(void *v, struct queue_entry *q); | |
40 | static void log_recent_added(void *v, struct queue_entry *q); | |
41 | static void log_recent_removed(void *v, const char *id); | |
42 | static void log_removed(void *v, const char *id, const char *user); | |
43 | static void log_scratched(void *v, const char *track, const char *user); | |
44 | static void log_state(void *v, unsigned long state); | |
45 | static void log_volume(void *v, int l, int r); | |
46 | ||
47 | /* Variables --------------------------------------------------------------- */ | |
48 | ||
49 | GMainLoop *mainloop; /* event loop */ | |
50 | GtkWidget *toplevel; /* top level window */ | |
51 | GtkWidget *report_label; /* label for progress indicator */ | |
52 | GtkWidget *tabs; /* main tabs */ | |
53 | ||
54 | disorder_eclient *client; /* main client */ | |
55 | ||
56 | unsigned long last_state; /* last reported state */ | |
57 | int playing; /* true if playing some track */ | |
58 | int volume_l, volume_r; /* volume */ | |
59 | double goesupto = 10; /* volume upper bound */ | |
60 | int choosealpha; /* break up choose by letter */ | |
61 | ||
460b9539 | 62 | static const disorder_eclient_log_callbacks gdisorder_log_callbacks = { |
63 | log_connected, | |
64 | log_completed, | |
65 | log_failed, | |
66 | log_moved, | |
67 | log_playing, | |
68 | log_queue, | |
69 | log_recent_added, | |
70 | log_recent_removed, | |
71 | log_removed, | |
72 | log_scratched, | |
73 | log_state, | |
74 | log_volume | |
75 | }; | |
76 | ||
77 | /* Window creation --------------------------------------------------------- */ | |
78 | ||
79 | /* Note that all the client operations kicked off from here will only complete | |
80 | * after we've entered the event loop. */ | |
81 | ||
82 | static gboolean delete_event(GtkWidget attribute((unused)) *widget, | |
83 | GdkEvent attribute((unused)) *event, | |
84 | gpointer attribute((unused)) data) { | |
85 | D(("delete_event")); | |
86 | exit(0); /* die immediately */ | |
87 | } | |
88 | ||
89 | /* Called when the current tab is switched. */ | |
90 | static void tab_switched(GtkNotebook attribute((unused)) *notebook, | |
91 | GtkNotebookPage attribute((unused)) *page, | |
92 | guint page_num, | |
93 | gpointer attribute((unused)) user_data) { | |
94 | menu_update(page_num); | |
95 | } | |
96 | ||
97 | static GtkWidget *report_box(void) { | |
98 | GtkWidget *vbox = gtk_vbox_new(FALSE, 0); | |
99 | ||
100 | report_label = gtk_label_new(""); | |
101 | gtk_label_set_ellipsize(GTK_LABEL(report_label), PANGO_ELLIPSIZE_END); | |
102 | gtk_misc_set_alignment(GTK_MISC(report_label), 0, 0); | |
103 | gtk_container_add(GTK_CONTAINER(vbox), gtk_hseparator_new()); | |
104 | gtk_container_add(GTK_CONTAINER(vbox), report_label); | |
105 | return vbox; | |
106 | } | |
107 | ||
108 | static GtkWidget *notebook(void) { | |
109 | tabs = gtk_notebook_new(); | |
110 | g_signal_connect(tabs, "switch-page", G_CALLBACK(tab_switched), 0); | |
111 | gtk_notebook_append_page(GTK_NOTEBOOK(tabs), queue_widget(), | |
112 | gtk_label_new("Queue")); | |
113 | gtk_notebook_append_page(GTK_NOTEBOOK(tabs), recent_widget(), | |
114 | gtk_label_new("Recent")); | |
115 | gtk_notebook_append_page(GTK_NOTEBOOK(tabs), choose_widget(), | |
116 | gtk_label_new("Choose")); | |
117 | return tabs; | |
118 | } | |
119 | ||
120 | static void make_toplevel_window(void) { | |
121 | GtkWidget *const vbox = gtk_vbox_new(FALSE, 1); | |
122 | GtkWidget *const rb = report_box(); | |
123 | ||
124 | D(("top_window")); | |
125 | toplevel = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
126 | /* default size is too small */ | |
127 | gtk_window_set_default_size(GTK_WINDOW(toplevel), 640, 480); | |
128 | /* terminate on close */ | |
129 | g_signal_connect(G_OBJECT(toplevel), "delete_event", | |
130 | G_CALLBACK(delete_event), NULL); | |
131 | /* lay out the window */ | |
132 | gtk_window_set_title(GTK_WINDOW(toplevel), "Disobedience"); | |
133 | gtk_container_add(GTK_CONTAINER(toplevel), vbox); | |
134 | /* lay out the vbox */ | |
135 | gtk_box_pack_start(GTK_BOX(vbox), | |
136 | menubar(toplevel), | |
137 | FALSE, /* expand */ | |
138 | FALSE, /* fill */ | |
139 | 0); | |
140 | gtk_box_pack_start(GTK_BOX(vbox), | |
141 | control_widget(), | |
142 | FALSE, /* expand */ | |
143 | FALSE, /* fill */ | |
144 | 0); | |
145 | gtk_container_add(GTK_CONTAINER(vbox), notebook()); | |
146 | gtk_box_pack_end(GTK_BOX(vbox), | |
147 | rb, | |
148 | FALSE, /* expand */ | |
149 | FALSE, /* fill */ | |
150 | 0); | |
151 | gtk_widget_set_name(toplevel, "disobedience"); | |
152 | } | |
153 | ||
154 | /* State monitoring -------------------------------------------------------- */ | |
155 | ||
156 | static void all_update(void) { | |
157 | playing_update(); | |
158 | queue_update(); | |
159 | recent_update(); | |
160 | control_update(); | |
161 | } | |
162 | ||
163 | static void log_connected(void attribute((unused)) *v) { | |
164 | struct callbackdata *cbd; | |
165 | ||
166 | /* Don't know what we might have missed while disconnected so update | |
167 | * everything. We get this at startup too and this is how we do the initial | |
168 | * state fetch. */ | |
169 | all_update(); | |
170 | /* Re-get the volume */ | |
171 | cbd = xmalloc(sizeof *cbd); | |
172 | cbd->onerror = 0; | |
173 | disorder_eclient_volume(client, log_volume, -1, -1, cbd); | |
174 | } | |
175 | ||
176 | static void log_completed(void attribute((unused)) *v, | |
177 | const char attribute((unused)) *track) { | |
178 | playing = 0; | |
179 | playing_update(); | |
180 | control_update(); | |
181 | } | |
182 | ||
183 | static void log_failed(void attribute((unused)) *v, | |
184 | const char attribute((unused)) *track, | |
185 | const char attribute((unused)) *status) { | |
186 | playing = 0; | |
187 | playing_update(); | |
188 | control_update(); | |
189 | } | |
190 | ||
191 | static void log_moved(void attribute((unused)) *v, | |
192 | const char attribute((unused)) *user) { | |
193 | queue_update(); | |
194 | } | |
195 | ||
196 | static void log_playing(void attribute((unused)) *v, | |
197 | const char attribute((unused)) *track, | |
198 | const char attribute((unused)) *user) { | |
199 | playing = 1; | |
200 | playing_update(); | |
201 | control_update(); | |
202 | /* we get a log_removed() anyway so we don't need to update_queue() from | |
203 | * here */ | |
204 | } | |
205 | ||
206 | static void log_queue(void attribute((unused)) *v, | |
207 | struct queue_entry attribute((unused)) *q) { | |
208 | queue_update(); | |
209 | } | |
210 | ||
211 | static void log_recent_added(void attribute((unused)) *v, | |
212 | struct queue_entry attribute((unused)) *q) { | |
213 | recent_update(); | |
214 | } | |
215 | ||
216 | static void log_recent_removed(void attribute((unused)) *v, | |
217 | const char attribute((unused)) *id) { | |
218 | /* nothing - log_recent_added() will trigger the relevant update */ | |
219 | } | |
220 | ||
221 | static void log_removed(void attribute((unused)) *v, | |
222 | const char attribute((unused)) *id, | |
223 | const char attribute((unused)) *user) { | |
224 | queue_update(); | |
225 | } | |
226 | ||
227 | static void log_scratched(void attribute((unused)) *v, | |
228 | const char attribute((unused)) *track, | |
229 | const char attribute((unused)) *user) { | |
230 | playing = 0; | |
231 | playing_update(); | |
232 | control_update(); | |
233 | } | |
234 | ||
235 | static void log_state(void attribute((unused)) *v, | |
236 | unsigned long state) { | |
237 | last_state = state; | |
238 | control_update(); | |
239 | /* If the track is paused or resume then the currently playing track is | |
240 | * refetched so that we can continue to correctly calculate the played so-far | |
241 | * field */ | |
242 | playing_update(); | |
243 | } | |
244 | ||
245 | static void log_volume(void attribute((unused)) *v, | |
246 | int l, int r) { | |
247 | if(volume_l != l || volume_r != r) { | |
248 | volume_l = l; | |
249 | volume_r = r; | |
250 | control_update(); | |
251 | } | |
252 | } | |
253 | ||
0b7c8909 | 254 | #if MDEBUG |
255 | static int widget_count, container_count; | |
256 | ||
257 | static void count_callback(GtkWidget *w, | |
258 | gpointer attribute((unused)) data) { | |
259 | ++widget_count; | |
260 | if(GTK_IS_CONTAINER(w)) { | |
261 | ++container_count; | |
262 | gtk_container_foreach(GTK_CONTAINER(w), count_callback, 0); | |
263 | } | |
264 | } | |
265 | ||
266 | static void count_widgets(void) { | |
267 | widget_count = 0; | |
268 | container_count = 1; | |
269 | if(toplevel) | |
270 | gtk_container_foreach(GTK_CONTAINER(toplevel), count_callback, 0); | |
271 | fprintf(stderr, "widget count: %8d container count: %8d\n", | |
272 | widget_count, container_count); | |
273 | } | |
274 | #endif | |
275 | ||
460b9539 | 276 | /* Called once every 10 minutes */ |
277 | static gboolean periodic(gpointer attribute((unused)) data) { | |
278 | D(("periodic")); | |
279 | /* Expire cached data */ | |
280 | cache_expire(); | |
281 | /* Update everything to be sure that the connection to the server hasn't | |
282 | * mysteriously gone stale on us. */ | |
283 | all_update(); | |
0b7c8909 | 284 | #if MDEBUG |
285 | count_widgets(); | |
286 | fprintf(stderr, "cache size: %zu\n", cache_count()); | |
287 | #endif | |
460b9539 | 288 | return TRUE; /* don't remove me */ |
289 | } | |
290 | ||
291 | static gboolean heartbeat(gpointer attribute((unused)) data) { | |
292 | static struct timeval last; | |
293 | struct timeval now; | |
294 | double delta; | |
295 | ||
296 | xgettimeofday(&now, 0); | |
297 | if(last.tv_sec) { | |
298 | delta = (now.tv_sec + now.tv_sec / 1.0E6) | |
299 | - (last.tv_sec + last.tv_sec / 1.0E6); | |
300 | if(delta >= 1.0625) | |
301 | fprintf(stderr, "%f: %fs between 1s heartbeats\n", | |
302 | now.tv_sec + now.tv_sec / 1.0E6, | |
303 | delta); | |
304 | } | |
305 | last = now; | |
306 | return TRUE; | |
307 | } | |
308 | ||
309 | /* main -------------------------------------------------------------------- */ | |
310 | ||
311 | static const struct option options[] = { | |
312 | { "help", no_argument, 0, 'h' }, | |
313 | { "version", no_argument, 0, 'V' }, | |
314 | { "config", required_argument, 0, 'c' }, | |
315 | { "tufnel", no_argument, 0, 't' }, | |
316 | { "choosealpha", no_argument, 0, 'C' }, | |
317 | { "debug", no_argument, 0, 'd' }, | |
318 | { 0, 0, 0, 0 } | |
319 | }; | |
320 | ||
321 | /* display usage message and terminate */ | |
322 | static void help(void) { | |
323 | xprintf("Disobedience - GUI client for DisOrder\n" | |
324 | "\n" | |
325 | "Usage:\n" | |
326 | " disobedience [OPTIONS]\n" | |
327 | "Options:\n" | |
328 | " --help, -h Display usage message\n" | |
329 | " --version, -V Display version number\n" | |
330 | " --config PATH, -c PATH Set configuration file\n" | |
331 | " --debug, -d Turn on debugging\n" | |
332 | "\n" | |
333 | "Also GTK+ options will work.\n"); | |
334 | xfclose(stdout); | |
335 | exit(0); | |
336 | } | |
337 | ||
338 | /* display version number and terminate */ | |
339 | static void version(void) { | |
340 | xprintf("disorder version %s\n", disorder_version_string); | |
341 | xfclose(stdout); | |
342 | exit(0); | |
343 | } | |
344 | ||
345 | int main(int argc, char **argv) { | |
346 | int n; | |
347 | disorder_eclient *logclient; | |
348 | ||
320598d4 | 349 | mem_init(); |
fe8f8518 RK |
350 | /* garbage-collect PCRE's memory */ |
351 | pcre_malloc = xmalloc; | |
352 | pcre_free = xfree; | |
460b9539 | 353 | if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale"); |
460b9539 | 354 | gtk_init(&argc, &argv); |
355 | gtk_rc_parse_string(style); | |
356 | while((n = getopt_long(argc, argv, "hVc:dtH", options, 0)) >= 0) { | |
357 | switch(n) { | |
358 | case 'h': help(); | |
359 | case 'V': version(); | |
360 | case 'c': configfile = optarg; break; | |
361 | case 'd': debugging = 1; break; | |
362 | case 't': goesupto = 11; break; | |
363 | case 'C': choosealpha = 1; break; /* not well tested any more */ | |
364 | default: fatal(0, "invalid option"); | |
365 | } | |
366 | } | |
346ba8d5 | 367 | signal(SIGPIPE, SIG_IGN); |
460b9539 | 368 | /* create the event loop */ |
369 | D(("create main loop")); | |
370 | mainloop = g_main_loop_new(0, 0); | |
371 | if(config_read()) fatal(0, "cannot read configuration"); | |
372 | /* create the clients */ | |
1c0d78bd RK |
373 | if(!(client = gtkclient()) |
374 | || !(logclient = gtkclient())) | |
375 | return 1; /* already reported an error */ | |
460b9539 | 376 | disorder_eclient_log(logclient, &gdisorder_log_callbacks, 0); |
377 | /* periodic operations (e.g. expiring the cache) */ | |
0b7c8909 | 378 | #if MDEBUG |
379 | g_timeout_add(5000/*milliseconds*/, periodic, 0); | |
380 | #else | |
460b9539 | 381 | g_timeout_add(600000/*milliseconds*/, periodic, 0); |
0b7c8909 | 382 | #endif |
460b9539 | 383 | /* The point of this is to try and get a handle on mysterious |
384 | * unresponsiveness. It's not very useful in production use. */ | |
385 | if(0) | |
386 | g_timeout_add(1000/*milliseconds*/, heartbeat, 0); | |
387 | make_toplevel_window(); | |
388 | /* reset styles now everything has its name */ | |
389 | gtk_rc_reset_styles(gtk_settings_get_for_screen(gdk_screen_get_default())); | |
390 | gtk_widget_show_all(toplevel); | |
391 | D(("enter main loop")); | |
392 | g_main_loop_run(mainloop); | |
393 | return 0; | |
394 | } | |
395 | ||
396 | /* | |
397 | Local Variables: | |
398 | c-basic-offset:2 | |
399 | comment-column:40 | |
400 | fill-column:79 | |
401 | indent-tabs-mode:nil | |
402 | End: | |
403 | */ |