chiark / gitweb /
start to create menu items corresponding to control items
[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(button);
32 WT(image);
33 WT(label);
34 WT(vbox);
35
36 struct icon;
37
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);
49
50 static int enable_rtp(disorder_eclient *c,
51                       disorder_eclient_no_response *completed,
52                       void *v);
53 static int disable_rtp(disorder_eclient *c,
54                        disorder_eclient_no_response *completed,
55                        void *v);
56
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);
61
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);
65
66 /* Control bar ------------------------------------------------------------- */
67
68 /** @brief Guard against feedback loop in volume control */
69 static int suppress_set_volume;
70
71 /** @brief Definition of an icon
72  *
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.
75  */
76 struct icon {
77   /** @brief Filename for image */
78   const char *icon;
79
80   /** @brief Text for tooltip */
81   const char *tip;
82
83   /** @brief Associated menu item or NULL */
84   const char *menuitem;
85
86   /** @brief Called to update button when state may have changed */
87   void (*update)(const struct icon *i);
88
89   /** @brief @ref eclient.h function to call */
90   int (*action)(disorder_eclient *c,
91                 disorder_eclient_no_response *completed,
92                 void *v);
93
94   /** @brief Pointer to button */
95   GtkWidget *button;
96
97   /** @brief Pointer to menu item */
98   GtkWidget *item;
99 };
100
101 /** @brief Table of all icons */
102 static struct icon icons[] = {
103   { "pause.png", "Pause playing track", 0,
104     update_pause,
105     disorder_eclient_pause, 0, 0 },
106   { "play.png", "Resume playing track", 0,
107     update_play,
108     disorder_eclient_resume, 0, 0 },
109   { "cross.png", "Cancel playing track", "<GdisorderMain>/Control/Scratch",
110     update_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,
119     update_enable,
120     disorder_eclient_enable, 0, 0 },
121   { "notescross.png", "Disable play", 0,
122     update_disable,
123     disorder_eclient_disable, 0, 0 },
124   { "speaker.png", "Play network stream", 0,
125     update_rtp,
126     enable_rtp, 0, 0 },
127   { "speakercross.png", "Stop playing network stream", 0,
128     update_nortp,
129     disable_rtp, 0, 0 },
130 };
131
132 /** @brief Count of icons */
133 #define NICONS (int)(sizeof icons / sizeof *icons)
134
135 static GtkAdjustment *volume_adj;
136 static GtkAdjustment *balance_adj;
137
138 /** @brief Called whenever last_state changes in any way */
139 void control_monitor(void attribute((unused)) *u) {
140   int n;
141
142   D(("control_monitor"));
143   for(n = 0; n < NICONS; ++n)
144     icons[n].update(&icons[n]);
145 }
146
147 /** @brief Create the control bar */
148 GtkWidget *control_widget(void) {
149   GtkWidget *hbox = gtk_hbox_new(FALSE, 1), *vbox;
150   GtkWidget *content;
151   GdkPixbuf *pb;
152   GtkWidget *v, *b;
153   int n;
154
155   NW(hbox);
156   D(("control_widget"));
157   assert(mainmenufactory);              /* ordering must be right */
158   for(n = 0; n < NICONS; ++n) {
159     NW(button);
160     icons[n].button = gtk_button_new();
161     if((pb = find_image(icons[n].icon))) {
162       NW(image);
163       content = gtk_image_new_from_pixbuf(pb);
164     } else {
165       NW(label);
166       content = gtk_label_new(icons[n].icon);
167     }
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 */
174     NW(vbox);
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,
180                                                   icons[n].menuitem);
181       g_signal_connect(G_OBJECT(icons[n].item), "activate",
182                        G_CALLBACK(clicked_menu), &icons[n]);
183     }
184   }
185   /* create the adjustments for the volume control */
186   NW(adjustment);
187   volume_adj = GTK_ADJUSTMENT(gtk_adjustment_new(0, 0, goesupto,
188                                                  goesupto / 20, goesupto / 20,
189                                                  0));
190   NW(adjustment);
191   balance_adj = GTK_ADJUSTMENT(gtk_adjustment_new(0, -1, 1,
192                                                   0.2, 0.2, 0));
193   /* the volume control */
194   NW(hscale);
195   v = gtk_hscale_new(volume_adj);
196   NW(hscale);
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);
220   return hbox;
221 }
222
223 /** @brief Update the volume control when it changes */
224 void volume_update(void) {
225   double l, r;
226
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;
234 }
235
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
240  *
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
243  * moment.
244  *
245  * @p usable need not take into account server availability, that is done
246  * automatically.
247  */
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))
252     usable = 0;
253   (visible ? gtk_widget_show : gtk_widget_hide)(icon->button);
254   /* Only both updating usability if the button is visible */
255   if(visible)
256     gtk_widget_set_sensitive(icon->button, usable);
257   if(icon->item) {
258     /* There's an associated menu item */
259     (visible ? gtk_widget_show : gtk_widget_hide)(icon->item);
260     if(visible)
261       gtk_widget_set_sensitive(icon->item, usable);
262   }
263 }
264
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);
269 }
270
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);
275 }
276
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);
281 }
282
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);
287 }
288
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);
293 }
294
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);
299 }
300
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);
305 }
306
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);
311 }
312
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);
317 }
318
319 static void clicked_icon(GtkButton attribute((unused)) *button,
320                          gpointer userdata) {
321   const struct icon *icon = userdata;
322
323   icon->action(client, 0, 0);
324 }
325
326 static void clicked_menu(GtkMenuItem attribute((unused)) *menuitem,
327                          gpointer userdata) {
328   const struct icon *icon = userdata;
329
330   icon->action(client, 0, 0);
331 }
332
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);
338
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! */
342     return;
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
348    * from the log. */
349   disorder_eclient_volume(client, 0,
350                           nearbyint(left(v, b) * 100),
351                           nearbyint(right(v, b) * 100),
352                           0);
353 }
354
355 /** @brief Formats the volume value */
356 static gchar *format_volume(GtkScale attribute((unused)) *scale,
357                             gdouble value) {
358   char s[32];
359
360   snprintf(s, sizeof s, "%.1f", (double)value);
361   return g_strdup(s);
362 }
363
364 /** @brief Formats the balance value */
365 static gchar *format_balance(GtkScale attribute((unused)) *scale,
366                              gdouble value) {
367   char s[32];
368
369   if(fabs(value) < 0.1)
370     return g_strdup("0");
371   snprintf(s, sizeof s, "%+.1f", (double)value);
372   return g_strdup(s);
373 }
374
375 /* Volume mapping.  We consider left, right, volume to be in [0,1]
376  * and balance to be in [-1,1].
377  * 
378  * First, we just have volume = max(left, right).
379  *
380  * Balance we consider to linearly represent the amount by which the quieter
381  * channel differs from the louder.  In detail:
382  *
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.)
389  * 
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.)
396  *
397  *  if left = right then we just have balance = 0.
398  *
399  * Thanks to Clive and Andrew.
400  */
401
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;
405 }
406
407 /** @brief Compute the left channel volume */
408 static double left(double v, double b) {
409   if(b > 0)                             /* volume = right */
410     return v * (1 - b);
411   else                                  /* volume = left */
412     return v;
413 }
414
415 /** @brief Compute the right channel volume */
416 static double right(double v, double b) {
417   if(b > 0)                             /* volume = right */
418     return v;
419   else                                  /* volume = left */
420     return v * (1 + b);
421 }
422
423 /** @brief Compute the overall volume */
424 static double volume(double l, double r) {
425   return max(l, r);
426 }
427
428 /** @brief Compute the balance */
429 static double balance(double l, double r) {
430   if(l > r)
431     return r / l - 1;
432   else if(r > l)
433     return 1 - l / r;
434   else                                  /* left = right */
435     return 0;
436 }
437
438 /** @brief Called to enable RTP play
439  *
440  * Rather odd signature is to fit in with the other icons which all call @ref
441  * lib/eclient.h functions.
442  */
443 static int enable_rtp(disorder_eclient attribute((unused)) *c,
444                       disorder_eclient_no_response attribute((unused)) *completed,
445                       void attribute((unused)) *v) {
446   start_rtp();
447   return 0;
448 }
449
450 /** @brief Called to disable RTP play
451  *
452  * Rather odd signature is to fit in with the other icons which all call @ref
453  * lib/eclient.h functions.
454  */
455 static int disable_rtp(disorder_eclient attribute((unused)) *c,
456                        disorder_eclient_no_response attribute((unused)) *completed,
457                        void attribute((unused)) *v) {
458   stop_rtp();
459   return 0;
460 }
461
462 /*
463 Local Variables:
464 c-basic-offset:2
465 comment-column:40
466 fill-column:79
467 indent-tabs-mode:nil
468 End:
469 */