From 5ba33f846129f3e6c0505b19f94634b24505e828 Mon Sep 17 00:00:00 2001 From: Vaxry <43317083+vaxerski@users.noreply.github.com> Date: Sun, 26 Apr 2026 15:16:36 +0100 Subject: config/lua: init lua config manager, use lua if available (#13817) Adds a new lua config manager, which will automatically be used if there is a lua config present. --- src/config/lua/ConfigManager.hpp | 186 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 186 insertions(+) create mode 100644 src/config/lua/ConfigManager.hpp (limited to 'src/config/lua/ConfigManager.hpp') diff --git a/src/config/lua/ConfigManager.hpp b/src/config/lua/ConfigManager.hpp new file mode 100644 index 00000000..2f9827d4 --- /dev/null +++ b/src/config/lua/ConfigManager.hpp @@ -0,0 +1,186 @@ +#pragma once + +#include + +#include +#include +#include +#include +#include +#include + +#include "../../helpers/memory/Memory.hpp" +#include "../ConfigManager.hpp" +#include "../../managers/eventLoop/EventLoopTimer.hpp" + +#include "./types/LuaConfigValue.hpp" +#include "./LuaEventHandler.hpp" + +#include "../../desktop/rule/windowRule/WindowRule.hpp" +#include "../../desktop/rule/layerRule/LayerRule.hpp" + +#include "../../SharedDefs.hpp" +#include "../../managers/KeybindManager.hpp" + +extern "C" { +#include +#include +#include +} + +namespace Config::Supplementary { + struct SConfigOptionDescription; +}; + +namespace Config::Lua { + class CConfigManager; + class CConfigManagerPluginLuaTestAccessor; +} + +namespace Config::Lua::Bindings { + void registerBindings(lua_State* L, CConfigManager* mgr); +} + +namespace Config::Lua { + + class CConfigManager : public Config::IConfigManager { + public: + CConfigManager(); + + virtual eConfigManagerType type() override; + + virtual void init() override; + virtual void reload() override; + virtual std::string verify() override; + + virtual int getDeviceInt(const std::string&, const std::string&, const std::string& fallback = "") override; + virtual float getDeviceFloat(const std::string&, const std::string&, const std::string& fallback = "") override; + virtual Vector2D getDeviceVec(const std::string&, const std::string&, const std::string& fallback = "") override; + virtual std::string getDeviceString(const std::string&, const std::string&, const std::string& fallback = "") override; + virtual bool deviceConfigExplicitlySet(const std::string&, const std::string&) override; + virtual bool deviceConfigExists(const std::string&) override; + + virtual SConfigOptionReply getConfigValue(const std::string&) override; + + virtual std::string getMainConfigPath() override; + virtual std::string getErrors() override; + virtual std::string getConfigString() override; + virtual std::string currentConfigPath() override; + virtual const std::vector& getConfigPaths() override; + + virtual std::expected generateDefaultConfig(const std::filesystem::path&, bool safeMode) override; + + virtual void handlePluginLoads() override; + virtual bool configVerifPassed() override; + + virtual std::expected registerPluginValue(void* handle, SP value) override; + virtual void onPluginUnload(void* handle) override; + + int invokePluginLuaFunctionByID(uint64_t id, lua_State* L); + + std::expected registerPluginLuaFunction(void* handle, const std::string& namespace_, const std::string& name, PLUGIN_LUA_FN fn); + std::expected unregisterPluginLuaFunction(void* handle, const std::string& namespace_, const std::string& name); + + void addError(std::string&& str); + + void registerLuaRef(int ref); + void callLuaFn(int ref); + + // execute an arbitrary lua string on the current state. + std::optional eval(const std::string& code); + + int guardedPCall(int nargs, int nresults, int errfunc, int timeoutMs, std::string_view context); + + static CConfigManager* fromLuaState(lua_State* L); + + static constexpr int LUA_WATCHDOG_INSTRUCTION_INTERVAL = 10000; + static constexpr int LUA_TIMEOUT_CONFIG_RELOAD_MS = 1500; + static constexpr int LUA_TIMEOUT_EVENT_CALLBACK_MS = 50; + static constexpr int LUA_TIMEOUT_KEYBIND_CALLBACK_MS = 100; + static constexpr int LUA_TIMEOUT_TIMER_CALLBACK_MS = 50; + static constexpr int LUA_TIMEOUT_EVAL_MS = 250; + static constexpr int LUA_TIMEOUT_DISPATCH_MS = 100; + + bool isFirstLaunch() const; + bool isDynamicParse() const; + + std::string m_currentSubmap; + std::string m_currentSubmapReset; + + UP m_eventHandler; + + struct SLuaTimer { + SP timer; + int luaRef = LUA_NOREF; // registry ref to the lua callback + }; + std::vector m_luaTimers; + + std::vector m_registeredPlugins; + + std::unordered_map> m_configValues; + + struct SDeviceConfig { + SDeviceConfig(); + + std::unordered_map> values; + }; + + std::unordered_map m_deviceConfigs; + std::vector m_errors, m_configPaths; + + // named window/layer rules for merge-on-redeclaration + std::unordered_map> m_luaWindowRules; + std::unordered_map> m_luaLayerRules; + + private: + void reinitLuaState(); + void postConfigReload(); + void registerValue(const char* name, ILuaConfigValue* val); + void cleanTimers(); + void clearHeldLuaRefs(); + std::string luaConfigValueName(const std::string& s); + std::expected registerPluginLuaFunctionInState(uint64_t id, const std::string& namespace_, const std::string& name); + std::expected unregisterPluginLuaFunctionInState(const std::string& namespace_, const std::string& name); + void erasePluginLuaFunction(uint64_t id); + void reregisterLuaPluginFns(); + + static void watchdogHook(lua_State* L, lua_Debug* ar); + + lua_State* m_lua = nullptr; + + bool m_lastConfigVerificationWasSuccessful = true; + bool m_isFirstLaunch = true; + bool m_manualCrashInitiated = false; + bool m_watchdogActive = false; + bool m_isParsingConfig = false; + bool m_isEvaluating = false; + + std::chrono::steady_clock::time_point m_watchdogDeadline; + std::string m_watchdogContext; + + std::string m_mainConfigPath; + + std::vector m_heldLuaRefs; + + // this is here for legacy reasons. + std::unordered_map m_configPtrMap; + + // this is here for plugin reasons. + std::unordered_map> m_pluginValues; + + struct SPluginLuaFunction { + uint64_t id = 0; + void* handle = nullptr; + std::string namespace_; + std::string name; + Config::PLUGIN_LUA_FN fn = nullptr; + }; + std::vector m_pluginLuaFunctions; + + ILuaConfigValue* findDeviceValue(const std::string& dev, const std::string& field); + + friend class CConfigManagerPluginLuaTestAccessor; + }; + + WP mgr(); +} -- cgit v1.2.3