summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lua/browser.lua2
-rw-r--r--lua/file_browser.lua73
-rw-r--r--lua/main_menu.lua10
-rw-r--r--lua/widgets.lua49
-rw-r--r--src/tangara/lua/bridge.cpp2
-rw-r--r--src/tangara/lua/file_iterator.cpp87
-rw-r--r--src/tangara/lua/file_iterator.hpp45
-rw-r--r--src/tangara/lua/lua_filesystem.cpp179
-rw-r--r--src/tangara/lua/lua_filesystem.hpp17
9 files changed, 434 insertions, 30 deletions
diff --git a/lua/browser.lua b/lua/browser.lua
index 96ebbcab..7ea8e240 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, {
+ 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
new file mode 100644
index 00000000..91b84c84
--- /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
+
+ widgets.InfiniteList(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/lua/widgets.lua b/lua/widgets.lua
index 4d7ff077..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
@@ -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()
@@ -332,7 +323,7 @@ function widgets.RecyclerList(parent, iterator, opts)
add_item(val, idx)
end
- return recycler_list
+ return infinite_list
end
return widgets
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
new file mode 100644
index 00000000..d0eb0bae
--- /dev/null
+++ b/src/tangara/lua/file_iterator.cpp
@@ -0,0 +1,87 @@
+/*
+ * Copyright 2023 ailurux <ailuruxx@gmail.com>
+ *
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+#include "lua/file_iterator.hpp"
+#include "esp_log.h"
+
+#include <string>
+
+#include "ff.h"
+#include "drivers/spi.hpp"
+
+namespace lua {
+
+[[maybe_unused]] static const char* kTag = "FileIterator";
+
+FileIterator::FileIterator(std::string filepath)
+: original_path_(filepath),
+ current_(),
+ offset_(-1)
+ {
+ auto lock = drivers::acquire_spi();
+
+ const TCHAR* path = static_cast<const TCHAR*>(filepath.c_str());
+ FRESULT res = f_opendir(&dir_, path);
+ if (res != FR_OK) {
+ ESP_LOGE(kTag, "Error opening directory: %s", filepath.c_str());
+ }
+}
+
+FileIterator::~FileIterator() {
+ auto lock = drivers::acquire_spi();
+ f_closedir(&dir_);
+}
+
+auto FileIterator::value() const -> const std::optional<FileEntry>& {
+ return current_;
+}
+
+auto FileIterator::next() -> void {
+ iterate(false);
+}
+
+auto FileIterator::prev() -> void {
+ 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;
+ {
+ auto lock = drivers::acquire_spi();
+ auto res = f_readdir(&dir_, &info);
+ if (res != FR_OK) {
+ 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();
+ } else {
+ // Update current value
+ offset_++;
+ current_ = FileEntry{
+ .index = offset_,
+ .isHidden = (info.fattrib & AM_HID) > 0,
+ .isDirectory = (info.fattrib & AM_DIR) > 0,
+ .isTrack = false, // TODO
+ .filepath = original_path_ + (original_path_.size()>0?"/":"") + info.fname,
+
+ };
+ }
+ return true;
+}
+
+} // namespace lua \ No newline at end of file
diff --git a/src/tangara/lua/file_iterator.hpp b/src/tangara/lua/file_iterator.hpp
new file mode 100644
index 00000000..b803062c
--- /dev/null
+++ b/src/tangara/lua/file_iterator.hpp
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2023 ailurux <ailuruxx@gmail.com>
+ *
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+
+#pragma once
+
+#include <string>
+#include <optional>
+
+#include "ff.h"
+
+namespace lua {
+
+// 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;
+ std::string filepath;
+};
+
+class FileIterator {
+ public:
+ FileIterator(std::string filepath);
+ ~FileIterator();
+
+ auto value() const -> const std::optional<FileEntry>&;
+ auto next() -> void;
+ auto prev() -> void;
+
+ private:
+ FF_DIR dir_;
+ std::string original_path_;
+
+ std::optional<FileEntry> current_;
+ int offset_;
+
+ auto iterate(bool reverse = false) -> bool;
+};
+
+} // namespace lua \ No newline at end of file
diff --git a/src/tangara/lua/lua_filesystem.cpp b/src/tangara/lua/lua_filesystem.cpp
new file mode 100644
index 00000000..de51f555
--- /dev/null
+++ b/src/tangara/lua/lua_filesystem.cpp
@@ -0,0 +1,179 @@
+/*
+ * Copyright 2023 jacqueline <me@jacqueline.id.au>
+ *
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+
+#include "lua/lua_filesystem.hpp"
+#include <string>
+#include <cstring>
+#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<LuaFileEntry>());
+static_assert(std::is_trivially_copy_assignable<LuaFileEntry>());
+
+static auto push_lua_file_entry(lua_State* L, const lua::FileEntry& r) -> void {
+ // Create and init the userdata.
+ LuaFileEntry* file_entry = reinterpret_cast<LuaFileEntry*>(
+ 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) -> lua::FileIterator* {
+ lua::FileIterator* it = *reinterpret_cast<lua::FileIterator**>(
+ luaL_checkudata(L, stack_pos, kFileIteratorMetatable));
+ return it;
+}
+
+static auto push_iterator(lua_State* state, const lua::FileIterator& it)
+ -> void {
+ lua::FileIterator** data = reinterpret_cast<lua::FileIterator**>(
+ lua_newuserdata(state, sizeof(uintptr_t)));
+ *data = new lua::FileIterator(it); // TODO...
+ luaL_setmetatable(state, kFileIteratorMetatable);
+}
+
+static auto fs_iterate_prev(lua_State* state) -> int {
+ lua::FileIterator* it = check_file_iterator(state, 1);
+ it->prev();
+ std::optional<lua::FileEntry> 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 {
+ lua::FileIterator* it = check_file_iterator(state, 1);
+ it->next();
+ std::optional<lua::FileEntry> res = it->value();
+
+ if (res) {
+ push_lua_file_entry(state, *res);
+ } else {
+ lua_pushnil(state);
+ }
+
+ return 1;
+}
+
+static auto fs_iterator_clone(lua_State* state) -> int {
+ lua::FileIterator* it = check_file_iterator(state, 1);
+ push_iterator(state, *it);
+ return 1;
+}
+
+static auto fs_iterator_gc(lua_State* state) -> int {
+ lua::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", fs_iterator_clone},
+ {"__call", fs_iterate},
+ {"__gc", fs_iterator_gc},
+ {NULL, NULL}};
+
+static auto file_entry_path(lua_State* state) -> int {
+ LuaFileEntry* data = reinterpret_cast<LuaFileEntry*>(
+ luaL_checkudata(state, 1, kFileEntryMetatable));
+ lua_pushlstring(state, data->path, data->path_size);
+ return 1;
+}
+
+static auto file_entry_is_dir(lua_State* state) -> int {
+ LuaFileEntry* data = reinterpret_cast<LuaFileEntry*>(
+ 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<LuaFileEntry*>(
+ 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<LuaFileEntry*>(
+ 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}};
+
+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);
+ lua::FileIterator iter(filepath);
+ push_iterator(state, iter);
+ return 1;
+}
+
+static const struct luaL_Reg kFilesystemFuncs[] = {{"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, kFilesystemFuncs);
+ return 1;
+}
+
+auto RegisterFileSystemModule(lua_State* s) -> void {
+ luaL_requiref(s, "filesystem", lua_filesystem, true);
+ lua_pop(s, 1);
+}
+
+} // namespace lua
diff --git a/src/tangara/lua/lua_filesystem.hpp b/src/tangara/lua/lua_filesystem.hpp
new file mode 100644
index 00000000..d0f51498
--- /dev/null
+++ b/src/tangara/lua/lua_filesystem.hpp
@@ -0,0 +1,17 @@
+/*
+ * Copyright 2023 ailurux <ailuruxx@gmail.com>
+ *
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+#pragma once
+
+#include "lua.hpp"
+#include "lua/file_iterator.hpp"
+
+namespace lua {
+
+auto check_file_iterator(lua_State*, int stack_pos) -> lua::FileIterator*;
+
+auto RegisterFileSystemModule(lua_State*) -> void;
+
+} // namespace lua