xdgshellclient.cpp 64.4 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
7
    SPDX-FileCopyrightText: 2015 Martin Gräßlin <mgraesslin@kde.org>
    SPDX-FileCopyrightText: 2018 David Edmundson <davidedmundson@kde.org>
    SPDX-FileCopyrightText: 2019 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
8

Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
9
10
    SPDX-License-Identifier: GPL-2.0-or-later
*/
11
#include "xdgshellclient.h"
12
#include "deleted.h"
13
#include "screenedge.h"
14
#include "screens.h"
15
16
17
18
#include "subsurfacemonitor.h"
#include "wayland_server.h"
#include "workspace.h"

19
#include <KDecoration2/DecoratedClient>
20
#include <KDecoration2/Decoration>
21
22
#include <KWaylandServer/appmenu_interface.h>
#include <KWaylandServer/buffer_interface.h>
23
#include <KWaylandServer/output_interface.h>
24
25
26
27
28
#include <KWaylandServer/plasmashell_interface.h>
#include <KWaylandServer/seat_interface.h>
#include <KWaylandServer/server_decoration_interface.h>
#include <KWaylandServer/server_decoration_palette_interface.h>
#include <KWaylandServer/surface_interface.h>
29
#include <KWaylandServer/xdgdecoration_v1_interface.h>
30

31
using namespace KWaylandServer;
32
33
34
35

