From 122eea9e4ef5b9da6b130a8542aeb1c4f9988b68 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Sat, 30 Jan 2021 21:35:56 +0100
Subject: [PATCH] CMD: Add the `__flat` command that flatten the priorityies in
 the queue

---
 README.md             | 1 +
 inc/lektor/commands.h | 1 +
 inc/lektor/database.h | 1 +
 src/base/commands.c   | 7 +++++++
 src/database/queue.c  | 1 +
 src/net/listen.c      | 2 ++
 6 files changed, 13 insertions(+)

diff --git a/README.md b/README.md
index 51dcdb12..cde27020 100644
--- a/README.md
+++ b/README.md
@@ -274,6 +274,7 @@ For the compatibility column, the possible values are the following:
 | `__rescan`              | same as `rescan`, but force populating without timestamp check  |
 | `sticker __add {name}`  | create a sticker                                                |
 | `__insert {uri}`        | do the `add` command, but with the maximum priority             |
+| `__flat`                | flat is justice => all prio are now 1 in the queue              |
 
 ### Commands with no plan for support
 
diff --git a/inc/lektor/commands.h b/inc/lektor/commands.h
index 7fc6240f..65affefa 100644
--- a/inc/lektor/commands.h
+++ b/inc/lektor/commands.h
@@ -44,6 +44,7 @@ bool command_move   (struct lkt_state *srv, char *args[LKT_MESSAGE_ARGS_MAX]);
 bool command_shuffle(struct lkt_state *srv, char *args[LKT_MESSAGE_ARGS_MAX]);
 bool command_dump   (struct lkt_state *srv, char *args[LKT_MESSAGE_ARGS_MAX]);
 bool command_swap   (struct lkt_state *srv, char *args[LKT_MESSAGE_ARGS_MAX]);
+bool command_flat   (struct lkt_state *srv, char *args[LKT_MESSAGE_ARGS_MAX]);
 
 bool command_queue_list(struct lkt_state *srv, size_t c, char *args[LKT_MESSAGE_ARGS_MAX]);
 
diff --git a/inc/lektor/database.h b/inc/lektor/database.h
index 86f8672c..fa74abee 100644
--- a/inc/lektor/database.h
+++ b/inc/lektor/database.h
@@ -64,6 +64,7 @@ bool database_queue_del_id (volatile sqlite3 *db, int id);
 bool database_queue_del_pos(volatile sqlite3 *db, int pos);
 bool database_queue_clear  (volatile sqlite3 *db);
 bool database_queue_crop   (volatile sqlite3 *db);
+bool database_queue_flat   (volatile sqlite3 *db);
 bool database_queue_move   (volatile sqlite3 *db, int from, int to);
 bool database_queue_swap   (volatile sqlite3 *db, int from, int to);
 bool database_queue_shuffle(volatile sqlite3 *db);
diff --git a/src/base/commands.c b/src/base/commands.c
index 97fc71db..fd61a9dc 100644
--- a/src/base/commands.c
+++ b/src/base/commands.c
@@ -368,6 +368,13 @@ command_crop(struct lkt_state *srv, char UNUSED *args[LKT_MESSAGE_ARGS_MAX])
     return database_queue_crop(srv->db);
 }
 
+inline bool
+command_flat(struct lkt_state *srv, char UNUSED *args[LKT_MESSAGE_ARGS_MAX])
+{
+    srv->mpd_idle_events |= MPD_IDLE_PLAYLIST;
+    return database_queue_flat(srv->db);
+}
+
 static inline bool
 __skip_current(struct lkt_state *srv)
 {
diff --git a/src/database/queue.c b/src/database/queue.c
index 2c8ac930..61bb64a1 100644
--- a/src/database/queue.c
+++ b/src/database/queue.c
@@ -31,6 +31,7 @@ sqlite_just_exec(database_queue_clear,          "DELETE FROM queue;"
 sqlite_just_exec(database_config_queue_default, "UPDATE queue_state SET volume = 100, paused = 1,"
                                                 " random = 0, repeat = 0, single = 0, consume = 0,"
                                                 " current = NULL, duration = 0;")
+sqlite_just_exec(database_queue_flat,           "UPDATE queue SET priority = 1;");
 #undef sqlite_just_exec
 // *INDENT-ON*
 
diff --git a/src/net/listen.c b/src/net/listen.c
index 89617e16..c97aae32 100644
--- a/src/net/listen.c
+++ b/src/net/listen.c
@@ -205,6 +205,8 @@ handle_simple_command(struct lkt_state *srv, size_t c, struct lkt_command cmd)
             err = ! command_shuffle(srv, NULL);
         else if (STR_MATCH(cmd.name, "swap"))
             err = ! command_swap(srv, cmd.args);
+        else if (STR_MATCH(cmd.name, "__flat"))
+            err = ! command_flat(srv, cmd.args);
 
         else if (STR_MATCH(cmd.name, "seek"))
             err = ! command_seek(srv, cmd.args);
-- 
GitLab