2010-09-21 14:31:40 +00:00
|
|
|
/********************************************************************
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
|
|
|
|
|
|
|
Copyright (C) 2010 Rohan Prabhu <rohan@rohanprabhu.com>
|
2011-12-23 10:52:06 +00:00
|
|
|
Copyright (C) 2011 Martin Gräßlin <mgraesslin@kde.org>
|
2010-09-21 14:31:40 +00:00
|
|
|
|
|
|
|
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 2 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*********************************************************************/
|
|
|
|
|
|
|
|
#include "scripting.h"
|
2011-12-23 10:52:06 +00:00
|
|
|
// own
|
2013-08-12 12:58:05 +00:00
|
|
|
#include "dbuscall.h"
|
2011-12-23 10:52:06 +00:00
|
|
|
#include "meta.h"
|
2012-03-25 07:59:01 +00:00
|
|
|
#include "scriptingutils.h"
|
2012-01-22 11:38:03 +00:00
|
|
|
#include "workspace_wrapper.h"
|
2013-08-13 07:20:18 +00:00
|
|
|
#include "screenedgeitem.h"
|
2013-03-06 08:42:45 +00:00
|
|
|
#include "scripting_model.h"
|
2015-07-31 11:24:56 +00:00
|
|
|
#include "scripting_logging.h"
|
2012-03-24 09:49:21 +00:00
|
|
|
#include "../client.h"
|
2012-02-19 14:43:24 +00:00
|
|
|
#include "../thumbnailitem.h"
|
2012-03-01 09:18:39 +00:00
|
|
|
#include "../options.h"
|
2012-03-01 10:29:24 +00:00
|
|
|
#include "../workspace.h"
|
2011-12-23 10:52:06 +00:00
|
|
|
// KDE
|
2014-03-17 15:24:10 +00:00
|
|
|
#include <KConfigGroup>
|
2015-07-07 06:48:24 +00:00
|
|
|
#include <KPackage/PackageLoader>
|
2011-12-23 10:52:06 +00:00
|
|
|
// Qt
|
2018-06-05 10:52:57 +00:00
|
|
|
#include <QDBusConnection>
|
|
|
|
#include <QDBusMessage>
|
|
|
|
#include <QDBusPendingCallWatcher>
|
2013-09-02 11:14:39 +00:00
|
|
|
#include <QDebug>
|
2013-02-26 08:00:51 +00:00
|
|
|
#include <QFutureWatcher>
|
|
|
|
#include <QSettings>
|
|
|
|
#include <QtConcurrentRun>
|
|
|
|
#include <QMenu>
|
2013-08-12 11:09:46 +00:00
|
|
|
#include <QQmlContext>
|
|
|
|
#include <QQmlEngine>
|
2016-11-02 21:56:56 +00:00
|
|
|
#include <QQmlExpression>
|
2011-12-23 10:52:06 +00:00
|
|
|
#include <QtScript/QScriptEngine>
|
|
|
|
#include <QtScript/QScriptValue>
|
2018-06-05 10:52:57 +00:00
|
|
|
#include <QStandardPaths>
|
2013-11-07 13:10:35 +00:00
|
|
|
#include <QQuickWindow>
|
2011-12-23 10:52:06 +00:00
|
|
|
|
|
|
|
QScriptValue kwinScriptPrint(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
2012-03-01 10:28:27 +00:00
|
|
|
KWin::AbstractScript *script = qobject_cast<KWin::Script*>(context->callee().data().toQObject());
|
|
|
|
if (!script) {
|
|
|
|
return engine->undefinedValue();
|
|
|
|
}
|
2011-12-23 10:52:06 +00:00
|
|
|
QString result;
|
2013-02-14 14:19:35 +00:00
|
|
|
QTextStream stream(&result);
|
2011-12-23 10:52:06 +00:00
|
|
|
for (int i = 0; i < context->argumentCount(); ++i) {
|
|
|
|
if (i > 0) {
|
2013-02-14 14:19:35 +00:00
|
|
|
stream << " ";
|
|
|
|
}
|
|
|
|
QScriptValue argument = context->argument(i);
|
|
|
|
if (KWin::Client *client = qscriptvalue_cast<KWin::Client*>(argument)) {
|
|
|
|
client->print<QTextStream>(stream);
|
|
|
|
} else {
|
|
|
|
stream << argument.toString();
|
2011-12-23 10:52:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
script->printMessage(result);
|
|
|
|
|
|
|
|
return engine->undefinedValue();
|
|
|
|
}
|
2010-09-21 14:31:40 +00:00
|
|
|
|
2012-02-25 10:30:45 +00:00
|
|
|
QScriptValue kwinScriptReadConfig(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
KWin::AbstractScript *script = qobject_cast<KWin::AbstractScript*>(context->callee().data().toQObject());
|
2012-03-01 10:28:27 +00:00
|
|
|
if (!script) {
|
|
|
|
return engine->undefinedValue();
|
|
|
|
}
|
2012-02-25 10:30:45 +00:00
|
|
|
if (context->argumentCount() < 1 || context->argumentCount() > 2) {
|
2015-07-31 11:24:56 +00:00
|
|
|
qCDebug(KWIN_SCRIPTING) << "Incorrect number of arguments";
|
2012-02-25 10:30:45 +00:00
|
|
|
return engine->undefinedValue();
|
|
|
|
}
|
|
|
|
const QString key = context->argument(0).toString();
|
|
|
|
QVariant defaultValue;
|
|
|
|
if (context->argumentCount() == 2) {
|
|
|
|
defaultValue = context->argument(1).toVariant();
|
|
|
|
}
|
|
|
|
return engine->newVariant(script->config().readEntry(key, defaultValue));
|
|
|
|
}
|
|
|
|
|
2012-03-25 07:59:01 +00:00
|
|
|
QScriptValue kwinScriptGlobalShortcut(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
return KWin::globalShortcut<KWin::AbstractScript*>(context, engine);
|
|
|
|
}
|
|
|
|
|
2012-05-06 10:06:10 +00:00
|
|
|
QScriptValue kwinAssertTrue(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
return KWin::scriptingAssert<bool>(context, engine, 1, 2, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue kwinAssertFalse(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
return KWin::scriptingAssert<bool>(context, engine, 1, 2, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue kwinAssertEquals(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
return KWin::scriptingAssert<QVariant>(context, engine, 2, 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue kwinAssertNull(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
if (!KWin::validateParameters(context, 1, 2)) {
|
|
|
|
return engine->undefinedValue();
|
|
|
|
}
|
|
|
|
if (!context->argument(0).isNull()) {
|
|
|
|
if (context->argumentCount() == 2) {
|
|
|
|
context->throwError(QScriptContext::UnknownError, context->argument(1).toString());
|
|
|
|
} else {
|
|
|
|
context->throwError(QScriptContext::UnknownError,
|
|
|
|
i18nc("Assertion failed in KWin script with given value",
|
|
|
|
"Assertion failed: %1 is not null", context->argument(0).toString()));
|
|
|
|
}
|
|
|
|
return engine->undefinedValue();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue kwinAssertNotNull(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
if (!KWin::validateParameters(context, 1, 2)) {
|
|
|
|
return engine->undefinedValue();
|
|
|
|
}
|
|
|
|
if (context->argument(0).isNull()) {
|
|
|
|
if (context->argumentCount() == 2) {
|
|
|
|
context->throwError(QScriptContext::UnknownError, context->argument(1).toString());
|
|
|
|
} else {
|
|
|
|
context->throwError(QScriptContext::UnknownError,
|
|
|
|
i18nc("Assertion failed in KWin script",
|
|
|
|
"Assertion failed: argument is null"));
|
|
|
|
}
|
|
|
|
return engine->undefinedValue();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-10 14:09:36 +00:00
|
|
|
QScriptValue kwinRegisterScreenEdge(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
return KWin::registerScreenEdge<KWin::AbstractScript*>(context, engine);
|
|
|
|
}
|
|
|
|
|
2016-10-07 12:29:29 +00:00
|
|
|
QScriptValue kwinUnregisterScreenEdge(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
return KWin::unregisterScreenEdge<KWin::AbstractScript*>(context, engine);
|
|
|
|
}
|
|
|
|
|
2017-04-02 13:42:18 +00:00
|
|
|
QScriptValue kwinRegisterTouchScreenEdge(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
return KWin::registerTouchScreenEdge<KWin::Script*>(context, engine);
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptValue kwinUnregisterTouchScreenEdge(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
return KWin::unregisterTouchScreenEdge<KWin::Script*>(context, engine);
|
|
|
|
}
|
2016-10-07 12:29:29 +00:00
|
|
|
|
2012-08-31 11:28:31 +00:00
|
|
|
QScriptValue kwinRegisterUserActionsMenu(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
return KWin::registerUserActionsMenu<KWin::AbstractScript*>(context, engine);
|
|
|
|
}
|
|
|
|
|
2012-06-30 14:13:47 +00:00
|
|
|
QScriptValue kwinCallDBus(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
KWin::AbstractScript *script = qobject_cast<KWin::AbstractScript*>(context->callee().data().toQObject());
|
|
|
|
if (!script) {
|
2013-07-23 05:02:52 +00:00
|
|
|
context->throwError(QScriptContext::UnknownError, QStringLiteral("Internal Error: script not registered"));
|
2012-06-30 14:13:47 +00:00
|
|
|
return engine->undefinedValue();
|
|
|
|
}
|
|
|
|
if (context->argumentCount() < 4) {
|
|
|
|
context->throwError(QScriptContext::SyntaxError,
|
|
|
|
i18nc("Error in KWin Script",
|
|
|
|
"Invalid number of arguments. At least service, path, interface and method need to be provided"));
|
|
|
|
return engine->undefinedValue();
|
|
|
|
}
|
|
|
|
if (!KWin::validateArgumentType<QString, QString, QString, QString>(context)) {
|
|
|
|
context->throwError(QScriptContext::SyntaxError,
|
|
|
|
i18nc("Error in KWin Script",
|
|
|
|
"Invalid type. Service, path, interface and method need to be string values"));
|
|
|
|
return engine->undefinedValue();
|
|
|
|
}
|
|
|
|
const QString service = context->argument(0).toString();
|
|
|
|
const QString path = context->argument(1).toString();
|
|
|
|
const QString interface = context->argument(2).toString();
|
|
|
|
const QString method = context->argument(3).toString();
|
|
|
|
int argumentsCount = context->argumentCount();
|
|
|
|
if (context->argument(argumentsCount-1).isFunction()) {
|
|
|
|
--argumentsCount;
|
|
|
|
}
|
|
|
|
QDBusMessage msg = QDBusMessage::createMethodCall(service, path, interface, method);
|
|
|
|
QVariantList arguments;
|
|
|
|
for (int i=4; i<argumentsCount; ++i) {
|
2013-01-17 08:30:09 +00:00
|
|
|
if (context->argument(i).isArray()) {
|
|
|
|
QStringList stringArray = engine->fromScriptValue<QStringList>(context->argument(i));
|
|
|
|
arguments << qVariantFromValue(stringArray);
|
|
|
|
} else {
|
|
|
|
arguments << context->argument(i).toVariant();
|
|
|
|
}
|
2012-06-30 14:13:47 +00:00
|
|
|
}
|
|
|
|
if (!arguments.isEmpty()) {
|
|
|
|
msg.setArguments(arguments);
|
|
|
|
}
|
|
|
|
if (argumentsCount == context->argumentCount()) {
|
|
|
|
// no callback, just fire and forget
|
|
|
|
QDBusConnection::sessionBus().asyncCall(msg);
|
|
|
|
} else {
|
|
|
|
// with a callback
|
|
|
|
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(QDBusConnection::sessionBus().asyncCall(msg), script);
|
|
|
|
watcher->setProperty("callback", script->registerCallback(context->argument(context->argumentCount()-1)));
|
|
|
|
QObject::connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)), script, SLOT(slotPendingDBusCall(QDBusPendingCallWatcher*)));
|
|
|
|
}
|
|
|
|
return engine->undefinedValue();
|
|
|
|
}
|
|
|
|
|
2012-02-25 10:30:45 +00:00
|
|
|
KWin::AbstractScript::AbstractScript(int id, QString scriptName, QString pluginName, QObject *parent)
|
2011-12-23 07:49:28 +00:00
|
|
|
: QObject(parent)
|
2012-02-19 14:43:24 +00:00
|
|
|
, m_scriptId(id)
|
2012-02-25 10:30:45 +00:00
|
|
|
, m_pluginName(pluginName)
|
2019-01-09 19:40:09 +00:00
|
|
|
, m_fileName(scriptName)
|
2011-12-23 10:52:06 +00:00
|
|
|
, m_running(false)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2012-02-25 10:30:45 +00:00
|
|
|
if (m_pluginName.isNull()) {
|
|
|
|
m_pluginName = scriptName;
|
|
|
|
}
|
2012-02-19 14:43:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
KWin::AbstractScript::~AbstractScript()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-02-25 10:30:45 +00:00
|
|
|
KConfigGroup KWin::AbstractScript::config() const
|
|
|
|
{
|
2016-01-29 10:24:18 +00:00
|
|
|
return kwinApp()->config()->group(QLatin1String("Script-") + m_pluginName);
|
2012-02-25 10:30:45 +00:00
|
|
|
}
|
|
|
|
|
2012-02-19 14:43:24 +00:00
|
|
|
void KWin::AbstractScript::stop()
|
|
|
|
{
|
|
|
|
deleteLater();
|
|
|
|
}
|
|
|
|
|
2012-03-01 10:28:27 +00:00
|
|
|
void KWin::AbstractScript::printMessage(const QString &message)
|
|
|
|
{
|
2019-01-09 19:40:09 +00:00
|
|
|
qCDebug(KWIN_SCRIPTING) << fileName() << ":" << message;
|
2012-03-01 10:28:27 +00:00
|
|
|
emit print(message);
|
|
|
|
}
|
|
|
|
|
2012-03-25 07:59:01 +00:00
|
|
|
void KWin::AbstractScript::registerShortcut(QAction *a, QScriptValue callback)
|
|
|
|
{
|
|
|
|
m_shortcutCallbacks.insert(a, callback);
|
|
|
|
connect(a, SIGNAL(triggered(bool)), SLOT(globalShortcutTriggered()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void KWin::AbstractScript::globalShortcutTriggered()
|
|
|
|
{
|
|
|
|
callGlobalShortcutCallback<KWin::AbstractScript*>(this, sender());
|
|
|
|
}
|
|
|
|
|
2013-01-22 11:47:06 +00:00
|
|
|
bool KWin::AbstractScript::borderActivated(KWin::ElectricBorder edge)
|
2012-05-10 14:09:36 +00:00
|
|
|
{
|
|
|
|
screenEdgeActivated(this, edge);
|
2013-01-22 11:47:06 +00:00
|
|
|
return true;
|
2012-05-10 14:09:36 +00:00
|
|
|
}
|
|
|
|
|
2013-08-12 11:09:46 +00:00
|
|
|
void KWin::Script::installScriptFunctions(QScriptEngine* engine)
|
2012-02-25 10:30:45 +00:00
|
|
|
{
|
|
|
|
// add our print
|
|
|
|
QScriptValue printFunc = engine->newFunction(kwinScriptPrint);
|
|
|
|
printFunc.setData(engine->newQObject(this));
|
2013-07-23 05:02:52 +00:00
|
|
|
engine->globalObject().setProperty(QStringLiteral("print"), printFunc);
|
2012-02-25 10:30:45 +00:00
|
|
|
// add read config
|
|
|
|
QScriptValue configFunc = engine->newFunction(kwinScriptReadConfig);
|
|
|
|
configFunc.setData(engine->newQObject(this));
|
2013-07-23 05:02:52 +00:00
|
|
|
engine->globalObject().setProperty(QStringLiteral("readConfig"), configFunc);
|
2012-06-30 14:13:47 +00:00
|
|
|
QScriptValue dbusCallFunc = engine->newFunction(kwinCallDBus);
|
|
|
|
dbusCallFunc.setData(engine->newQObject(this));
|
2013-07-23 05:02:52 +00:00
|
|
|
engine->globalObject().setProperty(QStringLiteral("callDBus"), dbusCallFunc);
|
2012-03-25 07:59:01 +00:00
|
|
|
// add global Shortcut
|
|
|
|
registerGlobalShortcutFunction(this, engine, kwinScriptGlobalShortcut);
|
2012-05-10 14:09:36 +00:00
|
|
|
// add screen edge
|
|
|
|
registerScreenEdgeFunction(this, engine, kwinRegisterScreenEdge);
|
2016-10-07 12:29:29 +00:00
|
|
|
unregisterScreenEdgeFunction(this, engine, kwinUnregisterScreenEdge);
|
2017-04-02 13:42:18 +00:00
|
|
|
registerTouchScreenEdgeFunction(this, engine, kwinRegisterTouchScreenEdge);
|
|
|
|
unregisterTouchScreenEdgeFunction(this, engine, kwinUnregisterTouchScreenEdge);
|
2016-10-07 12:29:29 +00:00
|
|
|
|
2012-08-31 11:28:31 +00:00
|
|
|
// add user actions menu register function
|
2016-10-07 12:30:32 +00:00
|
|
|
registerUserActionsMenuFunction(this, engine, kwinRegisterUserActionsMenu);
|
2012-05-06 10:06:10 +00:00
|
|
|
// add assertions
|
|
|
|
QScriptValue assertTrueFunc = engine->newFunction(kwinAssertTrue);
|
2013-07-23 05:02:52 +00:00
|
|
|
engine->globalObject().setProperty(QStringLiteral("assertTrue"), assertTrueFunc);
|
|
|
|
engine->globalObject().setProperty(QStringLiteral("assert"), assertTrueFunc);
|
2012-05-06 10:06:10 +00:00
|
|
|
QScriptValue assertFalseFunc = engine->newFunction(kwinAssertFalse);
|
2013-07-23 05:02:52 +00:00
|
|
|
engine->globalObject().setProperty(QStringLiteral("assertFalse"), assertFalseFunc);
|
2012-05-06 10:06:10 +00:00
|
|
|
QScriptValue assertEqualsFunc = engine->newFunction(kwinAssertEquals);
|
2013-07-23 05:02:52 +00:00
|
|
|
engine->globalObject().setProperty(QStringLiteral("assertEquals"), assertEqualsFunc);
|
2012-05-06 10:06:10 +00:00
|
|
|
QScriptValue assertNullFunc = engine->newFunction(kwinAssertNull);
|
2013-07-23 05:02:52 +00:00
|
|
|
engine->globalObject().setProperty(QStringLiteral("assertNull"), assertNullFunc);
|
|
|
|
engine->globalObject().setProperty(QStringLiteral("assertEquals"), assertEqualsFunc);
|
2012-05-06 10:06:10 +00:00
|
|
|
QScriptValue assertNotNullFunc = engine->newFunction(kwinAssertNotNull);
|
2013-07-23 05:02:52 +00:00
|
|
|
engine->globalObject().setProperty(QStringLiteral("assertNotNull"), assertNotNullFunc);
|
2012-04-19 20:12:06 +00:00
|
|
|
// global properties
|
2016-11-02 21:56:56 +00:00
|
|
|
engine->globalObject().setProperty(QStringLiteral("KWin"), engine->newQMetaObject(&QtScriptWorkspaceWrapper::staticMetaObject));
|
2014-03-03 12:23:55 +00:00
|
|
|
QScriptValue workspace = engine->newQObject(Scripting::self()->workspaceWrapper(), QScriptEngine::QtOwnership,
|
2016-11-03 07:13:25 +00:00
|
|
|
QScriptEngine::ExcludeDeleteLater);
|
2013-07-23 05:02:52 +00:00
|
|
|
engine->globalObject().setProperty(QStringLiteral("workspace"), workspace, QScriptValue::Undeletable);
|
2012-04-19 20:12:06 +00:00
|
|
|
// install meta functions
|
|
|
|
KWin::MetaScripting::registration(engine);
|
2012-02-25 10:30:45 +00:00
|
|
|
}
|
|
|
|
|
2012-06-30 14:13:47 +00:00
|
|
|
int KWin::AbstractScript::registerCallback(QScriptValue value)
|
|
|
|
{
|
|
|
|
int id = m_callbacks.size();
|
|
|
|
m_callbacks.insert(id, value);
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void KWin::AbstractScript::slotPendingDBusCall(QDBusPendingCallWatcher* watcher)
|
|
|
|
{
|
|
|
|
if (watcher->isError()) {
|
2015-07-31 11:24:56 +00:00
|
|
|
qCDebug(KWIN_SCRIPTING) << "Received D-Bus message is error";
|
2012-06-30 14:13:47 +00:00
|
|
|
watcher->deleteLater();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const int id = watcher->property("callback").toInt();
|
|
|
|
QDBusMessage reply = watcher->reply();
|
|
|
|
QScriptValue callback (m_callbacks.value(id));
|
|
|
|
QScriptValueList arguments;
|
|
|
|
foreach (const QVariant &argument, reply.arguments()) {
|
|
|
|
arguments << callback.engine()->newVariant(argument);
|
|
|
|
}
|
|
|
|
callback.call(QScriptValue(), arguments);
|
|
|
|
m_callbacks.remove(id);
|
|
|
|
watcher->deleteLater();
|
|
|
|
}
|
|
|
|
|
2012-08-31 11:28:31 +00:00
|
|
|
void KWin::AbstractScript::registerUseractionsMenuCallback(QScriptValue callback)
|
|
|
|
{
|
|
|
|
m_userActionsMenuCallbacks.append(callback);
|
|
|
|
}
|
|
|
|
|
2015-03-06 09:49:37 +00:00
|
|
|
QList< QAction * > KWin::AbstractScript::actionsForUserActionMenu(KWin::AbstractClient *c, QMenu *parent)
|
2012-08-31 11:28:31 +00:00
|
|
|
{
|
|
|
|
QList<QAction*> returnActions;
|
|
|
|
for (QList<QScriptValue>::const_iterator it = m_userActionsMenuCallbacks.constBegin(); it != m_userActionsMenuCallbacks.constEnd(); ++it) {
|
|
|
|
QScriptValue callback(*it);
|
|
|
|
QScriptValueList arguments;
|
|
|
|
arguments << callback.engine()->newQObject(c);
|
|
|
|
QScriptValue actions = callback.call(QScriptValue(), arguments);
|
|
|
|
if (!actions.isValid() || actions.isUndefined() || actions.isNull()) {
|
|
|
|
// script does not want to handle this Client
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (actions.isObject()) {
|
|
|
|
QAction *a = scriptValueToAction(actions, parent);
|
|
|
|
if (a) {
|
|
|
|
returnActions << a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return returnActions;
|
|
|
|
}
|
|
|
|
|
|
|
|
QAction *KWin::AbstractScript::scriptValueToAction(QScriptValue &value, QMenu *parent)
|
|
|
|
{
|
2013-07-23 05:02:52 +00:00
|
|
|
QScriptValue titleValue = value.property(QStringLiteral("text"));
|
|
|
|
QScriptValue checkableValue = value.property(QStringLiteral("checkable"));
|
|
|
|
QScriptValue checkedValue = value.property(QStringLiteral("checked"));
|
|
|
|
QScriptValue itemsValue = value.property(QStringLiteral("items"));
|
|
|
|
QScriptValue triggeredValue = value.property(QStringLiteral("triggered"));
|
2012-08-31 11:28:31 +00:00
|
|
|
|
|
|
|
if (!titleValue.isValid()) {
|
|
|
|
// title not specified - does not make any sense to include
|
2014-02-24 15:33:40 +00:00
|
|
|
return nullptr;
|
2012-08-31 11:28:31 +00:00
|
|
|
}
|
|
|
|
const QString title = titleValue.toString();
|
|
|
|
const bool checkable = checkableValue.isValid() && checkableValue.toBool();
|
|
|
|
const bool checked = checkable && checkedValue.isValid() && checkedValue.toBool();
|
|
|
|
// either a menu or a menu item
|
|
|
|
if (itemsValue.isValid()) {
|
|
|
|
if (!itemsValue.isArray()) {
|
|
|
|
// not an array, so cannot be a menu
|
2014-02-24 15:33:40 +00:00
|
|
|
return nullptr;
|
2012-08-31 11:28:31 +00:00
|
|
|
}
|
2013-07-23 05:02:52 +00:00
|
|
|
QScriptValue lengthValue = itemsValue.property(QStringLiteral("length"));
|
2012-08-31 11:28:31 +00:00
|
|
|
if (!lengthValue.isValid() || !lengthValue.isNumber() || lengthValue.toInteger() == 0) {
|
|
|
|
// length property missing
|
2014-02-24 15:33:40 +00:00
|
|
|
return nullptr;
|
2012-08-31 11:28:31 +00:00
|
|
|
}
|
|
|
|
return createMenu(title, itemsValue, parent);
|
|
|
|
} else if (triggeredValue.isValid()) {
|
|
|
|
// normal item
|
|
|
|
return createAction(title, checkable, checked, triggeredValue, parent);
|
|
|
|
}
|
2014-02-24 15:33:40 +00:00
|
|
|
return nullptr;
|
2012-08-31 11:28:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QAction *KWin::AbstractScript::createAction(const QString &title, bool checkable, bool checked, QScriptValue &callback, QMenu *parent)
|
|
|
|
{
|
|
|
|
QAction *action = new QAction(title, parent);
|
|
|
|
action->setCheckable(checkable);
|
|
|
|
action->setChecked(checked);
|
|
|
|
// TODO: rename m_shortcutCallbacks
|
|
|
|
m_shortcutCallbacks.insert(action, callback);
|
|
|
|
connect(action, SIGNAL(triggered(bool)), SLOT(globalShortcutTriggered()));
|
|
|
|
connect(action, SIGNAL(destroyed(QObject*)), SLOT(actionDestroyed(QObject*)));
|
|
|
|
return action;
|
|
|
|
}
|
|
|
|
|
|
|
|
QAction *KWin::AbstractScript::createMenu(const QString &title, QScriptValue &items, QMenu *parent)
|
|
|
|
{
|
|
|
|
QMenu *menu = new QMenu(title, parent);
|
2013-07-23 05:02:52 +00:00
|
|
|
const int length = static_cast<int>(items.property(QStringLiteral("length")).toInteger());
|
2012-08-31 11:28:31 +00:00
|
|
|
for (int i=0; i<length; ++i) {
|
|
|
|
QScriptValue value = items.property(QString::number(i));
|
|
|
|
if (!value.isValid()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (value.isObject()) {
|
|
|
|
QAction *a = scriptValueToAction(value, menu);
|
|
|
|
if (a) {
|
|
|
|
menu->addAction(a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return menu->menuAction();
|
|
|
|
}
|
|
|
|
|
|
|
|
void KWin::AbstractScript::actionDestroyed(QObject *object)
|
|
|
|
{
|
|
|
|
// TODO: Qt 5 - change to lambda function
|
|
|
|
m_shortcutCallbacks.remove(static_cast<QAction*>(object));
|
|
|
|
}
|
|
|
|
|
2012-02-25 10:30:45 +00:00
|
|
|
KWin::Script::Script(int id, QString scriptName, QString pluginName, QObject* parent)
|
|
|
|
: AbstractScript(id, scriptName, pluginName, parent)
|
2012-02-19 14:43:24 +00:00
|
|
|
, m_engine(new QScriptEngine(this))
|
2012-04-29 17:30:12 +00:00
|
|
|
, m_starting(false)
|
2012-05-06 13:26:41 +00:00
|
|
|
, m_agent(new ScriptUnloaderAgent(this))
|
2012-02-19 14:43:24 +00:00
|
|
|
{
|
2015-11-05 14:14:06 +00:00
|
|
|
QDBusConnection::sessionBus().registerObject(QLatin1Char('/') + QString::number(scriptId()), this, QDBusConnection::ExportScriptableContents | QDBusConnection::ExportScriptableInvokables);
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-09-21 14:31:40 +00:00
|
|
|
|
2011-12-23 07:49:28 +00:00
|
|
|
KWin::Script::~Script()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2015-11-05 14:14:06 +00:00
|
|
|
QDBusConnection::sessionBus().unregisterObject(QLatin1Char('/') + QString::number(scriptId()));
|
2011-12-23 10:52:06 +00:00
|
|
|
}
|
|
|
|
|
2011-12-23 07:49:28 +00:00
|
|
|
void KWin::Script::run()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2012-04-29 17:30:12 +00:00
|
|
|
if (running() || m_starting) {
|
2011-12-23 10:52:06 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-04-29 17:30:12 +00:00
|
|
|
m_starting = true;
|
|
|
|
QFutureWatcher<QByteArray> *watcher = new QFutureWatcher<QByteArray>(this);
|
|
|
|
connect(watcher, SIGNAL(finished()), SLOT(slotScriptLoadedFromFile()));
|
2019-01-09 19:40:09 +00:00
|
|
|
watcher->setFuture(QtConcurrent::run(this, &KWin::Script::loadScriptFromFile, fileName()));
|
2012-04-29 17:30:12 +00:00
|
|
|
}
|
|
|
|
|
2019-01-09 19:40:09 +00:00
|
|
|
QByteArray KWin::Script::loadScriptFromFile(const QString &fileName)
|
2012-04-29 17:30:12 +00:00
|
|
|
{
|
2019-01-09 19:40:09 +00:00
|
|
|
QFile file(fileName);
|
|
|
|
if (!file.open(QIODevice::ReadOnly)) {
|
2012-04-29 17:30:12 +00:00
|
|
|
return QByteArray();
|
|
|
|
}
|
2019-01-09 19:40:09 +00:00
|
|
|
QByteArray result(file.readAll());
|
2012-04-29 17:30:12 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void KWin::Script::slotScriptLoadedFromFile()
|
|
|
|
{
|
|
|
|
QFutureWatcher<QByteArray> *watcher = dynamic_cast< QFutureWatcher< QByteArray>* >(sender());
|
|
|
|
if (!watcher) {
|
|
|
|
// not invoked from a QFutureWatcher
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (watcher->result().isNull()) {
|
|
|
|
// do not load empty script
|
|
|
|
deleteLater();
|
|
|
|
watcher->deleteLater();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QScriptValue optionsValue = m_engine->newQObject(options, QScriptEngine::QtOwnership,
|
|
|
|
QScriptEngine::ExcludeSuperClassContents | QScriptEngine::ExcludeDeleteLater);
|
2013-07-23 05:02:52 +00:00
|
|
|
m_engine->globalObject().setProperty(QStringLiteral("options"), optionsValue, QScriptValue::Undeletable);
|
|
|
|
m_engine->globalObject().setProperty(QStringLiteral("QTimer"), constructTimerClass(m_engine));
|
2012-04-29 17:30:12 +00:00
|
|
|
QObject::connect(m_engine, SIGNAL(signalHandlerException(QScriptValue)), this, SLOT(sigException(QScriptValue)));
|
|
|
|
KWin::MetaScripting::supplyConfig(m_engine);
|
|
|
|
installScriptFunctions(m_engine);
|
|
|
|
|
2013-07-23 05:02:52 +00:00
|
|
|
QScriptValue ret = m_engine->evaluate(QString::fromUtf8(watcher->result()));
|
2012-04-29 17:30:12 +00:00
|
|
|
|
|
|
|
if (ret.isError()) {
|
|
|
|
sigException(ret);
|
|
|
|
deleteLater();
|
2010-09-21 14:31:40 +00:00
|
|
|
}
|
2012-04-29 17:30:12 +00:00
|
|
|
|
|
|
|
watcher->deleteLater();
|
2012-02-19 14:43:24 +00:00
|
|
|
setRunning(true);
|
2012-04-29 17:30:12 +00:00
|
|
|
m_starting = false;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-09-21 14:31:40 +00:00
|
|
|
|
2011-12-23 07:49:28 +00:00
|
|
|
void KWin::Script::sigException(const QScriptValue& exception)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2010-09-21 14:31:40 +00:00
|
|
|
QScriptValue ret = exception;
|
2011-01-30 14:34:42 +00:00
|
|
|
if (ret.isError()) {
|
2015-07-31 11:24:56 +00:00
|
|
|
qCDebug(KWIN_SCRIPTING) << "defaultscript encountered an error at [Line " << m_engine->uncaughtExceptionLineNumber() << "]";
|
|
|
|
qCDebug(KWIN_SCRIPTING) << "Message: " << ret.toString();
|
|
|
|
qCDebug(KWIN_SCRIPTING) << "-----------------";
|
2010-09-21 14:31:40 +00:00
|
|
|
|
|
|
|
QScriptValueIterator iter(ret);
|
2011-01-30 14:34:42 +00:00
|
|
|
while (iter.hasNext()) {
|
2010-09-21 14:31:40 +00:00
|
|
|
iter.next();
|
2015-07-31 11:24:56 +00:00
|
|
|
qCDebug(KWIN_SCRIPTING) << " " << iter.name() << ": " << iter.value().toString();
|
2010-09-21 14:31:40 +00:00
|
|
|
}
|
|
|
|
}
|
2011-12-23 10:52:06 +00:00
|
|
|
emit printError(exception.toString());
|
2012-05-06 13:26:41 +00:00
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
2017-04-02 13:42:18 +00:00
|
|
|
bool KWin::Script::registerTouchScreenCallback(int edge, QScriptValue callback)
|
|
|
|
{
|
|
|
|
if (m_touchScreenEdgeCallbacks.constFind(edge) != m_touchScreenEdgeCallbacks.constEnd()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
QAction *action = new QAction(this);
|
|
|
|
connect(action, &QAction::triggered, this,
|
|
|
|
[callback] {
|
|
|
|
QScriptValue invoke(callback);
|
|
|
|
invoke.call();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
ScreenEdges::self()->reserveTouch(KWin::ElectricBorder(edge), action);
|
|
|
|
m_touchScreenEdgeCallbacks.insert(edge, action);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool KWin::Script::unregisterTouchScreenCallback(int edge)
|
|
|
|
{
|
|
|
|
auto it = m_touchScreenEdgeCallbacks.find(edge);
|
2017-04-07 18:36:36 +00:00
|
|
|
if (it == m_touchScreenEdgeCallbacks.end()) {
|
2017-04-02 13:42:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
delete it.value();
|
|
|
|
m_touchScreenEdgeCallbacks.erase(it);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-06 13:26:41 +00:00
|
|
|
KWin::ScriptUnloaderAgent::ScriptUnloaderAgent(KWin::Script *script)
|
|
|
|
: QScriptEngineAgent(script->engine())
|
|
|
|
, m_script(script)
|
|
|
|
{
|
|
|
|
script->engine()->setAgent(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void KWin::ScriptUnloaderAgent::scriptUnload(qint64 id)
|
|
|
|
{
|
2012-05-10 19:03:35 +00:00
|
|
|
Q_UNUSED(id)
|
2012-05-06 13:26:41 +00:00
|
|
|
m_script->stop();
|
2011-12-23 10:52:06 +00:00
|
|
|
}
|
|
|
|
|
2012-02-25 10:30:45 +00:00
|
|
|
KWin::DeclarativeScript::DeclarativeScript(int id, QString scriptName, QString pluginName, QObject* parent)
|
|
|
|
: AbstractScript(id, scriptName, pluginName, parent)
|
2016-11-02 21:56:56 +00:00
|
|
|
, m_context(new QQmlContext(Scripting::self()->declarativeScriptSharedContext(), this))
|
2014-03-03 12:23:55 +00:00
|
|
|
, m_component(new QQmlComponent(Scripting::self()->qmlEngine(), this))
|
2012-02-19 14:43:24 +00:00
|
|
|
{
|
2014-03-03 12:23:55 +00:00
|
|
|
m_context->setContextProperty(QStringLiteral("KWin"), new JSEngineGlobalMethodsWrapper(this));
|
2012-02-19 14:43:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
KWin::DeclarativeScript::~DeclarativeScript()
|
2011-12-23 10:52:06 +00:00
|
|
|
{
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-09-21 14:31:40 +00:00
|
|
|
|
2012-02-19 14:43:24 +00:00
|
|
|
void KWin::DeclarativeScript::run()
|
|
|
|
{
|
|
|
|
if (running()) {
|
|
|
|
return;
|
|
|
|
}
|
2013-03-27 10:37:43 +00:00
|
|
|
|
2019-01-09 19:40:09 +00:00
|
|
|
m_component->loadUrl(QUrl::fromLocalFile(fileName()));
|
2013-03-27 10:37:43 +00:00
|
|
|
if (m_component->isLoading()) {
|
2013-08-12 11:09:46 +00:00
|
|
|
connect(m_component, &QQmlComponent::statusChanged, this, &DeclarativeScript::createComponent);
|
2013-03-27 10:37:43 +00:00
|
|
|
} else {
|
|
|
|
createComponent();
|
|
|
|
}
|
|
|
|
}
|
2012-02-19 14:43:24 +00:00
|
|
|
|
2013-03-27 10:37:43 +00:00
|
|
|
void KWin::DeclarativeScript::createComponent()
|
|
|
|
{
|
|
|
|
if (m_component->isError()) {
|
2015-07-31 11:24:56 +00:00
|
|
|
qCDebug(KWIN_SCRIPTING) << "Component failed to load: " << m_component->errors();
|
2013-03-27 10:37:43 +00:00
|
|
|
} else {
|
2014-05-27 09:05:01 +00:00
|
|
|
if (QObject *object = m_component->create(m_context)) {
|
|
|
|
object->setParent(this);
|
|
|
|
}
|
2013-03-27 10:37:43 +00:00
|
|
|
}
|
2012-02-19 14:43:24 +00:00
|
|
|
setRunning(true);
|
|
|
|
}
|
2011-12-23 07:49:28 +00:00
|
|
|
|
2013-08-12 11:09:46 +00:00
|
|
|
KWin::JSEngineGlobalMethodsWrapper::JSEngineGlobalMethodsWrapper(KWin::DeclarativeScript *parent)
|
|
|
|
: QObject(parent)
|
|
|
|
, m_script(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
KWin::JSEngineGlobalMethodsWrapper::~JSEngineGlobalMethodsWrapper()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant KWin::JSEngineGlobalMethodsWrapper::readConfig(const QString &key, QVariant defaultValue)
|
|
|
|
{
|
|
|
|
return m_script->config().readEntry(key, defaultValue);
|
|
|
|
}
|
|
|
|
|
2013-11-07 13:10:35 +00:00
|
|
|
void KWin::JSEngineGlobalMethodsWrapper::registerWindow(QQuickWindow *window)
|
|
|
|
{
|
2018-09-13 09:19:12 +00:00
|
|
|
connect(window, &QWindow::visibilityChanged, this, [window](QWindow::Visibility visibility) {
|
2013-11-07 13:10:35 +00:00
|
|
|
if (visibility == QWindow::Hidden) {
|
|
|
|
window->destroy();
|
|
|
|
}
|
2018-09-13 09:19:12 +00:00
|
|
|
}, Qt::QueuedConnection);
|
2013-11-07 13:10:35 +00:00
|
|
|
}
|
|
|
|
|
2016-10-30 00:00:22 +00:00
|
|
|
bool KWin::JSEngineGlobalMethodsWrapper::registerShortcut(const QString &name, const QString &text, const QKeySequence& keys, QJSValue function)
|
|
|
|
{
|
|
|
|
if (!function.isCallable()) {
|
|
|
|
qCDebug(KWIN_SCRIPTING) << "Fourth and final argument must be a javascript function";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QAction *a = new QAction(this);
|
|
|
|
a->setObjectName(name);
|
|
|
|
a->setText(text);
|
|
|
|
const QKeySequence shortcut = QKeySequence(keys);
|
|
|
|
KGlobalAccel::self()->setShortcut(a, QList<QKeySequence>{shortcut});
|
|
|
|
KWin::input()->registerShortcut(shortcut, a);
|
|
|
|
|
|
|
|
connect(a, &QAction::triggered, this, [=]() mutable {
|
|
|
|
QJSValueList arguments;
|
|
|
|
arguments << Scripting::self()->qmlEngine()->toScriptValue(a);
|
|
|
|
function.call(arguments);
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-02-24 15:33:40 +00:00
|
|
|
KWin::Scripting *KWin::Scripting::s_self = nullptr;
|
2013-04-04 07:11:17 +00:00
|
|
|
|
|
|
|
KWin::Scripting *KWin::Scripting::create(QObject *parent)
|
|
|
|
{
|
|
|
|
Q_ASSERT(!s_self);
|
|
|
|
s_self = new Scripting(parent);
|
|
|
|
return s_self;
|
|
|
|
}
|
|
|
|
|
2011-12-23 07:56:25 +00:00
|
|
|
KWin::Scripting::Scripting(QObject *parent)
|
|
|
|
: QObject(parent)
|
2012-04-28 07:55:11 +00:00
|
|
|
, m_scriptsLock(new QMutex(QMutex::Recursive))
|
2014-03-03 12:23:55 +00:00
|
|
|
, m_qmlEngine(new QQmlEngine(this))
|
2016-11-02 21:56:56 +00:00
|
|
|
, m_declarativeScriptSharedContext(new QQmlContext(m_qmlEngine, this))
|
|
|
|
, m_workspaceWrapper(new QtScriptWorkspaceWrapper(this))
|
2011-12-23 07:49:28 +00:00
|
|
|
{
|
2014-03-03 12:23:55 +00:00
|
|
|
init();
|
2013-07-23 05:02:52 +00:00
|
|
|
QDBusConnection::sessionBus().registerObject(QStringLiteral("/Scripting"), this, QDBusConnection::ExportScriptableContents | QDBusConnection::ExportScriptableInvokables);
|
2012-03-01 10:29:24 +00:00
|
|
|
connect(Workspace::self(), SIGNAL(configChanged()), SLOT(start()));
|
2012-04-13 09:27:50 +00:00
|
|
|
connect(Workspace::self(), SIGNAL(workspaceInitialized()), SLOT(start()));
|
2011-12-23 07:49:28 +00:00
|
|
|
}
|
|
|
|
|
2014-03-03 12:23:55 +00:00
|
|
|
void KWin::Scripting::init()
|
|
|
|
{
|
|
|
|
qmlRegisterType<DesktopThumbnailItem>("org.kde.kwin", 2, 0, "DesktopThumbnailItem");
|
|
|
|
qmlRegisterType<WindowThumbnailItem>("org.kde.kwin", 2, 0, "ThumbnailItem");
|
|
|
|
qmlRegisterType<DBusCall>("org.kde.kwin", 2, 0, "DBusCall");
|
|
|
|
qmlRegisterType<ScreenEdgeItem>("org.kde.kwin", 2, 0, "ScreenEdgeItem");
|
|
|
|
qmlRegisterType<KWin::ScriptingClientModel::ClientModel>();
|
|
|
|
qmlRegisterType<KWin::ScriptingClientModel::SimpleClientModel>("org.kde.kwin", 2, 0, "ClientModel");
|
|
|
|
qmlRegisterType<KWin::ScriptingClientModel::ClientModelByScreen>("org.kde.kwin", 2, 0, "ClientModelByScreen");
|
|
|
|
qmlRegisterType<KWin::ScriptingClientModel::ClientModelByScreenAndDesktop>("org.kde.kwin", 2, 0, "ClientModelByScreenAndDesktop");
|
|
|
|
qmlRegisterType<KWin::ScriptingClientModel::ClientFilterModel>("org.kde.kwin", 2, 0, "ClientFilterModel");
|
2015-05-12 09:32:05 +00:00
|
|
|
qmlRegisterType<KWin::AbstractClient>();
|
2014-03-03 12:23:55 +00:00
|
|
|
qmlRegisterType<KWin::Client>();
|
2014-03-03 12:42:13 +00:00
|
|
|
qmlRegisterType<QAbstractItemModel>();
|
2014-03-03 12:23:55 +00:00
|
|
|
|
|
|
|
m_qmlEngine->rootContext()->setContextProperty(QStringLiteral("workspace"), m_workspaceWrapper);
|
|
|
|
m_qmlEngine->rootContext()->setContextProperty(QStringLiteral("options"), options);
|
2016-11-02 21:56:56 +00:00
|
|
|
|
|
|
|
m_declarativeScriptSharedContext->setContextProperty(QStringLiteral("workspace"), new DeclarativeScriptWorkspaceWrapper(this));
|
|
|
|
// QQmlListProperty interfaces only work via properties, rebind them as functions here
|
|
|
|
QQmlExpression expr(m_declarativeScriptSharedContext, nullptr, "workspace.clientList = function() { return workspace.clients }");
|
|
|
|
expr.evaluate();
|
2014-03-03 12:23:55 +00:00
|
|
|
}
|
|
|
|
|
2011-12-23 07:49:28 +00:00
|
|
|
void KWin::Scripting::start()
|
2012-04-28 07:55:11 +00:00
|
|
|
{
|
2012-09-14 14:08:45 +00:00
|
|
|
#if 0
|
|
|
|
// TODO make this threaded again once KConfigGroup is sufficiently thread safe, bug #305361 and friends
|
2012-04-28 07:55:11 +00:00
|
|
|
// perform querying for the services in a thread
|
|
|
|
QFutureWatcher<LoadScriptList> *watcher = new QFutureWatcher<LoadScriptList>(this);
|
|
|
|
connect(watcher, SIGNAL(finished()), this, SLOT(slotScriptsQueried()));
|
2012-07-20 20:01:26 +00:00
|
|
|
watcher->setFuture(QtConcurrent::run(this, &KWin::Scripting::queryScriptsToLoad, pluginStates, offers));
|
2012-09-14 14:08:45 +00:00
|
|
|
#else
|
|
|
|
LoadScriptList scriptsToLoad = queryScriptsToLoad();
|
|
|
|
for (LoadScriptList::const_iterator it = scriptsToLoad.constBegin();
|
|
|
|
it != scriptsToLoad.constEnd();
|
|
|
|
++it) {
|
|
|
|
if (it->first) {
|
|
|
|
loadScript(it->second.first, it->second.second);
|
|
|
|
} else {
|
|
|
|
loadDeclarativeScript(it->second.first, it->second.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
runScripts();
|
|
|
|
#endif
|
2012-05-05 15:36:30 +00:00
|
|
|
}
|
2011-12-23 07:49:28 +00:00
|
|
|
|
2012-09-14 14:08:45 +00:00
|
|
|
LoadScriptList KWin::Scripting::queryScriptsToLoad()
|
2012-05-05 15:36:30 +00:00
|
|
|
{
|
2016-01-29 10:24:18 +00:00
|
|
|
KSharedConfig::Ptr _config = kwinApp()->config();
|
2013-05-08 08:45:45 +00:00
|
|
|
static bool s_started = false;
|
|
|
|
if (s_started) {
|
|
|
|
_config->reparseConfiguration();
|
|
|
|
} else {
|
|
|
|
s_started = true;
|
|
|
|
}
|
2012-09-14 14:08:45 +00:00
|
|
|
QMap<QString,QString> pluginStates = KConfigGroup(_config, "Plugins").entryMap();
|
2015-11-05 14:14:06 +00:00
|
|
|
const QString scriptFolder = QStringLiteral(KWIN_NAME "/scripts/");
|
2015-07-07 06:48:24 +00:00
|
|
|
const auto offers = KPackage::PackageLoader::self()->listPackages(QStringLiteral("KWin/Script"), scriptFolder);
|
2012-09-14 14:08:45 +00:00
|
|
|
|
2012-04-28 07:55:11 +00:00
|
|
|
LoadScriptList scriptsToLoad;
|
2011-12-23 07:49:28 +00:00
|
|
|
|
2015-07-07 06:48:24 +00:00
|
|
|
for (const KPluginMetaData &service: offers) {
|
2015-11-05 14:14:06 +00:00
|
|
|
const QString value = pluginStates.value(service.pluginId() + QLatin1String("Enabled"), QString());
|
2015-07-07 06:48:24 +00:00
|
|
|
const bool enabled = value.isNull() ? service.isEnabledByDefault() : QVariant(value).toBool();
|
2015-11-05 14:14:06 +00:00
|
|
|
const bool javaScript = service.value(QStringLiteral("X-Plasma-API")) == QLatin1String("javascript");
|
|
|
|
const bool declarativeScript = service.value(QStringLiteral("X-Plasma-API")) == QLatin1String("declarativescript");
|
2012-02-19 14:43:24 +00:00
|
|
|
if (!javaScript && !declarativeScript) {
|
2012-02-18 09:34:27 +00:00
|
|
|
continue;
|
|
|
|
}
|
2011-12-23 07:49:28 +00:00
|
|
|
|
2015-07-07 06:48:24 +00:00
|
|
|
if (!enabled) {
|
|
|
|
if (isScriptLoaded(service.pluginId())) {
|
2012-03-01 10:29:24 +00:00
|
|
|
// unload the script
|
2015-07-07 06:48:24 +00:00
|
|
|
unloadScript(service.pluginId());
|
2012-03-01 10:29:24 +00:00
|
|
|
}
|
2012-02-18 09:34:27 +00:00
|
|
|
continue;
|
|
|
|
}
|
2015-07-07 06:48:24 +00:00
|
|
|
const QString pluginName = service.pluginId();
|
|
|
|
const QString scriptName = service.value(QStringLiteral("X-Plasma-MainScript"));
|
2015-11-05 14:14:06 +00:00
|
|
|
const QString file = QStandardPaths::locate(QStandardPaths::GenericDataLocation, scriptFolder + pluginName + QLatin1String("/contents/") + scriptName);
|
2012-02-18 09:34:27 +00:00
|
|
|
if (file.isNull()) {
|
2015-07-31 11:24:56 +00:00
|
|
|
qCDebug(KWIN_SCRIPTING) << "Could not find script file for " << pluginName;
|
2012-02-18 09:34:27 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-04-28 07:55:11 +00:00
|
|
|
scriptsToLoad << qMakePair(javaScript, qMakePair(file, pluginName));
|
|
|
|
}
|
|
|
|
return scriptsToLoad;
|
|
|
|
}
|
|
|
|
|
|
|
|
void KWin::Scripting::slotScriptsQueried()
|
|
|
|
{
|
|
|
|
QFutureWatcher<LoadScriptList> *watcher = dynamic_cast< QFutureWatcher<LoadScriptList>* >(sender());
|
|
|
|
if (!watcher) {
|
|
|
|
// slot invoked not from a FutureWatcher
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LoadScriptList scriptsToLoad = watcher->result();
|
|
|
|
for (LoadScriptList::const_iterator it = scriptsToLoad.constBegin();
|
|
|
|
it != scriptsToLoad.constEnd();
|
|
|
|
++it) {
|
|
|
|
if (it->first) {
|
|
|
|
loadScript(it->second.first, it->second.second);
|
|
|
|
} else {
|
|
|
|
loadDeclarativeScript(it->second.first, it->second.second);
|
2012-02-19 14:43:24 +00:00
|
|
|
}
|
2011-12-23 07:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
runScripts();
|
2012-04-28 07:55:11 +00:00
|
|
|
watcher->deleteLater();
|
2011-12-23 07:49:28 +00:00
|
|
|
}
|
|
|
|
|
2012-03-01 10:29:24 +00:00
|
|
|
bool KWin::Scripting::isScriptLoaded(const QString &pluginName) const
|
2016-08-19 07:12:49 +00:00
|
|
|
{
|
|
|
|
return findScript(pluginName) != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
KWin::AbstractScript *KWin::Scripting::findScript(const QString &pluginName) const
|
2012-03-01 10:29:24 +00:00
|
|
|
{
|
2012-04-28 07:55:11 +00:00
|
|
|
QMutexLocker locker(m_scriptsLock.data());
|
2012-03-01 10:29:24 +00:00
|
|
|
foreach (AbstractScript *script, scripts) {
|
|
|
|
if (script->pluginName() == pluginName) {
|
2016-08-19 07:12:49 +00:00
|
|
|
return script;
|
2012-03-01 10:29:24 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-19 07:12:49 +00:00
|
|
|
return nullptr;
|
2012-03-01 10:29:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool KWin::Scripting::unloadScript(const QString &pluginName)
|
|
|
|
{
|
2012-04-28 07:55:11 +00:00
|
|
|
QMutexLocker locker(m_scriptsLock.data());
|
2012-03-01 10:29:24 +00:00
|
|
|
foreach (AbstractScript *script, scripts) {
|
|
|
|
if (script->pluginName() == pluginName) {
|
|
|
|
script->deleteLater();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-12-23 07:49:28 +00:00
|
|
|
void KWin::Scripting::runScripts()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2012-04-28 07:55:11 +00:00
|
|
|
QMutexLocker locker(m_scriptsLock.data());
|
2011-01-30 14:34:42 +00:00
|
|
|
for (int i = 0; i < scripts.size(); i++) {
|
2011-12-23 07:49:28 +00:00
|
|
|
scripts.at(i)->run();
|
2010-09-21 14:31:40 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2011-12-23 07:49:28 +00:00
|
|
|
|
2011-12-23 10:52:06 +00:00
|
|
|
void KWin::Scripting::scriptDestroyed(QObject *object)
|
|
|
|
{
|
2012-04-28 07:55:11 +00:00
|
|
|
QMutexLocker locker(m_scriptsLock.data());
|
2011-12-23 10:52:06 +00:00
|
|
|
scripts.removeAll(static_cast<KWin::Script*>(object));
|
|
|
|
}
|
|
|
|
|
2012-02-25 10:30:45 +00:00
|
|
|
int KWin::Scripting::loadScript(const QString &filePath, const QString& pluginName)
|
2011-12-23 10:52:06 +00:00
|
|
|
{
|
2012-04-28 07:55:11 +00:00
|
|
|
QMutexLocker locker(m_scriptsLock.data());
|
2012-03-01 10:29:24 +00:00
|
|
|
if (isScriptLoaded(pluginName)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2011-12-23 10:52:06 +00:00
|
|
|
const int id = scripts.size();
|
2012-02-25 10:30:45 +00:00
|
|
|
KWin::Script *script = new KWin::Script(id, filePath, pluginName, this);
|
2011-12-23 10:52:06 +00:00
|
|
|
connect(script, SIGNAL(destroyed(QObject*)), SLOT(scriptDestroyed(QObject*)));
|
|
|
|
scripts.append(script);
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2012-02-25 10:30:45 +00:00
|
|
|
int KWin::Scripting::loadDeclarativeScript(const QString& filePath, const QString& pluginName)
|
2012-02-19 14:43:24 +00:00
|
|
|
{
|
2012-04-28 07:55:11 +00:00
|
|
|
QMutexLocker locker(m_scriptsLock.data());
|
2012-03-01 10:29:24 +00:00
|
|
|
if (isScriptLoaded(pluginName)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2012-02-19 14:43:24 +00:00
|
|
|
const int id = scripts.size();
|
2012-02-25 10:30:45 +00:00
|
|
|
KWin::DeclarativeScript *script = new KWin::DeclarativeScript(id, filePath, pluginName, this);
|
2012-02-19 14:43:24 +00:00
|
|
|
connect(script, SIGNAL(destroyed(QObject*)), SLOT(scriptDestroyed(QObject*)));
|
|
|
|
scripts.append(script);
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2011-12-23 07:49:28 +00:00
|
|
|
KWin::Scripting::~Scripting()
|
|
|
|
{
|
2013-07-23 05:02:52 +00:00
|
|
|
QDBusConnection::sessionBus().unregisterObject(QStringLiteral("/Scripting"));
|
2014-02-24 15:33:40 +00:00
|
|
|
s_self = nullptr;
|
2011-12-23 07:49:28 +00:00
|
|
|
}
|
2011-12-23 10:52:06 +00:00
|
|
|
|
2015-03-06 09:49:37 +00:00
|
|
|
QList< QAction * > KWin::Scripting::actionsForUserActionMenu(KWin::AbstractClient *c, QMenu *parent)
|
2012-08-31 11:28:31 +00:00
|
|
|
{
|
|
|
|
QList<QAction*> actions;
|
|
|
|
foreach (AbstractScript *script, scripts) {
|
|
|
|
actions << script->actionsForUserActionMenu(c, parent);
|
|
|
|
}
|
|
|
|
return actions;
|
|
|
|
}
|
|
|
|
|