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>
|
|
|
|
|
|
|
|
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-01-01 09:37:08 +00:00
|
|
|
#include <kstandarddirs.h>
|
2010-09-21 14:31:40 +00:00
|
|
|
|
|
|
|
KWin::Scripting::Scripting()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2010-09-21 14:31:40 +00:00
|
|
|
// Default constructor no longer used, scripting can
|
|
|
|
// be disabled by calling kwin --noscript
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-09-21 14:31:40 +00:00
|
|
|
|
|
|
|
void KWin::Scripting::start()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2010-09-21 14:31:40 +00:00
|
|
|
QStringList scriptFilters;
|
|
|
|
QString sDirectory = KStandardDirs::locateLocal("data", "kwin/scripts/");
|
2011-01-30 14:34:42 +00:00
|
|
|
|
|
|
|
if (sDirectory.isEmpty()) {
|
2010-09-21 14:31:40 +00:00
|
|
|
// Abort scripting setup. No location found to locate scripts
|
|
|
|
return;
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
|
|
|
|
scriptFilters << "*.kwinscript" << "*.kws" << "*.kwinqs";
|
2010-09-21 14:31:40 +00:00
|
|
|
scriptsDir.setPath(sDirectory);
|
2011-01-30 14:34:42 +00:00
|
|
|
scriptList = scriptsDir.entryList(scriptFilters, QDir::Files | QDir::Readable | QDir::Executable);
|
2010-09-21 14:31:40 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
for (int i = 0; i < scriptList.size(); i++) {
|
2010-09-21 14:31:40 +00:00
|
|
|
scripts.append(new KWin::Script(new QScriptEngine(), scriptsDir.filePath(scriptList.at(i)), scriptsDir));
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-09-21 14:31:40 +00:00
|
|
|
|
|
|
|
// Initialize singletons. Currently, only KWin::Workspace.
|
|
|
|
SWrapper::Workspace::initialize(KWin::Workspace::self());
|
|
|
|
|
|
|
|
runScripts();
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-09-21 14:31:40 +00:00
|
|
|
|
|
|
|
void KWin::Scripting::runScript(KWin::Script* script)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
|
|
|
if (script->scriptFile.open(QIODevice::ReadOnly)) {
|
2010-09-21 14:31:40 +00:00
|
|
|
script->workspace = new SWrapper::Workspace(script->engine);
|
|
|
|
(script->workspace)->attach(script->engine);
|
|
|
|
((script->engine)->globalObject()).setProperty("QTimer", constructTimerClass((script->engine)));
|
|
|
|
((script->engine)->globalObject()).setProperty("ClientGroup", SWrapper::ClientGroup::publishClientGroupClass((script->engine)));
|
|
|
|
((script->engine)->globalObject()).setProperty("chelate", KWin::Chelate::publishChelate(script->engine));
|
|
|
|
((script->engine)->globalObject()).setProperty("ch", KWin::Chelate::publishChelate(script->engine));
|
2011-08-17 21:51:55 +00:00
|
|
|
QObject::connect((script->engine), SIGNAL(signalHandlerException(QScriptValue)), this, SLOT(sigException(QScriptValue)));
|
2010-09-21 14:31:40 +00:00
|
|
|
KWin::MetaScripting::registration(script->engine);
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
if (scriptsDir.exists(script->configFile)) {
|
2010-09-21 14:31:40 +00:00
|
|
|
QSettings scriptSettings(scriptsDir.filePath(script->configFile), QSettings::IniFormat);
|
|
|
|
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
|
|
|
|
|
|
|
KWin::MetaScripting::supplyConfig(script->engine, QVariant(scriptConfig));
|
2011-01-30 14:34:42 +00:00
|
|
|
} else {
|
2010-09-21 14:31:40 +00:00
|
|
|
KWin::MetaScripting::supplyConfig(script->engine);
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-09-21 14:31:40 +00:00
|
|
|
|
|
|
|
QScriptValue ret = (script->engine)->evaluate(QString((script->scriptFile).readAll()));
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
if (ret.isError()) {
|
2010-09-21 14:31:40 +00:00
|
|
|
sigException(ret);
|
|
|
|
}
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-09-21 14:31:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
void KWin::Scripting::runScripts()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
|
|
|
for (int i = 0; i < scripts.size(); i++) {
|
2010-09-21 14:31:40 +00:00
|
|
|
runScript(scripts.at(i));
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-09-21 14:31:40 +00:00
|
|
|
|
|
|
|
void KWin::Scripting::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()) {
|
|
|
|
kDebug(1212) << "defaultscript encountered an error at [Line " << uncaughtExceptionLineNumber() << "]";
|
|
|
|
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-01-30 14:34:42 +00:00
|
|
|
}
|
2010-09-21 14:31:40 +00:00
|
|
|
|
|
|
|
KWin::Scripting::~Scripting()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
|
|
|
for (int i = 0; i < scripts.size(); i++) {
|
2010-09-21 14:31:40 +00:00
|
|
|
delete scripts.at(i);
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|