summaryrefslogtreecommitdiffstats
path: root/tests/auto/client/shared/coreprotocol.cpp
blob: fdd5f627c80f600277549c2b95a2cf58b7cd4511 (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
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "coreprotocol.h"

namespace MockCompositor {

void Surface::sendFrameCallbacks()
{
    uint time = m_wlCompositor->m_compositor->currentTimeMilliseconds();
    for (auto *callback : m_waitingFrameCallbacks)
        callback->sendDone(time);
    m_waitingFrameCallbacks.clear();
}

void Surface::sendEnter(Output *output)
{
    m_outputs.append(output);
    const auto outputResources = output->resourceMap().values(resource()->client());
    for (auto outputResource: outputResources)
        wl_surface::send_enter(resource()->handle, outputResource->handle);
}

void Surface::sendLeave(Output *output)
{
    m_outputs.removeOne(output);
    const auto outputResources = output->resourceMap().values(resource()->client());
    for (auto outputResource: outputResources)
        wl_surface::send_leave(resource()->handle, outputResource->handle);
}

void Surface::surface_destroy_resource(Resource *resource)
{
    Q_UNUSED(resource);
    for (auto *commit : m_commits)
        delete commit->commitSpecific.frame;
    bool removed = m_wlCompositor->m_surfaces.removeOne(this);
    Q_ASSERT(removed);
    delete this;
}

void Surface::surface_attach(Resource *resource, wl_resource *buffer, int32_t x, int32_t y)
{
    Q_UNUSED(resource);
    QPoint offset(x, y);
    m_pending.buffer = fromResource<Buffer>(buffer);
    m_pending.commitSpecific.attachOffset = offset;
    m_pending.commitSpecific.attached = true;
    emit attach(buffer, offset);
}

void Surface::surface_set_buffer_scale(QtWaylandServer::wl_surface::Resource *resource, int32_t scale)
{
    Q_UNUSED(resource);
    m_pending.bufferScale = scale;
}

void Surface::surface_commit(Resource *resource)
{
    Q_UNUSED(resource);
    m_committed = m_pending;
    m_commits.append(new DoubleBufferedState(m_committed));

    if (auto *frame = m_pending.commitSpecific.frame)
        m_waitingFrameCallbacks.append(frame);

    m_pending.commitSpecific = PerCommitData();
    emit commit();
    if (m_committed.commitSpecific.attached)
        emit bufferCommitted();
}

void Surface::surface_frame(Resource *resource, uint32_t callback)
{
    // Although valid, there is really no point having multiple frame requests in the same commit.
    // Make sure we don't do it
    QCOMPARE(m_pending.commitSpecific.frame, nullptr);

    auto *frame = new Callback(resource->client(), callback, 1);
    m_pending.commitSpecific.frame = frame;
}

bool WlCompositor::isClean() {
    for (auto *surface : qAsConst(m_surfaces)) {
        if (!CursorRole::fromSurface(surface))
            return false;
    }
    return true;
}

QString WlCompositor::dirtyMessage()
{
    if (isClean())
        return "clean";
    QStringList messages;
    for (auto *s : qAsConst(m_surfaces)) {
        QString role = s->m_role ? s->m_role->staticMetaObject.className(): "none/unknown";
        messages << "Surface with role: " + role;
    }
    return "Dirty, surfaces left:\n\t" + messages.join("\n\t");
}

void Output::sendGeometry()
{
    const auto resources = resourceMap().values();
    for (auto r : resources)
        sendGeometry(r);
}

void Output::sendGeometry(Resource *resource)
{
    // TODO: check resource version as well?
    wl_output::send_geometry(resource->handle,
                             m_data.position.x(), m_data.position.y(),
                             m_data.physicalSize.width(), m_data.physicalSize.height(),
                             m_data.subpixel, m_data.make, m_data.model, m_data.transform);
}

void Output::sendScale(int factor)
{
    Q_ASSERT(m_version >= WL_OUTPUT_SCALE_SINCE_VERSION);
    m_data.scale = factor;
    const auto resources = resourceMap().values();
    for (auto r : resources)
        sendScale(r);
}

void Output::sendScale(Resource *resource)
{
    Q_ASSERT(m_version >= WL_OUTPUT_SCALE_SINCE_VERSION);
    // TODO: check resource version as well?
    wl_output::send_scale(resource->handle, m_data.scale);
}

void Output::sendDone()
{
    Q_ASSERT(m_version >= WL_OUTPUT_DONE_SINCE_VERSION);
    // TODO: check resource version as well?
    const auto resources = resourceMap().values();
    for (auto r : resources)
        wl_output::send_done(r->handle);
}

void Output::output_bind_resource(QtWaylandServer::wl_output::Resource *resource)
{
    sendGeometry(resource);
    send_mode(resource->handle, mode_preferred | mode_current,
              m_data.mode.resolution.width(), m_data.mode.resolution.height(), m_data.mode.refreshRate);
    if (m_version >= WL_OUTPUT_SCALE_SINCE_VERSION)
        sendScale(resource);

    if (m_version >= WL_OUTPUT_DONE_SINCE_VERSION)
        wl_output::send_done(resource->handle);
}

// Seat stuff
Seat::Seat(CoreCompositor *compositor, uint capabilities, int version) //TODO: check version
    : QtWaylandServer::wl_seat(compositor->m_display, version)
    , m_compositor(compositor)
{
    setCapabilities(capabilities);
}

Seat::~Seat()
{
    qDeleteAll(m_oldPointers);
    delete m_pointer;
}

void Seat::setCapabilities(uint capabilities) {
    // TODO: Add support for touch and keyboard
    Q_ASSERT(capabilities == 0 || capabilities == capability_pointer);

    m_capabilities = capabilities;

    if (m_capabilities & capability_pointer) {
        if (!m_pointer)
            m_pointer = (new Pointer(this));
    } else if (m_pointer) {
        m_oldPointers << m_pointer;
        m_pointer = nullptr;
    }

    for (auto *resource : resourceMap())
        wl_seat::send_capabilities(resource->handle, capabilities);
}

void Seat::seat_get_pointer(Resource *resource, uint32_t id)
{
    if (~m_capabilities & capability_pointer) {
        qWarning() << "Client requested a wl_pointer without the capability being available."
                   << "This Could be a race condition when hotunplugging,"
                   << "but is most likely a client error";
        Pointer *pointer = new Pointer(this);
        pointer->add(resource->client(), id, resource->version());
        // TODO: mark as destroyed
        m_oldPointers << pointer;
        return;
    }
    m_pointer->add(resource->client(), id, resource->version());
}

Surface *Pointer::cursorSurface()
{
    return m_cursorRole ? m_cursorRole->m_surface : nullptr;
}

uint Pointer::sendEnter(Surface *surface, const QPointF &position)
{
    wl_fixed_t x = wl_fixed_from_double(position.x());
    wl_fixed_t y = wl_fixed_from_double(position.y());
    m_enterSerial = m_seat->m_compositor->nextSerial();

    wl_client *client = surface->resource()->client();
    const auto pointerResources = resourceMap().values(client);
    for (auto *r : pointerResources)
        send_enter(r->handle, m_enterSerial, surface->resource()->handle, x ,y);
    return m_enterSerial;
}

// Make sure you call enter, frame etc. first
void Pointer::sendMotion(wl_client *client, const QPointF &position)
{
    wl_fixed_t x = wl_fixed_from_double(position.x());
    wl_fixed_t y = wl_fixed_from_double(position.y());
    auto time = m_seat->m_compositor->currentTimeMilliseconds();
    const auto pointerResources = resourceMap().values(client);
    for (auto *r : pointerResources)
        send_motion(r->handle, time, x, y);
}

// Make sure you call enter, frame etc. first
uint Pointer::sendButton(wl_client *client, uint button, uint state)
{
    Q_ASSERT(state == button_state_pressed || state == button_state_released);
    auto time = m_seat->m_compositor->currentTimeMilliseconds();
    uint serial = m_seat->m_compositor->nextSerial();
    const auto pointerResources = resourceMap().values(client);
    for (auto *r : pointerResources)
        send_button(r->handle, serial, time, button, state);
    return serial;
}

// Make sure you call enter, frame etc. first
void Pointer::sendAxis(wl_client *client, axis axis, qreal value)
{
    auto time = m_seat->m_compositor->currentTimeMilliseconds();
    wl_fixed_t val = wl_fixed_from_double(value);
    const auto pointerResources = resourceMap().values(client);
    for (auto *r : pointerResources)
        send_axis(r->handle, time, axis, val);
}

void Pointer::pointer_set_cursor(Resource *resource, uint32_t serial, wl_resource *surface, int32_t hotspot_x, int32_t hotspot_y)
{
    Q_UNUSED(resource);
    Q_UNUSED(hotspot_x);
    Q_UNUSED(hotspot_y);
    auto *s = fromResource<Surface>(surface);
    QVERIFY(s);

    if (s->m_role) {
        auto *cursorRole = CursorRole::fromSurface(s);
        QVERIFY(cursorRole);
        QVERIFY(cursorRole == m_cursorRole);
    } else {
        m_cursorRole = new CursorRole(s); //TODO: make sure we don't leak CursorRole
        s->m_role = m_cursorRole;
    }
//    QCOMPARE(serial, m_enterSerial); //TODO: uncomment when this bug is fixed
    emit setCursor(serial);
}

// Shm implementation
Shm::Shm(CoreCompositor *compositor, QVector<format> formats, int version)
    : QtWaylandServer::wl_shm(compositor->m_display, version)
    , m_compositor(compositor)
    , m_formats(formats)
{
    // Some formats are specified as mandatory
    Q_ASSERT(m_formats.contains(format_argb8888));
    Q_ASSERT(m_formats.contains(format_xrgb8888));
}

bool Shm::isClean()
{
//    for (ShmPool *pool : qAsConst(m_pools)) {
//        //TODO: return false if not cursor buffer
//        if (pool->m_buffers.isEmpty()) {
//            return false;
//        }
//    }
    return true;
}

void Shm::shm_create_pool(Resource *resource, uint32_t id, int32_t fd, int32_t size)
{
    Q_UNUSED(fd);
    Q_UNUSED(size);
    auto *pool = new ShmPool(this, resource->client(), id, 1);
    m_pools.append(pool);
}

ShmPool::ShmPool(Shm *shm, wl_client *client, int id, int version)
    : QtWaylandServer::wl_shm_pool(client, id, version)
    , m_shm(shm)
{
}

void ShmPool::shm_pool_create_buffer(Resource *resource, uint32_t id, int32_t offset, int32_t width, int32_t height, int32_t stride, uint32_t format)
{
    QSize size(width, height);
    new ShmBuffer(offset, size, stride, Shm::format(format), resource->client(), id);
}

void ShmPool::shm_pool_destroy_resource(Resource *resource)
{
    Q_UNUSED(resource);
    bool removed = m_shm->m_pools.removeOne(this);
    Q_ASSERT(removed);
    delete this;
}

} // namespace MockCompositor