windowsystem.cpp 15.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
 * Copyright 2015 Martin Gräßlin <mgraesslin@kde.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) version 3, or any
 * later version accepted by the membership of KDE e.V. (or its
 * successor approved by the membership of KDE e.V.), which shall
 * act as a proxy defined in Section 6 of version 3 of the license.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "windowsystem.h"
21
#include "waylandintegration.h"
22
23
24
25
26
27
28
#include "logging.h"

#include <KWindowSystem/KWindowSystem>

#include <KWayland/Client/connection_thread.h>
#include <KWayland/Client/plasmawindowmanagement.h>
#include <KWayland/Client/registry.h>
29
30
#include <KWayland/Client/plasmashell.h>
#include <KWayland/Client/surface.h>
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

#include <QPixmap>
#include <QPoint>
#include <QString>

using namespace KWayland::Client;

WindowSystem::WindowSystem()
    : QObject()
    , KWindowSystemPrivate()
{
}

KWayland::Client::PlasmaWindow *WindowSystem::window(WId wid) const
{
46
    if (!WaylandIntegration::self()->plasmaWindowManagement()) {
47
48
        return nullptr;
    }
49
50

    const auto &windows = WaylandIntegration::self()->plasmaWindowManagement()->windows();
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
    auto it = std::find_if(windows.begin(), windows.end(), [wid] (PlasmaWindow *w) { return w->internalId() == wid; } );
    if (it != windows.end()) {
        return *it;
    }
    return nullptr;
}

void WindowSystem::activateWindow(WId win, long int time)
{
    Q_UNUSED(time)
    if (PlasmaWindow *w = window(win)) {
        w->requestActivate();
    }
}

void WindowSystem::forceActiveWindow(WId win, long int time)
{
    Q_UNUSED(time)
    if (PlasmaWindow *w = window(win)) {
        w->requestActivate();
71
72
73
74
75
76
77
78
79
80
81
82
    } else {
        Surface *s = Surface::fromQtWinId(win);
        if (!s) {
            return;
        }
        auto plasmaShellSurface = PlasmaShellSurface::get(s);
        if (!plasmaShellSurface) {
            return;
        }
        if (plasmaShellSurface->role() == PlasmaShellSurface::Role::Panel) {
            plasmaShellSurface->setPanelTakesFocus(true);
        }
83
84
85
86
87
    }
}

WId WindowSystem::activeWindow()
{
88
89
90
    KWayland::Client::PlasmaWindowManagement *wm = WaylandIntegration::self()->plasmaWindowManagement();
    if (wm && wm->activeWindow()) {
        return wm->activeWindow()->internalId();
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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
    }
    return 0;
}

bool WindowSystem::allowedActionsSupported()
{
    return false;
}

void WindowSystem::allowExternalProcessWindowActivation(int pid)
{
    Q_UNUSED(pid)
}

bool WindowSystem::compositingActive()
{
    // wayland is always composited
    return true;
}

void WindowSystem::connectNotify(const QMetaMethod &signal)
{
    Q_UNUSED(signal)
}

QPoint WindowSystem::constrainViewportRelativePosition(const QPoint &pos)
{
    Q_UNUSED(pos)
    qCDebug(KWAYLAND_KWS) << "This plugin does not support viewport positions";
    return QPoint();
}

int WindowSystem::currentDesktop()
{
    qCDebug(KWAYLAND_KWS) << "This plugin does not support virtual desktops";
    return 0;
}

void WindowSystem::demandAttention(WId win, bool set)
{
    Q_UNUSED(win)
    Q_UNUSED(set)
    qCDebug(KWAYLAND_KWS) << "This plugin does not support demanding attention";
}

QString WindowSystem::desktopName(int desktop)
{
    Q_UNUSED(desktop)
    qCDebug(KWAYLAND_KWS) << "This plugin does not support virtual desktops";
    return QString();
}

QPoint WindowSystem::desktopToViewport(int desktop, bool absolute)
{
    Q_UNUSED(desktop)
    Q_UNUSED(absolute)
    qCDebug(KWAYLAND_KWS) << "This plugin does not support viewport positions";
    return QPoint();
}

WId WindowSystem::groupLeader(WId window)
{
    Q_UNUSED(window)
    qCDebug(KWAYLAND_KWS) << "This plugin does not support group leader";
    return 0;
}

bool WindowSystem::icccmCompliantMappingState()
{
    return false;
}

