redirect(0);
}
+static void act_pause(void) {
+ if(dcgi_client)
+ disorder_pause(dcgi_client);
+ redirect(0);
+}
+
+static void act_resume(void) {
+ if(dcgi_client)
+ disorder_resume(dcgi_client);
+ redirect(0);
+}
+
static void act_remove(void) {
const char *id;
struct queue_entry *q;
redirect(0);
}
+static void act_move(void) {
+ const char *id, *delta;
+ struct queue_entry *q;
+
+ if(dcgi_client) {
+ if(!(id = cgi_get("id")))
+ error(0, "missing 'id' argument");
+ else if(!(delta = cgi_get("delta")))
+ error(0, "missing 'delta' argument");
+ else if(!(q = dcgi_findtrack(id)))
+ error(0, "unknown queue id %s", id);
+ else switch(q->state) {
+ case playing_random: /* unplayed randomly chosen track */
+ case playing_unplayed: /* haven't played this track yet */
+ disorder_move(dcgi_client, id, atol(delta));
+ break;
+ default:
+ error(0, "does not make sense to scratch %s", id);
+ break;
+ }
+ }
+ redirect(0);
+}
+
+static void act_play(void) {
+ const char *track, *dir;
+ char **tracks;
+ int ntracks, n;
+ struct dcgi_entry *e;
+
+ if(dcgi_client) {
+ if((track = cgi_get("file"))) {
+ disorder_play(dcgi_client, track);
+ } else if((dir = cgi_get("dir"))) {
+ if(disorder_files(dcgi_client, dir, 0, &tracks, &ntracks))
+ ntracks = 0;
+ e = xmalloc(ntracks * sizeof (struct dcgi_entry));
+ for(n = 0; n < ntracks; ++n) {
+ e[n].track = tracks[n];
+ e[n].sort = trackname_transform("track", tracks[n], "sort");
+ e[n].display = trackname_transform("track", tracks[n], "display");
+ }
+ qsort(e, ntracks, sizeof (struct dcgi_entry), dcgi_compare_entry);
+ for(n = 0; n < ntracks; ++n)
+ disorder_play(dcgi_client, e[n].track);
+ }
+ }
+ redirect(0);
+}
+
+static int clamp(int n, int min, int max) {
+ if(n < min)
+ return min;
+ if(n > max)
+ return max;
+ return n;
+}
+
+static void act_volume(void) {
+ const char *l, *r, *d;
+ int nd;
+
+ if(dcgi_client) {
+ if((d = cgi_get("delta"))) {
+ dcgi_lookup(DCGI_VOLUME);
+ nd = clamp(atoi(d), -255, 255);
+ disorder_set_volume(dcgi_client,
+ clamp(dcgi_volume_left + nd, 0, 255),
+ clamp(dcgi_volume_right + nd, 0, 255));
+ } else if((l = cgi_get("left")) && (r = cgi_get("right")))
+ disorder_set_volume(dcgi_client, atoi(l), atoi(r));
+ }
+ redirect(0);
+}
+
/** @brief Table of actions */
static const struct action {
/** @brief Action name */
{ "disable", act_disable },
{ "enable", act_enable },
{ "manage", act_playing },
+ { "move", act_move },
+ { "pause", act_pause },
+ { "play", act_play },
{ "playing", act_playing },
{ "randomdisable", act_random_disable },
{ "randomenable", act_random_enable },
{ "remove", act_remove },
+ { "resume", act_resume },
+ { "volume", act_volume },
};
+/** @brief Check that an action name is valid
+ * @param name Action
+ * @return 1 if valid, 0 if not
+ */
+static int dcgi_valid_action(const char *name) {
+ int c;
+
+ /* First character must be letter or digit (this also requires there to _be_
+ * a first character) */
+ if(!isalnum((unsigned char)*name))
+ return 0;
+ /* Only letters, digits, '.' and '-' allowed */
+ while((c = (unsigned char)*name++)) {
+ if(!(isalnum(c)
+ || c == '.'
+ || c == '_'))
+ return 0;
+ }
+ return 1;
+}
+
/** @brief Expand a template
* @param name Base name of template, or NULL to consult CGI args
*/
void dcgi_expand(const char *name) {
- const char *p;
-
+ const char *p, *found;
+
+ /* Parse macros first */
+ if((found = mx_find("macros.tmpl")))
+ mx_expand_file(found, sink_discard(), 0);
/* For unknown actions check that they aren't evil */
- for(p = name; *p && isalnum((unsigned char)*p); ++p)
- ;
- if(*p)
+ if(!dcgi_valid_action(name))
fatal(0, "invalid action name '%s'", name);
byte_xasprintf((char **)&p, "%s.tmpl", name);
- if(mx_expand_file(p, sink_stdio("stdout", stdout), 0) == -1
+ if(!(found = mx_find(p)))
+ fatal(errno, "cannot find %s", p);
+ if(mx_expand_file(found, sink_stdio("stdout", stdout), 0) == -1
|| fflush(stdout) < 0)
fatal(errno, "error writing to stdout");
}
}
/** @brief Generate an error page */
-void dcgi_error(const char *msg, ...) {
- va_list ap;
-
- va_start(ap, msg);
- byte_xvasprintf(&dcgi_error_string, msg, ap);
- va_end(ap);
+void dcgi_error(const char *key) {
+ dcgi_error_string = xstrdup(key);
dcgi_expand("error");
}