Skip to content
Draft
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
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
run: pacman -Syu --noconfirm

- name: Install dependencies
run: pacman -S --needed --noconfirm base-devel git extra-cmake-modules qt6-tools yaml-cpp gtest
run: pacman -S --needed --noconfirm base-devel git extra-cmake-modules qt6-declarative qt6-tools yaml-cpp gtest

- name: Check out repository
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ include(KDECMakeSettings)
find_package(Qt6 ${QT_MIN_VERSION} CONFIG REQUIRED COMPONENTS
Core
DBus
Qml
)

find_package(PkgConfig REQUIRED)
Expand Down
7 changes: 7 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ set(libinputactions_SRCS
libinputactions/config/parsers/flags.cpp
libinputactions/config/parsers/NodeParser.h
libinputactions/config/parsers/qt.cpp
libinputactions/config/parsers/scripting.cpp
libinputactions/config/parsers/separated-string.h
libinputactions/config/parsers/std.cpp
libinputactions/config/parsers/triggers.cpp
Expand Down Expand Up @@ -80,6 +81,11 @@ set(libinputactions_SRCS
libinputactions/interfaces/TextInput.cpp
libinputactions/interfaces/Window.h
libinputactions/interfaces/WindowProvider.cpp
libinputactions/scripting/modules/core/CoreModule.cpp
libinputactions/scripting/FunctionWrapper.cpp
libinputactions/scripting/ScriptAction.cpp
libinputactions/scripting/ScriptCondition.cpp
libinputactions/scripting/ScriptingEngine.cpp
libinputactions/triggers/core/DirectionalMotionTriggerCore.cpp
libinputactions/triggers/core/MotionTriggerCore.cpp
libinputactions/triggers/core/StrokeTriggerCore.cpp
Expand Down Expand Up @@ -132,6 +138,7 @@ target_link_libraries(libinputactions PUBLIC
libevdev-cpp
Qt6::Core
Qt6::DBus
Qt6::Qml
${LIBEVDEV_LIBRARIES}
)
target_compile_definitions(libinputactions PUBLIC TEST_VIRTUAL=$<IF:$<BOOL:${BUILD_TESTS}>,virtual,>)
Expand Down
3 changes: 3 additions & 0 deletions src/libinputactions/InputActionsMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "interfaces/implementations/DBusPlasmaGlobalShortcutInvoker.h"
#include "interfaces/implementations/FileConfigProvider.h"
#include "interfaces/implementations/ProcessRunnerImpl.h"
#include "scripting/ScriptingEngine.h"
#include "variables/VariableRegistry.h"
#include <QFile>
#include <QStandardPaths>
Expand Down Expand Up @@ -51,6 +52,7 @@ InputActionsMain::~InputActionsMain()
g_globalConfig.reset();
g_configProvider.reset();
g_inputBackend.reset();
g_scriptingEngine.reset();
g_strokeRecorder.reset();
g_variableRegistry.reset();
}
Expand Down Expand Up @@ -96,6 +98,7 @@ void InputActionsMain::setMissingImplementations()
setMissingImplementation(g_configLoader);
setMissingImplementation(g_globalConfig);
setMissingImplementation(g_inputBackend);
setMissingImplementation(g_scriptingEngine);
setMissingImplementation(g_strokeRecorder);
setMissingImplementation(g_variableRegistry);
}
Expand Down
25 changes: 25 additions & 0 deletions src/libinputactions/config/ConfigIssue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@

#include "ConfigIssue.h"
#include "ConfigIssueManager.h"
#include "ConfigLoader.h"
#include "Node.h"
#include <QRegularExpression>
#include <libinputactions/ansi-escape-codes.h>
#include <libinputactions/helpers/QString.h>
#include <libinputactions/scripting/ScriptingEngine.h>

namespace InputActions
{
Expand Down Expand Up @@ -183,6 +186,17 @@ QString UnusedPropertyConfigIssue::message() const
return QString("Property '%1' does not exist or has no effect in this context.").arg(m_property);
}

UncaughtScriptErrorConfigIssue::UncaughtScriptErrorConfigIssue(const Node *node, QJSValue error)
: ConfigIssue(node)
, m_message(g_configLoader->futureScriptingEngine().errorToString(error))
{
}

QString UncaughtScriptErrorConfigIssue::message() const
{
return QString("Uncaught script error\n\n%1").arg(m_message);
}

MissingRequiredPropertyConfigException::MissingRequiredPropertyConfigException(const Node *node, QString property)
: ConfigException(node)
, m_property(std::move(property))
Expand All @@ -194,6 +208,17 @@ QString MissingRequiredPropertyConfigException::message() const
return QString("Required property '%1' was not specified.").arg(m_property);
}

