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/>.
|
|
|
|
*********************************************************************/
|
|
|
|
|
2011-01-01 09:37:08 +00:00
|
|
|
#ifndef KWIN_SCRIPTING_H
|
|
|
|
#define KWIN_SCRIPTING_H
|
2010-09-21 14:31:40 +00:00
|
|
|
|
2012-05-10 14:09:36 +00:00
|
|
|
#include <kwinglobals.h>
|
|
|
|
|
2012-02-18 12:58:00 +00:00
|
|
|
#include <QtCore/QFile>
|
2012-03-25 07:59:01 +00:00
|
|
|
#include <QtCore/QHash>
|
2012-02-18 12:58:00 +00:00
|
|
|
#include <QtCore/QStringList>
|
2012-05-06 13:26:41 +00:00
|
|
|
#include <QtScript/QScriptEngineAgent>
|
2010-09-21 14:31:40 +00:00
|
|
|
|
2012-03-25 07:59:01 +00:00
|
|
|
class QAction;
|
2012-02-19 14:43:24 +00:00
|
|
|
class QDeclarativeView;
|
2012-04-28 07:55:11 +00:00
|
|
|
class QMutex;
|
2011-12-23 10:52:06 +00:00
|
|
|
class QScriptEngine;
|
|
|
|
class QScriptValue;
|
2012-02-25 10:30:45 +00:00
|
|
|
class KConfigGroup;
|
2010-09-21 14:31:40 +00:00
|
|
|
|
2012-04-28 07:55:11 +00:00
|
|
|
/// @c true == javascript, @c false == qml
|
|
|
|
typedef QList< QPair<bool, QPair<QString, QString > > > LoadScriptList;
|
|
|
|
|
2010-09-21 14:31:40 +00:00
|
|
|
namespace KWin
|
|
|
|
{
|
2012-05-06 13:26:41 +00:00
|
|
|
class ScriptUnloaderAgent;
|
2012-01-22 11:38:03 +00:00
|
|
|
class WorkspaceWrapper;
|
2010-09-21 14:31:40 +00:00
|
|
|
|
2012-02-19 14:43:24 +00:00
|
|
|
class AbstractScript : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
2012-02-25 10:30:45 +00:00
|
|
|
AbstractScript(int id, QString scriptName, QString pluginName, QObject *parent = NULL);
|
2012-02-19 14:43:24 +00:00
|
|
|
~AbstractScript();
|
|
|
|
QString fileName() const {
|
|
|
|
return m_scriptFile.fileName();
|
|
|
|
}
|
2012-03-01 10:29:24 +00:00
|
|
|
const QString &pluginName() {
|
|
|
|
return m_pluginName;
|
|
|
|
}
|
2012-02-19 14:43:24 +00:00
|
|
|
|
2012-03-01 10:28:27 +00:00
|
|
|
void printMessage(const QString &message);
|
2012-03-25 07:59:01 +00:00
|
|
|
void registerShortcut(QAction *a, QScriptValue callback);
|
2012-03-01 10:28:27 +00:00
|
|
|
|
2012-02-25 10:30:45 +00:00
|
|
|
KConfigGroup config() const;
|
2012-03-25 07:59:01 +00:00
|
|
|
const QHash<QAction*, QScriptValue> &shortcutCallbacks() const {
|
|
|
|
return m_shortcutCallbacks;
|
|
|
|
}
|
2012-05-10 14:09:36 +00:00
|
|
|
QHash<int, QList<QScriptValue > > &screenEdgeCallbacks() {
|
|
|
|
return m_screenEdgeCallbacks;
|
|
|
|
}
|
2012-02-25 10:30:45 +00:00
|
|
|
|
2012-02-19 14:43:24 +00:00
|
|
|
public Q_SLOTS:
|
|
|
|
Q_SCRIPTABLE void stop();
|
|
|
|
Q_SCRIPTABLE virtual void run() = 0;
|
|
|
|
|
2012-03-25 07:59:01 +00:00
|
|
|
private Q_SLOTS:
|
|
|
|
void globalShortcutTriggered();
|
2012-05-10 14:09:36 +00:00
|
|
|
void borderActivated(ElectricBorder edge);
|
2012-03-25 07:59:01 +00:00
|
|
|
|
2012-03-01 10:28:27 +00:00
|
|
|
Q_SIGNALS:
|
|
|
|
Q_SCRIPTABLE void print(const QString &text);
|
|
|
|
|
2012-02-19 14:43:24 +00:00
|
|
|
protected:
|
|
|
|
QFile &scriptFile() {
|
|
|
|
return m_scriptFile;
|
|
|
|
}
|
|
|
|
bool running() const {
|
|
|
|
return m_running;
|
|
|
|
}
|
|
|
|
void setRunning(bool running) {
|
|
|
|
m_running = running;
|
|
|
|
}
|
|
|
|
int scriptId() const {
|
|
|
|
return m_scriptId;
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkspaceWrapper *workspace() {
|
|
|
|
return m_workspace;
|
|
|
|
}
|
|
|
|
|
2012-02-25 10:30:45 +00:00
|
|
|
void installScriptFunctions(QScriptEngine *engine);
|
|
|
|
|
2012-02-19 14:43:24 +00:00
|
|
|
private:
|
|
|
|
int m_scriptId;
|
|
|
|
QFile m_scriptFile;
|
2012-02-25 10:30:45 +00:00
|
|
|
QString m_pluginName;
|
2012-02-19 14:43:24 +00:00
|
|
|
bool m_running;
|
|
|
|
WorkspaceWrapper *m_workspace;
|
2012-03-25 07:59:01 +00:00
|
|
|
QHash<QAction*, QScriptValue> m_shortcutCallbacks;
|
2012-05-10 14:09:36 +00:00
|
|
|
QHash<int, QList<QScriptValue> > m_screenEdgeCallbacks;
|
2012-02-19 14:43:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class Script : public AbstractScript
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2011-12-23 07:49:28 +00:00
|
|
|
Q_OBJECT
|
2011-12-23 10:52:06 +00:00
|
|
|
Q_CLASSINFO("D-Bus Interface", "org.kde.kwin.Scripting")
|
2011-01-30 14:34:42 +00:00
|
|
|
public:
|
2011-12-23 07:49:28 +00:00
|
|
|
|
2012-02-25 10:30:45 +00:00
|
|
|
Script(int id, QString scriptName, QString pluginName, QObject *parent = NULL);
|
2011-12-23 07:49:28 +00:00
|
|
|
virtual ~Script();
|
2012-05-06 13:26:41 +00:00
|
|
|
QScriptEngine *engine() {
|
|
|
|
return m_engine;
|
|
|
|
}
|
2011-12-23 10:52:06 +00:00
|
|
|
|
|
|
|
public Q_SLOTS:
|
|
|
|
Q_SCRIPTABLE void run();
|
|
|
|
|
|
|
|
Q_SIGNALS:
|
|
|
|
Q_SCRIPTABLE void printError(const QString &text);
|
2011-12-23 07:49:28 +00:00
|
|
|
|
|
|
|
private slots:
|
|
|
|
/**
|
|
|
|
* A nice clean way to handle exceptions in scripting.
|
|
|
|
* TODO: Log to file, show from notifier..
|
|
|
|
*/
|
|
|
|
void sigException(const QScriptValue &exception);
|
2012-04-29 17:30:12 +00:00
|
|
|
/**
|
|
|
|
* Callback for when loadScriptFromFile has finished.
|
|
|
|
**/
|
|
|
|
void slotScriptLoadedFromFile();
|
2011-12-23 07:49:28 +00:00
|
|
|
|
|
|
|
private:
|
2012-04-29 17:30:12 +00:00
|
|
|
/**
|
|
|
|
* Read the script from file into a byte array.
|
|
|
|
* If file cannot be read an empty byte array is returned.
|
|
|
|
**/
|
|
|
|
QByteArray loadScriptFromFile();
|
2011-12-23 07:49:28 +00:00
|
|
|
QScriptEngine *m_engine;
|
2012-04-29 17:30:12 +00:00
|
|
|
bool m_starting;
|
2012-05-06 13:26:41 +00:00
|
|
|
QScopedPointer<ScriptUnloaderAgent> m_agent;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ScriptUnloaderAgent : public QScriptEngineAgent
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ScriptUnloaderAgent(Script *script);
|
|
|
|
virtual void scriptUnload(qint64 id);
|
|
|
|
|
|
|
|
private:
|
|
|
|
Script *m_script;
|
2012-02-19 14:43:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class DeclarativeScript : public AbstractScript
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
Q_CLASSINFO("D-Bus Interface", "org.kde.kwin.Scripting")
|
|
|
|
public:
|
2012-02-25 10:30:45 +00:00
|
|
|
explicit DeclarativeScript(int id, QString scriptName, QString pluginName, QObject *parent = 0);
|
2012-02-19 14:43:24 +00:00
|
|
|
virtual ~DeclarativeScript();
|
|
|
|
|
|
|
|
public Q_SLOTS:
|
|
|
|
Q_SCRIPTABLE void run();
|
|
|
|
|
|
|
|
private:
|
2012-05-04 06:22:53 +00:00
|
|
|
QScopedPointer<QDeclarativeView> m_view;
|
2011-01-30 14:34:42 +00:00
|
|
|
};
|
2010-09-21 14:31:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The heart of KWin::Scripting. Infinite power lies beyond
|
|
|
|
*/
|
2011-12-23 07:56:25 +00:00
|
|
|
class Scripting : public QObject
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
|
|
|
Q_OBJECT
|
2011-12-23 10:52:06 +00:00
|
|
|
Q_CLASSINFO("D-Bus Interface", "org.kde.kwin.Scripting")
|
2011-01-30 14:34:42 +00:00
|
|
|
private:
|
|
|
|
QStringList scriptList;
|
2012-02-19 14:43:24 +00:00
|
|
|
QList<KWin::AbstractScript*> scripts;
|
2012-04-28 07:55:11 +00:00
|
|
|
/**
|
|
|
|
* Lock to protect the scripts member variable.
|
|
|
|
**/
|
|
|
|
QScopedPointer<QMutex> m_scriptsLock;
|
2011-01-30 14:34:42 +00:00
|
|
|
|
|
|
|
// Preferably call ONLY at load time
|
|
|
|
void runScripts();
|
|
|
|
|
|
|
|
public:
|
2011-12-23 07:56:25 +00:00
|
|
|
Scripting(QObject *parent = NULL);
|
2011-01-30 14:34:42 +00:00
|
|
|
~Scripting();
|
2012-02-25 10:30:45 +00:00
|
|
|
Q_SCRIPTABLE Q_INVOKABLE int loadScript(const QString &filePath, const QString &pluginName = QString());
|
|
|
|
Q_SCRIPTABLE Q_INVOKABLE int loadDeclarativeScript(const QString &filePath, const QString &pluginName = QString());
|
2012-03-01 10:29:24 +00:00
|
|
|
Q_SCRIPTABLE Q_INVOKABLE bool isScriptLoaded(const QString &pluginName) const;
|
|
|
|
Q_SCRIPTABLE Q_INVOKABLE bool unloadScript(const QString &pluginName);
|
2011-12-23 10:52:06 +00:00
|
|
|
|
|
|
|
public Q_SLOTS:
|
|
|
|
void scriptDestroyed(QObject *object);
|
2012-05-06 17:20:38 +00:00
|
|
|
Q_SCRIPTABLE void start();
|
2012-04-28 07:55:11 +00:00
|
|
|
|
|
|
|
private Q_SLOTS:
|
|
|
|
void slotScriptsQueried();
|
|
|
|
|
|
|
|
private:
|
2012-05-05 15:36:30 +00:00
|
|
|
LoadScriptList queryScriptsToLoad(KConfigGroup &conf);
|
2011-01-30 14:34:42 +00:00
|
|
|
};
|
2010-09-21 14:31:40 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
#endif
|