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