chiark / gitweb /
Switch to GPL v3
[disorder] / disobedience / log.c
index ced5eee66b902bed84698d9a1d1e356d371fb793..d11e7bbcf4ee62b96883acbd5ca49198f8cbe897 100644 (file)
@@ -2,20 +2,18 @@
  * This file is part of DisOrder.
  * Copyright (C) 2006, 2007 Richard Kettlewell
  *
- * This program is free software; you can redistribute it and/or modify
+ * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
  *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License for more details.
- *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
- * USA
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 /** @file disobedience/log.c
  * @brief State monitoring
@@ -42,52 +40,33 @@ static void log_scratched(void *v, const char *track, const char *user);
 static void log_state(void *v, unsigned long state);
 static void log_volume(void *v, int l, int r);
 static void log_rescanned(void *v);
+static void log_rights_changed(void *v, rights_type r);
 
 /** @brief Callbacks for server state monitoring */
 const disorder_eclient_log_callbacks log_callbacks = {
-  log_connected,
-  log_completed,
-  log_failed,
-  log_moved,
-  log_playing,
-  log_queue,
-  log_recent_added,
-  log_recent_removed,
-  log_removed,
-  log_scratched,
-  log_state,
-  log_volume,
-  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;
+  .connected = log_connected,
+  .completed = log_completed,
+  .failed = log_failed,
+  .moved = log_moved,
+  .playing = log_playing,
+  .queue = log_queue,
+  .recent_added = log_recent_added,
+  .recent_removed = log_recent_removed,
+  .removed = log_removed,
+  .scratched = log_scratched,
+  .state = log_state,
+  .volume = log_volume,
+  .rescanned = log_rescanned,
+  .rights_changed = log_rights_changed
 };
 
-/** @brief List of monitors */
-static struct monitor *monitors;
-
 /** @brief Update everything */
 void all_update(void) {
   ++suppress_actions;
   event_raise("queue-changed", 0);
   event_raise("recent-changed", 0);
   event_raise("volume-changed", 0);
-  event_raise("added-changed", 0);
+  event_raise("rescan-complete", 0);
   --suppress_actions;
 }
 
@@ -96,14 +75,13 @@ 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.
- *
- * 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
    * everything.  We get this at startup too and this is how we do the initial
    * state fetch. */
   all_update();
+  event_raise("log-connected", 0);
 }
 
 /** @brief Called when the current track finishes playing */
@@ -171,13 +149,13 @@ static const struct {
   { DISORDER_RANDOM_ENABLED, "random-changed" },
   { DISORDER_TRACK_PAUSED, "pause-changed" },
   { DISORDER_PLAYING, "playing-changed" },
+  { DISORDER_CONNECTED, "connected-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) {
-  const struct monitor *m;
   unsigned long changes = state ^ last_state;
   static int first = 1;
 
@@ -195,11 +173,6 @@ static void log_state(void attribute((unused)) *v,
   for(unsigned n = 0; n < NSTATE_EVENTS; ++n)
     if(changes & state_events[n].bit)
       event_raise(state_events[n].event, 0);
-  /* Tell anything that cares about the state change */
-  for(m = monitors; m; m = m->next) {
-    if(changes & m->mask)
-      m->callback(m->u);
-  }
   --suppress_actions;
 }
 
@@ -217,26 +190,16 @@ static void log_volume(void attribute((unused)) *v,
 
 /** @brief Called when a rescan completes */
 static void log_rescanned(void attribute((unused)) *v) {
-  event_raise("added-changed", 0);
+  event_raise("rescan-complete", 0);
 }
 
-/** @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;
+/** @brief Called when our rights change */
+static void log_rights_changed(void attribute((unused)) *v,
+                               rights_type new_rights) {
+  ++suppress_actions;
+  last_rights = new_rights;
+  event_raise("rights-changed", 0);
+  --suppress_actions;
 }
 
 /*