2 * This file is part of DisOrder.
3 * Copyright (C) 2006, 2007 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"
26 /* Forward declarations ---------------------------------------------------- */
38 static void update_pause(const struct icon
*);
39 static void update_play(const struct icon
*);
40 static void update_scratch(const struct icon
*);
41 static void update_random_enable(const struct icon
*);
42 static void update_random_disable(const struct icon
*);
43 static void update_enable(const struct icon
*);
44 static void update_disable(const struct icon
*);
45 static void update_rtp(const struct icon
*);
46 static void update_nortp(const struct icon
*);
47 static void clicked_icon(GtkButton
*, gpointer
);
48 static void clicked_menu(GtkMenuItem
*, gpointer userdata
);
49 static void toggled_menu(GtkCheckMenuItem
*, gpointer userdata
);
51 static int enable_rtp(disorder_eclient
*c
,
52 disorder_eclient_no_response
*completed
,
54 static int disable_rtp(disorder_eclient
*c
,
55 disorder_eclient_no_response
*completed
,
58 static double left(double v
, double b
);
59 static double right(double v
, double b
);
60 static double volume(double l
, double r
);
61 static double balance(double l
, double r
);
63 static void volume_adjusted(GtkAdjustment
*a
, gpointer user_data
);
64 static gchar
*format_volume(GtkScale
*scale
, gdouble value
);
65 static gchar
*format_balance(GtkScale
*scale
, gdouble value
);
67 /* Control bar ------------------------------------------------------------- */
69 /** @brief Guard against feedback loop in volume control */
70 static int suppress_set_volume
;
72 /** @brief Definition of an icon
74 * The design here is rather mad: rather than changing the image displayed by
75 * icons according to their state, we flip the visibility of pairs of icons.
78 /** @brief Filename for image */
81 /** @brief Text for tooltip */
84 /** @brief Associated menu item or NULL */
87 /** @brief Called to update button when state may have changed */
88 void (*update
)(const struct icon
*i
);
90 /** @brief @ref eclient.h function to call */
91 int (*action
)(disorder_eclient
*c
,
92 disorder_eclient_no_response
*completed
,
98 /** @brief Pointer to button */
101 /** @brief Pointer to menu item */
105 /** @brief This is the active half of a pair */
106 #define ICON_ACTIVE 0x0001
108 /** @brief This is the inactive half of a pair */
109 #define ICON_INACTIVE 0x0002
111 /** @brief Table of all icons */
112 static struct icon icons
[] = {
114 "pause.png", /* icon */
115 "Pause playing track", /* tip */
116 "<GdisorderMain>/Control/Playing", /* menuitem */
117 update_pause
, /* update */
118 disorder_eclient_pause
, /* action */
119 ICON_ACTIVE
, /* flags */
124 "play.png", /* icon */
125 "Resume playing track", /* tip */
126 "<GdisorderMain>/Control/Playing", /* menuitem */
127 update_play
, /* update */
128 disorder_eclient_resume
, /* action */
129 ICON_INACTIVE
, /* flags */
134 "cross.png", /* icon */
135 "Cancel playing track", /* tip */
136 "<GdisorderMain>/Control/Scratch", /* menuitem */
137 update_scratch
, /* update */
138 disorder_eclient_scratch_playing
, /* action */
144 "random.png", /* icon */
145 "Enable random play", /* tip */
146 "<GdisorderMain>/Control/Random play", /* menuitem */
147 update_random_enable
, /* update */
148 disorder_eclient_random_enable
, /* action */
149 ICON_INACTIVE
, /* flags */
154 "randomcross.png", /* icon */
155 "Disable random play", /* tip */
156 "<GdisorderMain>/Control/Random play", /* menuitem */
157 update_random_disable
, /* update */
158 disorder_eclient_random_disable
, /* action */
159 ICON_ACTIVE
, /* flags */
164 "notes.png", /* icon */
165 "Enable play", /* tip */
167 update_enable
, /* update */
168 disorder_eclient_enable
, /* action */
169 ICON_INACTIVE
, /* flags */
174 "notescross.png", /* icon */
175 "Disable play", /* tip */
177 update_disable
, /* update */
178 disorder_eclient_disable
, /* action */
179 ICON_ACTIVE
, /* flags */
184 "speaker.png", /* icon */
185 "Play network stream", /* tip */
186 "<GdisorderMain>/Control/Network player", /* menuitem */
187 update_rtp
, /* update */
188 enable_rtp
, /* action */
189 ICON_INACTIVE
, /* flags */
194 "speakercross.png", /* icon */
195 "Stop playing network stream", /* tip */
196 "<GdisorderMain>/Control/Network player", /* menuitem */
197 update_nortp
, /* update */
198 disable_rtp
, /* action */
199 ICON_ACTIVE
, /* flags */
205 /** @brief Count of icons */
206 #define NICONS (int)(sizeof icons / sizeof *icons)
208 static GtkAdjustment
*volume_adj
;
209 static GtkAdjustment
*balance_adj
;
211 /** @brief Called whenever last_state changes in any way */
212 void control_monitor(void attribute((unused
)) *u
) {
215 D(("control_monitor"));
216 for(n
= 0; n
< NICONS
; ++n
)
217 icons
[n
].update(&icons
[n
]);
220 /** @brief Create the control bar */
221 GtkWidget
*control_widget(void) {
222 GtkWidget
*hbox
= gtk_hbox_new(FALSE
, 1), *vbox
;
229 D(("control_widget"));
230 assert(mainmenufactory
); /* ordering must be right */
231 for(n
= 0; n
< NICONS
; ++n
) {
233 icons
[n
].button
= gtk_button_new();
234 gtk_widget_modify_bg(icons
[n
].button
, GTK_STATE_NORMAL
, &tool_bg
);
235 gtk_widget_modify_bg(icons
[n
].button
, GTK_STATE_ACTIVE
, &tool_active
);
236 gtk_widget_modify_bg(icons
[n
].button
, GTK_STATE_PRELIGHT
, &tool_active
);
237 gtk_widget_modify_bg(icons
[n
].button
, GTK_STATE_SELECTED
, &tool_active
);
238 gtk_widget_modify_bg(icons
[n
].button
, GTK_STATE_INSENSITIVE
, &tool_active
);
239 if((pb
= find_image(icons
[n
].icon
))) {
241 content
= gtk_image_new_from_pixbuf(pb
);
244 content
= gtk_label_new(icons
[n
].icon
);
246 gtk_container_add(GTK_CONTAINER(icons
[n
].button
), content
);
247 gtk_tooltips_set_tip(tips
, icons
[n
].button
, icons
[n
].tip
, "");
248 g_signal_connect(G_OBJECT(icons
[n
].button
), "clicked",
249 G_CALLBACK(clicked_icon
), &icons
[n
]);
250 /* pop the icon in a vbox so it doesn't get vertically stretch if there are
251 * taller things in the control bar */
253 vbox
= gtk_vbox_new(FALSE
, 0);
254 gtk_box_pack_start(GTK_BOX(vbox
), icons
[n
].button
, TRUE
, FALSE
, 0);
255 gtk_box_pack_start(GTK_BOX(hbox
), vbox
, FALSE
, FALSE
, 0);
256 if(icons
[n
].menuitem
) {
257 icons
[n
].item
= gtk_item_factory_get_widget(mainmenufactory
,
259 switch(icons
[n
].flags
& (ICON_ACTIVE
|ICON_INACTIVE
)) {
261 g_signal_connect(G_OBJECT(icons
[n
].item
), "toggled",
262 G_CALLBACK(toggled_menu
), &icons
[n
]);
265 /* Don't connect two instances of the signal! */
268 g_signal_connect(G_OBJECT(icons
[n
].item
), "activate",
269 G_CALLBACK(clicked_menu
), &icons
[n
]);
274 /* create the adjustments for the volume control */
276 volume_adj
= GTK_ADJUSTMENT(gtk_adjustment_new(0, 0, goesupto
,
277 goesupto
/ 20, goesupto
/ 20,
280 balance_adj
= GTK_ADJUSTMENT(gtk_adjustment_new(0, -1, 1,
282 /* the volume control */
284 v
= gtk_hscale_new(volume_adj
);
286 b
= gtk_hscale_new(balance_adj
);
287 set_slider_colors(v
);
288 set_slider_colors(b
);
289 gtk_scale_set_digits(GTK_SCALE(v
), 10);
290 gtk_scale_set_digits(GTK_SCALE(b
), 10);
291 gtk_widget_set_size_request(v
, 192, -1);
292 gtk_widget_set_size_request(b
, 192, -1);
293 gtk_tooltips_set_tip(tips
, v
, "Volume", "");
294 gtk_tooltips_set_tip(tips
, b
, "Balance", "");
295 gtk_box_pack_start(GTK_BOX(hbox
), v
, FALSE
, TRUE
, 0);
296 gtk_box_pack_start(GTK_BOX(hbox
), b
, FALSE
, TRUE
, 0);
297 /* space updates rather than hammering the server */
298 gtk_range_set_update_policy(GTK_RANGE(v
), GTK_UPDATE_DELAYED
);
299 gtk_range_set_update_policy(GTK_RANGE(b
), GTK_UPDATE_DELAYED
);
300 /* notice when the adjustments are changed */
301 g_signal_connect(G_OBJECT(volume_adj
), "value-changed",
302 G_CALLBACK(volume_adjusted
), 0);
303 g_signal_connect(G_OBJECT(balance_adj
), "value-changed",
304 G_CALLBACK(volume_adjusted
), 0);
305 /* format the volume/balance values ourselves */
306 g_signal_connect(G_OBJECT(v
), "format-value",
307 G_CALLBACK(format_volume
), 0);
308 g_signal_connect(G_OBJECT(b
), "format-value",
309 G_CALLBACK(format_balance
), 0);
310 register_monitor(control_monitor
, 0, -1UL);
314 /** @brief Update the volume control when it changes */
315 void volume_update(void) {
318 D(("volume_update"));
319 l
= volume_l
/ 100.0;
320 r
= volume_r
/ 100.0;
321 ++suppress_set_volume
;
322 gtk_adjustment_set_value(volume_adj
, volume(l
, r
) * goesupto
);
323 gtk_adjustment_set_value(balance_adj
, balance(l
, r
));
324 --suppress_set_volume
;
327 /** @brief Update the state of one of the control icons
328 * @param icon Target icon
329 * @param visible True if this version of the button should be visible
330 * @param usable True if the button is currently usable
332 * Several of the icons, rather bizarrely, come in pairs: for instance exactly
333 * one of the play and pause buttons is supposed to be visible at any given
336 * @p usable need not take into account server availability, that is done
339 static void update_icon(const struct icon
*icon
,
340 int visible
, int usable
) {
341 /* If the connection is down nothing is ever usable */
342 if(!(last_state
& DISORDER_CONNECTED
))
344 (visible ? gtk_widget_show
: gtk_widget_hide
)(icon
->button
);
345 /* Only both updating usability if the button is visible */
347 gtk_widget_set_sensitive(icon
->button
, usable
);
349 /* There's an associated menu item. These are always visible, but may not
351 if((icon
->flags
& (ICON_ACTIVE
|ICON_INACTIVE
)) == ICON_ACTIVE
) {
352 /* The active half of a pair */
353 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(icon
->item
), visible
);
355 gtk_widget_set_sensitive(icon
->item
, usable
);
359 static void update_pause(const struct icon
*icon
) {
360 const int visible
= !(last_state
& DISORDER_TRACK_PAUSED
);
361 const int usable
= !!(last_state
& DISORDER_PLAYING
); /* TODO: might be a lie */
362 update_icon(icon
, visible
, usable
);
365 static void update_play(const struct icon
*icon
) {
366 const int visible
= !!(last_state
& DISORDER_TRACK_PAUSED
);
367 const int usable
= !!(last_state
& DISORDER_PLAYING
);
368 update_icon(icon
, visible
, usable
);
371 static void update_scratch(const struct icon
*icon
) {
372 const int visible
= 1;
373 const int usable
= !!(last_state
& DISORDER_PLAYING
);
374 update_icon(icon
, visible
, usable
);
377 static void update_random_enable(const struct icon
*icon
) {
378 const int visible
= !(last_state
& DISORDER_RANDOM_ENABLED
);
379 const int usable
= 1;
380 update_icon(icon
, visible
, usable
);
383 static void update_random_disable(const struct icon
*icon
) {
384 const int visible
= !!(last_state
& DISORDER_RANDOM_ENABLED
);
385 const int usable
= 1;
386 update_icon(icon
, visible
, usable
);
389 static void update_enable(const struct icon
*icon
) {
390 const int visible
= !(last_state
& DISORDER_PLAYING_ENABLED
);
391 const int usable
= 1;
392 update_icon(icon
, visible
, usable
);
395 static void update_disable(const struct icon
*icon
) {
396 const int visible
= !!(last_state
& DISORDER_PLAYING_ENABLED
);
397 const int usable
= 1;
398 update_icon(icon
, visible
, usable
);
401 static void update_rtp(const struct icon
*icon
) {
402 const int visible
= !rtp_is_running
;
403 const int usable
= rtp_supported
;
404 update_icon(icon
, visible
, usable
);
407 static void update_nortp(const struct icon
*icon
) {
408 const int visible
= rtp_is_running
;
409 const int usable
= rtp_supported
;
410 update_icon(icon
, visible
, usable
);
413 static void clicked_icon(GtkButton
attribute((unused
)) *button
,
415 const struct icon
*icon
= userdata
;
417 icon
->action(client
, 0, 0);
420 static void clicked_menu(GtkMenuItem
attribute((unused
)) *menuitem
,
422 const struct icon
*icon
= userdata
;
424 icon
->action(client
, 0, 0);
427 static void toggled_menu(GtkCheckMenuItem
*menuitem
,
429 const struct icon
*icon
= userdata
;
432 /* This is a bit fiddlier than the others, we need to find the action for the
433 * new state. If the new state is active then we want the ICON_INACTIVE
434 * version and vica versa. */
435 for(n
= 0; n
< NICONS
; ++n
)
436 if(icons
[n
].item
== icon
->item
437 && !!(icons
[n
].flags
& ICON_INACTIVE
) == !!menuitem
->active
)
440 icons
[n
].action(client
, 0, 0);
443 /** @brief Called when the volume has been adjusted */
444 static void volume_adjusted(GtkAdjustment
attribute((unused
)) *a
,
445 gpointer
attribute((unused
)) user_data
) {
446 double v
= gtk_adjustment_get_value(volume_adj
) / goesupto
;
447 double b
= gtk_adjustment_get_value(balance_adj
);
449 if(suppress_set_volume
)
450 /* This is the result of an update from the server, not a change from the
451 * user. Don't feedback! */
453 D(("volume_adjusted"));
454 /* force to 'stereotypical' values */
455 v
= nearbyint(100 * v
) / 100;
456 b
= nearbyint(5 * b
) / 5;
457 /* Set the volume. We don't want a reply, we'll get the actual new volume
459 disorder_eclient_volume(client
, 0,
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
) {