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