chiark / gitweb /
act_playing
authorRichard Kettlewell <rjk@greenend.org.uk>
Sat, 10 May 2008 10:17:21 +0000 (11:17 +0100)
committerRichard Kettlewell <rjk@greenend.org.uk>
Sat, 10 May 2008 10:17:21 +0000 (11:17 +0100)
server/actions.c
server/lookup.c [new file with mode: 0644]
server/lookup.h [new file with mode: 0644]
server/macros-disorder.c
server/macros-disorder.h

index d6d227d..2fa3f8a 100644 (file)
 #include <config.h>
 #include "types.h"
 
+#include "actions.h"
+#include "lookups.h"
+
 /** @brief Login cookie */
 char *login_cookie;
 
+/* 'playing' and 'manage' just add a Refresh: header */
+static void act_playing(void) {
+  long refresh = config->refresh;
+  long length;
+  time_t now, fin;
+  char *url;
+
+  lookups(DC_PLAYING|DC_QUEUE|DC_ENABLED|DC_RANDOM_ENABLED);
+  if(playing
+     && playing->state == playing_started /* i.e. not paused */
+     && !disorder_length(client, playing->track, &length)
+     && length
+     && playing->sofar >= 0) {
+    /* Try to put the next refresh at the start of the next track. */
+    time(&now);
+    fin = now + length - playing->sofar + config->gap;
+    if(now + refresh > fin)
+      refresh = fin - now;
+  }
+  if(queue && queue->state == playing_isscratch) {
+    /* next track is a scratch, don't leave more than the inter-track gap */
+    if(refresh > config->gap)
+      refresh = config->gap;
+  }
+  if(!playing
+     && ((queue
+          && queue->state != playing_random)
+         || random_enabled)
+     && enabled) {
+    /* no track playing but playing is enabled and there is something coming
+     * up, must be in a gap */
+    if(refresh > config->gap)
+      refresh = config->gap;
+  }
+  if((action = cgi_get("action")))
+    url = cgi_makeurl(config->url, "action", action, (char *)0);
+  else
+    url = config->url;
+  if(printf("Content-Type: text/html\n"
+            "Refresh: %ld;url=%s\n"
+            /* TODO cookie */
+            "\n",
+            refresh, url) < 0)
+    fatal(errno, "error writing to stdout");
+  disorder_cgi_expand(action ? action : "playing");
+}
+
 /** @brief Table of actions */
 static const struct action {
   /** @brief Action name */
@@ -37,7 +87,7 @@ static const struct action {
   { "enable", act_enable },
   { "login", act_login },
   { "logout", act_logout },
-  { "manage", act_manage },
+  { "manage", act_playing },
   { "move", act_move },
   { "pause", act_pause },
   { "play", act_play },
@@ -95,9 +145,14 @@ void disorder_cgi_action(const char *action) {
   if((n = TABLE_FIND(actions, struct action, name, action)) >= 0)
     /* Its a known action */
     actions[n].handler();
-  else
+  else {
     /* Just expand the template */
+    if(printf("Content-Type: text/html\n"
+              /* TODO cookie */
+              "\n") < 0)
+      fatal(errno, "error writing to stdout");
     disorder_cgi_expand(action);
+  }
 }
 
 /** @brief Generate an error page */
diff --git a/server/lookup.c b/server/lookup.c
new file mode 100644 (file)
index 0000000..235a7d2
--- /dev/null
@@ -0,0 +1,127 @@
+/*
+ * This file is part of DisOrder.
+ * Copyright (C) 2004-2008 Richard Kettlewell
+ *
+ * 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
+ * (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.
+ *
+ * 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
+ */
+/** @file server/macros-disorder.c
+ * @brief DisOrder-specific expansions
+ */
+
+#include <config.h>
+#include "types.h"
+
+#include "sink.h"
+#include "client.h"
+#include "lookup.h"
+#include "cgi.h"
+
+/** @brief Client used by CGI
+ *
+ * The caller should arrange for this to be created before any of
+ * these expansions are used (if it cannot connect then it's safe to
+ * leave it as NULL).
+ */
+disorder_client *client;
+
+/** @brief Cached data */
+static unsigned flags;
+
+struct queue_entry *queue;
+struct queue_entry *playing;
+struct queue_entry *recent;
+
+int volume_left;
+int volume_right;
+
+char **files;
+int nfiles;
+
+char **dirs;
+int ndirs;
+
+char **new;
+int nnew;
+
+rights_type rights;
+
+int enabled;
+int random_enabled;
+
+/** @brief Fetch cachable data */
+static void lookup(unsigned want) {
+  unsigned need = want ^ (flags & want);
+  struct queue_entry *r, *rnext;
+  const char *dir, *re;
+  char *rights;
+
+  if(!client || !need)
+    return;
+  if(need & DC_QUEUE)
+    disorder_queue(client, &queue);
+  if(need & DC_PLAYING)
+    disorder_playing(client, &playing);
+  if(need & DC_NEW)
+    disorder_new_tracks(client, &new, &nnew, 0);
+  if(need & DC_RECENT) {
+    /* we need to reverse the order of the list */
+    disorder_recent(client, &r);
+    while(r) {
+      rnext = r->next;
+      r->next = recent;
+      recent = r;
+      r = rnext;
+    }
+  }
+  if(need & DC_VOLUME)
+    disorder_get_volume(client,
+                        &volume_left, &volume_right);
+  /* DC_FILES and DC_DIRS are looking obsolete now */
+  if(need & (DC_FILES|DC_DIRS)) {
+    if(!(dir = cgi_get("directory")))
+      dir = "";
+    re = cgi_get("regexp");
+    if(need & DC_DIRS)
+      if(disorder_directories(client, dir, re,
+                              &dirs, &ndirs))
+        ndirs = 0;
+    if(need & DC_FILES)
+      if(disorder_files(client, dir, re,
+                        &files, &nfiles))
+        nfiles = 0;
+  }
+  if(need & DC_RIGHTS) {
+    rights = RIGHT_READ;       /* fail-safe */
+    if(!disorder_userinfo(client, disorder_user(client),
+                          "rights", &rights))
+      parse_rights(rights, &rights, 1);
+  }
+  if(need & DC_ENABLED)
+    disorder_enabled(client, &enabled);
+  if(need & DC_RANDOM_ENABLED)
+    disorder_random_enabled(client, &random_enabled);
+  if(need & DC_RANDOM_ENABLED)
+  flags |= need;
+}
+
+/*
+Local Variables:
+c-basic-offset:2
+comment-column:40
+fill-column:79
+indent-tabs-mode:nil
+End:
+*/
diff --git a/server/lookup.h b/server/lookup.h
new file mode 100644 (file)
index 0000000..0ab7355
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * This file is part of DisOrder.
+ * Copyright (C) 2008 Richard Kettlewell
+ *
+ * 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
+ * (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.
+ *
+ * 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
+ */
+/** @file server/lookup.h
+ * @brief Server lookup code for CGI
+ */
+
+#ifndef LOOKUP_H
+#define LOOKUP_H
+
+extern disorder_client *client;
+
+#define DC_QUEUE 0x0001
+#define DC_PLAYING 0x0002
+#define DC_RECENT 0x0004
+#define DC_VOLUME 0x0008
+#define DC_DIRS 0x0010
+#define DC_FILES 0x0020
+#define DC_NEW 0x0040
+#define DC_RIGHTS 0x0080
+#define DC_ENABLED 0x0100
+#define DC_RANDOM_ENABLED 0x0200
+
+extern struct queue_entry *queue;
+extern struct queue_entry *playing;
+extern struct queue_entry *recent;
+
+extern int volume_left;
+extern int volume_right;
+
+extern char **files;
+extern int nfiles;
+
+extern char **dirs;
+extern int ndirs;
+
+extern char **new;
+extern int nnew;
+
+extern rights_type rights;
+
+extern int enabled;
+extern int random_enabled;
+
+#endif /* LOOKUP_H */
+
+/*
+Local Variables:
+c-basic-offset:2
+comment-column:40
+fill-column:79
+indent-tabs-mode:nil
+End:
+*/
index b4fdc29..7105be1 100644 (file)
 #include "client.h"
 #include "cgi.h"
 #include "macros-disorder.h"
-
-/** @brief Client to use for DisOrder-specific expansions
- *
- * The caller should arrange for this to be created before any of
- * these expansions are used (if it cannot connect then it's safe to
- * leave it as NULL).
- */
-disorder_client *client;
+#include "lookups.h"
 
 /** @brief For error template */
 char *error_string;
 
-/** @brief Cached data */
-static unsigned flags;
-
-#define DC_QUEUE 0x0001
-#define DC_PLAYING 0x0002
-#define DC_RECENT 0x0004
-#define DC_VOLUME 0x0008
-#define DC_DIRS 0x0010
-#define DC_FILES 0x0020
-#define DC_NEW 0x0040
-#define DC_RIGHTS 0x0080
-
-static struct queue_entry *queue;
-static struct queue_entry *playing;
-static struct queue_entry *recent;
-
-static int volume_left;
-static int volume_right;
-
-static char **files;
-static int nfiles;
-
-static char **dirs;
-static int ndirs;
-
-static char **new;
-static int nnew;
-
-static rights_type rights;
-
-/** @brief Fetch cachable data */
-static void lookup(unsigned want) {
-  unsigned need = want ^ (flags & want);
-  struct queue_entry *r, *rnext;
-  const char *dir, *re;
-  char *rights;
-
-  if(!client || !need)
-    return;
-  if(need & DC_QUEUE)
-    disorder_queue(client, &queue);
-  if(need & DC_PLAYING)
-    disorder_playing(client, &playing);
-  if(need & DC_NEW)
-    disorder_new_tracks(client, &new, &nnew, 0);
-  if(need & DC_RECENT) {
-    /* we need to reverse the order of the list */
-    disorder_recent(client, &r);
-    while(r) {
-      rnext = r->next;
-      r->next = recent;
-      recent = r;
-      r = rnext;
-    }
-  }
-  if(need & DC_VOLUME)
-    disorder_get_volume(client,
-                        &volume_left, &volume_right);
-  if(need & (DC_FILES|DC_DIRS)) {
-    if(!(dir = cgi_get("directory")))
-      dir = "";
-    re = cgi_get("regexp");
-    if(need & DC_DIRS)
-      if(disorder_directories(client, dir, re,
-                              &dirs, &ndirs))
-        ndirs = 0;
-    if(need & DC_FILES)
-      if(disorder_files(client, dir, re,
-                        &files, &nfiles))
-        nfiles = 0;
-  }
-  if(need & DC_RIGHTS) {
-    rights = RIGHT_READ;       /* fail-safe */
-    if(!disorder_userinfo(client, disorder_user(client),
-                          "rights", &rights))
-      parse_rights(rights, &rights, 1);
-  }
-  flags |= need;
-}
-
 /** @brief Locate a track by ID */
 static struct queue_entry *findtrack(const char *id) {
   struct queue_entry *q;
index 0556e0d..371e0f0 100644 (file)
@@ -1,4 +1,3 @@
-
 /*
  * This file is part of DisOrder.
  * Copyright (C) 2008 Richard Kettlewell
@@ -29,6 +28,33 @@ extern disorder_client *client;
 extern char *error_string;
 void register_disorder_expansions(void);
 
+#define DC_QUEUE 0x0001
+#define DC_PLAYING 0x0002
+#define DC_RECENT 0x0004
+#define DC_VOLUME 0x0008
+#define DC_DIRS 0x0010
+#define DC_FILES 0x0020
+#define DC_NEW 0x0040
+#define DC_RIGHTS 0x0080
+
+static struct queue_entry *queue;
+static struct queue_entry *playing;
+static struct queue_entry *recent;
+
+static int volume_left;
+static int volume_right;
+
+static char **files;
+static int nfiles;
+
+static char **dirs;
+static int ndirs;
+
+static char **new;
+static int nnew;
+
+static rights_type rights;
+
 #endif /* MACROS_DISORDER_H */
 
 /*