File: pointerconstraintstest.cpp

package info (click to toggle)
kwin 4%3A6.5.4-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 53,376 kB
  • sloc: cpp: 240,899; javascript: 2,225; xml: 2,096; ansic: 775; sh: 37; python: 15; makefile: 8
file content (393 lines) | stat: -rw-r--r-- 12,590 bytes parent folder | download | duplicates (6)
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
/*
    SPDX-FileCopyrightText: 2018 Roman Gilg <subdiff@gmail.com>

    SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
#include "pointerconstraintstest.h"

#include <KWayland/Client/compositor.h>
#include <KWayland/Client/connection_thread.h>
#include <KWayland/Client/pointer.h>
#include <KWayland/Client/pointerconstraints.h>
#include <KWayland/Client/region.h>
#include <KWayland/Client/registry.h>
#include <KWayland/Client/seat.h>
#include <KWayland/Client/surface.h>

#include <QCursor>
#include <QGuiApplication>
#include <QQmlContext>
#include <QQmlEngine>

#include <QDebug>

#include <xcb/xproto.h>

using namespace KWayland::Client;

WaylandBackend::WaylandBackend(QObject *parent)
    : Backend(parent)
    , m_connectionThreadObject(ConnectionThread::fromApplication(this))
{
    setMode(Mode::Wayland);
}

void WaylandBackend::init(QQuickView *view)
{
    Backend::init(view);

    Registry *registry = new Registry(this);
    setupRegistry(registry);
}

void WaylandBackend::setupRegistry(Registry *registry)
{
    connect(registry, &Registry::compositorAnnounced, this,
            [this, registry](quint32 name, quint32 version) {
                m_compositor = registry->createCompositor(name, version, this);
            });
    connect(registry, &Registry::seatAnnounced, this,
            [this, registry](quint32 name, quint32 version) {
                m_seat = registry->createSeat(name, version, this);
                if (m_seat->hasPointer()) {
                    m_pointer = m_seat->createPointer(this);
                }
                connect(m_seat, &Seat::hasPointerChanged, this,
                        [this]() {
                            delete m_pointer;
                            m_pointer = m_seat->createPointer(this);
                        });
            });
    connect(registry, &Registry::pointerConstraintsUnstableV1Announced, this,
            [this, registry](quint32 name, quint32 version) {
                m_pointerConstraints = registry->createPointerConstraints(name, version, this);
            });
    connect(registry, &Registry::interfacesAnnounced, this,
            [this] {
                Q_ASSERT(m_compositor);
                Q_ASSERT(m_seat);
                Q_ASSERT(m_pointerConstraints);
            });
    registry->create(m_connectionThreadObject);
    registry->setup();
}

bool WaylandBackend::isLocked()
{
    return m_lockedPointer && m_lockedPointer->isValid();
}

bool WaylandBackend::isConfined()
{
    return m_confinedPointer && m_confinedPointer->isValid();
}

static PointerConstraints::LifeTime lifeTime(bool persistent)
{
    return persistent ? PointerConstraints::LifeTime::Persistent : PointerConstraints::LifeTime::OneShot;
}

void WaylandBackend::lockRequest(bool persistent, QRect region)
{
    if (isLocked()) {
        if (!errorsAllowed()) {
            qDebug() << "Abort locking because already locked. Allow errors to test relocking (and crashing).";
            return;
        }
        qDebug() << "Trying to lock although already locked. Crash expected.";
    }
    if (isConfined()) {
        if (!errorsAllowed()) {
            qDebug() << "Abort locking because already confined. Allow errors to test locking while being confined (and crashing).";
            return;
        }
        qDebug() << "Trying to lock although already confined. Crash expected.";
    }
    qDebug() << "------ Lock requested ------";
    qDebug() << "Persistent:" << persistent << "| Region:" << region;
    std::unique_ptr<Surface> winSurface(Surface::fromWindow(view()));
    std::unique_ptr<Region> wlRegion(m_compositor->createRegion(this));
    wlRegion->add(region);

    auto *lockedPointer = m_pointerConstraints->lockPointer(winSurface.get(),
                                                            m_pointer,
                                                            wlRegion.get(),
                                                            lifeTime(persistent),
                                                            this);

    if (!lockedPointer) {
        qDebug() << "ERROR when receiving locked pointer!";
        return;
    }
    m_lockedPointer = lockedPointer;
    m_lockedPointerPersistent = persistent;

    connect(lockedPointer, &LockedPointer::locked, this, [this]() {
        qDebug() << "------ LOCKED! ------";
        if (lockHint()) {
            m_lockedPointer->setCursorPositionHint(QPointF(10., 10.));
            Q_EMIT forceSurfaceCommit();
        }

        Q_EMIT lockChanged(true);
    });
    connect(lockedPointer, &LockedPointer::unlocked, this, [this]() {
        qDebug() << "------ UNLOCKED! ------";
        if (!m_lockedPointerPersistent) {
            cleanupLock();
        }
        Q_EMIT lockChanged(false);
    });
}

void WaylandBackend::unlockRequest()
{
    if (!m_lockedPointer) {
        qDebug() << "Unlock requested, but there is no lock. Abort.";
        return;
    }
    qDebug() << "------ Unlock requested ------";
    cleanupLock();
    Q_EMIT lockChanged(false);
}
void WaylandBackend::cleanupLock()
{
    if (!m_lockedPointer) {
        return;
    }
    m_lockedPointer->release();
    m_lockedPointer->deleteLater();
    m_lockedPointer = nullptr;
}

void WaylandBackend::confineRequest(bool persistent, QRect region)
{
    if (isConfined()) {
        if (!errorsAllowed()) {
            qDebug() << "Abort confining because already confined. Allow errors to test reconfining (and crashing).";
            return;
        }
        qDebug() << "Trying to lock although already locked. Crash expected.";
    }
    if (isLocked()) {
        if (!errorsAllowed()) {
            qDebug() << "Abort confining because already locked. Allow errors to test confining while being locked (and crashing).";
            return;
        }
        qDebug() << "Trying to confine although already locked. Crash expected.";
    }
    qDebug() << "------ Confine requested ------";
    qDebug() << "Persistent:" << persistent << "| Region:" << region;
    std::unique_ptr<Surface> winSurface(Surface::fromWindow(view()));
    std::unique_ptr<Region> wlRegion(m_compositor->createRegion(this));
    wlRegion->add(region);

    auto *confinedPointer = m_pointerConstraints->confinePointer(winSurface.get(),
                                                                 m_pointer,
                                                                 wlRegion.get(),
                                                                 lifeTime(persistent),
                                                                 this);

    if (!confinedPointer) {
        qDebug() << "ERROR when receiving confined pointer!";
        return;
    }
    m_confinedPointer = confinedPointer;
    m_confinedPointerPersistent = persistent;
    connect(confinedPointer, &ConfinedPointer::confined, this, [this]() {
        qDebug() << "------ CONFINED! ------";
        Q_EMIT confineChanged(true);
    });
    connect(confinedPointer, &ConfinedPointer::unconfined, this, [this]() {
        qDebug() << "------ UNCONFINED! ------";
        if (!m_confinedPointerPersistent) {
            cleanupConfine();
        }
        Q_EMIT confineChanged(false);
    });
}
void WaylandBackend::unconfineRequest()
{
    if (!m_confinedPointer) {
        qDebug() << "Unconfine requested, but there is no confine. Abort.";
        return;
    }
    qDebug() << "------ Unconfine requested ------";
    cleanupConfine();
    Q_EMIT confineChanged(false);
}
void WaylandBackend::cleanupConfine()
{
    if (!m_confinedPointer) {
        return;
    }
    m_confinedPointer->release();
    m_confinedPointer->deleteLater();
    m_confinedPointer = nullptr;
}

XBackend::XBackend(QObject *parent)
    : Backend(parent)
{
    setMode(Mode::X);
    if (m_xcbConn) {
        xcb_disconnect(m_xcbConn);
        free(m_xcbConn);
    }
}

void XBackend::init(QQuickView *view)
{
    Backend::init(view);
    m_xcbConn = xcb_connect(nullptr, nullptr);

    if (xcb_connection_has_error(m_xcbConn)) {
        xcb_disconnect(m_xcbConn);
        qFatal() << "Could not open XCB connection.";
    }
}

void XBackend::lockRequest(bool persistent, QRect region)
{
    auto winId = view()->winId();

    /* Cursor needs to be hidden such that Xwayland emulates warps. */
    QGuiApplication::setOverrideCursor(QCursor(Qt::BlankCursor));

    auto cookie = xcb_warp_pointer_checked(m_xcbConn, /* connection */
                                           XCB_NONE, /* src_w */
                                           winId, /* dest_w */
                                           0, /* src_x */
                                           0, /* src_y */
                                           0, /* src_width */
                                           0, /* src_height */
                                           20, /* dest_x */
                                           20 /* dest_y */
    );
    xcb_flush(m_xcbConn);

    xcb_generic_error_t *error = xcb_request_check(m_xcbConn, cookie);
    if (error) {
        qDebug() << "Lock (warp) failed with XCB error:" << error->error_code;
        free(error);
        return;
    }
    qDebug() << "LOCK (warp)";
    Q_EMIT lockChanged(true);
}

