chiark / gitweb /
Move RTP check. This seems to improve matters - formerly the RTP
[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
29WT(adjustment);
30WT(hscale);
31WT(hbox);
e9b70a84 32WT(button);
33WT(image);
34WT(label);
35WT(vbox);
460b9539 36
37struct icon;
38
460b9539 39static void clicked_icon(GtkButton *, gpointer);
ac6bf2ba 40static void clicked_menu(GtkMenuItem *, gpointer userdata);
2d504956 41static void toggled_menu(GtkCheckMenuItem *, gpointer userdata);
460b9539 42
a99c4e9a
RK
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
460b9539 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
6856f665
RK
59static void icon_changed(const char *event,
60 void *evendata,
61 void *callbackdata);
1f868ca3
RK
62static void volume_changed(const char *event,
63 void *eventdata,
64 void *callbackdata);
65
460b9539 66/* Control bar ------------------------------------------------------------- */
67
fb44b59e
RK
68/** @brief Guard against feedback */
69int suppress_actions = 1;
460b9539 70
219dc95c
RK
71/** @brief Definition of an icon
72 *
3849817f
RK
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.)
219dc95c
RK
81 */
82struct icon {
3849817f
RK
83 /** @brief Filename for 'on' image */
84 const char *icon_on;
85
86 /** @brief Text for 'on' tooltip */
87 const char *tip_on;
219dc95c 88
3849817f
RK
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;
219dc95c 94
ac6bf2ba
RK
95 /** @brief Associated menu item or NULL */
96 const char *menuitem;
219dc95c 97
6856f665
RK
98 /** @brief Events that change this icon, separated by spaces */
99 const char *events;
100
3849817f
RK
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);
219dc95c 108
3849817f
RK
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);
219dc95c 116
3849817f
RK
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);
2d504956 128
219dc95c 129 /** @brief Pointer to button */
460b9539 130 GtkWidget *button;
ac6bf2ba
RK
131
132 /** @brief Pointer to menu item */
133 GtkWidget *item;
3849817f
RK
134
135 GtkWidget *image_on;
136 GtkWidget *image_off;
219dc95c
RK
137};
138
3849817f
RK
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) {
6f09d54d
RK
146 return !!(last_state & DISORDER_PLAYING)
147 && (last_rights & RIGHT_PAUSE);
3849817f
RK
148}
149
150static int scratch_sensitive(void) {
6f09d54d 151 return !!(last_state & DISORDER_PLAYING)
afe9f743 152 && right_scratchable(last_rights, config->username, playing_track);
6f09d54d
RK
153}
154
155static int random_sensitive(void) {
156 return !!(last_rights & RIGHT_GLOBAL_PREFS);
3849817f
RK
157}
158
159static int random_enabled(void) {
160 return !!(last_state & DISORDER_RANDOM_ENABLED);
161}
162
6f09d54d
RK
163static int playing_sensitive(void) {
164 return !!(last_rights & RIGHT_GLOBAL_PREFS);
165}
166
3849817f
RK
167static int playing_enabled(void) {
168 return !!(last_state & DISORDER_PLAYING_ENABLED);
169}
2d504956 170
3849817f
RK
171static int rtp_enabled(void) {
172 return rtp_is_running;
173}
174
175static int rtp_sensitive(void) {
176 return rtp_supported;
177}
2d504956 178
219dc95c
RK
179/** @brief Table of all icons */
180static struct icon icons[] = {
2d504956 181 {
3849817f
RK
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,
6856f665 191 events: "pause-changed playing-changed rights-changed",
2d504956
RK
192 },
193 {
3849817f
RK
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,
6856f665 199 events: "playing-track-changed rights-changed",
2d504956
RK
200 },
201 {
3849817f
RK
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,
6f09d54d 208 sensitive: random_sensitive,
3849817f
RK
209 action_go_on: disorder_eclient_random_enable,
210 action_go_off: disorder_eclient_random_disable,
6856f665 211 events: "random-changed rights-changed",
2d504956
RK
212 },
213 {
3849817f
RK
214 icon_on: "notescross.png",
215 tip_on: "Disable play",
216 icon_off: "notes.png",
217 tip_off: "Enable play",
218 on: playing_enabled,
6f09d54d 219 sensitive: playing_sensitive,
3849817f
RK
220 action_go_on: disorder_eclient_enable,
221 action_go_off: disorder_eclient_disable,
6856f665 222 events: "enabled-changed rights-changed",
2d504956
RK
223 },
224 {
3849817f
RK
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,
6856f665
RK
233 action_go_off: disable_rtp,
234 events: "rtp-changed",
2d504956 235 },
460b9539 236};
219dc95c
RK
237
238/** @brief Count of icons */
460b9539 239#define NICONS (int)(sizeof icons / sizeof *icons)
240
219dc95c
RK
241static GtkAdjustment *volume_adj;
242static GtkAdjustment *balance_adj;
3c499fe7
RK
243static GtkWidget *volume_widget;
244static GtkWidget *balance_widget;
460b9539 245
219dc95c 246/** @brief Create the control bar */
e9b70a84 247GtkWidget *control_widget(void) {
460b9539 248 GtkWidget *hbox = gtk_hbox_new(FALSE, 1), *vbox;
460b9539 249 int n;
250
e9b70a84 251 NW(hbox);
460b9539 252 D(("control_widget"));
ac6bf2ba 253 assert(mainmenufactory); /* ordering must be right */
460b9539 254 for(n = 0; n < NICONS; ++n) {
3849817f 255 /* Create the button */
e9b70a84 256 NW(button);
3849817f
RK
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 }
460b9539 268 g_signal_connect(G_OBJECT(icons[n].button), "clicked",
ac6bf2ba 269 G_CALLBACK(clicked_icon), &icons[n]);
460b9539 270 /* pop the icon in a vbox so it doesn't get vertically stretch if there are
271 * taller things in the control bar */
e9b70a84 272 NW(vbox);
460b9539 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);
ac6bf2ba 276 if(icons[n].menuitem) {
3849817f 277 /* Find the menu item */
ac6bf2ba
RK
278 icons[n].item = gtk_item_factory_get_widget(mainmenufactory,
279 icons[n].menuitem);
3849817f 280 if(icons[n].icon_off)
2d504956
RK
281 g_signal_connect(G_OBJECT(icons[n].item), "toggled",
282 G_CALLBACK(toggled_menu), &icons[n]);
3849817f 283 else
2d504956
RK
284 g_signal_connect(G_OBJECT(icons[n].item), "activate",
285 G_CALLBACK(clicked_menu), &icons[n]);
ac6bf2ba 286 }
6856f665
RK
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]);
460b9539 291 }
292 /* create the adjustments for the volume control */
e9b70a84 293 NW(adjustment);
460b9539 294 volume_adj = GTK_ADJUSTMENT(gtk_adjustment_new(0, 0, goesupto,
295 goesupto / 20, goesupto / 20,
296 0));
e9b70a84 297 NW(adjustment);
460b9539 298 balance_adj = GTK_ADJUSTMENT(gtk_adjustment_new(0, -1, 1,
299 0.2, 0.2, 0));
300 /* the volume control */
e9b70a84 301 NW(hscale);
3c499fe7 302 volume_widget = gtk_hscale_new(volume_adj);
e9b70a84 303 NW(hscale);
3c499fe7
RK
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);
460b9539 315 /* space updates rather than hammering the server */
3c499fe7
RK
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);
460b9539 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 */
3c499fe7 324 g_signal_connect(G_OBJECT(volume_widget), "format-value",
460b9539 325 G_CALLBACK(format_volume), 0);
3c499fe7 326 g_signal_connect(G_OBJECT(balance_widget), "format-value",
460b9539 327 G_CALLBACK(format_balance), 0);
1f868ca3 328 event_register("volume-changed", volume_changed, 0);
6856f665 329 event_register("rtp-changed", volume_changed, 0);
460b9539 330 return hbox;
331}
332
6d1302f0 333/** @brief Update the volume control when it changes */
1f868ca3
RK
334static void volume_changed(const char attribute((unused)) *event,
335 void attribute((unused)) *eventdata,
336 void attribute((unused)) *callbackdata) {
460b9539 337 double l, r;
6856f665 338 gboolean volume_supported;
460b9539 339
1f868ca3 340 D(("volume_changed"));
fb44b59e 341 ++suppress_actions;
6856f665
RK
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 }
fb44b59e 361 --suppress_actions;
460b9539 362}
363
ad58ebcc 364/** @brief Update the state of one of the control icons
ad58ebcc 365 */
6856f665
RK
366static void icon_changed(const char attribute((unused)) *event,
367 void attribute((unused)) *evendata,
368 void *callbackdata) {
369 const struct icon *const icon = callbackdata;
3849817f
RK
370 int on = icon->on ? icon->on() : 1;
371 int sensitive = icon->sensitive ? icon->sensitive() : 1;
372 GtkWidget *child, *newchild;
373
374 ++suppress_actions;
ad58ebcc 375 /* If the connection is down nothing is ever usable */
00959300 376 if(!(last_state & DISORDER_CONNECTED))
3849817f
RK
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 */
ac6bf2ba 392 if(icon->item) {
3849817f
RK
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);
ac6bf2ba 396 }
3849817f 397 --suppress_actions;
a99c4e9a
RK
398}
399
53fa91bb
RK
400static void icon_action_completed(void attribute((unused)) *v,
401 const char *error) {
402 if(error)
403 popup_protocol_error(0, error);
404}
405
460b9539 406static void clicked_icon(GtkButton attribute((unused)) *button,
407 gpointer userdata) {
408 const struct icon *icon = userdata;
409
3849817f
RK
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);
460b9539 416}
417
ac6bf2ba
RK
418static void clicked_menu(GtkMenuItem attribute((unused)) *menuitem,
419 gpointer userdata) {
3849817f 420 clicked_icon(NULL, userdata);
ac6bf2ba
RK
421}
422
3849817f 423static void toggled_menu(GtkCheckMenuItem attribute((unused)) *menuitem,
2d504956 424 gpointer userdata) {
3849817f 425 clicked_icon(NULL, userdata);
2d504956
RK
426}
427
699517af
RK
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
219dc95c 439/** @brief Called when the volume has been adjusted */
460b9539 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
fb44b59e 445 if(suppress_actions)
460b9539 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. */
321f7536
RK
455 if(rtp_supported) {
456 int l = nearbyint(left(v, b) * 100), r = nearbyint(right(v, b) * 100);
3c499fe7 457 mixer_control(DEFAULT_BACKEND, &l, &r, 1);
321f7536 458 } else
699517af 459 disorder_eclient_volume(client, volume_completed,
321f7536
RK
460 nearbyint(left(v, b) * 100),
461 nearbyint(right(v, b) * 100),
462 0);
460b9539 463}
464
219dc95c 465/** @brief Formats the volume value */
460b9539 466static gchar *format_volume(GtkScale attribute((unused)) *scale,
467 gdouble value) {
468 char s[32];
469
470 snprintf(s, sizeof s, "%.1f", (double)value);
2fc4475d 471 return g_strdup(s);
460b9539 472}
473
219dc95c 474/** @brief Formats the balance value */
460b9539 475static gchar *format_balance(GtkScale attribute((unused)) *scale,
476 gdouble value) {
477 char s[32];
478
479 if(fabs(value) < 0.1)
2fc4475d 480 return g_strdup("0");
460b9539 481 snprintf(s, sizeof s, "%+.1f", (double)value);
2fc4475d 482 return g_strdup(s);
460b9539 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
219dc95c 512/** @brief Return the greater of @p x and @p y */
460b9539 513static double max(double x, double y) {
514 return x > y ? x : y;
515}
516
219dc95c 517/** @brief Compute the left channel volume */
460b9539 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
219dc95c 525/** @brief Compute the right channel volume */
460b9539 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
219dc95c 533/** @brief Compute the overall volume */
460b9539 534static double volume(double l, double r) {
535 return max(l, r);
536}
537
219dc95c 538/** @brief Compute the balance */
460b9539 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
a99c4e9a
RK
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
460b9539 572/*
573Local Variables:
574c-basic-offset:2
575comment-column:40
576fill-column:79
577indent-tabs-mode:nil
578End:
579*/