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