chiark / gitweb /
update control buttons directly from state bits
[disorder] / disobedience / control.c
index 5e2e0d32c8828992b3846b9b13c9706805be321a..cb2d63c04cb75bc3e7db9da1d1f942e4af796dd5 100644 (file)
@@ -51,6 +51,8 @@ static void volume_adjusted(GtkAdjustment *a, gpointer user_data);
 static gchar *format_volume(GtkScale *scale, gdouble value);
 static gchar *format_balance(GtkScale *scale, gdouble value);
 
+static void control_monitor(void *u);
+
 /* Control bar ------------------------------------------------------------- */
 
 static int suppress_set_volume;
@@ -152,71 +154,97 @@ 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 */
+/** @brief Update the control bar after some kind of state change */
 void control_update(void) {
-  int n;
   double l, r;
 
   D(("control_update"));
-  for(n = 0; n < NICONS; ++n)
-    icons[n].update(&icons[n]);
+  /*control_monitor(0, disorder_eclient_state(client));*/
   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 */
+static void control_monitor(void attribute((unused)) *u) {
+  int n;
+
+  fprintf(stderr, "control_monitor\n");
+  for(n = 0; n < NICONS; ++n)
+    icons[n].update(&icons[n]);
+  fprintf(stderr, "\n");
+}
+
+/** @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;
+  fprintf(stderr, "%s usable=%d visible=%d\n", icon->tip, usable, visible);
+  (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,