chiark / gitweb /
Playing checkbox in Disobedience choose tab is now only visible for
[disorder] / disobedience / log.c
index af386431f0fb66b2ff1af6a049f608aa5e43f994..f0ea7d16bc59387ba4eb8fd43ebefa5e9228ff17 100644 (file)
@@ -60,33 +60,14 @@ const disorder_eclient_log_callbacks log_callbacks = {
   log_rescanned
 };
 
   log_rescanned
 };
 
-/** @brief State monitor
- *
- * We keep a linked list of everything that is interested in state changes.
- */
-struct monitor {
-  /** @brief Next monitor */
-  struct monitor *next;
-
-  /** @brief State bits of interest */
-  unsigned long mask;
-
-  /** @brief Function to call if any of @c mask change */
-  monitor_callback *callback;
-
-  /** @brief User data for callback */
-  void *u;
-};
-
-/** @brief List of monitors */
-static struct monitor *monitors;
-
 /** @brief Update everything */
 void all_update(void) {
 /** @brief Update everything */
 void all_update(void) {
-  queue_update();
-  recent_update();
-  volume_update();
-  added_update();
+  ++suppress_actions;
+  event_raise("queue-changed", 0);
+  event_raise("recent-changed", 0);
+  event_raise("volume-changed", 0);
+  event_raise("added-changed", 0);
+  --suppress_actions;
 }
 
 /** @brief Called when the client connects
 }
 
 /** @brief Called when the client connects
@@ -94,8 +75,6 @@ void all_update(void) {
  * Depending on server and network state the TCP connection to the server may
  * go up or down many times during the lifetime of Disobedience.  This function
  * is called whenever it connects.
  * Depending on server and network state the TCP connection to the server may
  * go up or down many times during the lifetime of Disobedience.  This function
  * is called whenever it connects.
- *
- * The intent is to use the monitor logic to achieve this in future.
  */
 static void log_connected(void attribute((unused)) *v) {
   /* Don't know what we might have missed while disconnected so update
  */
 static void log_connected(void attribute((unused)) *v) {
   /* Don't know what we might have missed while disconnected so update
@@ -118,7 +97,7 @@ static void log_failed(void attribute((unused)) *v,
 /** @brief Called when some track is moved within the queue */
 static void log_moved(void attribute((unused)) *v,
                       const char attribute((unused)) *user) {
 /** @brief Called when some track is moved within the queue */
 static void log_moved(void attribute((unused)) *v,
                       const char attribute((unused)) *user) {
-  queue_update();
+  event_raise("queue-changed", 0);
 }
 
 static void log_playing(void attribute((unused)) *v,
 }
 
 static void log_playing(void attribute((unused)) *v,
@@ -129,13 +108,13 @@ static void log_playing(void attribute((unused)) *v,
 /** @brief Called when a track is added to the queue */
 static void log_queue(void attribute((unused)) *v,
                       struct queue_entry attribute((unused)) *q) {
 /** @brief Called when a track is added to the queue */
 static void log_queue(void attribute((unused)) *v,
                       struct queue_entry attribute((unused)) *q) {
-  queue_update();
+  event_raise("queue-changed", 0);
 }
 
 /** @brief Called when a track is added to the recently-played list */
 static void log_recent_added(void attribute((unused)) *v,
                              struct queue_entry attribute((unused)) *q) {
 }
 
 /** @brief Called when a track is added to the recently-played list */
 static void log_recent_added(void attribute((unused)) *v,
                              struct queue_entry attribute((unused)) *q) {
-  recent_update();
+  event_raise("recent-changed", 0);
 }
 
 /** @brief Called when a track is removed from the recently-played list
 }
 
 /** @brief Called when a track is removed from the recently-played list
@@ -151,8 +130,7 @@ static void log_recent_removed(void attribute((unused)) *v,
 static void log_removed(void attribute((unused)) *v,
                         const char attribute((unused)) *id,
                         const char attribute((unused)) *user) {
 static void log_removed(void attribute((unused)) *v,
                         const char attribute((unused)) *id,
                         const char attribute((unused)) *user) {
-
-  queue_update();
+  event_raise("queue-changed", 0);
 }
 
 /** @brief Called when the current track is scratched */
 }
 
 /** @brief Called when the current track is scratched */
@@ -161,13 +139,25 @@ static void log_scratched(void attribute((unused)) *v,
                           const char attribute((unused)) *user) {
 }
 
                           const char attribute((unused)) *user) {
 }
 
+/** @brief Map from state bits to state change events */
+static const struct {
+  unsigned long bit;
+  const char *event;
+} state_events[] = {
+  { DISORDER_PLAYING_ENABLED, "enabled-changed" },
+  { DISORDER_RANDOM_ENABLED, "random-changed" },
+  { DISORDER_TRACK_PAUSED, "pause-changed" },
+  { DISORDER_PLAYING, "playing-changed" },
+};
+#define NSTATE_EVENTS (sizeof state_events / sizeof *state_events)
+
 /** @brief Called when a state change occurs */
 static void log_state(void attribute((unused)) *v,
                       unsigned long state) {
 /** @brief Called when a state change occurs */
 static void log_state(void attribute((unused)) *v,
                       unsigned long state) {
-  const struct monitor *m;
   unsigned long changes = state ^ last_state;
   static int first = 1;
   unsigned long changes = state ^ last_state;
   static int first = 1;
-  
+
+  ++suppress_actions;
   if(first) {
     changes = -1UL;
     first = 0;
   if(first) {
     changes = -1UL;
     first = 0;
@@ -177,11 +167,11 @@ static void log_state(void attribute((unused)) *v,
      disorder_eclient_interpret_state(state),
      disorder_eclient_interpret_state(changes)));
   last_state = state;
      disorder_eclient_interpret_state(state),
      disorder_eclient_interpret_state(changes)));
   last_state = state;
-  /* Tell anything that cares about the state change */
-  for(m = monitors; m; m = m->next) {
-    if(changes & m->mask)
-      m->callback(m->u);
-  }
+  /* Notify interested parties what has changed */
+  for(unsigned n = 0; n < NSTATE_EVENTS; ++n)
+    if(changes & state_events[n].bit)
+      event_raise(state_events[n].event, 0);
+  --suppress_actions;
 }
 
 /** @brief Called when volume changes */
 }
 
 /** @brief Called when volume changes */
@@ -190,32 +180,15 @@ static void log_volume(void attribute((unused)) *v,
   if(!rtp_supported && (volume_l != l || volume_r != r)) {
     volume_l = l;
     volume_r = r;
   if(!rtp_supported && (volume_l != l || volume_r != r)) {
     volume_l = l;
     volume_r = r;
-    volume_update();
+    ++suppress_actions;
+    event_raise("volume-changed", 0);
+    --suppress_actions;
   }
 }
 
 /** @brief Called when a rescan completes */
 static void log_rescanned(void attribute((unused)) *v) {
   }
 }
 
 /** @brief Called when a rescan completes */
 static void log_rescanned(void attribute((unused)) *v) {
-  added_update();
-}
-
-/** @brief Add a monitor to the list
- * @param callback Function to call
- * @param u User data to pass to @p callback
- * @param mask Mask of flags that @p callback cares about
- *
- * Pass @p mask as -1UL to match all flags.
- */
-void register_monitor(monitor_callback *callback,
-                      void *u,
-                      unsigned long mask) {
-  struct monitor *m = xmalloc(sizeof *m);
-
-  m->next = monitors;
-  m->mask = mask;
-  m->callback = callback;
-  m->u = u;
-  monitors = m;
+  event_raise("added-changed", 0);
 }
 
 /*
 }
 
 /*