From af7a70450e3ceaaf291aa09b9383b50b879759d9 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Wed, 28 Aug 2024 15:30:25 +1000 Subject: [PATCH] Support adding filepaths to the track queue --- src/tangara/audio/track_queue.cpp | 16 ++++++++++------ src/tangara/audio/track_queue.hpp | 12 +++++++----- src/tangara/lua/lua_queue.cpp | 18 ++++++++++++++---- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/tangara/audio/track_queue.cpp b/src/tangara/audio/track_queue.cpp index 6fa5511c..cc4770ae 100644 --- a/src/tangara/audio/track_queue.cpp +++ b/src/tangara/audio/track_queue.cpp @@ -161,12 +161,6 @@ auto TrackQueue::getFilepath(database::TrackId id) return db->getTrackPath(id); } -// TODO WIP: Atm only appends are allowed, this will only ever append regardless -// of what index is given. But it is kept like this for compatability for now. -auto TrackQueue::insert(Item i, size_t index) -> void { - append(i); -} - auto TrackQueue::append(Item i) -> void { bool was_queue_empty; bool current_changed; @@ -186,6 +180,16 @@ auto TrackQueue::append(Item i) -> void { updateShuffler(was_queue_empty); } notifyChanged(current_changed, Reason::kExplicitUpdate); + } else if (std::holds_alternative(i)) { + auto& path = std::get(i); + if (!path.empty()) { + { + const std::unique_lock lock(mutex_); + playlist_.append(std::get(i)); + updateShuffler(was_queue_empty); + } + notifyChanged(current_changed, Reason::kExplicitUpdate); + } } else if (std::holds_alternative(i)) { // Iterators can be very large, and retrieving items from them often // requires disk i/o. Handle them asynchronously so that inserting them diff --git a/src/tangara/audio/track_queue.hpp b/src/tangara/audio/track_queue.hpp index 54898a5d..1d25568d 100644 --- a/src/tangara/audio/track_queue.hpp +++ b/src/tangara/audio/track_queue.hpp @@ -16,8 +16,8 @@ #include "cppbor_parse.h" #include "database/database.hpp" #include "database/track.hpp" -#include "tasks.hpp" #include "playlist.hpp" +#include "tasks.hpp" namespace audio { @@ -68,16 +68,18 @@ class TrackQueue { TrackQueue(tasks::WorkerPool& bg_worker, database::Handle db); /* Returns the currently playing track. */ - using TrackItem = std::variant; + using TrackItem = + std::variant; auto current() const -> TrackItem; auto currentPosition() const -> size_t; auto totalSize() const -> size_t; auto open() -> bool; - auto openPlaylist(const std::string& playlist_file, bool notify = true) -> bool; + auto openPlaylist(const std::string& playlist_file, bool notify = true) + -> bool; - using Item = std::variant; - auto insert(Item, size_t index = 0) -> void; + using Item = + std::variant; auto append(Item i) -> void; auto updateShuffler(bool andUpdatePosition) -> void; diff --git a/src/tangara/lua/lua_queue.cpp b/src/tangara/lua/lua_queue.cpp index 9e2002e6..7eb32c62 100644 --- a/src/tangara/lua/lua_queue.cpp +++ b/src/tangara/lua/lua_queue.cpp @@ -4,6 +4,7 @@ * SPDX-License-Identifier: GPL-3.0-only */ +#include "audio/audio_events.hpp" #include "lua/lua_database.hpp" #include @@ -39,6 +40,14 @@ static auto queue_add(lua_State* state) -> int { audio::TrackQueue& queue = instance->services().track_queue(); queue.append(id); }); + } else if (lua_isstring(state, 1)) { + size_t len; + const char* str = luaL_checklstring(state, 1, &len); + std::string path{str, len}; + instance->services().bg_worker().Dispatch([=]() { + audio::TrackQueue& queue = instance->services().track_queue(); + queue.append(path); + }); } else { database::Iterator* it = db_check_iterator(state, 1); instance->services().bg_worker().Dispatch([=]() { @@ -70,10 +79,11 @@ static auto queue_open_playlist(lua_State* state) -> int { return 0; } -static const struct luaL_Reg kQueueFuncs[] = {{"add", queue_add}, - {"clear", queue_clear}, - {"open_playlist", queue_open_playlist}, - {NULL, NULL}}; +static const struct luaL_Reg kQueueFuncs[] = { + {"add", queue_add}, + {"clear", queue_clear}, + {"open_playlist", queue_open_playlist}, + {NULL, NULL}}; static auto lua_queue(lua_State* state) -> int { luaL_newlib(state, kQueueFuncs);