1
/********************************************************************
2
KWin - the KDE window manager
3
This file is part of the KDE project.
5
Copyright (C) 2010 Rohan Prabhu <rohan@rohanprabhu.com>
7
This program is free software; you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 2 of the License, or
10
(at your option) any later version.
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
GNU General Public License for more details.
17
You should have received a copy of the GNU General Public License
18
along with this program. If not, see <http://www.gnu.org/licenses/>.
19
*********************************************************************/
23
Q_DECLARE_METATYPE(SWrapper::Client*)
24
Q_DECLARE_METATYPE(SWrapper::ClientGroup*)
26
using namespace KWin::MetaScripting;
28
// Wrapper for KWin::Client* objects
29
QScriptValue Client::toScriptValue(QScriptEngine* eng, const KClientRef& client)
31
return SWrapper::Client::generate(client, eng);
34
void Client::fromScriptValue(const QScriptValue& obj, KWin::Client*& client)
36
SWrapper::Client* wrapper = qscriptvalue_cast<SWrapper::Client*>(obj);
41
client = wrapper->getCentralObject();
44
// End of metas for KWin::Client* objects
46
// Meta for KWin::ClientGroup* objects
47
QScriptValue ClientGroup::toScriptValue(QScriptEngine* eng, const KClientGroupRef& cGrp)
49
return SWrapper::ClientGroup::generate(eng, new SWrapper::ClientGroup(cGrp));
52
void ClientGroup::fromScriptValue(const QScriptValue& obj, KWin::ClientGroup*& cGrp)
54
SWrapper::ClientGroup* wrapper = qscriptvalue_cast<SWrapper::ClientGroup*>(obj);
59
cGrp = wrapper->getCentralObject();
62
// End of metas for KWin::ClientGroup* objects
64
// Wrapper for KWin::Toplevel* objects [ONE DIRTY HACK]
65
QScriptValue Toplevel::toScriptValue(QScriptEngine* eng, const KToplevelRef& tlevel)
69
return QScriptValue();
72
void Toplevel::fromScriptValue(const QScriptValue& obj, KWin::Toplevel*& tlevel)
74
SWrapper::Client* wrapper = qscriptvalue_cast<SWrapper::Client*>(obj);
78
tlevel = static_cast<KWin::Client*>(wrapper->getCentralObject());
81
// End of wrapper for KWin::Toplevel* object
83
// Meta for QPoint object
84
QScriptValue Point::toScriptValue(QScriptEngine* eng, const QPoint& point)
86
QScriptValue temp = eng->newObject();
87
temp.setProperty("x", point.x());
88
temp.setProperty("y", point.y());
92
void Point::fromScriptValue(const QScriptValue& obj, QPoint& point)
94
QScriptValue x = obj.property("x", QScriptValue::ResolveLocal);
95
QScriptValue y = obj.property("y", QScriptValue::ResolveLocal);
97
if (!x.isUndefined() && !y.isUndefined()) {
98
point.setX(x.toInt32());
99
point.setY(y.toInt32());
102
// End of meta for QPoint object
104
// Meta for QSize object
105
QScriptValue Size::toScriptValue(QScriptEngine* eng, const QSize& size)
107
QScriptValue temp = eng->newObject();
108
temp.setProperty("w", size.width());
109
temp.setProperty("h", size.height());
113
void Size::fromScriptValue(const QScriptValue& obj, QSize& size)
115
QScriptValue w = obj.property("w", QScriptValue::ResolveLocal);
116
QScriptValue h = obj.property("h", QScriptValue::ResolveLocal);
118
if (!w.isUndefined() && !h.isUndefined()) {
119
size.setWidth(w.toInt32());
120
size.setHeight(h.toInt32());
123
// End of meta for QSize object
125
// Meta for QRect object. Just a temporary measure, hope to
126
// add a much better wrapping of the QRect object soon
127
QScriptValue Rect::toScriptValue(QScriptEngine* eng, const QRect& rect)
129
QScriptValue temp = eng->newObject();
130
temp.setProperty("x", rect.x());
131
temp.setProperty("y", rect.y());
132
temp.setProperty("width", rect.width());
133
temp.setProperty("height", rect.height());
138
void Rect::fromScriptValue(const QScriptValue& obj, QRect &rect)
140
QScriptValue w = obj.property("w", QScriptValue::ResolveLocal);
141
QScriptValue h = obj.property("h", QScriptValue::ResolveLocal);
142
QScriptValue x = obj.property("x", QScriptValue::ResolveLocal);
143
QScriptValue y = obj.property("y", QScriptValue::ResolveLocal);
145
if (!w.isUndefined() && !h.isUndefined() && !x.isUndefined() && !y.isUndefined()) {
146
rect.setX(x.toInt32());
147
rect.setY(y.toInt32());
148
rect.setWidth(w.toInt32());
149
rect.setHeight(h.toInt32());
152
// End of meta for QRect object
154
// Other helper functions
155
void KWin::MetaScripting::registration(QScriptEngine* eng)
157
qScriptRegisterMetaType<KClientRef>(eng, Client::toScriptValue, Client::fromScriptValue);
158
qScriptRegisterMetaType<QPoint>(eng, Point::toScriptValue, Point::fromScriptValue);
159
qScriptRegisterMetaType<QSize>(eng, Size::toScriptValue, Size::fromScriptValue);
160
qScriptRegisterMetaType<QRect>(eng, Rect::toScriptValue, Rect::fromScriptValue);
161
qScriptRegisterMetaType<KToplevelRef>(eng, Toplevel::toScriptValue, Toplevel::fromScriptValue);
162
qScriptRegisterMetaType<KClientGroupRef>(eng, ClientGroup::toScriptValue, ClientGroup::fromScriptValue);
164
qScriptRegisterSequenceMetaType<QStringList>(eng);
165
qScriptRegisterSequenceMetaType< QList<KWin::ClientGroup*> >(eng);
166
qScriptRegisterSequenceMetaType<KClientList>(eng);
169
QScriptValue KWin::MetaScripting::configExists(QScriptContext* ctx, QScriptEngine* eng)
171
QHash<QString, QVariant> scriptConfig = (((ctx->thisObject()).data()).toVariant()).toHash();
172
QVariant val = scriptConfig.value((ctx->argument(0)).toString(), QVariant());
174
return eng->toScriptValue<bool>(val.isValid());
177
QScriptValue KWin::MetaScripting::getConfigValue(QScriptContext* ctx, QScriptEngine* eng)
179
int num = ctx->argumentCount();
180
QHash<QString, QVariant> scriptConfig = (((ctx->thisObject()).data()).toVariant()).toHash();
183
* Handle config.get() separately. Compute and return immediately.
186
QHash<QString, QVariant>::const_iterator i;
187
QScriptValue ret = eng->newArray();
189
for (i = scriptConfig.constBegin(); i != scriptConfig.constEnd(); ++i) {
190
ret.setProperty(i.key(), eng->newVariant(i.value()));
196
if ((num == 1) && !((ctx->argument(0)).isArray())) {
197
QVariant val = scriptConfig.value((ctx->argument(0)).toString(), QVariant());
200
return eng->newVariant(val);
202
return QScriptValue();
205
QScriptValue ret = eng->newArray();
208
if ((ctx->argument(0)).isArray()) {
209
bool simple = (num == 1) ? 0 : (ctx->argument(1)).toBool();
210
QScriptValue array = (ctx->argument(0));
211
int len = (array.property("length").isValid()) ? array.property("length").toNumber() : 0;
213
for (int i = 0; i < len; i++) {
214
QVariant val = scriptConfig.value(array.property(i).toString(), QVariant());
218
ret.setProperty(j, eng->newVariant(val));
220
ret.setProperty(array.property(i).toString(), eng->newVariant(val));
227
for (int i = 0; i < num; i++) {
228
QVariant val = scriptConfig.value((ctx->argument(i)).toString(), QVariant());
231
ret.setProperty((ctx->argument(i)).toString(), eng->newVariant(val));
239
return QScriptValue();
246
void KWin::MetaScripting::supplyConfig(QScriptEngine* eng, const QVariant& scriptConfig)
248
QScriptValue configObject = eng->newObject();
249
configObject.setData(eng->newVariant(scriptConfig));
250
configObject.setProperty("get", eng->newFunction(getConfigValue, 0), QScriptValue::Undeletable);
251
configObject.setProperty("exists", eng->newFunction(configExists, 0), QScriptValue::Undeletable);
252
configObject.setProperty("loaded", ((scriptConfig.toHash().empty()) ? eng->newVariant((bool)0) : eng->newVariant((bool)1)), QScriptValue::Undeletable);
253
(eng->globalObject()).setProperty("config", configObject);
256
void KWin::MetaScripting::supplyConfig(QScriptEngine* eng)
258
KWin::MetaScripting::supplyConfig(eng, QVariant(QHash<QString, QVariant>()));
261
void KWin::MetaScripting::valueMerge(QScriptValue& first, QScriptValue second)
263
QScriptValueIterator value_it(second);
265
while (value_it.hasNext()) {
267
first.setProperty(value_it.name(), value_it.value());
271
QScriptValue KWin::MetaScripting::getLazyLogicFunction(QScriptEngine* eng, const QString& type)
273
QScriptValue base = eng->newFunction(KWin::Chelate::lazyLogicGenerate, 0);
274
QScriptValue data = eng->newObject();
275
data.setProperty("lazylogic_type", eng->toScriptValue<QString>(type));