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