summaryrefslogtreecommitdiffstats
path: root/src/imports/compositor-extensions/qtshell/qwaylandqtshell.cpp
blob: 7788e3ea874be38916edc7e54b102a6147e34c21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
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
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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
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
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include "qwaylandqtshell.h"
#include "qwaylandqtshell_p.h"
#include "qwaylandqtshellchrome.h"

#include <QtWaylandCompositor/QWaylandCompositor>
#include <QtWaylandCompositor/QWaylandSurface>
#include "qwaylandqtshell.h"
#include <QtWaylandCompositor/QWaylandResource>

#if QT_CONFIG(wayland_compositor_quick)
# include "qwaylandqtshellintegration_p.h"
#endif

#include <QtWaylandCompositor/QWaylandResource>
#include <QDebug>
#include <compositor/compositor_api/qwaylandseat.h>

#include <QtWaylandCompositor/private/qwaylandutils_p.h>

QT_BEGIN_NAMESPACE

/*!
 * \qmltype QtShell
 * \instantiates QWaylandQtShell
 * \inqmlmodule QtWayland.Compositor.QtShell
 * \since 6.3
 * \brief Provides a shell extension for Qt applications running on a Qt Wayland Compositor.
 *
 * The QtShell extension provides a way to associate an QtShellSurface with a regular Wayland
 * surface. The QtShell extension is written to support the window management features which are
 * supported by Qt. It may be suitable on a platform where both the compositor and client
 * applications are written with Qt, and where applications are trusted not to abuse features such
 * as manual window positioning and "bring-to-front".
 *
 * For other use cases, consider using IviApplication or XdgShell instead.
 *
 * \qml
 * import QtWayland.Compositor.QtShell
 *
 * WaylandCompositor {
 *     property ListModel shellSurfaces: ListModel {}
 *     QtShell {
 *         onQtShellSurfaceCreated: {
 *              shellSurfaces.append({shellSurface: qtShellSurface})
 *         }
 *     }
 * }
 * \endqml
 */
QWaylandQtShell::QWaylandQtShell()
    : QWaylandCompositorExtensionTemplate<QWaylandQtShell>(*new QWaylandQtShellPrivate())
{
}

QWaylandQtShell::QWaylandQtShell(QWaylandCompositor *compositor)
    : QWaylandCompositorExtensionTemplate<QWaylandQtShell>(compositor, *new QWaylandQtShellPrivate())
{
}

bool QWaylandQtShell::moveChromeToFront(QWaylandQtShellChrome *chrome)
{
    Q_D(QWaylandQtShell);
    for (int i = 0; i < d->m_chromes.size(); ++i) {
        if (d->m_chromes.at(i) == chrome) {
            if (i > 0) {
                QWaylandQtShellChrome *currentActive = d->m_chromes.first();
                d->m_chromes.move(i, 0);
                chrome->activate();
                currentActive->deactivate();
            }
            return true;
        }
    }

    return false;
}

void QWaylandQtShell::registerChrome(QWaylandQtShellChrome *chrome)
{
    Q_D(QWaylandQtShell);
    if (moveChromeToFront(chrome))
        return;

    QWaylandQtShellChrome *currentActive = d->m_chromes.isEmpty() ? nullptr : d->m_chromes.first();

    d->m_chromes.prepend(chrome);
    chrome->activate();

    if (currentActive != nullptr)
        currentActive->deactivate();

    connect(chrome, &QWaylandQtShellChrome::activated, this, &QWaylandQtShell::chromeActivated);
    connect(chrome, &QWaylandQtShellChrome::deactivated, this, &QWaylandQtShell::chromeDeactivated);
}

void QWaylandQtShell::unregisterChrome(QWaylandQtShellChrome *chrome)
{
    Q_D(QWaylandQtShell);

    chrome->disconnect(this);
    int index = d->m_chromes.indexOf(chrome);
    if (index >= 0) {
        d->m_chromes.removeAt(index);
        if (index == 0 && d->m_chromes.size() > 0)
            d->m_chromes.at(0)->activate();
    }
}

void QWaylandQtShell::chromeActivated()
{
    QWaylandQtShellChrome *c = qobject_cast<QWaylandQtShellChrome *>(sender());
    if (c != nullptr) {
        moveChromeToFront(c);
    }
}

