2 * This file is part of DisOrder.
3 * Copyright (C) 2006-2008 Richard Kettlewell
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.
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.
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
20 /** @file disobedience/control.c
21 * @brief Volume control and buttons
24 #include "disobedience.h"
27 /* Forward declarations ---------------------------------------------------- */
39 static void clicked_icon(GtkButton
*, gpointer
);
40 static void clicked_menu(GtkMenuItem
*, gpointer userdata
);
41 static void toggled_menu(GtkCheckMenuItem
*, gpointer userdata
);
43 static int enable_rtp(disorder_eclient
*c
,
44 disorder_eclient_no_response
*completed
,
46 static int disable_rtp(disorder_eclient
*c
,
47 disorder_eclient_no_response
*completed
,
50 static double left(double v
, double b
);
51 static double right(double v
, double b
);
52 static double volume(double l
, double r
);
53 static double balance(double l
, double r
);
55 static void volume_adjusted(GtkAdjustment
*a
, gpointer user_data
);
56 static gchar
*format_volume(GtkScale
*scale
, gdouble value
);
57 static gchar
*format_balance(GtkScale
*scale
, gdouble value
);
59 static void icon_changed(const char *event
,
62 static void volume_changed(const char *event
,
66 /* Control bar ------------------------------------------------------------- */
68 /** @brief Guard against feedback */
69 int suppress_actions
= 1;
71 /** @brief Definition of an icon
73 * We have two kinds of icon:
74 * - action icons, which just do something but don't have a state as such
75 * - toggle icons, which toggle between two states ("on" and "off").
77 * The scratch button is an action icon; currently all the others are toggle
80 * (All icons can be sensitive or insensitive, separately to the above.)
83 /** @brief Filename for 'on' image */
86 /** @brief Text for 'on' tooltip */
89 /** @brief Filename for 'off' image or NULL for an action icon */
92 /** @brief Text for 'off tooltip */
95 /** @brief Associated menu item or NULL */
98 /** @brief Events that change this icon, separated by spaces */
101 /** @brief @ref eclient.h function to call to go from off to on
103 * For action buttons, this should be NULL.
105 int (*action_go_on
)(disorder_eclient
*c
,
106 disorder_eclient_no_response
*completed
,
109 /** @brief @ref eclient.h function to call to go from on to off
111 * For action buttons, this action is used.
113 int (*action_go_off
)(disorder_eclient
*c
,
114 disorder_eclient_no_response
*completed
,
117 /** @brief Get button state
118 * @return 1 for on, 0 for off
122 /** @brief Get button sensitivity
123 * @return 1 for sensitive, 0 for insensitive
125 * Can be NULL for always sensitive.
127 int (*sensitive
)(void);
129 /** @brief Pointer to button */
132 /** @brief Pointer to menu item */
136 GtkWidget
*image_off
;
139 /* TODO: Add rights into the mix below */
141 static int pause_resume_on(void) {
142 return !(last_state
& DISORDER_TRACK_PAUSED
);
145 static int pause_resume_sensitive(void) {
146 return !!(last_state
& DISORDER_PLAYING
)
147 && (last_rights
& RIGHT_PAUSE
);
150 static int scratch_sensitive(void) {
151 return !!(last_state
& DISORDER_PLAYING
)
152 && right_scratchable(last_rights
, config
->username
, playing_track
);
155 static int random_sensitive(void) {
156 return !!(last_rights
& RIGHT_GLOBAL_PREFS
);
159 static int random_enabled(void) {
160 return !!(last_state
& DISORDER_RANDOM_ENABLED
);
163 static int playing_sensitive(void) {
164 return !!(last_rights
& RIGHT_GLOBAL_PREFS
);
167 static int playing_enabled(void) {
168 return !!(last_state
& DISORDER_PLAYING_ENABLED
);
171 static int rtp_enabled(void) {
172 return rtp_is_running
;
175 static int rtp_sensitive(void) {
176 return rtp_supported
;
179 /** @brief Table of all icons */
180 static struct icon icons
[] = {
182 icon_on
: "pause.png",
183 tip_on
: "Pause playing track",
184 icon_off
: "play.png",
185 tip_off
: "Resume playing track",
186 menuitem
: "<GdisorderMain>/Control/Playing",
188 sensitive
: pause_resume_sensitive
,
189 action_go_on
: disorder_eclient_resume
,
190 action_go_off
: disorder_eclient_pause
,
191 events
: "pause-changed playing-changed rights-changed",
194 icon_on
: "cross.png",
195 tip_on
: "Cancel playing track",
196 menuitem
: "<GdisorderMain>/Control/Scratch",
197 sensitive
: scratch_sensitive
,
198 action_go_off
: disorder_eclient_scratch_playing
,
199 events
: "playing-track-changed rights-changed",
202 icon_on
: "randomcross.png",
203 tip_on
: "Disable random play",
204 icon_off
: "random.png",
205 tip_off
: "Enable random play",
206 menuitem
: "<GdisorderMain>/Control/Random play",
208 sensitive
: random_sensitive
,
209 action_go_on
: disorder_eclient_random_enable
,
210 action_go_off
: disorder_eclient_random_disable
,
211 events
: "random-changed rights-changed",
214 icon_on
: "notescross.png",
215 tip_on
: "Disable play",
216 icon_off
: "notes.png",
217 tip_off
: "Enable play",
219 sensitive
: playing_sensitive
,
220 action_go_on
: disorder_eclient_enable
,
221 action_go_off
: disorder_eclient_disable
,
222 events
: "enabled-changed rights-changed",
225 icon_on
: "speakercross.png",
226 tip_on
: "Stop playing network stream",
227 icon_off
: "speaker.png",
228 tip_off
: "Play network stream",
229 menuitem
: "<GdisorderMain>/Control/Network player",
231 sensitive
: rtp_sensitive
,
232 action_go_on
: enable_rtp
,
233 action_go_off
: disable_rtp
,
234 events
: "rtp-changed",
238 /** @brief Count of icons */
239 #define NICONS (int)(sizeof icons / sizeof *icons)
241 static GtkAdjustment
*volume_adj
;
242 static GtkAdjustment
*balance_adj
;
243 static GtkWidget
*volume_widget
;
244 static GtkWidget
*balance_widget
;
246 /** @brief Create the control bar */
247 GtkWidget
*control_widget(void) {
248 GtkWidget
*hbox
= gtk_hbox_new(FALSE
, 1), *vbox
;
252 D(("control_widget"));
253 assert(mainmenufactory
); /* ordering must be right */
254 for(n
= 0; n
< NICONS
; ++n
) {
255 /* Create the button */
257 icons
[n
].button
= gtk_button_new();
258 gtk_widget_set_style(icons
[n
].button
, tool_style
);
259 icons
[n
].image_on
= gtk_image_new_from_pixbuf(find_image(icons
[n
].icon_on
));
260 gtk_widget_set_style(icons
[n
].image_on
, tool_style
);
261 g_object_ref(icons
[n
].image_on
);
262 /* If it's a toggle icon, create the 'off' half too */
263 if(icons
[n
].icon_off
) {
264 icons
[n
].image_off
= gtk_image_new_from_pixbuf(find_image(icons
[n
].icon_off
));
265 gtk_widget_set_style(icons
[n
].image_off
, tool_style
);
266 g_object_ref(icons
[n
].image_off
);
268 g_signal_connect(G_OBJECT(icons
[n
].button
), "clicked",
269 G_CALLBACK(clicked_icon
), &icons
[n
]);
270 /* pop the icon in a vbox so it doesn't get vertically stretch if there are
271 * taller things in the control bar */
273 vbox
= gtk_vbox_new(FALSE
, 0);
274 gtk_box_pack_start(GTK_BOX(vbox
), icons
[n
].button
, TRUE
, FALSE
, 0);
275 gtk_box_pack_start(GTK_BOX(hbox
), vbox
, FALSE
, FALSE
, 0);
276 if(icons
[n
].menuitem
) {
277 /* Find the menu item */
278 icons
[n
].item
= gtk_item_factory_get_widget(mainmenufactory
,
280 if(icons
[n
].icon_off
)
281 g_signal_connect(G_OBJECT(icons
[n
].item
), "toggled",
282 G_CALLBACK(toggled_menu
), &icons
[n
]);
284 g_signal_connect(G_OBJECT(icons
[n
].item
), "activate",
285 G_CALLBACK(clicked_menu
), &icons
[n
]);
287 /* Make sure the icon is updated when relevant things changed */
288 char **events
= split(icons
[n
].events
, 0, 0, 0, 0);
290 event_register(*events
++, icon_changed
, &icons
[n
]);
292 /* create the adjustments for the volume control */
294 volume_adj
= GTK_ADJUSTMENT(gtk_adjustment_new(0, 0, goesupto
,
295 goesupto
/ 20, goesupto
/ 20,
298 balance_adj
= GTK_ADJUSTMENT(gtk_adjustment_new(0, -1, 1,
300 /* the volume control */
302 volume_widget
= gtk_hscale_new(volume_adj
);
304 balance_widget
= gtk_hscale_new(balance_adj
);
305 gtk_widget_set_style(volume_widget
, tool_style
);
306 gtk_widget_set_style(balance_widget
, tool_style
);
307 gtk_scale_set_digits(GTK_SCALE(volume_widget
), 10);
308 gtk_scale_set_digits(GTK_SCALE(balance_widget
), 10);
309 gtk_widget_set_size_request(volume_widget
, 192, -1);
310 gtk_widget_set_size_request(balance_widget
, 192, -1);
311 gtk_tooltips_set_tip(tips
, volume_widget
, "Volume", "");
312 gtk_tooltips_set_tip(tips
, balance_widget
, "Balance", "");
313 gtk_box_pack_start(GTK_BOX(hbox
), volume_widget
, FALSE
, TRUE
, 0);
314 gtk_box_pack_start(GTK_BOX(hbox
), balance_widget
, FALSE
, TRUE
, 0);
315 /* space updates rather than hammering the server */
316 gtk_range_set_update_policy(GTK_RANGE(volume_widget
), GTK_UPDATE_DELAYED
);
317 gtk_range_set_update_policy(GTK_RANGE(balance_widget
), GTK_UPDATE_DELAYED
);
318 /* notice when the adjustments are changed */
319 g_signal_connect(G_OBJECT(volume_adj
), "value-changed",
320 G_CALLBACK(volume_adjusted
), 0);
321 g_signal_connect(G_OBJECT(balance_adj
), "value-changed",
322 G_CALLBACK(volume_adjusted
), 0);
323 /* format the volume/balance values ourselves */
324 g_signal_connect(G_OBJECT(volume_widget
), "format-value",
325 G_CALLBACK(format_volume
), 0);
326 g_signal_connect(G_OBJECT(balance_widget
), "format-value",
327 G_CALLBACK(format_balance
), 0);
328 event_register("volume-changed", volume_changed
, 0);
329 event_register("rtp-changed", volume_changed
, 0);
333 /** @brief Update the volume control when it changes */
334 static void volume_changed(const char attribute((unused
)) *event
,
335 void attribute((unused
)) *eventdata
,
336 void attribute((unused
)) *callbackdata
) {
338 gboolean volume_supported
;
340 D(("volume_changed"));
342 /* Only display volume/balance controls if they will work */
344 || (rtp_supported
&& mixer_supported(DEFAULT_BACKEND
)))
345 volume_supported
= TRUE
;
347 volume_supported
= FALSE
;
348 /* TODO: if the server doesn't know how to set the volume [but isn't using
349 * network play] then we should have volume_supported = FALSE */
350 if(volume_supported
) {
351 gtk_widget_show(volume_widget
);
352 gtk_widget_show(balance_widget
);
353 l
= volume_l
/ 100.0;
354 r
= volume_r
/ 100.0;
355 gtk_adjustment_set_value(volume_adj
, volume(l
, r
) * goesupto
);
356 gtk_adjustment_set_value(balance_adj
, balance(l
, r
));
358 gtk_widget_hide(volume_widget
);
359 gtk_widget_hide(balance_widget
);
364 /** @brief Update the state of one of the control icons
366 static void icon_changed(const char attribute((unused
)) *event
,
367 void attribute((unused
)) *evendata
,
368 void *callbackdata
) {
369 const struct icon
*const icon
= callbackdata
;
370 int on
= icon
->on ? icon
->on() : 1;
371 int sensitive
= icon
->sensitive ? icon
->sensitive() : 1;
372 GtkWidget
*child
, *newchild
;
375 /* If the connection is down nothing is ever usable */
376 if(!(last_state
& DISORDER_CONNECTED
))
378 /* Replace the child */
379 newchild
= on ? icon
->image_on
: icon
->image_off
;
380 child
= gtk_bin_get_child(GTK_BIN(icon
->button
));
381 if(child
!= newchild
) {
383 gtk_container_remove(GTK_CONTAINER(icon
->button
), child
);
384 gtk_container_add(GTK_CONTAINER(icon
->button
), newchild
);
385 gtk_widget_show(newchild
);
388 gtk_tooltips_set_tip(tips
, icon
->button
,
389 on ? icon
->tip_on
: icon
->tip_off
, "");
390 gtk_widget_set_sensitive(icon
->button
, sensitive
);
391 /* Icons with an associated menu item */
394 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(icon
->item
), on
);
395 gtk_widget_set_sensitive(icon
->item
, sensitive
);
400 static void icon_action_completed(void attribute((unused
)) *v
,
403 popup_protocol_error(0, error
);
406 static void clicked_icon(GtkButton
attribute((unused
)) *button
,
408 const struct icon
*icon
= userdata
;
412 if(!icon
->on
|| icon
->on())
413 icon
->action_go_off(client
, icon_action_completed
, 0);
415 icon
->action_go_on(client
, icon_action_completed
, 0);
418 static void clicked_menu(GtkMenuItem
attribute((unused
)) *menuitem
,
420 clicked_icon(NULL
, userdata
);
423 static void toggled_menu(GtkCheckMenuItem
attribute((unused
)) *menuitem
,
425 clicked_icon(NULL
, userdata
);
428 /** @brief Called when a volume command completes */
429 static void volume_completed(void attribute((unused
)) *v
,
431 int attribute((unused
)) l
,
432 int attribute((unused
)) r
) {
434 popup_protocol_error(0, error
);
435 /* We don't set the UI's notion of the volume here, it is set from the log
436 * regardless of the reason it changed */
439 /** @brief Called when the volume has been adjusted */
440 static void volume_adjusted(GtkAdjustment
attribute((unused
)) *a
,
441 gpointer
attribute((unused
)) user_data
) {
442 double v
= gtk_adjustment_get_value(volume_adj
) / goesupto
;
443 double b
= gtk_adjustment_get_value(balance_adj
);
446 /* This is the result of an update from the server, not a change from the
447 * user. Don't feedback! */
449 D(("volume_adjusted"));
450 /* force to 'stereotypical' values */
451 v
= nearbyint(100 * v
) / 100;
452 b
= nearbyint(5 * b
) / 5;
453 /* Set the volume. We don't want a reply, we'll get the actual new volume
456 int l
= nearbyint(left(v
, b
) * 100), r
= nearbyint(right(v
, b
) * 100);
457 mixer_control(DEFAULT_BACKEND
, &l
, &r
, 1);
459 disorder_eclient_volume(client
, volume_completed
,
460 nearbyint(left(v
, b
) * 100),
461 nearbyint(right(v
, b
) * 100),
465 /** @brief Formats the volume value */
466 static gchar
*format_volume(GtkScale
attribute((unused
)) *scale
,
470 snprintf(s
, sizeof s
, "%.1f", (double)value
);
474 /** @brief Formats the balance value */
475 static gchar
*format_balance(GtkScale
attribute((unused
)) *scale
,
479 if(fabs(value
) < 0.1)
480 return g_strdup("0");
481 snprintf(s
, sizeof s
, "%+.1f", (double)value
);
485 /* Volume mapping. We consider left, right, volume to be in [0,1]
486 * and balance to be in [-1,1].
488 * First, we just have volume = max(left, right).
490 * Balance we consider to linearly represent the amount by which the quieter
491 * channel differs from the louder. In detail:
493 * if right > left then balance > 0:
494 * balance = 0 => left = right (as an endpoint, not an instance)
495 * balance = 1 => left = 0
496 * fitting to linear, left = right * (1 - balance)
497 * so balance = 1 - left / right
498 * (right > left => right > 0 so no division by 0.)
500 * if left > right then balance < 0:
501 * balance = 0 => right = left (same caveat as above)
502 * balance = -1 => right = 0
503 * again fitting to linear, right = left * (1 + balance)
504 * so balance = right / left - 1
505 * (left > right => left > 0 so no division by 0.)
507 * if left = right then we just have balance = 0.
509 * Thanks to Clive and Andrew.
512 /** @brief Return the greater of @p x and @p y */
513 static double max(double x
, double y
) {
514 return x
> y ? x
: y
;
517 /** @brief Compute the left channel volume */
518 static double left(double v
, double b
) {
519 if(b
> 0) /* volume = right */
521 else /* volume = left */
525 /** @brief Compute the right channel volume */
526 static double right(double v
, double b
) {
527 if(b
> 0) /* volume = right */
529 else /* volume = left */
533 /** @brief Compute the overall volume */
534 static double volume(double l
, double r
) {
538 /** @brief Compute the balance */
539 static double balance(double l
, double r
) {
544 else /* left = right */
548 /** @brief Called to enable RTP play
550 * Rather odd signature is to fit in with the other icons which all call @ref
551 * lib/eclient.h functions.
553 static int enable_rtp(disorder_eclient
attribute((unused
)) *c
,
554 disorder_eclient_no_response
attribute((unused
)) *completed
,
555 void attribute((unused
)) *v
) {
560 /** @brief Called to disable RTP play
562 * Rather odd signature is to fit in with the other icons which all call @ref
563 * lib/eclient.h functions.
565 static int disable_rtp(disorder_eclient
attribute((unused
)) *c
,
566 disorder_eclient_no_response
attribute((unused
)) *completed
,
567 void attribute((unused
)) *v
) {