chiark / gitweb /
propagate button in track properties popup
[disorder] / disobedience / control.c
index 5e2e0d32c8828992b3846b9b13c9706805be321a..f4541f73be55e30f459d5cb2623db97ad83b62cf 100644 (file)
@@ -17,6 +17,9 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  * USA
  */
+/** @file disobedience/control.c
+ * @brief Volume control and buttons
+ */
 
 #include "disobedience.h"
 
@@ -25,7 +28,6 @@
 WT(adjustment);
 WT(hscale);
 WT(hbox);
-WT(tooltips);
 WT(button);
 WT(image);
 WT(label);
@@ -53,19 +55,38 @@ static gchar *format_balance(GtkScale *scale, gdouble value);
 
 /* Control bar ------------------------------------------------------------- */
 
+/** @brief Guard against feedback loop in volume control */
 static int suppress_set_volume;
-/* Guard against feedback loop in volume control */
 
-static struct icon {
+/** @brief Definition of an icon
+ *
+ * The design here is rather mad: rather than changing the image displayed by
+ * icons according to their state, we flip the visibility of pairs of icons.
+ */
+struct icon {
+  /** @brief Filename for image */
   const char *icon;
+
+  /** @brief Text for tooltip */
   const char *tip;
+
+  /** @brief Called when button is clicked (activated) */
   void (*clicked)(GtkButton *button, gpointer userdata);
+
+  /** @brief Called to update button when state may have changed */
   void (*update)(const struct icon *i);
+
+  /** @brief @ref eclient.h function to call */
   int (*action)(disorder_eclient *c,
                 disorder_eclient_no_response *completed,
                 void *v);
+
+  /** @brief Pointer to button */
   GtkWidget *button;
-} icons[] = {
+};
+
+/** @brief Table of all icons */
+static struct icon icons[] = {
   { "pause.png", "Pause playing track", clicked_icon, update_pause,
     disorder_eclient_pause, 0 },
   { "play.png", "Resume playing track", clicked_icon, update_play,
@@ -81,21 +102,31 @@ static struct icon {
   { "notescross.png", "Disable play", clicked_icon, update_disable,
     disorder_eclient_disable, 0 },
 };
+
+/** @brief Count of icons */
 #define NICONS (int)(sizeof icons / sizeof *icons)
 
-GtkAdjustment *volume_adj, *balance_adj;
+static GtkAdjustment *volume_adj;
+static GtkAdjustment *balance_adj;
 
-/* Create the control bar */
+/** @brief Called whenever last_state changes in any way */
+static void control_monitor(void attribute((unused)) *u) {
+  int n;
+
+  D(("control_monitor"));
+  for(n = 0; n < NICONS; ++n)
+    icons[n].update(&icons[n]);
+}
+
+/** @brief Create the control bar */
 GtkWidget *control_widget(void) {
   GtkWidget *hbox = gtk_hbox_new(FALSE, 1), *vbox;
   GtkWidget *content;
   GdkPixbuf *pb;
   GtkWidget *v, *b;
-  GtkTooltips *tips = gtk_tooltips_new();
   int n;
 
   NW(hbox);
-  NW(tooltips);
   D(("control_widget"));
   for(n = 0; n < NICONS; ++n) {
     NW(button);
@@ -152,71 +183,86 @@ GtkWidget *control_widget(void) {
                    G_CALLBACK(format_volume), 0);
   g_signal_connect(G_OBJECT(b), "format-value",
                    G_CALLBACK(format_balance), 0);
+  register_monitor(control_monitor, 0, -1UL);
   return hbox;
 }
 
-/* Update the control bar after some kind of state change */
-void control_update(void) {
-  int n;
+/** @brief Update the volume control when it changes */
+void volume_update(void) {
   double l, r;
 
-  D(("control_update"));
-  for(n = 0; n < NICONS; ++n)
-    icons[n].update(&icons[n]);
+  D(("volume_update"));
   l = volume_l / 100.0;
   r = volume_r / 100.0;
-  ++suppress_set_volume;;
+  ++suppress_set_volume;
   gtk_adjustment_set_value(volume_adj, volume(l, r) * goesupto);
   gtk_adjustment_set_value(balance_adj, balance(l, r));
   --suppress_set_volume;
 }
 
-static void update_icon(GtkWidget *button, 
-                        int visible, int attribute((unused)) usable) {
-  (visible ? gtk_widget_show : gtk_widget_hide)(button);
-  /* TODO: show usability */
+/** @brief Update the state of one of the control icons
+ * @param icon Target icon
+ * @param visible True if this version of the button should be visible
+ * @param usable True if the button is currently usable
+ *
+ * Several of the icons, rather bizarrely, come in pairs: for instance exactly
+ * one of the play and pause buttons is supposed to be visible at any given
+ * moment.
+ *
+ * @p usable need not take into account server availability, that is done
+ * automatically.
+ */
+static void update_icon(const struct icon *icon,
+                        int visible, int usable) {
+  /* If the connection is down nothing is ever usable */
+  if(!(last_state & DISORDER_CONNECTED))
+    usable = 0;
+  (visible ? gtk_widget_show : gtk_widget_hide)(icon->button);
+  /* Only both updating usability if the button is visible */
+  if(visible)
+    gtk_widget_set_sensitive(icon->button, usable);
 }
 
 static void update_pause(const struct icon *icon) {
-  int visible = !(last_state & DISORDER_TRACK_PAUSED);
-  int usable = playing;                 /* TODO: might be a lie */
-  update_icon(icon->button, visible, usable);
+  const int visible = !(last_state & DISORDER_TRACK_PAUSED);
+  const int usable = !!(last_state & DISORDER_PLAYING); /* TODO: might be a lie */
+  update_icon(icon, visible, usable);
 }
 
 static void update_play(const struct icon *icon) {
-  int visible = !!(last_state & DISORDER_TRACK_PAUSED);
-  int usable = playing;
-  update_icon(icon->button, visible, usable);
+  const int visible = !!(last_state & DISORDER_TRACK_PAUSED);
+  const int usable = !!(last_state & DISORDER_PLAYING);
+  update_icon(icon, visible, usable);
 }
 
 static void update_scratch(const struct icon *icon) {
-  int visible = 1;
-  int usable = playing;
-  update_icon(icon->button, visible, usable);
+  const int visible = 1;
+  const int usable = !!(last_state & DISORDER_PLAYING);
+  update_icon(icon, visible, usable);
 }
 
 static void update_random_enable(const struct icon *icon) {
-  int visible = !(last_state & DISORDER_RANDOM_ENABLED);
-  int usable = 1;
-  update_icon(icon->button, visible, usable);
+  const int visible = !(last_state & DISORDER_RANDOM_ENABLED);
+  const int usable = 1;
+  update_icon(icon, visible, usable);
 }
 
 static void update_random_disable(const struct icon *icon) {
-  int visible = !!(last_state & DISORDER_RANDOM_ENABLED);
-  int usable = 1;
-  update_icon(icon->button, visible, usable);
+  const int visible = !!(last_state & DISORDER_RANDOM_ENABLED);
+  const int usable = 1;
+  update_icon(icon, visible, usable);
 }
 
 static void update_enable(const struct icon *icon) {
-  int visible = !(last_state & DISORDER_PLAYING_ENABLED);
-  int usable = 1;
-  update_icon(icon->button, visible, usable);
+  const int visible = !(last_state & DISORDER_PLAYING_ENABLED);
+  const int usable = 1;
+  update_icon(icon, visible, usable);
 }
 
 static void update_disable(const struct icon *icon) {
-  int visible = !!(last_state & DISORDER_PLAYING_ENABLED);
-  int usable = 1;
-  update_icon(icon->button, visible, usable);
+  const int visible = !!(last_state & DISORDER_PLAYING_ENABLED);
+  const int usable = 1;
+  update_icon(icon, visible, usable);
 }
 
 static void clicked_icon(GtkButton attribute((unused)) *button,
@@ -226,6 +272,7 @@ static void clicked_icon(GtkButton attribute((unused)) *button,
   icon->action(client, 0, 0);
 }
 
+/** @brief Called when the volume has been adjusted */
 static void volume_adjusted(GtkAdjustment attribute((unused)) *a,
                             gpointer attribute((unused)) user_data) {
   double v = gtk_adjustment_get_value(volume_adj) / goesupto;
@@ -247,7 +294,7 @@ static void volume_adjusted(GtkAdjustment attribute((unused)) *a,
                           0);
 }
 
-/* Called to format the volume value */
+/** @brief Formats the volume value */
 static gchar *format_volume(GtkScale attribute((unused)) *scale,
                             gdouble value) {
   char s[32];
@@ -256,7 +303,7 @@ static gchar *format_volume(GtkScale attribute((unused)) *scale,
   return g_strdup(s);
 }
 
-/* Called to format the balance value. */
+/** @brief Formats the balance value */
 static gchar *format_balance(GtkScale attribute((unused)) *scale,
                              gdouble value) {
   char s[32];
@@ -294,10 +341,12 @@ static gchar *format_balance(GtkScale attribute((unused)) *scale,
  * Thanks to Clive and Andrew.
  */
 
+/** @brief Return the greater of @p x and @p y */
 static double max(double x, double y) {
   return x > y ? x : y;
 }
 
+/** @brief Compute the left channel volume */
 static double left(double v, double b) {
   if(b > 0)                             /* volume = right */
     return v * (1 - b);
@@ -305,6 +354,7 @@ static double left(double v, double b) {
     return v;
 }
 
+/** @brief Compute the right channel volume */
 static double right(double v, double b) {
   if(b > 0)                             /* volume = right */
     return v;
@@ -312,10 +362,12 @@ static double right(double v, double b) {
     return v * (1 + b);
 }
 
+/** @brief Compute the overall volume */
 static double volume(double l, double r) {
   return max(l, r);
 }
 
+/** @brief Compute the balance */
 static double balance(double l, double r) {
   if(l > r)
     return r / l - 1;