Skip to content

Commit 60bcbab

Browse files
authored
Merge pull request #26 from InputActions/config-signals
scripting: add config signals
2 parents 11db77e + 45664bf commit 60bcbab

9 files changed

Lines changed: 126 additions & 9 deletions

File tree

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ set(libinputactions_SRCS
8181
libinputactions/interfaces/TextInput.cpp
8282
libinputactions/interfaces/Window.h
8383
libinputactions/interfaces/WindowProvider.cpp
84+
libinputactions/scripting/modules/core/Config.cpp
8485
libinputactions/scripting/modules/core/CoreModule.cpp
8586
libinputactions/scripting/modules/fs/File.cpp
8687
libinputactions/scripting/FunctionWrapper.cpp

src/libinputactions/config/ConfigLoader.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@
3434
#include <libinputactions/input/backends/LibevdevComplementaryInputBackend.h>
3535
#include <libinputactions/input/devices/InputDeviceRule.h>
3636
#include <libinputactions/interfaces/NotificationManager.h>
37+
#include <libinputactions/scripting/modules/core/Config.h>
38+
#include <libinputactions/scripting/modules/core/CoreModule.h>
3739
#include <libinputactions/scripting/ScriptingEngine.h>
3840

3941
namespace InputActions
4042
{
4143

42-
struct Config
44+
struct ConfigData
4345
{
4446
bool allowExternalVariableAccess = true;
4547
bool autoReload = true;
@@ -64,13 +66,15 @@ void ConfigLoader::loadEmpty()
6466
bool ConfigLoader::load(const ConfigLoadSettings &settings)
6567
{
6668
auto scriptingEngine = g_scriptingEngine;
69+
6770
try {
6871
qCDebug(INPUTACTIONS, "Reloading config");
6972
const auto rawConfig = settings.config.value_or(g_configProvider->currentConfig());
7073

7174
g_configIssueManager = std::make_shared<ConfigIssueManager>(rawConfig);
7275
g_scriptingEngine = std::make_shared<ScriptingEngine>();
7376
auto config = createConfig(rawConfig);
77+
scriptingEngine.reset();
7478
activateConfig(std::move(config), true);
7579
} catch (const ConfigException &e) {
7680
g_scriptingEngine = scriptingEngine;
@@ -94,14 +98,14 @@ bool ConfigLoader::load(const ConfigLoadSettings &settings)
9498
return true;
9599
}
96100

97-
Config ConfigLoader::createConfig(const QString &raw)
101+
ConfigData ConfigLoader::createConfig(const QString &raw)
98102
{
99103
const auto root = Node::create(raw);
100104
if (!root->isMap()) {
101105
throw InvalidNodeTypeConfigException(root.get(), NodeType::Map);
102106
}
103107

104-
Config config;
108+
ConfigData config;
105109

106110
if (const auto *scriptingNode = root->mapAt("scripting")) {
107111
if (const auto *scriptsNode = scriptingNode->at("scripts")) {
@@ -147,12 +151,17 @@ Config ConfigLoader::createConfig(const QString &raw)
147151
return config;
148152
}
149153

150-
void ConfigLoader::activateConfig(Config config, bool initialize)
154+
void ConfigLoader::activateConfig(ConfigData config, bool initialize)
151155
{
152156
g_inputBackend->reset(); // Okay because required keys are not cleared
153157
g_actionExecutor->clearQueue();
154158
g_actionExecutor->waitForDone();
155159

160+
const auto *coreModule = g_scriptingEngine->coreModule();
161+
if (coreModule) {
162+
Q_EMIT coreModule->config()->aboutToBeActivated();
163+
}
164+
156165
g_globalConfig->setAllowExternalVariableAccess(config.allowExternalVariableAccess);
157166
g_globalConfig->setAutoReload(config.autoReload);
158167
g_globalConfig->setSendNotificationOnError(config.sendNotificationOnError);
@@ -172,6 +181,10 @@ void ConfigLoader::activateConfig(Config config, bool initialize)
172181
if (initialize) {
173182
g_inputBackend->initialize();
174183
}
184+
185+
if (coreModule) {
186+
Q_EMIT coreModule->config()->activated();
187+
}
175188
}
176189

177190
}

src/libinputactions/config/ConfigLoader.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
namespace InputActions
2626
{
2727

28-
struct Config;
28+
struct ConfigData;
2929

3030
struct ConfigLoadSettings
3131
{
@@ -53,8 +53,8 @@ class ConfigLoader
5353
void loadEmpty();
5454

5555
private:
56-
Config createConfig(const QString &raw);
57-
void activateConfig(Config config, bool initialize);
56+
ConfigData createConfig(const QString &raw);
57+
void activateConfig(ConfigData config, bool initialize);
5858
};
5959

6060
inline std::shared_ptr<ConfigLoader> g_configLoader;

src/libinputactions/scripting/ScriptingEngine.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "ScriptingEngine.h"
2020
#include "Promise.h"
21+
#include "modules/core/Config.h"
2122
#include "modules/core/CoreModule.h"
2223
#include "modules/fs/File.h"
2324
#include <libinputactions/InputActionsMain.h>
@@ -35,12 +36,16 @@ namespace InputActions
3536
static const std::chrono::milliseconds WATCHDOG_TIMER_TIMEOUT{2000};
3637
static const std::chrono::milliseconds WATCHDOG_TIMER_RESET_INTERVAL{1000};
3738

39+
ScriptingEngine::ScriptingEngine() = default;
40+
3841
ScriptingEngine::~ScriptingEngine()
3942
{
4043
if (!m_engine) {
4144
return;
4245
}
4346

47+
Q_EMIT coreModule()->config()->aboutToBeDestroyed();
48+
4449
QMetaObject::invokeMethod(m_watchdogTimer, "stop", Qt::BlockingQueuedConnection);
4550
m_watchdogTimerThread->quit();
4651
m_watchdogTimerThread->wait();
@@ -65,7 +70,9 @@ void ScriptingEngine::initialize()
6570
}
6671
)");
6772

68-
auto coreModule = m_engine->newQObject(new CoreModule);
73+
m_coreModule = std::make_unique<CoreModule>();
74+
QJSEngine::setObjectOwnership(m_coreModule.get(), QJSEngine::CppOwnership);
75+
auto coreModule = m_engine->newQObject(m_coreModule.get());
6976
coreModule.setProperty("Point", m_engine->newQMetaObject(&PointF::staticMetaObject));
7077
registerBuiltinModule("inputactions/core", coreModule);
7178

@@ -179,4 +186,9 @@ void ScriptingEngine::onWatchdogRestartTimerTick()
179186
QMetaObject::invokeMethod(m_watchdogTimer, "start", Qt::QueuedConnection);
180187
}
181188

189+
CoreModule *ScriptingEngine::coreModule() const
190+
{
191+
return m_coreModule.get();
192+
}
193+
182194
}

src/libinputactions/scripting/ScriptingEngine.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
namespace InputActions
2727
{
2828

29+
class CoreModule;
2930
class Promise;
3031

3132
/**
@@ -36,9 +37,14 @@ class ScriptingEngine : public QObject
3637
Q_OBJECT
3738

3839
public:
39-
ScriptingEngine() = default;
40+
ScriptingEngine();
4041
~ScriptingEngine() override;
4142

43+
/**
44+
* Nullptr if engine not initialized.
45+
*/
46+
CoreModule *coreModule() const;
47+
4248
/**
4349
* Same as QJSEngine::evaluate but with error logging.
4450
*/
@@ -84,6 +90,8 @@ private slots:
8490
void registerBuiltinModule(const QString &name, QJSValue value);
8591

8692
std::optional<QJSEngine> m_engine;
93+
94+
std::unique_ptr<CoreModule> m_coreModule;
8795
std::map<QString, QJSValue> m_builtinModules;
8896

8997
QJSValue m_promiseFactory;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 "Config.h"
20+
21+
namespace InputActions
22+
{
23+
}
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 <QObject>
22+
23+
namespace InputActions
24+
{
25+
26+
class Config : public QObject
27+
{
28+
Q_OBJECT
29+
30+
signals:
31+
void aboutToBeActivated();
32+
void activated();
33+
void aboutToBeDestroyed();
34+
};
35+
36+
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,27 @@
1717
*/
1818

1919
#include "CoreModule.h"
20+
#include "Config.h"
2021
#include <libinputactions/input/backends/InputBackend.h>
2122
#include <libinputactions/variables/VariableRegistry.h>
23+
#include <QJSEngine>
2224

2325
namespace InputActions
2426
{
2527

28+
CoreModule::CoreModule()
29+
: m_config(std::make_unique<Config>())
30+
{
31+
QJSEngine::setObjectOwnership(m_config.get(), QJSEngine::CppOwnership);
32+
}
33+
34+
CoreModule::~CoreModule() = default;
35+
36+
Config *CoreModule::config() const
37+
{
38+
return m_config.get();
39+
}
40+
2641
InputBackend *CoreModule::input() const
2742
{
2843
return g_inputBackend.get();

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,28 @@
2323
namespace InputActions
2424
{
2525

26+
class Config;
2627
class InputBackend;
2728
class VariableRegistry;
2829

2930
class CoreModule : public QObject
3031
{
3132
Q_OBJECT
3233

34+
Q_PROPERTY(Config *config READ config)
3335
Q_PROPERTY(InputBackend *input READ input)
3436
Q_PROPERTY(VariableRegistry *variableRegistry READ variableRegistry)
3537

3638
public:
39+
CoreModule();
40+
~CoreModule() override;
41+
42+
Config *config() const;
3743
InputBackend *input() const;
3844
VariableRegistry *variableRegistry() const;
45+
46+
private:
47+
std::unique_ptr<Config> m_config;
3948
};
4049

4150
}

0 commit comments

Comments
 (0)