From b249d236c7b59d324f7e38b8b292daf5699cce3b Mon Sep 17 00:00:00 2001 From: taj-ny <79316397+taj-ny@users.noreply.github.com> Date: Sun, 21 Jun 2026 10:51:08 +0200 Subject: [PATCH] scripting: clean up module initialization --- src/CMakeLists.txt | 3 ++ .../scripting/ScriptingEngine.cpp | 37 ++++++--------- .../scripting/ScriptingEngine.h | 17 +++---- .../scripting/modules/Module.cpp | 29 ++++++++++++ .../scripting/modules/Module.h | 45 +++++++++++++++++++ .../scripting/modules/core/CoreModule.cpp | 9 +++- .../scripting/modules/core/CoreModule.h | 8 ++-- .../modules/desktop/DesktopModule.cpp | 36 +++++++++++++++ .../scripting/modules/desktop/DesktopModule.h | 36 +++++++++++++++ .../scripting/modules/fs/FSModule.cpp | 39 ++++++++++++++++ .../scripting/modules/fs/FSModule.h | 36 +++++++++++++++ .../scripting/modules/main/MainModule.cpp | 10 ++++- .../scripting/modules/main/MainModule.h | 10 ++--- 13 files changed, 269 insertions(+), 46 deletions(-) create mode 100644 src/libinputactions/scripting/modules/Module.cpp create mode 100644 src/libinputactions/scripting/modules/Module.h create mode 100644 src/libinputactions/scripting/modules/desktop/DesktopModule.cpp create mode 100644 src/libinputactions/scripting/modules/desktop/DesktopModule.h create mode 100644 src/libinputactions/scripting/modules/fs/FSModule.cpp create mode 100644 src/libinputactions/scripting/modules/fs/FSModule.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3e87ddf..cfa250f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -87,9 +87,12 @@ set(libinputactions_SRCS libinputactions/scripting/modules/core/StoredVariableWrapper.cpp libinputactions/scripting/modules/core/VariableRegistryWrapper.cpp libinputactions/scripting/modules/core/VariableWrapper.cpp + libinputactions/scripting/modules/desktop/DesktopModule.cpp libinputactions/scripting/modules/fs/File.cpp + libinputactions/scripting/modules/fs/FSModule.cpp libinputactions/scripting/modules/main/MainModule.cpp libinputactions/scripting/modules/main/Script.cpp + libinputactions/scripting/modules/Module.cpp libinputactions/scripting/FunctionWrapper.cpp libinputactions/scripting/JSFunctionAction.cpp libinputactions/scripting/JSFunctionCondition.cpp diff --git a/src/libinputactions/scripting/ScriptingEngine.cpp b/src/libinputactions/scripting/ScriptingEngine.cpp index b4feb96..e5cf46b 100644 --- a/src/libinputactions/scripting/ScriptingEngine.cpp +++ b/src/libinputactions/scripting/ScriptingEngine.cpp @@ -18,11 +18,12 @@ #include "ScriptingEngine.h" #include "Promise.h" +#include "modules/Module.h" #include "modules/core/CoreModule.h" -#include "modules/fs/File.h" +#include "modules/desktop/DesktopModule.h" +#include "modules/fs/FSModule.h" #include "modules/main/MainModule.h" #include -#include #include #include #include @@ -74,25 +75,11 @@ void ScriptingEngine::initialize() m_coreModule = std::make_unique(*this, m_variableRegistry); QJSEngine::setObjectOwnership(m_coreModule.get(), QJSEngine::CppOwnership); - auto coreModule = m_engine->newQObject(m_coreModule.get()); - coreModule.setProperty("KeyboardModifier", newEnum()); - coreModule.setProperty("VariableType", newEnum()); - registerBuiltinModule("inputactions/core", coreModule); - - auto desktopModule = m_engine->newObject(); - desktopModule.setProperty("CursorShape", newEnum()); - registerBuiltinModule("inputactions/desktop", desktopModule); - - auto fsModule = m_engine->newObject(); - fsModule.setProperty("File", m_engine->newQMetaObject(&File::staticMetaObject)); - auto file = fsModule.property("File"); - file.setProperty("readAllText", newFunction(&File::readAllText)); - file.setProperty("writeAllText", newFunction(&File::writeAllText)); - registerBuiltinModule("inputactions/fs", fsModule); - - auto mainModule = m_engine->newQObject(new MainModule(*this)); - mainModule.setProperty("Point", m_engine->newQMetaObject(&PointF::staticMetaObject)); - registerBuiltinModule("inputactions", mainModule); + registerBuiltinModule("inputactions/core", m_coreModule.get()); + + registerBuiltinModule("inputactions", new MainModule(*this)); + registerBuiltinModule("inputactions/desktop", new DesktopModule(*this)); + registerBuiltinModule("inputactions/fs", new FSModule(*this)); auto globalObject = m_engine->globalObject(); globalObject.setProperty("require", newFunction([this](QString module) { @@ -170,10 +157,12 @@ void ScriptingEngine::initializeWatchdog() onWatchdogRestartTimerTick(); } -void ScriptingEngine::registerBuiltinModule(const QString &name, QJSValue value) +void ScriptingEngine::registerBuiltinModule(const QString &name, Module *module) { - ensureEngine().registerModule(name, value); - m_builtinModules[name] = std::move(value); + auto object = m_engine->newQObject(module); + module->initialize(object); + ensureEngine().registerModule(name, object); + m_builtinModules[name] = std::move(object); } QJSValue ScriptingEngine::newEnum(const QMetaEnum &metaEnum) diff --git a/src/libinputactions/scripting/ScriptingEngine.h b/src/libinputactions/scripting/ScriptingEngine.h index 1d69716..1716c58 100644 --- a/src/libinputactions/scripting/ScriptingEngine.h +++ b/src/libinputactions/scripting/ScriptingEngine.h @@ -30,6 +30,7 @@ namespace InputActions { class CoreModule; +class Module; class Promise; class VariableRegistry; @@ -78,6 +79,13 @@ class ScriptingEngine : public QObject Promise newPromise(); + template + QJSValue newEnum() + { + return newEnum(QMetaEnum::fromType()); + } + QJSValue newEnum(const QMetaEnum &metaEnum); + /** * Returns an instance of the engine. Initializes the engine if it has not been initialized yet. */ @@ -92,14 +100,7 @@ private slots: void initialize(); void initializeWatchdog(); - void registerBuiltinModule(const QString &name, QJSValue value); - - template - QJSValue newEnum() - { - return newEnum(QMetaEnum::fromType()); - } - QJSValue newEnum(const QMetaEnum &metaEnum); + void registerBuiltinModule(const QString &name, Module *module); VariableRegistry &m_variableRegistry; diff --git a/src/libinputactions/scripting/modules/Module.cpp b/src/libinputactions/scripting/modules/Module.cpp new file mode 100644 index 0000000..df17d88 --- /dev/null +++ b/src/libinputactions/scripting/modules/Module.cpp @@ -0,0 +1,29 @@ +/* + Input Actions - Input handler that executes user-defined actions + Copyright (C) 2024-2026 Marcin Woźniak + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "Module.h" + +namespace InputActions +{ + +Module::Module(ScriptingEngine &engine) + : m_engine(engine) +{ +} + +} \ No newline at end of file diff --git a/src/libinputactions/scripting/modules/Module.h b/src/libinputactions/scripting/modules/Module.h new file mode 100644 index 0000000..aa3aeaa --- /dev/null +++ b/src/libinputactions/scripting/modules/Module.h @@ -0,0 +1,45 @@ +/* + Input Actions - Input handler that executes user-defined actions + Copyright (C) 2024-2026 Marcin Woźniak + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#pragma once + +#include +#include + +namespace InputActions +{ + +class ScriptingEngine; + +class Module : public QObject +{ + Q_OBJECT + +public: + virtual void initialize(QJSValue &self) {} + +protected: + Module(ScriptingEngine &engine); + + ScriptingEngine &engine() const { return m_engine; } + +private: + ScriptingEngine &m_engine; +}; + +} \ No newline at end of file diff --git a/src/libinputactions/scripting/modules/core/CoreModule.cpp b/src/libinputactions/scripting/modules/core/CoreModule.cpp index 36d0fa1..c9350c3 100644 --- a/src/libinputactions/scripting/modules/core/CoreModule.cpp +++ b/src/libinputactions/scripting/modules/core/CoreModule.cpp @@ -27,13 +27,18 @@ namespace InputActions { CoreModule::CoreModule(ScriptingEngine &engine, VariableRegistry &variableRegistry) - : m_variableRegistry(variableRegistry, engine) + : Module(engine) + , m_variableRegistry(variableRegistry, engine) { QJSEngine::setObjectOwnership(&m_config, QJSEngine::CppOwnership); QJSEngine::setObjectOwnership(&m_variableRegistry, QJSEngine::CppOwnership); } -CoreModule::~CoreModule() = default; +void CoreModule::initialize(QJSValue &self) +{ + self.setProperty("KeyboardModifier", engine().newEnum()); + self.setProperty("VariableType", engine().newEnum()); +} Config *CoreModule::config() { diff --git a/src/libinputactions/scripting/modules/core/CoreModule.h b/src/libinputactions/scripting/modules/core/CoreModule.h index 4faf404..269d0bb 100644 --- a/src/libinputactions/scripting/modules/core/CoreModule.h +++ b/src/libinputactions/scripting/modules/core/CoreModule.h @@ -20,15 +20,14 @@ #include "Config.h" #include "VariableRegistryWrapper.h" -#include +#include namespace InputActions { class InputBackend; -class ScriptingEngine; -class CoreModule : public QObject +class CoreModule : public Module { Q_OBJECT @@ -38,12 +37,13 @@ class CoreModule : public QObject public: CoreModule(ScriptingEngine &engine, VariableRegistry &variableRegistry); - ~CoreModule() override; Config *config(); InputBackend *input() const; VariableRegistryWrapper *variableRegistry(); + void initialize(QJSValue &self) override; + private: Config m_config; VariableRegistryWrapper m_variableRegistry; diff --git a/src/libinputactions/scripting/modules/desktop/DesktopModule.cpp b/src/libinputactions/scripting/modules/desktop/DesktopModule.cpp new file mode 100644 index 0000000..cf113f9 --- /dev/null +++ b/src/libinputactions/scripting/modules/desktop/DesktopModule.cpp @@ -0,0 +1,36 @@ +/* + Input Actions - Input handler that executes user-defined actions + Copyright (C) 2024-2026 Marcin Woźniak + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "DesktopModule.h" +#include +#include + +namespace InputActions +{ + +DesktopModule::DesktopModule(ScriptingEngine &engine) + : Module(engine) +{ +} + +void DesktopModule::initialize(QJSValue &self) +{ + self.setProperty("CursorShape", engine().newEnum()); +} + +} \ No newline at end of file diff --git a/src/libinputactions/scripting/modules/desktop/DesktopModule.h b/src/libinputactions/scripting/modules/desktop/DesktopModule.h new file mode 100644 index 0000000..9bd19de --- /dev/null +++ b/src/libinputactions/scripting/modules/desktop/DesktopModule.h @@ -0,0 +1,36 @@ +/* + Input Actions - Input handler that executes user-defined actions + Copyright (C) 2024-2026 Marcin Woźniak + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#pragma once + +#include + +namespace InputActions +{ + +class DesktopModule : public Module +{ + Q_OBJECT + +public: + DesktopModule(ScriptingEngine &engine); + + void initialize(QJSValue &self) override; +}; + +} \ No newline at end of file diff --git a/src/libinputactions/scripting/modules/fs/FSModule.cpp b/src/libinputactions/scripting/modules/fs/FSModule.cpp new file mode 100644 index 0000000..20bf66d --- /dev/null +++ b/src/libinputactions/scripting/modules/fs/FSModule.cpp @@ -0,0 +1,39 @@ +/* + Input Actions - Input handler that executes user-defined actions + Copyright (C) 2024-2026 Marcin Woźniak + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "FSModule.h" +#include "File.h" +#include + +namespace InputActions +{ + +FSModule::FSModule(ScriptingEngine &engine) + : Module(engine) +{ +} + +void FSModule::initialize(QJSValue &self) +{ + auto file = engine().ensureEngine().newQMetaObject(&File::staticMetaObject); + file.setProperty("readAllText", engine().newFunction(&File::readAllText)); + file.setProperty("writeAllText", engine().newFunction(&File::writeAllText)); + self.setProperty("File", file); +} + +} \ No newline at end of file diff --git a/src/libinputactions/scripting/modules/fs/FSModule.h b/src/libinputactions/scripting/modules/fs/FSModule.h new file mode 100644 index 0000000..c3206ea --- /dev/null +++ b/src/libinputactions/scripting/modules/fs/FSModule.h @@ -0,0 +1,36 @@ +/* + Input Actions - Input handler that executes user-defined actions + Copyright (C) 2024-2026 Marcin Woźniak + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#pragma once + +#include + +namespace InputActions +{ + +class FSModule : public Module +{ + Q_OBJECT + +public: + FSModule(ScriptingEngine &engine); + + void initialize(QJSValue &self) override; +}; + +} \ No newline at end of file diff --git a/src/libinputactions/scripting/modules/main/MainModule.cpp b/src/libinputactions/scripting/modules/main/MainModule.cpp index 13d09fc..9d31dd1 100644 --- a/src/libinputactions/scripting/modules/main/MainModule.cpp +++ b/src/libinputactions/scripting/modules/main/MainModule.cpp @@ -17,21 +17,27 @@ */ #include "MainModule.h" +#include #include namespace InputActions { MainModule::MainModule(ScriptingEngine &engine) - : m_engine(engine) + : Module(engine) , m_globalObject(engine.ensureEngine().globalObject()) { } +void MainModule::initialize(QJSValue &self) +{ + self.setProperty("Point", engine().ensureEngine().newQMetaObject(&PointF::staticMetaObject)); +} + QJSValue MainModule::delay(double duration) { if (duration < 1 || duration > INT32_MAX) { - m_engine.ensureEngine().throwError(QJSValue::RangeError, QString("Value %1 is out of range.").arg(QString::number(duration))); + engine().ensureEngine().throwError(QJSValue::RangeError, QString("Value %1 is out of range.").arg(QString::number(duration))); return {}; } diff --git a/src/libinputactions/scripting/modules/main/MainModule.h b/src/libinputactions/scripting/modules/main/MainModule.h index 4c61dfd..aadb043 100644 --- a/src/libinputactions/scripting/modules/main/MainModule.h +++ b/src/libinputactions/scripting/modules/main/MainModule.h @@ -18,15 +18,12 @@ #pragma once -#include -#include +#include namespace InputActions { -class ScriptingEngine; - -class MainModule : public QObject +class MainModule : public Module { Q_OBJECT @@ -39,8 +36,9 @@ class MainModule : public QObject Q_INVOKABLE QJSValue delay(double duration); + void initialize(QJSValue &self) override; + private: - ScriptingEngine &m_engine; QJSValue m_globalObject; };