summaryrefslogtreecommitdiff
path: root/tests/auto/client/shared/xdgshell.cpp
blob: b50f36e7d050f7e8ff671ee8acb8751d445c5849 (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
// Copyright (C) 2018 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "xdgshell.h"

namespace MockCompositor {

XdgWmBase::XdgWmBase(CoreCompositor *compositor, int version)
    : QtWaylandServer::xdg_wm_base(compositor->m_display, version)
    , m_compositor(compositor)
{
}

XdgToplevel *XdgWmBase::toplevel(int i)
{
    int j = 0;
    for (auto *xdgSurface : qAsConst(m_xdgSurfaces)) {
        if (auto *toplevel = xdgSurface->m_toplevel) {
            if (j == i)
                return toplevel;
            ++j;
        }
    }
    return nullptr;
}

XdgPopup *XdgWmBase::popup(int i)
{
    int j = 0;
    for (auto *xdgSurface : qAsConst(m_xdgSurfaces)) {
        if (auto *popup = xdgSurface->m_popup) {
            if (j == i)
                return popup;
            ++j;
        }
    }
    return nullptr;
}

void XdgWmBase::xdg_wm_base_get_xdg_surface(Resource *resource, uint32_t id, wl_resource *surface)
{
    auto *s = fromResource<Surface>(surface);
    auto *xdgSurface = new XdgSurface(this, s, resource->client(), id, resource->version());
    m_xdgSurfaces << xdgSurface;
    emit xdgSurfaceCreated(xdgSurface);
}

void XdgWmBase::xdg_wm_base_pong(Resource *resource, uint32_t serial)
{
    Q_UNUSED(resource);
    emit pong(serial);
}

XdgSurface::XdgSurface(XdgWmBase *xdgWmBase, Surface *surface, wl_client *client, int id, int version)
    : QtWaylandServer::xdg_surface(client, id, version)
    , m_xdgWmBase(xdgWmBase)
    , m_surface(surface)
{
    QVERIFY(!surface->m_pending.buffer);
    QVERIFY(!surface->m_committed.buffer);
    connect(this, &XdgSurface::toplevelCreated, xdgWmBase, &XdgWmBase::toplevelCreated, Qt::DirectConnection);
    connect(surface, &Surface::attach, this, &XdgSurface::verifyConfigured);
    connect(surface, &Surface::commit, this, [this] {
        m_committed = m_pending;

        if (m_ackedConfigureSerial != m_committedConfigureSerial) {
            m_committedConfigureSerial = m_ackedConfigureSerial;
            emit configureCommitted(m_committedConfigureSerial);
        }
    });
}

void XdgSurface::sendConfigure(uint serial)
{
    Q_ASSERT(serial);
    m_pendingConfigureSerials.append(serial);
    m_configureSent = true;
    xdg_surface::send_configure(serial);
}

uint XdgSurface::sendConfigure()
{
    const uint serial = m_xdgWmBase->m_compositor->nextSerial();
    sendConfigure(serial);
    return serial;
}

void XdgSurface::xdg_surface_get_toplevel(Resource *resource, uint32_t id)
{
    QVERIFY(!m_toplevel);
    QVERIFY(!m_popup);
    m_toplevel = new XdgToplevel(this, id, resource->version());
    emit toplevelCreated(m_toplevel);
}

void XdgSurface::xdg_surface_get_popup(Resource *resource, uint32_t id, wl_resource *parent, wl_resource *positioner)
{
    Q_UNUSED(positioner);
    QVERIFY(!m_toplevel);
    QVERIFY(!m_popup);
    auto *p = fromResource<XdgSurface>(parent);
    m_popup = new XdgPopup(this, p, id, resource->version());
}

void XdgSurface::xdg_surface_destroy_resource(Resource *resource)
{
    Q_UNUSED(resource);
    bool removed = m_xdgWmBase->m_xdgSurfaces.removeOne(this);
    Q_ASSERT(removed);
    delete this;
}

void XdgSurface::xdg_surface_destroy(QtWaylandServer::xdg_surface::Resource *resource)
{
    QVERIFY(m_popups.empty());
    wl_resource_destroy(resource->handle);
}

void XdgSurface::xdg_surface_set_window_geometry(Resource *resource, int32_t x, int32_t y, int32_t width, int32_t height)
{
    Q_UNUSED(resource);
    QRect rect(x, y, width, height);
    QVERIFY(rect.isValid());
    m_pending.windowGeometry = rect;
}

void XdgSurface::xdg_surface_ack_configure(Resource *resource, uint32_t serial)
{
    Q_UNUSED(resource);
    QVERIFY2(m_pendingConfigureSerials.contains(serial), qPrintable(QString::number(serial)));
    m_ackedConfigureSerial = serial;
    while (!m_pendingConfigureSerials.empty()) {
        uint s = m_pendingConfigureSerials.takeFirst();
        if (s == serial)
            return;
    }
}

XdgToplevel::XdgToplevel(XdgSurface *xdgSurface, int id, int version)
    : QtWaylandServer::xdg_toplevel(xdgSurface->resource()->client(), id, version)
    , m_xdgSurface(xdgSurface)
{
    connect(surface(), &Surface::commit, this, [this] { m_committed = m_pending; });
}

void XdgToplevel::sendConfigure(const QSize &size, const QList<uint> &states)
{
    send_configure(size.width(), size.height(), toByteArray(states));
}

uint XdgToplevel::sendCompleteConfigure(const QSize &size, const QList<uint> &states)
{
    sendConfigure(size, states);
    return m_xdgSurface->sendConfigure();
}

void XdgToplevel::xdg_toplevel_set_max_size(Resource *resource, int32_t width, int32_t height)
{
    Q_UNUSED(resource);
    QSize size(width, height);
    QVERIFY(size.isValid());
    m_pending.maxSize = size;
}

void XdgToplevel::xdg_toplevel_set_min_size(Resource *resource, int32_t width, int32_t height)
{
    Q_UNUSED(resource);
    QSize size(width, height);
    QVERIFY(size.isValid());
    m_pending.minSize = size;
}

XdgPopup::XdgPopup(XdgSurface *xdgSurface, XdgSurface *parent, int id, int version)
    : QtWaylandServer::xdg_popup(xdgSurface->resource()->client(), id, version)
    , m_xdgSurface(xdgSurface)
    , m_parentXdgSurface(parent)
{
    Q_ASSERT(m_xdgSurface);
    Q_ASSERT(m_parentXdgSurface);
    m_parentXdgSurface->m_popups << this;
}

void XdgPopup::sendConfigure(const QRect &geometry)
{
    send_configure(geometry.x(), geometry.y(), geometry.width(), geometry.height());
}

uint XdgPopup::sendCompleteConfigure(const QRect &geometry)
{
    sendConfigure(geometry);
    return m_xdgSurface->sendConfigure();
}

void XdgPopup::xdg_popup_grab(QtWaylandServer::xdg_popup::Resource *resource, wl_resource *seat, uint32_t serial)
{
    Q_UNUSED(resource);
    Q_UNUSED(seat); // TODO: verify correct seat as well
    QVERIFY(!m_grabbed);
    QVERIFY(m_parentXdgSurface->isValidPopupGrabParent());
    m_xdgSurface->m_xdgWmBase->m_topmostGrabbingPopup = this;
    m_grabbed = true;
    m_grabSerial = serial;
}

void XdgPopup::xdg_popup_destroy(Resource *resource) {
    Q_UNUSED(resource);
    if (m_grabbed) {
        auto *base = m_xdgSurface->m_xdgWmBase;
        QCOMPARE(base->m_topmostGrabbingPopup, this);
        base->m_topmostGrabbingPopup = this->m_parentXdgSurface->m_popup;
    }
    m_xdgSurface->m_popup = nullptr;
    m_parentXdgSurface->m_popups.removeAll(this);
    emit destroyRequested();
}

} // namespace MockCompositor