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