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
);
50 static int enable_rtp(disorder_eclient
*c
,
51 disorder_eclient_no_response
*completed
,
53 static int disable_rtp(disorder_eclient
*c
,
54 disorder_eclient_no_response
*completed
,
57 static double left(double v
, double b
);
58 static double right(double v
, double b
);
59 static double volume(double l
, double r
);
60 static double balance(double l
, double r
);
62 static void volume_adjusted(GtkAdjustment
*a
, gpointer user_data
);
63 static gchar
*format_volume(GtkScale
*scale
, gdouble value
);
64 static gchar
*format_balance(GtkScale
*scale
, gdouble value
);
66 /* Control bar ------------------------------------------------------------- */
68 /** @brief Guard against feedback loop in volume control */
69 static int suppress_set_volume
;
71 /** @brief Definition of an icon
73 * The design here is rather mad: rather than changing the image displayed by
74 * icons according to their state, we flip the visibility of pairs of icons.
77 /** @brief Filename for image */
80 /** @brief Text for tooltip */
83 /** @brief Associated menu item or NULL */
86 /** @brief Called to update button when state may have changed */
87 void (*update
)(const struct icon
*i
);
89 /** @brief @ref eclient.h function to call */
90 int (*action
)(disorder_eclient
*c
,
91 disorder_eclient_no_response
*completed
,
94 /** @brief Pointer to button */
97 /** @brief Pointer to menu item */
101 /** @brief Table of all icons */
102 static struct icon icons
[] = {
103 { "pause.png", "Pause playing track", 0,
105 disorder_eclient_pause
, 0, 0 },
106 { "play.png", "Resume playing track", 0,
108 disorder_eclient_resume
, 0, 0 },
109 { "cross.png", "Cancel playing track", "<GdisorderMain>/Control/Scratch",
111 disorder_eclient_scratch_playing
, 0, 0 },
112 { "random.png", "Enable random play", 0,
113 update_random_enable
,
114 disorder_eclient_random_enable
, 0, 0 },
115 { "randomcross.png", "Disable random play", 0,
116 update_random_disable
,
117 disorder_eclient_random_disable
, 0, 0 },
118 { "notes.png", "Enable play", 0,
120 disorder_eclient_enable
, 0, 0 },
121 { "notescross.png", "Disable play", 0,
123 disorder_eclient_disable
, 0, 0 },
124 { "speaker.png", "Play network stream", 0,
127 { "speakercross.png", "Stop playing network stream", 0,
132 /** @brief Count of icons */
133 #define NICONS (int)(sizeof icons / sizeof *icons)
135 static GtkAdjustment
*volume_adj
;
136 static GtkAdjustment
*balance_adj
;
138 /** @brief Called whenever last_state changes in any way */
139 void control_monitor(void attribute((unused
)) *u
) {
142 D(("control_monitor"));
143 for(n
= 0; n
< NICONS
; ++n
)
144 icons
[n
].update(&icons
[n
]);
147 /** @brief Create the control bar */
148 GtkWidget
*control_widget(void) {
149 GtkWidget
*hbox
= gtk_hbox_new(FALSE
, 1), *vbox
;
156 D(("control_widget"));
157 assert(mainmenufactory
); /* ordering must be right */
158 for(n
= 0; n
< NICONS
; ++n
) {
160 icons
[n
].button
= gtk_button_new();
161 if((pb
= find_image(icons
[n
].icon
))) {
163 content
= gtk_image_new_from_pixbuf(pb
);
166 content
= gtk_label_new(icons
[n
].icon
);
168 gtk_container_add(GTK_CONTAINER(icons
[n
].button
), content
);
169 gtk_tooltips_set_tip(tips
, icons
[n
].button
, icons
[n
].tip
, "");
170 g_signal_connect(G_OBJECT(icons
[n
].button
), "clicked",
171 G_CALLBACK(clicked_icon
), &icons
[n
]);
172 /* pop the icon in a vbox so it doesn't get vertically stretch if there are
173 * taller things in the control bar */
175 vbox
= gtk_vbox_new(FALSE
, 0);
176 gtk_box_pack_start(GTK_BOX(vbox
), icons
[n
].button
, TRUE
, FALSE
, 0);
177 gtk_box_pack_start(GTK_BOX(hbox
), vbox
, FALSE
, FALSE
, 0);
178 if(icons
[n
].menuitem
) {
179 icons
[n
].item
= gtk_item_factory_get_widget(mainmenufactory
,
181 g_signal_connect(G_OBJECT(icons
[n
].item
), "activate",
182 G_CALLBACK(clicked_menu
), &icons
[n
]);
185 /* create the adjustments for the volume control */
187 volume_adj
= GTK_ADJUSTMENT(gtk_adjustment_new(0, 0, goesupto
,
188 goesupto
/ 20, goesupto
/ 20,
191 balance_adj
= GTK_ADJUSTMENT(gtk_adjustment_new(0, -1, 1,
193 /* the volume control */
195 v
= gtk_hscale_new(volume_adj
);
197 b
= gtk_hscale_new(balance_adj
);
198 gtk_scale_set_digits(GTK_SCALE(v
), 10);
199 gtk_scale_set_digits(GTK_SCALE(b
), 10);
200 gtk_widget_set_size_request(v
, 192, -1);
201 gtk_widget_set_size_request(b
, 192, -1);
202 gtk_tooltips_set_tip(tips
, v
, "Volume", "");
203 gtk_tooltips_set_tip(tips
, b
, "Balance", "");
204 gtk_box_pack_start(GTK_BOX(hbox
), v
, FALSE
, TRUE
, 0);
205 gtk_box_pack_start(GTK_BOX(hbox
), b
, FALSE
, TRUE
, 0);
206 /* space updates rather than hammering the server */
207 gtk_range_set_update_policy(GTK_RANGE(v
), GTK_UPDATE_DELAYED
);
208 gtk_range_set_update_policy(GTK_RANGE(b
), GTK_UPDATE_DELAYED
);
209 /* notice when the adjustments are changed */
210 g_signal_connect(G_OBJECT(volume_adj
), "value-changed",
211 G_CALLBACK(volume_adjusted
), 0);
212 g_signal_connect(G_OBJECT(balance_adj
), "value-changed",
213 G_CALLBACK(volume_adjusted
), 0);
214 /* format the volume/balance values ourselves */
215 g_signal_connect(G_OBJECT(v
), "format-value",
216 G_CALLBACK(format_volume
), 0);
217 g_signal_connect(G_OBJECT(b
), "format-value",
218 G_CALLBACK(format_balance
), 0);
219 register_monitor(control_monitor
, 0, -1UL);
223 /** @brief Update the volume control when it changes */
224 void volume_update(void) {
227 D(("volume_update"));
228 l
= volume_l
/ 100.0;
229 r
= volume_r
/ 100.0;
230 ++suppress_set_volume
;
231 gtk_adjustment_set_value(volume_adj
, volume(l
, r
) * goesupto
);
232 gtk_adjustment_set_value(balance_adj
, balance(l
, r
));
233 --suppress_set_volume
;
236 /** @brief Update the state of one of the control icons
237 * @param icon Target icon
238 * @param visible True if this version of the button should be visible
239 * @param usable True if the button is currently usable
241 * Several of the icons, rather bizarrely, come in pairs: for instance exactly
242 * one of the play and pause buttons is supposed to be visible at any given
245 * @p usable need not take into account server availability, that is done
248 static void update_icon(const struct icon
*icon
,
249 int visible
, int usable
) {
250 /* If the connection is down nothing is ever usable */
251 if(!(last_state
& DISORDER_CONNECTED
))
253 (visible ? gtk_widget_show
: gtk_widget_hide
)(icon
->button
);
254 /* Only both updating usability if the button is visible */
256 gtk_widget_set_sensitive(icon
->button
, usable
);
258 /* There's an associated menu item */
259 (visible ? gtk_widget_show
: gtk_widget_hide
)(icon
->item
);
261 gtk_widget_set_sensitive(icon
->item
, usable
);
265 static void update_pause(const struct icon
*icon
) {
266 const int visible
= !(last_state
& DISORDER_TRACK_PAUSED
);
267 const int usable
= !!(last_state
& DISORDER_PLAYING
); /* TODO: might be a lie */
268 update_icon(icon
, visible
, usable
);
271 static void update_play(const struct icon
*icon
) {
272 const int visible
= !!(last_state
& DISORDER_TRACK_PAUSED
);
273 const int usable
= !!(last_state
& DISORDER_PLAYING
);
274 update_icon(icon
, visible
, usable
);
277 static void update_scratch(const struct icon
*icon
) {
278 const int visible
= 1;
279 const int usable
= !!(last_state
& DISORDER_PLAYING
);
280 update_icon(icon
, visible
, usable
);
283 static void update_random_enable(const struct icon
*icon
) {
284 const int visible
= !(last_state
& DISORDER_RANDOM_ENABLED
);
285 const int usable
= 1;
286 update_icon(icon
, visible
, usable
);
289 static void update_random_disable(const struct icon
*icon
) {
290 const int visible
= !!(last_state
& DISORDER_RANDOM_ENABLED
);
291 const int usable
= 1;
292 update_icon(icon
, visible
, usable
);
295 static void update_enable(const struct icon
*icon
) {
296 const int visible
= !(last_state
& DISORDER_PLAYING_ENABLED
);
297 const int usable
= 1;
298 update_icon(icon
, visible
, usable
);
301 static void update_disable(const struct icon
*icon
) {
302 const int visible
= !!(last_state
& DISORDER_PLAYING_ENABLED
);
303 const int usable
= 1;
304 update_icon(icon
, visible
, usable
);
307 static void update_rtp(const struct icon
*icon
) {
308 const int visible
= !rtp_is_running
;
309 const int usable
= rtp_supported
;
310 update_icon(icon
, visible
, usable
);
313 static void update_nortp(const struct icon
*icon
) {
314 const int visible
= rtp_is_running
;
315 const int usable
= rtp_supported
;
316 update_icon(icon
, visible
, usable
);
319 static void clicked_icon(GtkButton
attribute((unused
)) *button
,
321 const struct icon
*icon
= userdata
;
323 icon
->action(client
, 0, 0);
326 static void clicked_menu(GtkMenuItem
attribute((unused
)) *menuitem
,
328 const struct icon
*icon
= userdata
;
330 icon
->action(client
, 0, 0);
333 /** @brief Called when the volume has been adjusted */
334 static void volume_adjusted(GtkAdjustment
attribute((unused
)) *a
,
335 gpointer
attribute((unused
)) user_data
) {
336 double v
= gtk_adjustment_get_value(volume_adj
) / goesupto
;
337 double b
= gtk_adjustment_get_value(balance_adj
);
339 if(suppress_set_volume
)
340 /* This is the result of an update from the server, not a change from the
341 * user. Don't feedback! */
343 D(("volume_adjusted"));
344 /* force to 'stereotypical' values */
345 v
= nearbyint(100 * v
) / 100;
346 b
= nearbyint(5 * b
) / 5;
347 /* Set the volume. We don't want a reply, we'll get the actual new volume
349 disorder_eclient_volume(client
, 0,
350 nearbyint(left(v
, b
) * 100),
351 nearbyint(right(v
, b
) * 100),
355 /** @brief Formats the volume value */
356 static gchar
*format_volume(GtkScale
attribute((unused
)) *scale
,
360 snprintf(s
, sizeof s
, "%.1f", (double)value
);
364 /** @brief Formats the balance value */
365 static gchar
*format_balance(GtkScale
attribute((unused
)) *scale
,
369 if(fabs(value
) < 0.1)
370 return g_strdup("0");
371 snprintf(s
, sizeof s
, "%+.1f", (double)value
);
375 /* Volume mapping. We consider left, right, volume to be in [0,1]
376 * and balance to be in [-1,1].
378 * First, we just have volume = max(left, right).
380 * Balance we consider to linearly represent the amount by which the quieter
381 * channel differs from the louder. In detail:
383 * if right > left then balance > 0:
384 * balance = 0 => left = right (as an endpoint, not an instance)
385 * balance = 1 => left = 0
386 * fitting to linear, left = right * (1 - balance)
387 * so balance = 1 - left / right
388 * (right > left => right > 0 so no division by 0.)
390 * if left > right then balance < 0:
391 * balance = 0 => right = left (same caveat as above)
392 * balance = -1 => right = 0
393 * again fitting to linear, right = left * (1 + balance)
394 * so balance = right / left - 1
395 * (left > right => left > 0 so no division by 0.)
397 * if left = right then we just have balance = 0.
399 * Thanks to Clive and Andrew.
402 /** @brief Return the greater of @p x and @p y */
403 static double max(double x
, double y
) {
404 return x
> y ? x
: y
;
407 /** @brief Compute the left channel volume */
408 static double left(double v
, double b
) {
409 if(b
> 0) /* volume = right */
411 else /* volume = left */
415 /** @brief Compute the right channel volume */
416 static double right(double v
, double b
) {
417 if(b
> 0) /* volume = right */
419 else /* volume = left */
423 /** @brief Compute the overall volume */
424 static double volume(double l
, double r
) {
428 /** @brief Compute the balance */
429 static double balance(double l
, double r
) {
434 else /* left = right */
438 /** @brief Called to enable RTP play
440 * Rather odd signature is to fit in with the other icons which all call @ref
441 * lib/eclient.h functions.
443 static int enable_rtp(disorder_eclient
attribute((unused
)) *c
,
444 disorder_eclient_no_response
attribute((unused
)) *completed
,
445 void attribute((unused
)) *v
) {
450 /** @brief Called to disable RTP play
452 * Rather odd signature is to fit in with the other icons which all call @ref
453 * lib/eclient.h functions.
455 static int disable_rtp(disorder_eclient
attribute((unused
)) *c
,
456 disorder_eclient_no_response
attribute((unused
)) *completed
,
457 void attribute((unused
)) *v
) {