void QWaylandQtShell::chromeDeactivated()
{
    Q_D(QWaylandQtShell);
    QWaylandQtShellChrome *c = qobject_cast<QWaylandQtShellChrome *>(sender());
    if (d->m_chromes.size() > 1 && d->m_chromes.at(0) == c) {
        d->m_chromes.move(0, 1);
        d->m_chromes.at(0)->activate();
    } else if (d->m_chromes.size() == 1) { // One window must be active
        d->m_chromes.at(0)->activate();
    }
}

void QWaylandQtShell::initialize()
{
    Q_D(QWaylandQtShell);
    QWaylandCompositorExtensionTemplate::initialize();

    QWaylandCompositor *compositor = static_cast<QWaylandCompositor *>(extensionContainer());
    if (!compositor) {
        qWarning() << "Failed to find QWaylandCompositor when initializing QWaylandQtShell";
        return;
    }

    d->init(compositor->display(), 1);
}

const struct wl_interface *QWaylandQtShell::interface()
{
    return QWaylandQtShellPrivate::interface();
}

/*!
 * \internal
 */
QByteArray QWaylandQtShell::interfaceName()
{
    return QWaylandQtShellPrivate::interfaceName();
}

/*!
 * \qmlsignal void QtWaylandCompositor::QtShell::qtShellSurfaceRequested(WaylandSurface surface, WaylandResource resource)
 *
 * This signal is emitted when the client has requested a QtShellSurface to be associated
 * with \a surface. The handler for this signal is expected to create the QtShellSurface for
 * \a resource and initialize it within the scope of the signal emission. If no QtShellSurface is
 * created, a default one will be created instead.
 */

/*!
 * \qmlsignal void QtWaylandCompositor::QtShell::qtShellSurfaceCreated(QtShellSurface *qtShellSurface)
 *
 * This signal is emitted when an QtShellSurface has been created. The supplied \a qtShellSurface is
 * most commonly used to instantiate a ShellSurfaceItem.
 */

QWaylandQtShellPrivate::QWaylandQtShellPrivate()
{
}

void QWaylandQtShellPrivate::unregisterQtShellSurface(QWaylandQtShellSurface *qtShellSurface)
{
    Q_UNUSED(qtShellSurface)
}

void QWaylandQtShellPrivate::zqt_shell_v1_surface_create(QtWaylandServer::zqt_shell_v1::Resource *resource, wl_resource *surfaceResource, uint32_t id)
{
    Q_Q(QWaylandQtShell);
    QWaylandSurface *surface = QWaylandSurface::fromResource(surfaceResource);

    if (!surface->setRole(QWaylandQtShellSurface::role(), resource->handle, ZQT_SHELL_V1_ERROR_ROLE))
        return;

    QWaylandResource qtShellSurfaceResource(wl_resource_create(resource->client(), &zqt_shell_surface_v1_interface,
                                                           wl_resource_get_version(resource->handle), id));

    emit q->qtShellSurfaceRequested(surface, qtShellSurfaceResource);

    QWaylandQtShellSurface *qtShellSurface = QWaylandQtShellSurface::fromResource(qtShellSurfaceResource.resource());

    if (!qtShellSurface)
        qtShellSurface = new QWaylandQtShellSurface(q, surface, qtShellSurfaceResource);

    emit q->qtShellSurfaceCreated(qtShellSurface);
}

QWaylandSurfaceRole QWaylandQtShellSurfacePrivate::s_role("qt_shell_surface");

/*!
 * \qmltype QtShellSurface
 * \instantiates QWaylandQtShellSurface
 * \inqmlmodule QtWayland.Compositor.QtShell
 * \since 6.3
 * \brief Provides a simple way to identify and resize a surface.
 *
 * This type is part of the \l{QtShell} extension and provides a way to extend
 * the functionality of an existing WaylandSurface with window management functionality.
 *
 * The QtShellSurface type holds the core functionality needed to create a compositor that supports
 * the QtShell extension. It can be used directly, or via the QtShellChrome type, depending on what
 * the needs of the compositor are. The QtShellChrome type has default behaviors and convenience
 * APIs for working with QtShellSurface objects.
 */

