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
SWrapper::Client::Client(KWin::Client* client) :
27
QObject::connect(client, SIGNAL(s_clientMoved()), this, SLOT(sl_clientMoved()));
28
QObject::connect(client, SIGNAL(maximizeSet(QPair<bool, bool>)), this, SLOT(sl_maximizeSet(QPair<bool, bool>)));
29
QObject::connect(client, SIGNAL(s_minimized()), this, SIGNAL(minimized()));
30
QObject::connect(client, SIGNAL(s_activated()), this, SIGNAL(gotFocus()));
31
QObject::connect(client, SIGNAL(s_fullScreenSet(bool, bool)), this, SIGNAL(fullScreenSet(bool, bool)));
32
QObject::connect(client, SIGNAL(s_setKeepAbove(bool)), this, SIGNAL(onSetKeepAbove(bool)));
33
QObject::connect(client, SIGNAL(s_unminimized()), this, SIGNAL(unminimized()));
34
QObject::connect(this, SIGNAL(unminimized()), this, SIGNAL(restored()));
37
centralObject = client;
40
KWin::Client* SWrapper::Client::getCentralObject()
46
SWrapper::ClientResolution SWrapper::Client::clientResolve(const QScriptEngine*, const KWin::Client* client) {
48
return ClientResolution(0, QScriptValue());
55
void SWrapper::Client::setEngine(QScriptEngine* eng)
60
void SWrapper::Client::sl_clientMoved()
65
void SWrapper::Client::sl_maximizeSet(QPair<bool, bool> param)
67
QScriptValue temp = engine->newObject();
68
temp.setProperty("v", engine->toScriptValue(param.first));
69
temp.setProperty("h", engine->toScriptValue(param.second));
71
emit maximizeSet(temp);
75
bool SWrapper::Client::clientRelease(const KWin::Client* client) {
79
if (client->swrapper != 0) {
80
delete client->swrapper;
89
//newWrapper does not search clientMap for existing entries
90
//a search must be performed prior to this call.
91
//However, since now clientMap is actually a QHash, it wouldn't
92
//hurt to perform a check here as well.
93
SWrapper::ClientResolution SWrapper::Client::newWrapper(KWin::Client* client, QScriptEngine* eng)
95
SWrapper::Client* wrapper = new SWrapper::Client(client);
96
wrapper->setEngine(eng);
99
// Use this value to avoid repeated function generations
100
// for function aliases.
105
* Connect all signals and slots from client to
106
* the wrapper object.
114
* Wrap the object, set the data parameter
115
* and obtain a QScriptValue
118
value = eng->newQObject(wrapper,
119
QScriptEngine::AutoOwnership,
120
QScriptEngine::ExcludeSuperClassContents | QScriptEngine::ExcludeDeleteLater
123
wrapper->tl_centralObject = client;
124
tl_append(value, eng);
130
* Add the static functions and other things
131
* that rely on the value of data() and not the
135
value.setProperty("caption", eng->newFunction(caption, 0), QScriptValue::Undeletable);
136
value.setProperty("close", eng->newFunction(close, 0), QScriptValue::Undeletable);
137
value.setProperty("resize", eng->newFunction(resize, 1), QScriptValue::Undeletable);
138
value.setProperty("move", eng->newFunction(move, 1), QScriptValue::Undeletable);
139
value.setProperty("setGeometry", eng->newFunction(setGeometry, 1), QScriptValue::Undeletable);
140
value.setProperty("getWindowInfo", eng->newFunction(getWindowInfo, 0), QScriptValue::Undeletable);
141
value.setProperty("isTransient", eng->newFunction(isTransient, 0), QScriptValue::Undeletable);
142
value.setProperty("transientFor", eng->newFunction(transientFor, 0), QScriptValue::Undeletable);
143
value.setProperty("activate", eng->newFunction(activate, 1), QScriptValue::Undeletable);
144
value.setProperty("setCaption", eng->newFunction(setCaption, 1), QScriptValue::Undeletable);
145
value.setProperty("setFullScreen", eng->newFunction(setFullScreen, 2), QScriptValue::Undeletable);
146
value.setProperty("isFullScreen", eng->newFunction(isFullScreen, 0), QScriptValue::Undeletable);
147
value.setProperty("isFullScreenable", eng->newFunction(isFullScreenable, 0), QScriptValue::Undeletable);
148
value.setProperty("minimize", eng->newFunction(minimize, 0), QScriptValue::Undeletable);
149
value.setProperty("clientGroup", eng->newFunction(clientGroup, 0), QScriptValue::Undeletable);
150
value.setProperty("maximize", eng->newFunction(maximize, 0), QScriptValue::Undeletable);
151
value.setProperty("setMaximize", eng->newFunction(setMaximize, 2), QScriptValue::Undeletable);
152
value.setProperty("desktop", eng->newFunction(desktop, 0), QScriptValue::Undeletable);
153
value.setProperty("setKeepAbove", eng->newFunction(setKeepAbove, 0), QScriptValue::Undeletable);
154
value.setProperty("setKeepBelow", eng->newFunction(setKeepBelow, 0), QScriptValue::Undeletable);
156
BOOLATTACHCLIENT(isShade)
157
BOOLATTACHCLIENT(isShadeable)
158
BOOLATTACHCLIENT(isMinimized)
159
BOOLATTACHCLIENT(isMinimizable)
160
BOOLATTACHCLIENT(isMaximizable)
161
BOOLATTACHCLIENT(isResizable)
162
BOOLATTACHCLIENT(isMovable)
163
BOOLATTACHCLIENT(isMovableAcrossScreens)
164
BOOLATTACHCLIENT(isCloseable)
165
BOOLATTACHCLIENT(keepAbove)
166
BOOLATTACHCLIENT(keepBelow)
168
func = eng->newFunction(unminimize, 0);
169
value.setProperty("unminimize", func, QScriptValue::Undeletable);
170
value.setProperty("restore", func, QScriptValue::Undeletable);
172
ClientResolution final(wrapper, value);
174
//Finally, append the entire thing to the clientMap
175
//clientMap.insert(const_cast<KWin::Client*>(client), ClientResolution(wrapper, value));
178
(client->scriptCache)->insert(eng, final);
182
QScriptValue SWrapper::Client::generate(KWin::Client* client, QScriptEngine* eng)
185
return QScriptValue();
187
ClientResolution res = (client->scriptCache)->value(eng, ClientResolution(0, QScriptValue()));
189
if (res.first != 0) {
190
//The required object has been already created
191
//Return that instead of creating a new one
194
res = newWrapper(client, eng);
200
QScriptValue SWrapper::Client::setMaximize(QScriptContext* ctx, QScriptEngine* eng)
202
KWin::Client* central = eng->fromScriptValue<KWin::Client*>(ctx->thisObject());
205
return eng->toScriptValue<bool>(0);
207
central->setMaximize((ctx->argument(0)).toBool(), (ctx->argument(1)).toBool());
208
return eng->toScriptValue<bool>(1);
212
BOOLEXPORTCLIENT(isShade)
213
BOOLEXPORTCLIENT(isShadeable)
214
BOOLEXPORTCLIENT(isMinimized)
215
BOOLEXPORTCLIENT(isMinimizable)
216
BOOLEXPORTCLIENT(isMaximizable)
217
BOOLEXPORTCLIENT(isResizable)
218
BOOLEXPORTCLIENT(isMovable)
219
BOOLEXPORTCLIENT(isMovableAcrossScreens)
220
BOOLEXPORTCLIENT(isCloseable)
221
BOOLEXPORTCLIENT(keepAbove)
222
BOOLEXPORTCLIENT(keepBelow)
224
QScriptValue SWrapper::Client::setKeepAbove(QScriptContext* ctx, QScriptEngine* eng)
226
KWin::Client* central = eng->fromScriptValue<KWin::Client*>(ctx->thisObject());
227
QScriptValue setValue = ctx->argument(0);
229
if ((central == 0) || (setValue.isUndefined())) {
230
return eng->toScriptValue<bool>(0);
232
central->setKeepAbove(eng->fromScriptValue<bool>(setValue));
233
return eng->toScriptValue<bool>(1);
237
QScriptValue SWrapper::Client::setKeepBelow(QScriptContext* ctx, QScriptEngine* eng)
239
KWin::Client* central = eng->fromScriptValue<KWin::Client*>(ctx->thisObject());
240
QScriptValue setValue = ctx->argument(0);
242
if ((central == 0) || (setValue.isUndefined())) {
243
return eng->toScriptValue<bool>(0);
245
central->setKeepBelow(eng->fromScriptValue<bool>(setValue));
246
return eng->toScriptValue<bool>(1);
250
QScriptValue SWrapper::Client::isNormal(QScriptContext* ctx, QScriptEngine* eng)
252
KWin::Client* central = eng->fromScriptValue<KWin::Client*>(ctx->thisObject());
255
return eng->undefinedValue();
257
return eng->toScriptValue<bool>(!central->isSpecialWindow());
261
QScriptValue SWrapper::Client::maximize(QScriptContext* ctx, QScriptEngine* eng)
263
KWin::Client* central = eng->fromScriptValue<KWin::Client*>(ctx->thisObject());
266
return eng->toScriptValue<bool>(0);
268
central->setMaximize(1, 1);
269
return eng->toScriptValue<bool>(1);
273
QScriptValue SWrapper::Client::desktop(QScriptContext* ctx, QScriptEngine* eng)
275
KWin::Client* central = eng->fromScriptValue<KWin::Client*>(ctx->thisObject());
278
return eng->undefinedValue();
280
return eng->toScriptValue(central->desktop());
284
QScriptValue SWrapper::Client::clientGroup(QScriptContext* ctx, QScriptEngine* eng)
286
KWin::Client* central = eng->fromScriptValue<KWin::Client*>(ctx->thisObject());
289
return QScriptValue();
291
return eng->toScriptValue<KWin::ClientGroup*>(central->clientGroup());
295
QScriptValue SWrapper::Client::caption(QScriptContext* ctx, QScriptEngine* eng)
297
KWin::Client* central = eng->fromScriptValue<KWin::Client*>(ctx->thisObject());
300
return QScriptValue();
302
return eng->toScriptValue(central->caption());
306
QScriptValue SWrapper::Client::setCaption(QScriptContext* ctx, QScriptEngine* eng)
308
KWin::Client* central = eng->fromScriptValue<KWin::Client*>(ctx->thisObject());
310
if (ctx->argument(0).isUndefined()) {
311
return eng->toScriptValue<bool>(0);
313
QString cap = (ctx->argument(0)).toString();
314
central->setCaption(cap);
315
return eng->toScriptValue<bool>(1);
319
QScriptValue SWrapper::Client::resize(QScriptContext* ctx, QScriptEngine* eng)
321
KWin::Client* central = eng->fromScriptValue<KWin::Client*>(ctx->thisObject());
322
QScriptValue arg = ctx->argument(0);
325
QScriptValue emitJsQs;
327
if (central == 0 || arg.isUndefined()) {
328
return eng->toScriptValue<bool>(0);
330
if (arg.isNumber()) {
331
size = QSize(arg.toInt32(), (ctx->argument(1)).toInt32());
332
emitJsQs = ctx->argument(2);
333
} else if (arg.isObject()) {
334
size = eng->fromScriptValue<QSize>(arg);
335
emitJsQs = ctx->argument(1);
338
if (emitJsQs.isUndefined() || !emitJsQs.isValid()) {
341
emitJs = emitJsQs.toBool();
344
if (size.isValid()) {
345
central->plainResize(size, KWin::NormalGeometrySet, emitJs);
346
return eng->toScriptValue<bool>(0);
348
return eng->toScriptValue<bool>(1);
353
QScriptValue SWrapper::Client::move(QScriptContext* ctx, QScriptEngine* eng)
355
KWin::Client* central = eng->fromScriptValue<KWin::Client*>(ctx->thisObject());
356
QScriptValue arg = ctx->argument(0);
358
if (central == 0 || arg.isUndefined()) {
359
return eng->toScriptValue<bool>(0);
363
QScriptValue emitJsQs;
365
if (arg.isNumber()) {
366
point = QPoint(arg.toInt32(), (ctx->argument(1)).toInt32());
367
emitJsQs = ctx->argument(2);
368
} else if (arg.isObject()) {
369
point = eng->fromScriptValue<QPoint>(arg);
370
emitJsQs = ctx->argument(1);
373
if (emitJsQs.isUndefined() || !emitJsQs.isValid()) {
376
emitJs = emitJsQs.toBool();
380
central->setGeometry(QRect(point, central->size()), KWin::NormalGeometrySet, emitJs);
381
return eng->toScriptValue<bool>(1);
385
QScriptValue SWrapper::Client::setGeometry(QScriptContext* ctx, QScriptEngine* eng)
387
KWin::Client* central = eng->fromScriptValue<KWin::Client*>(ctx->thisObject());
388
QScriptValue arg = ctx->argument(0);
391
QScriptValue emitJsQs;
393
if (central == 0 || arg.isUndefined()) {
394
return eng->toScriptValue<bool>(0);
396
if (arg.isNumber()) {
397
rect = QRect(arg.toInt32(), (ctx->argument(1)).toInt32(),
398
(ctx->argument(2)).toInt32(), (ctx->argument(3)).toInt32());
399
emitJsQs = ctx->argument(4);
400
} else if (arg.isObject()) {
401
rect = eng->fromScriptValue<QRect>(arg);
402
emitJsQs = ctx->argument(1);
405
if (emitJsQs.isUndefined() || !emitJsQs.isValid()) {
408
emitJs = emitJsQs.toBool();
411
central->setGeometry(rect, KWin::NormalGeometrySet, emitJs);
412
return eng->toScriptValue<bool>(1);
416
QScriptValue SWrapper::Client::getWindowInfo(QScriptContext* ctx, QScriptEngine* eng)
418
KWin::Client* central = eng->fromScriptValue<KWin::Client*>(ctx->thisObject());
421
return QScriptValue();
423
// For now at least, just get all properties. Tweaking will be taken care
425
KWindowInfo info = KWindowSystem::windowInfo(central->window(), -1U, -1U);
426
return SWrapper::WindowInfo::generate(info, eng, central);
431
QString SWrapper::Client::caption() {
432
return centralObject->caption();
435
QScriptValue SWrapper::Client::unminimize(QScriptContext* ctx, QScriptEngine* eng)
437
KWin::Client* central = eng->fromScriptValue<KWin::Client*>(ctx->thisObject());
440
return eng->toScriptValue<bool>(0);
442
central->unminimize(false);
443
return eng->toScriptValue<bool>(1);
447
QScriptValue SWrapper::Client::minimize(QScriptContext* ctx, QScriptEngine* eng)
449
KWin::Client* central = eng->fromScriptValue<KWin::Client*>(ctx->thisObject());
452
return eng->toScriptValue<bool>(0);
454
central->minimize(false);
455
return eng->toScriptValue<bool>(1);
459
QScriptValue SWrapper::Client::close(QScriptContext* ctx, QScriptEngine* eng)
461
KWin::Client* central = eng->fromScriptValue<KWin::Client*>(ctx->thisObject());
464
return QScriptValue();
466
central->closeWindow();
467
return eng->toScriptValue(true);
471
QScriptValue SWrapper::Client::isTransient(QScriptContext* ctx, QScriptEngine* eng)
473
KWin::Client* central = eng->fromScriptValue<KWin::Client*>(ctx->thisObject());
476
return QScriptValue();
478
bool x = central->isTransient();
479
return eng->toScriptValue(x);
483
QScriptValue SWrapper::Client::transientFor(QScriptContext* ctx, QScriptEngine* eng)
485
KWin::Client* central = eng->fromScriptValue<KWin::Client*>(ctx->thisObject());
488
return QScriptValue();
490
return eng->toScriptValue(central->transientFor());
494
QScriptValue SWrapper::Client::setFullScreen(QScriptContext* ctx, QScriptEngine* eng)
496
KWin::Client* central = eng->fromScriptValue<KWin::Client*>(ctx->thisObject());
497
QScriptValue set = ctx->argument(0);
498
QScriptValue user = ctx->argument(0);
500
if (set.isUndefined() || user.isUndefined()) {
501
return QScriptValue();
505
return eng->toScriptValue<bool>(0);
507
central->setFullScreen(set.toBool(), user.toBool());
508
return eng->toScriptValue<bool>(1);
512
QScriptValue SWrapper::Client::isFullScreen(QScriptContext* ctx, QScriptEngine* eng)
514
KWin::Client* central = eng->fromScriptValue<KWin::Client*>(ctx->thisObject());
517
return QScriptValue();
519
return eng->toScriptValue<bool>(central->isFullScreen());
523
QScriptValue SWrapper::Client::isFullScreenable(QScriptContext* ctx, QScriptEngine* eng)
525
KWin::Client* central = eng->fromScriptValue<KWin::Client*>(ctx->thisObject());
528
return QScriptValue();
530
return eng->toScriptValue<bool>(central->isFullScreenable());
534
QScriptValue SWrapper::Client::activate(QScriptContext* ctx, QScriptEngine* eng)
536
KWin::Client* central = eng->fromScriptValue<KWin::Client*>(ctx->thisObject());
537
QScriptValue force = ctx->argument(0);
541
return eng->toScriptValue<bool>(0);
543
if (!force.isBool()) {
544
_force = force.toBool();
547
(KWin::Workspace::self())->activateClient(central, _force);
548
return eng->toScriptValue<bool>(1);