workspace_wrapper.cpp 11.7 KB
Newer Older
Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
1
2
3
/*
    KWin - the KDE window manager
    This file is part of the KDE project.
4

Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
5
6
    SPDX-FileCopyrightText: 2010 Rohan Prabhu <rohan@rohanprabhu.com>
    SPDX-FileCopyrightText: 2011, 2012 Martin Gräßlin <mgraesslin@kde.org>
7

Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
8
9
    SPDX-License-Identifier: GPL-2.0-or-later
*/
10

11
#include "workspace_wrapper.h"
Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
12
#include "../x11client.h"
13
#include "../outline.h"
14
#include "../screens.h"
15
#include "../virtualdesktops.h"
16
#include "../workspace.h"
17
18
19
#ifdef KWIN_BUILD_ACTIVITIES
#include "../activities.h"
#endif
20

21
#include <QDesktopWidget>
22
#include <QApplication>
23

24
25
26
namespace KWin {

WorkspaceWrapper::WorkspaceWrapper(QObject* parent) : QObject(parent)
27
{
28
    KWin::Workspace *ws = KWin::Workspace::self();
29
    KWin::VirtualDesktopManager *vds = KWin::VirtualDesktopManager::self();
30
    connect(ws, &Workspace::desktopPresenceChanged, this, &WorkspaceWrapper::desktopPresenceChanged);
31
    connect(ws, &Workspace::currentDesktopChanged, this, &WorkspaceWrapper::currentDesktopChanged);
32
33
    connect(ws, &Workspace::clientAdded, this, &WorkspaceWrapper::clientAdded);
    connect(ws, &Workspace::clientAdded, this, &WorkspaceWrapper::setupClientConnections);
34
    connect(ws, &Workspace::clientRemoved, this, &WorkspaceWrapper::clientRemoved);
35
    connect(ws, &Workspace::clientActivated, this, &WorkspaceWrapper::clientActivated);
36
37
    connect(vds, &VirtualDesktopManager::countChanged, this, &WorkspaceWrapper::numberDesktopsChanged);
    connect(vds, &VirtualDesktopManager::layoutChanged, this, &WorkspaceWrapper::desktopLayoutChanged);
38
    connect(ws, &Workspace::clientDemandsAttentionChanged, this, &WorkspaceWrapper::clientDemandsAttentionChanged);
39
#ifdef KWIN_BUILD_ACTIVITIES
40
    if (KWin::Activities *activities = KWin::Activities::self()) {
41
42
43
44
45
        connect(activities, &Activities::currentChanged, this, &WorkspaceWrapper::currentActivityChanged);
        connect(activities, &Activities::added, this, &WorkspaceWrapper::activitiesChanged);
        connect(activities, &Activities::added, this, &WorkspaceWrapper::activityAdded);
        connect(activities, &Activities::removed, this, &WorkspaceWrapper::activitiesChanged);
        connect(activities, &Activities::removed, this, &WorkspaceWrapper::activityRemoved);
46
    }
47
#endif
48
49
    connect(screens(), &Screens::sizeChanged, this, &WorkspaceWrapper::virtualScreenSizeChanged);
    connect(screens(), &Screens::geometryChanged, this, &WorkspaceWrapper::virtualScreenGeometryChanged);
50
51
52
53
54
55
    connect(screens(), &Screens::countChanged, this,
        [this] (int previousCount, int currentCount) {
            Q_UNUSED(previousCount)
            emit numberScreensChanged(currentCount);
        }
    );
56
    connect(QApplication::desktop(), &QDesktopWidget::resized, this, &WorkspaceWrapper::screenResized);
57
58
59

    const QList<AbstractClient *> clients = ws->allClientList();
    for (AbstractClient *client : clients) {
60
        setupClientConnections(client);
61
    }
62
}
63

64
65
66
67
68
69
70
71
int WorkspaceWrapper::currentDesktop() const
{
    return VirtualDesktopManager::self()->current();
}

int WorkspaceWrapper::numberOfDesktops() const
{
    return VirtualDesktopManager::self()->count();
72
73
}

74
75
76
77
void WorkspaceWrapper::setCurrentDesktop(int desktop)
{
    VirtualDesktopManager::self()->setCurrent(desktop);
}
78

79
80
81
82
void WorkspaceWrapper::setNumberOfDesktops(int count)
{
    VirtualDesktopManager::self()->setCount(count);
}
83

84
85
86
AbstractClient *WorkspaceWrapper::activeClient() const
{
    return workspace()->activeClient();
87
}
88

89
90
91
QString WorkspaceWrapper::currentActivity() const
{
#ifdef KWIN_BUILD_ACTIVITIES
92
93
94
    if (!Activities::self()) {
        return QString();
    }
95
96
97
98
99
100
    return Activities::self()->current();
#else
    return QString();
#endif
}

101
102
103
104
105
106
107
108
109
110
111
void WorkspaceWrapper::setCurrentActivity(QString activity)
{
#ifdef KWIN_BUILD_ACTIVITIES
    if (Activities::self()) {
        Activities::self()->setCurrent(activity);
    }
#else
    Q_UNUSED(activity)
#endif
}

112
113
114
QStringList WorkspaceWrapper::activityList() const
{
#ifdef KWIN_BUILD_ACTIVITIES
115
116
117
    if (!Activities::self()) {
        return QStringList();
    }
118
119
120
121
122
123
    return Activities::self()->all();
#else
    return QStringList();
#endif
}

Laurent Montel's avatar
Laurent Montel committed
124
#define SLOTWRAPPER(name) \
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
void WorkspaceWrapper::name( ) { \
    Workspace::self()->name(); \
}

SLOTWRAPPER(slotSwitchToNextScreen)
SLOTWRAPPER(slotWindowToNextScreen)
SLOTWRAPPER(slotToggleShowDesktop)

SLOTWRAPPER(slotWindowMaximize)
SLOTWRAPPER(slotWindowMaximizeVertical)
SLOTWRAPPER(slotWindowMaximizeHorizontal)
SLOTWRAPPER(slotWindowMinimize)
SLOTWRAPPER(slotWindowShade)
SLOTWRAPPER(slotWindowRaise)
SLOTWRAPPER(slotWindowLower)
SLOTWRAPPER(slotWindowRaiseOrLower)
SLOTWRAPPER(slotActivateAttentionWindow)
SLOTWRAPPER(slotWindowPackLeft)
SLOTWRAPPER(slotWindowPackRight)
SLOTWRAPPER(slotWindowPackUp)
SLOTWRAPPER(slotWindowPackDown)
SLOTWRAPPER(slotWindowGrowHorizontal)
SLOTWRAPPER(slotWindowGrowVertical)
SLOTWRAPPER(slotWindowShrinkHorizontal)
SLOTWRAPPER(slotWindowShrinkVertical)

SLOTWRAPPER(slotIncreaseWindowOpacity)
SLOTWRAPPER(slotLowerWindowOpacity)

SLOTWRAPPER(slotWindowOperations)
SLOTWRAPPER(slotWindowClose)
SLOTWRAPPER(slotWindowMove)
SLOTWRAPPER(slotWindowResize)
SLOTWRAPPER(slotWindowAbove)
SLOTWRAPPER(slotWindowBelow)
SLOTWRAPPER(slotWindowOnAllDesktops)
SLOTWRAPPER(slotWindowFullScreen)
SLOTWRAPPER(slotWindowNoBorder)

SLOTWRAPPER(slotWindowToNextDesktop)
SLOTWRAPPER(slotWindowToPreviousDesktop)
SLOTWRAPPER(slotWindowToDesktopRight)
SLOTWRAPPER(slotWindowToDesktopLeft)
SLOTWRAPPER(slotWindowToDesktopUp)
SLOTWRAPPER(slotWindowToDesktopDown)

#undef SLOTWRAPPER

173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#define SLOTWRAPPER(name,modes) \
void WorkspaceWrapper::name() { \
    Workspace::self()->quickTileWindow(modes); \
}

SLOTWRAPPER(slotWindowQuickTileLeft, QuickTileFlag::Left)
SLOTWRAPPER(slotWindowQuickTileRight, QuickTileFlag::Right)
SLOTWRAPPER(slotWindowQuickTileTop, QuickTileFlag::Top)
SLOTWRAPPER(slotWindowQuickTileBottom, QuickTileFlag::Bottom)
SLOTWRAPPER(slotWindowQuickTileTopLeft, QuickTileFlag::Top | QuickTileFlag::Left)
SLOTWRAPPER(slotWindowQuickTileTopRight, QuickTileFlag::Top | QuickTileFlag::Right)
SLOTWRAPPER(slotWindowQuickTileBottomLeft, QuickTileFlag::Bottom | QuickTileFlag::Left)
SLOTWRAPPER(slotWindowQuickTileBottomRight, QuickTileFlag::Bottom | QuickTileFlag::Right)

#undef SLOTWRAPPER

189
190
191
192
193
194
195
196
197
198
199
200
#define SLOTWRAPPER(name,direction) \
void WorkspaceWrapper::name() { \
    Workspace::self()->switchWindow(Workspace::direction); \
}

SLOTWRAPPER(slotSwitchWindowUp, DirectionNorth)
SLOTWRAPPER(slotSwitchWindowDown, DirectionSouth)
SLOTWRAPPER(slotSwitchWindowRight, DirectionEast)
SLOTWRAPPER(slotSwitchWindowLeft, DirectionWest)

#undef SLOTWRAPPER

Laurent Montel's avatar
Laurent Montel committed
201
#define SLOTWRAPPER(name,direction) \
202
203
204
205
void WorkspaceWrapper::name( ) { \
    VirtualDesktopManager::self()->moveTo<direction>(options->isRollOverDesktops()); \
}

Laurent Montel's avatar
Laurent Montel committed
206
207
208
209
210
211
SLOTWRAPPER(slotSwitchDesktopNext,DesktopNext)
SLOTWRAPPER(slotSwitchDesktopPrevious,DesktopPrevious)
SLOTWRAPPER(slotSwitchDesktopRight,DesktopRight)
SLOTWRAPPER(slotSwitchDesktopLeft,DesktopLeft)
SLOTWRAPPER(slotSwitchDesktopUp,DesktopAbove)
SLOTWRAPPER(slotSwitchDesktopDown,DesktopBelow)
212
213
214

#undef SLOTWRAPPER

215
void WorkspaceWrapper::setActiveClient(KWin::AbstractClient* client)
216
{
217
    KWin::Workspace::self()->activateClient(client);
218
}
219

220
QSize WorkspaceWrapper::workspaceSize() const
221
{
222
    return QSize(workspaceWidth(), workspaceHeight());
223
}
224

225
226
QSize WorkspaceWrapper::displaySize() const
{
227
    return screens()->displaySize();
228
229
230
231
}

int WorkspaceWrapper::displayWidth() const
{
232
    return displaySize().width();
233
234
235
236
}

int WorkspaceWrapper::displayHeight() const
{
237
    return displaySize().height();
238
239
}

240
241
242
243
244
QRect WorkspaceWrapper::clientArea(ClientAreaOption option, const QPoint &p, int desktop) const
{
    return Workspace::self()->clientArea(static_cast<clientAreaOption>(option), p, desktop);
}

245
QRect WorkspaceWrapper::clientArea(ClientAreaOption option, const KWin::AbstractClient *c) const
246
247
248
249
250
251
252
253
254
{
    return Workspace::self()->clientArea(static_cast<clientAreaOption>(option), c);
}

QRect WorkspaceWrapper::clientArea(ClientAreaOption option, int screen, int desktop) const
{
    return Workspace::self()->clientArea(static_cast<clientAreaOption>(option), screen, desktop);
}

255
256
QString WorkspaceWrapper::desktopName(int desktop) const
{
257
    return VirtualDesktopManager::self()->name(desktop);
258
259
}

260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
void WorkspaceWrapper::createDesktop(int position, const QString &name) const
{
    VirtualDesktopManager::self()->createVirtualDesktop(position, name);
}

void WorkspaceWrapper::removeDesktop(int position) const
{
    VirtualDesktop *vd = VirtualDesktopManager::self()->desktopForX11Id(position + 1);
    if (!vd) {
        return;
    }

    VirtualDesktopManager::self()->removeVirtualDesktop(vd->id());
}

275
276
277
278
279
QString WorkspaceWrapper::supportInformation() const
{
    return Workspace::self()->supportInformation();
}

280
void WorkspaceWrapper::setupClientConnections(AbstractClient *client)
281
{
282
283
284
    connect(client, &AbstractClient::clientMinimized, this, &WorkspaceWrapper::clientMinimized);
    connect(client, &AbstractClient::clientUnminimized, this, &WorkspaceWrapper::clientUnminimized);
    connect(client, qOverload<AbstractClient *, bool, bool>(&AbstractClient::clientMaximizedStateChanged),
285
            this, &WorkspaceWrapper::clientMaximizeSet);
286

287
288
289
    X11Client *x11Client = qobject_cast<X11Client *>(client); // TODO: Drop X11-specific signals.
    if (!x11Client)
        return;
290

291
292
    connect(x11Client, &X11Client::clientManaging, this, &WorkspaceWrapper::clientManaging);
    connect(x11Client, &X11Client::clientFullScreenSet, this, &WorkspaceWrapper::clientFullScreenSet);
293
294
}

295
296
void WorkspaceWrapper::showOutline(const QRect &geometry)
{
Martin Flöser's avatar
Martin Flöser committed
297
    outline()->show(geometry);
298
299
300
301
}

void WorkspaceWrapper::showOutline(int x, int y, int width, int height)
{
Martin Flöser's avatar
Martin Flöser committed
302
    outline()->show(QRect(x, y, width, height));
303
304
305
306
}

void WorkspaceWrapper::hideOutline()
{
Martin Flöser's avatar
Martin Flöser committed
307
    outline()->hide();
308
309
}

Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
310
X11Client *WorkspaceWrapper::getClient(qulonglong windowId)
311
{
312
    return Workspace::self()->findClient(Predicate::WindowMatch, windowId);
313
314
}

315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
QSize WorkspaceWrapper::desktopGridSize() const
{
    return VirtualDesktopManager::self()->grid().size();
}

int WorkspaceWrapper::desktopGridWidth() const
{
    return desktopGridSize().width();
}

int WorkspaceWrapper::desktopGridHeight() const
{
    return desktopGridSize().height();
}

int WorkspaceWrapper::workspaceHeight() const
{
    return desktopGridHeight() * displayHeight();
}

int WorkspaceWrapper::workspaceWidth() const
{
    return desktopGridWidth() * displayWidth();
}

340
341
342
343
344
345
346
347
348
349
int WorkspaceWrapper::numScreens() const
{
    return screens()->count();
}

int WorkspaceWrapper::activeScreen() const
{
    return screens()->current();
}

350
351
352
353
354
355
356
357
358
359
QRect WorkspaceWrapper::virtualScreenGeometry() const
{
    return screens()->geometry();
}

QSize WorkspaceWrapper::virtualScreenSize() const
{
    return screens()->size();
}

360
361
362
363
364
365
366
367
void WorkspaceWrapper::sendClientToScreen(AbstractClient *client, int screen)
{
    if (screen < 0 || screen >= screens()->count()) {
        return;
    }
    workspace()->sendClientToScreen(client, screen);
}

368
369
370
QtScriptWorkspaceWrapper::QtScriptWorkspaceWrapper(QObject* parent)
    : WorkspaceWrapper(parent) {}

371
372
373
374
QList<KWin::AbstractClient *> QtScriptWorkspaceWrapper::clientList() const
{
    return workspace()->allClientList();
}
375

376
QQmlListProperty<KWin::AbstractClient> DeclarativeScriptWorkspaceWrapper::clients()
377
{
Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
378
    return QQmlListProperty<KWin::AbstractClient>(this, nullptr, &DeclarativeScriptWorkspaceWrapper::countClientList, &DeclarativeScriptWorkspaceWrapper::atClientList);
379
380
}

381
int DeclarativeScriptWorkspaceWrapper::countClientList(QQmlListProperty<KWin::AbstractClient> *clients)
382
{
383
    Q_UNUSED(clients)
384
    return workspace()->allClientList().size();
385
386
}

387
KWin::AbstractClient *DeclarativeScriptWorkspaceWrapper::atClientList(QQmlListProperty<KWin::AbstractClient> *clients, int index)
388
{
389
    Q_UNUSED(clients)
390
    return workspace()->allClientList().at(index);
391
392
393
394
395
}

DeclarativeScriptWorkspaceWrapper::DeclarativeScriptWorkspaceWrapper(QObject* parent)
    : WorkspaceWrapper(parent) {}

396
} // KWin