/*!
  \qmlsignal void QtWaylandCompositor::QtShellSurface::startMove()

  The client has requested an interactive move operation in the compositor by calling
  \l{QWindow::startSystemMove()}.

  \sa capabilities
*/

/*!
  \qmlsignal void QtWaylandCompositor::QtShellSurface::startResize(enum edges)

  The client has requested an interactive resize operation in the compositor by calling
  \l{QWindow::startSystemResize()}.

  The \a edges provides information about which edge of the window should be moved during the
  resize. It is a mask of the following values:
  \list
    \li Qt.TopEdge
    \li Qt.LeftEdge
    \li Qt.RightEdge
    \li Qt.BottomEdge
 \endlist

  \sa capabilities
*/

QWaylandQtShellSurface::QWaylandQtShellSurface()
    : QWaylandShellSurfaceTemplate<QWaylandQtShellSurface>(*new QWaylandQtShellSurfacePrivate())
{
}

QWaylandQtShellSurface::QWaylandQtShellSurface(QWaylandQtShell *application, QWaylandSurface *surface, const QWaylandResource &resource)
    : QWaylandShellSurfaceTemplate<QWaylandQtShellSurface>(*new QWaylandQtShellSurfacePrivate())
{
    initialize(application, surface, resource);
}

/*!
 * \qmlmethod void QtWaylandCompositor::QtShellSurface::initialize(QtShell qtShell, WaylandSurface surface, WaylandResource resource)
 *
 * Initializes the QtShellSurface, associating it with the given \a qtShell, \a surface, and
 * \a resource.
 */
void QWaylandQtShellSurface::initialize(QWaylandQtShell *qtShell, QWaylandSurface *surface, const QWaylandResource &resource)
{
    Q_D(QWaylandQtShellSurface);

    d->m_qtShell = qtShell;
    d->m_surface = surface;

    connect(d->m_surface, &QWaylandSurface::damaged, this, &QWaylandQtShellSurface::surfaceCommitted);

    d->init(resource.resource());
    setExtensionContainer(surface);

    emit surfaceChanged();

    QWaylandCompositorExtension::initialize();
}

/*!
 * \qmlproperty WaylandSurface QtWaylandCompositor::QtShellSurface::surface
 *
 * This property holds the surface associated with this QtShellSurface.
 */
QWaylandSurface *QWaylandQtShellSurface::surface() const
{
    Q_D(const QWaylandQtShellSurface);
    return d->m_surface;
}

QWaylandQtShell *QWaylandQtShellSurface::shell() const
{
    Q_D(const QWaylandQtShellSurface);
    return d->m_qtShell;
}

/*!
 * \qmlproperty point QtWaylandCompositor::QtShellSurface::windowPosition
 *
 * This property holds the position of the shell surface relative to its output.
 */
QPoint QWaylandQtShellSurface::windowPosition() const
{
    Q_D(const QWaylandQtShellSurface);
    return d->m_windowGeometry.topLeft();
}

void QWaylandQtShellSurface::setWindowPosition(const QPoint &position)
{
    Q_D(QWaylandQtShellSurface);

    // We don't care about the ack in this case, so use UINT_MAX as serial
    d->send_set_position(UINT32_MAX, position.x(), position.y());
    d->send_configure(UINT32_MAX);

    d->m_windowGeometry.moveTopLeft(position);
    d->m_positionSet = true;
    emit positionAutomaticChanged();
    emit windowGeometryChanged();
}

/*!
 * \qmlproperty rect QtWaylandCompositor::QtShellSurface::windowGeometry
 *
 * This property holds the window geometry of the shell surface.
 */
QRect QWaylandQtShellSurface::windowGeometry() const
{
    Q_D(const QWaylandQtShellSurface);
    return d->m_windowGeometry;
}

/*!
 * \qmlproperty size QtWaylandCompositor::QtShellSurface::minimumSize
 *
 * The minimum size of the window if the client has specified one. Otherwise an invalid size.
 */
QSize QWaylandQtShellSurface::minimumSize() const
{
    Q_D(const QWaylandQtShellSurface);
    return d->m_minimumSize;
}

