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