From 6b564d23a27cf846dff93f505d4782ba4d6726f5 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Wed, 15 Apr 2020 17:16:29 +0200
Subject: [PATCH] Stick to the MPD protocol

---
 src/commands.c | 44 +++++++++++++++-----------------------------
 src/main/lkt.c |  1 +
 2 files changed, 16 insertions(+), 29 deletions(-)

diff --git a/src/commands.c b/src/commands.c
index 03baf866..69bbec25 100644
--- a/src/commands.c
+++ b/src/commands.c
@@ -884,10 +884,9 @@ command_shuffle(sqlite3 *db, enum mpd_idle_flag *watch_mask_ptr)
 bool
 command_queue_list(struct lkt_state *srv, size_t c, char *args[LKT_MESSAGE_ARGS_MAX])
 {
-    unsigned int count, from, to, tmp_switch;
+    unsigned int from, to, tmp_switch;
     long val;
     char *endptr, *str;
-    struct lkt_queue_state state;
     struct lkt_callback callback = {
         .call = lkt_callback_send_row_v1,
         .srv = srv,
@@ -915,16 +914,15 @@ command_queue_list(struct lkt_state *srv, size_t c, char *args[LKT_MESSAGE_ARGS_
         return false;
     }
 
-    if (*endptr == '\0') {
-        /* There is nothing after, is is the relative forme of the command. */
-        count = labs(val);
-        goto is_relative;
-    }
+    from = labs(val);
+
+    /* There is nothing after, is is the song pos version. */
+    if (*endptr == '\0')
+        goto only_one;
 
+    /* There is something after, this is the absolute forme of the command. Then
+       parse the second value. Skip the supposed ':' character. */
     else {
-        /* There is something after, this is the absolute forme of the command. Then
-           parse the second value. Skip the supposed ':' character. */
-        from = labs(val);
         str = endptr + strspn(endptr, "-+: "); // NO NEGATIVE! //
         val = strtol(str, &endptr, 0);
 
@@ -948,37 +946,25 @@ command_queue_list(struct lkt_state *srv, size_t c, char *args[LKT_MESSAGE_ARGS_
             from = tmp_switch;
         }
 
-        goto is_absolute;
+        goto is_a_range;
     }
 
     return false;
 
     /* The command is used in its relative forme, display elements from the
        current one. */
-is_relative:
-    if (!database_queue_state(srv->db, &state)) {
-        fprintf(stderr, " . command_queue_list: Could not get current position in queue\n");
-        return false;
-    }
-
-    if (count < lkt_remaining_msg(srv, c) - 1) {
-        lkt_set_continuation(srv, c, 0);
-        return database_queue_list(srv->db, state.current, state.current + count, &callback);
-    } else {
-        count = lkt_remaining_msg(srv, c) - 2;  /* One for `continue: i` and one for OK/ACK. */
-        lkt_set_continuation(srv, c, state.current + count);
-        return database_queue_list(srv->db, state.current, state.current + count, &callback);
-    }
+only_one:
+        return database_queue_list(srv->db, from, from, &callback);
 
     /* The command is used with a range specifier. */
-is_absolute:
+is_a_range:
     if (to - from < lkt_remaining_msg(srv, c) - 1) {
         lkt_set_continuation(srv, c, 0);
         return database_queue_list(srv->db, from, to, &callback);
     } else {
-        count = lkt_remaining_msg(srv, c) - 2 + from;
-        lkt_set_continuation(srv, c, count);
-        return database_queue_list(srv->db, from, count, &callback);
+        to = from + lkt_remaining_msg(srv, c) - 2;
+        lkt_set_continuation(srv, c, to + 1);
+        return database_queue_list(srv->db, from, to, &callback);
     }
 }
 
diff --git a/src/main/lkt.c b/src/main/lkt.c
index 834aa483..db26aea6 100644
--- a/src/main/lkt.c
+++ b/src/main/lkt.c
@@ -598,6 +598,7 @@ list__(struct lkt_cmd_args *args)
 
     FILE *sock = NULL;
 redo:
+    printf("%ld:%ld\n", continuation, up);
     sock = lkt_connect();
     if (up != 0)
         write_socket_format(sock, "playlist %d:%d\n", continuation, up);
-- 
GitLab