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