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