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