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