void XBackend::unlockRequest()
{
    /* Xwayland unlocks the pointer, when the cursor is shown again. */
    QGuiApplication::restoreOverrideCursor();
    qDebug() << "------ Unlock requested ------";
    Q_EMIT lockChanged(false);
}

void XBackend::confineRequest(bool persistent, QRect region)
{
    int error;
    if (!tryConfine(error)) {
        qDebug() << "Confine (grab) failed with XCB error:" << error;
        return;
    }
    qDebug() << "CONFINE (grab)";
    Q_EMIT confineChanged(true);
}

void XBackend::unconfineRequest()
{
    auto cookie = xcb_ungrab_pointer_checked(m_xcbConn, XCB_CURRENT_TIME);
    xcb_flush(m_xcbConn);

    xcb_generic_error_t *error = xcb_request_check(m_xcbConn, cookie);
    if (error) {
        qDebug() << "Unconfine failed with XCB error:" << error->error_code;
        free(error);
        return;
    }
    qDebug() << "UNCONFINE (ungrab)";
    Q_EMIT confineChanged(false);
}

void XBackend::hideAndConfineRequest(bool confineBeforeHide)
{
    if (!confineBeforeHide) {
        QGuiApplication::setOverrideCursor(QCursor(Qt::BlankCursor));
    }

    int error;
    if (!tryConfine(error)) {
        qDebug() << "Confine failed with XCB error:" << error;
        if (!confineBeforeHide) {
            QGuiApplication::restoreOverrideCursor();
        }
        return;
    }
    if (confineBeforeHide) {
        QGuiApplication::setOverrideCursor(QCursor(Qt::BlankCursor));
    }
    qDebug() << "HIDE AND CONFINE (lock)";
    Q_EMIT confineChanged(true);
}

