summaryrefslogtreecommitdiff
path: root/src/tangara/lua/registry.cpp
diff options
context:
space:
mode:
authorailurux <ailuruxx@gmail.com>2024-05-10 13:06:20 +1000
committerailurux <ailuruxx@gmail.com>2024-05-10 13:06:20 +1000
commit3f177cdb8880abf199f4445f1398cd69fb813892 (patch)
treee20de4949b1344c826e5af41ab701f3db75b21bc /src/tangara/lua/registry.cpp
parent8019c7691889cde4c3d40bbd78d485a92d713bbf (diff)
parente4ce7c4ac23402e09be8d6a52e0f739c0dff4ff0 (diff)
downloadtangara-fw-3f177cdb8880abf199f4445f1398cd69fb813892.tar.gz
Merge branch 'main' into file-browser
Diffstat (limited to 'src/tangara/lua/registry.cpp')
-rw-r--r--src/tangara/lua/registry.cpp73
1 files changed, 73 insertions, 0 deletions
diff --git a/src/tangara/lua/registry.cpp b/src/tangara/lua/registry.cpp
new file mode 100644
index 00000000..d33594a3
--- /dev/null
+++ b/src/tangara/lua/registry.cpp
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2023 jacqueline <me@jacqueline.id.au>
+ *
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+
+#include "lua/lua_registry.hpp"
+
+#include <iostream>
+#include <memory>
+
+#include "esp_heap_caps.h"
+#include "esp_log.h"
+#include "lua.hpp"
+
+#include "events/event_queue.hpp"
+#include "lua/bridge.hpp"
+#include "memory_resource.hpp"
+#include "system_fsm/service_locator.hpp"
+#include "ui/ui_events.hpp"
+
+namespace lua {
+
+[[maybe_unused]] static constexpr char kTag[] = "lua";
+
+auto Registry::instance(system_fsm::ServiceLocator& s) -> Registry& {
+ static Registry sRegistry{s};
+ return sRegistry;
+}
+
+Registry::Registry(system_fsm::ServiceLocator& services)
+ : services_(services), bridge_(new Bridge(services)) {}
+
+auto Registry::uiThread() -> std::shared_ptr<LuaThread> {
+ if (!ui_thread_) {
+ ui_thread_ = newThread();
+ bridge_->installLvgl(ui_thread_->state());
+ }
+ return ui_thread_;
+}
+
+auto Registry::newThread() -> std::shared_ptr<LuaThread> {
+ std::shared_ptr<LuaThread> thread{LuaThread::Start(services_)};
+ bridge_->installBaseModules(thread->state());
+ for (auto& module : modules_) {
+ bridge_->installPropertyModule(thread->state(), module.first,
+ module.second);
+ }
+ threads_.push_back(thread);
+ return thread;
+}
+
+auto Registry::AddPropertyModule(
+ const std::string& name,
+ std::vector<std::pair<std::string, std::variant<LuaFunction, Property*>>>
+ properties) -> void {
+ modules_.push_back(std::make_pair(name, properties));
+
+ // Any live threads will need to be updated to include the new module.
+ auto it = threads_.begin();
+ while (it != threads_.end()) {
+ auto thread = it->lock();
+ if (!thread) {
+ // Thread has been destroyed; stop tracking it.
+ it = threads_.erase(it);
+ } else {
+ bridge_->installPropertyModule(thread->state(), name, properties);
+ it++;
+ }
+ }
+}
+
+} // namespace lua