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 | */ | |
219dc95c RK |
20 | /** @file disobedience/control.c |
21 | * @brief Volume control and buttons | |
22 | */ | |
460b9539 | 23 | |
24 | #include "disobedience.h" | |
321f7536 | 25 | #include "mixer.h" |
460b9539 | 26 | |
e9b70a84 | 27 | /* Forward declarations ---------------------------------------------------- */ |
28 | ||
29 | WT(adjustment); | |
30 | WT(hscale); | |
31 | WT(hbox); | |
e9b70a84 | 32 | WT(button); |
33 | WT(image); | |
34 | WT(label); | |
35 | WT(vbox); | |
460b9539 | 36 | |
37 | struct icon; | |
38 | ||
39 | static void update_pause(const struct icon *); | |
40 | static void update_play(const struct icon *); | |
41 | static void update_scratch(const struct icon *); | |
42 | static void update_random_enable(const struct icon *); | |
43 | static void update_random_disable(const struct icon *); | |
44 | static void update_enable(const struct icon *); | |
45 | static void update_disable(const struct icon *); | |
a99c4e9a RK |
46 | static void update_rtp(const struct icon *); |
47 | static void update_nortp(const struct icon *); | |
460b9539 | 48 | static void clicked_icon(GtkButton *, gpointer); |
ac6bf2ba | 49 | static void clicked_menu(GtkMenuItem *, gpointer userdata); |
2d504956 | 50 | static void toggled_menu(GtkCheckMenuItem *, gpointer userdata); |
460b9539 | 51 | |
a99c4e9a RK |
52 | static int enable_rtp(disorder_eclient *c, |
53 | disorder_eclient_no_response *completed, | |
54 | void *v); | |
55 | static int disable_rtp(disorder_eclient *c, | |
56 | disorder_eclient_no_response *completed, | |
57 | void *v); | |
58 | ||
460b9539 | 59 | static double left(double v, double b); |
60 | static double right(double v, double b); | |
61 | static double volume(double l, double r); | |
62 | static double balance(double l, double r); | |
63 | ||
64 | static void volume_adjusted(GtkAdjustment *a, gpointer user_data); | |
65 | static gchar *format_volume(GtkScale *scale, gdouble value); | |
66 | static gchar *format_balance(GtkScale *scale, gdouble value); | |
67 | ||
1f868ca3 RK |
68 | static void volume_changed(const char *event, |
69 | void *eventdata, | |
70 | void *callbackdata); | |
71 | ||
460b9539 | 72 | /* Control bar ------------------------------------------------------------- */ |
73 | ||
fb44b59e RK |
74 | /** @brief Guard against feedback */ |
75 | int suppress_actions = 1; | |
460b9539 | 76 | |
219dc95c RK |
77 | /** @brief Definition of an icon |
78 | * | |
79 | * The design here is rather mad: rather than changing the image displayed by | |
80 | * icons according to their state, we flip the visibility of pairs of icons. | |
81 | */ | |
82 | struct icon { | |
83 | /** @brief Filename for image */ | |
460b9539 | 84 | const char *icon; |
219dc95c RK |
85 | |
86 | /** @brief Text for tooltip */ | |
460b9539 | 87 | const char *tip; |
219dc95c | 88 | |
ac6bf2ba RK |
89 | /** @brief Associated menu item or NULL */ |
90 | const char *menuitem; | |
219dc95c RK |
91 | |
92 | /** @brief Called to update button when state may have changed */ | |
460b9539 | 93 | void (*update)(const struct icon *i); |
219dc95c RK |
94 | |
95 | /** @brief @ref eclient.h function to call */ | |
460b9539 | 96 | int (*action)(disorder_eclient *c, |
97 | disorder_eclient_no_response *completed, | |
98 | void *v); | |
219dc95c | 99 | |
2d504956 RK |
100 | /** @brief Flag */ |
101 | unsigned flags; | |
102 | ||
219dc95c | 103 | /** @brief Pointer to button */ |
460b9539 | 104 | GtkWidget *button; |
ac6bf2ba RK |
105 | |
106 | /** @brief Pointer to menu item */ | |
107 | GtkWidget *item; | |
219dc95c RK |
108 | }; |
109 | ||
2d504956 RK |
110 | /** @brief This is the active half of a pair */ |
111 | #define ICON_ACTIVE 0x0001 | |
112 | ||
113 | /** @brief This is the inactive half of a pair */ | |
114 | #define ICON_INACTIVE 0x0002 | |
115 | ||
219dc95c RK |
116 | /** @brief Table of all icons */ |
117 | static struct icon icons[] = { | |
2d504956 RK |
118 | { |
119 | "pause.png", /* icon */ | |
120 | "Pause playing track", /* tip */ | |
685bdfbd | 121 | "<GdisorderMain>/Control/Playing", /* menuitem */ |
2d504956 RK |
122 | update_pause, /* update */ |
123 | disorder_eclient_pause, /* action */ | |
685bdfbd | 124 | ICON_ACTIVE, /* flags */ |
2d504956 RK |
125 | 0, /* button */ |
126 | 0 /* item */ | |
127 | }, | |
128 | { | |
129 | "play.png", /* icon */ | |
130 | "Resume playing track", /* tip */ | |
685bdfbd | 131 | "<GdisorderMain>/Control/Playing", /* menuitem */ |
2d504956 RK |
132 | update_play, /* update */ |
133 | disorder_eclient_resume, /* action */ | |
685bdfbd | 134 | ICON_INACTIVE, /* flags */ |
2d504956 RK |
135 | 0, /* button */ |
136 | 0 /* item */ | |
137 | }, | |
138 | { | |
139 | "cross.png", /* icon */ | |
140 | "Cancel playing track", /* tip */ | |
141 | "<GdisorderMain>/Control/Scratch", /* menuitem */ | |
142 | update_scratch, /* update */ | |
143 | disorder_eclient_scratch_playing, /* action */ | |
144 | 0, /* flags */ | |
145 | 0, /* button */ | |
146 | 0 /* item */ | |
147 | }, | |
148 | { | |
149 | "random.png", /* icon */ | |
150 | "Enable random play", /* tip */ | |
151 | "<GdisorderMain>/Control/Random play", /* menuitem */ | |
152 | update_random_enable, /* update */ | |
153 | disorder_eclient_random_enable, /* action */ | |
154 | ICON_INACTIVE, /* flags */ | |
155 | 0, /* button */ | |
156 | 0 /* item */ | |
157 | }, | |
158 | { | |
159 | "randomcross.png", /* icon */ | |
160 | "Disable random play", /* tip */ | |
161 | "<GdisorderMain>/Control/Random play", /* menuitem */ | |
162 | update_random_disable, /* update */ | |
163 | disorder_eclient_random_disable, /* action */ | |
164 | ICON_ACTIVE, /* flags */ | |
165 | 0, /* button */ | |
166 | 0 /* item */ | |
167 | }, | |
168 | { | |
169 | "notes.png", /* icon */ | |
170 | "Enable play", /* tip */ | |
171 | 0, /* menuitem */ | |
172 | update_enable, /* update */ | |
173 | disorder_eclient_enable, /* action */ | |
174 | ICON_INACTIVE, /* flags */ | |
175 | 0, /* button */ | |
176 | 0 /* item */ | |
177 | }, | |
178 | { | |
179 | "notescross.png", /* icon */ | |
180 | "Disable play", /* tip */ | |
181 | 0, /* menuitem */ | |
182 | update_disable, /* update */ | |
183 | disorder_eclient_disable, /* action */ | |
184 | ICON_ACTIVE, /* flags */ | |
185 | 0, /* button */ | |
186 | 0 /* item */ | |
187 | }, | |
188 | { | |
189 | "speaker.png", /* icon */ | |
190 | "Play network stream", /* tip */ | |
685bdfbd | 191 | "<GdisorderMain>/Control/Network player", /* menuitem */ |
2d504956 RK |
192 | update_rtp, /* update */ |
193 | enable_rtp, /* action */ | |
194 | ICON_INACTIVE, /* flags */ | |
195 | 0, /* button */ | |
196 | 0 /* item */ | |
197 | }, | |
198 | { | |
199 | "speakercross.png", /* icon */ | |
200 | "Stop playing network stream", /* tip */ | |
685bdfbd | 201 | "<GdisorderMain>/Control/Network player", /* menuitem */ |
2d504956 RK |
202 | update_nortp, /* update */ |
203 | disable_rtp, /* action */ | |
204 | ICON_ACTIVE, /* flags */ | |
205 | 0, /* button */ | |
206 | 0 /* item */ | |
207 | }, | |
460b9539 | 208 | }; |
219dc95c RK |
209 | |
210 | /** @brief Count of icons */ | |
460b9539 | 211 | #define NICONS (int)(sizeof icons / sizeof *icons) |
212 | ||
219dc95c RK |
213 | static GtkAdjustment *volume_adj; |
214 | static GtkAdjustment *balance_adj; | |
3c499fe7 RK |
215 | static GtkWidget *volume_widget; |
216 | static GtkWidget *balance_widget; | |
460b9539 | 217 | |
6d1302f0 | 218 | /** @brief Called whenever last_state changes in any way */ |
6c7a654c RK |
219 | static void control_changed(const char attribute((unused)) *event, |
220 | void attribute((unused)) *evendata, | |
221 | void attribute((unused)) *callbackdata) { | |
6d1302f0 | 222 | int n; |
3c499fe7 | 223 | gboolean volume_supported; |
6d1302f0 | 224 | |
6c7a654c | 225 | D(("control_changed")); |
6d1302f0 RK |
226 | for(n = 0; n < NICONS; ++n) |
227 | icons[n].update(&icons[n]); | |
3c499fe7 RK |
228 | /* Only display volume/balance controls if they will work */ |
229 | if(!rtp_supported | |
230 | || (rtp_supported && mixer_supported(DEFAULT_BACKEND))) | |
231 | volume_supported = TRUE; | |
232 | else | |
233 | volume_supported = FALSE; | |
234 | (volume_supported ? gtk_widget_show : gtk_widget_hide)(volume_widget); | |
235 | (volume_supported ? gtk_widget_show : gtk_widget_hide)(balance_widget); | |
6d1302f0 RK |
236 | } |
237 | ||
219dc95c | 238 | /** @brief Create the control bar */ |
e9b70a84 | 239 | GtkWidget *control_widget(void) { |
460b9539 | 240 | GtkWidget *hbox = gtk_hbox_new(FALSE, 1), *vbox; |
460b9539 | 241 | int n; |
242 | ||
e9b70a84 | 243 | NW(hbox); |
460b9539 | 244 | D(("control_widget")); |
ac6bf2ba | 245 | assert(mainmenufactory); /* ordering must be right */ |
460b9539 | 246 | for(n = 0; n < NICONS; ++n) { |
e9b70a84 | 247 | NW(button); |
33288048 | 248 | icons[n].button = iconbutton(icons[n].icon, icons[n].tip); |
460b9539 | 249 | g_signal_connect(G_OBJECT(icons[n].button), "clicked", |
ac6bf2ba | 250 | G_CALLBACK(clicked_icon), &icons[n]); |
460b9539 | 251 | /* pop the icon in a vbox so it doesn't get vertically stretch if there are |
252 | * taller things in the control bar */ | |
e9b70a84 | 253 | NW(vbox); |
460b9539 | 254 | vbox = gtk_vbox_new(FALSE, 0); |
255 | gtk_box_pack_start(GTK_BOX(vbox), icons[n].button, TRUE, FALSE, 0); | |
256 | gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0); | |
ac6bf2ba RK |
257 | if(icons[n].menuitem) { |
258 | icons[n].item = gtk_item_factory_get_widget(mainmenufactory, | |
259 | icons[n].menuitem); | |
2d504956 RK |
260 | switch(icons[n].flags & (ICON_ACTIVE|ICON_INACTIVE)) { |
261 | case ICON_ACTIVE: | |
262 | g_signal_connect(G_OBJECT(icons[n].item), "toggled", | |
263 | G_CALLBACK(toggled_menu), &icons[n]); | |
264 | break; | |
265 | case ICON_INACTIVE: | |
266 | /* Don't connect two instances of the signal! */ | |
267 | break; | |
268 | default: | |
269 | g_signal_connect(G_OBJECT(icons[n].item), "activate", | |
270 | G_CALLBACK(clicked_menu), &icons[n]); | |
271 | break; | |
272 | } | |
ac6bf2ba | 273 | } |
460b9539 | 274 | } |
275 | /* create the adjustments for the volume control */ | |
e9b70a84 | 276 | NW(adjustment); |
460b9539 | 277 | volume_adj = GTK_ADJUSTMENT(gtk_adjustment_new(0, 0, goesupto, |
278 | goesupto / 20, goesupto / 20, | |
279 | 0)); | |
e9b70a84 | 280 | NW(adjustment); |
460b9539 | 281 | balance_adj = GTK_ADJUSTMENT(gtk_adjustment_new(0, -1, 1, |
282 | 0.2, 0.2, 0)); | |
283 | /* the volume control */ | |
e9b70a84 | 284 | NW(hscale); |
3c499fe7 | 285 | volume_widget = gtk_hscale_new(volume_adj); |
e9b70a84 | 286 | NW(hscale); |
3c499fe7 RK |
287 | balance_widget = gtk_hscale_new(balance_adj); |
288 | gtk_widget_set_style(volume_widget, tool_style); | |
289 | gtk_widget_set_style(balance_widget, tool_style); | |
290 | gtk_scale_set_digits(GTK_SCALE(volume_widget), 10); | |
291 | gtk_scale_set_digits(GTK_SCALE(balance_widget), 10); | |
292 | gtk_widget_set_size_request(volume_widget, 192, -1); | |
293 | gtk_widget_set_size_request(balance_widget, 192, -1); | |
294 | gtk_tooltips_set_tip(tips, volume_widget, "Volume", ""); | |
295 | gtk_tooltips_set_tip(tips, balance_widget, "Balance", ""); | |
296 | gtk_box_pack_start(GTK_BOX(hbox), volume_widget, FALSE, TRUE, 0); | |
297 | gtk_box_pack_start(GTK_BOX(hbox), balance_widget, FALSE, TRUE, 0); | |
460b9539 | 298 | /* space updates rather than hammering the server */ |
3c499fe7 RK |
299 | gtk_range_set_update_policy(GTK_RANGE(volume_widget), GTK_UPDATE_DELAYED); |
300 | gtk_range_set_update_policy(GTK_RANGE(balance_widget), GTK_UPDATE_DELAYED); | |
460b9539 | 301 | /* notice when the adjustments are changed */ |
302 | g_signal_connect(G_OBJECT(volume_adj), "value-changed", | |
303 | G_CALLBACK(volume_adjusted), 0); | |
304 | g_signal_connect(G_OBJECT(balance_adj), "value-changed", | |
305 | G_CALLBACK(volume_adjusted), 0); | |
306 | /* format the volume/balance values ourselves */ | |
3c499fe7 | 307 | g_signal_connect(G_OBJECT(volume_widget), "format-value", |
460b9539 | 308 | G_CALLBACK(format_volume), 0); |
3c499fe7 | 309 | g_signal_connect(G_OBJECT(balance_widget), "format-value", |
460b9539 | 310 | G_CALLBACK(format_balance), 0); |
6c7a654c RK |
311 | event_register("enabled-changed", control_changed, 0); |
312 | event_register("random-changed", control_changed, 0); | |
313 | event_register("pause-changed", control_changed, 0); | |
314 | event_register("playing-changed", control_changed, 0); | |
315 | event_register("rtp-changed", control_changed, 0); | |
1f868ca3 | 316 | event_register("volume-changed", volume_changed, 0); |
460b9539 | 317 | return hbox; |
318 | } | |
319 | ||
6d1302f0 | 320 | /** @brief Update the volume control when it changes */ |
1f868ca3 RK |
321 | static void volume_changed(const char attribute((unused)) *event, |
322 | void attribute((unused)) *eventdata, | |
323 | void attribute((unused)) *callbackdata) { | |
460b9539 | 324 | double l, r; |
325 | ||
1f868ca3 | 326 | D(("volume_changed")); |
460b9539 | 327 | l = volume_l / 100.0; |
328 | r = volume_r / 100.0; | |
fb44b59e | 329 | ++suppress_actions; |
460b9539 | 330 | gtk_adjustment_set_value(volume_adj, volume(l, r) * goesupto); |
331 | gtk_adjustment_set_value(balance_adj, balance(l, r)); | |
fb44b59e | 332 | --suppress_actions; |
460b9539 | 333 | } |
334 | ||
ad58ebcc | 335 | /** @brief Update the state of one of the control icons |
00959300 | 336 | * @param icon Target icon |
ad58ebcc | 337 | * @param visible True if this version of the button should be visible |
338 | * @param usable True if the button is currently usable | |
339 | * | |
340 | * Several of the icons, rather bizarrely, come in pairs: for instance exactly | |
341 | * one of the play and pause buttons is supposed to be visible at any given | |
342 | * moment. | |
343 | * | |
344 | * @p usable need not take into account server availability, that is done | |
345 | * automatically. | |
346 | */ | |
00959300 | 347 | static void update_icon(const struct icon *icon, |
ad58ebcc | 348 | int visible, int usable) { |
349 | /* If the connection is down nothing is ever usable */ | |
00959300 | 350 | if(!(last_state & DISORDER_CONNECTED)) |
ad58ebcc | 351 | usable = 0; |
00959300 | 352 | (visible ? gtk_widget_show : gtk_widget_hide)(icon->button); |
ad58ebcc | 353 | /* Only both updating usability if the button is visible */ |
354 | if(visible) | |
00959300 | 355 | gtk_widget_set_sensitive(icon->button, usable); |
ac6bf2ba | 356 | if(icon->item) { |
2d504956 RK |
357 | /* There's an associated menu item. These are always visible, but may not |
358 | * be usable. */ | |
359 | if((icon->flags & (ICON_ACTIVE|ICON_INACTIVE)) == ICON_ACTIVE) { | |
360 | /* The active half of a pair */ | |
361 | gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(icon->item), visible); | |
362 | } | |
363 | gtk_widget_set_sensitive(icon->item, usable); | |
ac6bf2ba | 364 | } |
460b9539 | 365 | } |
366 | ||
367 | static void update_pause(const struct icon *icon) { | |
00959300 RK |
368 | const int visible = !(last_state & DISORDER_TRACK_PAUSED); |
369 | const int usable = !!(last_state & DISORDER_PLAYING); /* TODO: might be a lie */ | |
370 | update_icon(icon, visible, usable); | |
460b9539 | 371 | } |
372 | ||
373 | static void update_play(const struct icon *icon) { | |
00959300 RK |
374 | const int visible = !!(last_state & DISORDER_TRACK_PAUSED); |
375 | const int usable = !!(last_state & DISORDER_PLAYING); | |
376 | update_icon(icon, visible, usable); | |
460b9539 | 377 | } |
378 | ||
379 | static void update_scratch(const struct icon *icon) { | |
00959300 RK |
380 | const int visible = 1; |
381 | const int usable = !!(last_state & DISORDER_PLAYING); | |
382 | update_icon(icon, visible, usable); | |
460b9539 | 383 | } |
384 | ||
385 | static void update_random_enable(const struct icon *icon) { | |
00959300 RK |
386 | const int visible = !(last_state & DISORDER_RANDOM_ENABLED); |
387 | const int usable = 1; | |
388 | update_icon(icon, visible, usable); | |
460b9539 | 389 | } |
390 | ||
391 | static void update_random_disable(const struct icon *icon) { | |
00959300 RK |
392 | const int visible = !!(last_state & DISORDER_RANDOM_ENABLED); |
393 | const int usable = 1; | |
394 | update_icon(icon, visible, usable); | |
460b9539 | 395 | } |
396 | ||
397 | static void update_enable(const struct icon *icon) { | |
00959300 RK |
398 | const int visible = !(last_state & DISORDER_PLAYING_ENABLED); |
399 | const int usable = 1; | |
400 | update_icon(icon, visible, usable); | |
460b9539 | 401 | } |
402 | ||
403 | static void update_disable(const struct icon *icon) { | |
00959300 RK |
404 | const int visible = !!(last_state & DISORDER_PLAYING_ENABLED); |
405 | const int usable = 1; | |
406 | update_icon(icon, visible, usable); | |
460b9539 | 407 | } |
408 | ||
a99c4e9a RK |
409 | static void update_rtp(const struct icon *icon) { |
410 | const int visible = !rtp_is_running; | |
411 | const int usable = rtp_supported; | |
412 | update_icon(icon, visible, usable); | |
413 | } | |
414 | ||
415 | static void update_nortp(const struct icon *icon) { | |
416 | const int visible = rtp_is_running; | |
417 | const int usable = rtp_supported; | |
418 | update_icon(icon, visible, usable); | |
419 | } | |
420 | ||
53fa91bb RK |
421 | static void icon_action_completed(void attribute((unused)) *v, |
422 | const char *error) { | |
423 | if(error) | |
424 | popup_protocol_error(0, error); | |
425 | } | |
426 | ||
460b9539 | 427 | static void clicked_icon(GtkButton attribute((unused)) *button, |
428 | gpointer userdata) { | |
429 | const struct icon *icon = userdata; | |
430 | ||
fb44b59e | 431 | if(!suppress_actions) |
53fa91bb | 432 | icon->action(client, icon_action_completed, 0); |
460b9539 | 433 | } |
434 | ||
ac6bf2ba RK |
435 | static void clicked_menu(GtkMenuItem attribute((unused)) *menuitem, |
436 | gpointer userdata) { | |
437 | const struct icon *icon = userdata; | |
438 | ||
fb44b59e | 439 | if(!suppress_actions) |
53fa91bb | 440 | icon->action(client, icon_action_completed, 0); |
ac6bf2ba RK |
441 | } |
442 | ||
2d504956 RK |
443 | static void toggled_menu(GtkCheckMenuItem *menuitem, |
444 | gpointer userdata) { | |
445 | const struct icon *icon = userdata; | |
446 | size_t n; | |
447 | ||
fb44b59e RK |
448 | if(suppress_actions) |
449 | return; | |
2d504956 RK |
450 | /* This is a bit fiddlier than the others, we need to find the action for the |
451 | * new state. If the new state is active then we want the ICON_INACTIVE | |
452 | * version and vica versa. */ | |
453 | for(n = 0; n < NICONS; ++n) | |
454 | if(icons[n].item == icon->item | |
455 | && !!(icons[n].flags & ICON_INACTIVE) == !!menuitem->active) | |
456 | break; | |
457 | if(n < NICONS) | |
53fa91bb | 458 | icons[n].action(client, icon_action_completed, 0); |
2d504956 RK |
459 | } |
460 | ||
699517af RK |
461 | /** @brief Called when a volume command completes */ |
462 | static void volume_completed(void attribute((unused)) *v, | |
463 | const char *error, | |
464 | int attribute((unused)) l, | |
465 | int attribute((unused)) r) { | |
466 | if(error) | |
467 | popup_protocol_error(0, error); | |
468 | /* We don't set the UI's notion of the volume here, it is set from the log | |
469 | * regardless of the reason it changed */ | |
470 | } | |
471 | ||
219dc95c | 472 | /** @brief Called when the volume has been adjusted */ |
460b9539 | 473 | static void volume_adjusted(GtkAdjustment attribute((unused)) *a, |
474 | gpointer attribute((unused)) user_data) { | |
475 | double v = gtk_adjustment_get_value(volume_adj) / goesupto; | |
476 | double b = gtk_adjustment_get_value(balance_adj); | |
477 | ||
fb44b59e | 478 | if(suppress_actions) |
460b9539 | 479 | /* This is the result of an update from the server, not a change from the |
480 | * user. Don't feedback! */ | |
481 | return; | |
482 | D(("volume_adjusted")); | |
483 | /* force to 'stereotypical' values */ | |
484 | v = nearbyint(100 * v) / 100; | |
485 | b = nearbyint(5 * b) / 5; | |
486 | /* Set the volume. We don't want a reply, we'll get the actual new volume | |
487 | * from the log. */ | |
321f7536 RK |
488 | if(rtp_supported) { |
489 | int l = nearbyint(left(v, b) * 100), r = nearbyint(right(v, b) * 100); | |
3c499fe7 | 490 | mixer_control(DEFAULT_BACKEND, &l, &r, 1); |
321f7536 | 491 | } else |
699517af | 492 | disorder_eclient_volume(client, volume_completed, |
321f7536 RK |
493 | nearbyint(left(v, b) * 100), |
494 | nearbyint(right(v, b) * 100), | |
495 | 0); | |
460b9539 | 496 | } |
497 | ||
219dc95c | 498 | /** @brief Formats the volume value */ |
460b9539 | 499 | static gchar *format_volume(GtkScale attribute((unused)) *scale, |
500 | gdouble value) { | |
501 | char s[32]; | |
502 | ||
503 | snprintf(s, sizeof s, "%.1f", (double)value); | |
2fc4475d | 504 | return g_strdup(s); |
460b9539 | 505 | } |
506 | ||
219dc95c | 507 | /** @brief Formats the balance value */ |
460b9539 | 508 | static gchar *format_balance(GtkScale attribute((unused)) *scale, |
509 | gdouble value) { | |
510 | char s[32]; | |
511 | ||
512 | if(fabs(value) < 0.1) | |
2fc4475d | 513 | return g_strdup("0"); |
460b9539 | 514 | snprintf(s, sizeof s, "%+.1f", (double)value); |
2fc4475d | 515 | return g_strdup(s); |
460b9539 | 516 | } |
517 | ||
518 | /* Volume mapping. We consider left, right, volume to be in [0,1] | |
519 | * and balance to be in [-1,1]. | |
520 | * | |
521 | * First, we just have volume = max(left, right). | |
522 | * | |
523 | * Balance we consider to linearly represent the amount by which the quieter | |
524 | * channel differs from the louder. In detail: | |
525 | * | |
526 | * if right > left then balance > 0: | |
527 | * balance = 0 => left = right (as an endpoint, not an instance) | |
528 | * balance = 1 => left = 0 | |
529 | * fitting to linear, left = right * (1 - balance) | |
530 | * so balance = 1 - left / right | |
531 | * (right > left => right > 0 so no division by 0.) | |
532 | * | |
533 | * if left > right then balance < 0: | |
534 | * balance = 0 => right = left (same caveat as above) | |
535 | * balance = -1 => right = 0 | |
536 | * again fitting to linear, right = left * (1 + balance) | |
537 | * so balance = right / left - 1 | |
538 | * (left > right => left > 0 so no division by 0.) | |
539 | * | |
540 | * if left = right then we just have balance = 0. | |
541 | * | |
542 | * Thanks to Clive and Andrew. | |
543 | */ | |
544 | ||
219dc95c | 545 | /** @brief Return the greater of @p x and @p y */ |
460b9539 | 546 | static double max(double x, double y) { |
547 | return x > y ? x : y; | |
548 | } | |
549 | ||
219dc95c | 550 | /** @brief Compute the left channel volume */ |
460b9539 | 551 | static double left(double v, double b) { |
552 | if(b > 0) /* volume = right */ | |
553 | return v * (1 - b); | |
554 | else /* volume = left */ | |
555 | return v; | |
556 | } | |
557 | ||
219dc95c | 558 | /** @brief Compute the right channel volume */ |
460b9539 | 559 | static double right(double v, double b) { |
560 | if(b > 0) /* volume = right */ | |
561 | return v; | |
562 | else /* volume = left */ | |
563 | return v * (1 + b); | |
564 | } | |
565 | ||
219dc95c | 566 | /** @brief Compute the overall volume */ |
460b9539 | 567 | static double volume(double l, double r) { |
568 | return max(l, r); | |
569 | } | |
570 | ||
219dc95c | 571 | /** @brief Compute the balance */ |
460b9539 | 572 | static double balance(double l, double r) { |
573 | if(l > r) | |
574 | return r / l - 1; | |
575 | else if(r > l) | |
576 | return 1 - l / r; | |
577 | else /* left = right */ | |
578 | return 0; | |
579 | } | |
580 | ||
a99c4e9a RK |
581 | /** @brief Called to enable RTP play |
582 | * | |
583 | * Rather odd signature is to fit in with the other icons which all call @ref | |
584 | * lib/eclient.h functions. | |
585 | */ | |
586 | static int enable_rtp(disorder_eclient attribute((unused)) *c, | |
587 | disorder_eclient_no_response attribute((unused)) *completed, | |
588 | void attribute((unused)) *v) { | |
589 | start_rtp(); | |
590 | return 0; | |
591 | } | |
592 | ||
593 | /** @brief Called to disable RTP play | |
594 | * | |
595 | * Rather odd signature is to fit in with the other icons which all call @ref | |
596 | * lib/eclient.h functions. | |
597 | */ | |
598 | static int disable_rtp(disorder_eclient attribute((unused)) *c, | |
599 | disorder_eclient_no_response attribute((unused)) *completed, | |
600 | void attribute((unused)) *v) { | |
601 | stop_rtp(); | |
602 | return 0; | |
603 | } | |
604 | ||
460b9539 | 605 | /* |
606 | Local Variables: | |
607 | c-basic-offset:2 | |
608 | comment-column:40 | |
609 | fill-column:79 | |
610 | indent-tabs-mode:nil | |
611 | End: | |
612 | */ |