UncaughtScriptErrorConfigException::UncaughtScriptErrorConfigException(const Node *node, QJSValue error)
: ConfigException(node)
, m_message(g_configLoader->futureScriptingEngine().errorToString(error))
{
}

QString UncaughtScriptErrorConfigException::message() const
{
return QString("Uncaught script error\n\n%1").arg(m_message);
}

YamlCppConfigException::YamlCppConfigException(TextPosition position, QString message)
: ConfigException(position)
, m_message(std::move(message))
Expand Down
27 changes: 27 additions & 0 deletions src/libinputactions/config/ConfigIssue.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#pragma once

#include "TextPosition.h"
#include <QJSValue>
#include <QString>
#include <libinputactions/helpers/Copyable.h>

Expand Down Expand Up @@ -121,6 +122,19 @@ class UnusedPropertyConfigIssue
QString m_property;
};

class UncaughtScriptErrorConfigIssue
: public ConfigIssue
, public virtual Copyable<UncaughtScriptErrorConfigIssue, ConfigIssue>
{
public:
UncaughtScriptErrorConfigIssue(const Node *node, QJSValue error);

QString message() const override;

private:
QString m_message;
};

class DuplicateSetItemConfigException
: public ConfigException
, public virtual Copyable<DuplicateSetItemConfigException, ConfigIssue>
Expand Down Expand Up @@ -212,6 +226,19 @@ class MissingRequiredPropertyConfigException
QString m_property;
};

class UncaughtScriptErrorConfigException
: public ConfigException
, public virtual Copyable<UncaughtScriptErrorConfigException, ConfigIssue>
{
public:
UncaughtScriptErrorConfigException(const Node *node, QJSValue error);

QString message() const override;

private:
QString m_message;
};

class YamlCppConfigException
: public ConfigException
, public virtual Copyable<YamlCppConfigException, ConfigIssue>
Expand Down
21 changes: 21 additions & 0 deletions src/libinputactions/config/ConfigLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "ConfigIssueManager.h"
#include "GlobalConfig.h"
#include "Node.h"
#include "config/ConfigIssue.h"
#include "interfaces/ConfigProvider.h"
#include "parsers/containers.h"
#include "parsers/core.h"
Expand All @@ -33,6 +34,7 @@
#include <libinputactions/input/backends/LibevdevComplementaryInputBackend.h>
#include <libinputactions/input/devices/InputDeviceRule.h>
#include <libinputactions/interfaces/NotificationManager.h>
#include <libinputactions/scripting/ScriptingEngine.h>

namespace InputActions
{
Expand All @@ -52,6 +54,8 @@ struct Config

std::vector<InputDeviceRule> deviceRules;
std::set<KeyboardKey> emergencyCombination = {KEY_BACKSPACE, KEY_SPACE, KEY_ENTER};

std::unique_ptr<ScriptingEngine> scriptingEngine = std::make_unique<ScriptingEngine>();
};

void ConfigLoader::loadEmpty()
Expand Down Expand Up @@ -97,6 +101,21 @@ Config ConfigLoader::createConfig(const QString &raw)
}

Config config;
m_scriptingEngine = config.scriptingEngine.get();

if (const auto *scriptingNode = root->mapAt("scripting")) {
if (const auto *scriptsNode = scriptingNode->at("scripts")) {
for (const auto *scriptNode : scriptsNode->sequenceItems()) {
const auto *sourceNode = scriptNode->at("source", true);
const auto source = sourceNode->as<QString>();
const auto result = m_scriptingEngine->evaluate(sourceNode->as<QString>());
if (result.isError()) {
throw UncaughtScriptErrorConfigException(sourceNode, result);
}
}
}
}

