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 "meta.h"
|
2012-01-01 13:31:11 +00:00
|
|
|
#include "client.h"
|
2010-09-21 14:31:40 +00:00
|
|
|
|
2012-01-01 13:31:11 +00:00
|
|
|
#include <QtScript/QScriptEngine>
|
2010-09-21 14:31:40 +00:00
|
|
|
|
|
|
|
using namespace KWin::MetaScripting;
|
|
|
|
|
|
|
|
// Meta for QPoint object
|
|
|
|
QScriptValue Point::toScriptValue(QScriptEngine* eng, const QPoint& point)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2010-09-21 14:31:40 +00:00
|
|
|
QScriptValue temp = eng->newObject();
|
|
|
|
temp.setProperty("x", point.x());
|
|
|
|
temp.setProperty("y", point.y());
|
|
|
|
return temp;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-09-21 14:31:40 +00:00
|
|
|
|
|
|
|
void Point::fromScriptValue(const QScriptValue& obj, QPoint& point)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2010-09-21 14:31:40 +00:00
|
|
|
QScriptValue x = obj.property("x", QScriptValue::ResolveLocal);
|
|
|
|
QScriptValue y = obj.property("y", QScriptValue::ResolveLocal);
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
if (!x.isUndefined() && !y.isUndefined()) {
|
2010-09-21 14:31:40 +00:00
|
|
|
point.setX(x.toInt32());
|
|
|
|
point.setY(y.toInt32());
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-09-21 14:31:40 +00:00
|
|
|
// End of meta for QPoint object
|
|
|
|
|
|
|
|
// Meta for QSize object
|
|
|
|
QScriptValue Size::toScriptValue(QScriptEngine* eng, const QSize& size)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2010-09-21 14:31:40 +00:00
|
|
|
QScriptValue temp = eng->newObject();
|
|
|
|
temp.setProperty("w", size.width());
|
|
|
|
temp.setProperty("h", size.height());
|
|
|
|
return temp;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-09-21 14:31:40 +00:00
|
|
|
|
|
|
|
void Size::fromScriptValue(const QScriptValue& obj, QSize& size)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2010-09-21 14:31:40 +00:00
|
|
|
QScriptValue w = obj.property("w", QScriptValue::ResolveLocal);
|
|
|
|
QScriptValue h = obj.property("h", QScriptValue::ResolveLocal);
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
if (!w.isUndefined() && !h.isUndefined()) {
|
2010-09-21 14:31:40 +00:00
|
|
|
size.setWidth(w.toInt32());
|
|
|
|
size.setHeight(h.toInt32());
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-09-21 14:31:40 +00:00
|
|
|
// End of meta for QSize object
|
|
|
|
|
|
|
|
// Meta for QRect object. Just a temporary measure, hope to
|
|
|
|
// add a much better wrapping of the QRect object soon
|
|
|
|
QScriptValue Rect::toScriptValue(QScriptEngine* eng, const QRect& rect)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2010-09-21 14:31:40 +00:00
|
|
|
QScriptValue temp = eng->newObject();
|
|
|
|
temp.setProperty("x", rect.x());
|
|
|
|
temp.setProperty("y", rect.y());
|
|
|
|
temp.setProperty("width", rect.width());
|
|
|
|
temp.setProperty("height", rect.height());
|
|
|
|
|
|
|
|
return temp;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-09-21 14:31:40 +00:00
|
|
|
|
|
|
|
void Rect::fromScriptValue(const QScriptValue& obj, QRect &rect)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2011-12-22 18:41:18 +00:00
|
|
|
QScriptValue w = obj.property("width", QScriptValue::ResolveLocal);
|
|
|
|
QScriptValue h = obj.property("height", QScriptValue::ResolveLocal);
|
2010-09-21 14:31:40 +00:00
|
|
|
QScriptValue x = obj.property("x", QScriptValue::ResolveLocal);
|
|
|
|
QScriptValue y = obj.property("y", QScriptValue::ResolveLocal);
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
if (!w.isUndefined() && !h.isUndefined() && !x.isUndefined() && !y.isUndefined()) {
|
2010-09-21 14:31:40 +00:00
|
|
|
rect.setX(x.toInt32());
|
|
|
|
rect.setY(y.toInt32());
|
|
|
|
rect.setWidth(w.toInt32());
|
|
|
|
rect.setHeight(h.toInt32());
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-09-21 14:31:40 +00:00
|
|
|
// End of meta for QRect object
|
|
|
|
|
2012-01-01 13:31:11 +00:00
|
|
|
QScriptValue Client::toScriptValue(QScriptEngine *eng, const KClientRef &client)
|
|
|
|
{
|
|
|
|
return eng->newQObject(client, QScriptEngine::QtOwnership,
|
2012-04-24 07:31:00 +00:00
|
|
|
QScriptEngine::ExcludeChildObjects |
|
|
|
|
QScriptEngine::ExcludeDeleteLater |
|
|
|
|
QScriptEngine::PreferExistingWrapperObject |
|
|
|
|
QScriptEngine::AutoCreateDynamicProperties);
|
2012-01-01 13:31:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Client::fromScriptValue(const QScriptValue &value, KWin::Client* &client)
|
|
|
|
{
|
|
|
|
client = qobject_cast<KWin::Client*>(value.toQObject());
|
|
|
|
}
|
|
|
|
|
2013-02-06 14:25:02 +00:00
|
|
|
QScriptValue Toplevel::toScriptValue(QScriptEngine *eng, const KToplevelRef &client)
|
|
|
|
{
|
|
|
|
return eng->newQObject(client, QScriptEngine::QtOwnership,
|
|
|
|
QScriptEngine::ExcludeChildObjects |
|
|
|
|
QScriptEngine::ExcludeDeleteLater |
|
|
|
|
QScriptEngine::PreferExistingWrapperObject |
|
|
|
|
QScriptEngine::AutoCreateDynamicProperties);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Toplevel::fromScriptValue(const QScriptValue &value, KToplevelRef &client)
|
|
|
|
{
|
|
|
|
client = qobject_cast<KWin::Toplevel*>(value.toQObject());
|
|
|
|
}
|
|
|
|
|
2010-09-21 14:31:40 +00:00
|
|
|
// Other helper functions
|
|
|
|
void KWin::MetaScripting::registration(QScriptEngine* eng)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2010-09-21 14:31:40 +00:00
|
|
|
qScriptRegisterMetaType<QPoint>(eng, Point::toScriptValue, Point::fromScriptValue);
|
|
|
|
qScriptRegisterMetaType<QSize>(eng, Size::toScriptValue, Size::fromScriptValue);
|
|
|
|
qScriptRegisterMetaType<QRect>(eng, Rect::toScriptValue, Rect::fromScriptValue);
|
2012-01-01 13:31:11 +00:00
|
|
|
qScriptRegisterMetaType<KClientRef>(eng, Client::toScriptValue, Client::fromScriptValue);
|
2013-02-06 14:25:02 +00:00
|
|
|
qScriptRegisterMetaType<KToplevelRef>(eng, Toplevel::toScriptValue, Toplevel::fromScriptValue);
|
2010-09-21 14:31:40 +00:00
|
|
|
|
|
|
|
qScriptRegisterSequenceMetaType<QStringList>(eng);
|
2012-01-01 14:01:54 +00:00
|
|
|
qScriptRegisterSequenceMetaType< QList<KWin::Client*> >(eng);
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-09-21 14:31:40 +00:00
|
|
|
|
|
|
|
QScriptValue KWin::MetaScripting::configExists(QScriptContext* ctx, QScriptEngine* eng)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2010-09-21 14:31:40 +00:00
|
|
|
QHash<QString, QVariant> scriptConfig = (((ctx->thisObject()).data()).toVariant()).toHash();
|
|
|
|
QVariant val = scriptConfig.value((ctx->argument(0)).toString(), QVariant());
|
|
|
|
|
|
|
|
return eng->toScriptValue<bool>(val.isValid());
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-09-21 14:31:40 +00:00
|
|
|
|
|
|
|
QScriptValue KWin::MetaScripting::getConfigValue(QScriptContext* ctx, QScriptEngine* eng)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2010-09-21 14:31:40 +00:00
|
|
|
int num = ctx->argumentCount();
|
|
|
|
QHash<QString, QVariant> scriptConfig = (((ctx->thisObject()).data()).toVariant()).toHash();
|
|
|
|
|
|
|
|
/*
|
2011-01-01 09:37:08 +00:00
|
|
|
* Handle config.get() separately. Compute and return immediately.
|
2010-09-21 14:31:40 +00:00
|
|
|
**/
|
2011-01-30 14:34:42 +00:00
|
|
|
if (num == 0) {
|
2010-09-21 14:31:40 +00:00
|
|
|
QHash<QString, QVariant>::const_iterator i;
|
|
|
|
QScriptValue ret = eng->newArray();
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
for (i = scriptConfig.constBegin(); i != scriptConfig.constEnd(); ++i) {
|
2010-09-21 14:31:40 +00:00
|
|
|
ret.setProperty(i.key(), eng->newVariant(i.value()));
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-09-21 14:31:40 +00:00
|
|
|
|
|
|
|
return ret;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-09-21 14:31:40 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
if ((num == 1) && !((ctx->argument(0)).isArray())) {
|
2010-09-21 14:31:40 +00:00
|
|
|
QVariant val = scriptConfig.value((ctx->argument(0)).toString(), QVariant());
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
if (val.isValid()) {
|
2010-09-21 14:31:40 +00:00
|
|
|
return eng->newVariant(val);
|
2011-01-30 14:34:42 +00:00
|
|
|
} else {
|
2010-09-21 14:31:40 +00:00
|
|
|
return QScriptValue();
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
} else {
|
2010-09-21 14:31:40 +00:00
|
|
|
QScriptValue ret = eng->newArray();
|
|
|
|
int j = 0;
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
if ((ctx->argument(0)).isArray()) {
|
|
|
|
bool simple = (num == 1) ? 0 : (ctx->argument(1)).toBool();
|
2010-09-21 14:31:40 +00:00
|
|
|
QScriptValue array = (ctx->argument(0));
|
2011-01-30 14:34:42 +00:00
|
|
|
int len = (array.property("length").isValid()) ? array.property("length").toNumber() : 0;
|
2010-09-21 14:31:40 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
for (int i = 0; i < len; i++) {
|
2010-09-21 14:31:40 +00:00
|
|
|
QVariant val = scriptConfig.value(array.property(i).toString(), QVariant());
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
if (val.isValid()) {
|
|
|
|
if (simple) {
|
2010-09-21 14:31:40 +00:00
|
|
|
ret.setProperty(j, eng->newVariant(val));
|
2011-01-30 14:34:42 +00:00
|
|
|
} else {
|
2010-09-21 14:31:40 +00:00
|
|
|
ret.setProperty(array.property(i).toString(), eng->newVariant(val));
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-09-21 14:31:40 +00:00
|
|
|
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
} else {
|
|
|
|
for (int i = 0; i < num; i++) {
|
2010-09-21 14:31:40 +00:00
|
|
|
QVariant val = scriptConfig.value((ctx->argument(i)).toString(), QVariant());
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
if (val.isValid()) {
|
2010-09-21 14:31:40 +00:00
|
|
|
ret.setProperty((ctx->argument(i)).toString(), eng->newVariant(val));
|
|
|
|
j = 1;
|
|
|
|
}
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-09-21 14:31:40 +00:00
|
|
|
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
if (j == 0) {
|
2010-09-21 14:31:40 +00:00
|
|
|
return QScriptValue();
|
2011-01-30 14:34:42 +00:00
|
|
|
} else {
|
2010-09-21 14:31:40 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-09-21 14:31:40 +00:00
|
|
|
|
|
|
|
void KWin::MetaScripting::supplyConfig(QScriptEngine* eng, const QVariant& scriptConfig)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2010-09-21 14:31:40 +00:00
|
|
|
QScriptValue configObject = eng->newObject();
|
|
|
|
configObject.setData(eng->newVariant(scriptConfig));
|
|
|
|
configObject.setProperty("get", eng->newFunction(getConfigValue, 0), QScriptValue::Undeletable);
|
|
|
|
configObject.setProperty("exists", eng->newFunction(configExists, 0), QScriptValue::Undeletable);
|
2011-01-30 14:34:42 +00:00
|
|
|
configObject.setProperty("loaded", ((scriptConfig.toHash().empty()) ? eng->newVariant((bool)0) : eng->newVariant((bool)1)), QScriptValue::Undeletable);
|
2010-09-21 14:31:40 +00:00
|
|
|
(eng->globalObject()).setProperty("config", configObject);
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-09-21 14:31:40 +00:00
|
|
|
|
|
|
|
void KWin::MetaScripting::supplyConfig(QScriptEngine* eng)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2010-09-21 14:31:40 +00:00
|
|
|
KWin::MetaScripting::supplyConfig(eng, QVariant(QHash<QString, QVariant>()));
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-09-21 14:31:40 +00:00
|
|
|
|
|
|
|
void KWin::MetaScripting::valueMerge(QScriptValue& first, QScriptValue second)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2010-09-21 14:31:40 +00:00
|
|
|
QScriptValueIterator value_it(second);
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
while (value_it.hasNext()) {
|
2010-09-21 14:31:40 +00:00
|
|
|
value_it.next();
|
|
|
|
first.setProperty(value_it.name(), value_it.value());
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|