Commit | Line | Data |
---|---|---|
460b9539 | 1 | /* |
2 | * This file is part of DisOrder. | |
851bbc58 | 3 | * Copyright (C) 2006-2009 Richard Kettlewell |
460b9539 | 4 | * |
e7eb3a27 | 5 | * This program is free software: you can redistribute it and/or modify |
460b9539 | 6 | * it under the terms of the GNU General Public License as published by |
e7eb3a27 | 7 | * the Free Software Foundation, either version 3 of the License, or |
460b9539 | 8 | * (at your option) any later version. |
9 | * | |
e7eb3a27 RK |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
460b9539 | 15 | * You should have received a copy of the GNU General Public License |
e7eb3a27 | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
460b9539 | 17 | */ |
219dc95c RK |
18 | /** @file disobedience/control.c |
19 | * @brief Volume control and buttons | |
20 | */ | |
460b9539 | 21 | |
22 | #include "disobedience.h" | |
23 | ||
e9b70a84 | 24 | /* Forward declarations ---------------------------------------------------- */ |
25 | ||
460b9539 | 26 | struct icon; |
27 | ||
460b9539 | 28 | static void clicked_icon(GtkButton *, gpointer); |
ac6bf2ba | 29 | static void clicked_menu(GtkMenuItem *, gpointer userdata); |
2d504956 | 30 | static void toggled_menu(GtkCheckMenuItem *, gpointer userdata); |
460b9539 | 31 | |
a99c4e9a RK |
32 | static int enable_rtp(disorder_eclient *c, |
33 | disorder_eclient_no_response *completed, | |
34 | void *v); | |
35 | static int disable_rtp(disorder_eclient *c, | |
36 | disorder_eclient_no_response *completed, | |
37 | void *v); | |
38 | ||
460b9539 | 39 | static double left(double v, double b); |
40 | static double right(double v, double b); | |
41 | static double volume(double l, double r); | |
42 | static double balance(double l, double r); | |
43 | ||
44 | static void volume_adjusted(GtkAdjustment *a, gpointer user_data); | |
45 | static gchar *format_volume(GtkScale *scale, gdouble value); | |
46 | static gchar *format_balance(GtkScale *scale, gdouble value); | |
47 | ||
6856f665 RK |
48 | static void icon_changed(const char *event, |
49 | void *evendata, | |
50 | void *callbackdata); | |
1f868ca3 RK |
51 | static void volume_changed(const char *event, |
52 | void *eventdata, | |
53 | void *callbackdata); | |
96295b25 RK |
54 | static void control_minimode(const char *event, |
55 | void *eventdata, | |
56 | void *callbackdata); | |
1f868ca3 | 57 | |
460b9539 | 58 | /* Control bar ------------------------------------------------------------- */ |
59 | ||
fb44b59e RK |
60 | /** @brief Guard against feedback */ |
61 | int suppress_actions = 1; | |
460b9539 | 62 | |
219dc95c RK |
63 | /** @brief Definition of an icon |
64 | * | |
3849817f RK |
65 | * We have two kinds of icon: |
66 | * - action icons, which just do something but don't have a state as such | |
67 | * - toggle icons, which toggle between two states ("on" and "off"). | |
68 | * | |
69 | * The scratch button is an action icon; currently all the others are toggle | |
70 | * icons. | |
71 | * | |
72 | * (All icons can be sensitive or insensitive, separately to the above.) | |
219dc95c RK |
73 | */ |
74 | struct icon { | |
851bbc58 RK |
75 | /** @brief True to use GTK+ stock icons instead of filenames */ |
76 | gboolean stock; | |
77 | ||
3849817f RK |
78 | /** @brief Filename for 'on' image */ |
79 | const char *icon_on; | |
80 | ||
81 | /** @brief Text for 'on' tooltip */ | |
82 | const char *tip_on; | |
219dc95c | 83 | |
3849817f RK |
84 | /** @brief Filename for 'off' image or NULL for an action icon */ |
85 | const char *icon_off; | |
86 | ||
851bbc58 | 87 | /** @brief Text for 'off' tooltip */ |
3849817f | 88 | const char *tip_off; |
219dc95c | 89 | |
ac6bf2ba RK |
90 | /** @brief Associated menu item or NULL */ |
91 | const char *menuitem; | |
219dc95c | 92 | |
6856f665 RK |
93 | /** @brief Events that change this icon, separated by spaces */ |
94 | const char *events; | |
95 | ||
3849817f RK |
96 | /** @brief @ref eclient.h function to call to go from off to on |
97 | * | |
98 | * For action buttons, this should be NULL. | |
99 | */ | |
100 | int (*action_go_on)(disorder_eclient *c, | |
101 | disorder_eclient_no_response *completed, | |
102 | void *v); | |
219dc95c | 103 | |
3849817f RK |
104 | /** @brief @ref eclient.h function to call to go from on to off |
105 | * | |
106 | * For action buttons, this action is used. | |
107 | */ | |
108 | int (*action_go_off)(disorder_eclient *c, | |
109 | disorder_eclient_no_response *completed, | |
110 | void *v); | |
219dc95c | 111 | |
3849817f RK |
112 | /** @brief Get button state |
113 | * @return 1 for on, 0 for off | |
114 | */ | |
115 | int (*on)(void); | |
116 | ||
117 | /** @brief Get button sensitivity | |
118 | * @return 1 for sensitive, 0 for insensitive | |
119 | * | |
120 | * Can be NULL for always sensitive. | |
121 | */ | |
122 | int (*sensitive)(void); | |
2d504956 | 123 | |
219dc95c | 124 | /** @brief Pointer to button */ |
460b9539 | 125 | GtkWidget *button; |
ac6bf2ba RK |
126 | |
127 | /** @brief Pointer to menu item */ | |
128 | GtkWidget *item; | |
3849817f RK |
129 | |
130 | GtkWidget *image_on; | |
131 | GtkWidget *image_off; | |
219dc95c RK |
132 | }; |
133 | ||
3849817f RK |
134 | static int pause_resume_on(void) { |
135 | return !(last_state & DISORDER_TRACK_PAUSED); | |
136 | } | |
137 | ||
138 | static int pause_resume_sensitive(void) { | |
851bbc58 RK |
139 | return playing_track |
140 | && !!(last_state & DISORDER_PLAYING) | |
6f09d54d | 141 | && (last_rights & RIGHT_PAUSE); |
3849817f RK |
142 | } |
143 | ||
144 | static int scratch_sensitive(void) { | |
6f09d54d | 145 | return !!(last_state & DISORDER_PLAYING) |
afe9f743 | 146 | && right_scratchable(last_rights, config->username, playing_track); |
6f09d54d RK |
147 | } |
148 | ||
149 | static int random_sensitive(void) { | |
150 | return !!(last_rights & RIGHT_GLOBAL_PREFS); | |
3849817f RK |
151 | } |
152 | ||
153 | static int random_enabled(void) { | |
154 | return !!(last_state & DISORDER_RANDOM_ENABLED); | |
155 | } | |
156 | ||
851bbc58 | 157 | #if 0 |
6f09d54d RK |
158 | static int playing_sensitive(void) { |
159 | return !!(last_rights & RIGHT_GLOBAL_PREFS); | |
160 | } | |
161 | ||
3849817f RK |
162 | static int playing_enabled(void) { |
163 | return !!(last_state & DISORDER_PLAYING_ENABLED); | |
164 | } | |
851bbc58 | 165 | #endif |
2d504956 | 166 | |
3849817f RK |
167 | static int rtp_enabled(void) { |
168 | return rtp_is_running; | |
169 | } | |
170 | ||
171 | static int rtp_sensitive(void) { | |
172 | return rtp_supported; | |
173 | } | |
2d504956 | 174 | |
219dc95c RK |
175 | /** @brief Table of all icons */ |
176 | static struct icon icons[] = { | |
2d504956 | 177 | { |
851bbc58 RK |
178 | stock: TRUE, |
179 | icon_on: GTK_STOCK_MEDIA_PAUSE, | |
3849817f | 180 | tip_on: "Pause playing track", |
851bbc58 | 181 | icon_off: GTK_STOCK_MEDIA_PLAY, |
3849817f RK |
182 | tip_off: "Resume playing track", |
183 | menuitem: "<GdisorderMain>/Control/Playing", | |
184 | on: pause_resume_on, | |
185 | sensitive: pause_resume_sensitive, | |
186 | action_go_on: disorder_eclient_resume, | |
187 | action_go_off: disorder_eclient_pause, | |
851bbc58 | 188 | events: "pause-changed playing-changed rights-changed playing-track-changed", |
2d504956 RK |
189 | }, |
190 | { | |
851bbc58 RK |
191 | stock: TRUE, |
192 | icon_on: GTK_STOCK_STOP, | |
3849817f RK |
193 | tip_on: "Cancel playing track", |
194 | menuitem: "<GdisorderMain>/Control/Scratch", | |
195 | sensitive: scratch_sensitive, | |
196 | action_go_off: disorder_eclient_scratch_playing, | |
6856f665 | 197 | events: "playing-track-changed rights-changed", |
2d504956 RK |
198 | }, |
199 | { | |
851bbc58 RK |
200 | stock: TRUE, |
201 | icon_on: GTK_STOCK_YES, | |
3849817f | 202 | tip_on: "Disable random play", |
851bbc58 | 203 | icon_off: GTK_STOCK_NO, |
3849817f RK |
204 | tip_off: "Enable random play", |
205 | menuitem: "<GdisorderMain>/Control/Random play", | |
206 | on: random_enabled, | |
6f09d54d | 207 | sensitive: random_sensitive, |
3849817f RK |
208 | action_go_on: disorder_eclient_random_enable, |
209 | action_go_off: disorder_eclient_random_disable, | |
6856f665 | 210 | events: "random-changed rights-changed", |
2d504956 | 211 | }, |
851bbc58 | 212 | #if 0 |
2d504956 | 213 | { |
851bbc58 RK |
214 | stock: TRUE, |
215 | icon_on: GTK_STOCK_YES, | |
3849817f | 216 | tip_on: "Disable play", |
851bbc58 | 217 | icon_off: GTK_STOCK_NO, |
3849817f RK |
218 | tip_off: "Enable play", |
219 | on: playing_enabled, | |
6f09d54d | 220 | sensitive: playing_sensitive, |
3849817f RK |
221 | action_go_on: disorder_eclient_enable, |
222 | action_go_off: disorder_eclient_disable, | |
6856f665 | 223 | events: "enabled-changed rights-changed", |
2d504956 | 224 | }, |
851bbc58 | 225 | #endif |
2d504956 | 226 | { |
851bbc58 RK |
227 | stock: TRUE, |
228 | icon_on: GTK_STOCK_CONNECT, | |
3849817f | 229 | tip_on: "Stop playing network stream", |
851bbc58 | 230 | icon_off: GTK_STOCK_DISCONNECT, |
3849817f RK |
231 | tip_off: "Play network stream", |
232 | menuitem: "<GdisorderMain>/Control/Network player", | |
233 | on: rtp_enabled, | |
234 | sensitive: rtp_sensitive, | |
235 | action_go_on: enable_rtp, | |
6856f665 RK |
236 | action_go_off: disable_rtp, |
237 | events: "rtp-changed", | |
2d504956 | 238 | }, |
460b9539 | 239 | }; |
219dc95c RK |
240 | |
241 | /** @brief Count of icons */ | |
460b9539 | 242 | #define NICONS (int)(sizeof icons / sizeof *icons) |
243 | ||
219dc95c RK |
244 | static GtkAdjustment *volume_adj; |
245 | static GtkAdjustment *balance_adj; | |
3c499fe7 RK |
246 | static GtkWidget *volume_widget; |
247 | static GtkWidget *balance_widget; | |
460b9539 | 248 | |
219dc95c | 249 | /** @brief Create the control bar */ |
e9b70a84 | 250 | GtkWidget *control_widget(void) { |
460b9539 | 251 | GtkWidget *hbox = gtk_hbox_new(FALSE, 1), *vbox; |
460b9539 | 252 | int n; |
253 | ||
254 | D(("control_widget")); | |
ac6bf2ba | 255 | assert(mainmenufactory); /* ordering must be right */ |
460b9539 | 256 | for(n = 0; n < NICONS; ++n) { |
3849817f | 257 | /* Create the button */ |
3849817f RK |
258 | icons[n].button = gtk_button_new(); |
259 | gtk_widget_set_style(icons[n].button, tool_style); | |
851bbc58 RK |
260 | if(icons[n].stock) { |
261 | /* We'll use the stock icons for this one */ | |
262 | gtk_button_set_use_stock(GTK_BUTTON(icons[n].button), TRUE); | |
263 | icons[n].image_on = gtk_image_new_from_stock(icons[n].icon_on, | |
264 | GTK_ICON_SIZE_LARGE_TOOLBAR); | |
265 | if(icons[n].icon_off) | |
266 | icons[n].image_off = gtk_image_new_from_stock(icons[n].icon_off, | |
267 | GTK_ICON_SIZE_LARGE_TOOLBAR); | |
268 | } else { | |
269 | /* Create the 'on' image */ | |
270 | icons[n].image_on = gtk_image_new_from_pixbuf(find_image(icons[n].icon_on)); | |
271 | gtk_widget_set_style(icons[n].image_on, tool_style); | |
272 | /* If it's a toggle icon, create the 'off' half too */ | |
273 | if(icons[n].icon_off) { | |
274 | icons[n].image_off = gtk_image_new_from_pixbuf(find_image(icons[n].icon_off)); | |
275 | gtk_widget_set_style(icons[n].image_off, tool_style); | |
276 | } | |
277 | } | |
3849817f | 278 | g_object_ref(icons[n].image_on); |
851bbc58 | 279 | if(icons[n].image_off) |
3849817f | 280 | g_object_ref(icons[n].image_off); |
460b9539 | 281 | g_signal_connect(G_OBJECT(icons[n].button), "clicked", |
ac6bf2ba | 282 | G_CALLBACK(clicked_icon), &icons[n]); |
460b9539 | 283 | /* pop the icon in a vbox so it doesn't get vertically stretch if there are |
284 | * taller things in the control bar */ | |
285 | vbox = gtk_vbox_new(FALSE, 0); | |
286 | gtk_box_pack_start(GTK_BOX(vbox), icons[n].button, TRUE, FALSE, 0); | |
287 | gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0); | |
ac6bf2ba | 288 | if(icons[n].menuitem) { |
3849817f | 289 | /* Find the menu item */ |
ac6bf2ba RK |
290 | icons[n].item = gtk_item_factory_get_widget(mainmenufactory, |
291 | icons[n].menuitem); | |
3849817f | 292 | if(icons[n].icon_off) |
2d504956 RK |
293 | g_signal_connect(G_OBJECT(icons[n].item), "toggled", |
294 | G_CALLBACK(toggled_menu), &icons[n]); | |
3849817f | 295 | else |
2d504956 RK |
296 | g_signal_connect(G_OBJECT(icons[n].item), "activate", |
297 | G_CALLBACK(clicked_menu), &icons[n]); | |
ac6bf2ba | 298 | } |
6856f665 RK |
299 | /* Make sure the icon is updated when relevant things changed */ |
300 | char **events = split(icons[n].events, 0, 0, 0, 0); | |
301 | while(*events) | |
302 | event_register(*events++, icon_changed, &icons[n]); | |
f513e538 | 303 | event_register("connected-changed", icon_changed, &icons[n]); |
460b9539 | 304 | } |
305 | /* create the adjustments for the volume control */ | |
306 | volume_adj = GTK_ADJUSTMENT(gtk_adjustment_new(0, 0, goesupto, | |
307 | goesupto / 20, goesupto / 20, | |
308 | 0)); | |
309 | balance_adj = GTK_ADJUSTMENT(gtk_adjustment_new(0, -1, 1, | |
310 | 0.2, 0.2, 0)); | |
311 | /* the volume control */ | |
3c499fe7 | 312 | volume_widget = gtk_hscale_new(volume_adj); |
3c499fe7 RK |
313 | balance_widget = gtk_hscale_new(balance_adj); |
314 | gtk_widget_set_style(volume_widget, tool_style); | |
315 | gtk_widget_set_style(balance_widget, tool_style); | |
316 | gtk_scale_set_digits(GTK_SCALE(volume_widget), 10); | |
317 | gtk_scale_set_digits(GTK_SCALE(balance_widget), 10); | |
318 | gtk_widget_set_size_request(volume_widget, 192, -1); | |
319 | gtk_widget_set_size_request(balance_widget, 192, -1); | |
a31f7a72 RK |
320 | gtk_widget_set_tooltip_text(volume_widget, "Volume"); |
321 | gtk_widget_set_tooltip_text(balance_widget, "Balance"); | |
3c499fe7 RK |
322 | gtk_box_pack_start(GTK_BOX(hbox), volume_widget, FALSE, TRUE, 0); |
323 | gtk_box_pack_start(GTK_BOX(hbox), balance_widget, FALSE, TRUE, 0); | |
460b9539 | 324 | /* space updates rather than hammering the server */ |
3c499fe7 RK |
325 | gtk_range_set_update_policy(GTK_RANGE(volume_widget), GTK_UPDATE_DELAYED); |
326 | gtk_range_set_update_policy(GTK_RANGE(balance_widget), GTK_UPDATE_DELAYED); | |
460b9539 | 327 | /* notice when the adjustments are changed */ |
328 | g_signal_connect(G_OBJECT(volume_adj), "value-changed", | |
329 | G_CALLBACK(volume_adjusted), 0); | |
330 | g_signal_connect(G_OBJECT(balance_adj), "value-changed", | |
331 | G_CALLBACK(volume_adjusted), 0); | |
332 | /* format the volume/balance values ourselves */ | |
3c499fe7 | 333 | g_signal_connect(G_OBJECT(volume_widget), "format-value", |
460b9539 | 334 | G_CALLBACK(format_volume), 0); |
3c499fe7 | 335 | g_signal_connect(G_OBJECT(balance_widget), "format-value", |
460b9539 | 336 | G_CALLBACK(format_balance), 0); |
1f868ca3 | 337 | event_register("volume-changed", volume_changed, 0); |
6856f665 | 338 | event_register("rtp-changed", volume_changed, 0); |
96295b25 | 339 | event_register("mini-mode-changed", control_minimode, 0); |
460b9539 | 340 | return hbox; |
341 | } | |
342 | ||
96295b25 RK |
343 | /** @brief Return TRUE if volume setting is supported */ |
344 | static int volume_supported(void) { | |
345 | /* TODO: if the server doesn't know how to set the volume [but isn't using | |
346 | * network play] then we should have volume_supported = FALSE */ | |
347 | return (!rtp_supported | |
348 | || (rtp_supported && backend && backend->set_volume)); | |
349 | } | |
350 | ||
6d1302f0 | 351 | /** @brief Update the volume control when it changes */ |
1f868ca3 RK |
352 | static void volume_changed(const char attribute((unused)) *event, |
353 | void attribute((unused)) *eventdata, | |
354 | void attribute((unused)) *callbackdata) { | |
460b9539 | 355 | double l, r; |
356 | ||
1f868ca3 | 357 | D(("volume_changed")); |
fb44b59e | 358 | ++suppress_actions; |
6856f665 | 359 | /* Only display volume/balance controls if they will work */ |
96295b25 | 360 | if(volume_supported()) { |
6856f665 | 361 | gtk_widget_show(volume_widget); |
1b0a811c RK |
362 | if(full_mode) |
363 | gtk_widget_show(balance_widget); | |
6856f665 RK |
364 | l = volume_l / 100.0; |
365 | r = volume_r / 100.0; | |
366 | gtk_adjustment_set_value(volume_adj, volume(l, r) * goesupto); | |
367 | gtk_adjustment_set_value(balance_adj, balance(l, r)); | |
368 | } else { | |
369 | gtk_widget_hide(volume_widget); | |
370 | gtk_widget_hide(balance_widget); | |
371 | } | |
fb44b59e | 372 | --suppress_actions; |
460b9539 | 373 | } |
374 | ||
ad58ebcc | 375 | /** @brief Update the state of one of the control icons |
ad58ebcc | 376 | */ |
6856f665 RK |
377 | static void icon_changed(const char attribute((unused)) *event, |
378 | void attribute((unused)) *evendata, | |
379 | void *callbackdata) { | |
00e0c652 | 380 | //fprintf(stderr, "icon_changed (%s)\n", event); |
6856f665 | 381 | const struct icon *const icon = callbackdata; |
3849817f RK |
382 | int on = icon->on ? icon->on() : 1; |
383 | int sensitive = icon->sensitive ? icon->sensitive() : 1; | |
00e0c652 | 384 | //fprintf(stderr, "sensitive->%d\n", sensitive); |
3849817f RK |
385 | GtkWidget *child, *newchild; |
386 | ||
387 | ++suppress_actions; | |
ad58ebcc | 388 | /* If the connection is down nothing is ever usable */ |
00959300 | 389 | if(!(last_state & DISORDER_CONNECTED)) |
3849817f | 390 | sensitive = 0; |
00e0c652 | 391 | //fprintf(stderr, "(checked connected) sensitive->%d\n", sensitive); |
3849817f RK |
392 | /* Replace the child */ |
393 | newchild = on ? icon->image_on : icon->image_off; | |
394 | child = gtk_bin_get_child(GTK_BIN(icon->button)); | |
395 | if(child != newchild) { | |
396 | if(child) | |
397 | gtk_container_remove(GTK_CONTAINER(icon->button), child); | |
398 | gtk_container_add(GTK_CONTAINER(icon->button), newchild); | |
399 | gtk_widget_show(newchild); | |
400 | } | |
7392b131 RK |
401 | /* If you disable play or random play NOT via the icon (for instance, via the |
402 | * edit menu or via a completely separate command line invocation) then the | |
00e0c652 | 403 | * icon shows up as insensitive. Hover the mouse over it and the correct |
7392b131 RK |
404 | * state is immediately displayed. sensitive and GTK_WIDGET_SENSITIVE show |
405 | * it to be in the correct state, so I think this is may be a GTK+ bug. */ | |
3849817f | 406 | if(icon->tip_on) |
a31f7a72 RK |
407 | gtk_widget_set_tooltip_text(icon->button, |
408 | on ? icon->tip_on : icon->tip_off); | |
3849817f RK |
409 | gtk_widget_set_sensitive(icon->button, sensitive); |
410 | /* Icons with an associated menu item */ | |
ac6bf2ba | 411 | if(icon->item) { |
3849817f RK |
412 | if(icon->icon_off) |
413 | gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(icon->item), on); | |
414 | gtk_widget_set_sensitive(icon->item, sensitive); | |
ac6bf2ba | 415 | } |
3849817f | 416 | --suppress_actions; |
a99c4e9a RK |
417 | } |
418 | ||
53fa91bb | 419 | static void icon_action_completed(void attribute((unused)) *v, |
abf99697 RK |
420 | const char *err) { |
421 | if(err) | |
422 | popup_protocol_error(0, err); | |
53fa91bb RK |
423 | } |
424 | ||
460b9539 | 425 | static void clicked_icon(GtkButton attribute((unused)) *button, |
426 | gpointer userdata) { | |
427 | const struct icon *icon = userdata; | |
428 | ||
3849817f RK |
429 | if(suppress_actions) |
430 | return; | |
431 | if(!icon->on || icon->on()) | |
432 | icon->action_go_off(client, icon_action_completed, 0); | |
433 | else | |
434 | icon->action_go_on(client, icon_action_completed, 0); | |
460b9539 | 435 | } |
436 | ||
ac6bf2ba RK |
437 | static void clicked_menu(GtkMenuItem attribute((unused)) *menuitem, |
438 | gpointer userdata) { | |
3849817f | 439 | clicked_icon(NULL, userdata); |
ac6bf2ba RK |
440 | } |
441 | ||
3849817f | 442 | static void toggled_menu(GtkCheckMenuItem attribute((unused)) *menuitem, |
2d504956 | 443 | gpointer userdata) { |
3849817f | 444 | clicked_icon(NULL, userdata); |
2d504956 RK |
445 | } |
446 | ||
699517af RK |
447 | /** @brief Called when a volume command completes */ |
448 | static void volume_completed(void attribute((unused)) *v, | |
abf99697 | 449 | const char *err, |
699517af RK |
450 | int attribute((unused)) l, |
451 | int attribute((unused)) r) { | |
abf99697 RK |
452 | if(err) |
453 | popup_protocol_error(0, err); | |
699517af RK |
454 | /* We don't set the UI's notion of the volume here, it is set from the log |
455 | * regardless of the reason it changed */ | |
456 | } | |
457 | ||
219dc95c | 458 | /** @brief Called when the volume has been adjusted */ |
460b9539 | 459 | static void volume_adjusted(GtkAdjustment attribute((unused)) *a, |
460 | gpointer attribute((unused)) user_data) { | |
461 | double v = gtk_adjustment_get_value(volume_adj) / goesupto; | |
462 | double b = gtk_adjustment_get_value(balance_adj); | |
463 | ||
fb44b59e | 464 | if(suppress_actions) |
460b9539 | 465 | /* This is the result of an update from the server, not a change from the |
466 | * user. Don't feedback! */ | |
467 | return; | |
468 | D(("volume_adjusted")); | |
469 | /* force to 'stereotypical' values */ | |
470 | v = nearbyint(100 * v) / 100; | |
471 | b = nearbyint(5 * b) / 5; | |
472 | /* Set the volume. We don't want a reply, we'll get the actual new volume | |
473 | * from the log. */ | |
321f7536 RK |
474 | if(rtp_supported) { |
475 | int l = nearbyint(left(v, b) * 100), r = nearbyint(right(v, b) * 100); | |
b50cfb8a RK |
476 | if(backend && backend->set_volume) |
477 | backend->set_volume(&l, &r); | |
321f7536 | 478 | } else |
699517af | 479 | disorder_eclient_volume(client, volume_completed, |
321f7536 RK |
480 | nearbyint(left(v, b) * 100), |
481 | nearbyint(right(v, b) * 100), | |
482 | 0); | |
460b9539 | 483 | } |
484 | ||
219dc95c | 485 | /** @brief Formats the volume value */ |
460b9539 | 486 | static gchar *format_volume(GtkScale attribute((unused)) *scale, |
487 | gdouble value) { | |
488 | char s[32]; | |
489 | ||
490 | snprintf(s, sizeof s, "%.1f", (double)value); | |
2fc4475d | 491 | return g_strdup(s); |
460b9539 | 492 | } |
493 | ||
219dc95c | 494 | /** @brief Formats the balance value */ |
460b9539 | 495 | static gchar *format_balance(GtkScale attribute((unused)) *scale, |
496 | gdouble value) { | |
497 | char s[32]; | |
498 | ||
499 | if(fabs(value) < 0.1) | |
2fc4475d | 500 | return g_strdup("0"); |
460b9539 | 501 | snprintf(s, sizeof s, "%+.1f", (double)value); |
2fc4475d | 502 | return g_strdup(s); |
460b9539 | 503 | } |
504 | ||
505 | /* Volume mapping. We consider left, right, volume to be in [0,1] | |
506 | * and balance to be in [-1,1]. | |
507 | * | |
508 | * First, we just have volume = max(left, right). | |
509 | * | |
510 | * Balance we consider to linearly represent the amount by which the quieter | |
511 | * channel differs from the louder. In detail: | |
512 | * | |
513 | * if right > left then balance > 0: | |
514 | * balance = 0 => left = right (as an endpoint, not an instance) | |
515 | * balance = 1 => left = 0 | |
516 | * fitting to linear, left = right * (1 - balance) | |
517 | * so balance = 1 - left / right | |
518 | * (right > left => right > 0 so no division by 0.) | |
519 | * | |
520 | * if left > right then balance < 0: | |
521 | * balance = 0 => right = left (same caveat as above) | |
522 | * balance = -1 => right = 0 | |
523 | * again fitting to linear, right = left * (1 + balance) | |
524 | * so balance = right / left - 1 | |
525 | * (left > right => left > 0 so no division by 0.) | |
526 | * | |
527 | * if left = right then we just have balance = 0. | |
528 | * | |
529 | * Thanks to Clive and Andrew. | |
530 | */ | |
531 | ||
219dc95c | 532 | /** @brief Return the greater of @p x and @p y */ |
460b9539 | 533 | static double max(double x, double y) { |
534 | return x > y ? x : y; | |
535 | } | |
536 | ||
219dc95c | 537 | /** @brief Compute the left channel volume */ |
460b9539 | 538 | static double left(double v, double b) { |
539 | if(b > 0) /* volume = right */ | |
540 | return v * (1 - b); | |
541 | else /* volume = left */ | |
542 | return v; | |
543 | } | |
544 | ||
219dc95c | 545 | /** @brief Compute the right channel volume */ |
460b9539 | 546 | static double right(double v, double b) { |
547 | if(b > 0) /* volume = right */ | |
548 | return v; | |
549 | else /* volume = left */ | |
550 | return v * (1 + b); | |
551 | } | |
552 | ||
219dc95c | 553 | /** @brief Compute the overall volume */ |
460b9539 | 554 | static double volume(double l, double r) { |
555 | return max(l, r); | |
556 | } | |
557 | ||
219dc95c | 558 | /** @brief Compute the balance */ |
460b9539 | 559 | static double balance(double l, double r) { |
560 | if(l > r) | |
561 | return r / l - 1; | |
562 | else if(r > l) | |
563 | return 1 - l / r; | |
564 | else /* left = right */ | |
565 | return 0; | |
566 | } | |
567 | ||
a99c4e9a RK |
568 | /** @brief Called to enable RTP play |
569 | * | |
570 | * Rather odd signature is to fit in with the other icons which all call @ref | |
571 | * lib/eclient.h functions. | |
572 | */ | |
573 | static int enable_rtp(disorder_eclient attribute((unused)) *c, | |
574 | disorder_eclient_no_response attribute((unused)) *completed, | |
575 | void attribute((unused)) *v) { | |
576 | start_rtp(); | |
577 | return 0; | |
578 | } | |
579 | ||
580 | /** @brief Called to disable RTP play | |
581 | * | |
582 | * Rather odd signature is to fit in with the other icons which all call @ref | |
583 | * lib/eclient.h functions. | |
584 | */ | |
585 | static int disable_rtp(disorder_eclient attribute((unused)) *c, | |
586 | disorder_eclient_no_response attribute((unused)) *completed, | |
587 | void attribute((unused)) *v) { | |
588 | stop_rtp(); | |
589 | return 0; | |
590 | } | |
591 | ||
96295b25 RK |
592 | static void control_minimode(const char attribute((unused)) *event, |
593 | void attribute((unused)) *evendata, | |
594 | void attribute((unused)) *callbackdata) { | |
faa19503 | 595 | if(full_mode && volume_supported()) { |
96295b25 | 596 | gtk_widget_show(balance_widget); |
faa19503 RK |
597 | gtk_scale_set_value_pos(GTK_SCALE(volume_widget), GTK_POS_TOP); |
598 | } else { | |
96295b25 | 599 | gtk_widget_hide(balance_widget); |
faa19503 RK |
600 | gtk_scale_set_value_pos(GTK_SCALE(volume_widget), GTK_POS_RIGHT); |
601 | } | |
96295b25 RK |
602 | } |
603 | ||
460b9539 | 604 | /* |
605 | Local Variables: | |
606 | c-basic-offset:2 | |
607 | comment-column:40 | |
608 | fill-column:79 | |
609 | indent-tabs-mode:nil | |
610 | End: | |
611 | */ |