namespace KWin
{

36
37
38
39
XdgSurfaceClient::XdgSurfaceClient(XdgSurfaceInterface *shellSurface)
    : WaylandClient(shellSurface->surface())
    , m_shellSurface(shellSurface)
    , m_configureTimer(new QTimer(this))
40
{
41
42
    setSizeSyncMode(SyncMode::Async);
    setPositionSyncMode(SyncMode::Async);
43

44
45
    connect(shellSurface, &XdgSurfaceInterface::configureAcknowledged,
            this, &XdgSurfaceClient::handleConfigureAcknowledged);
46
47
    connect(shellSurface, &XdgSurfaceInterface::resetOccurred,
            this, &XdgSurfaceClient::destroyClient);
48
49
    connect(shellSurface->surface(), &SurfaceInterface::committed,
            this, &XdgSurfaceClient::handleCommit);
50
51
52
53
#if 0 // TODO: Refactor kwin core in order to uncomment this code.
    connect(shellSurface->surface(), &SurfaceInterface::mapped,
            this, &XdgSurfaceClient::setReadyForPainting);
#endif
54
    connect(shellSurface->surface(), &SurfaceInterface::aboutToBeDestroyed,
55
            this, &XdgSurfaceClient::destroyClient);
56

57
58
59
60
61
62
63
    // The effective window geometry is determined by two things: (a) the rectangle that bounds
    // the main surface and all of its sub-surfaces, (b) the client-specified window geometry, if
    // any. If the client hasn't provided the window geometry, we fallback to the bounding sub-
    // surface rectangle. If the client has provided the window geometry, we intersect it with
    // the bounding rectangle and that will be the effective window geometry. It's worth to point
    // out that geometry updates do not occur that frequently, so we don't need to recompute the
    // bounding geometry every time the client commits the surface.
64

65
    SubSurfaceMonitor *treeMonitor = new SubSurfaceMonitor(surface(), this);
66

67
68
69
70
71
72
73
74
75
76
77
78
    connect(treeMonitor, &SubSurfaceMonitor::subSurfaceAdded,
            this, &XdgSurfaceClient::setHaveNextWindowGeometry);
    connect(treeMonitor, &SubSurfaceMonitor::subSurfaceRemoved,
            this, &XdgSurfaceClient::setHaveNextWindowGeometry);
    connect(treeMonitor, &SubSurfaceMonitor::subSurfaceMoved,
            this, &XdgSurfaceClient::setHaveNextWindowGeometry);
    connect(treeMonitor, &SubSurfaceMonitor::subSurfaceResized,
            this, &XdgSurfaceClient::setHaveNextWindowGeometry);
    connect(shellSurface, &XdgSurfaceInterface::windowGeometryChanged,
            this, &XdgSurfaceClient::setHaveNextWindowGeometry);
    connect(surface(), &SurfaceInterface::sizeChanged,
            this, &XdgSurfaceClient::setHaveNextWindowGeometry);
79

80
81
82
    // Configure events are not sent immediately, but rather scheduled to be sent when the event
    // loop is about to be idle. By doing this, we can avoid sending configure events that do
    // nothing, and implementation-wise, it's simpler.
83

84
85
    m_configureTimer->setSingleShot(true);
    connect(m_configureTimer, &QTimer::timeout, this, &XdgSurfaceClient::sendConfigure);
86

87
88
89
    // Unfortunately, AbstractClient::checkWorkspacePosition() operates on the geometry restore
    // so we need to initialize it with some reasonable value; otherwise bad things will happen
    // when we want to decorate the client or move the client to another screen. This is a hack.
90

91
92
    connect(this, &XdgSurfaceClient::frameGeometryChanged,
            this, &XdgSurfaceClient::updateGeometryRestoreHack);
93
94
}

95
XdgSurfaceClient::~XdgSurfaceClient()
96
{
97
    qDeleteAll(m_configureEvents);
98
99
}

100
QRect XdgSurfaceClient::inputGeometry() const
101
{
102
    return isDecorated() ? AbstractClient::inputGeometry() : bufferGeometry();
103
104
}

105
QMatrix4x4 XdgSurfaceClient::inputTransformation() const
106
{
107
    QMatrix4x4 transformation;
108
    transformation.translate(-bufferGeometry().x(), -bufferGeometry().y());
109
    return transformation;
110
111
}

112
XdgSurfaceConfigure *XdgSurfaceClient::lastAcknowledgedConfigure() const
113
{
114
    return m_lastAcknowledgedConfigure.data();
115
116
}

117
bool XdgSurfaceClient::stateCompare() const
118
{
119
    if (requestedFrameGeometry() != frameGeometry()) {
120
        return true;
121
    }
122
    if (requestedClientGeometry() != clientGeometry()) {
123
124
        return true;
    }
125
    if (requestedClientGeometry().isEmpty()) {
126
127
128
        return true;
    }
    return false;
129
130
}

131
void XdgSurfaceClient::scheduleConfigure(ConfigureFlags flags)
132
{
133
    if (isZombie()) {
134
135
        return;
    }
136

137
138
139
    m_configureFlags |= flags;

    if ((m_configureFlags & ConfigureRequired) || stateCompare()) {
140
141
142
143
        m_configureTimer->start();
    } else {
        m_configureTimer->stop();
    }
144
145
}

146
void XdgSurfaceClient::sendConfigure()
147
{
148
    XdgSurfaceConfigure *configureEvent = sendRoleConfigure();
149

150
151
    if (configureEvent->position != pos()) {
        configureEvent->presentFields |= XdgSurfaceConfigure::PositionField;
152
    }
153
154
    if (configureEvent->size != size()) {
        configureEvent->presentFields |= XdgSurfaceConfigure::SizeField;
155
    }
156
157

    m_configureEvents.append(configureEvent);
158
    m_configureFlags = ConfigureFlags();
159
160
}

161
void XdgSurfaceClient::handleConfigureAcknowledged(quint32 serial)
162
{
163
164
165
    while (!m_configureEvents.isEmpty()) {
        if (serial < m_configureEvents.first()->serial) {
            break;
166
        }
167
        m_lastAcknowledgedConfigure.reset(m_configureEvents.takeFirst());
168
    }
169
170
}

171
void XdgSurfaceClient::handleCommit()
172
{
173
    if (!surface()->buffer()) {
174
175
        return;
    }
176

177
178
179
    if (haveNextWindowGeometry()) {
        handleNextWindowGeometry();
        resetHaveNextWindowGeometry();
180
    }
181

182
183
    handleRoleCommit();
    m_lastAcknowledgedConfigure.reset();
184

185
    setReadyForPainting();
186
    updateDepth();
187
188
}

189
void XdgSurfaceClient::handleRoleCommit()
190
191
192
{
}

193
void XdgSurfaceClient::handleNextWindowGeometry()
194
{
195
    const QRect boundingGeometry = surface()->boundingRect();
196

197
198
199
200
201
    // The effective window geometry is defined as the intersection of the window geometry
    // and the rectangle that bounds the main surface and all of its sub-surfaces. If the
    // client hasn't specified the window geometry, we must fallback to the bounding geometry.
    // Note that the xdg-shell spec is not clear about when exactly we have to clamp the
    // window geometry.
202

203
204
205
206
207
    m_windowGeometry = m_shellSurface->windowGeometry();
    if (m_windowGeometry.isValid()) {
        m_windowGeometry &= boundingGeometry;
    } else {
        m_windowGeometry = boundingGeometry;
208
    }
209

210
211
    if (m_windowGeometry.isEmpty()) {
        qCWarning(KWIN_CORE) << "Committed empty window geometry, dealing with a buggy client!";
212
    }
213

214
    QRect frameGeometry(pos(), clientSizeToFrameSize(m_windowGeometry.size()));
215

216
217
218
219
220
221
222
223
224
225
226
227
228
    // We're not done yet. The xdg-shell spec allows clients to attach buffers smaller than
    // we asked. Normally, this is not a big deal, but when the client is being interactively
    // resized, it may cause the window contents to bounce. In order to counter this, we have
    // to "gravitate" the new geometry according to the current move-resize pointer mode so
    // the opposite window corner stays still.

    if (isMoveResize()) {
        frameGeometry = adjustMoveResizeGeometry(frameGeometry);
    } else if (lastAcknowledgedConfigure()) {
        XdgSurfaceConfigure *configureEvent = lastAcknowledgedConfigure();

        if (configureEvent->presentFields & XdgSurfaceConfigure::PositionField) {
            frameGeometry.moveTopLeft(configureEvent->position);
229
        }
230
    }
231

232
    updateGeometry(frameGeometry);
233
234
235
236

    if (isResize()) {
        performMoveResize();
    }
237
238
}

239
bool XdgSurfaceClient::haveNextWindowGeometry() const
240
{
241
    return m_haveNextWindowGeometry || m_lastAcknowledgedConfigure;
242
243
}

244
void XdgSurfaceClient::setHaveNextWindowGeometry()
245
{
246
    m_haveNextWindowGeometry = true;
247
248
}

249
void XdgSurfaceClient::resetHaveNextWindowGeometry()
250
{
251
    m_haveNextWindowGeometry = false;
252
253
}

254
QRect XdgSurfaceClient::adjustMoveResizeGeometry(const QRect &rect) const
255
{
256
    QRect geometry = rect;
257

258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
    switch (moveResizePointerMode()) {
    case PositionTopLeft:
        geometry.moveRight(moveResizeGeometry().right());
        geometry.moveBottom(moveResizeGeometry().bottom());
        break;
    case PositionTop:
    case PositionTopRight:
        geometry.moveLeft(moveResizeGeometry().left());
        geometry.moveBottom(moveResizeGeometry().bottom());
        break;
    case PositionRight:
    case PositionBottomRight:
    case PositionBottom:
    case PositionCenter:
        geometry.moveLeft(moveResizeGeometry().left());
        geometry.moveTop(moveResizeGeometry().top());
        break;
    case PositionBottomLeft:
    case PositionLeft:
        geometry.moveRight(moveResizeGeometry().right());
        geometry.moveTop(moveResizeGeometry().top());
        break;
    }
281

282
    return geometry;
283
284
}

285
286
287
288
289
bool XdgSurfaceClient::isInitialPositionSet() const
{
    return m_plasmaShellSurface ? m_plasmaShellSurface->isPositionSet() : false;
}

290
void XdgSurfaceClient::requestGeometry(const QRect &rect)
291
{
292
    WaylandClient::requestGeometry(rect);
293
294

    scheduleConfigure(); // Send the configure event later.
295
296
}

297
298
299
300
301
302
303
304
305
306
307
/**
 * \internal
 * \todo We have to check the current frame geometry in checkWorskpacePosition().
 *
 * Sets the geometry restore to the first valid frame geometry. This is a HACK!
 *
 * Unfortunately, AbstractClient::checkWorkspacePosition() operates on the geometry restore
 * rather than the current frame geometry, so we have to ensure that it's initialized with
 * some reasonable value even if the client is not maximized or quick tiled.
 */
void XdgSurfaceClient::updateGeometryRestoreHack()
308
{
309
    if (geometryRestore().isEmpty() && !frameGeometry().isEmpty()) {
310
        setGeometryRestore(frameGeometry());
311
    }
312
313
}

314
QRect XdgSurfaceClient::frameRectToBufferRect(const QRect &rect) const
315
{
316
317
318
    const int left = rect.left() + borderLeft() - m_windowGeometry.left();
    const int top = rect.top() + borderTop() - m_windowGeometry.top();
    return QRect(QPoint(left, top), surface()->size());
319
320
}

321
void XdgSurfaceClient::addDamage(const QRegion &damage)
322
{
323
324
    const int offsetX = bufferGeometry().x() - frameGeometry().x();
    const int offsetY = bufferGeometry().y() - frameGeometry().y();
325
    addRepaint(damage.translated(offsetX, offsetY));
326
    Toplevel::addDamage(damage);
327
328
}

329
330
void XdgSurfaceClient::destroyClient()
{
331
    markAsZombie();
332
333
334
335
336
337
338
    m_configureTimer->stop();
    if (isMoveResize()) {
        leaveMoveResize();
    }
    cleanTabBox();
    Deleted *deleted = Deleted::create(this);
    emit windowClosed(this, deleted);
339
    StackingUpdatesBlocker blocker(workspace());
340
341
342
343
344
345
346
347
    RuleBook::self()->discardUsed(this, true);
    destroyWindowManagementInterface();
    destroyDecoration();
    cleanGrouping();
    waylandServer()->removeClient(this);
    deleted->unrefWindow();
    delete this;
}
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
374
375
376
377
378
379
380
void XdgSurfaceClient::setVirtualKeyboardGeometry(const QRect &geo)
{
    // No keyboard anymore
    if (geo.isEmpty() && !keyboardGeometryRestore().isEmpty()) {
        setFrameGeometry(keyboardGeometryRestore());
        setKeyboardGeometryRestore(QRect());
    } else if (geo.isEmpty()) {
        return;
    // The keyboard has just been opened (rather than resized) save client geometry for a restore
    } else if (keyboardGeometryRestore().isEmpty()) {
        setKeyboardGeometryRestore(requestedFrameGeometry().isEmpty() ? frameGeometry() : requestedFrameGeometry());
    }

    m_virtualKeyboardGeometry = geo;

    // Don't resize Desktop and fullscreen windows
    if (isFullScreen() || isDesktop()) {
        return;
    }

    if (!geo.intersects(keyboardGeometryRestore())) {
        return;
    }

    const QRect availableArea = workspace()->clientArea(MaximizeArea, this);
    QRect newWindowGeometry = keyboardGeometryRestore();
    newWindowGeometry.moveBottom(geo.top());
    newWindowGeometry.setTop(qMax(newWindowGeometry.top(), availableArea.top()));

    setFrameGeometry(newWindowGeometry);
}

381
382
383
384
385
386
387
388
389
390
XdgToplevelClient::XdgToplevelClient(XdgToplevelInterface *shellSurface)
    : XdgSurfaceClient(shellSurface->xdgSurface())
    , m_shellSurface(shellSurface)
{
    setupWindowManagementIntegration();
    setupPlasmaShellIntegration();
    setDesktop(VirtualDesktopManager::self()->current());

    if (waylandServer()->inputMethodConnection() == surface()->client()) {
        m_windowType = NET::OnScreenDisplay;
391
392
    }

393
394
395
396
397
398
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
    connect(shellSurface, &XdgToplevelInterface::windowTitleChanged,
            this, &XdgToplevelClient::handleWindowTitleChanged);
    connect(shellSurface, &XdgToplevelInterface::windowClassChanged,
            this, &XdgToplevelClient::handleWindowClassChanged);
    connect(shellSurface, &XdgToplevelInterface::windowMenuRequested,
            this, &XdgToplevelClient::handleWindowMenuRequested);
    connect(shellSurface, &XdgToplevelInterface::moveRequested,
            this, &XdgToplevelClient::handleMoveRequested);
    connect(shellSurface, &XdgToplevelInterface::resizeRequested,
            this, &XdgToplevelClient::handleResizeRequested);
    connect(shellSurface, &XdgToplevelInterface::maximizeRequested,
            this, &XdgToplevelClient::handleMaximizeRequested);
    connect(shellSurface, &XdgToplevelInterface::unmaximizeRequested,
            this, &XdgToplevelClient::handleUnmaximizeRequested);
    connect(shellSurface, &XdgToplevelInterface::fullscreenRequested,
            this, &XdgToplevelClient::handleFullscreenRequested);
    connect(shellSurface, &XdgToplevelInterface::unfullscreenRequested,
            this, &XdgToplevelClient::handleUnfullscreenRequested);
    connect(shellSurface, &XdgToplevelInterface::minimizeRequested,
            this, &XdgToplevelClient::handleMinimizeRequested);
    connect(shellSurface, &XdgToplevelInterface::parentXdgToplevelChanged,
            this, &XdgToplevelClient::handleTransientForChanged);
    connect(shellSurface, &XdgToplevelInterface::initializeRequested,
            this, &XdgToplevelClient::initialize);
    connect(shellSurface, &XdgToplevelInterface::destroyed,
            this, &XdgToplevelClient::destroyClient);
    connect(shellSurface->shell(), &XdgShellInterface::pingTimeout,
            this, &XdgToplevelClient::handlePingTimeout);
    connect(shellSurface->shell(), &XdgShellInterface::pingDelayed,
            this, &XdgToplevelClient::handlePingDelayed);
    connect(shellSurface->shell(), &XdgShellInterface::pongReceived,
            this, &XdgToplevelClient::handlePongReceived);
425

426
427
428
429
430
431
432
433
    connect(waylandServer(), &WaylandServer::foreignTransientChanged,
            this, &XdgToplevelClient::handleForeignTransientForChanged);
}

XdgToplevelClient::~XdgToplevelClient()
{
}

434
435
436
437
438
XdgToplevelInterface *XdgToplevelClient::shellSurface() const
{
    return m_shellSurface;
}

439
440
441
442
443
444
445
446
NET::WindowType XdgToplevelClient::windowType(bool direct, int supported_types) const
{
    Q_UNUSED(direct)
    Q_UNUSED(supported_types)
    return m_windowType;
}

MaximizeMode XdgToplevelClient::maximizeMode() const
447
{
448
    return m_maximizeMode;
449
450
}

451
MaximizeMode XdgToplevelClient::requestedMaximizeMode() const
452
453
454
455
{
    return m_requestedMaximizeMode;
}

456
QSize XdgToplevelClient::minSize() const
457
{
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
    return rules()->checkMinSize(m_shellSurface->minimumSize());
}

QSize XdgToplevelClient::maxSize() const
{
    return rules()->checkMaxSize(m_shellSurface->maximumSize());
}

bool XdgToplevelClient::isFullScreen() const
{
    return m_isFullScreen;
}

bool XdgToplevelClient::isMovable() const
{
    if (isFullScreen()) {
        return false;
475
    }
476
477
478
479
480
    if (isSpecialWindow() && !isSplash() && !isToolbar()) {
        return false;
    }
    if (rules()->checkPosition(invalidPoint) != invalidPoint) {
        return false;
481
    }
482
483
484
    return true;
}

485
bool XdgToplevelClient::isMovableAcrossScreens() const
486
{
487
    if (isSpecialWindow() && !isSplash() && !isToolbar()) {
488
489
        return false;
    }
490
491
492
493
    if (rules()->checkPosition(invalidPoint) != invalidPoint) {
        return false;
    }
    return true;
494
495
}

496
bool XdgToplevelClient::isResizable() const
497
{
498
499
    if (isFullScreen()) {
        return false;
500
    }
501
502
    if (isSpecialWindow() || isSplash() || isToolbar()) {
        return false;
503
    }
504
505
    if (rules()->checkSize(QSize()).isValid()) {
        return false;
506
    }
507
508
509
510
    const QSize min = minSize();
    const QSize max = maxSize();
    return min.width() < max.width() || min.height() < max.height();
}
511

512
513
514
515
516
517
518
519
520
bool XdgToplevelClient::isCloseable() const
{
    return !isDesktop() && !isDock();
}

bool XdgToplevelClient::isFullScreenable() const
{
    if (!rules()->checkFullScreen(true)) {
        return false;
521
    }
522
523
    return !isSpecialWindow();
}
524

525
526
527
528
bool XdgToplevelClient::isMaximizable() const
{
    if (!isResizable()) {
        return false;
529
    }
530
531
532
533
534
535
    if (rules()->checkMaximize(MaximizeRestore) != MaximizeRestore ||
            rules()->checkMaximize(MaximizeFull) != MaximizeFull) {
        return false;
    }
    return true;
}
536

537
538
539
540
541
542
543
544
545
546
bool XdgToplevelClient::isMinimizable() const
{
    if (isSpecialWindow() && !isTransient()) {
        return false;
    }
    if (!rules()->checkMinimize(true)) {
        return false;
    }
    return true;
}
547

548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
bool XdgToplevelClient::isTransient() const
{
    return m_isTransient;
}

bool XdgToplevelClient::userCanSetFullScreen() const
{
    return true;
}

bool XdgToplevelClient::userCanSetNoBorder() const
{
    if (m_serverDecoration) {
        switch (m_serverDecoration->mode()) {
        case ServerSideDecorationManagerInterface::Mode::Server:
            return !isFullScreen() && !isShade();
        case ServerSideDecorationManagerInterface::Mode::Client:
        case ServerSideDecorationManagerInterface::Mode::None:
            return false;
        }
    }
    if (m_xdgDecoration) {
        switch (m_xdgDecoration->preferredMode()) {
        case XdgToplevelDecorationV1Interface::Mode::Server:
        case XdgToplevelDecorationV1Interface::Mode::Undefined:
            return !isFullScreen() && !isShade();
        case XdgToplevelDecorationV1Interface::Mode::Client:
            return false;
        }
    }
    return false;
}

bool XdgToplevelClient::noBorder() const
{
    if (m_serverDecoration) {
        switch (m_serverDecoration->mode()) {
        case ServerSideDecorationManagerInterface::Mode::Server:
            return m_userNoBorder || isFullScreen();
        case ServerSideDecorationManagerInterface::Mode::Client:
        case ServerSideDecorationManagerInterface::Mode::None:
            return true;
        }
    }
    if (m_xdgDecoration) {
        switch (m_xdgDecoration->preferredMode()) {
        case XdgToplevelDecorationV1Interface::Mode::Server:
        case XdgToplevelDecorationV1Interface::Mode::Undefined:
            return m_userNoBorder || isFullScreen();
        case XdgToplevelDecorationV1Interface::Mode::Client:
            return true;
599
600
        }
    }
601
    return true;
602
603
}

604
void XdgToplevelClient::setNoBorder(bool set)
605
{
606
607
608
609
610
611
612
613
614
615
    if (!userCanSetNoBorder()) {
        return;
    }
    set = rules()->checkNoBorder(set);
    if (m_userNoBorder == set) {
        return;
    }
    m_userNoBorder = set;
    updateDecoration(true, false);
    updateWindowRules(Rules::NoBorder);
616
617
}

618
void XdgToplevelClient::updateDecoration(bool check_workspace_pos, bool force)
619
{
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
    if (!force && ((!isDecorated() && noBorder()) || (isDecorated() && !noBorder()))) {
        return;
    }
    const QRect oldFrameGeometry = frameGeometry();
    const QRect oldClientGeometry = clientGeometry();
    blockGeometryUpdates(true);
    if (force) {
        destroyDecoration();
    }
    if (!noBorder()) {
        createDecoration(oldFrameGeometry);
    } else {
        destroyDecoration();
    }
    if (m_serverDecoration && isDecorated()) {
        m_serverDecoration->setMode(ServerSideDecorationManagerInterface::Mode::Server);
    }
    if (m_xdgDecoration) {
        if (isDecorated() || m_userNoBorder) {
            m_xdgDecoration->sendConfigure(XdgToplevelDecorationV1Interface::Mode::Server);
        } else {
            m_xdgDecoration->sendConfigure(XdgToplevelDecorationV1Interface::Mode::Client);
David Edmundson's avatar
David Edmundson committed
642
        }
643
        scheduleConfigure();
644
    }
645
646
647
    updateShadow();
    if (check_workspace_pos) {
        checkWorkspacePosition(oldFrameGeometry, -2, oldClientGeometry);
648
    }
649
    blockGeometryUpdates(false);
650
651
}

652
bool XdgToplevelClient::supportsWindowRules() const
653
{
654
    return !m_plasmaShellSurface;
655
656
}

657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
StrutRect XdgToplevelClient::strutRect(StrutArea area) const
{
    if (!hasStrut()) {
        return StrutRect();
    }

    const QRect windowRect = frameGeometry();
    const QRect outputRect = screens()->geometry(screen());

    const bool left = windowRect.left() == outputRect.left();
    const bool right = windowRect.right() == outputRect.right();
    const bool top = windowRect.top() == outputRect.top();
    const bool bottom = windowRect.bottom() == outputRect.bottom();
    const bool horizontal = width() >= height();

    switch (area) {
    case StrutAreaTop:
        if (top && ((!left && !right) || horizontal)) {
            return StrutRect(windowRect, StrutAreaTop);
        }
        return StrutRect();
    case StrutAreaRight:
        if (right && ((!top && !bottom) || !horizontal)) {
            return StrutRect(windowRect, StrutAreaRight);
        }
        return StrutRect();
    case StrutAreaBottom:
        if (bottom && ((!left && !right) || horizontal)) {
            return StrutRect(windowRect, StrutAreaBottom);
        }
        return StrutRect();
    case StrutAreaLeft:
        if (left && ((!top && !bottom) || !horizontal)) {
            return StrutRect(windowRect, StrutAreaLeft);
        }
        return StrutRect();
    default:
        return StrutRect();
    }
}

698
bool XdgToplevelClient::hasStrut() const
699
{
700
701
    if (!isShown(true)) {
        return false;
702
    }
703
704
705
706
707
708
709
    if (!m_plasmaShellSurface) {
        return false;
    }
    if (m_plasmaShellSurface->role() != PlasmaShellSurfaceInterface::Role::Panel) {
        return false;
    }
    return m_plasmaShellSurface->panelBehavior() == PlasmaShellSurfaceInterface::PanelBehavior::AlwaysVisible;
710
711
}

712
void XdgToplevelClient::showOnScreenEdge()
713
{
714
    if (!m_plasmaShellSurface) {
715
        return;
716
    }
717
718
719
    
    // ShowOnScreenEdge can be called by an Edge, and hideClient could destroy the Edge
    // Use the singleshot to avoid use-after-free
720
    QTimer::singleShot(0, this, [this](){
721
722
723
724
725
726
        hideClient(false);
        workspace()->raiseClient(this);
        if (m_plasmaShellSurface->panelBehavior() == PlasmaShellSurfaceInterface::PanelBehavior::AutoHide) {
            m_plasmaShellSurface->showAutoHidingPanel();
        } 
    });
727
728
}

729
void XdgToplevelClient::closeWindow()
730
{
731
732
733
    if (isCloseable()) {
        sendPing(PingReason::CloseWindow);
        m_shellSurface->sendClose();
734
    }
735
}
736

737
738
739
740
741
742
743
744
745
746
747
XdgSurfaceConfigure *XdgToplevelClient::sendRoleConfigure() const
{
    const quint32 serial = m_shellSurface->sendConfigure(requestedClientSize(), m_requestedStates);

    XdgToplevelConfigure *configureEvent = new XdgToplevelConfigure();
    configureEvent->position = requestedPos();
    configureEvent->size = requestedSize();
    configureEvent->states = m_requestedStates;
    configureEvent->serial = serial;

    return configureEvent;
748
749
}

750
bool XdgToplevelClient::stateCompare() const
751
{
752
753
754
755
    if (m_requestedStates != m_acknowledgedStates) {
        return true;
    }
    return XdgSurfaceClient::stateCompare();
756
757
}

758
void XdgToplevelClient::handleRoleCommit()
759
{
760
761
762
763
    auto configureEvent = static_cast<XdgToplevelConfigure *>(lastAcknowledgedConfigure());
    if (configureEvent) {
        handleStatesAcknowledged(configureEvent->states);
    }
764
765
}

766
void XdgToplevelClient::doMinimize()
767
{
768
769
770
771
772
773
    if (isMinimized()) {
        workspace()->clientHidden(this);
    } else {
        emit windowShown(this);
    }
    workspace()->updateMinimizedOfTransients(this);
774
775
}

776
void XdgToplevelClient::doResizeSync()
777
{
778
    requestGeometry(moveResizeGeometry());
779
780
}

781
void XdgToplevelClient::doSetActive()
782
{
783
784
785
786
787
788
    WaylandClient::doSetActive();

    if (isActive()) {
        m_requestedStates |= XdgToplevelInterface::State::Activated;
    } else {
        m_requestedStates &= ~XdgToplevelInterface::State::Activated;
789
    }
790

791
792
793
794
795
796
797
    scheduleConfigure();
}

void XdgToplevelClient::doSetFullScreen()
{
    if (isFullScreen()) {
        m_requestedStates |= XdgToplevelInterface::State::FullScreen;
798
    } else {
799
        m_requestedStates &= ~XdgToplevelInterface::State::FullScreen;
800
    }
801

802
803
    scheduleConfigure();
}
804

805
806
807
808
809
810
void XdgToplevelClient::doSetMaximized()
{
    if (requestedMaximizeMode() & MaximizeHorizontal) {
        m_requestedStates |= XdgToplevelInterface::State::MaximizedHorizontal;
    } else {
        m_requestedStates &= ~XdgToplevelInterface::State::MaximizedHorizontal;
811
    }
812
813
814
815
816

    if (requestedMaximizeMode() & MaximizeVertical) {
        m_requestedStates |= XdgToplevelInterface::State::MaximizedVertical;
    } else {
        m_requestedStates &= ~XdgToplevelInterface::State::MaximizedVertical;
David Edmundson's avatar
David Edmundson committed
817
818
    }

819
820
821
    scheduleConfigure();
}

822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
static Qt::Edges anchorsForQuickTileMode(QuickTileMode mode)
{
    if (mode == QuickTileMode(QuickTileFlag::None)) {
        return Qt::Edges();
    }

    Qt::Edges anchors = Qt::LeftEdge | Qt::TopEdge | Qt::RightEdge | Qt::BottomEdge;

    if ((mode & QuickTileFlag::Left) && !(mode & QuickTileFlag::Right)) {
        anchors &= ~Qt::RightEdge;
    }
    if ((mode & QuickTileFlag::Right) && !(mode & QuickTileFlag::Left)) {
        anchors &= ~Qt::LeftEdge;
    }

    if ((mode & QuickTileFlag::Top) && !(mode & QuickTileFlag::Bottom)) {
        anchors &= ~Qt::BottomEdge;
    }
    if ((mode & QuickTileFlag::Bottom) && !(mode & QuickTileFlag::Top)) {
        anchors &= ~Qt::TopEdge;
    }

    return anchors;
}

void XdgToplevelClient::doSetQuickTileMode()
{
    const Qt::Edges anchors = anchorsForQuickTileMode(quickTileMode());

    if (anchors & Qt::LeftEdge) {
        m_requestedStates |= XdgToplevelInterface::State::TiledLeft;
    } else {
        m_requestedStates &= ~XdgToplevelInterface::State::TiledLeft;
    }

    if (anchors & Qt::RightEdge) {
        m_requestedStates |= XdgToplevelInterface::State::TiledRight;
    } else {
        m_requestedStates &= ~XdgToplevelInterface::State::TiledRight;
    }

    if (anchors & Qt::TopEdge) {
        m_requestedStates |= XdgToplevelInterface::State::TiledTop;
    } else {
        m_requestedStates &= ~XdgToplevelInterface::State::TiledTop;
    }

    if (anchors & Qt::BottomEdge) {
        m_requestedStates |= XdgToplevelInterface::State::TiledBottom;
    } else {
        m_requestedStates &= ~XdgToplevelInterface::State::TiledBottom;
    }

    scheduleConfigure();
}

878
879
880
881
bool XdgToplevelClient::doStartMoveResize()
{
    if (moveResizePointerMode() != PositionCenter) {
        m_requestedStates |= XdgToplevelInterface::State::Resizing;
882
    }
883

884
885
    scheduleConfigure();
    return true;
886
887
}

888
void XdgToplevelClient::doFinishMoveResize()
889
{
890
891
892
    m_requestedStates &= ~XdgToplevelInterface::State::Resizing;
    scheduleConfigure();
}
893

894
bool XdgToplevelClient::takeFocus()
895
896
897
898
{
    if (wantsInput()) {
        sendPing(PingReason::FocusWindow);
        setActive(true);
899
    }
900
901
    if (!keepAbove() && !isOnScreenDisplay() && !belongsToDesktop()) {
        workspace()->setShowingDesktop(false);
902
    }
903
    return true;
904
905
}

906
bool XdgToplevelClient::wantsInput() const
907
{
908
    return rules()->checkAcceptFocus(acceptsFocus());
909
910
}

911
bool XdgToplevelClient::dockWantsInput() const
912
{
913
914
915
    if (m_plasmaShellSurface) {
        if (m_plasmaShellSurface->role() == PlasmaShellSurfaceInterface::Role::Panel) {
            return m_plasmaShellSurface->panelTakesFocus();
916
917
        }
    }
918
919
920
921
922
923
924
925
926
    return false;
}

bool XdgToplevelClient::acceptsFocus() const
{
    if (m_plasmaShellSurface) {
        if (m_plasmaShellSurface->role() == PlasmaShellSurfaceInterface::Role::OnScreenDisplay ||
            m_plasmaShellSurface->role() == PlasmaShellSurfaceInterface::Role::ToolTip) {
            return false;
927
        }
928
929
930
        if (m_plasmaShellSurface->role() == PlasmaShellSurfaceInterface::Role::Notification ||
            m_plasmaShellSurface->role() == PlasmaShellSurfaceInterface::Role::CriticalNotification) {
            return m_plasmaShellSurface->panelTakesFocus();
931
932
        }
    }
933
    return !isZombie() && readyForPainting();
934
935
}

936
Layer XdgToplevelClient::layerForDock() const
937
{
938
939
940
941
942
943
    if (m_plasmaShellSurface) {
        switch (m_plasmaShellSurface->panelBehavior()) {
        case PlasmaShellSurfaceInterface::PanelBehavior::WindowsCanCover:
            return NormalLayer;
        case PlasmaShellSurfaceInterface::PanelBehavior::AutoHide:
        case PlasmaShellSurfaceInterface::PanelBehavior::WindowsGoBelow:
944
            return AboveLayer;
945
946
947
948
949
950
        case PlasmaShellSurfaceInterface::PanelBehavior::AlwaysVisible:
            return DockLayer;
        default:
            Q_UNREACHABLE();
            break;
        }
951
    }
952
    return AbstractClient::layerForDock();
953
954
}

955
void XdgToplevelClient::handleWindowTitleChanged()
956
{
957
    setCaption(m_shellSurface->windowTitle());
958
959
}

960
void XdgToplevelClient::handleWindowClassChanged()
961
{
962
963
964
965
    const QByteArray applicationId = m_shellSurface->windowClass().toUtf8();
    setResourceClass(resourceName(), applicationId);
    if (shellSurface()->isConfigured() && supportsWindowRules()) {
        evaluateWindowRules();
966
    }
967
968
969
970
971
972
973
974
975
    setDesktopFileName(applicationId);
}

void XdgToplevelClient::handleWindowMenuRequested(SeatInterface *seat, const QPoint &surfacePos,
                                                  quint32 serial)
{
    Q_UNUSED(seat)
    Q_UNUSED(serial)
    performMouseCommand(Options::MouseOperationsMenu, pos() + surfacePos);
976
977
}

978
void XdgToplevelClient::handleMoveRequested(SeatInterface *seat, quint32 serial)
979
980
981
{
    Q_UNUSED(seat)
    Q_UNUSED(serial)
982
    performMouseCommand(Options::MouseMove, Cursors::self()->mouse()->pos());
983
984
}

985
void XdgToplevelClient::handleResizeRequested(SeatInterface *seat, Qt::Edges edges, quint32 serial)
986
987
988
989
990
991
992
993
994
995
{
    Q_UNUSED(seat)
    Q_UNUSED(serial)
    if (!isResizable() || isShade()) {
        return;
    }
    if (isMoveResize()) {
        finishMoveResize(false);
    }
    setMoveResizePointerButtonDown(true);
996
    setMoveOffset(Cursors::self()->mouse()->pos() - pos());  // map from global
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
    setInvertedMoveOffset(rect().bottomRight() - moveOffset());
    setUnrestrictedMoveResize(false);
    auto toPosition = [edges] {
        Position position = PositionCenter;
        if (edges.testFlag(Qt::TopEdge)) {
            position = PositionTop;
        } else if (edges.testFlag(Qt::BottomEdge)) {
            position = PositionBottom;
        }
        if (edges.testFlag(Qt::LeftEdge)) {
            position = Position(position | PositionLeft);
        } else if (edges.testFlag(Qt::RightEdge)) {
            position = Position(position | PositionRight);
        }
        return position;
    };
    setMoveResizePointerMode(toPosition());
    if (!startMoveResize()) {
        setMoveResizePointerButtonDown(false);
    }
    updateCursor();
}

1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
void XdgToplevelClient::handleStatesAcknowledged(const XdgToplevelInterface::States &states)
{
    const XdgToplevelInterface::States delta = m_acknowledgedStates ^ states;

    if (delta & XdgToplevelInterface::State::Maximized) {
        MaximizeMode maximizeMode = MaximizeRestore;
        if (states & XdgToplevelInterface::State::MaximizedHorizontal) {
            maximizeMode = MaximizeMode(maximizeMode | MaximizeHorizontal);
        }
        if (states & XdgToplevelInterface::State::MaximizedVertical) {
            maximizeMode = MaximizeMode(maximizeMode | MaximizeVertical);
        }
        updateMaximizeMode(maximizeMode);
    }
    if (delta & XdgToplevelInterface::State::FullScreen) {
        updateFullScreenMode(states & XdgToplevelInterface::State::FullScreen);
    }

    m_acknowledgedStates = states;
}

void XdgToplevelClient::handleMaximizeRequested()
{
1043
1044
    if (m_isInitialized) {
        maximize(MaximizeFull);
1045
        scheduleConfigure(ConfigureRequired);
1046
1047
1048
    } else {
        m_initialStates |= XdgToplevelInterface::State::Maximized;
    }
1049
1050
1051
1052
}

void XdgToplevelClient::handleUnmaximizeRequested()
{
1053
1054
    if (m_isInitialized) {
        maximize(MaximizeRestore);
1055
        scheduleConfigure(ConfigureRequired);
1056
1057
1058
    } else {
        m_initialStates &= ~XdgToplevelInterface::State::Maximized;
    }
1059
1060
1061
1062
1063
}

void XdgToplevelClient::handleFullscreenRequested(OutputInterface *output)
{
    Q_UNUSED(output)
1064
1065
    if (m_isInitialized) {
        setFullScreen(/* set */ true, /* user */ false);
1066
        scheduleConfigure(ConfigureRequired);
1067
1068
1069
    } else {
        m_initialStates |= XdgToplevelInterface::State::FullScreen;
    }
1070
1071
1072
1073
}

void XdgToplevelClient::handleUnfullscreenRequested()
{
1074
1075
    if (m_isInitialized) {
        setFullScreen(/* set */ false, /* user */ false);
1076
        scheduleConfigure(ConfigureRequired);
1077
1078
1079
    } else {
        m_initialStates &= ~XdgToplevelInterface::State::FullScreen;
    }
1080
1081
1082
}

void XdgToplevelClient::handleMinimizeRequested()
1083
{
1084
    performMouseCommand(Options::MouseMinimize, Cursors::self()->mouse()->pos());
1085
1086
}

1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
void XdgToplevelClient::handleTransientForChanged()
{
    SurfaceInterface *transientForSurface = nullptr;
    if (XdgToplevelInterface *parentToplevel = m_shellSurface->parentXdgToplevel()) {
        transientForSurface = parentToplevel->surface();
    }
    if (!transientForSurface) {
        transientForSurface = waylandServer()->findForeignTransientForSurface(surface());
    }
    AbstractClient *transientForClient = waylandServer()->findClient(transientForSurface);
    if (transientForClient != transientFor()) {
        if (transientFor()) {
            transientFor()->removeTransient(this);
        }
        if (transientForClient) {
            transientForClient->addTransient(this);
        }
        setTransientFor(transientForClient);
    }
    m_isTransient = transientForClient;
}

void XdgToplevelClient::handleForeignTransientForChanged(SurfaceInterface *child)
{
    if (surface() == child) {
        handleTransientForChanged();
    }
}

void XdgToplevelClient::handlePingTimeout(quint32 serial)
{
    auto pingIt = m_pings.find(serial);
    if (pingIt == m_pings.end()) {
        return;
    }
    if (pingIt.value() == PingReason::CloseWindow) {
        qCDebug(KWIN_CORE) << "Final ping timeout on a close attempt, asking to kill:" << caption();

        //for internal windows, killing the window will delete this
        QPointer<QObject> guard(this);
        killWindow();
        if (!guard) {
            return;
        }
    }
    m_pings.erase(pingIt);
}

void XdgToplevelClient::handlePingDelayed(quint32 serial)
{
    auto it = m_pings.find(serial);
    if (it != m_pings.end()) {
        qCDebug(KWIN_CORE) << "First ping timeout:" << caption();
        setUnresponsive(true);
    }
}

void XdgToplevelClient::handlePongReceived(quint32 serial)
{
    auto it = m_pings.find(serial);
    if (it != m_pings.end()) {
        setUnresponsive(false);
        m_pings.erase(it);
    }
}

void XdgToplevelClient::sendPing(PingReason reason)
{
    XdgShellInterface *shell = m_shellSurface->shell();
    XdgSurfaceInterface *surface = m_shellSurface->xdgSurface();

    const quint32 serial = shell->ping(surface);
    m_pings.insert(serial, reason);
}

1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
MaximizeMode XdgToplevelClient::initialMaximizeMode() const
{
    MaximizeMode maximizeMode = MaximizeRestore;
    if (m_initialStates & XdgToplevelInterface::State::MaximizedHorizontal) {
        maximizeMode = MaximizeMode(maximizeMode | MaximizeHorizontal);
    }
    if (m_initialStates & XdgToplevelInterface::State::MaximizedVertical) {
        maximizeMode = MaximizeMode(maximizeMode | MaximizeVertical);
    }
    return maximizeMode;
}

bool XdgToplevelClient::initialFullScreenMode() const
{
    return m_initialStates & XdgToplevelInterface::State::FullScreen;
}

1179
void XdgToplevelClient::initialize()
1180
{
1181
1182
1183
1184
    blockGeometryUpdates(true);

    bool needsPlacement = !isInitialPositionSet();

1185
1186
    updateDecoration(false, false);

1187
1188
1189
1190
1191
1192
1193
1194
    if (supportsWindowRules()) {
        setupWindowRules(false);

        const QRect originalGeometry = frameGeometry();
        const QRect ruledGeometry = rules()->checkGeometry(originalGeometry, true);
        if (originalGeometry != ruledGeometry) {
            setFrameGeometry(ruledGeometry);
        }
1195
1196
        maximize(rules()->checkMaximize(initialMaximizeMode(), true));
        setFullScreen(rules()->checkFullScreen(initialFullScreenMode(), true), false);
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
        setDesktop(rules()->checkDesktop(desktop(), true));
        setDesktopFileName(rules()->checkDesktopFile(desktopFileName(), true).toUtf8());
        if (rules()->checkMinimize(isMinimized(), true)) {
            minimize(true); // No animation.
        }
        setSkipTaskbar(rules()->checkSkipTaskbar(skipTaskbar(),