/*!
 * \qmlproperty size QtWaylandCompositor::QtShellSurface::maximumSize
 *
 * The maximum size of the window if the client has specified one. Otherwise an invalid size.
 */
QSize QWaylandQtShellSurface::maximumSize() const
{
    Q_D(const QWaylandQtShellSurface);
    return d->m_maximumSize;
}

/*!
 *  \qmlmethod void QtWaylandCompositor::QtShellSurface::requestWindowGeometry(int windowState, rect windowGeometry)
 *
 *  Requests a new \a windowState and \a windowGeometry for the QtShellSurface. The state and
 *  geometry is updated when the client has acknowledged the request (at which point it is safe to
 *  assume that the surface's buffer has been resized if necessary).
 */
void QWaylandQtShellSurface::requestWindowGeometry(uint windowState, const QRect &windowGeometry)
{
    Q_D(QWaylandQtShellSurface);
    if (!windowGeometry.isValid())
        return;

    d->configure(windowState, windowGeometry);
}

void QWaylandQtShellSurfacePrivate::configure(uint windowState, const QRect &newGeometry)
{
    QWaylandCompositor *compositor = m_surface != nullptr ? m_surface->compositor() : nullptr;
    if (!compositor) {
        qWarning() << "Failed to find QWaylandCompositor when configuring QWaylandQtShell";
        return;
    }

    uint32_t serial = compositor->nextSerial();
    m_pendingConfigures[serial] = qMakePair(windowState, newGeometry);

    send_set_position(serial, newGeometry.x(), newGeometry.y());
    send_resize(serial, newGeometry.width(), newGeometry.height());
    send_set_window_state(serial, windowState & ~Qt::WindowActive);
    send_configure(serial);
}

void QWaylandQtShellSurface::setFrameMargins(const QMargins &margins)
{
    Q_D(QWaylandQtShellSurface);
    if (d->m_frameMargins == margins)
        return;

    d->m_frameMargins = margins;
    d->updateFrameMargins();

    emit frameMarginChanged();
}

/*!
 *  \qmlproperty int QtWaylandCompositor::QtShellSurface::frameMarginLeft
 *
 *  This holds the window frame margin to the left of the surface.
 */
void QWaylandQtShellSurface::setFrameMarginLeft(int left)
{
    Q_D(QWaylandQtShellSurface);
    if (d->m_frameMargins.left() == left)
        return;

    d->m_frameMargins.setLeft(left);
    d->updateFrameMargins();

    emit frameMarginChanged();
}

int QWaylandQtShellSurface::frameMarginLeft() const
{
    Q_D(const QWaylandQtShellSurface);
    return d->m_frameMargins.left();
}

/*!
 *  \qmlproperty int QtWaylandCompositor::QtShellSurface::frameMarginRight
 *
 *  This holds the window frame margin to the right of the surface.
 */
void QWaylandQtShellSurface::setFrameMarginRight(int right)
{
    Q_D(QWaylandQtShellSurface);
    if (d->m_frameMargins.right() == right)
        return;

    d->m_frameMargins.setRight(right);
    d->updateFrameMargins();

    emit frameMarginChanged();
}

int QWaylandQtShellSurface::frameMarginRight() const
{
    Q_D(const QWaylandQtShellSurface);
    return d->m_frameMargins.right();
}

/*!
 *  \qmlproperty int QtWaylandCompositor::QtShellSurface::frameMarginTop
 *
 *  This holds the window frame margin above the surface.
 */

void QWaylandQtShellSurface::setFrameMarginTop(int top)
{
    Q_D(QWaylandQtShellSurface);
    if (d->m_frameMargins.top() == top)
        return;
    d->m_frameMargins.setTop(top);
    d->updateFrameMargins();

    emit frameMarginChanged();
}

int QWaylandQtShellSurface::frameMarginTop() const
{
    Q_D(const QWaylandQtShellSurface);
    return d->m_frameMargins.top();
}

/*!
 *  \qmlproperty int QtWaylandCompositor::QtShellSurface::frameMarginBottom
 *
 *  This holds the window frame margin below the surface.
 */
