2 * This file is part of DisOrder.
3 * Copyright (C) 2006, 2007 Richard Kettlewell
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.
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.
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
21 #include "disobedience.h"
27 /* Apologies for the numerous de-consting casts, but GLib et al do not seem to
28 * have heard of const. */
30 #include "style.h" /* generated style */
32 /* Variables --------------------------------------------------------------- */
34 GMainLoop *mainloop; /* event loop */
35 GtkWidget *toplevel; /* top level window */
36 GtkWidget *report_label; /* label for progress indicator */
37 GtkWidget *tabs; /* main tabs */
39 disorder_eclient *client; /* main client */
41 unsigned long last_state; /* last reported state */
42 int playing; /* true if playing some track */
43 int volume_l, volume_r; /* volume */
44 double goesupto = 10; /* volume upper bound */
45 int choosealpha; /* break up choose by letter */
47 /** @brief True if a NOP is in flight */
48 static int nop_in_flight;
50 /* Window creation --------------------------------------------------------- */
52 /* Note that all the client operations kicked off from here will only complete
53 * after we've entered the event loop. */
55 static gboolean delete_event(GtkWidget attribute((unused)) *widget,
56 GdkEvent attribute((unused)) *event,
57 gpointer attribute((unused)) data) {
59 exit(0); /* die immediately */
62 /* Called when the current tab is switched. */
63 static void tab_switched(GtkNotebook attribute((unused)) *notebook,
64 GtkNotebookPage attribute((unused)) *page,
66 gpointer attribute((unused)) user_data) {
67 menu_update(page_num);
70 static GtkWidget *report_box(void) {
71 GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
73 report_label = gtk_label_new("");
74 gtk_label_set_ellipsize(GTK_LABEL(report_label), PANGO_ELLIPSIZE_END);
75 gtk_misc_set_alignment(GTK_MISC(report_label), 0, 0);
76 gtk_container_add(GTK_CONTAINER(vbox), gtk_hseparator_new());
77 gtk_container_add(GTK_CONTAINER(vbox), report_label);
81 static GtkWidget *notebook(void) {
82 tabs = gtk_notebook_new();
83 g_signal_connect(tabs, "switch-page", G_CALLBACK(tab_switched), 0);
84 gtk_notebook_append_page(GTK_NOTEBOOK(tabs), queue_widget(),
85 gtk_label_new("Queue"));
86 gtk_notebook_append_page(GTK_NOTEBOOK(tabs), recent_widget(),
87 gtk_label_new("Recent"));
88 gtk_notebook_append_page(GTK_NOTEBOOK(tabs), choose_widget(),
89 gtk_label_new("Choose"));
93 static void make_toplevel_window(void) {
94 GtkWidget *const vbox = gtk_vbox_new(FALSE, 1);
95 GtkWidget *const rb = report_box();
98 toplevel = gtk_window_new(GTK_WINDOW_TOPLEVEL);
99 /* default size is too small */
100 gtk_window_set_default_size(GTK_WINDOW(toplevel), 640, 480);
101 /* terminate on close */
102 g_signal_connect(G_OBJECT(toplevel), "delete_event",
103 G_CALLBACK(delete_event), NULL);
104 /* lay out the window */
105 gtk_window_set_title(GTK_WINDOW(toplevel), "Disobedience");
106 gtk_container_add(GTK_CONTAINER(toplevel), vbox);
107 /* lay out the vbox */
108 gtk_box_pack_start(GTK_BOX(vbox),
113 gtk_box_pack_start(GTK_BOX(vbox),
118 gtk_container_add(GTK_CONTAINER(vbox), notebook());
119 gtk_box_pack_end(GTK_BOX(vbox),
124 gtk_widget_set_name(toplevel, "disobedience");
128 static int widget_count, container_count;
130 static void count_callback(GtkWidget *w,
131 gpointer attribute((unused)) data) {
133 if(GTK_IS_CONTAINER(w)) {
135 gtk_container_foreach(GTK_CONTAINER(w), count_callback, 0);
139 static void count_widgets(void) {
143 gtk_container_foreach(GTK_CONTAINER(toplevel), count_callback, 0);
144 fprintf(stderr, "widget count: %8d container count: %8d\n",
145 widget_count, container_count);
150 const char *mtag = "init";
151 static hash *mtrack_hash;
153 static int *mthfind(const char *tag) {
154 static const int zero = 0;
155 int *cp = hash_find(mtrack_hash, tag);
157 hash_add(mtrack_hash, tag, &zero, HASH_INSERT);
158 cp = hash_find(mtrack_hash, tag);
163 static void *trap_malloc(size_t n) {
164 void *ptr = malloc(n + sizeof(char *));
166 *(const char **)ptr = mtag;
168 return (char *)ptr + sizeof(char *);
171 static void trap_free(void *ptr) {
175 ptr = (char *)ptr - sizeof(char *);
176 tag = *(const char **)ptr;
181 static void *trap_realloc(void *ptr, size_t n) {
183 return trap_malloc(n);
188 ptr = (char *)ptr - sizeof(char *);
189 ptr = realloc(ptr, n + sizeof(char *));
190 *(const char **)ptr = mtag;
191 return (char *)ptr + sizeof(char *);
194 static int report_tags_callback(const char *key, void *value,
195 void attribute((unused)) *u) {
196 fprintf(stderr, "%16s: %d\n", key, *(int *)value);
200 static void report_tags(void) {
201 hash_foreach(mtrack_hash, report_tags_callback, 0);
202 fprintf(stderr, "\n");
205 static const GMemVTable glib_memvtable = {
215 /* Called once every 10 minutes */
216 static gboolean periodic(gpointer attribute((unused)) data) {
218 /* Expire cached data */
220 /* Update everything to be sure that the connection to the server hasn't
221 * mysteriously gone stale on us. */
225 fprintf(stderr, "cache size: %zu\n", cache_count());
230 return TRUE; /* don't remove me */
233 static gboolean heartbeat(gpointer attribute((unused)) data) {
234 static struct timeval last;
238 xgettimeofday(&now, 0);
240 delta = (now.tv_sec + now.tv_sec / 1.0E6)
241 - (last.tv_sec + last.tv_sec / 1.0E6);
243 fprintf(stderr, "%f: %fs between 1s heartbeats\n",
244 now.tv_sec + now.tv_sec / 1.0E6,
251 /** @brief Called when a NOP completes */
252 static void nop_completed(void attribute((unused)) *v) {
256 /** @brief Called from time to time to arrange for a NOP to be sent
258 * At most one NOP remains in flight at any moment. If the client is not
259 * currently connected then no NOP is sent.
261 static gboolean maybe_send_nop(gpointer attribute((unused)) data) {
262 if(!nop_in_flight && (disorder_eclient_state(client) & DISORDER_CONNECTED)) {
264 disorder_eclient_nop(client, nop_completed, 0);
266 return TRUE; /* keep call me please */
269 /* main -------------------------------------------------------------------- */
271 static const struct option options[] = {
272 { "help", no_argument, 0, 'h' },
273 { "version", no_argument, 0, 'V' },
274 { "config", required_argument, 0, 'c' },
275 { "tufnel", no_argument, 0, 't' },
276 { "choosealpha", no_argument, 0, 'C' },
277 { "debug", no_argument, 0, 'd' },
281 /* display usage message and terminate */
282 static void help(void) {
283 xprintf("Disobedience - GUI client for DisOrder\n"
286 " disobedience [OPTIONS]\n"
288 " --help, -h Display usage message\n"
289 " --version, -V Display version number\n"
290 " --config PATH, -c PATH Set configuration file\n"
291 " --debug, -d Turn on debugging\n"
293 "Also GTK+ options will work.\n");
298 /* display version number and terminate */
299 static void version(void) {
300 xprintf("disorder version %s\n", disorder_version_string);
305 int main(int argc, char **argv) {
307 disorder_eclient *logclient;
310 /* garbage-collect PCRE's memory */
311 pcre_malloc = xmalloc;
314 mtrack_hash = hash_new(sizeof (int));
315 g_mem_set_vtable((GMemVTable *)&glib_memvtable);
317 if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
318 gtk_init(&argc, &argv);
319 gtk_rc_parse_string(style);
320 while((n = getopt_long(argc, argv, "hVc:dtH", options, 0)) >= 0) {
324 case 'c': configfile = optarg; break;
325 case 'd': debugging = 1; break;
326 case 't': goesupto = 11; break;
327 case 'C': choosealpha = 1; break; /* not well tested any more */
328 default: fatal(0, "invalid option");
331 signal(SIGPIPE, SIG_IGN);
332 /* create the event loop */
333 D(("create main loop"));
334 mainloop = g_main_loop_new(0, 0);
335 if(config_read()) fatal(0, "cannot read configuration");
336 /* create the clients */
337 if(!(client = gtkclient())
338 || !(logclient = gtkclient()))
339 return 1; /* already reported an error */
340 disorder_eclient_log(logclient, &log_callbacks, 0);
341 /* periodic operations (e.g. expiring the cache) */
343 g_timeout_add(5000/*milliseconds*/, periodic, 0);
345 g_timeout_add(600000/*milliseconds*/, periodic, 0);
347 /* The point of this is to try and get a handle on mysterious
348 * unresponsiveness. It's not very useful in production use. */
350 g_timeout_add(1000/*milliseconds*/, heartbeat, 0);
351 make_toplevel_window();
352 /* reset styles now everything has its name */
353 gtk_rc_reset_styles(gtk_settings_get_for_screen(gdk_screen_get_default()));
354 gtk_widget_show_all(toplevel);
355 /* set initial control button visibility/usability */
357 /* issue a NOP every so often */
358 g_timeout_add_full(G_PRIORITY_LOW,
359 1000/*interval, ms*/,
363 D(("enter main loop"));
365 g_main_loop_run(mainloop);