chiark / gitweb /
Mini-mode now hides/shows tab group widget and has a separate label
[disorder] / disobedience / disobedience.c
... / ...
CommitLineData
1/*
2 * This file is part of DisOrder.
3 * Copyright (C) 2006-2009 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 3 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,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU 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, see <http://www.gnu.org/licenses/>.
17 */
18/** @file disobedience/disobedience.c
19 * @brief Main Disobedience program
20 */
21
22#include "disobedience.h"
23#include "version.h"
24
25#include <getopt.h>
26#include <locale.h>
27#include <pcre.h>
28#include <gcrypt.h>
29
30/* Apologies for the numerous de-consting casts, but GLib et al do not seem to
31 * have heard of const. */
32
33/* Variables --------------------------------------------------------------- */
34
35/** @brief Event loop */
36GMainLoop *mainloop;
37
38/** @brief Top-level window */
39GtkWidget *toplevel;
40
41/** @brief Label for progress indicator */
42GtkWidget *report_label;
43
44/** @brief Main tab group */
45GtkWidget *tabs;
46
47/** @brief Mini-mode widget for playing track */
48GtkWidget *playing_mini;
49
50/** @brief Main client */
51disorder_eclient *client;
52
53/** @brief Log client */
54disorder_eclient *logclient;
55
56/** @brief Last reported state
57 *
58 * This is updated by log_state().
59 */
60unsigned long last_state;
61
62/** @brief True if some track is playing
63 *
64 * This ought to be removed in favour of last_state & DISORDER_PLAYING
65 */
66int playing;
67
68/** @brief Left channel volume */
69int volume_l;
70
71/** @brief Right channel volume */
72int volume_r;
73
74/** @brief Audio backend */
75const struct uaudio *backend;
76
77double goesupto = 10; /* volume upper bound */
78
79/** @brief True if a NOP is in flight */
80static int nop_in_flight;
81
82/** @brief True if an rtp-address command is in flight */
83static int rtp_address_in_flight;
84
85/** @brief True if a rights lookup is in flight */
86static int rights_lookup_in_flight;
87
88/** @brief Current rights bitmap */
89rights_type last_rights;
90
91/** @brief True if RTP play is available
92 *
93 * This is a bit of a bodge...
94 */
95int rtp_supported;
96
97/** @brief True if RTP play is enabled */
98int rtp_is_running;
99
100/** @brief Server version */
101const char *server_version;
102
103/** @brief Parsed server version */
104long server_version_bytes;
105
106static GtkWidget *queue;
107
108static GtkWidget *notebook_box;
109
110static void check_rtp_address(const char *event,
111 void *eventdata,
112 void *callbackdata);
113
114/* Window creation --------------------------------------------------------- */
115
116/* Note that all the client operations kicked off from here will only complete
117 * after we've entered the event loop. */
118
119/** @brief Called when main window is deleted
120 *
121 * Terminates the program.
122 */
123static gboolean delete_event(GtkWidget attribute((unused)) *widget,
124 GdkEvent attribute((unused)) *event,
125 gpointer attribute((unused)) data) {
126 D(("delete_event"));
127 exit(0); /* die immediately */
128}
129
130/** @brief Called when the current tab is switched
131 *
132 * Updates the menu settings to correspond to the new page.
133 */
134static void tab_switched(GtkNotebook *notebook,
135 GtkNotebookPage attribute((unused)) *page,
136 guint page_num,
137 gpointer attribute((unused)) user_data) {
138 GtkWidget *const tab = gtk_notebook_get_nth_page(notebook, page_num);
139 const struct tabtype *const t = g_object_get_data(G_OBJECT(tab), "type");
140 assert(t != 0);
141 if(t->selected)
142 t->selected();
143}
144
145/** @brief Create the report box */
146static GtkWidget *report_box(void) {
147 GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
148
149 report_label = gtk_label_new("");
150 gtk_label_set_ellipsize(GTK_LABEL(report_label), PANGO_ELLIPSIZE_END);
151 gtk_misc_set_alignment(GTK_MISC(report_label), 0, 0);
152 gtk_container_add(GTK_CONTAINER(vbox), gtk_hseparator_new());
153 gtk_container_add(GTK_CONTAINER(vbox), report_label);
154 return vbox;
155}
156
157/** @brief Create and populate the main tab group */
158static GtkWidget *notebook(void) {
159 tabs = gtk_notebook_new();
160 /* The current tab is _NORMAL, the rest are _ACTIVE, which is bizarre but
161 * produces not too dreadful appearance */
162 gtk_widget_set_style(tabs, tool_style);
163 g_signal_connect(tabs, "switch-page", G_CALLBACK(tab_switched), 0);
164 gtk_notebook_append_page(GTK_NOTEBOOK(tabs), queue = queue_widget(),
165 gtk_label_new("Queue"));
166 gtk_notebook_append_page(GTK_NOTEBOOK(tabs), recent_widget(),
167 gtk_label_new("Recent"));
168 gtk_notebook_append_page(GTK_NOTEBOOK(tabs), choose_widget(),
169 gtk_label_new("Choose"));
170 gtk_notebook_append_page(GTK_NOTEBOOK(tabs), added_widget(),
171 gtk_label_new("Added"));
172 return tabs;
173}
174
175static void main_minimode(const char attribute((unused)) *event,
176 void attribute((unused)) *evendata,
177 void attribute((unused)) *callbackdata) {
178 if(full_mode) {
179 gtk_widget_show(tabs);
180 gtk_widget_hide(playing_mini);
181 /* Show the queue (bit confusing otherwise!) */
182 gtk_notebook_set_current_page(GTK_NOTEBOOK(tabs), 0);
183 } else {
184 gtk_widget_hide(tabs);
185 gtk_widget_show(playing_mini);
186 }
187}
188
189/** @brief Create and populate the main window */
190static void make_toplevel_window(void) {
191 GtkWidget *const vbox = gtk_vbox_new(FALSE, 1);
192 GtkWidget *const rb = report_box();
193
194 D(("top_window"));
195 toplevel = gtk_window_new(GTK_WINDOW_TOPLEVEL);
196 /* default size is too small */
197 gtk_window_set_default_size(GTK_WINDOW(toplevel), 640, 480);
198 /* terminate on close */
199 g_signal_connect(G_OBJECT(toplevel), "delete_event",
200 G_CALLBACK(delete_event), NULL);
201 /* lay out the window */
202 gtk_window_set_title(GTK_WINDOW(toplevel), "Disobedience");
203 gtk_container_add(GTK_CONTAINER(toplevel), vbox);
204 /* lay out the vbox */
205 gtk_box_pack_start(GTK_BOX(vbox),
206 menubar(toplevel),
207 FALSE, /* expand */
208 FALSE, /* fill */
209 0);
210 gtk_box_pack_start(GTK_BOX(vbox),
211 control_widget(),
212 FALSE, /* expand */
213 FALSE, /* fill */
214 0);
215 playing_mini = playing_widget();
216 gtk_box_pack_start(GTK_BOX(vbox),
217 playing_mini,
218 FALSE,
219 FALSE,
220 0);
221 notebook_box = gtk_vbox_new(FALSE, 0);
222 gtk_container_add(GTK_CONTAINER(notebook_box), notebook());
223 gtk_container_add(GTK_CONTAINER(vbox), notebook_box);
224 gtk_box_pack_end(GTK_BOX(vbox),
225 rb,
226 FALSE, /* expand */
227 FALSE, /* fill */
228 0);
229 gtk_widget_set_style(toplevel, tool_style);
230 event_register("mini-mode-changed", main_minimode, 0);
231}
232
233static void userinfo_rights_completed(void attribute((unused)) *v,
234 const char *err,
235 const char *value) {
236 rights_type r;
237
238 if(err) {
239 popup_protocol_error(0, err);
240 r = 0;
241 } else {
242 if(parse_rights(value, &r, 0))
243 r = 0;
244 }
245 /* If rights have changed, signal everything that cares */
246 if(r != last_rights) {
247 last_rights = r;
248 ++suppress_actions;
249 event_raise("rights-changed", 0);
250 --suppress_actions;
251 }
252 rights_lookup_in_flight = 0;
253}
254
255static void check_rights(void) {
256 if(!rights_lookup_in_flight) {
257 rights_lookup_in_flight = 1;
258 disorder_eclient_userinfo(client,
259 userinfo_rights_completed,
260 config->username, "rights",
261 0);
262 }
263}
264
265/** @brief Called occasionally */
266static gboolean periodic_slow(gpointer attribute((unused)) data) {
267 D(("periodic_slow"));
268 /* Expire cached data */
269 cache_expire();
270 /* Update everything to be sure that the connection to the server hasn't
271 * mysteriously gone stale on us. */
272 all_update();
273 event_raise("periodic-slow", 0);
274 /* Recheck RTP status too */
275 check_rtp_address(0, 0, 0);
276 return TRUE; /* don't remove me */
277}
278
279/** @brief Called frequently */
280static gboolean periodic_fast(gpointer attribute((unused)) data) {
281#if 0 /* debugging hack */
282 static struct timeval last;
283 struct timeval now;
284 double delta;
285
286 xgettimeofday(&now, 0);
287 if(last.tv_sec) {
288 delta = (now.tv_sec + now.tv_sec / 1.0E6)
289 - (last.tv_sec + last.tv_sec / 1.0E6);
290 if(delta >= 1.0625)
291 fprintf(stderr, "%f: %fs between 1s heartbeats\n",
292 now.tv_sec + now.tv_sec / 1.0E6,
293 delta);
294 }
295 last = now;
296#endif
297 if(rtp_supported && backend && backend->get_volume) {
298 int nl, nr;
299 backend->get_volume(&nl, &nr);
300 if(nl != volume_l || nr != volume_r) {
301 volume_l = nl;
302 volume_r = nr;
303 event_raise("volume-changed", 0);
304 }
305 }
306 /* Periodically check what our rights are */
307 int recheck_rights = 1;
308 if(server_version_bytes >= 0x04010000)
309 /* Server versions after 4.1 will send updates */
310 recheck_rights = 0;
311 if((server_version_bytes & 0xFF) == 0x01)
312 /* Development servers might do regardless of their version number */
313 recheck_rights = 0;
314 if(recheck_rights)
315 check_rights();
316 event_raise("periodic-fast", 0);
317 return TRUE;
318}
319
320/** @brief Called when a NOP completes */
321static void nop_completed(void attribute((unused)) *v,
322 const char attribute((unused)) *err) {
323 /* TODO report the error somewhere */
324 nop_in_flight = 0;
325}
326
327/** @brief Called from time to time to arrange for a NOP to be sent
328 *
329 * At most one NOP remains in flight at any moment. If the client is not
330 * currently connected then no NOP is sent.
331 */
332static gboolean maybe_send_nop(gpointer attribute((unused)) data) {
333 if(!nop_in_flight && (disorder_eclient_state(client) & DISORDER_CONNECTED)) {
334 nop_in_flight = 1;
335 disorder_eclient_nop(client, nop_completed, 0);
336 }
337 if(rtp_supported) {
338 const int rtp_was_running = rtp_is_running;
339 rtp_is_running = rtp_running();
340 if(rtp_was_running != rtp_is_running)
341 event_raise("rtp-changed", 0);
342 }
343 return TRUE; /* keep call me please */
344}
345
346/** @brief Called when a rtp-address command succeeds */
347static void got_rtp_address(void attribute((unused)) *v,
348 const char *err,
349 int attribute((unused)) nvec,
350 char attribute((unused)) **vec) {
351 const int rtp_was_supported = rtp_supported;
352 const int rtp_was_running = rtp_is_running;
353
354 ++suppress_actions;
355 rtp_address_in_flight = 0;
356 if(err) {
357 /* An error just means that we're not using network play */
358 rtp_supported = 0;
359 rtp_is_running = 0;
360 } else {
361 rtp_supported = 1;
362 rtp_is_running = rtp_running();
363 }
364 /*fprintf(stderr, "rtp supported->%d, running->%d\n",
365 rtp_supported, rtp_is_running);*/
366 if(rtp_supported != rtp_was_supported
367 || rtp_is_running != rtp_was_running)
368 event_raise("rtp-changed", 0);
369 --suppress_actions;
370}
371
372/** @brief Called to check whether RTP play is available */
373static void check_rtp_address(const char attribute((unused)) *event,
374 void attribute((unused)) *eventdata,
375 void attribute((unused)) *callbackdata) {
376 if(!rtp_address_in_flight) {
377 //fprintf(stderr, "checking rtp\n");
378 disorder_eclient_rtp_address(client, got_rtp_address, NULL);
379 }
380}
381
382/* main -------------------------------------------------------------------- */
383
384static const struct option options[] = {
385 { "help", no_argument, 0, 'h' },
386 { "version", no_argument, 0, 'V' },
387 { "config", required_argument, 0, 'c' },
388 { "tufnel", no_argument, 0, 't' },
389 { "debug", no_argument, 0, 'd' },
390 { 0, 0, 0, 0 }
391};
392
393/* display usage message and terminate */
394static void help(void) {
395 xprintf("Disobedience - GUI client for DisOrder\n"
396 "\n"
397 "Usage:\n"
398 " disobedience [OPTIONS]\n"
399 "Options:\n"
400 " --help, -h Display usage message\n"
401 " --version, -V Display version number\n"
402 " --config PATH, -c PATH Set configuration file\n"
403 " --debug, -d Turn on debugging\n"
404 "\n"
405 "Also GTK+ options will work.\n");
406 xfclose(stdout);
407 exit(0);
408}
409
410static void version_completed(void attribute((unused)) *v,
411 const char attribute((unused)) *err,
412 const char *ver) {
413 long major, minor, patch, dev;
414
415 if(!ver) {
416 server_version = 0;
417 server_version_bytes = 0;
418 return;
419 }
420 server_version = ver;
421 server_version_bytes = 0;
422 major = strtol(ver, (char **)&ver, 10);
423 if(*ver != '.')
424 return;
425 ++ver;
426 minor = strtol(ver, (char **)&ver, 10);
427 if(*ver == '.') {
428 ++ver;
429 patch = strtol(ver, (char **)&ver, 10);
430 } else
431 patch = 0;
432 if(*ver) {
433 if(*ver == '+') {
434 dev = 1;
435 ++ver;
436 }
437 if(*ver)
438 dev = 2;
439 } else
440 dev = 0;
441 server_version_bytes = (major << 24) + (minor << 16) + (patch << 8) + dev;
442}
443
444void logged_in(void) {
445 /* reset the clients */
446 disorder_eclient_close(client);
447 disorder_eclient_close(logclient);
448 rtp_supported = 0;
449 event_raise("logged-in", 0);
450 /* Force the periodic checks */
451 periodic_slow(0);
452 periodic_fast(0);
453 /* Recheck server version */
454 disorder_eclient_version(client, version_completed, 0);
455 disorder_eclient_enable_connect(client);
456 disorder_eclient_enable_connect(logclient);
457}
458
459int main(int argc, char **argv) {
460 int n;
461 gboolean gtkok;
462
463 mem_init();
464 /* garbage-collect PCRE's memory */
465 pcre_malloc = xmalloc;
466 pcre_free = xfree;
467 if(!setlocale(LC_CTYPE, "")) disorder_fatal(errno, "error calling setlocale");
468 gtkok = gtk_init_check(&argc, &argv);
469 while((n = getopt_long(argc, argv, "hVc:dtHC", options, 0)) >= 0) {
470 switch(n) {
471 case 'h': help();
472 case 'V': version("disobedience");
473 case 'c': configfile = optarg; break;
474 case 'd': debugging = 1; break;
475 case 't': goesupto = 11; break;
476 default: disorder_fatal(0, "invalid option");
477 }
478 }
479 if(!gtkok)
480 disorder_fatal(0, "failed to initialize GTK+");
481 /* gcrypt initialization */
482 if(!gcry_check_version(NULL))
483 disorder_fatal(0, "gcry_check_version failed");
484 gcry_control(GCRYCTL_INIT_SECMEM, 0);
485 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
486 signal(SIGPIPE, SIG_IGN);
487 init_styles();
488 load_settings();
489 /* create the event loop */
490 D(("create main loop"));
491 mainloop = g_main_loop_new(0, 0);
492 if(config_read(0, NULL)) disorder_fatal(0, "cannot read configuration");
493 /* we'll need mixer support */
494 backend = uaudio_apis[0];
495 if(backend->configure)
496 backend->configure();
497 if(backend->open_mixer)
498 backend->open_mixer();
499 /* create the clients */
500 if(!(client = gtkclient())
501 || !(logclient = gtkclient()))
502 return 1; /* already reported an error */
503 /* periodic operations (e.g. expiring the cache, checking local volume) */
504 g_timeout_add(600000/*milliseconds*/, periodic_slow, 0);
505 g_timeout_add(1000/*milliseconds*/, periodic_fast, 0);
506 make_toplevel_window();
507 /* reset styles now everything has its name */
508 gtk_rc_reset_styles(gtk_settings_get_for_screen(gdk_screen_get_default()));
509 gtk_widget_show_all(toplevel);
510 gtk_widget_hide(playing_mini);
511 /* issue a NOP every so often */
512 g_timeout_add_full(G_PRIORITY_LOW,
513 2000/*interval, ms*/,
514 maybe_send_nop,
515 0/*data*/,
516 0/*notify*/);
517 /* Start monitoring the log */
518 disorder_eclient_log(logclient, &log_callbacks, 0);
519 /* Initiate all the checks */
520 periodic_fast(0);
521 disorder_eclient_version(client, version_completed, 0);
522 event_register("log-connected", check_rtp_address, 0);
523 suppress_actions = 0;
524 playlists_init();
525 /* If no password is set yet pop up a login box */
526 if(!config->password)
527 login_box();
528 D(("enter main loop"));
529 g_main_loop_run(mainloop);
530 return 0;
531}
532
533/*
534Local Variables:
535c-basic-offset:2
536comment-column:40
537fill-column:79
538indent-tabs-mode:nil
539End:
540*/