chiark / gitweb /
Doxygen-clean
[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 #include "mixer.h"
24
25 /* Forward declarations ---------------------------------------------------- */
26
27 struct icon;
28
29 static void clicked_icon(GtkButton *, gpointer);
30 static void clicked_menu(GtkMenuItem *, gpointer userdata);
31 static void toggled_menu(GtkCheckMenuItem *, gpointer userdata);
32
33 static int enable_rtp(disorder_eclient *c,
34                       disorder_eclient_no_response *completed,
35                       void *v);
36 static int disable_rtp(disorder_eclient *c,
37                        disorder_eclient_no_response *completed,
38                        void *v);
39
40 static double left(double v, double b);
41 static double right(double v, double b);
42 static double volume(double l, double r);
43 static double balance(double l, double r);
44
45 static void volume_adjusted(GtkAdjustment *a, gpointer user_data);
46 static gchar *format_volume(GtkScale *scale, gdouble value);
47 static gchar *format_balance(GtkScale *scale, gdouble value);
48
49 static void icon_changed(const char *event,
50                          void *evendata,
51                          void *callbackdata);
52 static void volume_changed(const char *event,
53                            void *eventdata,
54                            void *callbackdata);
55
56 /* Control bar ------------------------------------------------------------- */
57
58 /** @brief Guard against feedback */
59 int suppress_actions = 1;
60
61 /** @brief Definition of an icon
62  *
63  * We have two kinds of icon:
64  * - action icons, which just do something but don't have a state as such
65  * - toggle icons, which toggle between two states ("on" and "off").
66  *
67  * The scratch button is an action icon; currently all the others are toggle
68  * icons.
69  *
70  * (All icons can be sensitive or insensitive, separately to the above.)
71  */
72 struct icon {
73   /** @brief Filename for 'on' image */
74   const char *icon_on;
75
76   /** @brief Text for 'on' tooltip */
77   const char *tip_on;
78
79   /** @brief Filename for 'off' image or NULL for an action icon */
80   const char *icon_off;
81
82   /** @brief Text for 'off tooltip */
83   const char *tip_off;
84
85   /** @brief Associated menu item or NULL */
86   const char *menuitem;
87
88   /** @brief Events that change this icon, separated by spaces */
89   const char *events;
90
91   /** @brief @ref eclient.h function to call to go from off to on
92    *
93    * For action buttons, this should be NULL.
94    */
95   int (*action_go_on)(disorder_eclient *c,
96                       disorder_eclient_no_response *completed,
97                       void *v);
98
99   /** @brief @ref eclient.h function to call to go from on to off
100    *
101    * For action buttons, this action is used.
102    */
103   int (*action_go_off)(disorder_eclient *c,
104                        disorder_eclient_no_response *completed,
105                        void *v);
106
107   /** @brief Get button state
108    * @return 1 for on, 0 for off
109    */
110   int (*on)(void);
111
112   /** @brief Get button sensitivity
113    * @return 1 for sensitive, 0 for insensitive
114    *
115    * Can be NULL for always sensitive.
116    */
117   int (*sensitive)(void);
118   
119   /** @brief Pointer to button */
120   GtkWidget *button;
121
122   /** @brief Pointer to menu item */
123   GtkWidget *item;
124
125   GtkWidget *image_on;
126   GtkWidget *image_off;
127 };
128
129 static int pause_resume_on(void) {
130   return !(last_state & DISORDER_TRACK_PAUSED);
131 }
132
133 static int pause_resume_sensitive(void) {
134   return !!(last_state & DISORDER_PLAYING)
135     && (last_rights & RIGHT_PAUSE);
136 }
137
138 static int scratch_sensitive(void) {
139   return !!(last_state & DISORDER_PLAYING)
140     && right_scratchable(last_rights, config->username, playing_track);
141 }
142
143 static int random_sensitive(void) {
144   return !!(last_rights & RIGHT_GLOBAL_PREFS);
145 }
146
147 static int random_enabled(void) {
148   return !!(last_state & DISORDER_RANDOM_ENABLED);
149 }
150
151 static int playing_sensitive(void) {
152   return !!(last_rights & RIGHT_GLOBAL_PREFS);
153 }
154
155 static int playing_enabled(void) {
156   return !!(last_state & DISORDER_PLAYING_ENABLED);
157 }
158
159 static int rtp_enabled(void) {
160   return rtp_is_running;
161 }
162
163 static int rtp_sensitive(void) {
164   return rtp_supported;
165 }
166
167 /** @brief Table of all icons */
168 static struct icon icons[] = {
169   {
170     icon_on: "pause32.png",
171     tip_on: "Pause playing track",
172     icon_off: "play32.png",
173     tip_off: "Resume playing track",
174     menuitem: "<GdisorderMain>/Control/Playing",
175     on: pause_resume_on,
176     sensitive: pause_resume_sensitive,
177     action_go_on: disorder_eclient_resume,
178     action_go_off: disorder_eclient_pause,
179     events: "pause-changed playing-changed rights-changed",
180   },
181   {
182     icon_on: "cross32.png",
183     tip_on: "Cancel playing track",
184     menuitem: "<GdisorderMain>/Control/Scratch",
185     sensitive: scratch_sensitive,
186     action_go_off: disorder_eclient_scratch_playing,
187     events: "playing-track-changed rights-changed",
188   },
189   {
190     icon_on: "randomenabled32.png",
191     tip_on: "Disable random play",
192     icon_off: "randomdisabled32.png",
193     tip_off: "Enable random play",
194     menuitem: "<GdisorderMain>/Control/Random play",
195     on: random_enabled,
196     sensitive: random_sensitive,
197     action_go_on: disorder_eclient_random_enable,
198     action_go_off: disorder_eclient_random_disable,
199     events: "random-changed rights-changed",
200   },
201   {
202     icon_on: "playenabled32.png",
203     tip_on: "Disable play",
204     icon_off: "playdisabled32.png",
205     tip_off: "Enable play",
206     on: playing_enabled,
207     sensitive: playing_sensitive,
208     action_go_on: disorder_eclient_enable,
209     action_go_off: disorder_eclient_disable,
210     events: "enabled-changed rights-changed",
211   },
212   {
213     icon_on: "rtpenabled32.png",
214     tip_on: "Stop playing network stream",
215     icon_off: "rtpdisabled32.png",
216     tip_off: "Play network stream",
217     menuitem: "<GdisorderMain>/Control/Network player",
218     on: rtp_enabled,
219     sensitive: rtp_sensitive,
220     action_go_on: enable_rtp,
221     action_go_off: disable_rtp,
222     events: "rtp-changed",
223   },
224 };
225
226 /** @brief Count of icons */
227 #define NICONS (int)(sizeof icons / sizeof *icons)
228
229 static GtkAdjustment *volume_adj;
230 static GtkAdjustment *balance_adj;
231 static GtkWidget *volume_widget;
232 static GtkWidget *balance_widget;
233
234 /** @brief Create the control bar */
235 GtkWidget *control_widget(void) {
236   GtkWidget *hbox = gtk_hbox_new(FALSE, 1), *vbox;
237   int n;
238
239   D(("control_widget"));
240   assert(mainmenufactory);              /* ordering must be right */
241   for(n = 0; n < NICONS; ++n) {
242     /* Create the button */
243     icons[n].button = gtk_button_new();
244     gtk_widget_set_style(icons[n].button, tool_style);
245     icons[n].image_on = gtk_image_new_from_pixbuf(find_image(icons[n].icon_on));
246     gtk_widget_set_style(icons[n].image_on, tool_style);
247     g_object_ref(icons[n].image_on);
248     /* If it's a toggle icon, create the 'off' half too */
249     if(icons[n].icon_off) {
250       icons[n].image_off = gtk_image_new_from_pixbuf(find_image(icons[n].icon_off));
251       gtk_widget_set_style(icons[n].image_off, tool_style);
252       g_object_ref(icons[n].image_off);
253     }
254     g_signal_connect(G_OBJECT(icons[n].button), "clicked",
255                      G_CALLBACK(clicked_icon), &icons[n]);
256     /* pop the icon in a vbox so it doesn't get vertically stretch if there are
257      * taller things in the control bar */
258     vbox = gtk_vbox_new(FALSE, 0);
259     gtk_box_pack_start(GTK_BOX(vbox), icons[n].button, TRUE, FALSE, 0);
260     gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
261     if(icons[n].menuitem) {
262       /* Find the menu item */
263       icons[n].item = gtk_item_factory_get_widget(mainmenufactory,
264                                                   icons[n].menuitem);
265       if(icons[n].icon_off)
266         g_signal_connect(G_OBJECT(icons[n].item), "toggled",
267                          G_CALLBACK(toggled_menu), &icons[n]);
268       else
269         g_signal_connect(G_OBJECT(icons[n].item), "activate",
270                          G_CALLBACK(clicked_menu), &icons[n]);
271     }
272     /* Make sure the icon is updated when relevant things changed */
273     char **events = split(icons[n].events, 0, 0, 0, 0);
274     while(*events)
275       event_register(*events++, icon_changed, &icons[n]);
276     event_register("connected-changed", icon_changed, &icons[n]);
277   }
278   /* create the adjustments for the volume control */
279   volume_adj = GTK_ADJUSTMENT(gtk_adjustment_new(0, 0, goesupto,
280                                                  goesupto / 20, goesupto / 20,
281                                                  0));
282   balance_adj = GTK_ADJUSTMENT(gtk_adjustment_new(0, -1, 1,
283                                                   0.2, 0.2, 0));
284   /* the volume control */
285   volume_widget = gtk_hscale_new(volume_adj);
286   balance_widget = gtk_hscale_new(balance_adj);
287   gtk_widget_set_style(volume_widget, tool_style);
288   gtk_widget_set_style(balance_widget, tool_style);
289   gtk_scale_set_digits(GTK_SCALE(volume_widget), 10);
290   gtk_scale_set_digits(GTK_SCALE(balance_widget), 10);
291   gtk_widget_set_size_request(volume_widget, 192, -1);
292   gtk_widget_set_size_request(balance_widget, 192, -1);
293   gtk_tooltips_set_tip(tips, volume_widget, "Volume", "");
294   gtk_tooltips_set_tip(tips, balance_widget, "Balance", "");
295   gtk_box_pack_start(GTK_BOX(hbox), volume_widget, FALSE, TRUE, 0);
296   gtk_box_pack_start(GTK_BOX(hbox), balance_widget, FALSE, TRUE, 0);
297   /* space updates rather than hammering the server */
298   gtk_range_set_update_policy(GTK_RANGE(volume_widget), GTK_UPDATE_DELAYED);
299   gtk_range_set_update_policy(GTK_RANGE(balance_widget), GTK_UPDATE_DELAYED);
300   /* notice when the adjustments are changed */
301   g_signal_connect(G_OBJECT(volume_adj), "value-changed",
302                    G_CALLBACK(volume_adjusted), 0);
303   g_signal_connect(G_OBJECT(balance_adj), "value-changed",
304                    G_CALLBACK(volume_adjusted), 0);
305   /* format the volume/balance values ourselves */
306   g_signal_connect(G_OBJECT(volume_widget), "format-value",
307                    G_CALLBACK(format_volume), 0);
308   g_signal_connect(G_OBJECT(balance_widget), "format-value",
309                    G_CALLBACK(format_balance), 0);
310   event_register("volume-changed", volume_changed, 0);
311   event_register("rtp-changed", volume_changed, 0);
312   return hbox;
313 }
314
315 /** @brief Update the volume control when it changes */
316 static void volume_changed(const char attribute((unused)) *event,
317                            void attribute((unused)) *eventdata,
318                            void attribute((unused)) *callbackdata) {
319   double l, r;
320   gboolean volume_supported;
321
322   D(("volume_changed"));
323   ++suppress_actions;
324   /* Only display volume/balance controls if they will work */
325   if(!rtp_supported
326      || (rtp_supported && mixer_supported(DEFAULT_BACKEND)))
327     volume_supported = TRUE;
328   else
329     volume_supported = FALSE;
330   /* TODO: if the server doesn't know how to set the volume [but isn't using
331    * network play] then we should have volume_supported = FALSE */
332   if(volume_supported) {
333     gtk_widget_show(volume_widget);
334     gtk_widget_show(balance_widget);
335     l = volume_l / 100.0;
336     r = volume_r / 100.0;
337     gtk_adjustment_set_value(volume_adj, volume(l, r) * goesupto);
338     gtk_adjustment_set_value(balance_adj, balance(l, r));
339   } else {
340     gtk_widget_hide(volume_widget);
341     gtk_widget_hide(balance_widget);
342   }
343   --suppress_actions;
344 }
345
346 /** @brief Update the state of one of the control icons
347  */
348 static void icon_changed(const char attribute((unused)) *event,
349                          void attribute((unused)) *evendata,
350                          void *callbackdata) {
351   //fprintf(stderr, "icon_changed (%s)\n", event);
352   const struct icon *const icon = callbackdata;
353   int on = icon->on ? icon->on() : 1;
354   int sensitive = icon->sensitive ? icon->sensitive() : 1;
355   //fprintf(stderr, "sensitive->%d\n", sensitive);
356   GtkWidget *child, *newchild;
357
358   ++suppress_actions;
359   /* If the connection is down nothing is ever usable */
360   if(!(last_state & DISORDER_CONNECTED))
361     sensitive = 0;
362   //fprintf(stderr, "(checked connected) sensitive->%d\n", sensitive);
363   /* Replace the child */
364   newchild = on ? icon->image_on : icon->image_off;
365   child = gtk_bin_get_child(GTK_BIN(icon->button));
366   if(child != newchild) {
367     if(child)
368       gtk_container_remove(GTK_CONTAINER(icon->button), child);
369     gtk_container_add(GTK_CONTAINER(icon->button), newchild);
370     gtk_widget_show(newchild);
371   }
372   /* If you disable play or random play NOT via the icon (for instance, via the
373    * edit menu or via a completely separate command line invocation) then the
374    * icon shows up as insensitive.  Hover the mouse over it and the correct
375    * state is immediately displayed.  sensitive and GTK_WIDGET_SENSITIVE show
376    * it to be in the correct state, so I think this is may be a GTK+ bug. */
377   if(icon->tip_on)
378     gtk_tooltips_set_tip(tips, icon->button,
379                            on ? icon->tip_on : icon->tip_off, "");
380   gtk_widget_set_sensitive(icon->button, sensitive);
381   /* Icons with an associated menu item */
382   if(icon->item) {
383     if(icon->icon_off)
384       gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(icon->item), on);
385     gtk_widget_set_sensitive(icon->item, sensitive);
386   }
387   --suppress_actions;
388 }
389
390 static void icon_action_completed(void attribute((unused)) *v,
391                                   const char *err) {
392   if(err)
393     popup_protocol_error(0, err);
394 }
395
396 static void clicked_icon(GtkButton attribute((unused)) *button,
397                          gpointer userdata) {
398   const struct icon *icon = userdata;
399
400   if(suppress_actions)
401     return;
402   if(!icon->on || icon->on())
403     icon->action_go_off(client, icon_action_completed, 0);
404   else
405     icon->action_go_on(client, icon_action_completed, 0);
406 }
407
408 static void clicked_menu(GtkMenuItem attribute((unused)) *menuitem,
409                          gpointer userdata) {
410   clicked_icon(NULL, userdata);
411 }
412
413 static void toggled_menu(GtkCheckMenuItem attribute((unused)) *menuitem,
414                          gpointer userdata) {
415   clicked_icon(NULL, userdata);
416 }
417
418 /** @brief Called when a volume command completes */
419 static void volume_completed(void attribute((unused)) *v,
420                              const char *err,
421                              int attribute((unused)) l,
422                              int attribute((unused)) r) {
423   if(err)
424     popup_protocol_error(0, err);
425   /* We don't set the UI's notion of the volume here, it is set from the log
426    * regardless of the reason it changed */
427 }
428
429 /** @brief Called when the volume has been adjusted */
430 static void volume_adjusted(GtkAdjustment attribute((unused)) *a,
431                             gpointer attribute((unused)) user_data) {
432   double v = gtk_adjustment_get_value(volume_adj) / goesupto;
433   double b = gtk_adjustment_get_value(balance_adj);
434
435   if(suppress_actions)
436     /* This is the result of an update from the server, not a change from the
437      * user.  Don't feedback! */
438     return;
439   D(("volume_adjusted"));
440   /* force to 'stereotypical' values */
441   v = nearbyint(100 * v) / 100;
442   b = nearbyint(5 * b) / 5;
443   /* Set the volume.  We don't want a reply, we'll get the actual new volume
444    * from the log. */
445   if(rtp_supported) {
446     int l = nearbyint(left(v, b) * 100), r = nearbyint(right(v, b) * 100);
447     mixer_control(DEFAULT_BACKEND, &l, &r, 1);
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 */