QPixmap WindowSystem::icon(WId win, int width, int height, bool scale, int flags)
{
    Q_UNUSED(flags)
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184

    // Since width can be any arbitrary size, but the icons cannot,
    // take the nearest value for best results (ignoring 22 pixel
    // icons as they don't exist for apps):
    int iconWidth;
    if (width < 24) {
        iconWidth = 16;
    } else if (width < 40) {
        iconWidth = 32;
    } else if (width < 56) {
        iconWidth = 48;
    } else if (width < 96) {
        iconWidth = 64;
    } else if (width < 192) {
        iconWidth = 128;
    } else {
        iconWidth = 256;
    }

185
    if (PlasmaWindow *w = window(win)) {
186
187
188
189
190
        QPixmap pixmap = w->icon().pixmap(iconWidth, iconWidth);
        if (scale) {
            return pixmap.scaled(width, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
        }
        return pixmap;
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
    }
    return QPixmap();
}

void WindowSystem::lowerWindow(WId win)
{
    Q_UNUSED(win)
    qCDebug(KWAYLAND_KWS) << "This plugin does not support lower window";
}

bool WindowSystem::mapViewport()
{
    qCDebug(KWAYLAND_KWS) << "This plugin does not support viewport positions";
    return false;
}

void WindowSystem::minimizeWindow(WId win)
{
209
210
211
212
213
    if (PlasmaWindow *w = window(win)) {
        if (!w->isMinimized()) {
            w->requestToggleMinimized();
        }
    }
214
215
216
217
}

void WindowSystem::unminimizeWindow(WId win)
{
218
219
220
221
222
    if (PlasmaWindow *w = window(win)) {
        if (w->isMinimized()) {
            w->requestToggleMinimized();
        }
    }
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
}

int WindowSystem::numberOfDesktops()
{
    qCDebug(KWAYLAND_KWS) << "This plugin does not support virtual desktops";
    return 1;
}

void WindowSystem::raiseWindow(WId win)
{
    Q_UNUSED(win)
    qCDebug(KWAYLAND_KWS) << "This plugin does not support raising windows";
}

QString WindowSystem::readNameProperty(WId window, long unsigned int atom)
{
    Q_UNUSED(window)
    Q_UNUSED(atom)
    qCDebug(KWAYLAND_KWS) << "This plugin does not support reading X11 properties";
    return QString();
}

void WindowSystem::setBlockingCompositing(WId window, bool active)
{
    Q_UNUSED(window)
    Q_UNUSED(active)
    qCDebug(KWAYLAND_KWS) << "This plugin does not support blocking compositing";
}

void WindowSystem::setCurrentDesktop(int desktop)
{
    Q_UNUSED(desktop)
    qCDebug(KWAYLAND_KWS) << "This plugin does not support virtual desktops";
}

void WindowSystem::setDesktopName(int desktop, const QString &name)
{
    Q_UNUSED(desktop)
    Q_UNUSED(name)
    qCDebug(KWAYLAND_KWS) << "This plugin does not support virtual desktops";
}

void WindowSystem::setExtendedStrut(WId win, int left_width, int left_start, int left_end, int right_width, int right_start, int right_end, int top_width, int top_start, int top_end, int bottom_width, int bottom_start, int bottom_end)
{
    Q_UNUSED(win)
    Q_UNUSED(left_width)
    Q_UNUSED(left_start)
    Q_UNUSED(left_end)
    Q_UNUSED(right_width)
    Q_UNUSED(right_start)
    Q_UNUSED(right_end)
    Q_UNUSED(top_width)
    Q_UNUSED(top_start)
    Q_UNUSED(top_end)
    Q_UNUSED(bottom_width)
    Q_UNUSED(bottom_start)
    Q_UNUSED(bottom_end)
    qCDebug(KWAYLAND_KWS) << "This plugin does not support window struts";
}

void WindowSystem::setStrut(WId win, int left, int right, int top, int bottom)
{
    Q_UNUSED(win)
    Q_UNUSED(left)
    Q_UNUSED(right)
    Q_UNUSED(top)
    Q_UNUSED(bottom)
    qCDebug(KWAYLAND_KWS) << "This plugin does not support window struts";
}

void WindowSystem::setIcons(WId win, const QPixmap &icon, const QPixmap &miniIcon)
{
    Q_UNUSED(win)
    Q_UNUSED(icon)
    Q_UNUSED(miniIcon)
    qCDebug(KWAYLAND_KWS) << "This plugin does not support setting window icons";
}

void WindowSystem::setOnActivities(WId win, const QStringList &activities)
{
    Q_UNUSED(win)
    Q_UNUSED(activities)
    qCDebug(KWAYLAND_KWS) << "This plugin does not support activities";
}

void WindowSystem::setOnAllDesktops(WId win, bool b)
{
    Q_UNUSED(win)
    Q_UNUSED(b)
    qCDebug(KWAYLAND_KWS) << "This plugin does not support setting window on all desktops";
}

void WindowSystem::setOnDesktop(WId win, int desktop)
{
    if (PlasmaWindow *w = window(win)) {
        w->requestVirtualDesktop(desktop - 1);
    }
}

void WindowSystem::setShowingDesktop(bool showing)
{
324
325
    if (!WaylandIntegration::self()->plasmaWindowManagement()) {
        return;
326
    }
327
    WaylandIntegration::self()->plasmaWindowManagement()->setShowingDesktop(showing);
328
329
330
331
}

void WindowSystem::clearState(WId win, NET::States state)
{
Marco Martin's avatar
Marco Martin committed
332
333
334
335
336
    Surface *s = Surface::fromQtWinId(win);
    if (!s) {
        return;
    }

Laurent Montel's avatar
Laurent Montel committed
337
    KWayland::Client::PlasmaShellSurface *plasmaShellSurface = nullptr;
Marco Martin's avatar
Marco Martin committed
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373

    if (state & NET::SkipTaskbar) {
        if (!WaylandIntegration::self()->waylandPlasmaShell()) {
            return;
        }
        plasmaShellSurface = PlasmaShellSurface::get(s);
        if (!plasmaShellSurface) {
            plasmaShellSurface = WaylandIntegration::self()->waylandPlasmaShell()->createSurface(s, this);
        }
        if (!plasmaShellSurface) {
            return;
        }
    }

    if (state & NET::SkipTaskbar) {
        plasmaShellSurface->setSkipTaskbar(false);
    }

    if (state & NET::Max) {
        qCDebug(KWAYLAND_KWS) << "This plugin does not support changing Max window state";
    }
    if (state & NET::FullScreen) {
        qCDebug(KWAYLAND_KWS) << "This plugin does not support changing FullScreen window state";
    }
    if (state & NET::Modal) {
        qCDebug(KWAYLAND_KWS) << "This plugin does not support changing Modal window state";
    }
    if (state & NET::Sticky) {
        qCDebug(KWAYLAND_KWS) << "This plugin does not support changing Sticky window state";
    }
    if (state & NET::Shaded) {
        qCDebug(KWAYLAND_KWS) << "This plugin does not support changing Shaded window state";
    }
    if (state & NET::KeepAbove) {
        qCDebug(KWAYLAND_KWS) << "This plugin does not support changing KeepAbove window state";
    }
Laurent Montel's avatar
Laurent Montel committed
374
    if (state & NET::KeepAbove) {
Marco Martin's avatar
Marco Martin committed
375
376
377
378
379
380
381
382
383
384
385
386
387
388
        qCDebug(KWAYLAND_KWS) << "This plugin does not support changing StaysOnTop window state";
    }
    if (state & NET::SkipPager) {
        qCDebug(KWAYLAND_KWS) << "This plugin does not support changing SkipPager window state";
    }
    if (state & NET::Hidden) {
        qCDebug(KWAYLAND_KWS) << "This plugin does not support changing Hidden window state";
    }
    if (state & NET::KeepBelow) {
        qCDebug(KWAYLAND_KWS) << "This plugin does not support changing KeepBelow window state";
    }
    if (state & NET::DemandsAttention) {
        qCDebug(KWAYLAND_KWS) << "This plugin does not support changing DemandsAttention window state";
    }
389
390
391
392
}

void WindowSystem::setState(WId win, NET::States state)
{
Marco Martin's avatar
Marco Martin committed
393
394
395
396
397
    Surface *s = Surface::fromQtWinId(win);
    if (!s) {
        return;
    }

Laurent Montel's avatar
Laurent Montel committed
398
    KWayland::Client::PlasmaShellSurface *plasmaShellSurface = nullptr;
Marco Martin's avatar
Marco Martin committed
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434

    if (state & NET::SkipTaskbar) {
        if (!WaylandIntegration::self()->waylandPlasmaShell()) {
            return;
        }
        plasmaShellSurface = PlasmaShellSurface::get(s);
        if (!plasmaShellSurface) {
            plasmaShellSurface = WaylandIntegration::self()->waylandPlasmaShell()->createSurface(s, this);
        }
        if (!plasmaShellSurface) {
            return;
        }
    }

    if (state & NET::SkipTaskbar) {
        plasmaShellSurface->setSkipTaskbar(true);
    }

    if (state & NET::Max) {
        qCDebug(KWAYLAND_KWS) << "This plugin does not support changing Max window state";
    }
    if (state & NET::FullScreen) {
        qCDebug(KWAYLAND_KWS) << "This plugin does not support changing FullScreen window state";
    }
    if (state & NET::Modal) {
        qCDebug(KWAYLAND_KWS) << "This plugin does not support changing Modal window state";
    }
    if (state & NET::Sticky) {
        qCDebug(KWAYLAND_KWS) << "This plugin does not support changing Sticky window state";
    }
    if (state & NET::Shaded) {
        qCDebug(KWAYLAND_KWS) << "This plugin does not support changing Shaded window state";
    }
    if (state & NET::KeepAbove) {
        qCDebug(KWAYLAND_KWS) << "This plugin does not support changing KeepAbove window state";
    }
Laurent Montel's avatar
Laurent Montel committed
435
    if (state & NET::KeepAbove) {
Marco Martin's avatar
Marco Martin committed
436
437
438
439
440
441
442
443
444
445
446
447
448
449
        qCDebug(KWAYLAND_KWS) << "This plugin does not support changing StaysOnTop window state";
    }
    if (state & NET::SkipPager) {
        qCDebug(KWAYLAND_KWS) << "This plugin does not support changing SkipPager window state";
    }
    if (state & NET::Hidden) {
        qCDebug(KWAYLAND_KWS) << "This plugin does not support changing Hidden window state";
    }
    if (state & NET::KeepBelow) {
        qCDebug(KWAYLAND_KWS) << "This plugin does not support changing KeepBelow window state";
    }
    if (state & NET::DemandsAttention) {
        qCDebug(KWAYLAND_KWS) << "This plugin does not support changing DemandsAttention window state";
    }
450
451
452
453
}

void WindowSystem::setType(WId win, NET::WindowType windowType)
{
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
    if (!WaylandIntegration::self()->waylandPlasmaShell()) {
        return;
    }
    KWayland::Client::PlasmaShellSurface::Role role;

    switch (windowType) {
    case NET::Normal:
        role = KWayland::Client::PlasmaShellSurface::Role::Normal;
        break;
    case NET::Desktop:
        role = KWayland::Client::PlasmaShellSurface::Role::Desktop;
        break;
    case NET::Dock:
        role = KWayland::Client::PlasmaShellSurface::Role::Panel;
        break;
    case NET::OnScreenDisplay:
        role = KWayland::Client::PlasmaShellSurface::Role::OnScreenDisplay;
        break;
472
473
    case NET::Notification:
        role = KWayland::Client::PlasmaShellSurface::Role::Notification;
Martin Flöser's avatar
Martin Flöser committed
474
        break;
475
476
477
    case NET::Tooltip:
        role = KWayland::Client::PlasmaShellSurface::Role::ToolTip;
        break;
478
479
480
    case NET::CriticalNotification:
        role = KWayland::Client::PlasmaShellSurface::Role::CriticalNotification;
        break;
481
482
483
484
485
486
487
488
489
490
    default:
        return;
    }
    Surface *s = Surface::fromQtWinId(win);
    if (!s) {
        return;
    }
    KWayland::Client::PlasmaShellSurface *shellSurface = WaylandIntegration::self()->waylandPlasmaShell()->createSurface(s, this);

    shellSurface->setRole(role);
491
492
493
494
495
496
497
498
499
500
501
}

void WindowSystem::setUserTime(WId win, long int time)
{
    Q_UNUSED(win)
    Q_UNUSED(time)
    qCDebug(KWAYLAND_KWS) << "This plugin does not support setting user type";
}

bool WindowSystem::showingDesktop()
{
502
503
    if (!WaylandIntegration::self()->plasmaWindowManagement()) {
        return false;
504
    }
505
    return WaylandIntegration::self()->plasmaWindowManagement()->isShowingDesktop();
506
507
508
509
}

QList< WId > WindowSystem::stackingOrder()
{
510
511
512
513
514
515
516
517
    if (!WaylandIntegration::self()->plasmaWindowManagement()) {
        return QList<WId>();
    }

    const auto &windows = WaylandIntegration::self()->plasmaWindowManagement()->windows();
    QList<WId> ret;
    for (auto w : windows) {
        ret << w->internalId();
518
    }
519
    return ret;
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
}

WId WindowSystem::transientFor(WId window)
{
    Q_UNUSED(window)
    qCDebug(KWAYLAND_KWS) << "This plugin does not support transient for windows";
    return 0;
}

int WindowSystem::viewportToDesktop(const QPoint &pos)
{
    Q_UNUSED(pos)
    qCDebug(KWAYLAND_KWS) << "This plugin does not support viewports";
    return 0;
}

int WindowSystem::viewportWindowToDesktop(const QRect &r)
{
    Q_UNUSED(r)
    qCDebug(KWAYLAND_KWS) << "This plugin does not support viewports";
    return 0;
}

QList< WId > WindowSystem::windows()
{
    return stackingOrder();
}

QRect WindowSystem::workArea(const QList< WId > &excludes, int desktop)
{
    Q_UNUSED(excludes)
    Q_UNUSED(desktop)
    qCDebug(KWAYLAND_KWS) << "This plugin does not support work area";
    return QRect();
}

QRect WindowSystem::workArea(int desktop)
{
    Q_UNUSED(desktop)
    qCDebug(KWAYLAND_KWS) << "This plugin does not support work area";
    return QRect();
}