chiark / gitweb /
Leave a comment about iffy icon sensivity; it's definitely setting the
[disorder] / disobedience / control.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2006-2008 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 clicked_icon(GtkButton *, gpointer);
40 static void clicked_menu(GtkMenuItem *, gpointer userdata);
41 static void toggled_menu(GtkCheckMenuItem *, gpointer userdata);
42
43 static int enable_rtp(disorder_eclient *c,
44                       disorder_eclient_no_response *completed,
45                       void *v);
46 static int disable_rtp(disorder_eclient *c,
47                        disorder_eclient_no_response *completed,
48                        void *v);
49
50 static double left(double v, double b);
51 static double right(double v, double b);
52 static double volume(double l, double r);
53 static double balance(double l, double r);
54
55 static void volume_adjusted(GtkAdjustment *a, gpointer user_data);
56 static gchar *format_volume(GtkScale *scale, gdouble value);
57 static gchar *format_balance(GtkScale *scale, gdouble value);
58
59 static void icon_changed(const char *event,
60                          void *evendata,
61                          void *callbackdata);
62 static void volume_changed(const char *event,
63                            void *eventdata,
64                            void *callbackdata);
65
66 /* Control bar ------------------------------------------------------------- */
67
68 /** @brief Guard against feedback */
69 int suppress_actions = 1;
70
71 /** @brief Definition of an icon
72  *
73  * We have two kinds of icon:
74  * - action icons, which just do something but don't have a state as such
75  * - toggle icons, which toggle between two states ("on" and "off").
76  *
77  * The scratch button is an action icon; currently all the others are toggle
78  * icons.
79  *
80  * (All icons can be sensitive or insensitive, separately to the above.)
81  */
82 struct icon {
83   /** @brief Filename for 'on' image */
84   const char *icon_on;
85
86   /** @brief Text for 'on' tooltip */
87   const char *tip_on;
88
89   /** @brief Filename for 'off' image or NULL for an action icon */
90   const char *icon_off;
91
92   /** @brief Text for 'off tooltip */
93   const char *tip_off;
94
95   /** @brief Associated menu item or NULL */
96   const char *menuitem;
97
98   /** @brief Events that change this icon, separated by spaces */
99   const char *events;
100
101   /** @brief @ref eclient.h function to call to go from off to on
102    *
103    * For action buttons, this should be NULL.
104    */
105   int (*action_go_on)(disorder_eclient *c,
106                       disorder_eclient_no_response *completed,
107                       void *v);
108
109   /** @brief @ref eclient.h function to call to go from on to off
110    *
111    * For action buttons, this action is used.
112    */
113   int (*action_go_off)(disorder_eclient *c,
114                        disorder_eclient_no_response *completed,
115                        void *v);
116
117   /** @brief Get button state
118    * @return 1 for on, 0 for off
119    */
120   int (*on)(void);
121
122   /** @brief Get button sensitivity
123    * @return 1 for sensitive, 0 for insensitive
124    *
125    * Can be NULL for always sensitive.
126    */
127   int (*sensitive)(void);
128   
129   /** @brief Pointer to button */
130   GtkWidget *button;
131
132   /** @brief Pointer to menu item */
133   GtkWidget *item;
134
135   GtkWidget *image_on;
136   GtkWidget *image_off;
137 };
138
139 /* TODO: Add rights into the mix below */
140
141 static int pause_resume_on(void) {
142   return !(last_state & DISORDER_TRACK_PAUSED);
143 }
144
145 static int pause_resume_sensitive(void) {
146   return !!(last_state & DISORDER_PLAYING)
147     && (last_rights & RIGHT_PAUSE);
148 }
149
150 static int scratch_sensitive(void) {
151   return !!(last_state & DISORDER_PLAYING)
152     && right_scratchable(last_rights, config->username, playing_track);
153 }
154
155 static int random_sensitive(void) {
156   return !!(last_rights & RIGHT_GLOBAL_PREFS);
157 }
158
159 static int random_enabled(void) {
160   return !!(last_state & DISORDER_RANDOM_ENABLED);
161 }
162
163 static int playing_sensitive(void) {
164   return !!(last_rights & RIGHT_GLOBAL_PREFS);
165 }
166
167 static int playing_enabled(void) {
168   return !!(last_state & DISORDER_PLAYING_ENABLED);
169 }
170
171 static int rtp_enabled(void) {
172   return rtp_is_running;
173 }
174
175 static int rtp_sensitive(void) {
176   return rtp_supported;
177 }
178
179 /** @brief Table of all icons */
180 static struct icon icons[] = {
181   {
182     icon_on: "pause.png",
183     tip_on: "Pause playing track",
184     icon_off: "play.png",
185     tip_off: "Resume playing track",
186     menuitem: "<GdisorderMain>/Control/Playing",
187     on: pause_resume_on,
188     sensitive: pause_resume_sensitive,
189     action_go_on: disorder_eclient_resume,
190     action_go_off: disorder_eclient_pause,
191     events: "pause-changed playing-changed rights-changed",
192   },
193   {
194     icon_on: "cross.png",
195     tip_on: "Cancel playing track",
196     menuitem: "<GdisorderMain>/Control/Scratch",
197     sensitive: scratch_sensitive,
198     action_go_off: disorder_eclient_scratch_playing,
199     events: "playing-track-changed rights-changed",
200   },
201   {
202     icon_on: "randomcross.png",
203     tip_on: "Disable random play",
204     icon_off: "random.png",
205     tip_off: "Enable random play",
206     menuitem: "<GdisorderMain>/Control/Random play",
207     on: random_enabled,
208     sensitive: random_sensitive,
209     action_go_on: disorder_eclient_random_enable,
210     action_go_off: disorder_eclient_random_disable,
211     events: "random-changed rights-changed",
212   },
213   {
214     icon_on: "notescross.png",
215     tip_on: "Disable play",
216     icon_off: "notes.png",
217     tip_off: "Enable play",
218     on: playing_enabled,
219     sensitive: playing_sensitive,
220     action_go_on: disorder_eclient_enable,
221     action_go_off: disorder_eclient_disable,
222     events: "enabled-changed rights-changed",
223   },
224   {
225     icon_on: "speakercross.png",
226     tip_on: "Stop playing network stream",
227     icon_off: "speaker.png",
228     tip_off: "Play network stream",
229     menuitem: "<GdisorderMain>/Control/Network player",
230     on: rtp_enabled,
231     sensitive: rtp_sensitive,
232     action_go_on: enable_rtp,
233     action_go_off: disable_rtp,
234     events: "rtp-changed",
235   },
236 };
237
238 /** @brief Count of icons */
239 #define NICONS (int)(sizeof icons / sizeof *icons)
240
241 static GtkAdjustment *volume_adj;
242 static GtkAdjustment *balance_adj;
243 static GtkWidget *volume_widget;
244 static GtkWidget *balance_widget;
245
246 /** @brief Create the control bar */
247 GtkWidget *control_widget(void) {
248   GtkWidget *hbox = gtk_hbox_new(FALSE, 1), *vbox;
249   int n;
250
251   NW(hbox);
252   D(("control_widget"));
253   assert(mainmenufactory);              /* ordering must be right */
254   for(n = 0; n < NICONS; ++n) {
255     /* Create the button */
256     NW(button);
257     icons[n].button = gtk_button_new();
258     gtk_widget_set_style(icons[n].button, tool_style);
259     icons[n].image_on = gtk_image_new_from_pixbuf(find_image(icons[n].icon_on));
260     gtk_widget_set_style(icons[n].image_on, tool_style);
261     g_object_ref(icons[n].image_on);
262     /* If it's a toggle icon, create the 'off' half too */
263     if(icons[n].icon_off) {
264       icons[n].image_off = gtk_image_new_from_pixbuf(find_image(icons[n].icon_off));
265       gtk_widget_set_style(icons[n].image_off, tool_style);
266       g_object_ref(icons[n].image_off);
267     }
268     g_signal_connect(G_OBJECT(icons[n].button), "clicked",
269                      G_CALLBACK(clicked_icon), &icons[n]);
270     /* pop the icon in a vbox so it doesn't get vertically stretch if there are
271      * taller things in the control bar */
272     NW(vbox);
273     vbox = gtk_vbox_new(FALSE, 0);
274     gtk_box_pack_start(GTK_BOX(vbox), icons[n].button, TRUE, FALSE, 0);
275     gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
276     if(icons[n].menuitem) {
277       /* Find the menu item */
278       icons[n].item = gtk_item_factory_get_widget(mainmenufactory,
279                                                   icons[n].menuitem);
280       if(icons[n].icon_off)
281         g_signal_connect(G_OBJECT(icons[n].item), "toggled",
282                          G_CALLBACK(toggled_menu), &icons[n]);
283       else
284         g_signal_connect(G_OBJECT(icons[n].item), "activate",
285                          G_CALLBACK(clicked_menu), &icons[n]);
286     }
287     /* Make sure the icon is updated when relevant things changed */
288     char **events = split(icons[n].events, 0, 0, 0, 0);
289     while(*events)
290       event_register(*events++, icon_changed, &icons[n]);
291   }
292   /* create the adjustments for the volume control */
293   NW(adjustment);
294   volume_adj = GTK_ADJUSTMENT(gtk_adjustment_new(0, 0, goesupto,
295                                                  goesupto / 20, goesupto / 20,
296                                                  0));
297   NW(adjustment);
298   balance_adj = GTK_ADJUSTMENT(gtk_adjustment_new(0, -1, 1,
299                                                   0.2, 0.2, 0));
300   /* the volume control */
301   NW(hscale);
302   volume_widget = gtk_hscale_new(volume_adj);
303   NW(hscale);
304   balance_widget = gtk_hscale_new(balance_adj);
305   gtk_widget_set_style(volume_widget, tool_style);
306   gtk_widget_set_style(balance_widget, tool_style);
307   gtk_scale_set_digits(GTK_SCALE(volume_widget), 10);
308   gtk_scale_set_digits(GTK_SCALE(balance_widget), 10);
309   gtk_widget_set_size_request(volume_widget, 192, -1);
310   gtk_widget_set_size_request(balance_widget, 192, -1);
311   gtk_tooltips_set_tip(tips, volume_widget, "Volume", "");
312   gtk_tooltips_set_tip(tips, balance_widget, "Balance", "");
313   gtk_box_pack_start(GTK_BOX(hbox), volume_widget, FALSE, TRUE, 0);
314   gtk_box_pack_start(GTK_BOX(hbox), balance_widget, FALSE, TRUE, 0);
315   /* space updates rather than hammering the server */
316   gtk_range_set_update_policy(GTK_RANGE(volume_widget), GTK_UPDATE_DELAYED);
317   gtk_range_set_update_policy(GTK_RANGE(balance_widget), GTK_UPDATE_DELAYED);
318   /* notice when the adjustments are changed */
319   g_signal_connect(G_OBJECT(volume_adj), "value-changed",
320                    G_CALLBACK(volume_adjusted), 0);
321   g_signal_connect(G_OBJECT(balance_adj), "value-changed",
322                    G_CALLBACK(volume_adjusted), 0);
323   /* format the volume/balance values ourselves */
324   g_signal_connect(G_OBJECT(volume_widget), "format-value",
325                    G_CALLBACK(format_volume), 0);
326   g_signal_connect(G_OBJECT(balance_widget), "format-value",
327                    G_CALLBACK(format_balance), 0);
328   event_register("volume-changed", volume_changed, 0);
329   event_register("rtp-changed", volume_changed, 0);
330   return hbox;
331 }
332
333 /** @brief Update the volume control when it changes */
334 static void volume_changed(const char attribute((unused)) *event,
335                            void attribute((unused)) *eventdata,
336                            void attribute((unused)) *callbackdata) {
337   double l, r;
338   gboolean volume_supported;
339
340   D(("volume_changed"));
341   ++suppress_actions;
342   /* Only display volume/balance controls if they will work */
343   if(!rtp_supported
344      || (rtp_supported && mixer_supported(DEFAULT_BACKEND)))
345     volume_supported = TRUE;
346   else
347     volume_supported = FALSE;
348   /* TODO: if the server doesn't know how to set the volume [but isn't using
349    * network play] then we should have volume_supported = FALSE */
350   if(volume_supported) {
351     gtk_widget_show(volume_widget);
352     gtk_widget_show(balance_widget);
353     l = volume_l / 100.0;
354     r = volume_r / 100.0;
355     gtk_adjustment_set_value(volume_adj, volume(l, r) * goesupto);
356     gtk_adjustment_set_value(balance_adj, balance(l, r));
357   } else {
358     gtk_widget_hide(volume_widget);
359     gtk_widget_hide(balance_widget);
360   }
361   --suppress_actions;
362 }
363
364 /** @brief Update the state of one of the control icons
365  */
366 static void icon_changed(const char attribute((unused)) *event,
367                          void attribute((unused)) *evendata,
368                          void *callbackdata) {
369   const struct icon *const icon = callbackdata;
370   int on = icon->on ? icon->on() : 1;
371   int sensitive = icon->sensitive ? icon->sensitive() : 1;
372   GtkWidget *child, *newchild;
373
374   ++suppress_actions;
375   /* If the connection is down nothing is ever usable */
376   if(!(last_state & DISORDER_CONNECTED))
377     sensitive = 0;
378   /* Replace the child */
379   newchild = on ? icon->image_on : icon->image_off;
380   child = gtk_bin_get_child(GTK_BIN(icon->button));
381   if(child != newchild) {
382     if(child)
383       gtk_container_remove(GTK_CONTAINER(icon->button), child);
384     gtk_container_add(GTK_CONTAINER(icon->button), newchild);
385     gtk_widget_show(newchild);
386   }
387   /* If you disable play or random play NOT via the icon (for instance, via the
388    * edit menu or via a completely separate command line invocation) then the
389    * icon shows up as insensitive.  Hover the mouse over it and the corrcet
390    * state is immediately displayed.  sensitive and GTK_WIDGET_SENSITIVE show
391    * it to be in the correct state, so I think this is may be a GTK+ bug. */
392   if(icon->tip_on)
393     gtk_tooltips_set_tip(tips, icon->button,
394                            on ? icon->tip_on : icon->tip_off, "");
395   gtk_widget_set_sensitive(icon->button, sensitive);
396   /* Icons with an associated menu item */
397   if(icon->item) {
398     if(icon->icon_off)
399       gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(icon->item), on);
400     gtk_widget_set_sensitive(icon->item, sensitive);
401   }
402   --suppress_actions;
403 }
404
405 static void icon_action_completed(void attribute((unused)) *v,
406                                   const char *error) {
407   if(error)
408     popup_protocol_error(0, error);
409 }
410
411 static void clicked_icon(GtkButton attribute((unused)) *button,
412                          gpointer userdata) {
413   const struct icon *icon = userdata;
414
415   if(suppress_actions)
416     return;
417   if(!icon->on || icon->on())
418     icon->action_go_off(client, icon_action_completed, 0);
419   else
420     icon->action_go_on(client, icon_action_completed, 0);
421 }
422
423 static void clicked_menu(GtkMenuItem attribute((unused)) *menuitem,
424                          gpointer userdata) {
425   clicked_icon(NULL, userdata);
426 }
427
428 static void toggled_menu(GtkCheckMenuItem attribute((unused)) *menuitem,
429                          gpointer userdata) {
430   clicked_icon(NULL, userdata);
431 }
432
433 /** @brief Called when a volume command completes */
434 static void volume_completed(void attribute((unused)) *v,
435                              const char *error,
436                              int attribute((unused)) l,
437                              int attribute((unused)) r) {
438   if(error)
439     popup_protocol_error(0, error);
440   /* We don't set the UI's notion of the volume here, it is set from the log
441    * regardless of the reason it changed */
442 }
443
444 /** @brief Called when the volume has been adjusted */
445 static void volume_adjusted(GtkAdjustment attribute((unused)) *a,
446                             gpointer attribute((unused)) user_data) {
447   double v = gtk_adjustment_get_value(volume_adj) / goesupto;
448   double b = gtk_adjustment_get_value(balance_adj);
449
450   if(suppress_actions)
451     /* This is the result of an update from the server, not a change from the
452      * user.  Don't feedback! */
453     return;
454   D(("volume_adjusted"));
455   /* force to 'stereotypical' values */
456   v = nearbyint(100 * v) / 100;
457   b = nearbyint(5 * b) / 5;
458   /* Set the volume.  We don't want a reply, we'll get the actual new volume
459    * from the log. */
460   if(rtp_supported) {
461     int l = nearbyint(left(v, b) * 100), r = nearbyint(right(v, b) * 100);
462     mixer_control(DEFAULT_BACKEND, &l, &r, 1);
463   } else
464     disorder_eclient_volume(client, volume_completed,
465                             nearbyint(left(v, b) * 100),
466                             nearbyint(right(v, b) * 100),
467                             0);
468 }
469
470 /** @brief Formats the volume value */
471 static gchar *format_volume(GtkScale attribute((unused)) *scale,
472                             gdouble value) {
473   char s[32];
474
475   snprintf(s, sizeof s, "%.1f", (double)value);
476   return g_strdup(s);
477 }
478
479 /** @brief Formats the balance value */
480 static gchar *format_balance(GtkScale attribute((unused)) *scale,
481                              gdouble value) {
482   char s[32];
483
484   if(fabs(value) < 0.1)
485     return g_strdup("0");
486   snprintf(s, sizeof s, "%+.1f", (double)value);
487   return g_strdup(s);
488 }
489
490 /* Volume mapping.  We consider left, right, volume to be in [0,1]
491  * and balance to be in [-1,1].
492  * 
493  * First, we just have volume = max(left, right).
494  *
495  * Balance we consider to linearly represent the amount by which the quieter
496  * channel differs from the louder.  In detail:
497  *
498  *  if right > left then balance > 0:
499  *   balance = 0 => left = right  (as an endpoint, not an instance)
500  *   balance = 1 => left = 0
501  *   fitting to linear, left = right * (1 - balance)
502  *                so balance = 1 - left / right
503  *   (right > left => right > 0 so no division by 0.)
504  * 
505  *  if left > right then balance < 0:
506  *   balance = 0 => right = left  (same caveat as above)
507  *   balance = -1 => right = 0
508  *   again fitting to linear, right = left * (1 + balance)
509  *                       so balance = right / left - 1
510  *   (left > right => left > 0 so no division by 0.)
511  *
512  *  if left = right then we just have balance = 0.
513  *
514  * Thanks to Clive and Andrew.
515  */
516
517 /** @brief Return the greater of @p x and @p y */
518 static double max(double x, double y) {
519   return x > y ? x : y;
520 }
521
522 /** @brief Compute the left channel volume */
523 static double left(double v, double b) {
524   if(b > 0)                             /* volume = right */
525     return v * (1 - b);
526   else                                  /* volume = left */
527     return v;
528 }
529
530 /** @brief Compute the right channel volume */
531 static double right(double v, double b) {
532   if(b > 0)                             /* volume = right */
533     return v;
534   else                                  /* volume = left */
535     return v * (1 + b);
536 }
537
538 /** @brief Compute the overall volume */
539 static double volume(double l, double r) {
540   return max(l, r);
541 }
542
543 /** @brief Compute the balance */
544 static double balance(double l, double r) {
545   if(l > r)
546     return r / l - 1;
547   else if(r > l)
548     return 1 - l / r;
549   else                                  /* left = right */
550     return 0;
551 }
552
553 /** @brief Called to enable 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 enable_rtp(disorder_eclient attribute((unused)) *c,
559                       disorder_eclient_no_response attribute((unused)) *completed,
560                       void attribute((unused)) *v) {
561   start_rtp();
562   return 0;
563 }
564
565 /** @brief Called to disable RTP play
566  *
567  * Rather odd signature is to fit in with the other icons which all call @ref
568  * lib/eclient.h functions.
569  */
570 static int disable_rtp(disorder_eclient attribute((unused)) *c,
571                        disorder_eclient_no_response attribute((unused)) *completed,
572                        void attribute((unused)) *v) {
573   stop_rtp();
574   return 0;
575 }
576
577 /*
578 Local Variables:
579 c-basic-offset:2
580 comment-column:40
581 fill-column:79
582 indent-tabs-mode:nil
583 End:
584 */