loadMember(config.autoReload, root->at("autoreload"));
loadMember(config.allowExternalVariableAccess, root->at("external_variable_access"));
if (const auto *notificationsNode = root->mapAt("notifications")) {
Expand Down Expand Up @@ -133,6 +152,7 @@ void ConfigLoader::activateConfig(Config config, bool initialize)
g_inputBackend->reset(); // Okay because required keys are not cleared
g_actionExecutor->clearQueue();
g_actionExecutor->waitForDone();
g_scriptingEngine.reset();

g_globalConfig->setAllowExternalVariableAccess(config.allowExternalVariableAccess);
g_globalConfig->setAutoReload(config.autoReload);
Expand All @@ -150,6 +170,7 @@ void ConfigLoader::activateConfig(Config config, bool initialize)
g_inputBackend->setDeviceRules(config.deviceRules);
g_inputBackend->setEmergencyCombination(config.emergencyCombination);

g_scriptingEngine = std::move(config.scriptingEngine);
if (initialize) {
g_inputBackend->initialize();
}
Expand Down
9 changes: 9 additions & 0 deletions src/libinputactions/config/ConfigLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#pragma once

#include <QJSEngine>
#include <QString>
#include <memory>
#include <optional>
Expand All @@ -26,6 +27,7 @@ namespace InputActions
{

struct Config;
class ScriptingEngine;

struct ConfigLoadSettings
{
Expand All @@ -42,6 +44,11 @@ struct ConfigLoadSettings
class ConfigLoader
{
public:
/**
* Scripting engine for the configuration that is currently being loaded.
*/
ScriptingEngine &futureScriptingEngine() const { return *m_scriptingEngine; }

/**
* @return Whether the operation was successful. Errors may be obtained from ConfigIssueManager.
*/
Expand All @@ -55,6 +62,8 @@ class ConfigLoader
private:
Config createConfig(const QString &raw);
void activateConfig(Config config, bool initialize);

ScriptingEngine *m_scriptingEngine;
};

inline std::shared_ptr<ConfigLoader> g_configLoader;
Expand Down
10 changes: 10 additions & 0 deletions src/libinputactions/config/parsers/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "containers.h"
#include "flags.h"
#include "globals.h"
#include "scripting.h"
#include "separated-string.h"
#include "triggers.h"
#include "utils.h"
Expand All @@ -39,6 +40,7 @@
#include <libinputactions/conditions/VariableCondition.h>
#include <libinputactions/config/ConfigIssue.h>
#include <libinputactions/config/ConfigIssueManager.h>
#include <libinputactions/config/ConfigLoader.h>
#include <libinputactions/config/Node.h>
#include <libinputactions/handlers/KeyboardTriggerHandler.h>
#include <libinputactions/handlers/MouseTriggerHandler.h>
Expand All @@ -51,6 +53,9 @@
#include <libinputactions/input/backends/InputBackend.h>
#include <libinputactions/input/devices/InputDeviceRule.h>
#include <libinputactions/interfaces/CursorShapeProvider.h>
#include <libinputactions/scripting/ScriptAction.h>
#include <libinputactions/scripting/ScriptCondition.h>
#include <libinputactions/scripting/ScriptingEngine.h>
#include <libinputactions/triggers/core/StrokeTriggerCore.h>
#include <libinputactions/triggers/keyboard/KeyboardShortcutTrigger.h>
#include <libinputactions/triggers/mouse/MouseTrigger.h>
Expand Down Expand Up @@ -158,6 +163,8 @@ void NodeParser<std::unique_ptr<Action>>::parse(const Node *node, std::unique_pt
result = std::make_unique<PlasmaGlobalShortcutAction>(shortcut.first, shortcut.second);
} else if (const auto *replaceTextNode = node->at("replace_text")) {
result = std::make_unique<ReplaceTextAction>(replaceTextNode->as<std::vector<TextSubstitutionRule>>(true));
} else if (const auto *scriptActionNode = node->at("script")) {
result = std::make_unique<ScriptAction>(parseFunction(scriptActionNode), scriptActionNode->shared_from_this());
} else if (const auto *sleepActionNode = node->at("sleep")) {
result = std::make_unique<SleepAction>(sleepActionNode->as<std::chrono::milliseconds>());
} else if (const auto *oneNode = node->at("one")) {
Expand Down Expand Up @@ -225,6 +232,9 @@ std::shared_ptr<Condition> parseCondition(const Node *node, const VariableRegist
if (const auto *canReplaceTextNode = node->at("can_replace_text")) {
return std::make_shared<CanReplaceTextCondition>(canReplaceTextNode->as<std::vector<TextSubstitutionRule>>(true));
}
if (const auto *scriptNode = node->at("script")) {
return std::make_shared<ScriptCondition>(parseFunction(scriptNode), scriptNode->shared_from_this());
}

if (isLegacy(node)) {
g_configIssueManager->addIssue(DeprecatedFeatureConfigIssue(node, DeprecatedFeature::LegacyConditions));
Expand Down
40 changes: 40 additions & 0 deletions src/libinputactions/config/parsers/scripting.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
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 "scripting.h"
#include <libinputactions/config/ConfigLoader.h>
#include <libinputactions/config/Node.h>
#include <libinputactions/scripting/ScriptingEngine.h>

namespace InputActions
{

QJSValue parseFunction(const Node *node)
{
auto result = g_configLoader->futureScriptingEngine().evaluate(node->as<QString>());
if (result.isError()) {
throw UncaughtScriptErrorConfigException(node, result);
}
if (!result.isCallable()) {
throw InvalidValueConfigException(node, "Expression is not a function.");
}

return result;
}

}
30 changes: 30 additions & 0 deletions src/libinputactions/config/parsers/scripting.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
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 <QJSValue>

namespace InputActions
{

class Node;

QJSValue parseFunction(const Node *node);

}
Loading