chiark / gitweb /
Switch Disobedience reset (i.e. fresh login) notification over to
[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 }
143
144 static int scratch_sensitive(void) {
145   return !!(last_state & DISORDER_PLAYING);
146 }
147
148 static int random_enabled(void) {
149   return !!(last_state & DISORDER_RANDOM_ENABLED);
150 }
151
152 static int playing_enabled(void) {
153   return !!(last_state & DISORDER_PLAYING_ENABLED);
154 }
155
156 static int rtp_enabled(void) {
157   return rtp_is_running;
158 }
159
160 static int rtp_sensitive(void) {
161   return rtp_supported;
162 }
163
164 /** @brief Table of all icons */
165 static struct icon icons[] = {
166   {
167     icon_on: "pause.png",
168     tip_on: "Pause playing track",
169     icon_off: "play.png",
170     tip_off: "Resume playing track",
171     menuitem: "<GdisorderMain>/Control/Playing",
172     on: pause_resume_on,
173     sensitive: pause_resume_sensitive,
174     action_go_on: disorder_eclient_resume,
175     action_go_off: disorder_eclient_pause,
176   },
177   {
178     icon_on: "cross.png",
179     tip_on: "Cancel playing track",
180     menuitem: "<GdisorderMain>/Control/Scratch",
181     sensitive: scratch_sensitive,
182     action_go_off: disorder_eclient_scratch_playing,
183   },
184   {
185     icon_on: "randomcross.png",
186     tip_on: "Disable random play",
187     icon_off: "random.png",
188     tip_off: "Enable random play",
189     menuitem: "<GdisorderMain>/Control/Random play",
190     on: random_enabled,
191     action_go_on: disorder_eclient_random_enable,
192     action_go_off: disorder_eclient_random_disable,
193   },
194   {
195     icon_on: "notescross.png",
196     tip_on: "Disable play",
197     icon_off: "notes.png",
198     tip_off: "Enable play",
199     on: playing_enabled,
200     action_go_on: disorder_eclient_enable,
201     action_go_off: disorder_eclient_disable,
202   },
203   {
204     icon_on: "speakercross.png",
205     tip_on: "Stop playing network stream",
206     icon_off: "speaker.png",
207     tip_off: "Play network stream",
208     menuitem: "<GdisorderMain>/Control/Network player",
209     on: rtp_enabled,
210     sensitive: rtp_sensitive,
211     action_go_on: enable_rtp,
212     action_go_off: disable_rtp
213   },
214 };
215
216 /** @brief Count of icons */
217 #define NICONS (int)(sizeof icons / sizeof *icons)
218
219 static GtkAdjustment *volume_adj;
220 static GtkAdjustment *balance_adj;
221 static GtkWidget *volume_widget;
222 static GtkWidget *balance_widget;
223
224 /** @brief Called whenever last_state changes in any way
225  *
226  * TODO we should only update things that have changed.
227  */
228 static void control_changed(const char attribute((unused)) *event,
229                             void attribute((unused)) *evendata,
230                             void attribute((unused)) *callbackdata) {
231   int n;
232   gboolean volume_supported;
233
234   D(("control_changed"));
235   /* Update all the icons */
236   for(n = 0; n < NICONS; ++n)
237     update_icon(&icons[n]);
238   /* Only display volume/balance controls if they will work */
239   if(!rtp_supported
240      || (rtp_supported && mixer_supported(DEFAULT_BACKEND)))
241     volume_supported = TRUE;
242   else
243     volume_supported = FALSE;
244   (volume_supported ? gtk_widget_show : gtk_widget_hide)(volume_widget);
245   (volume_supported ? gtk_widget_show : gtk_widget_hide)(balance_widget);
246 }
247
248 /** @brief Create the control bar */
249 GtkWidget *control_widget(void) {
250   GtkWidget *hbox = gtk_hbox_new(FALSE, 1), *vbox;
251   int n;
252
253   NW(hbox);
254   D(("control_widget"));
255   assert(mainmenufactory);              /* ordering must be right */
256   for(n = 0; n < NICONS; ++n) {
257     /* Create the button */
258     NW(button);
259     icons[n].button = gtk_button_new();
260     gtk_widget_set_style(icons[n].button, tool_style);
261     icons[n].image_on = gtk_image_new_from_pixbuf(find_image(icons[n].icon_on));
262     gtk_widget_set_style(icons[n].image_on, tool_style);
263     g_object_ref(icons[n].image_on);
264     /* If it's a toggle icon, create the 'off' half too */
265     if(icons[n].icon_off) {
266       icons[n].image_off = gtk_image_new_from_pixbuf(find_image(icons[n].icon_off));
267       gtk_widget_set_style(icons[n].image_off, tool_style);
268       g_object_ref(icons[n].image_off);
269     }
270     g_signal_connect(G_OBJECT(icons[n].button), "clicked",
271                      G_CALLBACK(clicked_icon), &icons[n]);
272     /* pop the icon in a vbox so it doesn't get vertically stretch if there are
273      * taller things in the control bar */
274     NW(vbox);
275     vbox = gtk_vbox_new(FALSE, 0);
276     gtk_box_pack_start(GTK_BOX(vbox), icons[n].button, TRUE, FALSE, 0);
277     gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
278     if(icons[n].menuitem) {
279       /* Find the menu item */
280       icons[n].item = gtk_item_factory_get_widget(mainmenufactory,
281                                                   icons[n].menuitem);
282       if(icons[n].icon_off)
283         g_signal_connect(G_OBJECT(icons[n].item), "toggled",
284                          G_CALLBACK(toggled_menu), &icons[n]);
285       else
286         g_signal_connect(G_OBJECT(icons[n].item), "activate",
287                          G_CALLBACK(clicked_menu), &icons[n]);
288     }
289   }
290   /* create the adjustments for the volume control */
291   NW(adjustment);
292   volume_adj = GTK_ADJUSTMENT(gtk_adjustment_new(0, 0, goesupto,
293                                                  goesupto / 20, goesupto / 20,
294                                                  0));
295   NW(adjustment);
296   balance_adj = GTK_ADJUSTMENT(gtk_adjustment_new(0, -1, 1,
297                                                   0.2, 0.2, 0));
298   /* the volume control */
299   NW(hscale);
300   volume_widget = gtk_hscale_new(volume_adj);
301   NW(hscale);
302   balance_widget = gtk_hscale_new(balance_adj);
303   gtk_widget_set_style(volume_widget, tool_style);
304   gtk_widget_set_style(balance_widget, tool_style);
305   gtk_scale_set_digits(GTK_SCALE(volume_widget), 10);
306   gtk_scale_set_digits(GTK_SCALE(balance_widget), 10);
307   gtk_widget_set_size_request(volume_widget, 192, -1);
308   gtk_widget_set_size_request(balance_widget, 192, -1);
309   gtk_tooltips_set_tip(tips, volume_widget, "Volume", "");
310   gtk_tooltips_set_tip(tips, balance_widget, "Balance", "");
311   gtk_box_pack_start(GTK_BOX(hbox), volume_widget, FALSE, TRUE, 0);
312   gtk_box_pack_start(GTK_BOX(hbox), balance_widget, FALSE, TRUE, 0);
313   /* space updates rather than hammering the server */
314   gtk_range_set_update_policy(GTK_RANGE(volume_widget), GTK_UPDATE_DELAYED);
315   gtk_range_set_update_policy(GTK_RANGE(balance_widget), GTK_UPDATE_DELAYED);
316   /* notice when the adjustments are changed */
317   g_signal_connect(G_OBJECT(volume_adj), "value-changed",
318                    G_CALLBACK(volume_adjusted), 0);
319   g_signal_connect(G_OBJECT(balance_adj), "value-changed",
320                    G_CALLBACK(volume_adjusted), 0);
321   /* format the volume/balance values ourselves */
322   g_signal_connect(G_OBJECT(volume_widget), "format-value",
323                    G_CALLBACK(format_volume), 0);
324   g_signal_connect(G_OBJECT(balance_widget), "format-value",
325                    G_CALLBACK(format_balance), 0);
326   event_register("enabled-changed", control_changed, 0);
327   event_register("random-changed", control_changed, 0);
328   event_register("pause-changed", control_changed, 0);
329   event_register("playing-changed", control_changed, 0);
330   event_register("rtp-changed", control_changed, 0);
331   event_register("volume-changed", volume_changed, 0);
332   return hbox;
333 }
334
335 /** @brief Update the volume control when it changes */
336 static void volume_changed(const char attribute((unused)) *event,
337                            void attribute((unused)) *eventdata,
338                            void attribute((unused)) *callbackdata) {
339   double l, r;
340
341   D(("volume_changed"));
342   l = volume_l / 100.0;
343   r = volume_r / 100.0;
344   ++suppress_actions;
345   gtk_adjustment_set_value(volume_adj, volume(l, r) * goesupto);
346   gtk_adjustment_set_value(balance_adj, balance(l, r));
347   --suppress_actions;
348 }
349
350 /** @brief Update the state of one of the control icons
351  * @param icon Target icon
352  */
353 static void update_icon(const struct icon *icon) {
354   int on = icon->on ? icon->on() : 1;
355   int sensitive = icon->sensitive ? icon->sensitive() : 1;
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   /* 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(icon->tip_on)
372     gtk_tooltips_set_tip(tips, icon->button,
373                            on ? icon->tip_on : icon->tip_off, "");
374   gtk_widget_set_sensitive(icon->button, sensitive);
375   /* Icons with an associated menu item */
376   if(icon->item) {
377     if(icon->icon_off)
378       gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(icon->item), on);
379     gtk_widget_set_sensitive(icon->item, sensitive);
380   }
381   --suppress_actions;
382 }
383
384 static void icon_action_completed(void attribute((unused)) *v,
385                                   const char *error) {
386   if(error)
387     popup_protocol_error(0, error);
388 }
389
390 static void clicked_icon(GtkButton attribute((unused)) *button,
391                          gpointer userdata) {
392   const struct icon *icon = userdata;
393
394   if(suppress_actions)
395     return;
396   if(!icon->on || icon->on())
397     icon->action_go_off(client, icon_action_completed, 0);
398   else
399     icon->action_go_on(client, icon_action_completed, 0);
400 }
401
402 static void clicked_menu(GtkMenuItem attribute((unused)) *menuitem,
403                          gpointer userdata) {
404   clicked_icon(NULL, userdata);
405 }
406
407 static void toggled_menu(GtkCheckMenuItem attribute((unused)) *menuitem,
408                          gpointer userdata) {
409   clicked_icon(NULL, userdata);
410 }
411
412 /** @brief Called when a volume command completes */
413 static void volume_completed(void attribute((unused)) *v,
414                              const char *error,
415                              int attribute((unused)) l,
416                              int attribute((unused)) r) {
417   if(error)
418     popup_protocol_error(0, error);
419   /* We don't set the UI's notion of the volume here, it is set from the log
420    * regardless of the reason it changed */
421 }
422
423 /** @brief Called when the volume has been adjusted */
424 static void volume_adjusted(GtkAdjustment attribute((unused)) *a,
425                             gpointer attribute((unused)) user_data) {
426   double v = gtk_adjustment_get_value(volume_adj) / goesupto;
427   double b = gtk_adjustment_get_value(balance_adj);
428
429   if(suppress_actions)
430     /* This is the result of an update from the server, not a change from the
431      * user.  Don't feedback! */
432     return;
433   D(("volume_adjusted"));
434   /* force to 'stereotypical' values */
435   v = nearbyint(100 * v) / 100;
436   b = nearbyint(5 * b) / 5;
437   /* Set the volume.  We don't want a reply, we'll get the actual new volume
438    * from the log. */
439   if(rtp_supported) {
440     int l = nearbyint(left(v, b) * 100), r = nearbyint(right(v, b) * 100);
441     mixer_control(DEFAULT_BACKEND, &l, &r, 1);
442   } else
443     disorder_eclient_volume(client, volume_completed,
444                             nearbyint(left(v, b) * 100),
445                             nearbyint(right(v, b) * 100),
446                             0);
447 }
448
449 /** @brief Formats the volume value */
450 static gchar *format_volume(GtkScale attribute((unused)) *scale,
451                             gdouble value) {
452   char s[32];
453
454   snprintf(s, sizeof s, "%.1f", (double)value);
455   return g_strdup(s);
456 }
457
458 /** @brief Formats the balance value */
459 static gchar *format_balance(GtkScale attribute((unused)) *scale,
460                              gdouble value) {
461   char s[32];
462
463   if(fabs(value) < 0.1)
464     return g_strdup("0");
465   snprintf(s, sizeof s, "%+.1f", (double)value);
466   return g_strdup(s);
467 }
468
469 /* Volume mapping.  We consider left, right, volume to be in [0,1]
470  * and balance to be in [-1,1].
471  * 
472  * First, we just have volume = max(left, right).
473  *
474  * Balance we consider to linearly represent the amount by which the quieter
475  * channel differs from the louder.  In detail:
476  *
477  *  if right > left then balance > 0:
478  *   balance = 0 => left = right  (as an endpoint, not an instance)
479  *   balance = 1 => left = 0
480  *   fitting to linear, left = right * (1 - balance)
481  *                so balance = 1 - left / right
482  *   (right > left => right > 0 so no division by 0.)
483  * 
484  *  if left > right then balance < 0:
485  *   balance = 0 => right = left  (same caveat as above)
486  *   balance = -1 => right = 0
487  *   again fitting to linear, right = left * (1 + balance)
488  *                       so balance = right / left - 1
489  *   (left > right => left > 0 so no division by 0.)
490  *
491  *  if left = right then we just have balance = 0.
492  *
493  * Thanks to Clive and Andrew.
494  */
495
496 /** @brief Return the greater of @p x and @p y */
497 static double max(double x, double y) {
498   return x > y ? x : y;
499 }
500
501 /** @brief Compute the left channel volume */
502 static double left(double v, double b) {
503   if(b > 0)                             /* volume = right */
504     return v * (1 - b);
505   else                                  /* volume = left */
506     return v;
507 }
508
509 /** @brief Compute the right channel volume */
510 static double right(double v, double b) {
511   if(b > 0)                             /* volume = right */
512     return v;
513   else                                  /* volume = left */
514     return v * (1 + b);
515 }
516
517 /** @brief Compute the overall volume */
518 static double volume(double l, double r) {
519   return max(l, r);
520 }
521
522 /** @brief Compute the balance */
523 static double balance(double l, double r) {
524   if(l > r)
525     return r / l - 1;
526   else if(r > l)
527     return 1 - l / r;
528   else                                  /* left = right */
529     return 0;
530 }
531
532 /** @brief Called to enable RTP play
533  *
534  * Rather odd signature is to fit in with the other icons which all call @ref
535  * lib/eclient.h functions.
536  */
537 static int enable_rtp(disorder_eclient attribute((unused)) *c,
538                       disorder_eclient_no_response attribute((unused)) *completed,
539                       void attribute((unused)) *v) {
540   start_rtp();
541   return 0;
542 }
543
544 /** @brief Called to disable RTP play
545  *
546  * Rather odd signature is to fit in with the other icons which all call @ref
547  * lib/eclient.h functions.
548  */
549 static int disable_rtp(disorder_eclient attribute((unused)) *c,
550                        disorder_eclient_no_response attribute((unused)) *completed,
551                        void attribute((unused)) *v) {
552   stop_rtp();
553   return 0;
554 }
555
556 /*
557 Local Variables:
558 c-basic-offset:2
559 comment-column:40
560 fill-column:79
561 indent-tabs-mode:nil
562 End:
563 */