void XBackend::undoHideRequest()
{
    QGuiApplication::restoreOverrideCursor();
    qDebug() << "UNDO HIDE AND CONFINE (unlock)";
}

bool XBackend::tryConfine(int &error)
{
    auto winId = view()->winId();

    auto cookie = xcb_grab_pointer(m_xcbConn, /* display */
                                   1, /* owner_events */
                                   winId, /* grab_window */
                                   0, /* event_mask */
                                   XCB_GRAB_MODE_ASYNC, /* pointer_mode */
                                   XCB_GRAB_MODE_ASYNC, /* keyboard_mode */
                                   winId, /* confine_to */
                                   XCB_NONE, /* cursor */
                                   XCB_CURRENT_TIME /* time */
    );
    xcb_flush(m_xcbConn);

    xcb_generic_error_t *e = nullptr;
    auto *reply = xcb_grab_pointer_reply(m_xcbConn, cookie, &e);
    if (!reply) {
        error = e->error_code;
        free(e);
        return false;
    }
    free(reply);
    return true;
}

int main(int argc, char **argv)
{
    QGuiApplication app(argc, argv);

    Backend *backend;
    if (app.platformName() == QStringLiteral("wayland")) {
        qDebug() << "Starting up: Wayland native mode";
        backend = new WaylandBackend(&app);
    } else {
        qDebug() << "Starting up: Xserver/Xwayland legacy mode";
        backend = new XBackend(&app);
    }

    QQuickView view;

    QQmlContext *context = view.engine()->rootContext();
    context->setContextProperty(QStringLiteral("org_kde_kwin_tests_pointerconstraints_backend"), backend);

    view.setSource(QUrl::fromLocalFile(QStringLiteral(DIR) + QStringLiteral("/pointerconstraintstest.qml")));
    view.show();

    backend->init(&view);

    return app.exec();
}

#include "moc_pointerconstraintstest.cpp"