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
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 13 additions & 24 deletions src/libinputactions/scripting/ScriptingEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <libinputactions/InputActionsMain.h>
#include <libinputactions/PointF.h>
#include <libinputactions/config/ConfigIssue.h>
#include <libinputactions/globals.h>
#include <libinputactions/helpers/QString.h>
Expand Down Expand Up @@ -74,25 +75,11 @@ void ScriptingEngine::initialize()

m_coreModule = std::make_unique<CoreModule>(*this, m_variableRegistry);
QJSEngine::setObjectOwnership(m_coreModule.get(), QJSEngine::CppOwnership);
auto coreModule = m_engine->newQObject(m_coreModule.get());
coreModule.setProperty("KeyboardModifier", newEnum<KeyboardModifier>());
coreModule.setProperty("VariableType", newEnum<VariableType>());
registerBuiltinModule("inputactions/core", coreModule);

auto desktopModule = m_engine->newObject();
desktopModule.setProperty("CursorShape", newEnum<CursorShape>());
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<QJSValue, QString>(&File::readAllText));
file.setProperty("writeAllText", newFunction<QJSValue, QString, QString>(&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<QJSValue, QString>([this](QString module) {
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 9 additions & 8 deletions src/libinputactions/scripting/ScriptingEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace InputActions
{

class CoreModule;
class Module;
class Promise;
class VariableRegistry;

Expand Down Expand Up @@ -78,6 +79,13 @@ class ScriptingEngine : public QObject

Promise newPromise();

template<typename T>
QJSValue newEnum()
{
return newEnum(QMetaEnum::fromType<T>());
}
QJSValue newEnum(const QMetaEnum &metaEnum);

/**
* Returns an instance of the engine. Initializes the engine if it has not been initialized yet.
*/
Expand All @@ -92,14 +100,7 @@ private slots:
void initialize();
void initializeWatchdog();

void registerBuiltinModule(const QString &name, QJSValue value);

template<typename T>
QJSValue newEnum()
{
return newEnum(QMetaEnum::fromType<T>());
}
QJSValue newEnum(const QMetaEnum &metaEnum);
void registerBuiltinModule(const QString &name, Module *module);

VariableRegistry &m_variableRegistry;

Expand Down
29 changes: 29 additions & 0 deletions src/libinputactions/scripting/modules/Module.cpp
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

#include "Module.h"

namespace InputActions
{

Module::Module(ScriptingEngine &engine)
: m_engine(engine)
{
}

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

#pragma once

#include <QJSValue>
#include <QObject>

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;
};

}
9 changes: 7 additions & 2 deletions src/libinputactions/scripting/modules/core/CoreModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<KeyboardModifier>());
self.setProperty("VariableType", engine().newEnum<VariableType>());
}

Config *CoreModule::config()
{
Expand Down
8 changes: 4 additions & 4 deletions src/libinputactions/scripting/modules/core/CoreModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@

#include "Config.h"
#include "VariableRegistryWrapper.h"
#include <QObject>
#include <libinputactions/scripting/modules/Module.h>

namespace InputActions
{

class InputBackend;
class ScriptingEngine;

class CoreModule : public QObject
class CoreModule : public Module
{
Q_OBJECT

Expand All @@ -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;
Expand Down
36 changes: 36 additions & 0 deletions src/libinputactions/scripting/modules/desktop/DesktopModule.cpp
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/>.
*/

#include "DesktopModule.h"
#include <libinputactions/globals.h>
#include <libinputactions/scripting/ScriptingEngine.h>

namespace InputActions
{

DesktopModule::DesktopModule(ScriptingEngine &engine)
: Module(engine)
{
}

void DesktopModule::initialize(QJSValue &self)
{
self.setProperty("CursorShape", engine().newEnum<CursorShape>());
}

}
36 changes: 36 additions & 0 deletions src/libinputactions/scripting/modules/desktop/DesktopModule.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 <libinputactions/scripting/modules/Module.h>

namespace InputActions
{

class DesktopModule : public Module
{
Q_OBJECT

public:
DesktopModule(ScriptingEngine &engine);

void initialize(QJSValue &self) override;
};

}
39 changes: 39 additions & 0 deletions src/libinputactions/scripting/modules/fs/FSModule.cpp
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

#include "FSModule.h"
#include "File.h"
#include <libinputactions/scripting/ScriptingEngine.h>

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<QJSValue, QString>(&File::readAllText));
file.setProperty("writeAllText", engine().newFunction<QJSValue, QString, QString>(&File::writeAllText));
self.setProperty("File", file);
}

}
36 changes: 36 additions & 0 deletions src/libinputactions/scripting/modules/fs/FSModule.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 <libinputactions/scripting/modules/Module.h>

namespace InputActions
{

class FSModule : public Module
{
Q_OBJECT

public:
FSModule(ScriptingEngine &engine);

void initialize(QJSValue &self) override;
};

}
Loading