chiark / gitweb /
(Slightly scrappy) new playlist box
[disorder] / lib / queue.c
index 71d85aaf2c4cfb91fede7ff59dc703247136dcd2..69a798696737e9f2d2efe629cffc62e3839d444b 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * This file is part of DisOrder.
- * Copyright (C) 2004-2008 Richard Kettlewell
+ * Copyright (C) 2004-2009 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
@@ -31,7 +31,7 @@
 #include "table.h"
 #include "printf.h"
 
-const char *playing_states[] = {
+const char *const playing_states[] = {
   "failed",
   "isscratch",
   "no_player",
@@ -44,9 +44,21 @@ const char *playing_states[] = {
   "unplayed"
 };
 
+/** @brief String values for @c origin field */
+const char *const track_origins[] = {
+  "adopted",
+  "picked",
+  "random",
+  "scheduled",
+  "scratch",
+};
+
 #define VALUE(q, offset, type) *(type *)((char *)q + offset)
 
-/* add new entry @n@ to a doubly linked list just after @b@ */
+/** @brief Insert queue entry @p n just after @p b
+ * @param b Insert after this entry
+ * @param n New entry to insert
+ */
 void queue_insert_entry(struct queue_entry *b, struct queue_entry *n) {
   n->prev = b;
   n->next = b->next;
@@ -77,9 +89,9 @@ static const char *marshall_long(const struct queue_entry *q, size_t offset) {
 
   n = byte_snprintf(buffer, sizeof buffer, "%ld", VALUE(q, offset, long));
   if(n < 0)
-    fatal(errno, "error converting int");
+    disorder_fatal(errno, "error converting int");
   else if((size_t)n >= sizeof buffer)
-    fatal(0, "long converted to decimal is too long");
+    disorder_fatal(0, "long converted to decimal is too long");
   return xstrdup(buffer);
 }
 
@@ -116,9 +128,9 @@ static const char *marshall_time_t(const struct queue_entry *q, size_t offset) {
   n = byte_snprintf(buffer, sizeof buffer,
                    "%"PRIdMAX, (intmax_t)VALUE(q, offset, time_t));
   if(n < 0)
-    fatal(errno, "error converting time");
+    disorder_fatal(errno, "error converting time");
   else if((size_t)n >= sizeof buffer)
-    fatal(0, "time converted to decimal is too long");
+    disorder_fatal(0, "time converted to decimal is too long");
   return xstrdup(buffer);
 }
 
@@ -139,10 +151,31 @@ static int unmarshall_state(char *data, struct queue_entry *q,
   return 0;
 }
 
+static int unmarshall_origin(char *data, struct queue_entry *q,
+                             size_t offset,
+                             void (*error_handler)(const char *, void *),
+                             void *u) {
+  int n;
+
+  if((n = table_find(track_origins, 0, sizeof (char *),
+                    sizeof track_origins / sizeof *track_origins,
+                    data)) < 0) {
+    D(("origin=[%s] n=%d", data, n));
+    error_handler("invalid origin", u);
+    return -1;
+  }
+  VALUE(q, offset, enum track_origin) = n;
+  return 0;
+}
+
 static const char *marshall_state(const struct queue_entry *q, size_t offset) {
   return playing_states[VALUE(q, offset, enum playing_state)];
 }
 
+static const char *marshall_origin(const struct queue_entry *q, size_t offset) {
+  return track_origins[VALUE(q, offset, enum track_origin)];
+}
+
 #define F(n, h) { #n, offsetof(struct queue_entry, n), marshall_##h, unmarshall_##h }
 
 static const struct field {
@@ -156,6 +189,7 @@ static const struct field {
   /* Keep this table sorted. */
   F(expected, time_t),
   F(id, string),
+  F(origin, origin),
   F(played, time_t),
   F(scratched, string),
   F(sofar, long),
@@ -172,6 +206,7 @@ int queue_unmarshall(struct queue_entry *q, const char *s,
   char **vec;
   int nvec;
 
+  q->pid = -1;                          /* =none */
   if(!(vec = split(s, &nvec, SPLIT_QUOTES, error_handler, u)))
     return -1;
   return queue_unmarshall_vec(q, nvec, vec, error_handler, u);