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