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