Skip to content

Commit b249d23

Browse files
committed
scripting: clean up module initialization
1 parent 92aaa58 commit b249d23

13 files changed

Lines changed: 269 additions & 46 deletions

File tree

src/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,12 @@ set(libinputactions_SRCS
8787
libinputactions/scripting/modules/core/StoredVariableWrapper.cpp
8888
libinputactions/scripting/modules/core/VariableRegistryWrapper.cpp
8989
libinputactions/scripting/modules/core/VariableWrapper.cpp
90+
libinputactions/scripting/modules/desktop/DesktopModule.cpp
9091
libinputactions/scripting/modules/fs/File.cpp
92+
libinputactions/scripting/modules/fs/FSModule.cpp
9193
libinputactions/scripting/modules/main/MainModule.cpp
9294
libinputactions/scripting/modules/main/Script.cpp
95+
libinputactions/scripting/modules/Module.cpp
9396
libinputactions/scripting/FunctionWrapper.cpp
9497
libinputactions/scripting/JSFunctionAction.cpp
9598
libinputactions/scripting/JSFunctionCondition.cpp

src/libinputactions/scripting/ScriptingEngine.cpp

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818

1919
#include "ScriptingEngine.h"
2020
#include "Promise.h"
21+
#include "modules/Module.h"
2122
#include "modules/core/CoreModule.h"
22-
#include "modules/fs/File.h"
23+
#include "modules/desktop/DesktopModule.h"
24+
#include "modules/fs/FSModule.h"
2325
#include "modules/main/MainModule.h"
2426
#include <libinputactions/InputActionsMain.h>
25-
#include <libinputactions/PointF.h>
2627
#include <libinputactions/config/ConfigIssue.h>
2728
#include <libinputactions/globals.h>
2829
#include <libinputactions/helpers/QString.h>
@@ -74,25 +75,11 @@ void ScriptingEngine::initialize()
7475

7576
m_coreModule = std::make_unique<CoreModule>(*this, m_variableRegistry);
7677
QJSEngine::setObjectOwnership(m_coreModule.get(), QJSEngine::CppOwnership);
77-
auto coreModule = m_engine->newQObject(m_coreModule.get());
78-
coreModule.setProperty("KeyboardModifier", newEnum<KeyboardModifier>());
79-
coreModule.setProperty("VariableType", newEnum<VariableType>());
80-
registerBuiltinModule("inputactions/core", coreModule);
81-
82-
auto desktopModule = m_engine->newObject();
83-
desktopModule.setProperty("CursorShape", newEnum<CursorShape>());
84-
registerBuiltinModule("inputactions/desktop", desktopModule);
85-
86-
auto fsModule = m_engine->newObject();
87-
fsModule.setProperty("File", m_engine->newQMetaObject(&File::staticMetaObject));
88-
auto file = fsModule.property("File");
89-
file.setProperty("readAllText", newFunction<QJSValue, QString>(&File::readAllText));
90-
file.setProperty("writeAllText", newFunction<QJSValue, QString, QString>(&File::writeAllText));
91-
registerBuiltinModule("inputactions/fs", fsModule);
92-
93-
auto mainModule = m_engine->newQObject(new MainModule(*this));
94-
mainModule.setProperty("Point", m_engine->newQMetaObject(&PointF::staticMetaObject));
95-
registerBuiltinModule("inputactions", mainModule);
78+
registerBuiltinModule("inputactions/core", m_coreModule.get());
79+
80+
registerBuiltinModule("inputactions", new MainModule(*this));
81+
registerBuiltinModule("inputactions/desktop", new DesktopModule(*this));
82+
registerBuiltinModule("inputactions/fs", new FSModule(*this));
9683

9784
auto globalObject = m_engine->globalObject();
9885
globalObject.setProperty("require", newFunction<QJSValue, QString>([this](QString module) {
@@ -170,10 +157,12 @@ void ScriptingEngine::initializeWatchdog()
170157
onWatchdogRestartTimerTick();
171158
}
172159

173-
void ScriptingEngine::registerBuiltinModule(const QString &name, QJSValue value)
160+
void ScriptingEngine::registerBuiltinModule(const QString &name, Module *module)
174161
{
175-
ensureEngine().registerModule(name, value);
176-
m_builtinModules[name] = std::move(value);
162+
auto object = m_engine->newQObject(module);
163+
module->initialize(object);
164+
ensureEngine().registerModule(name, object);
165+
m_builtinModules[name] = std::move(object);
177166
}
178167

179168
QJSValue ScriptingEngine::newEnum(const QMetaEnum &metaEnum)

src/libinputactions/scripting/ScriptingEngine.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ namespace InputActions
3030
{
3131

3232
class CoreModule;
33+
class Module;
3334
class Promise;
3435
class VariableRegistry;
3536

@@ -78,6 +79,13 @@ class ScriptingEngine : public QObject
7879

7980
Promise newPromise();
8081

82+
template<typename T>
83+
QJSValue newEnum()
84+
{
85+
return newEnum(QMetaEnum::fromType<T>());
86+
}
87+
QJSValue newEnum(const QMetaEnum &metaEnum);
88+
8189
/**
8290
* Returns an instance of the engine. Initializes the engine if it has not been initialized yet.
8391
*/
@@ -92,14 +100,7 @@ private slots:
92100
void initialize();
93101
void initializeWatchdog();
94102

95-
void registerBuiltinModule(const QString &name, QJSValue value);
96-
97-
template<typename T>
98-
QJSValue newEnum()
99-
{
100-
return newEnum(QMetaEnum::fromType<T>());
101-
}
102-
QJSValue newEnum(const QMetaEnum &metaEnum);
103+
void registerBuiltinModule(const QString &name, Module *module);
103104

104105
VariableRegistry &m_variableRegistry;
105106

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Input Actions - Input handler that executes user-defined actions
3+
Copyright (C) 2024-2026 Marcin Woźniak
4+
5+
This program is free software: you can redistribute it and/or modify
6+
it under the terms of the GNU General Public License as published by
7+
the Free Software Foundation, either version 3 of the License, or
8+
(at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU General Public License for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
19+
#include "Module.h"
20+
21+
namespace InputActions
22+
{
23+
24+
Module::Module(ScriptingEngine &engine)
25+
: m_engine(engine)
26+
{
27+
}
28+
29+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Input Actions - Input handler that executes user-defined actions
3+
Copyright (C) 2024-2026 Marcin Woźniak
4+
5+
This program is free software: you can redistribute it and/or modify
6+
it under the terms of the GNU General Public License as published by
7+
the Free Software Foundation, either version 3 of the License, or
8+
(at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU General Public License for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
19+
#pragma once
20+
21+
#include <QJSValue>
22+
#include <QObject>
23+
24+
namespace InputActions
25+
{
26+
27+
class ScriptingEngine;
28+
29+
class Module : public QObject
30+
{
31+
Q_OBJECT
32+
33+
public:
34+
virtual void initialize(QJSValue &self) {}
35+
36+
protected:
37+
Module(ScriptingEngine &engine);
38+
39+
ScriptingEngine &engine() const { return m_engine; }
40+
41+
private:
42+
ScriptingEngine &m_engine;
43+
};
44+
45+
}

src/libinputactions/scripting/modules/core/CoreModule.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,18 @@ namespace InputActions
2727
{
2828

2929
CoreModule::CoreModule(ScriptingEngine &engine, VariableRegistry &variableRegistry)
30-
: m_variableRegistry(variableRegistry, engine)
30+
: Module(engine)
31+
, m_variableRegistry(variableRegistry, engine)
3132
{
3233
QJSEngine::setObjectOwnership(&m_config, QJSEngine::CppOwnership);
3334
QJSEngine::setObjectOwnership(&m_variableRegistry, QJSEngine::CppOwnership);
3435
}
3536

36-
CoreModule::~CoreModule() = default;
37+
void CoreModule::initialize(QJSValue &self)
38+
{
39+
self.setProperty("KeyboardModifier", engine().newEnum<KeyboardModifier>());
40+
self.setProperty("VariableType", engine().newEnum<VariableType>());
41+
}
3742

3843
Config *CoreModule::config()
3944
{

src/libinputactions/scripting/modules/core/CoreModule.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@
2020

2121
#include "Config.h"
2222
#include "VariableRegistryWrapper.h"
23-
#include <QObject>
23+
#include <libinputactions/scripting/modules/Module.h>
2424

2525
namespace InputActions
2626
{
2727

2828
class InputBackend;
29-
class ScriptingEngine;
3029

31-
class CoreModule : public QObject
30+
class CoreModule : public Module
3231
{
3332
Q_OBJECT
3433

@@ -38,12 +37,13 @@ class CoreModule : public QObject
3837

3938
public:
4039
CoreModule(ScriptingEngine &engine, VariableRegistry &variableRegistry);
41-
~CoreModule() override;
4240

4341
Config *config();
4442
InputBackend *input() const;
4543
VariableRegistryWrapper *variableRegistry();
4644

45+
void initialize(QJSValue &self) override;
46+
4747
private:
4848
Config m_config;
4949
VariableRegistryWrapper m_variableRegistry;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Input Actions - Input handler that executes user-defined actions
3+
Copyright (C) 2024-2026 Marcin Woźniak
4+
5+
This program is free software: you can redistribute it and/or modify
6+
it under the terms of the GNU General Public License as published by
7+
the Free Software Foundation, either version 3 of the License, or
8+
(at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU General Public License for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
19+
#include "DesktopModule.h"
20+
#include <libinputactions/globals.h>
21+
#include <libinputactions/scripting/ScriptingEngine.h>
22+
23+
namespace InputActions
24+
{
25+
26+
DesktopModule::DesktopModule(ScriptingEngine &engine)
27+
: Module(engine)
28+
{
29+
}
30+
31+
void DesktopModule::initialize(QJSValue &self)
32+
{
33+
self.setProperty("CursorShape", engine().newEnum<CursorShape>());
34+
}
35+
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Input Actions - Input handler that executes user-defined actions
3+
Copyright (C) 2024-2026 Marcin Woźniak
4+
5+
This program is free software: you can redistribute it and/or modify
6+
it under the terms of the GNU General Public License as published by
7+
the Free Software Foundation, either version 3 of the License, or
8+
(at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU General Public License for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
19+
#pragma once
20+
21+
#include <libinputactions/scripting/modules/Module.h>
22+
23+
namespace InputActions
24+
{
25+
26+
class DesktopModule : public Module
27+
{
28+
Q_OBJECT
29+
30+
public:
31+
DesktopModule(ScriptingEngine &engine);
32+
33+
void initialize(QJSValue &self) override;
34+
};
35+
36+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Input Actions - Input handler that executes user-defined actions
3+
Copyright (C) 2024-2026 Marcin Woźniak
4+
5+
This program is free software: you can redistribute it and/or modify
6+
it under the terms of the GNU General Public License as published by
7+
the Free Software Foundation, either version 3 of the License, or
8+
(at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU General Public License for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
19+
#include "FSModule.h"
20+
#include "File.h"
21+
#include <libinputactions/scripting/ScriptingEngine.h>
22+
23+
namespace InputActions
24+
{
25+
26+
FSModule::FSModule(ScriptingEngine &engine)
27+
: Module(engine)
28+
{
29+
}
30+
31+
void FSModule::initialize(QJSValue &self)
32+
{
33+
auto file = engine().ensureEngine().newQMetaObject(&File::staticMetaObject);
34+
file.setProperty("readAllText", engine().newFunction<QJSValue, QString>(&File::readAllText));
35+
file.setProperty("writeAllText", engine().newFunction<QJSValue, QString, QString>(&File::writeAllText));
36+
self.setProperty("File", file);
37+
}
38+
39+
}

0 commit comments

Comments
 (0)