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