void QWaylandQtShellSurface::setFrameMarginBottom(int bottom)
{
    Q_D(QWaylandQtShellSurface);
    if (d->m_frameMargins.bottom() == bottom)
        return;
    d->m_frameMargins.setBottom(bottom);
    d->updateFrameMargins();

    emit frameMarginChanged();
}

bool QWaylandQtShellSurface::positionAutomatic() const
{
    Q_D(const QWaylandQtShellSurface);
    return !d->m_positionSet;
}

int QWaylandQtShellSurface::frameMarginBottom() const
{
    Q_D(const QWaylandQtShellSurface);
    return d->m_frameMargins.bottom();
}

/*!
 *  \qmlproperty int QtWaylandCompositor::QtShellSurface::windowFlags
 *
 *  This property holds the window flags of the QtShellSurface.
 */
uint QWaylandQtShellSurface::windowFlags() const
{
    Q_D(const QWaylandQtShellSurface);
    return d->m_windowFlags;
}

/*!
 *  \qmlmethod void QtWaylandCompositor::QtShellSurface::sendClose()
 *
 *  Requests that the client application closes itself.
 */
void QWaylandQtShellSurface::sendClose()
{
    Q_D(QWaylandQtShellSurface);
    d->send_close();
}

/*!
 *  \qmlproperty string QtWaylandCompositor::QtShellSurface::windowTitle
 *
 *  This property holds the window title of the QtShellSurface.
 */
QString QWaylandQtShellSurface::windowTitle() const
{
    Q_D(const QWaylandQtShellSurface);
    return d->m_windowTitle;
}

/*!
 *  \qmlproperty bool QtWaylandCompositor::QtShellSurface::active
 *
 *  This property holds whether the surface is currently considered active.
 *
 *  \note There are no restrictions in QtShellSurface that prevents multiple surfaces from being
 *  active simultaneously. Such logic must either be implemented by the compositor itself, or by
 *  using the QtShellChrome type, which will automatically manage the activation state of surfaces.
 */
void QWaylandQtShellSurface::setActive(bool active)
{
    Q_D(QWaylandQtShellSurface);
    if (d->m_active == active)
        return;

    d->m_active = active;
    QWaylandCompositor *compositor = d->m_surface ? d->m_surface->compositor() : nullptr;
    QWaylandSeat *seat = compositor ? compositor->defaultSeat() : nullptr;
    if (seat && active)
        seat->setKeyboardFocus(surface());
    emit activeChanged();
}

bool QWaylandQtShellSurface::active() const
{
    Q_D(const QWaylandQtShellSurface);
    return d->m_active;
}

/*!
 *  \qmlproperty enum QtWaylandCompositor::QtShellSurface::capabilities
 *
 * This property holds the capabilities of the compositor. By default, no special capabilities are
 * enabled.
 *
 * \list
 *   \li QtShellSurface.InteractiveMove The client can trigger a server-side interactive move
 *       operation using \l{QWindow::startSystemMove()}. The compositor will be notified of this
 *       through the \l{startMove()} signal.
 *   \li QtShellSurface.InteractiveResize The client can trigger a server-side interactive resize
 *       operation using \l{QWindow::startSystemResize()}. The compositor will be notified of this
 *       through the \l{startResize()} signal.
 * \endlist
 */
void QWaylandQtShellSurface::setCapabilities(CapabilityFlags capabilities)
{
    Q_D(QWaylandQtShellSurface);
    if (d->m_capabilities == capabilities)
        return;

    d->m_capabilities = capabilities;
    d->send_set_capabilities(capabilities);

    emit capabilitiesChanged();
}

QWaylandQtShellSurface::CapabilityFlags QWaylandQtShellSurface::capabilities() const
{
    Q_D(const QWaylandQtShellSurface);
    return d->m_capabilities;
}

/*!
 *  \qmlproperty int QtWaylandCompositor::QtShellSurface::windowState
 *
 *  This property holds the window state of the QtShellSurface.
 *
 *  \note When \l{requestWindowGeometry()} is called to update state of the surface, the
 *  \c windowState property will not be updated until the client has acknowledged the state change.
 */
uint QWaylandQtShellSurface::windowState() const
{
    Q_D(const QWaylandQtShellSurface);
    return d->m_windowState;
}

