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
|
|
|
|
#include "meta.h"
|
2012-01-22 11:38:03 +00:00
|
|
|
#include "workspace_wrapper.h"
|
2011-12-23 10:52:06 +00:00
|
|
|
// KDE
|
2011-01-01 09:37:08 +00:00
|
|
|
#include <kstandarddirs.h>
|
2012-01-21 09:04:47 +00:00
|
|
|
#include <KDE/KDebug>
|
2011-12-23 10:52:06 +00:00
|
|
|
// Qt
|
|
|
|
#include <QtDBus/QDBusConnection>
|
|
|
|
#include <QtCore/QSettings>
|
|
|
|
#include <QtScript/QScriptEngine>
|
|
|
|
#include <QtScript/QScriptValue>
|
|
|
|
|
|
|
|
QScriptValue kwinScriptPrint(QScriptContext *context, QScriptEngine *engine)
|
|
|
|
{
|
|
|
|
KWin::Script *script = qobject_cast<KWin::Script*>(context->callee().data().toQObject());
|
|
|
|
QString result;
|
|
|
|
for (int i = 0; i < context->argumentCount(); ++i) {
|
|
|
|
if (i > 0) {
|
|
|
|
result.append(" ");
|
|
|
|
}
|
|
|
|
result.append(context->argument(i).toString());
|
|
|
|
}
|
|
|
|
script->printMessage(result);
|
|
|
|
|
|
|
|
return engine->undefinedValue();
|
|
|
|
}
|
2010-09-21 14:31:40 +00:00
|
|
|
|
2011-12-23 07:49:28 +00:00
|
|
|
|
2011-12-23 10:52:06 +00:00
|
|
|
KWin::Script::Script(int scriptId, QString scriptName, QDir dir, QObject *parent)
|
2011-12-23 07:49:28 +00:00
|
|
|
: QObject(parent)
|
2011-12-23 10:52:06 +00:00
|
|
|
, m_scriptId(scriptId)
|
2011-12-23 07:49:28 +00:00
|
|
|
, m_engine(new QScriptEngine(this))
|
|
|
|
, m_scriptDir(dir)
|
|
|
|
, m_configFile(QFileInfo(m_scriptFile).completeBaseName() + QString(".kwscfg"))
|
2012-01-22 11:38:03 +00:00
|
|
|
, m_workspace(new WorkspaceWrapper(m_engine))
|
2011-12-23 10:52:06 +00:00
|
|
|
, m_running(false)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2011-12-23 07:49:28 +00:00
|
|
|
m_scriptFile.setFileName(dir.filePath(scriptName));
|
2012-01-01 08:19:46 +00:00
|
|
|
QDBusConnection::sessionBus().registerObject('/' + QString::number(m_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
|
|
|
{
|
2012-01-01 08:19:46 +00:00
|
|
|
QDBusConnection::sessionBus().unregisterObject('/' + QString::number(m_scriptId));
|
2011-12-23 10:52:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void KWin::Script::printMessage(const QString &message)
|
|
|
|
{
|
|
|
|
kDebug(1212) << m_scriptFile.fileName() << ":" << message;
|
|
|
|
emit print(message);
|
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::run()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2011-12-23 10:52:06 +00:00
|
|
|
if (m_running) {
|
|
|
|
return;
|
|
|
|
}
|
2011-12-23 07:49:28 +00:00
|
|
|
if (m_scriptFile.open(QIODevice::ReadOnly)) {
|
2012-01-21 16:48:07 +00:00
|
|
|
QScriptValue workspace = m_engine->newQObject(m_workspace, QScriptEngine::QtOwnership,
|
|
|
|
QScriptEngine::ExcludeSuperClassContents | QScriptEngine::ExcludeDeleteLater);
|
|
|
|
m_engine->globalObject().setProperty("workspace", workspace, QScriptValue::Undeletable);
|
2011-12-23 07:49:28 +00:00
|
|
|
m_engine->globalObject().setProperty("QTimer", constructTimerClass(m_engine));
|
|
|
|
QObject::connect(m_engine, SIGNAL(signalHandlerException(QScriptValue)), this, SLOT(sigException(QScriptValue)));
|
|
|
|
KWin::MetaScripting::registration(m_engine);
|
|
|
|
|
|
|
|
if (m_scriptDir.exists(m_configFile)) {
|
|
|
|
QSettings scriptSettings(m_scriptDir.filePath(m_configFile), QSettings::IniFormat);
|
2010-09-21 14:31:40 +00:00
|
|
|
QHash<QString, QVariant> scriptConfig;
|
|
|
|
QStringList keys = scriptSettings.allKeys();
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
for (int i = 0; i < keys.size(); i++) {
|
2010-09-21 14:31:40 +00:00
|
|
|
scriptConfig.insert(keys.at(i), scriptSettings.value(keys.at(i)));
|
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::MetaScripting::supplyConfig(m_engine, QVariant(scriptConfig));
|
2011-01-30 14:34:42 +00:00
|
|
|
} else {
|
2011-12-23 07:49:28 +00:00
|
|
|
KWin::MetaScripting::supplyConfig(m_engine);
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2011-12-23 10:52:06 +00:00
|
|
|
// add our print
|
|
|
|
QScriptValue printFunc = m_engine->newFunction(kwinScriptPrint);
|
|
|
|
printFunc.setData(m_engine->newQObject(this));
|
|
|
|
m_engine->globalObject().setProperty("print", printFunc);
|
2010-09-21 14:31:40 +00:00
|
|
|
|
2011-12-23 07:49:28 +00:00
|
|
|
QScriptValue ret = m_engine->evaluate(m_scriptFile.readAll());
|
2010-09-21 14:31:40 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
if (ret.isError()) {
|
2010-09-21 14:31:40 +00:00
|
|
|
sigException(ret);
|
2011-12-23 10:52:06 +00:00
|
|
|
deleteLater();
|
2010-09-21 14:31:40 +00:00
|
|
|
}
|
|
|
|
}
|
2011-12-23 10:52:06 +00:00
|
|
|
m_running = true;
|
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()) {
|
2011-12-23 07:49:28 +00:00
|
|
|
kDebug(1212) << "defaultscript encountered an error at [Line " << m_engine->uncaughtExceptionLineNumber() << "]";
|
2011-01-30 14:34:42 +00:00
|
|
|
kDebug(1212) << "Message: " << ret.toString();
|
|
|
|
kDebug(1212) << "-----------------";
|
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();
|
2011-01-30 14:34:42 +00:00
|
|
|
qDebug() << " " << 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());
|
|
|
|
}
|
|
|
|
|
|
|
|
void KWin::Script::stop()
|
|
|
|
{
|
|
|
|
deleteLater();
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-09-21 14:31:40 +00:00
|
|
|
|
2011-12-23 07:49:28 +00:00
|
|
|
|
2011-12-23 07:56:25 +00:00
|
|
|
KWin::Scripting::Scripting(QObject *parent)
|
|
|
|
: QObject(parent)
|
2011-12-23 07:49:28 +00:00
|
|
|
{
|
2011-12-23 10:52:06 +00:00
|
|
|
QDBusConnection::sessionBus().registerObject("/Scripting", this, QDBusConnection::ExportScriptableContents | QDBusConnection::ExportScriptableInvokables);
|
|
|
|
QDBusConnection::sessionBus().registerService("org.kde.kwin.Scripting");
|
2011-12-23 07:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void KWin::Scripting::start()
|
|
|
|
{
|
|
|
|
QStringList scriptFilters;
|
|
|
|
QString sDirectory = KStandardDirs::locateLocal("data", "kwin/scripts/");
|
|
|
|
|
|
|
|
if (sDirectory.isEmpty()) {
|
|
|
|
// Abort scripting setup. No location found to locate scripts
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
scriptFilters << "*.kwinscript" << "*.kws" << "*.kwinqs";
|
|
|
|
scriptsDir.setPath(sDirectory);
|
|
|
|
scriptList = scriptsDir.entryList(scriptFilters, QDir::Files | QDir::Readable | QDir::Executable);
|
|
|
|
|
|
|
|
for (int i = 0; i < scriptList.size(); i++) {
|
2011-12-23 10:52:06 +00:00
|
|
|
loadScript(scriptsDir.filePath(scriptList.at(i)));
|
2011-12-23 07:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
runScripts();
|
|
|
|
}
|
|
|
|
|
|
|
|
void KWin::Scripting::runScripts()
|
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)
|
|
|
|
{
|
|
|
|
scripts.removeAll(static_cast<KWin::Script*>(object));
|
|
|
|
}
|
|
|
|
|
|
|
|
int KWin::Scripting::loadScript(const QString &filePath)
|
|
|
|
{
|
|
|
|
const int id = scripts.size();
|
|
|
|
KWin::Script *script = new KWin::Script(id, filePath, scriptsDir, this);
|
|
|
|
connect(script, SIGNAL(destroyed(QObject*)), SLOT(scriptDestroyed(QObject*)));
|
|
|
|
scripts.append(script);
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2011-12-23 07:49:28 +00:00
|
|
|
KWin::Scripting::~Scripting()
|
|
|
|
{
|
2011-12-23 10:52:06 +00:00
|
|
|
QDBusConnection::sessionBus().unregisterObject("/Scripting");
|
|
|
|
QDBusConnection::sessionBus().unregisterService("org.kde.kwin.Scripting");
|
2011-12-23 07:49:28 +00:00
|
|
|
}
|
2011-12-23 10:52:06 +00:00
|
|
|
|
|
|
|
#include "scripting.moc"
|