2 * This file is part of DisOrder.
3 * Copyright (C) 2006, 2007 Richard Kettlewell
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.
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.
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
20 /** @file disobedience/control.c
21 * @brief Volume control and buttons
24 #include "disobedience.h"
26 /* Forward declarations ---------------------------------------------------- */
39 static void update_pause(const struct icon *);
40 static void update_play(const struct icon *);
41 static void update_scratch(const struct icon *);
42 static void update_random_enable(const struct icon *);
43 static void update_random_disable(const struct icon *);
44 static void update_enable(const struct icon *);
45 static void update_disable(const struct icon *);
46 static void clicked_icon(GtkButton *, gpointer);
48 static double left(double v, double b);
49 static double right(double v, double b);
50 static double volume(double l, double r);
51 static double balance(double l, double r);
53 static void volume_adjusted(GtkAdjustment *a, gpointer user_data);
54 static gchar *format_volume(GtkScale *scale, gdouble value);
55 static gchar *format_balance(GtkScale *scale, gdouble value);
57 /* Control bar ------------------------------------------------------------- */
59 /** @brief Guard against feedback loop in volume control */
60 static int suppress_set_volume;
62 /** @brief Definition of an icon
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.
68 /** @brief Filename for image */
71 /** @brief Text for tooltip */
74 /** @brief Called when button is clicked (activated) */
75 void (*clicked)(GtkButton *button, gpointer userdata);
77 /** @brief Called to update button when state may have changed */
78 void (*update)(const struct icon *i);
80 /** @brief @ref eclient.h function to call */
81 int (*action)(disorder_eclient *c,
82 disorder_eclient_no_response *completed,
85 /** @brief Pointer to button */
89 /** @brief Table of all icons */
90 static struct icon icons[] = {
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 },
107 /** @brief Count of icons */
108 #define NICONS (int)(sizeof icons / sizeof *icons)
110 static GtkAdjustment *volume_adj;
111 static GtkAdjustment *balance_adj;
113 /** @brief Called whenever last_state changes in any way */
114 static void control_monitor(void attribute((unused)) *u) {
117 D(("control_monitor"));
118 for(n = 0; n < NICONS; ++n)
119 icons[n].update(&icons[n]);
122 /** @brief Create the control bar */
123 GtkWidget *control_widget(void) {
124 GtkWidget *hbox = gtk_hbox_new(FALSE, 1), *vbox;
128 GtkTooltips *tips = gtk_tooltips_new();
133 D(("control_widget"));
134 for(n = 0; n < NICONS; ++n) {
136 icons[n].button = gtk_button_new();
137 if((pb = find_image(icons[n].icon))) {
139 content = gtk_image_new_from_pixbuf(pb);
142 content = gtk_label_new(icons[n].icon);
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 */
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);
155 /* create the adjustments for the volume control */
157 volume_adj = GTK_ADJUSTMENT(gtk_adjustment_new(0, 0, goesupto,
158 goesupto / 20, goesupto / 20,
161 balance_adj = GTK_ADJUSTMENT(gtk_adjustment_new(0, -1, 1,
163 /* the volume control */
165 v = gtk_hscale_new(volume_adj);
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);
189 register_monitor(control_monitor, 0, -1UL);
193 /** @brief Update the volume control when it changes */
194 void volume_update(void) {
197 D(("volume_update"));
198 l = volume_l / 100.0;
199 r = volume_r / 100.0;
200 ++suppress_set_volume;
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;
206 /** @brief Update the state of one of the control icons
207 * @param icon Target icon
208 * @param visible True if this version of the button should be visible
209 * @param usable True if the button is currently usable
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
215 * @p usable need not take into account server availability, that is done
218 static void update_icon(const struct icon *icon,
219 int visible, int usable) {
220 /* If the connection is down nothing is ever usable */
221 if(!(last_state & DISORDER_CONNECTED))
223 (visible ? gtk_widget_show : gtk_widget_hide)(icon->button);
224 /* Only both updating usability if the button is visible */
226 gtk_widget_set_sensitive(icon->button, usable);
229 static void update_pause(const struct icon *icon) {
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);
235 static void update_play(const struct icon *icon) {
236 const int visible = !!(last_state & DISORDER_TRACK_PAUSED);
237 const int usable = !!(last_state & DISORDER_PLAYING);
238 update_icon(icon, visible, usable);
241 static void update_scratch(const struct icon *icon) {
242 const int visible = 1;
243 const int usable = !!(last_state & DISORDER_PLAYING);
244 update_icon(icon, visible, usable);
247 static void update_random_enable(const struct icon *icon) {
248 const int visible = !(last_state & DISORDER_RANDOM_ENABLED);
249 const int usable = 1;
250 update_icon(icon, visible, usable);
253 static void update_random_disable(const struct icon *icon) {
254 const int visible = !!(last_state & DISORDER_RANDOM_ENABLED);
255 const int usable = 1;
256 update_icon(icon, visible, usable);
259 static void update_enable(const struct icon *icon) {
260 const int visible = !(last_state & DISORDER_PLAYING_ENABLED);
261 const int usable = 1;
262 update_icon(icon, visible, usable);
265 static void update_disable(const struct icon *icon) {
266 const int visible = !!(last_state & DISORDER_PLAYING_ENABLED);
267 const int usable = 1;
268 update_icon(icon, visible, usable);
271 static void clicked_icon(GtkButton attribute((unused)) *button,
273 const struct icon *icon = userdata;
275 icon->action(client, 0, 0);
278 /** @brief Called when the volume has been adjusted */
279 static 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);
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! */
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
294 disorder_eclient_volume(client, 0,
295 nearbyint(left(v, b) * 100),
296 nearbyint(right(v, b) * 100),
300 /** @brief Formats the volume value */
301 static gchar *format_volume(GtkScale attribute((unused)) *scale,
305 snprintf(s, sizeof s, "%.1f", (double)value);
309 /** @brief Formats the balance value */
310 static gchar *format_balance(GtkScale attribute((unused)) *scale,
314 if(fabs(value) < 0.1)
315 return g_strdup("0");
316 snprintf(s, sizeof s, "%+.1f", (double)value);
320 /* Volume mapping. We consider left, right, volume to be in [0,1]
321 * and balance to be in [-1,1].
323 * First, we just have volume = max(left, right).
325 * Balance we consider to linearly represent the amount by which the quieter
326 * channel differs from the louder. In detail:
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.)
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.)
342 * if left = right then we just have balance = 0.
344 * Thanks to Clive and Andrew.
347 /** @brief Return the greater of @p x and @p y */
348 static double max(double x, double y) {
349 return x > y ? x : y;
352 /** @brief Compute the left channel volume */
353 static double left(double v, double b) {
354 if(b > 0) /* volume = right */
356 else /* volume = left */
360 /** @brief Compute the right channel volume */
361 static double right(double v, double b) {
362 if(b > 0) /* volume = right */
364 else /* volume = left */
368 /** @brief Compute the overall volume */
369 static double volume(double l, double r) {
373 /** @brief Compute the balance */
374 static double balance(double l, double r) {
379 else /* left = right */