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