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