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