Skip to content

Commit e93b2ce

Browse files
committed
scripting/core: add Script object
1 parent 9f758c1 commit e93b2ce

4 files changed

Lines changed: 89 additions & 4 deletions

File tree

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ set(libinputactions_SRCS
8484
libinputactions/interfaces/WindowProvider.cpp
8585
libinputactions/scripting/modules/core/Config.cpp
8686
libinputactions/scripting/modules/core/CoreModule.cpp
87+
libinputactions/scripting/modules/core/Script.cpp
8788
libinputactions/scripting/modules/fs/File.cpp
8889
libinputactions/scripting/FunctionWrapper.cpp
8990
libinputactions/scripting/JSFunctionAction.cpp

src/libinputactions/config/ConfigLoader.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
#include "parsers/containers.h"
2626
#include "parsers/core.h"
2727
#include "parsers/utils.h"
28-
#include <QFile>
28+
#include <QDir>
29+
#include <QFileInfo>
2930
#include <libinputactions/actions/ActionExecutor.h>
3031
#include <libinputactions/handlers/KeyboardTriggerHandler.h>
3132
#include <libinputactions/handlers/MouseTriggerHandler.h>
@@ -38,6 +39,7 @@
3839
#include <libinputactions/scripting/ScriptingEngine.h>
3940
#include <libinputactions/scripting/modules/core/Config.h>
4041
#include <libinputactions/scripting/modules/core/CoreModule.h>
42+
#include <libinputactions/scripting/modules/core/Script.h>
4143

4244
namespace InputActions
4345
{
@@ -130,15 +132,27 @@ ConfigData ConfigLoader::createConfig(const QString &raw)
130132
throw UncaughtScriptErrorConfigException(sourceNode, result);
131133
}
132134
} else if (const auto *fileNode = scriptNode->at("file", true)) {
133-
const auto file = fileNode->as<QString>();
134-
if (!QFile::exists(file)) {
135+
const QFileInfo file(fileNode->as<QString>());
136+
if (!file.exists()) {
135137
throw InvalidValueConfigException(fileNode, "File does not exist.");
136138
}
137139

138-
const auto result = g_scriptingEngine->importModule(file);
140+
const auto result = g_scriptingEngine->importModule(file.canonicalFilePath());
139141
if (result.isError()) {
140142
throw UncaughtScriptErrorConfigException(fileNode, result);
141143
}
144+
145+
const auto defaultFunc = result.property("default");
146+
if (!defaultFunc.isCallable()) {
147+
continue;
148+
}
149+
150+
const auto rootDirectory = file.dir().absolutePath();
151+
const auto defaultFuncResult = g_scriptingEngine->call(defaultFunc,
152+
{g_scriptingEngine->ensureEngine().newQObject(new Script(rootDirectory))});
153+
if (defaultFuncResult.isError()) {
154+
throw UncaughtScriptErrorConfigException(fileNode, defaultFuncResult);
155+
}
142156
}
143157
}
144158
}
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 "Script.h"
20+
21+
namespace InputActions
22+
{
23+
24+
Script::Script(QString rootDirectory)
25+
: m_rootDirectory(std::move(rootDirectory))
26+
{
27+
}
28+
29+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 Script : public QObject
27+
{
28+
Q_OBJECT
29+
Q_PROPERTY(QString rootDirectory READ rootDirectory)
30+
31+
public:
32+
Script(QString rootDirectory);
33+
~Script() override { qWarning("script deleted"); }
34+
35+
const QString &rootDirectory() const { return m_rootDirectory; }
36+
37+
private:
38+
QString m_rootDirectory;
39+
};
40+
41+
}

0 commit comments

Comments
 (0)