From 4aed95a3cdf6eb39e158cb2333d09b354afe3614 Mon Sep 17 00:00:00 2001 From: ailurux Date: Thu, 2 May 2024 17:02:58 +1000 Subject: [PATCH 1/8] WIP: Lua filesystem starting point --- src/lua/CMakeLists.txt | 4 +- src/lua/bridge.cpp | 2 + src/lua/file_iterator.cpp | 76 ++++++++++++++ src/lua/include/file_iterator.hpp | 42 ++++++++ src/lua/include/lua_filesystem.hpp | 17 ++++ src/lua/lua_filesystem.cpp | 155 +++++++++++++++++++++++++++++ 6 files changed, 294 insertions(+), 2 deletions(-) create mode 100644 src/lua/file_iterator.cpp create mode 100644 src/lua/include/file_iterator.hpp create mode 100644 src/lua/include/lua_filesystem.hpp create mode 100644 src/lua/lua_filesystem.cpp diff --git a/src/lua/CMakeLists.txt b/src/lua/CMakeLists.txt index 0240a50c..4aa5a123 100644 --- a/src/lua/CMakeLists.txt +++ b/src/lua/CMakeLists.txt @@ -5,9 +5,9 @@ idf_component_register( SRCS "lua_theme.cpp" "lua_thread.cpp" "bridge.cpp" "property.cpp" "lua_database.cpp" "lua_queue.cpp" "lua_version.cpp" "lua_theme.cpp" "lua_controls.cpp" "registry.cpp" - "lua_screen.cpp" + "lua_screen.cpp" "lua_filesystem.cpp" "file_iterator.cpp" INCLUDE_DIRS "include" - REQUIRES "drivers" "lvgl" "tinyfsm" "events" "system_fsm" "database" + REQUIRES "drivers" "lvgl" "tinyfsm" "events" "system_fsm" "database" "fatfs" "esp_timer" "battery" "esp-idf-lua" "luavgl" "lua-linenoise" "lua-term" "esp_app_format") target_compile_options(${COMPONENT_LIB} PRIVATE ${EXTRA_WARNINGS}) diff --git a/src/lua/bridge.cpp b/src/lua/bridge.cpp index cfa9d5f7..a84eb0c1 100644 --- a/src/lua/bridge.cpp +++ b/src/lua/bridge.cpp @@ -18,6 +18,7 @@ #include "lua.hpp" #include "lua_controls.hpp" #include "lua_database.hpp" +#include "lua_filesystem.hpp" #include "lua_queue.hpp" #include "lua_screen.hpp" #include "lua_version.hpp" @@ -84,6 +85,7 @@ auto Bridge::installBaseModules(lua_State* L) -> void { RegisterControlsModule(L); RegisterDatabaseModule(L); + RegisterFileSystemModule(L); RegisterQueueModule(L); RegisterVersionModule(L); RegisterThemeModule(L); diff --git a/src/lua/file_iterator.cpp b/src/lua/file_iterator.cpp new file mode 100644 index 00000000..7f2929ba --- /dev/null +++ b/src/lua/file_iterator.cpp @@ -0,0 +1,76 @@ +/* + * Copyright 2023 ailurux + * + * SPDX-License-Identifier: GPL-3.0-only + */ +#include "file_iterator.hpp" +#include "esp_log.h" + +#include + +#include "ff.h" +#include "spi.hpp" + +namespace database { + +[[maybe_unused]] static const char* kTag = "FileIterator"; + +FileIterator::FileIterator(std::string filepath) +: original_path_(filepath), + current_() + { + auto lock = drivers::acquire_spi(); + + const TCHAR* next_path = static_cast(filepath.c_str()); + FRESULT res = f_opendir(&dir_, next_path); + if (res != FR_OK) { + ESP_LOGE(kTag, "Error opening directory: %s", filepath.c_str()); + } +} + +auto FileIterator::value() const -> const std::optional& { + return current_; +} + +auto FileIterator::next() -> void { + iterate(false); +} + +auto FileIterator::prev() -> void { + iterate(true); +} + +auto FileIterator::iterate(bool reverse) -> bool { + FILINFO info; + if (reverse) { + f_rewinddir(&dir_); + } + { + auto lock = drivers::acquire_spi(); + auto res = f_readdir(&dir_, &info); + if (res != FR_OK) { + ESP_LOGI(kTag, "AAAAAAAAAAAAAAAAAAA"); + ESP_LOGI(kTag, "%d", res); + return false; + } + } + if (info.fname[0] == 0) { + // End of directory + current_.reset(); + ESP_LOGI(kTag, "End of dir"); + + } else { + // Update current value + ESP_LOGI(kTag, "File: %s", info.fname); + current_ = FileEntry{ + .isHidden = (info.fattrib & AM_HID) > 0, + .isDirectory = (info.fattrib & AM_DIR) > 0, + .isTrack = false, // TODO + .filepath = original_path_ + info.fname, + + }; + } + return true; +} + +} // namespace database \ No newline at end of file diff --git a/src/lua/include/file_iterator.hpp b/src/lua/include/file_iterator.hpp new file mode 100644 index 00000000..6fc58245 --- /dev/null +++ b/src/lua/include/file_iterator.hpp @@ -0,0 +1,42 @@ +/* + * Copyright 2023 ailurux + * + * SPDX-License-Identifier: GPL-3.0-only + */ + +#pragma once + +#include +#include + +#include "ff.h" + +namespace database { + +// Note for when reading FILINFO, that we are in LFN mode: +// http://elm-chan.org/fsw/ff/doc/sfileinfo.html +struct FileEntry { + bool isHidden; + bool isDirectory; + bool isTrack; + std::string filepath; +}; + +class FileIterator { + public: + FileIterator(std::string filepath); + + auto value() const -> const std::optional&; + auto next() -> void; + auto prev() -> void; + + private: + FF_DIR dir_; + std::string original_path_; + + std::optional current_; + + auto iterate(bool reverse = false) -> bool; +}; + +} // namespace database \ No newline at end of file diff --git a/src/lua/include/lua_filesystem.hpp b/src/lua/include/lua_filesystem.hpp new file mode 100644 index 00000000..2a829405 --- /dev/null +++ b/src/lua/include/lua_filesystem.hpp @@ -0,0 +1,17 @@ +/* + * Copyright 2023 ailurux + * + * SPDX-License-Identifier: GPL-3.0-only + */ +#pragma once + +#include "lua.hpp" +#include "file_iterator.hpp" + +namespace lua { + +auto check_file_iterator(lua_State*, int stack_pos) -> database::FileIterator*; + +auto RegisterFileSystemModule(lua_State*) -> void; + +} // namespace lua diff --git a/src/lua/lua_filesystem.cpp b/src/lua/lua_filesystem.cpp new file mode 100644 index 00000000..5c690c16 --- /dev/null +++ b/src/lua/lua_filesystem.cpp @@ -0,0 +1,155 @@ +/* + * Copyright 2023 jacqueline + * + * SPDX-License-Identifier: GPL-3.0-only + */ + +#include "lua_filesystem.hpp" +#include +#include +#include "lauxlib.h" + +namespace lua { + +[[maybe_unused]] static constexpr char kTag[] = "lua_fs"; + +static constexpr char kFileEntryMetatable[] = "fs_file_entry"; +static constexpr char kFileIteratorMetatable[] = "fs_iterator"; + +// Todo: Use std::pmr::string for paths/dirs +struct LuaFileEntry { + bool isHidden; + bool isDirectory; + bool isTrack; + size_t path_size; + char path[]; +}; + +static_assert(std::is_trivially_destructible()); +static_assert(std::is_trivially_copy_assignable()); + +static auto push_lua_file_entry(lua_State* L, const database::FileEntry& r) -> void { + // Create and init the userdata. + LuaFileEntry* file_entry = reinterpret_cast( + lua_newuserdata(L, sizeof(LuaFileEntry) + r.filepath.size())); + luaL_setmetatable(L, kFileEntryMetatable); + + // Init all the fields + *file_entry = { + .isHidden = r.isHidden, + .isDirectory = r.isDirectory, + .isTrack = r.isTrack, + .path_size = r.filepath.size(), + }; + + // Copy the string data across. + std::memcpy(file_entry->path, r.filepath.data(), r.filepath.size()); +} + +auto check_file_iterator(lua_State* L, int stack_pos) -> database::FileIterator* { + database::FileIterator* it = *reinterpret_cast( + luaL_checkudata(L, stack_pos, kFileIteratorMetatable)); + return it; +} + +static auto push_iterator(lua_State* state, const database::FileIterator& it) + -> void { + database::FileIterator** data = reinterpret_cast( + lua_newuserdata(state, sizeof(uintptr_t))); + *data = new database::FileIterator(it); // TODO... + luaL_setmetatable(state, kFileIteratorMetatable); +} + +static auto fs_iterate_prev(lua_State* state) -> int { + database::FileIterator* it = check_file_iterator(state, 1); + it->prev(); + std::optional res = it->value(); + + if (res) { + push_lua_file_entry(state, *res); + } else { + lua_pushnil(state); + } + + return 1; +} + +static auto fs_iterate(lua_State* state) -> int { + database::FileIterator* it = check_file_iterator(state, 1); + it->next(); + std::optional res = it->value(); + + if (res) { + push_lua_file_entry(state, *res); + } else { + lua_pushnil(state); + } + + return 1; +} + +// static auto db_iterator_clone(lua_State* state) -> int { +// database::Iterator* it = db_check_iterator(state, 1); +// push_iterator(state, *it); +// return 1; +// } + +static auto fs_iterator_gc(lua_State* state) -> int { + database::FileIterator* it = check_file_iterator(state, 1); + delete it; + return 0; +} + +static const struct luaL_Reg kFileIteratorFuncs[] = {{"next", fs_iterate}, + {"prev", fs_iterate_prev}, + // {"clone", db_iterator_clone}, + {"__call", fs_iterate}, + {"__gc", fs_iterator_gc}, + {NULL, NULL}}; + +static auto file_entry_path(lua_State* state) -> int { + LuaFileEntry* data = reinterpret_cast( + luaL_checkudata(state, 1, kFileEntryMetatable)); + lua_pushlstring(state, data->path, data->path_size); + return 1; +} + +static const struct luaL_Reg kFileEntryFuncs[] = {{"filepath", file_entry_path}, + {"__tostring", file_entry_path}, + {NULL, NULL}}; + +static auto fs_new_iterator(lua_State* state) -> int { + // Takes a filepath as a string and returns a new FileIterator + // on that directory + std::string filepath = luaL_checkstring(state, -1); + database::FileIterator iter(filepath); + push_iterator(state, iter); + return 1; +} + +static const struct luaL_Reg kDatabaseFuncs[] = {{"iterator", fs_new_iterator}, + {NULL, NULL}}; + +static auto lua_filesystem(lua_State* state) -> int { + luaL_newmetatable(state, kFileIteratorMetatable); + lua_pushliteral(state, "__index"); + lua_pushvalue(state, -2); + lua_settable(state, -3); // metatable.__index = metatable + luaL_setfuncs(state, kFileIteratorFuncs, 0); + + luaL_newmetatable(state, kFileEntryMetatable); + lua_pushliteral(state, "__index"); + lua_pushvalue(state, -2); + lua_settable(state, -3); // metatable.__index = metatable + luaL_setfuncs(state, kFileEntryFuncs, 0); + + luaL_newlib(state, kDatabaseFuncs); + return 1; +} + +auto RegisterFileSystemModule(lua_State* s) -> void { + luaL_requiref(s, "filesystem", lua_filesystem, true); + lua_pop(s, 1); +} + +} // namespace lua From fb3d6a7b86991fe38da9a2741db8801785aa4c1b Mon Sep 17 00:00:00 2001 From: ailurux Date: Thu, 2 May 2024 17:08:40 +1000 Subject: [PATCH 2/8] WIP: Add destructor for FileIterator --- src/lua/file_iterator.cpp | 5 +++++ src/lua/include/file_iterator.hpp | 1 + 2 files changed, 6 insertions(+) diff --git a/src/lua/file_iterator.cpp b/src/lua/file_iterator.cpp index 7f2929ba..8de1a923 100644 --- a/src/lua/file_iterator.cpp +++ b/src/lua/file_iterator.cpp @@ -28,6 +28,11 @@ FileIterator::FileIterator(std::string filepath) } } +FileIterator::~FileIterator() { + auto lock = drivers::acquire_spi(); + f_closedir(&dir_); +} + auto FileIterator::value() const -> const std::optional& { return current_; } diff --git a/src/lua/include/file_iterator.hpp b/src/lua/include/file_iterator.hpp index 6fc58245..1632949e 100644 --- a/src/lua/include/file_iterator.hpp +++ b/src/lua/include/file_iterator.hpp @@ -25,6 +25,7 @@ struct FileEntry { class FileIterator { public: FileIterator(std::string filepath); + ~FileIterator(); auto value() const -> const std::optional&; auto next() -> void; From eeb3f2d406a951b423b83f559fe749df0b4f745a Mon Sep 17 00:00:00 2001 From: ailurux Date: Mon, 6 May 2024 12:36:16 +1000 Subject: [PATCH 3/8] WIP: File browser, needs bug fixes --- lua/file_browser.lua | 73 +++++++++++++++++++++++++++++++ lua/main_menu.lua | 10 +++++ src/lua/file_iterator.cpp | 31 +++++++------ src/lua/include/file_iterator.hpp | 1 + src/lua/lua_filesystem.cpp | 36 ++++++++++++--- 5 files changed, 132 insertions(+), 19 deletions(-) create mode 100644 lua/file_browser.lua diff --git a/lua/file_browser.lua b/lua/file_browser.lua new file mode 100644 index 00000000..57ebde58 --- /dev/null +++ b/lua/file_browser.lua @@ -0,0 +1,73 @@ +local lvgl = require("lvgl") +local widgets = require("widgets") +local backstack = require("backstack") +local font = require("font") +local queue = require("queue") +local playing = require("playing") +local styles = require("styles") +local playback = require("playback") +local theme = require("theme") +local screen = require("screen") +local filesystem = require("filesystem") + +return screen:new{ + createUi = function(self) + self.root = lvgl.Object(nil, { + flex = { + flex_direction = "column", + flex_wrap = "wrap", + justify_content = "flex-start", + align_items = "flex-start", + align_content = "flex-start" + }, + w = lvgl.HOR_RES(), + h = lvgl.VER_RES() + }) + self.root:center() + + self.status_bar = widgets.StatusBar(self, { + back_cb = backstack.pop, + title = self.title + }) + + local header = self.root:Object{ + flex = { + flex_direction = "column", + flex_wrap = "wrap", + justify_content = "flex-start", + align_items = "flex-start", + align_content = "flex-start" + }, + w = lvgl.HOR_RES(), + h = lvgl.SIZE_CONTENT, + pad_left = 4, + pad_right = 4, + pad_bottom = 2, + bg_opa = lvgl.OPA(100), + scrollbar_mode = lvgl.SCROLLBAR_MODE.OFF + } + theme.set_style(header, "header") + + if self.breadcrumb then + header:Label{ + text = self.breadcrumb, + text_font = font.fusion_10 + } + end + + local recycle_list = widgets.RecyclerList(self.root, self.iterator, { + callback = function(item) + return function() + local is_dir = item:is_directory() + if is_dir then + backstack.push(require("file_browser"):new{ + title = self.title, + iterator = filesystem.iterator(tostring(item)), + breadcrumb = tostring(item) + }) + end + end + end + }) + end +} diff --git a/lua/main_menu.lua b/lua/main_menu.lua index 5fd6417f..9c52340b 100644 --- a/lua/main_menu.lua +++ b/lua/main_menu.lua @@ -5,6 +5,7 @@ local backstack = require("backstack") local browser = require("browser") local playing = require("playing") local styles = require("styles") +local filesystem = require("filesystem") local screen = require("screen") return widgets.MenuScreen:new { @@ -35,6 +36,15 @@ return widgets.MenuScreen:new { btn:add_style(styles.list_item) end + local files = list:add_btn(nil, "Files") + files:onClicked(function() + backstack.push(require("file_browser"):new { + title = "Files", + iterator = filesystem.iterator(""), + }) + end) + files:add_style(styles.list_item) + local settings = list:add_btn(nil, "Settings") settings:onClicked(function() backstack.push(require("settings"):new()) diff --git a/src/lua/file_iterator.cpp b/src/lua/file_iterator.cpp index 8de1a923..58b256b2 100644 --- a/src/lua/file_iterator.cpp +++ b/src/lua/file_iterator.cpp @@ -17,12 +17,13 @@ namespace database { FileIterator::FileIterator(std::string filepath) : original_path_(filepath), - current_() + current_(), + offset_(-1) { auto lock = drivers::acquire_spi(); - const TCHAR* next_path = static_cast(filepath.c_str()); - FRESULT res = f_opendir(&dir_, next_path); + const TCHAR* path = static_cast(filepath.c_str()); + FRESULT res = f_opendir(&dir_, path); if (res != FR_OK) { ESP_LOGE(kTag, "Error opening directory: %s", filepath.c_str()); } @@ -42,36 +43,40 @@ auto FileIterator::next() -> void { } auto FileIterator::prev() -> void { - iterate(true); + if (offset_ == 0) { + current_.reset(); + return; + } + f_rewinddir(&dir_); + auto new_offset = offset_-1; + offset_ = -1; + for (int i = 0; i < new_offset; i++) { + iterate(false); + } } auto FileIterator::iterate(bool reverse) -> bool { FILINFO info; - if (reverse) { - f_rewinddir(&dir_); - } { auto lock = drivers::acquire_spi(); auto res = f_readdir(&dir_, &info); if (res != FR_OK) { - ESP_LOGI(kTag, "AAAAAAAAAAAAAAAAAAA"); - ESP_LOGI(kTag, "%d", res); + ESP_LOGE(kTag, "Error reading directory. Error: %d", res); return false; } } if (info.fname[0] == 0) { // End of directory + // Set value to nil current_.reset(); - ESP_LOGI(kTag, "End of dir"); - } else { // Update current value - ESP_LOGI(kTag, "File: %s", info.fname); + offset_++; current_ = FileEntry{ .isHidden = (info.fattrib & AM_HID) > 0, .isDirectory = (info.fattrib & AM_DIR) > 0, .isTrack = false, // TODO - .filepath = original_path_ + info.fname, + .filepath = original_path_ + (original_path_.size()>0?"/":"") + info.fname, }; } diff --git a/src/lua/include/file_iterator.hpp b/src/lua/include/file_iterator.hpp index 1632949e..da1a6eeb 100644 --- a/src/lua/include/file_iterator.hpp +++ b/src/lua/include/file_iterator.hpp @@ -36,6 +36,7 @@ class FileIterator { std::string original_path_; std::optional current_; + int offset_; auto iterate(bool reverse = false) -> bool; }; diff --git a/src/lua/lua_filesystem.cpp b/src/lua/lua_filesystem.cpp index 5c690c16..f0dbaf9a 100644 --- a/src/lua/lua_filesystem.cpp +++ b/src/lua/lua_filesystem.cpp @@ -88,11 +88,11 @@ static auto fs_iterate(lua_State* state) -> int { return 1; } -// static auto db_iterator_clone(lua_State* state) -> int { -// database::Iterator* it = db_check_iterator(state, 1); -// push_iterator(state, *it); -// return 1; -// } +static auto fs_iterator_clone(lua_State* state) -> int { + database::FileIterator* it = check_file_iterator(state, 1); + push_iterator(state, *it); + return 1; +} static auto fs_iterator_gc(lua_State* state) -> int { database::FileIterator* it = check_file_iterator(state, 1); @@ -102,7 +102,7 @@ static auto fs_iterator_gc(lua_State* state) -> int { static const struct luaL_Reg kFileIteratorFuncs[] = {{"next", fs_iterate}, {"prev", fs_iterate_prev}, - // {"clone", db_iterator_clone}, + {"clone", fs_iterator_clone}, {"__call", fs_iterate}, {"__gc", fs_iterator_gc}, {NULL, NULL}}; @@ -114,7 +114,31 @@ static auto file_entry_path(lua_State* state) -> int { return 1; } +static auto file_entry_is_dir(lua_State* state) -> int { + LuaFileEntry* data = reinterpret_cast( + luaL_checkudata(state, 1, kFileEntryMetatable)); + lua_pushboolean(state, data->isDirectory); + return 1; +} + +static auto file_entry_is_hidden(lua_State* state) -> int { + LuaFileEntry* data = reinterpret_cast( + luaL_checkudata(state, 1, kFileEntryMetatable)); + lua_pushboolean(state, data->isHidden); + return 1; +} + +static auto file_entry_is_track(lua_State* state) -> int { + LuaFileEntry* data = reinterpret_cast( + luaL_checkudata(state, 1, kFileEntryMetatable)); + lua_pushboolean(state, data->isTrack); + return 1; +} + static const struct luaL_Reg kFileEntryFuncs[] = {{"filepath", file_entry_path}, + {"is_directory", file_entry_is_dir}, + {"is_hidden", file_entry_is_hidden}, + {"is_track", file_entry_is_track}, {"__tostring", file_entry_path}, {NULL, NULL}}; From 8019c7691889cde4c3d40bbd78d485a92d713bbf Mon Sep 17 00:00:00 2001 From: ailurux Date: Mon, 6 May 2024 15:48:04 +1000 Subject: [PATCH 4/8] File browser and track browser bug fixes --- lua/widgets.lua | 29 ++++++++++------------------- src/lua/file_iterator.cpp | 3 ++- src/lua/include/file_iterator.hpp | 1 + 3 files changed, 13 insertions(+), 20 deletions(-) diff --git a/lua/widgets.lua b/lua/widgets.lua index 4d7ff077..6d37e9ac 100644 --- a/lua/widgets.lua +++ b/lua/widgets.lua @@ -288,13 +288,8 @@ function widgets.RecyclerList(parent, iterator, opts) end btn:onevent(lvgl.EVENT.FOCUSED, function() if refreshing then return end - selected = this_item - if this_item > last_selected and this_item > 3 then + if this_item > last_selected and this_item - first_index > 5 then -- moving forward - if moving_back == true then - fwd_iterator:next() - moving_back = false - end local to_add = fwd_iterator:next() if to_add then remove_top() @@ -303,19 +298,15 @@ function widgets.RecyclerList(parent, iterator, opts) end if this_item < last_selected then -- moving backward - if last_index - this_item > 3 then - if moving_back == false then - -- Special case for the element we switch on - bck_iterator:prev() - moving_back = true - end - if (last_index > 10) then - remove_last() - end - if (first_index > 0) then - add_item(bck_iterator:prev(), first_index-1) - end - end + if (last_index - first_index > 10) then + remove_last() + end + if (first_index > 0 and this_item - first_index < 5) then + local to_add = bck_iterator:prev(); + if to_add then + add_item(to_add, first_index-1) + end + end end last_selected = this_item refresh_group() diff --git a/src/lua/file_iterator.cpp b/src/lua/file_iterator.cpp index 58b256b2..194859a6 100644 --- a/src/lua/file_iterator.cpp +++ b/src/lua/file_iterator.cpp @@ -50,7 +50,7 @@ auto FileIterator::prev() -> void { f_rewinddir(&dir_); auto new_offset = offset_-1; offset_ = -1; - for (int i = 0; i < new_offset; i++) { + for (int i = 0; i <= new_offset; i++) { iterate(false); } } @@ -73,6 +73,7 @@ auto FileIterator::iterate(bool reverse) -> bool { // Update current value offset_++; current_ = FileEntry{ + .index = offset_, .isHidden = (info.fattrib & AM_HID) > 0, .isDirectory = (info.fattrib & AM_DIR) > 0, .isTrack = false, // TODO diff --git a/src/lua/include/file_iterator.hpp b/src/lua/include/file_iterator.hpp index da1a6eeb..82d6f397 100644 --- a/src/lua/include/file_iterator.hpp +++ b/src/lua/include/file_iterator.hpp @@ -16,6 +16,7 @@ namespace database { // Note for when reading FILINFO, that we are in LFN mode: // http://elm-chan.org/fsw/ff/doc/sfileinfo.html struct FileEntry { + int index; bool isHidden; bool isDirectory; bool isTrack; From ee5657cb447fcd7721385c957784705699ca0cdb Mon Sep 17 00:00:00 2001 From: ailurux Date: Fri, 10 May 2024 13:07:12 +1000 Subject: [PATCH 5/8] Fix imports after merge --- src/tangara/lua/bridge.cpp | 2 ++ src/tangara/lua/file_iterator.cpp | 4 ++-- src/tangara/lua/lua_filesystem.cpp | 2 +- src/tangara/lua/lua_filesystem.hpp | 2 +- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/tangara/lua/bridge.cpp b/src/tangara/lua/bridge.cpp index 07c299a7..f1b17636 100644 --- a/src/tangara/lua/bridge.cpp +++ b/src/tangara/lua/bridge.cpp @@ -18,6 +18,7 @@ #include "lua.hpp" #include "lua/lua_controls.hpp" #include "lua/lua_database.hpp" +#include "lua/lua_filesystem.hpp" #include "lua/lua_queue.hpp" #include "lua/lua_screen.hpp" #include "lua/lua_theme.hpp" @@ -86,6 +87,7 @@ auto Bridge::installBaseModules(lua_State* L) -> void { RegisterControlsModule(L); RegisterDatabaseModule(L); RegisterQueueModule(L); + RegisterFileSystemModule(L); RegisterVersionModule(L); RegisterThemeModule(L); RegisterScreenModule(L); diff --git a/src/tangara/lua/file_iterator.cpp b/src/tangara/lua/file_iterator.cpp index 194859a6..3afc57aa 100644 --- a/src/tangara/lua/file_iterator.cpp +++ b/src/tangara/lua/file_iterator.cpp @@ -3,13 +3,13 @@ * * SPDX-License-Identifier: GPL-3.0-only */ -#include "file_iterator.hpp" +#include "lua/file_iterator.hpp" #include "esp_log.h" #include #include "ff.h" -#include "spi.hpp" +#include "drivers/spi.hpp" namespace database { diff --git a/src/tangara/lua/lua_filesystem.cpp b/src/tangara/lua/lua_filesystem.cpp index f0dbaf9a..ea08ca48 100644 --- a/src/tangara/lua/lua_filesystem.cpp +++ b/src/tangara/lua/lua_filesystem.cpp @@ -4,7 +4,7 @@ * SPDX-License-Identifier: GPL-3.0-only */ -#include "lua_filesystem.hpp" +#include "lua/lua_filesystem.hpp" #include #include #include "lauxlib.h" diff --git a/src/tangara/lua/lua_filesystem.hpp b/src/tangara/lua/lua_filesystem.hpp index 2a829405..cb7170bd 100644 --- a/src/tangara/lua/lua_filesystem.hpp +++ b/src/tangara/lua/lua_filesystem.hpp @@ -6,7 +6,7 @@ #pragma once #include "lua.hpp" -#include "file_iterator.hpp" +#include "lua/file_iterator.hpp" namespace lua { From 0062eb9a9ea265c7802d3c4bc8c1cd043cefbea3 Mon Sep 17 00:00:00 2001 From: ailurux Date: Fri, 10 May 2024 13:16:29 +1000 Subject: [PATCH 6/8] Rename widget to InfiniteList --- lua/browser.lua | 2 +- lua/file_browser.lua | 2 +- lua/widgets.lua | 20 ++++++++++---------- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lua/browser.lua b/lua/browser.lua index 96ebbcab..c314e8f8 100644 --- a/lua/browser.lua +++ b/lua/browser.lua @@ -81,7 +81,7 @@ return screen:new{ backstack.push(playing:new()) end) - local recycle_list = widgets.RecyclerList(self.root, self.iterator, { + local infinite_list = widgets.InfiniteList(self.root, self.iterator, { callback = function(item) return function() local contents = item:contents() diff --git a/lua/file_browser.lua b/lua/file_browser.lua index 57ebde58..bd1e7d8e 100644 --- a/lua/file_browser.lua +++ b/lua/file_browser.lua @@ -55,7 +55,7 @@ return screen:new{ } end - local recycle_list = widgets.RecyclerList(self.root, self.iterator, { + local infinite_list = widgets.InfiniteList(self.root, self.iterator, { callback = function(item) return function() local is_dir = item:is_directory() diff --git a/lua/widgets.lua b/lua/widgets.lua index 6d37e9ac..bd8c84f8 100644 --- a/lua/widgets.lua +++ b/lua/widgets.lua @@ -215,10 +215,10 @@ function widgets.IconBtn(parent, icon, text) return btn end -function widgets.RecyclerList(parent, iterator, opts) - local recycler_list = {} +function widgets.InfiniteList(parent, iterator, opts) + local infinite_list = {} - recycler_list.root = lvgl.List(parent, { + infinite_list.root = lvgl.List(parent, { w = lvgl.PCT(100), h = lvgl.PCT(100), flex_grow = 1, @@ -230,13 +230,13 @@ function widgets.RecyclerList(parent, iterator, opts) refreshing = true local group = lvgl.group.get_default() local focused_obj = group:get_focused() - local num_children = recycler_list.root:get_child_cnt() + local num_children = infinite_list.root:get_child_cnt() -- remove all children from the group and re-add them for i = 0, num_children-1 do - lvgl.group.remove_obj(recycler_list.root:get_child(i)) + lvgl.group.remove_obj(infinite_list.root:get_child(i)) end for i = 0, num_children-1 do - group:add_obj(recycler_list.root:get_child(i)) + group:add_obj(infinite_list.root:get_child(i)) end if (focused_obj) then lvgl.group.focus_obj(focused_obj) @@ -252,14 +252,14 @@ function widgets.RecyclerList(parent, iterator, opts) local first_index = 0 local function remove_top() - local obj = recycler_list.root:get_child(0) + local obj = infinite_list.root:get_child(0) obj:delete() first_index = first_index + 1 bck_iterator:next() end local function remove_last() - local obj = recycler_list.root:get_child(-1) + local obj = infinite_list.root:get_child(-1) obj:delete() last_index = last_index - 1 fwd_iterator:prev() @@ -278,7 +278,7 @@ function widgets.RecyclerList(parent, iterator, opts) add_to_top = true end if this_item > last_index then last_index = index end - local btn = recycler_list.root:add_btn(nil, tostring(item)) + local btn = infinite_list.root:add_btn(nil, tostring(item)) if add_to_top then btn:move_to_index(0) end @@ -323,7 +323,7 @@ function widgets.RecyclerList(parent, iterator, opts) add_item(val, idx) end - return recycler_list + return infinite_list end return widgets From e06610f3a6d2bf034a20cc1b59fb25dc5efdda0a Mon Sep 17 00:00:00 2001 From: ailurux Date: Fri, 10 May 2024 13:27:01 +1000 Subject: [PATCH 7/8] Minor fixes before PR --- src/tangara/lua/file_iterator.cpp | 4 ++-- src/tangara/lua/file_iterator.hpp | 4 ++-- src/tangara/lua/lua_filesystem.cpp | 30 +++++++++++++++--------------- src/tangara/lua/lua_filesystem.hpp | 2 +- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/tangara/lua/file_iterator.cpp b/src/tangara/lua/file_iterator.cpp index 3afc57aa..d0eb0bae 100644 --- a/src/tangara/lua/file_iterator.cpp +++ b/src/tangara/lua/file_iterator.cpp @@ -11,7 +11,7 @@ #include "ff.h" #include "drivers/spi.hpp" -namespace database { +namespace lua { [[maybe_unused]] static const char* kTag = "FileIterator"; @@ -84,4 +84,4 @@ auto FileIterator::iterate(bool reverse) -> bool { return true; } -} // namespace database \ No newline at end of file +} // namespace lua \ No newline at end of file diff --git a/src/tangara/lua/file_iterator.hpp b/src/tangara/lua/file_iterator.hpp index 82d6f397..b803062c 100644 --- a/src/tangara/lua/file_iterator.hpp +++ b/src/tangara/lua/file_iterator.hpp @@ -11,7 +11,7 @@ #include "ff.h" -namespace database { +namespace lua { // Note for when reading FILINFO, that we are in LFN mode: // http://elm-chan.org/fsw/ff/doc/sfileinfo.html @@ -42,4 +42,4 @@ class FileIterator { auto iterate(bool reverse = false) -> bool; }; -} // namespace database \ No newline at end of file +} // namespace lua \ No newline at end of file diff --git a/src/tangara/lua/lua_filesystem.cpp b/src/tangara/lua/lua_filesystem.cpp index ea08ca48..de51f555 100644 --- a/src/tangara/lua/lua_filesystem.cpp +++ b/src/tangara/lua/lua_filesystem.cpp @@ -28,7 +28,7 @@ struct LuaFileEntry { static_assert(std::is_trivially_destructible()); static_assert(std::is_trivially_copy_assignable()); -static auto push_lua_file_entry(lua_State* L, const database::FileEntry& r) -> void { +static auto push_lua_file_entry(lua_State* L, const lua::FileEntry& r) -> void { // Create and init the userdata. LuaFileEntry* file_entry = reinterpret_cast( lua_newuserdata(L, sizeof(LuaFileEntry) + r.filepath.size())); @@ -46,24 +46,24 @@ static auto push_lua_file_entry(lua_State* L, const database::FileEntry& r) -> v std::memcpy(file_entry->path, r.filepath.data(), r.filepath.size()); } -auto check_file_iterator(lua_State* L, int stack_pos) -> database::FileIterator* { - database::FileIterator* it = *reinterpret_cast( +auto check_file_iterator(lua_State* L, int stack_pos) -> lua::FileIterator* { + lua::FileIterator* it = *reinterpret_cast( luaL_checkudata(L, stack_pos, kFileIteratorMetatable)); return it; } -static auto push_iterator(lua_State* state, const database::FileIterator& it) +static auto push_iterator(lua_State* state, const lua::FileIterator& it) -> void { - database::FileIterator** data = reinterpret_cast( + lua::FileIterator** data = reinterpret_cast( lua_newuserdata(state, sizeof(uintptr_t))); - *data = new database::FileIterator(it); // TODO... + *data = new lua::FileIterator(it); // TODO... luaL_setmetatable(state, kFileIteratorMetatable); } static auto fs_iterate_prev(lua_State* state) -> int { - database::FileIterator* it = check_file_iterator(state, 1); + lua::FileIterator* it = check_file_iterator(state, 1); it->prev(); - std::optional res = it->value(); + std::optional res = it->value(); if (res) { push_lua_file_entry(state, *res); @@ -75,9 +75,9 @@ static auto fs_iterate_prev(lua_State* state) -> int { } static auto fs_iterate(lua_State* state) -> int { - database::FileIterator* it = check_file_iterator(state, 1); + lua::FileIterator* it = check_file_iterator(state, 1); it->next(); - std::optional res = it->value(); + std::optional res = it->value(); if (res) { push_lua_file_entry(state, *res); @@ -89,13 +89,13 @@ static auto fs_iterate(lua_State* state) -> int { } static auto fs_iterator_clone(lua_State* state) -> int { - database::FileIterator* it = check_file_iterator(state, 1); + lua::FileIterator* it = check_file_iterator(state, 1); push_iterator(state, *it); return 1; } static auto fs_iterator_gc(lua_State* state) -> int { - database::FileIterator* it = check_file_iterator(state, 1); + lua::FileIterator* it = check_file_iterator(state, 1); delete it; return 0; } @@ -146,12 +146,12 @@ static auto fs_new_iterator(lua_State* state) -> int { // Takes a filepath as a string and returns a new FileIterator // on that directory std::string filepath = luaL_checkstring(state, -1); - database::FileIterator iter(filepath); + lua::FileIterator iter(filepath); push_iterator(state, iter); return 1; } -static const struct luaL_Reg kDatabaseFuncs[] = {{"iterator", fs_new_iterator}, +static const struct luaL_Reg kFilesystemFuncs[] = {{"iterator", fs_new_iterator}, {NULL, NULL}}; static auto lua_filesystem(lua_State* state) -> int { @@ -167,7 +167,7 @@ static auto lua_filesystem(lua_State* state) -> int { lua_settable(state, -3); // metatable.__index = metatable luaL_setfuncs(state, kFileEntryFuncs, 0); - luaL_newlib(state, kDatabaseFuncs); + luaL_newlib(state, kFilesystemFuncs); return 1; } diff --git a/src/tangara/lua/lua_filesystem.hpp b/src/tangara/lua/lua_filesystem.hpp index cb7170bd..d0f51498 100644 --- a/src/tangara/lua/lua_filesystem.hpp +++ b/src/tangara/lua/lua_filesystem.hpp @@ -10,7 +10,7 @@ namespace lua { -auto check_file_iterator(lua_State*, int stack_pos) -> database::FileIterator*; +auto check_file_iterator(lua_State*, int stack_pos) -> lua::FileIterator*; auto RegisterFileSystemModule(lua_State*) -> void; From ce9861260d5642eb496e447eb7e93de5938c6b1f Mon Sep 17 00:00:00 2001 From: ailurux Date: Fri, 10 May 2024 14:18:04 +1000 Subject: [PATCH 8/8] Remove unused local var --- lua/browser.lua | 2 +- lua/file_browser.lua | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/browser.lua b/lua/browser.lua index c314e8f8..7ea8e240 100644 --- a/lua/browser.lua +++ b/lua/browser.lua @@ -81,7 +81,7 @@ return screen:new{ backstack.push(playing:new()) end) - local infinite_list = widgets.InfiniteList(self.root, self.iterator, { + widgets.InfiniteList(self.root, self.iterator, { callback = function(item) return function() local contents = item:contents() diff --git a/lua/file_browser.lua b/lua/file_browser.lua index bd1e7d8e..91b84c84 100644 --- a/lua/file_browser.lua +++ b/lua/file_browser.lua @@ -55,7 +55,7 @@ return screen:new{ } end - local infinite_list = widgets.InfiniteList(self.root, self.iterator, { + widgets.InfiniteList(self.root, self.iterator, { callback = function(item) return function() local is_dir = item:is_directory()