chiark / gitweb /
Merge minimode improvements and a Disobedience bugfix
[disorder] / disobedience / disobedience.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
4778e044 3 * Copyright (C) 2006-2009 Richard Kettlewell
460b9539 4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
460b9539 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
460b9539 8 * (at your option) any later version.
9 *
e7eb3a27
RK
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 *
460b9539 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
460b9539 17 */
219dc95c
RK
18/** @file disobedience/disobedience.c
19 * @brief Main Disobedience program
20 */
460b9539 21
22#include "disobedience.h"
3fbdc96d 23#include "version.h"
460b9539 24
25#include <getopt.h>
26#include <locale.h>
fe8f8518 27#include <pcre.h>
5bf7c546 28#include <gcrypt.h>
460b9539 29
30/* Apologies for the numerous de-consting casts, but GLib et al do not seem to
31 * have heard of const. */
32
460b9539 33/* Variables --------------------------------------------------------------- */
34
219dc95c
RK
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
68be70c0
RK
47/** @brief Mini-mode widget for playing track */
48GtkWidget *playing_mini;
49
219dc95c
RK
50/** @brief Main client */
51disorder_eclient *client;
460b9539 52
73f1b9f3
RK
53/** @brief Log client */
54disorder_eclient *logclient;
55
219dc95c
RK
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;
460b9539 73
b50cfb8a
RK
74/** @brief Audio backend */
75const struct uaudio *backend;
76
460b9539 77double goesupto = 10; /* volume upper bound */
219dc95c 78
7858930d 79/** @brief True if a NOP is in flight */
80static int nop_in_flight;
81
a99c4e9a
RK
82/** @brief True if an rtp-address command is in flight */
83static int rtp_address_in_flight;
84
6f09d54d
RK
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
a99c4e9a
RK
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
45e6d04a
RK
100/** @brief Server version */
101const char *server_version;
102
103/** @brief Parsed server version */
104long server_version_bytes;
105
ac904902
RK
106static GtkWidget *queue;
107
108static GtkWidget *notebook_box;
109
00e0c652
RK
110static void check_rtp_address(const char *event,
111 void *eventdata,
112 void *callbackdata);
adafcbcf 113
460b9539 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
219dc95c
RK
119/** @brief Called when main window is deleted
120 *
121 * Terminates the program.
122 */
460b9539 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
219dc95c
RK
130/** @brief Called when the current tab is switched
131 *
132 * Updates the menu settings to correspond to the new page.
133 */
2a3fe554 134static void tab_switched(GtkNotebook *notebook,
460b9539 135 GtkNotebookPage attribute((unused)) *page,
136 guint page_num,
137 gpointer attribute((unused)) user_data) {
2a3fe554
RK
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");
ee7552f8 140 assert(t != 0);
2a3fe554
RK
141 if(t->selected)
142 t->selected();
460b9539 143}
144
219dc95c 145/** @brief Create the report box */
460b9539 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
219dc95c 157/** @brief Create and populate the main tab group */
460b9539 158static GtkWidget *notebook(void) {
159 tabs = gtk_notebook_new();
d4ef4132
RK
160 /* The current tab is _NORMAL, the rest are _ACTIVE, which is bizarre but
161 * produces not too dreadful appearance */
33288048 162 gtk_widget_set_style(tabs, tool_style);
460b9539 163 g_signal_connect(tabs, "switch-page", G_CALLBACK(tab_switched), 0);
ac904902 164 gtk_notebook_append_page(GTK_NOTEBOOK(tabs), queue = queue_widget(),
460b9539 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"));
4eb1f430
RK
170 gtk_notebook_append_page(GTK_NOTEBOOK(tabs), added_widget(),
171 gtk_label_new("Added"));
460b9539 172 return tabs;
173}
174
ac904902
RK
175static void main_minimode(const char attribute((unused)) *event,
176 void attribute((unused)) *evendata,
177 void attribute((unused)) *callbackdata) {
ac904902 178 if(full_mode) {
68be70c0
RK
179 gtk_widget_show(tabs);
180 gtk_widget_hide(playing_mini);
ac904902
RK
181 /* Show the queue (bit confusing otherwise!) */
182 gtk_notebook_set_current_page(GTK_NOTEBOOK(tabs), 0);
183 } else {
68be70c0
RK
184 gtk_widget_hide(tabs);
185 gtk_widget_show(playing_mini);
ac904902 186 }
ac904902
RK
187}
188
219dc95c 189/** @brief Create and populate the main window */
460b9539 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);
68be70c0
RK
215 playing_mini = playing_widget();
216 gtk_box_pack_start(GTK_BOX(vbox),
217 playing_mini,
218 FALSE,
219 FALSE,
220 0);
ac904902
RK
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);
460b9539 224 gtk_box_pack_end(GTK_BOX(vbox),
225 rb,
226 FALSE, /* expand */
227 FALSE, /* fill */
228 0);
33288048 229 gtk_widget_set_style(toplevel, tool_style);
ac904902 230 event_register("mini-mode-changed", main_minimode, 0);
460b9539 231}
232
6f09d54d 233static void userinfo_rights_completed(void attribute((unused)) *v,
abf99697 234 const char *err,
6f09d54d
RK
235 const char *value) {
236 rights_type r;
237
abf99697
RK
238 if(err) {
239 popup_protocol_error(0, err);
6f09d54d
RK
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
45e6d04a
RK
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
321f7536
RK
265/** @brief Called occasionally */
266static gboolean periodic_slow(gpointer attribute((unused)) data) {
6f09d54d 267 D(("periodic_slow"));
460b9539 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();
fc36ecb7 273 event_raise("periodic-slow", 0);
adafcbcf 274 /* Recheck RTP status too */
00e0c652 275 check_rtp_address(0, 0, 0);
460b9539 276 return TRUE; /* don't remove me */
277}
278
321f7536
RK
279/** @brief Called frequently */
280static gboolean periodic_fast(gpointer attribute((unused)) data) {
281#if 0 /* debugging hack */
460b9539 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;
321f7536 296#endif
b50cfb8a 297 if(rtp_supported && backend && backend->get_volume) {
321f7536 298 int nl, nr;
b50cfb8a
RK
299 backend->get_volume(&nl, &nr);
300 if(nl != volume_l || nr != volume_r) {
321f7536
RK
301 volume_l = nl;
302 volume_r = nr;
1f868ca3 303 event_raise("volume-changed", 0);
321f7536
RK
304 }
305 }
00e0c652 306 /* Periodically check what our rights are */
45e6d04a
RK
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();
fc36ecb7 316 event_raise("periodic-fast", 0);
460b9539 317 return TRUE;
318}
319
7858930d 320/** @brief Called when a NOP completes */
53fa91bb 321static void nop_completed(void attribute((unused)) *v,
abf99697 322 const char attribute((unused)) *err) {
53fa91bb 323 /* TODO report the error somewhere */
7858930d 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) {
8f763f1b 333 if(!nop_in_flight && (disorder_eclient_state(client) & DISORDER_CONNECTED)) {
7858930d 334 nop_in_flight = 1;
335 disorder_eclient_nop(client, nop_completed, 0);
336 }
a99c4e9a 337 if(rtp_supported) {
6c7a654c 338 const int rtp_was_running = rtp_is_running;
a99c4e9a 339 rtp_is_running = rtp_running();
6c7a654c
RK
340 if(rtp_was_running != rtp_is_running)
341 event_raise("rtp-changed", 0);
a99c4e9a 342 }
7858930d 343 return TRUE; /* keep call me please */
344}
345
a99c4e9a
RK
346/** @brief Called when a rtp-address command succeeds */
347static void got_rtp_address(void attribute((unused)) *v,
abf99697 348 const char *err,
a99c4e9a
RK
349 int attribute((unused)) nvec,
350 char attribute((unused)) **vec) {
3849817f 351 const int rtp_was_supported = rtp_supported;
6c7a654c
RK
352 const int rtp_was_running = rtp_is_running;
353
fb44b59e 354 ++suppress_actions;
a99c4e9a 355 rtp_address_in_flight = 0;
abf99697 356 if(err) {
4190a4d9
RK
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();
4190a4d9 363 }
00e0c652
RK
364 /*fprintf(stderr, "rtp supported->%d, running->%d\n",
365 rtp_supported, rtp_is_running);*/
3849817f
RK
366 if(rtp_supported != rtp_was_supported
367 || rtp_is_running != rtp_was_running)
6c7a654c 368 event_raise("rtp-changed", 0);
fb44b59e 369 --suppress_actions;
a99c4e9a
RK
370}
371
372/** @brief Called to check whether RTP play is available */
00e0c652
RK
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");
4190a4d9 378 disorder_eclient_rtp_address(client, got_rtp_address, NULL);
00e0c652 379 }
a99c4e9a
RK
380}
381
460b9539 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' },
460b9539 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
45e6d04a
RK
410static void version_completed(void attribute((unused)) *v,
411 const char attribute((unused)) *err,
3665689a 412 const char *ver) {
45e6d04a
RK
413 long major, minor, patch, dev;
414
3665689a 415 if(!ver) {
45e6d04a
RK
416 server_version = 0;
417 server_version_bytes = 0;
418 return;
419 }
3665689a 420 server_version = ver;
45e6d04a 421 server_version_bytes = 0;
3665689a
RK
422 major = strtol(ver, (char **)&ver, 10);
423 if(*ver != '.')
45e6d04a 424 return;
3665689a
RK
425 ++ver;
426 minor = strtol(ver, (char **)&ver, 10);
427 if(*ver == '.') {
428 ++ver;
429 patch = strtol(ver, (char **)&ver, 10);
45e6d04a
RK
430 } else
431 patch = 0;
3665689a
RK
432 if(*ver) {
433 if(*ver == '+') {
45e6d04a 434 dev = 1;
3665689a 435 ++ver;
45e6d04a 436 }
3665689a 437 if(*ver)
45e6d04a
RK
438 dev = 2;
439 } else
440 dev = 0;
441 server_version_bytes = (major << 24) + (minor << 16) + (patch << 8) + dev;
442}
443
7c30fc75 444void logged_in(void) {
73f1b9f3
RK
445 /* reset the clients */
446 disorder_eclient_close(client);
447 disorder_eclient_close(logclient);
a99c4e9a 448 rtp_supported = 0;
7c30fc75 449 event_raise("logged-in", 0);
6f09d54d 450 /* Force the periodic checks */
6f09d54d
RK
451 periodic_slow(0);
452 periodic_fast(0);
45e6d04a
RK
453 /* Recheck server version */
454 disorder_eclient_version(client, version_completed, 0);
68210888
RK
455 disorder_eclient_enable_connect(client);
456 disorder_eclient_enable_connect(logclient);
73f1b9f3
RK
457}
458
460b9539 459int main(int argc, char **argv) {
460 int n;
ce73fea7 461 gboolean gtkok;
460b9539 462
320598d4 463 mem_init();
fe8f8518
RK
464 /* garbage-collect PCRE's memory */
465 pcre_malloc = xmalloc;
466 pcre_free = xfree;
2e9ba080 467 if(!setlocale(LC_CTYPE, "")) disorder_fatal(errno, "error calling setlocale");
ce73fea7 468 gtkok = gtk_init_check(&argc, &argv);
c3df9503 469 while((n = getopt_long(argc, argv, "hVc:dtHC", options, 0)) >= 0) {
460b9539 470 switch(n) {
471 case 'h': help();
3fbdc96d 472 case 'V': version("disobedience");
460b9539 473 case 'c': configfile = optarg; break;
474 case 'd': debugging = 1; break;
475 case 't': goesupto = 11; break;
2e9ba080 476 default: disorder_fatal(0, "invalid option");
460b9539 477 }
478 }
ce73fea7 479 if(!gtkok)
2e9ba080 480 disorder_fatal(0, "failed to initialize GTK+");
5bf7c546
RK
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);
346ba8d5 486 signal(SIGPIPE, SIG_IGN);
33288048 487 init_styles();
b4f8eba1 488 load_settings();
460b9539 489 /* create the event loop */
490 D(("create main loop"));
491 mainloop = g_main_loop_new(0, 0);
2e9ba080 492 if(config_read(0, NULL)) disorder_fatal(0, "cannot read configuration");
b50cfb8a
RK
493 /* we'll need mixer support */
494 backend = uaudio_apis[0];
61ad0645
RK
495 if(backend->configure)
496 backend->configure();
b50cfb8a
RK
497 if(backend->open_mixer)
498 backend->open_mixer();
460b9539 499 /* create the clients */
1c0d78bd
RK
500 if(!(client = gtkclient())
501 || !(logclient = gtkclient()))
502 return 1; /* already reported an error */
321f7536 503 /* periodic operations (e.g. expiring the cache, checking local volume) */
00e0c652 504 g_timeout_add(600000/*milliseconds*/, periodic_slow, 0);
321f7536 505 g_timeout_add(1000/*milliseconds*/, periodic_fast, 0);
460b9539 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);
68be70c0 510 gtk_widget_hide(playing_mini);
7858930d 511 /* issue a NOP every so often */
512 g_timeout_add_full(G_PRIORITY_LOW,
d37d5336 513 2000/*interval, ms*/,
7858930d 514 maybe_send_nop,
515 0/*data*/,
516 0/*notify*/);
6d1302f0
RK
517 /* Start monitoring the log */
518 disorder_eclient_log(logclient, &log_callbacks, 0);
6f09d54d 519 /* Initiate all the checks */
6f09d54d 520 periodic_fast(0);
45e6d04a 521 disorder_eclient_version(client, version_completed, 0);
00e0c652 522 event_register("log-connected", check_rtp_address, 0);
fb44b59e 523 suppress_actions = 0;
fc36ecb7 524 playlists_init();
bf5fdf14
RK
525 /* If no password is set yet pop up a login box */
526 if(!config->password)
527 login_box();
460b9539 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*/