void QWaylandQtShellSurface::surfaceCommitted()
{
    Q_D(QWaylandQtShellSurface);
    if (d->m_lastAckedConfigure < UINT32_MAX) {
        QRect targetRect = d->m_windowGeometry;
        uint windowState = d->m_windowState;
        for (auto it = d->m_pendingConfigures.begin(); it != d->m_pendingConfigures.end(); ) {
            if (it.key() == d->m_lastAckedConfigure) {
                targetRect = it.value().second;
                windowState = it.value().first;
            }

            if (it.key() <= d->m_lastAckedConfigure)
                it = d->m_pendingConfigures.erase(it);
            else
                break;
        }

        if (d->m_windowState != windowState) {
            d->m_windowState = windowState;
            emit windowStateChanged();
        }

        if (d->m_windowGeometry != targetRect) {
            d->m_windowGeometry = targetRect;
            d->m_positionSet = true;
            emit positionAutomaticChanged();
            emit windowGeometryChanged();
        }

        d->m_lastAckedConfigure = UINT32_MAX;
        d->m_pendingPosition = QPoint{};
        d->m_pendingPositionValid = false;
        d->m_pendingSize = QSize{};
    } else {
        QRect oldRect = d->m_windowGeometry;
        if (d->m_pendingPositionValid) {
            d->m_windowGeometry.moveTopLeft(d->m_pendingPosition);
            d->m_pendingPosition = QPoint{};
            d->m_pendingPositionValid = false;
            d->m_positionSet = true;
            emit positionAutomaticChanged();
        }

        if (d->m_pendingSize.isValid()) {
            d->m_windowGeometry.setSize(d->m_pendingSize);
            d->m_pendingSize = QSize{};
        }

        if (d->m_windowGeometry != oldRect)
            emit windowGeometryChanged();
    }
}

/*!
 * Returns the Wayland interface for the QWaylandQtShellSurface.
 */
const wl_interface *QWaylandQtShellSurface::interface()
{
    return QWaylandQtShellSurfacePrivate::interface();
}

QByteArray QWaylandQtShellSurface::interfaceName()
{
    return QWaylandQtShellSurfacePrivate::interfaceName();
}

/*!
 * Returns the surface role for the QWaylandQtShellSurface.
 */
QWaylandSurfaceRole *QWaylandQtShellSurface::role()
{
    return &QWaylandQtShellSurfacePrivate::s_role;
}

/*!
 * Returns the QWaylandQtShellSurface corresponding to the \a resource.
 */
QWaylandQtShellSurface *QWaylandQtShellSurface::fromResource(wl_resource *resource)
{
    if (auto p = QtWayland::fromResource<QWaylandQtShellSurfacePrivate *>(resource))
        return p->q_func();
    return nullptr;
}

#if QT_CONFIG(wayland_compositor_quick)
QWaylandQuickShellIntegration *QWaylandQtShellSurface::createIntegration(QWaylandQuickShellSurfaceItem *item)
{
    return new QtWayland::QtShellIntegration(item);
}
#endif

/*!
 * \internal
 */
void QWaylandQtShellSurface::initialize()
{
    QWaylandShellSurfaceTemplate::initialize();
}

QWaylandQtShellSurfacePrivate::QWaylandQtShellSurfacePrivate()
{
}

void QWaylandQtShellSurfacePrivate::zqt_shell_surface_v1_ack_configure(Resource *resource, uint32_t serial)
{
    Q_UNUSED(resource);
    Q_Q(QWaylandQtShellSurface);
    if (serial < UINT32_MAX)
        m_lastAckedConfigure = serial;

    // Fake a surface commit because we won't get one as long as the window is unexposed
    if (m_windowState & Qt::WindowMinimized)
        q->surfaceCommitted();
}

void QWaylandQtShellSurfacePrivate::zqt_shell_surface_v1_reposition(Resource *resource, int32_t x, int32_t y)
{
    Q_UNUSED(resource);

    m_pendingPosition = QPoint(x, y);
    m_pendingPositionValid = true;
    m_lastAckedConfigure = UINT32_MAX;
}

