chiark / gitweb /
disobedience control buttons reflect current state properly
[disorder] / disobedience / control.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
eb525fcd 3 * Copyright (C) 2006, 2007 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 */
20
21#include "disobedience.h"
22
e9b70a84 23/* Forward declarations ---------------------------------------------------- */
24
25WT(adjustment);
26WT(hscale);
27WT(hbox);
28WT(tooltips);
29WT(button);
30WT(image);
31WT(label);
32WT(vbox);
460b9539 33
34struct icon;
35
36static void update_pause(const struct icon *);
37static void update_play(const struct icon *);
38static void update_scratch(const struct icon *);
39static void update_random_enable(const struct icon *);
40static void update_random_disable(const struct icon *);
41static void update_enable(const struct icon *);
42static void update_disable(const struct icon *);
43static void clicked_icon(GtkButton *, gpointer);
44
45static double left(double v, double b);
46static double right(double v, double b);
47static double volume(double l, double r);
48static double balance(double l, double r);
49
50static void volume_adjusted(GtkAdjustment *a, gpointer user_data);
51static gchar *format_volume(GtkScale *scale, gdouble value);
52static gchar *format_balance(GtkScale *scale, gdouble value);
53
54/* Control bar ------------------------------------------------------------- */
55
56static int suppress_set_volume;
57/* Guard against feedback loop in volume control */
58
59static struct icon {
60 const char *icon;
61 const char *tip;
62 void (*clicked)(GtkButton *button, gpointer userdata);
63 void (*update)(const struct icon *i);
64 int (*action)(disorder_eclient *c,
65 disorder_eclient_no_response *completed,
66 void *v);
67 GtkWidget *button;
68} icons[] = {
69 { "pause.png", "Pause playing track", clicked_icon, update_pause,
70 disorder_eclient_pause, 0 },
71 { "play.png", "Resume playing track", clicked_icon, update_play,
72 disorder_eclient_resume, 0 },
73 { "cross.png", "Cancel playing track", clicked_icon, update_scratch,
74 disorder_eclient_scratch_playing, 0 },
75 { "random.png", "Enable random play", clicked_icon, update_random_enable,
76 disorder_eclient_random_enable, 0 },
77 { "randomcross.png", "Disable random play", clicked_icon, update_random_disable,
78 disorder_eclient_random_disable, 0 },
79 { "notes.png", "Enable play", clicked_icon, update_enable,
80 disorder_eclient_enable, 0 },
81 { "notescross.png", "Disable play", clicked_icon, update_disable,
82 disorder_eclient_disable, 0 },
83};
84#define NICONS (int)(sizeof icons / sizeof *icons)
85
86GtkAdjustment *volume_adj, *balance_adj;
87
88/* Create the control bar */
e9b70a84 89GtkWidget *control_widget(void) {
460b9539 90 GtkWidget *hbox = gtk_hbox_new(FALSE, 1), *vbox;
91 GtkWidget *content;
92 GdkPixbuf *pb;
93 GtkWidget *v, *b;
94 GtkTooltips *tips = gtk_tooltips_new();
95 int n;
96
e9b70a84 97 NW(hbox);
98 NW(tooltips);
460b9539 99 D(("control_widget"));
100 for(n = 0; n < NICONS; ++n) {
e9b70a84 101 NW(button);
460b9539 102 icons[n].button = gtk_button_new();
e9b70a84 103 if((pb = find_image(icons[n].icon))) {
104 NW(image);
460b9539 105 content = gtk_image_new_from_pixbuf(pb);
e9b70a84 106 } else {
107 NW(label);
460b9539 108 content = gtk_label_new(icons[n].icon);
e9b70a84 109 }
460b9539 110 gtk_container_add(GTK_CONTAINER(icons[n].button), content);
111 gtk_tooltips_set_tip(tips, icons[n].button, icons[n].tip, "");
112 g_signal_connect(G_OBJECT(icons[n].button), "clicked",
113 G_CALLBACK(icons[n].clicked), &icons[n]);
114 /* pop the icon in a vbox so it doesn't get vertically stretch if there are
115 * taller things in the control bar */
e9b70a84 116 NW(vbox);
460b9539 117 vbox = gtk_vbox_new(FALSE, 0);
118 gtk_box_pack_start(GTK_BOX(vbox), icons[n].button, TRUE, FALSE, 0);
119 gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
120 }
121 /* create the adjustments for the volume control */
e9b70a84 122 NW(adjustment);
460b9539 123 volume_adj = GTK_ADJUSTMENT(gtk_adjustment_new(0, 0, goesupto,
124 goesupto / 20, goesupto / 20,
125 0));
e9b70a84 126 NW(adjustment);
460b9539 127 balance_adj = GTK_ADJUSTMENT(gtk_adjustment_new(0, -1, 1,
128 0.2, 0.2, 0));
129 /* the volume control */
e9b70a84 130 NW(hscale);
460b9539 131 v = gtk_hscale_new(volume_adj);
e9b70a84 132 NW(hscale);
460b9539 133 b = gtk_hscale_new(balance_adj);
134 gtk_scale_set_digits(GTK_SCALE(v), 10);
135 gtk_scale_set_digits(GTK_SCALE(b), 10);
136 gtk_widget_set_size_request(v, 192, -1);
137 gtk_widget_set_size_request(b, 192, -1);
138 gtk_tooltips_set_tip(tips, v, "Volume", "");
139 gtk_tooltips_set_tip(tips, b, "Balance", "");
140 gtk_box_pack_start(GTK_BOX(hbox), v, FALSE, TRUE, 0);
141 gtk_box_pack_start(GTK_BOX(hbox), b, FALSE, TRUE, 0);
142 /* space updates rather than hammering the server */
143 gtk_range_set_update_policy(GTK_RANGE(v), GTK_UPDATE_DELAYED);
144 gtk_range_set_update_policy(GTK_RANGE(b), GTK_UPDATE_DELAYED);
145 /* notice when the adjustments are changed */
146 g_signal_connect(G_OBJECT(volume_adj), "value-changed",
147 G_CALLBACK(volume_adjusted), 0);
148 g_signal_connect(G_OBJECT(balance_adj), "value-changed",
149 G_CALLBACK(volume_adjusted), 0);
150 /* format the volume/balance values ourselves */
151 g_signal_connect(G_OBJECT(v), "format-value",
152 G_CALLBACK(format_volume), 0);
153 g_signal_connect(G_OBJECT(b), "format-value",
154 G_CALLBACK(format_balance), 0);
155 return hbox;
156}
157
ad58ebcc 158/** @brief Update the control bar after some kind of state change */
460b9539 159void control_update(void) {
160 int n;
161 double l, r;
162
163 D(("control_update"));
164 for(n = 0; n < NICONS; ++n)
165 icons[n].update(&icons[n]);
166 l = volume_l / 100.0;
167 r = volume_r / 100.0;
168 ++suppress_set_volume;;
169 gtk_adjustment_set_value(volume_adj, volume(l, r) * goesupto);
170 gtk_adjustment_set_value(balance_adj, balance(l, r));
171 --suppress_set_volume;
172}
173
ad58ebcc 174/** @brief Update the state of one of the control icons
175 * @param button Widget for button
176 * @param visible True if this version of the button should be visible
177 * @param usable True if the button is currently usable
178 *
179 * Several of the icons, rather bizarrely, come in pairs: for instance exactly
180 * one of the play and pause buttons is supposed to be visible at any given
181 * moment.
182 *
183 * @p usable need not take into account server availability, that is done
184 * automatically.
185 */
460b9539 186static void update_icon(GtkWidget *button,
ad58ebcc 187 int visible, int usable) {
188 /* If the connection is down nothing is ever usable */
189 if(!disorder_eclient_connected(client))
190 usable = 0;
460b9539 191 (visible ? gtk_widget_show : gtk_widget_hide)(button);
ad58ebcc 192 /* Only both updating usability if the button is visible */
193 if(visible)
194 gtk_widget_set_sensitive(button, usable);
460b9539 195}
196
197static void update_pause(const struct icon *icon) {
198 int visible = !(last_state & DISORDER_TRACK_PAUSED);
199 int usable = playing; /* TODO: might be a lie */
200 update_icon(icon->button, visible, usable);
201}
202
203static void update_play(const struct icon *icon) {
204 int visible = !!(last_state & DISORDER_TRACK_PAUSED);
205 int usable = playing;
206 update_icon(icon->button, visible, usable);
207}
208
209static void update_scratch(const struct icon *icon) {
210 int visible = 1;
211 int usable = playing;
212 update_icon(icon->button, visible, usable);
213}
214
215static void update_random_enable(const struct icon *icon) {
216 int visible = !(last_state & DISORDER_RANDOM_ENABLED);
217 int usable = 1;
218 update_icon(icon->button, visible, usable);
219}
220
221static void update_random_disable(const struct icon *icon) {
222 int visible = !!(last_state & DISORDER_RANDOM_ENABLED);
223 int usable = 1;
224 update_icon(icon->button, visible, usable);
225}
226
227static void update_enable(const struct icon *icon) {
228 int visible = !(last_state & DISORDER_PLAYING_ENABLED);
229 int usable = 1;
230 update_icon(icon->button, visible, usable);
231}
232
233static void update_disable(const struct icon *icon) {
234 int visible = !!(last_state & DISORDER_PLAYING_ENABLED);
235 int usable = 1;
236 update_icon(icon->button, visible, usable);
237}
238
239static void clicked_icon(GtkButton attribute((unused)) *button,
240 gpointer userdata) {
241 const struct icon *icon = userdata;
242
243 icon->action(client, 0, 0);
244}
245
246static void volume_adjusted(GtkAdjustment attribute((unused)) *a,
247 gpointer attribute((unused)) user_data) {
248 double v = gtk_adjustment_get_value(volume_adj) / goesupto;
249 double b = gtk_adjustment_get_value(balance_adj);
250
251 if(suppress_set_volume)
252 /* This is the result of an update from the server, not a change from the
253 * user. Don't feedback! */
254 return;
255 D(("volume_adjusted"));
256 /* force to 'stereotypical' values */
257 v = nearbyint(100 * v) / 100;
258 b = nearbyint(5 * b) / 5;
259 /* Set the volume. We don't want a reply, we'll get the actual new volume
260 * from the log. */
261 disorder_eclient_volume(client, 0,
262 nearbyint(left(v, b) * 100),
263 nearbyint(right(v, b) * 100),
264 0);
265}
266
267/* Called to format the volume value */
268static gchar *format_volume(GtkScale attribute((unused)) *scale,
269 gdouble value) {
270 char s[32];
271
272 snprintf(s, sizeof s, "%.1f", (double)value);
2fc4475d 273 return g_strdup(s);
460b9539 274}
275
276/* Called to format the balance value. */
277static gchar *format_balance(GtkScale attribute((unused)) *scale,
278 gdouble value) {
279 char s[32];
280
281 if(fabs(value) < 0.1)
2fc4475d 282 return g_strdup("0");
460b9539 283 snprintf(s, sizeof s, "%+.1f", (double)value);
2fc4475d 284 return g_strdup(s);
460b9539 285}
286
287/* Volume mapping. We consider left, right, volume to be in [0,1]
288 * and balance to be in [-1,1].
289 *
290 * First, we just have volume = max(left, right).
291 *
292 * Balance we consider to linearly represent the amount by which the quieter
293 * channel differs from the louder. In detail:
294 *
295 * if right > left then balance > 0:
296 * balance = 0 => left = right (as an endpoint, not an instance)
297 * balance = 1 => left = 0
298 * fitting to linear, left = right * (1 - balance)
299 * so balance = 1 - left / right
300 * (right > left => right > 0 so no division by 0.)
301 *
302 * if left > right then balance < 0:
303 * balance = 0 => right = left (same caveat as above)
304 * balance = -1 => right = 0
305 * again fitting to linear, right = left * (1 + balance)
306 * so balance = right / left - 1
307 * (left > right => left > 0 so no division by 0.)
308 *
309 * if left = right then we just have balance = 0.
310 *
311 * Thanks to Clive and Andrew.
312 */
313
314static double max(double x, double y) {
315 return x > y ? x : y;
316}
317
318static double left(double v, double b) {
319 if(b > 0) /* volume = right */
320 return v * (1 - b);
321 else /* volume = left */
322 return v;
323}
324
325static double right(double v, double b) {
326 if(b > 0) /* volume = right */
327 return v;
328 else /* volume = left */
329 return v * (1 + b);
330}
331
332static double volume(double l, double r) {
333 return max(l, r);
334}
335
336static double balance(double l, double r) {
337 if(l > r)
338 return r / l - 1;
339 else if(r > l)
340 return 1 - l / r;
341 else /* left = right */
342 return 0;
343}
344
345/*
346Local Variables:
347c-basic-offset:2
348comment-column:40
349fill-column:79
350indent-tabs-mode:nil
351End:
352*/