Don't include the currently playing track in the queue listings.
authorNot Zed <notzed@gmail.com>
Tue, 9 Jan 2024 23:22:22 +0000 (09:52 +1030)
committerNot Zed <notzed@gmail.com>
Tue, 9 Jan 2024 23:22:22 +0000 (09:52 +1030)
dbindex.h
http-monitor.c
index-util.c

index 8ea61de..39ee648 100644 (file)
--- a/dbindex.h
+++ b/dbindex.h
@@ -97,6 +97,9 @@ struct dbstate {
 
 // current version/magic
 #define ZPL_STATE_VERSION 0x5a5e0001
+// state flags
+#define ZPL_STATEF_PLAYING 1
+#define ZPL_STATEF_PAUSED 2
 
 typedef struct dbindex dbindex;
 typedef struct MDB_txn dbtxn;
index 6fc480c..bd29cfc 100644 (file)
@@ -257,8 +257,6 @@ static void write_queue_json(dbindex *db, struct obstack *io) {
        int limit = 10;
        int count = 0;
        int res;
-       dbstate states[ZPL_SIZEOF] = { 0 };
-       int playing = -1;
 
        obstack_1grow(io, '{');
 
@@ -266,72 +264,44 @@ static void write_queue_json(dbindex *db, struct obstack *io) {
        write_value_json(io, "queue");
        obstack_sgrow(io, ",\"items\": [");
 
-       for (int i=ZPL_SIZEOF-1;i>=0 && count < limit;i--) {
-               if (!list_names[i])
-                       continue;
-
-               states[i].curr.listid = dblistid_get_name(tx, db, list_names[i]);
-               res = dbstate_get(tx, db, states[i].curr.listid, &states[i]);
-
-               // Grab the playing item as the first entry
-               if (res == 0 && (states[i].state & 1) == 1 && count == 0) {
-                       dbscan scan;
-                       dblistcursor cursor = states[i].curr;
-                       dbfile *file = dbscan_list_entry(tx, &scan, db, &cursor);
-
-                       if (file && dbfile_full_path(tx, db, file)) {
-                               obstack_sgrow(io, count > 0 ? ",{" : "{");
-                               write_file_json(io, file);
-
-                               obstack_printf(io, ",\"listid\":\"%u\"", cursor.listid);
-                               obstack_printf(io, ",\"seq\":\"%u\"", cursor.seq);
-
-                               obstack_1grow(io, '}');
-
-                               playing = i;
-                               count += 1;
-                       }
-                       dbfile_free(file);
-                       dbscan_free(&scan);
-               }
-       }
-
        for (int i=ZPL_SIZEOF-1;i>=0 && count < limit;i--) {
                dbfile *file;
                dbscan scan;
-               dblistcursor cursor = states[i].curr;
-               int scancount = 0;
+               dbstate state;
+               int listcount = 0;
 
+               if (!list_names[i])
+                       continue;
+
+               state.curr.listid = dblistid_get_name(tx, db, list_names[i]);
+               res = dbstate_get(tx, db, state.curr.listid, &state);
        loop:
-               file = dbscan_list_entry(tx, &scan, db, &cursor);
+               file = dbscan_list_entry(tx, &scan, db, &state.curr);
                while (count < limit && file) {
-                       if (dbfile_full_path(tx, db, file)
-                               && !(count == 1 && playing == i && scancount == 0)) {
+                       if (listcount > 0 || (state.state & ZPL_STATEF_PLAYING) == 0) {
                                obstack_sgrow(io, count > 0 ? ",{" : "{");
                                write_file_json(io, file);
 
-                               obstack_printf(io, ",\"listid\":\"%u\"", cursor.listid);
-                               obstack_printf(io, ",\"seq\":\"%u\"", cursor.seq);
+                               obstack_printf(io, ",\"listid\":\"%u\"", state.curr.listid);
+                               obstack_printf(io, ",\"seq\":\"%u\"", state.curr.seq);
 
                                obstack_1grow(io, '}');
                                count++;
                        }
 
                        dbfile_free(file);
-                       file = dbscan_list_entry_next(&scan, &cursor);
-                       scancount++;
+                       file = dbscan_list_entry_next(&scan, &state.curr);
+                       listcount++;
                }
                dbfile_free(file);
                res = dbscan_result(&scan);
                dbscan_free(&scan);
 
                // loop on default playlist always
-               if (res == MDB_NOTFOUND) {
-                       if (i == 0 && cursor.seq > 0) {
-                               cursor.seq = 0;
-                               cursor.fileid = 0;
-                               goto loop;
-                       }
+               if (res == MDB_NOTFOUND && i == 0 && state.curr.seq > 0) {
+                       state.curr.seq = 0;
+                       state.curr.fileid = 0;
+                       goto loop;
                }
        }
        obstack_sgrow(io, "]}");
@@ -583,7 +553,7 @@ static int handle_queue(struct ez_httprequest *req, struct ez_httpresponse *rep)
                write_queue_json(db, os);
                set_data_json(rep);
        } else if (strcmp(req->method, "POST") == 0) {
-               // TODO: enqueue
+               // TODO: enqueue?  This is handles by POST /x/list/[jukebox]?f=file
                int fid = param_int(req, "f", 0);
 
                if (fid) {
@@ -610,7 +580,7 @@ static void http_addheader(struct ez_http *http, const char *name, const char *v
 // /x/list/{listid}
 static int handle_list(struct ez_httprequest *req, struct ez_httpresponse *rep) {
        struct obstack *os = &rep->http.conn->os;
-       int lid = atoi(req->url + strlen("/x/list/"));
+       int lid = atoi(req->url + strlen("/x/list/")); // TODO: support symbolic names
        int fid = param_int(req, "f", 0);
        int seq = param_int(req, "s", 0); // TODO: should be in /x/list/{listid}/{seq} ?
 
index bb815bb..e47b180 100644 (file)
@@ -348,8 +348,8 @@ int main(int argc, char **argv) {
                                res = dbstate_get(tx, db, state.curr.listid, &state);
                                if (res != 0)
                                        continue;
-                               printf("%4d: ver=%08x state=%08x stamp=%016lx pos=%9.3fs lid=%4d seq=%4d fid=%5d\n",
-                                       i, state.version, state.state, state.stamp, state.poss,
+                               printf("%4d: state=%08x stamp=%016lx pos=%9.3fs lid=%4d seq=%4d fid=%5d\n",
+                                       i, state.state, state.stamp, state.poss,
                                        state.curr.listid, state.curr.seq, state.curr.fileid);
                        }
                        dbindex_commit(tx);
@@ -357,12 +357,13 @@ int main(int argc, char **argv) {
                        dbtxn *tx = dbindex_begin(db, NULL, 1);
                        int res;
                        int count = 0;
-                       int limit = 100;
+                       int limit = argc == 3 ? atoi(argv[2]) : 10;
 
                        for (int i=ZPL_SIZEOF-1;i>=0 && count < limit;i--) {
                                dbstate state;
                                dbscan scan;
                                dbfile *file;
+                               int listcount = 0;
 
                                if (!list_names[i])
                                        continue;
@@ -374,25 +375,24 @@ int main(int argc, char **argv) {
                        loop:
                                file = dbscan_list_entry(tx, &scan, db, &state.curr);
                                while (count < limit && file) {
-                                       if (dbfile_full_path(tx, db, file)) {
+                                       if (listcount > 0 || (state.state & ZPL_STATEF_PLAYING) == 0) {
                                                printf("%4d: fid=%4d lid=%4d seq=%4d title: %-60s %s\n", count++, file->id, state.curr.listid, state.curr.seq, file->title, file->path);
                                        }
+
                                        dbfile_free(file);
                                        file = dbscan_list_entry_next(&scan, &state.curr);
+                                       listcount++;
                                }
                                dbfile_free(file);
                                res = dbscan_result(&scan);
                                dbscan_free(&scan);
 
                                // loop on default playlist always
-                               if (res == MDB_NOTFOUND) {
-                                       if (i == 0 && state.curr.seq > 0) {
-                                               state.curr.seq = 0;
-                                               state.curr.fileid = 0;
-                                               goto loop;
-                                       }
+                               if (res == MDB_NOTFOUND && i == 0 && state.curr.seq > 0) {
+                                       state.curr.seq = 0;
+                                       state.curr.fileid = 0;
+                                       goto loop;
                                }
-
                        }
 
 #if 0