chiark / gitweb /
Send clients a rights-changed message when their rights change.
[disorder] / disobedience / control.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
3849817f 3 * Copyright (C) 2006-2008 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 */
219dc95c
RK
20/** @file disobedience/control.c
21 * @brief Volume control and buttons
22 */
460b9539 23
24#include "disobedience.h"
321f7536 25#include "mixer.h"
460b9539 26
e9b70a84 27/* Forward declarations ---------------------------------------------------- */
28
460b9539 29struct icon;
30
460b9539 31static void clicked_icon(GtkButton *, gpointer);
ac6bf2ba 32static void clicked_menu(GtkMenuItem *, gpointer userdata);
2d504956 33static void toggled_menu(GtkCheckMenuItem *, gpointer userdata);
460b9539 34
a99c4e9a
RK
35static int enable_rtp(disorder_eclient *c,
36 disorder_eclient_no_response *completed,
37 void *v);
38static int disable_rtp(disorder_eclient *c,
39 disorder_eclient_no_response *completed,
40 void *v);
41
460b9539 42static double left(double v, double b);
43static double right(double v, double b);
44static double volume(double l, double r);
45static double balance(double l, double r);
46
47static void volume_adjusted(GtkAdjustment *a, gpointer user_data);
48static gchar *format_volume(GtkScale *scale, gdouble value);
49static gchar *format_balance(GtkScale *scale, gdouble value);
50
6856f665
RK
51static void icon_changed(const char *event,
52 void *evendata,
53 void *callbackdata);
1f868ca3
RK
54static void volume_changed(const char *event,
55 void *eventdata,
56 void *callbackdata);
57
460b9539 58/* Control bar ------------------------------------------------------------- */
59
fb44b59e
RK
60/** @brief Guard against feedback */
61int suppress_actions = 1;
460b9539 62
219dc95c
RK
63/** @brief Definition of an icon
64 *
3849817f
RK
65 * We have two kinds of icon:
66 * - action icons, which just do something but don't have a state as such
67 * - toggle icons, which toggle between two states ("on" and "off").
68 *
69 * The scratch button is an action icon; currently all the others are toggle
70 * icons.
71 *
72 * (All icons can be sensitive or insensitive, separately to the above.)
219dc95c
RK
73 */
74struct icon {
3849817f
RK
75 /** @brief Filename for 'on' image */
76 const char *icon_on;
77
78 /** @brief Text for 'on' tooltip */
79 const char *tip_on;
219dc95c 80
3849817f
RK
81 /** @brief Filename for 'off' image or NULL for an action icon */
82 const char *icon_off;
83
84 /** @brief Text for 'off tooltip */
85 const char *tip_off;
219dc95c 86
ac6bf2ba
RK
87 /** @brief Associated menu item or NULL */
88 const char *menuitem;
219dc95c 89
6856f665
RK
90 /** @brief Events that change this icon, separated by spaces */
91 const char *events;
92
3849817f
RK
93 /** @brief @ref eclient.h function to call to go from off to on
94 *
95 * For action buttons, this should be NULL.
96 */
97 int (*action_go_on)(disorder_eclient *c,
98 disorder_eclient_no_response *completed,
99 void *v);
219dc95c 100
3849817f
RK
101 /** @brief @ref eclient.h function to call to go from on to off
102 *
103 * For action buttons, this action is used.
104 */
105 int (*action_go_off)(disorder_eclient *c,
106 disorder_eclient_no_response *completed,
107 void *v);
219dc95c 108
3849817f
RK
109 /** @brief Get button state
110 * @return 1 for on, 0 for off
111 */
112 int (*on)(void);
113
114 /** @brief Get button sensitivity
115 * @return 1 for sensitive, 0 for insensitive
116 *
117 * Can be NULL for always sensitive.
118 */
119 int (*sensitive)(void);
2d504956 120
219dc95c 121 /** @brief Pointer to button */
460b9539 122 GtkWidget *button;
ac6bf2ba
RK
123
124 /** @brief Pointer to menu item */
125 GtkWidget *item;
3849817f
RK
126
127 GtkWidget *image_on;
128 GtkWidget *image_off;
219dc95c
RK
129};
130
3849817f
RK
131/* TODO: Add rights into the mix below */
132
133static int pause_resume_on(void) {
134 return !(last_state & DISORDER_TRACK_PAUSED);
135}
136
137static int pause_resume_sensitive(void) {
6f09d54d
RK
138 return !!(last_state & DISORDER_PLAYING)
139 && (last_rights & RIGHT_PAUSE);
3849817f
RK
140}
141
142static int scratch_sensitive(void) {
6f09d54d 143 return !!(last_state & DISORDER_PLAYING)
afe9f743 144 && right_scratchable(last_rights, config->username, playing_track);
6f09d54d
RK
145}
146
147static int random_sensitive(void) {
148 return !!(last_rights & RIGHT_GLOBAL_PREFS);
3849817f
RK
149}
150
151static int random_enabled(void) {
152 return !!(last_state & DISORDER_RANDOM_ENABLED);
153}
154
6f09d54d
RK
155static int playing_sensitive(void) {
156 return !!(last_rights & RIGHT_GLOBAL_PREFS);
157}
158
3849817f
RK
159static int playing_enabled(void) {
160 return !!(last_state & DISORDER_PLAYING_ENABLED);
161}
2d504956 162
3849817f
RK
163static int rtp_enabled(void) {
164 return rtp_is_running;
165}
166
167static int rtp_sensitive(void) {
168 return rtp_supported;
169}
2d504956 170
219dc95c
RK
171/** @brief Table of all icons */
172static struct icon icons[] = {
2d504956 173 {
3849817f
RK
174 icon_on: "pause.png",
175 tip_on: "Pause playing track",
176 icon_off: "play.png",
177 tip_off: "Resume playing track",
178 menuitem: "<GdisorderMain>/Control/Playing",
179 on: pause_resume_on,
180 sensitive: pause_resume_sensitive,
181 action_go_on: disorder_eclient_resume,
182 action_go_off: disorder_eclient_pause,
6856f665 183 events: "pause-changed playing-changed rights-changed",
2d504956
RK
184 },
185 {
3849817f
RK
186 icon_on: "cross.png",
187 tip_on: "Cancel playing track",
188 menuitem: "<GdisorderMain>/Control/Scratch",
189 sensitive: scratch_sensitive,
190 action_go_off: disorder_eclient_scratch_playing,
6856f665 191 events: "playing-track-changed rights-changed",
2d504956
RK
192 },
193 {
3849817f
RK
194 icon_on: "randomcross.png",
195 tip_on: "Disable random play",
196 icon_off: "random.png",
197 tip_off: "Enable random play",
198 menuitem: "<GdisorderMain>/Control/Random play",
199 on: random_enabled,
6f09d54d 200 sensitive: random_sensitive,
3849817f
RK
201 action_go_on: disorder_eclient_random_enable,
202 action_go_off: disorder_eclient_random_disable,
6856f665 203 events: "random-changed rights-changed",
2d504956
RK
204 },
205 {
3849817f
RK
206 icon_on: "notescross.png",
207 tip_on: "Disable play",
208 icon_off: "notes.png",
209 tip_off: "Enable play",
210 on: playing_enabled,
6f09d54d 211 sensitive: playing_sensitive,
3849817f
RK
212 action_go_on: disorder_eclient_enable,
213 action_go_off: disorder_eclient_disable,
6856f665 214 events: "enabled-changed rights-changed",
2d504956
RK
215 },
216 {
3849817f
RK
217 icon_on: "speakercross.png",
218 tip_on: "Stop playing network stream",
219 icon_off: "speaker.png",
220 tip_off: "Play network stream",
221 menuitem: "<GdisorderMain>/Control/Network player",
222 on: rtp_enabled,
223 sensitive: rtp_sensitive,
224 action_go_on: enable_rtp,
6856f665
RK
225 action_go_off: disable_rtp,
226 events: "rtp-changed",
2d504956 227 },
460b9539 228};
219dc95c
RK
229
230/** @brief Count of icons */
460b9539 231#define NICONS (int)(sizeof icons / sizeof *icons)
232
219dc95c
RK
233static GtkAdjustment *volume_adj;
234static GtkAdjustment *balance_adj;
3c499fe7
RK
235static GtkWidget *volume_widget;
236static GtkWidget *balance_widget;
460b9539 237
219dc95c 238/** @brief Create the control bar */
e9b70a84 239GtkWidget *control_widget(void) {
460b9539 240 GtkWidget *hbox = gtk_hbox_new(FALSE, 1), *vbox;
460b9539 241 int n;
242
243 D(("control_widget"));
ac6bf2ba 244 assert(mainmenufactory); /* ordering must be right */
460b9539 245 for(n = 0; n < NICONS; ++n) {
3849817f 246 /* Create the button */
3849817f
RK
247 icons[n].button = gtk_button_new();
248 gtk_widget_set_style(icons[n].button, tool_style);
249 icons[n].image_on = gtk_image_new_from_pixbuf(find_image(icons[n].icon_on));
250 gtk_widget_set_style(icons[n].image_on, tool_style);
251 g_object_ref(icons[n].image_on);
252 /* If it's a toggle icon, create the 'off' half too */
253 if(icons[n].icon_off) {
254 icons[n].image_off = gtk_image_new_from_pixbuf(find_image(icons[n].icon_off));
255 gtk_widget_set_style(icons[n].image_off, tool_style);
256 g_object_ref(icons[n].image_off);
257 }
460b9539 258 g_signal_connect(G_OBJECT(icons[n].button), "clicked",
ac6bf2ba 259 G_CALLBACK(clicked_icon), &icons[n]);
460b9539 260 /* pop the icon in a vbox so it doesn't get vertically stretch if there are
261 * taller things in the control bar */
262 vbox = gtk_vbox_new(FALSE, 0);
263 gtk_box_pack_start(GTK_BOX(vbox), icons[n].button, TRUE, FALSE, 0);
264 gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
ac6bf2ba 265 if(icons[n].menuitem) {
3849817f 266 /* Find the menu item */
ac6bf2ba
RK
267 icons[n].item = gtk_item_factory_get_widget(mainmenufactory,
268 icons[n].menuitem);
3849817f 269 if(icons[n].icon_off)
2d504956
RK
270 g_signal_connect(G_OBJECT(icons[n].item), "toggled",
271 G_CALLBACK(toggled_menu), &icons[n]);
3849817f 272 else
2d504956
RK
273 g_signal_connect(G_OBJECT(icons[n].item), "activate",
274 G_CALLBACK(clicked_menu), &icons[n]);
ac6bf2ba 275 }
6856f665
RK
276 /* Make sure the icon is updated when relevant things changed */
277 char **events = split(icons[n].events, 0, 0, 0, 0);
278 while(*events)
279 event_register(*events++, icon_changed, &icons[n]);
460b9539 280 }
281 /* create the adjustments for the volume control */
282 volume_adj = GTK_ADJUSTMENT(gtk_adjustment_new(0, 0, goesupto,
283 goesupto / 20, goesupto / 20,
284 0));
285 balance_adj = GTK_ADJUSTMENT(gtk_adjustment_new(0, -1, 1,
286 0.2, 0.2, 0));
287 /* the volume control */
3c499fe7 288 volume_widget = gtk_hscale_new(volume_adj);
3c499fe7
RK
289 balance_widget = gtk_hscale_new(balance_adj);
290 gtk_widget_set_style(volume_widget, tool_style);
291 gtk_widget_set_style(balance_widget, tool_style);
292 gtk_scale_set_digits(GTK_SCALE(volume_widget), 10);
293 gtk_scale_set_digits(GTK_SCALE(balance_widget), 10);
294 gtk_widget_set_size_request(volume_widget, 192, -1);
295 gtk_widget_set_size_request(balance_widget, 192, -1);
296 gtk_tooltips_set_tip(tips, volume_widget, "Volume", "");
297 gtk_tooltips_set_tip(tips, balance_widget, "Balance", "");
298 gtk_box_pack_start(GTK_BOX(hbox), volume_widget, FALSE, TRUE, 0);
299 gtk_box_pack_start(GTK_BOX(hbox), balance_widget, FALSE, TRUE, 0);
460b9539 300 /* space updates rather than hammering the server */
3c499fe7
RK
301 gtk_range_set_update_policy(GTK_RANGE(volume_widget), GTK_UPDATE_DELAYED);
302 gtk_range_set_update_policy(GTK_RANGE(balance_widget), GTK_UPDATE_DELAYED);
460b9539 303 /* notice when the adjustments are changed */
304 g_signal_connect(G_OBJECT(volume_adj), "value-changed",
305 G_CALLBACK(volume_adjusted), 0);
306 g_signal_connect(G_OBJECT(balance_adj), "value-changed",
307 G_CALLBACK(volume_adjusted), 0);
308 /* format the volume/balance values ourselves */
3c499fe7 309 g_signal_connect(G_OBJECT(volume_widget), "format-value",
460b9539 310 G_CALLBACK(format_volume), 0);
3c499fe7 311 g_signal_connect(G_OBJECT(balance_widget), "format-value",
460b9539 312 G_CALLBACK(format_balance), 0);
1f868ca3 313 event_register("volume-changed", volume_changed, 0);
6856f665 314 event_register("rtp-changed", volume_changed, 0);
460b9539 315 return hbox;
316}
317
6d1302f0 318/** @brief Update the volume control when it changes */
1f868ca3
RK
319static void volume_changed(const char attribute((unused)) *event,
320 void attribute((unused)) *eventdata,
321 void attribute((unused)) *callbackdata) {
460b9539 322 double l, r;
6856f665 323 gboolean volume_supported;
460b9539 324
1f868ca3 325 D(("volume_changed"));
fb44b59e 326 ++suppress_actions;
6856f665
RK
327 /* Only display volume/balance controls if they will work */
328 if(!rtp_supported
329 || (rtp_supported && mixer_supported(DEFAULT_BACKEND)))
330 volume_supported = TRUE;
331 else
332 volume_supported = FALSE;
333 /* TODO: if the server doesn't know how to set the volume [but isn't using
334 * network play] then we should have volume_supported = FALSE */
335 if(volume_supported) {
336 gtk_widget_show(volume_widget);
337 gtk_widget_show(balance_widget);
338 l = volume_l / 100.0;
339 r = volume_r / 100.0;
340 gtk_adjustment_set_value(volume_adj, volume(l, r) * goesupto);
341 gtk_adjustment_set_value(balance_adj, balance(l, r));
342 } else {
343 gtk_widget_hide(volume_widget);
344 gtk_widget_hide(balance_widget);
345 }
fb44b59e 346 --suppress_actions;
460b9539 347}
348
ad58ebcc 349/** @brief Update the state of one of the control icons
ad58ebcc 350 */
6856f665
RK
351static void icon_changed(const char attribute((unused)) *event,
352 void attribute((unused)) *evendata,
353 void *callbackdata) {
00e0c652 354 //fprintf(stderr, "icon_changed (%s)\n", event);
6856f665 355 const struct icon *const icon = callbackdata;
3849817f
RK
356 int on = icon->on ? icon->on() : 1;
357 int sensitive = icon->sensitive ? icon->sensitive() : 1;
00e0c652 358 //fprintf(stderr, "sensitive->%d\n", sensitive);
3849817f
RK
359 GtkWidget *child, *newchild;
360
361 ++suppress_actions;
ad58ebcc 362 /* If the connection is down nothing is ever usable */
00959300 363 if(!(last_state & DISORDER_CONNECTED))
3849817f 364 sensitive = 0;
00e0c652 365 //fprintf(stderr, "(checked connected) sensitive->%d\n", sensitive);
3849817f
RK
366 /* Replace the child */
367 newchild = on ? icon->image_on : icon->image_off;
368 child = gtk_bin_get_child(GTK_BIN(icon->button));
369 if(child != newchild) {
370 if(child)
371 gtk_container_remove(GTK_CONTAINER(icon->button), child);
372 gtk_container_add(GTK_CONTAINER(icon->button), newchild);
373 gtk_widget_show(newchild);
374 }
7392b131
RK
375 /* If you disable play or random play NOT via the icon (for instance, via the
376 * edit menu or via a completely separate command line invocation) then the
00e0c652 377 * icon shows up as insensitive. Hover the mouse over it and the correct
7392b131
RK
378 * state is immediately displayed. sensitive and GTK_WIDGET_SENSITIVE show
379 * it to be in the correct state, so I think this is may be a GTK+ bug. */
3849817f
RK
380 if(icon->tip_on)
381 gtk_tooltips_set_tip(tips, icon->button,
382 on ? icon->tip_on : icon->tip_off, "");
383 gtk_widget_set_sensitive(icon->button, sensitive);
384 /* Icons with an associated menu item */
ac6bf2ba 385 if(icon->item) {
3849817f
RK
386 if(icon->icon_off)
387 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(icon->item), on);
388 gtk_widget_set_sensitive(icon->item, sensitive);
ac6bf2ba 389 }
3849817f 390 --suppress_actions;
a99c4e9a
RK
391}
392
53fa91bb
RK
393static void icon_action_completed(void attribute((unused)) *v,
394 const char *error) {
395 if(error)
396 popup_protocol_error(0, error);
397}
398
460b9539 399static void clicked_icon(GtkButton attribute((unused)) *button,
400 gpointer userdata) {
401 const struct icon *icon = userdata;
402
3849817f
RK
403 if(suppress_actions)
404 return;
405 if(!icon->on || icon->on())
406 icon->action_go_off(client, icon_action_completed, 0);
407 else
408 icon->action_go_on(client, icon_action_completed, 0);
460b9539 409}
410
ac6bf2ba
RK
411static void clicked_menu(GtkMenuItem attribute((unused)) *menuitem,
412 gpointer userdata) {
3849817f 413 clicked_icon(NULL, userdata);
ac6bf2ba
RK
414}
415
3849817f 416static void toggled_menu(GtkCheckMenuItem attribute((unused)) *menuitem,
2d504956 417 gpointer userdata) {
3849817f 418 clicked_icon(NULL, userdata);
2d504956
RK
419}
420
699517af
RK
421/** @brief Called when a volume command completes */
422static void volume_completed(void attribute((unused)) *v,
423 const char *error,
424 int attribute((unused)) l,
425 int attribute((unused)) r) {
426 if(error)
427 popup_protocol_error(0, error);
428 /* We don't set the UI's notion of the volume here, it is set from the log
429 * regardless of the reason it changed */
430}
431
219dc95c 432/** @brief Called when the volume has been adjusted */
460b9539 433static void volume_adjusted(GtkAdjustment attribute((unused)) *a,
434 gpointer attribute((unused)) user_data) {
435 double v = gtk_adjustment_get_value(volume_adj) / goesupto;
436 double b = gtk_adjustment_get_value(balance_adj);
437
fb44b59e 438 if(suppress_actions)
460b9539 439 /* This is the result of an update from the server, not a change from the
440 * user. Don't feedback! */
441 return;
442 D(("volume_adjusted"));
443 /* force to 'stereotypical' values */
444 v = nearbyint(100 * v) / 100;
445 b = nearbyint(5 * b) / 5;
446 /* Set the volume. We don't want a reply, we'll get the actual new volume
447 * from the log. */
321f7536
RK
448 if(rtp_supported) {
449 int l = nearbyint(left(v, b) * 100), r = nearbyint(right(v, b) * 100);
3c499fe7 450 mixer_control(DEFAULT_BACKEND, &l, &r, 1);
321f7536 451 } else
699517af 452 disorder_eclient_volume(client, volume_completed,
321f7536
RK
453 nearbyint(left(v, b) * 100),
454 nearbyint(right(v, b) * 100),
455 0);
460b9539 456}
457
219dc95c 458/** @brief Formats the volume value */
460b9539 459static gchar *format_volume(GtkScale attribute((unused)) *scale,
460 gdouble value) {
461 char s[32];
462
463 snprintf(s, sizeof s, "%.1f", (double)value);
2fc4475d 464 return g_strdup(s);
460b9539 465}
466
219dc95c 467/** @brief Formats the balance value */
460b9539 468static gchar *format_balance(GtkScale attribute((unused)) *scale,
469 gdouble value) {
470 char s[32];
471
472 if(fabs(value) < 0.1)
2fc4475d 473 return g_strdup("0");
460b9539 474 snprintf(s, sizeof s, "%+.1f", (double)value);
2fc4475d 475 return g_strdup(s);
460b9539 476}
477
478/* Volume mapping. We consider left, right, volume to be in [0,1]
479 * and balance to be in [-1,1].
480 *
481 * First, we just have volume = max(left, right).
482 *
483 * Balance we consider to linearly represent the amount by which the quieter
484 * channel differs from the louder. In detail:
485 *
486 * if right > left then balance > 0:
487 * balance = 0 => left = right (as an endpoint, not an instance)
488 * balance = 1 => left = 0
489 * fitting to linear, left = right * (1 - balance)
490 * so balance = 1 - left / right
491 * (right > left => right > 0 so no division by 0.)
492 *
493 * if left > right then balance < 0:
494 * balance = 0 => right = left (same caveat as above)
495 * balance = -1 => right = 0
496 * again fitting to linear, right = left * (1 + balance)
497 * so balance = right / left - 1
498 * (left > right => left > 0 so no division by 0.)
499 *
500 * if left = right then we just have balance = 0.
501 *
502 * Thanks to Clive and Andrew.
503 */
504
219dc95c 505/** @brief Return the greater of @p x and @p y */
460b9539 506static double max(double x, double y) {
507 return x > y ? x : y;
508}
509
219dc95c 510/** @brief Compute the left channel volume */
460b9539 511static double left(double v, double b) {
512 if(b > 0) /* volume = right */
513 return v * (1 - b);
514 else /* volume = left */
515 return v;
516}
517
219dc95c 518/** @brief Compute the right channel volume */
460b9539 519static double right(double v, double b) {
520 if(b > 0) /* volume = right */
521 return v;
522 else /* volume = left */
523 return v * (1 + b);
524}
525
219dc95c 526/** @brief Compute the overall volume */
460b9539 527static double volume(double l, double r) {
528 return max(l, r);
529}
530
219dc95c 531/** @brief Compute the balance */
460b9539 532static double balance(double l, double r) {
533 if(l > r)
534 return r / l - 1;
535 else if(r > l)
536 return 1 - l / r;
537 else /* left = right */
538 return 0;
539}
540
a99c4e9a
RK
541/** @brief Called to enable RTP play
542 *
543 * Rather odd signature is to fit in with the other icons which all call @ref
544 * lib/eclient.h functions.
545 */
546static int enable_rtp(disorder_eclient attribute((unused)) *c,
547 disorder_eclient_no_response attribute((unused)) *completed,
548 void attribute((unused)) *v) {
549 start_rtp();
550 return 0;
551}
552
553/** @brief Called to disable RTP play
554 *
555 * Rather odd signature is to fit in with the other icons which all call @ref
556 * lib/eclient.h functions.
557 */
558static int disable_rtp(disorder_eclient attribute((unused)) *c,
559 disorder_eclient_no_response attribute((unused)) *completed,
560 void attribute((unused)) *v) {
561 stop_rtp();
562 return 0;
563}
564
460b9539 565/*
566Local Variables:
567c-basic-offset:2
568comment-column:40
569fill-column:79
570indent-tabs-mode:nil
571End:
572*/