Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ set(libinputactions_SRCS
libinputactions/interfaces/TextInput.cpp
libinputactions/interfaces/Window.h
libinputactions/interfaces/WindowProvider.cpp
libinputactions/scripting/modules/core/Config.cpp
libinputactions/scripting/modules/core/CoreModule.cpp
libinputactions/scripting/modules/fs/File.cpp
libinputactions/scripting/FunctionWrapper.cpp
Expand Down
21 changes: 17 additions & 4 deletions src/libinputactions/config/ConfigLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@
#include <libinputactions/input/backends/LibevdevComplementaryInputBackend.h>
#include <libinputactions/input/devices/InputDeviceRule.h>
#include <libinputactions/interfaces/NotificationManager.h>
#include <libinputactions/scripting/modules/core/Config.h>
#include <libinputactions/scripting/modules/core/CoreModule.h>
#include <libinputactions/scripting/ScriptingEngine.h>

namespace InputActions
{

struct Config
struct ConfigData
{
bool allowExternalVariableAccess = true;
bool autoReload = true;
Expand All @@ -64,13 +66,15 @@ void ConfigLoader::loadEmpty()
bool ConfigLoader::load(const ConfigLoadSettings &settings)
{
auto scriptingEngine = g_scriptingEngine;

try {
qCDebug(INPUTACTIONS, "Reloading config");
const auto rawConfig = settings.config.value_or(g_configProvider->currentConfig());

g_configIssueManager = std::make_shared<ConfigIssueManager>(rawConfig);
g_scriptingEngine = std::make_shared<ScriptingEngine>();
auto config = createConfig(rawConfig);
scriptingEngine.reset();
activateConfig(std::move(config), true);
} catch (const ConfigException &e) {
g_scriptingEngine = scriptingEngine;
Expand All @@ -94,14 +98,14 @@ bool ConfigLoader::load(const ConfigLoadSettings &settings)
return true;
}

Config ConfigLoader::createConfig(const QString &raw)
ConfigData ConfigLoader::createConfig(const QString &raw)
{
const auto root = Node::create(raw);
if (!root->isMap()) {
throw InvalidNodeTypeConfigException(root.get(), NodeType::Map);
}

Config config;
ConfigData config;

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

void ConfigLoader::activateConfig(Config config, bool initialize)
void ConfigLoader::activateConfig(ConfigData config, bool initialize)
{
g_inputBackend->reset(); // Okay because required keys are not cleared
g_actionExecutor->clearQueue();
g_actionExecutor->waitForDone();

const auto *coreModule = g_scriptingEngine->coreModule();
if (coreModule) {
Q_EMIT coreModule->config()->aboutToBeActivated();
}

g_globalConfig->setAllowExternalVariableAccess(config.allowExternalVariableAccess);
g_globalConfig->setAutoReload(config.autoReload);
g_globalConfig->setSendNotificationOnError(config.sendNotificationOnError);
Expand All @@ -172,6 +181,10 @@ void ConfigLoader::activateConfig(Config config, bool initialize)
if (initialize) {
g_inputBackend->initialize();
}

if (coreModule) {
Q_EMIT coreModule->config()->activated();
}
}

}
6 changes: 3 additions & 3 deletions src/libinputactions/config/ConfigLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
namespace InputActions
{

struct Config;
struct ConfigData;

struct ConfigLoadSettings
{
Expand Down Expand Up @@ -53,8 +53,8 @@ class ConfigLoader
void loadEmpty();

private:
Config createConfig(const QString &raw);
void activateConfig(Config config, bool initialize);
ConfigData createConfig(const QString &raw);
void activateConfig(ConfigData config, bool initialize);
};

inline std::shared_ptr<ConfigLoader> g_configLoader;
Expand Down
14 changes: 13 additions & 1 deletion src/libinputactions/scripting/ScriptingEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "ScriptingEngine.h"
#include "Promise.h"
#include "modules/core/Config.h"
#include "modules/core/CoreModule.h"
#include "modules/fs/File.h"
#include <libinputactions/InputActionsMain.h>
Expand All @@ -35,12 +36,16 @@ namespace InputActions
static const std::chrono::milliseconds WATCHDOG_TIMER_TIMEOUT{2000};
static const std::chrono::milliseconds WATCHDOG_TIMER_RESET_INTERVAL{1000};

ScriptingEngine::ScriptingEngine() = default;

ScriptingEngine::~ScriptingEngine()
{
if (!m_engine) {
return;
}

Q_EMIT coreModule()->config()->aboutToBeDestroyed();

QMetaObject::invokeMethod(m_watchdogTimer, "stop", Qt::BlockingQueuedConnection);
m_watchdogTimerThread->quit();
m_watchdogTimerThread->wait();
Expand All @@ -65,7 +70,9 @@ void ScriptingEngine::initialize()
}
)");

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

Expand Down Expand Up @@ -179,4 +186,9 @@ void ScriptingEngine::onWatchdogRestartTimerTick()
QMetaObject::invokeMethod(m_watchdogTimer, "start", Qt::QueuedConnection);
}

CoreModule *ScriptingEngine::coreModule() const
{
return m_coreModule.get();
}

}
10 changes: 9 additions & 1 deletion src/libinputactions/scripting/ScriptingEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
namespace InputActions
{

class CoreModule;
class Promise;

/**
Expand All @@ -36,9 +37,14 @@ class ScriptingEngine : public QObject
Q_OBJECT

public:
ScriptingEngine() = default;
ScriptingEngine();
~ScriptingEngine() override;

/**
* Nullptr if engine not initialized.
*/
CoreModule *coreModule() const;

/**
* Same as QJSEngine::evaluate but with error logging.
*/
Expand Down Expand Up @@ -84,6 +90,8 @@ private slots:
void registerBuiltinModule(const QString &name, QJSValue value);

std::optional<QJSEngine> m_engine;

std::unique_ptr<CoreModule> m_coreModule;
std::map<QString, QJSValue> m_builtinModules;

QJSValue m_promiseFactory;
Expand Down
23 changes: 23 additions & 0 deletions src/libinputactions/scripting/modules/core/Config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
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 <https://www.gnu.org/licenses/>.
*/

#include "Config.h"

namespace InputActions
{
}
36 changes: 36 additions & 0 deletions src/libinputactions/scripting/modules/core/Config.h
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

#pragma once

#include <QObject>

namespace InputActions
{

class Config : public QObject
{
Q_OBJECT

signals:
void aboutToBeActivated();
void activated();
void aboutToBeDestroyed();
};

}
15 changes: 15 additions & 0 deletions src/libinputactions/scripting/modules/core/CoreModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,27 @@
*/

#include "CoreModule.h"
#include "Config.h"
#include <libinputactions/input/backends/InputBackend.h>
#include <libinputactions/variables/VariableRegistry.h>
#include <QJSEngine>

namespace InputActions
{

CoreModule::CoreModule()
: m_config(std::make_unique<Config>())
{
QJSEngine::setObjectOwnership(m_config.get(), QJSEngine::CppOwnership);
}

CoreModule::~CoreModule() = default;

Config *CoreModule::config() const
{
return m_config.get();
}

InputBackend *CoreModule::input() const
{
return g_inputBackend.get();
Expand Down
9 changes: 9 additions & 0 deletions src/libinputactions/scripting/modules/core/CoreModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,28 @@
namespace InputActions
{

class Config;
class InputBackend;
class VariableRegistry;

class CoreModule : public QObject
{
Q_OBJECT

Q_PROPERTY(Config *config READ config)
Q_PROPERTY(InputBackend *input READ input)
Q_PROPERTY(VariableRegistry *variableRegistry READ variableRegistry)

public:
CoreModule();
~CoreModule() override;

Config *config() const;
InputBackend *input() const;
VariableRegistry *variableRegistry() const;

private:
std::unique_ptr<Config> m_config;
};

}