From 076c9a0dd9ea786f6f2f47607e025b576201c6e7 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 7 Oct 2021 22:06:13 +0200 Subject: [PATCH] command/QueueCommands: offset relative "addid" positions by one Now, "+0" means "right after the current song" and "-0" means "right before the current song". Mnemonic: there are zero songs between the current song and the newly added song. --- doc/protocol.rst | 7 ++++--- src/command/QueueCommands.cxx | 34 +++++++++++++++++----------------- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/doc/protocol.rst b/doc/protocol.rst index bd0f7dcd7..28977c487 100644 --- a/doc/protocol.rst +++ b/doc/protocol.rst @@ -711,9 +711,10 @@ Whenever possible, ids should be used. If the second parameter is given, then the song is inserted at the specified position. If the parameter starts with ``+`` or ``-``, - then it is relative to the current song; e.g. ``+1`` inserts right - after the current song and ``-1`` inserts right before the current - song. (``±0`` is not a legal value.) + then it is relative to the current song; e.g. ``+0`` inserts right + after the current song and ``-0`` inserts right before the current + song (i.e. zero songs between the current song and the newly added + song). .. _command_clear: diff --git a/src/command/QueueCommands.cxx b/src/command/QueueCommands.cxx index 1bc30bbc7..d6c11d61c 100644 --- a/src/command/QueueCommands.cxx +++ b/src/command/QueueCommands.cxx @@ -120,8 +120,8 @@ handle_addid(Client &client, Request args, Response &r) const auto queue_length = partition.playlist.queue.GetLength(); const char *const s = args[1]; - if (*s == '+' || *s == '-') { - /* relative to the current song */ + if (*s == '+') { + /* after the current song */ const int current = partition.playlist.GetCurrentPosition(); @@ -129,23 +129,23 @@ handle_addid(Client &client, Request args, Response &r) throw ProtocolError(ACK_ERROR_PLAYER_SYNC, "No current song"); - to = args.ParseInt(1, -current - 1, - queue_length - current); - if (to == 0) - throw ProtocolError(ACK_ERROR_ARG, - "Zero is not a legal relative position"); + assert(unsigned(current) < queue_length); - /* special case for negative offsets: the - offset "-1" shall insert the new song right - before the current song (just like "+1" - inserts right after the current song); - computationally, that would be a zero - offset, but that's not intuitive, so we - need to add one here */ - if (to < 0) - ++to; + to = current + 1 + + ParseCommandArgUnsigned(s + 1, + queue_length - current - 1); + } else if (*s == '-') { + /* before the current song */ - to += current; + const int current = + partition.playlist.GetCurrentPosition(); + if (current < 0) + throw ProtocolError(ACK_ERROR_PLAYER_SYNC, + "No current song"); + + assert(unsigned(current) < queue_length); + + to = current - ParseCommandArgUnsigned(s + 1, current); } else /* absolute position */ to = args.ParseUnsigned(1, queue_length);