void QWaylandQtShellSurfacePrivate::zqt_shell_surface_v1_set_size(Resource *resource, int32_t width, int32_t height)
{
    Q_UNUSED(resource);

    m_pendingSize = QSize(width, height);
    m_lastAckedConfigure = UINT32_MAX;
}

void QWaylandQtShellSurfacePrivate::zqt_shell_surface_v1_set_minimum_size(Resource *resource, int32_t width, int32_t height)
{
    Q_UNUSED(resource);
    Q_Q(QWaylandQtShellSurface);
    m_minimumSize = QSize{width, height};
    emit q->minimumSizeChanged();
}

void QWaylandQtShellSurfacePrivate::zqt_shell_surface_v1_set_maximum_size(Resource *resource, int32_t width, int32_t height)
{
    Q_UNUSED(resource);
    Q_Q(QWaylandQtShellSurface);
    m_maximumSize = QSize{width, height};
    emit q->maximumSizeChanged();
}

void QWaylandQtShellSurfacePrivate::zqt_shell_surface_v1_destroy_resource(QtWaylandServer::zqt_shell_surface_v1::Resource *resource)
{
    Q_UNUSED(resource);
    Q_Q(QWaylandQtShellSurface);
    QWaylandQtShellPrivate::get(m_qtShell)->unregisterQtShellSurface(q);
    delete q;
}

void QWaylandQtShellSurfacePrivate::zqt_shell_surface_v1_destroy(QtWaylandServer::zqt_shell_surface_v1::Resource *resource)
{
    wl_resource_destroy(resource->handle);
}

void QWaylandQtShellSurfacePrivate::zqt_shell_surface_v1_set_window_flags(Resource *resource, uint32_t flags)
{
    Q_UNUSED(resource);
    Q_Q(QWaylandQtShellSurface);
    m_windowFlags = flags;
    emit q->windowFlagsChanged();
}

void QWaylandQtShellSurfacePrivate::zqt_shell_surface_v1_change_window_state(Resource *resource, uint32_t state)
{
    Q_UNUSED(resource);
    Q_Q(QWaylandQtShellSurface);
    uint oldWindowState = m_windowState;
    m_windowState = state & ~Qt::WindowActive;

    if (oldWindowState != m_windowState)
        emit q->windowStateChanged();
}

void QWaylandQtShellSurfacePrivate::zqt_shell_surface_v1_start_system_resize(Resource *resource, uint32_t serial, uint32_t edge)
{
    Q_UNUSED(resource);
    Q_UNUSED(serial);
    Q_Q(QWaylandQtShellSurface);
    emit q->startResize(Qt::Edges(edge));
}

void QWaylandQtShellSurfacePrivate::zqt_shell_surface_v1_start_system_move(Resource *resource, uint32_t serial)
{
    Q_UNUSED(resource);
    Q_UNUSED(serial);
    Q_Q(QWaylandQtShellSurface);
    emit q->startMove();
}

void QWaylandQtShellSurfacePrivate::zqt_shell_surface_v1_set_window_title(Resource *resource,
                                                                          const QString &title)
{
    Q_UNUSED(resource);
    Q_Q(QWaylandQtShellSurface);
    m_windowTitle = title;
    emit q->windowTitleChanged();
}

void QWaylandQtShellSurfacePrivate::zqt_shell_surface_v1_request_activate(Resource *resource)

{
    Q_UNUSED(resource);
    Q_Q(QWaylandQtShellSurface);
    q->setActive(true);
}

void QWaylandQtShellSurfacePrivate::updateFrameMargins()
{
    send_set_frame_margins(m_frameMargins.left(), m_frameMargins.right(),
                           m_frameMargins.top(), m_frameMargins.bottom());
}


void QWaylandQtShellSurfacePrivate::zqt_shell_surface_v1_raise(Resource *resource)
{
    Q_UNUSED(resource);
    Q_Q(QWaylandQtShellSurface);
    emit q->raiseRequested();
}

void QWaylandQtShellSurfacePrivate::zqt_shell_surface_v1_lower(Resource *resource)
{
    Q_UNUSED(resource);
    Q_Q(QWaylandQtShellSurface);
    emit q->lowerRequested();
}

QT_END_NAMESPACE

#include "moc_qwaylandqtshell.cpp"