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