chiark / gitweb /
1d051672000f1d766b9f6b192a6705fd73af1fd3
[disorder] / disobedience / control.c
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
26 /* Forward declarations ---------------------------------------------------- */
27
28 WT(adjustment);
29 WT(hscale);
30 WT(hbox);
31 WT(tooltips);
32 WT(button);
33 WT(image);
34 WT(label);
35 WT(vbox);
36
37 struct icon;
38
39 static void update_pause(const struct icon *);
40 static void update_play(const struct icon *);
41 static void update_scratch(const struct icon *);
42 static void update_random_enable(const struct icon *);
43 static void update_random_disable(const struct icon *);
44 static void update_enable(const struct icon *);
45 static void update_disable(const struct icon *);
46 static void clicked_icon(GtkButton *, gpointer);
47
48 static double left(double v, double b);
49 static double right(double v, double b);
50 static double volume(double l, double r);
51 static double balance(double l, double r);
52
53 static void volume_adjusted(GtkAdjustment *a, gpointer user_data);
54 static gchar *format_volume(GtkScale *scale, gdouble value);
55 static gchar *format_balance(GtkScale *scale, gdouble value);
56
57 /* Control bar ------------------------------------------------------------- */
58
59 /** @brief Guard against feedback loop in volume control */
60 static int suppress_set_volume;
61
62 /** @brief Definition of an icon
63  *
64  * The design here is rather mad: rather than changing the image displayed by
65  * icons according to their state, we flip the visibility of pairs of icons.
66  */
67 struct icon {
68   /** @brief Filename for image */
69   const char *icon;
70
71   /** @brief Text for tooltip */
72   const char *tip;
73
74   /** @brief Called when button is clicked (activated) */
75   void (*clicked)(GtkButton *button, gpointer userdata);
76
77   /** @brief Called to update button when state may have changed */
78   void (*update)(const struct icon *i);
79
80   /** @brief @ref eclient.h function to call */
81   int (*action)(disorder_eclient *c,
82                 disorder_eclient_no_response *completed,
83                 void *v);
84
85   /** @brief Pointer to button */
86   GtkWidget *button;
87 };
88
89 /** @brief Table of all icons */
90 static struct icon icons[] = {
91   { "pause.png", "Pause playing track", clicked_icon, update_pause,
92     disorder_eclient_pause, 0 },
93   { "play.png", "Resume playing track", clicked_icon, update_play,
94     disorder_eclient_resume, 0 },
95   { "cross.png", "Cancel playing track", clicked_icon, update_scratch,
96     disorder_eclient_scratch_playing, 0 },
97   { "random.png", "Enable random play", clicked_icon, update_random_enable,
98     disorder_eclient_random_enable, 0 },
99   { "randomcross.png", "Disable random play", clicked_icon, update_random_disable,
100     disorder_eclient_random_disable, 0 },
101   { "notes.png", "Enable play", clicked_icon, update_enable,
102     disorder_eclient_enable, 0 },
103   { "notescross.png", "Disable play", clicked_icon, update_disable,
104     disorder_eclient_disable, 0 },
105 };
106
107 /** @brief Count of icons */
108 #define NICONS (int)(sizeof icons / sizeof *icons)
109
110 static GtkAdjustment *volume_adj;
111 static GtkAdjustment *balance_adj;
112
113 /** @brief Called whenever last_state changes in any way */
114 static void control_monitor(void attribute((unused)) *u) {
115   int n;
116
117   D(("control_monitor"));
118   for(n = 0; n < NICONS; ++n)
119     icons[n].update(&icons[n]);
120 }
121
122 /** @brief Create the control bar */
123 GtkWidget *control_widget(void) {
124   GtkWidget *hbox = gtk_hbox_new(FALSE, 1), *vbox;
125   GtkWidget *content;
126   GdkPixbuf *pb;
127   GtkWidget *v, *b;
128   GtkTooltips *tips = gtk_tooltips_new();
129   int n;
130
131   NW(hbox);
132   NW(tooltips);
133   D(("control_widget"));
134   for(n = 0; n < NICONS; ++n) {
135     NW(button);
136     icons[n].button = gtk_button_new();
137     if((pb = find_image(icons[n].icon))) {
138       NW(image);
139       content = gtk_image_new_from_pixbuf(pb);
140     } else {
141       NW(label);
142       content = gtk_label_new(icons[n].icon);
143     }
144     gtk_container_add(GTK_CONTAINER(icons[n].button), content);
145     gtk_tooltips_set_tip(tips, icons[n].button, icons[n].tip, "");
146     g_signal_connect(G_OBJECT(icons[n].button), "clicked",
147                      G_CALLBACK(icons[n].clicked), &icons[n]);
148     /* pop the icon in a vbox so it doesn't get vertically stretch if there are
149      * taller things in the control bar */
150     NW(vbox);
151     vbox = gtk_vbox_new(FALSE, 0);
152     gtk_box_pack_start(GTK_BOX(vbox), icons[n].button, TRUE, FALSE, 0);
153     gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
154   }
155   /* create the adjustments for the volume control */
156   NW(adjustment);
157   volume_adj = GTK_ADJUSTMENT(gtk_adjustment_new(0, 0, goesupto,
158                                                  goesupto / 20, goesupto / 20,
159                                                  0));
160   NW(adjustment);
161   balance_adj = GTK_ADJUSTMENT(gtk_adjustment_new(0, -1, 1,
162                                                   0.2, 0.2, 0));
163   /* the volume control */
164   NW(hscale);
165   v = gtk_hscale_new(volume_adj);
166   NW(hscale);
167   b = gtk_hscale_new(balance_adj);
168   gtk_scale_set_digits(GTK_SCALE(v), 10);
169   gtk_scale_set_digits(GTK_SCALE(b), 10);
170   gtk_widget_set_size_request(v, 192, -1);
171   gtk_widget_set_size_request(b, 192, -1);
172   gtk_tooltips_set_tip(tips, v, "Volume", "");
173   gtk_tooltips_set_tip(tips, b, "Balance", "");
174   gtk_box_pack_start(GTK_BOX(hbox), v, FALSE, TRUE, 0);
175   gtk_box_pack_start(GTK_BOX(hbox), b, FALSE, TRUE, 0);
176   /* space updates rather than hammering the server */
177   gtk_range_set_update_policy(GTK_RANGE(v), GTK_UPDATE_DELAYED);
178   gtk_range_set_update_policy(GTK_RANGE(b), GTK_UPDATE_DELAYED);
179   /* notice when the adjustments are changed */
180   g_signal_connect(G_OBJECT(volume_adj), "value-changed",
181                    G_CALLBACK(volume_adjusted), 0);
182   g_signal_connect(G_OBJECT(balance_adj), "value-changed",
183                    G_CALLBACK(volume_adjusted), 0);
184   /* format the volume/balance values ourselves */
185   g_signal_connect(G_OBJECT(v), "format-value",
186                    G_CALLBACK(format_volume), 0);
187   g_signal_connect(G_OBJECT(b), "format-value",
188                    G_CALLBACK(format_balance), 0);
189   register_monitor(control_monitor, 0, -1UL);
190   return hbox;
191 }
192
193 /** @brief Update the volume control when it changes */
194 void volume_update(void) {
195   double l, r;
196
197   D(("volume_update"));
198   l = volume_l / 100.0;
199   r = volume_r / 100.0;
200   ++suppress_set_volume;
201   gtk_adjustment_set_value(volume_adj, volume(l, r) * goesupto);
202   gtk_adjustment_set_value(balance_adj, balance(l, r));
203   --suppress_set_volume;
204 }
205
206 /** @brief Update the state of one of the control icons
207  * @param icon Target icon
208  * @param visible True if this version of the button should be visible
209  * @param usable True if the button is currently usable
210  *
211  * Several of the icons, rather bizarrely, come in pairs: for instance exactly
212  * one of the play and pause buttons is supposed to be visible at any given
213  * moment.
214  *
215  * @p usable need not take into account server availability, that is done
216  * automatically.
217  */
218 static void update_icon(const struct icon *icon,
219                         int visible, int usable) {
220   /* If the connection is down nothing is ever usable */
221   if(!(last_state & DISORDER_CONNECTED))
222     usable = 0;
223   (visible ? gtk_widget_show : gtk_widget_hide)(icon->button);
224   /* Only both updating usability if the button is visible */
225   if(visible)
226     gtk_widget_set_sensitive(icon->button, usable);
227 }
228
229 static void update_pause(const struct icon *icon) {
230   const int visible = !(last_state & DISORDER_TRACK_PAUSED);
231   const int usable = !!(last_state & DISORDER_PLAYING); /* TODO: might be a lie */
232   update_icon(icon, visible, usable);
233 }
234
235 static void update_play(const struct icon *icon) {
236   const int visible = !!(last_state & DISORDER_TRACK_PAUSED);
237   const int usable = !!(last_state & DISORDER_PLAYING);
238   update_icon(icon, visible, usable);
239 }
240
241 static void update_scratch(const struct icon *icon) {
242   const int visible = 1;
243   const int usable = !!(last_state & DISORDER_PLAYING);
244   update_icon(icon, visible, usable);
245 }
246
247 static void update_random_enable(const struct icon *icon) {
248   const int visible = !(last_state & DISORDER_RANDOM_ENABLED);
249   const int usable = 1;
250   update_icon(icon, visible, usable);
251 }
252
253 static void update_random_disable(const struct icon *icon) {
254   const int visible = !!(last_state & DISORDER_RANDOM_ENABLED);
255   const int usable = 1;
256   update_icon(icon, visible, usable);
257 }
258
259 static void update_enable(const struct icon *icon) {
260   const int visible = !(last_state & DISORDER_PLAYING_ENABLED);
261   const int usable = 1;
262   update_icon(icon, visible, usable);
263 }
264
265 static void update_disable(const struct icon *icon) {
266   const int visible = !!(last_state & DISORDER_PLAYING_ENABLED);
267   const int usable = 1;
268   update_icon(icon, visible, usable);
269 }
270
271 static void clicked_icon(GtkButton attribute((unused)) *button,
272                          gpointer userdata) {
273   const struct icon *icon = userdata;
274
275   icon->action(client, 0, 0);
276 }
277
278 /** @brief Called when the volume has been adjusted */
279 static void volume_adjusted(GtkAdjustment attribute((unused)) *a,
280                             gpointer attribute((unused)) user_data) {
281   double v = gtk_adjustment_get_value(volume_adj) / goesupto;
282   double b = gtk_adjustment_get_value(balance_adj);
283
284   if(suppress_set_volume)
285     /* This is the result of an update from the server, not a change from the
286      * user.  Don't feedback! */
287     return;
288   D(("volume_adjusted"));
289   /* force to 'stereotypical' values */
290   v = nearbyint(100 * v) / 100;
291   b = nearbyint(5 * b) / 5;
292   /* Set the volume.  We don't want a reply, we'll get the actual new volume
293    * from the log. */
294   disorder_eclient_volume(client, 0,
295                           nearbyint(left(v, b) * 100),
296                           nearbyint(right(v, b) * 100),
297                           0);
298 }
299
300 /** @brief Formats the volume value */
301 static gchar *format_volume(GtkScale attribute((unused)) *scale,
302                             gdouble value) {
303   char s[32];
304
305   snprintf(s, sizeof s, "%.1f", (double)value);
306   return g_strdup(s);
307 }
308
309 /** @brief Formats the balance value */
310 static gchar *format_balance(GtkScale attribute((unused)) *scale,
311                              gdouble value) {
312   char s[32];
313
314   if(fabs(value) < 0.1)
315     return g_strdup("0");
316   snprintf(s, sizeof s, "%+.1f", (double)value);
317   return g_strdup(s);
318 }
319
320 /* Volume mapping.  We consider left, right, volume to be in [0,1]
321  * and balance to be in [-1,1].
322  * 
323  * First, we just have volume = max(left, right).
324  *
325  * Balance we consider to linearly represent the amount by which the quieter
326  * channel differs from the louder.  In detail:
327  *
328  *  if right > left then balance > 0:
329  *   balance = 0 => left = right  (as an endpoint, not an instance)
330  *   balance = 1 => left = 0
331  *   fitting to linear, left = right * (1 - balance)
332  *                so balance = 1 - left / right
333  *   (right > left => right > 0 so no division by 0.)
334  * 
335  *  if left > right then balance < 0:
336  *   balance = 0 => right = left  (same caveat as above)
337  *   balance = -1 => right = 0
338  *   again fitting to linear, right = left * (1 + balance)
339  *                       so balance = right / left - 1
340  *   (left > right => left > 0 so no division by 0.)
341  *
342  *  if left = right then we just have balance = 0.
343  *
344  * Thanks to Clive and Andrew.
345  */
346
347 /** @brief Return the greater of @p x and @p y */
348 static double max(double x, double y) {
349   return x > y ? x : y;
350 }
351
352 /** @brief Compute the left channel volume */
353 static double left(double v, double b) {
354   if(b > 0)                             /* volume = right */
355     return v * (1 - b);
356   else                                  /* volume = left */
357     return v;
358 }
359
360 /** @brief Compute the right channel volume */
361 static double right(double v, double b) {
362   if(b > 0)                             /* volume = right */
363     return v;
364   else                                  /* volume = left */
365     return v * (1 + b);
366 }
367
368 /** @brief Compute the overall volume */
369 static double volume(double l, double r) {
370   return max(l, r);
371 }
372
373 /** @brief Compute the balance */
374 static double balance(double l, double r) {
375   if(l > r)
376     return r / l - 1;
377   else if(r > l)
378     return 1 - l / r;
379   else                                  /* left = right */
380     return 0;
381 }
382
383 /*
384 Local Variables:
385 c-basic-offset:2
386 comment-column:40
387 fill-column:79
388 indent-tabs-mode:nil
389 End:
390 */