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