summaryrefslogtreecommitdiff
path: root/src/tangara/lua
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2024-09-19 15:03:43 +1000
committerjacqueline <me@jacqueline.id.au>2024-09-19 15:03:43 +1000
commit9c95c2b4222da1fb31855f3985ab96d90865086d (patch)
tree8a5bdca0fdffa3720cbef7b683295f629ce308da /src/tangara/lua
parent111085b857249a9442e118a5e37a1732716da6a2 (diff)
parent1eddfe97d9398215d4512785c669cf7cc94b6223 (diff)
downloadtangara-fw-9c95c2b4222da1fb31855f3985ab96d90865086d.tar.gz
Merge branch 'main' of codeberg.org:cool-tech-zone/tangara-fw
Diffstat (limited to 'src/tangara/lua')
-rw-r--r--src/tangara/lua/lua_database.cpp72
-rw-r--r--src/tangara/lua/lua_database.hpp2
-rw-r--r--src/tangara/lua/lua_queue.cpp14
-rw-r--r--src/tangara/lua/property.cpp24
4 files changed, 88 insertions, 24 deletions
diff --git a/src/tangara/lua/lua_database.cpp b/src/tangara/lua/lua_database.cpp
index bf84a399..e184265a 100644
--- a/src/tangara/lua/lua_database.cpp
+++ b/src/tangara/lua/lua_database.cpp
@@ -73,6 +73,56 @@ static auto indexes(lua_State* state) -> int {
return 1;
}
+auto pushTagValue(lua_State* L, const database::TagValue& val) -> void {
+ std::visit(
+ [&](auto&& arg) {
+ using T = std::decay_t<decltype(arg)>;
+ if constexpr (std::is_same_v<T, std::pmr::string>) {
+ lua_pushlstring(L, arg.data(), arg.size());
+ } else if constexpr (std::is_same_v<
+ T, std::span<const std::pmr::string>>) {
+ lua_createtable(L, 0, arg.size());
+ for (const auto& i : arg) {
+ lua_pushlstring(L, i.data(), i.size());
+ lua_pushboolean(L, true);
+ lua_rawset(L, -3);
+ }
+ } else if constexpr (std::is_same_v<T, uint32_t>) {
+ lua_pushinteger(L, arg);
+ } else {
+ lua_pushnil(L);
+ }
+ },
+ val);
+}
+
+static void pushTrack(lua_State* L, const database::Track& track) {
+ lua_newtable(L);
+
+ lua_pushliteral(L, "tags");
+ lua_newtable(L);
+ for (const auto& tag : track.tags().allPresent()) {
+ lua_pushstring(L, database::tagName(tag).c_str());
+ pushTagValue(L, track.tags().get(tag));
+ lua_settable(L, -3);
+ }
+ lua_settable(L, -3);
+
+ lua_pushliteral(L, "id");
+ lua_pushinteger(L, track.data().id);
+ lua_settable(L, -3);
+
+ lua_pushliteral(L, "filepath");
+ lua_pushstring(L, track.data().filepath.c_str());
+ lua_settable(L, -3);
+
+ lua_pushliteral(L, "saved_position");
+ lua_pushinteger(L, track.data().last_position);
+ lua_settable(L, -3);
+
+
+}
+
static auto version(lua_State* L) -> int {
Bridge* instance = Bridge::Get(L);
auto db = instance->services().database().lock();
@@ -111,9 +161,29 @@ static auto update(lua_State* L) -> int {
return 0;
}
+static auto track_by_id(lua_State* L) -> int {
+ auto id = luaL_checkinteger(L, -1);
+
+ Bridge* instance = Bridge::Get(L);
+ auto db = instance->services().database().lock();
+ if (!db) {
+ return 0;
+ }
+
+ auto track = db->getTrack(id);
+ if (!track) {
+ return 0;
+ }
+
+ pushTrack(L, *track);
+
+ return 1;
+}
+
static const struct luaL_Reg kDatabaseFuncs[] = {
{"indexes", indexes}, {"version", version}, {"size", size},
- {"recreate", recreate}, {"update", update}, {NULL, NULL}};
+ {"recreate", recreate}, {"update", update}, {"track_by_id", track_by_id},
+ {NULL, NULL}};
/*
* Struct to be used as userdata for the Lua representation of database records.
diff --git a/src/tangara/lua/lua_database.hpp b/src/tangara/lua/lua_database.hpp
index 328004ef..51e71758 100644
--- a/src/tangara/lua/lua_database.hpp
+++ b/src/tangara/lua/lua_database.hpp
@@ -14,6 +14,8 @@ namespace lua {
auto db_check_iterator(lua_State*, int stack_pos) -> database::Iterator*;
+auto pushTagValue(lua_State* L, const database::TagValue& val) -> void;
+
auto RegisterDatabaseModule(lua_State*) -> void;
} // namespace lua
diff --git a/src/tangara/lua/lua_queue.cpp b/src/tangara/lua/lua_queue.cpp
index 7eb32c62..07093390 100644
--- a/src/tangara/lua/lua_queue.cpp
+++ b/src/tangara/lua/lua_queue.cpp
@@ -79,10 +79,24 @@ static auto queue_open_playlist(lua_State* state) -> int {
return 0;
}
+static auto queue_play_from(lua_State* state) -> int {
+ Bridge* instance = Bridge::Get(state);
+ audio::TrackQueue& queue = instance->services().track_queue();
+ size_t len = 0;
+ const char* str = luaL_checklstring(state, 1, &len);
+ if (!str) {
+ return 0;
+ }
+ auto pos = luaL_checkinteger(state, 2);
+ queue.playFromPosition(str, pos);
+ return 0;
+}
+
static const struct luaL_Reg kQueueFuncs[] = {
{"add", queue_add},
{"clear", queue_clear},
{"open_playlist", queue_open_playlist},
+ {"play_from", queue_play_from},
{NULL, NULL}};
static auto lua_queue(lua_State* state) -> int {
diff --git a/src/tangara/lua/property.cpp b/src/tangara/lua/property.cpp
index 1be1fd2d..847bbe15 100644
--- a/src/tangara/lua/property.cpp
+++ b/src/tangara/lua/property.cpp
@@ -19,6 +19,7 @@
#include "lauxlib.h"
#include "lua.h"
#include "lua.hpp"
+#include "lua/lua_database.hpp"
#include "lua/lua_thread.hpp"
#include "lvgl.h"
#include "memory_resource.hpp"
@@ -240,29 +241,6 @@ auto Property::set(const LuaValue& val) -> bool {
return true;
}
-static auto pushTagValue(lua_State* L, const database::TagValue& val) -> void {
- std::visit(
- [&](auto&& arg) {
- using T = std::decay_t<decltype(arg)>;
- if constexpr (std::is_same_v<T, std::pmr::string>) {
- lua_pushlstring(L, arg.data(), arg.size());
- } else if constexpr (std::is_same_v<
- T, std::span<const std::pmr::string>>) {
- lua_createtable(L, 0, arg.size());
- for (const auto& i : arg) {
- lua_pushlstring(L, i.data(), i.size());
- lua_pushboolean(L, true);
- lua_rawset(L, -3);
- }
- } else if constexpr (std::is_same_v<T, uint32_t>) {
- lua_pushinteger(L, arg);
- } else {
- lua_pushnil(L);
- }
- },
- val);
-}
-
static void pushTrack(lua_State* L, const audio::TrackInfo& track) {
lua_newtable(L);