2020-08-02 22:22:19 +00:00
|
|
|
/*
|
|
|
|
SPDX-FileCopyrightText: 2018 Roman Gilg <subdiff@gmail.com>
|
2018-06-09 13:19:50 +00:00
|
|
|
|
2020-08-02 22:22:19 +00:00
|
|
|
SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
|
|
|
|
*/
|
2018-06-09 13:19:50 +00:00
|
|
|
#include "pointerconstraintstest.h"
|
|
|
|
|
|
|
|
#include <KWayland/Client/compositor.h>
|
|
|
|
#include <KWayland/Client/connection_thread.h>
|
|
|
|
#include <KWayland/Client/pointer.h>
|
|
|
|
#include <KWayland/Client/pointerconstraints.h>
|
2022-03-23 10:13:38 +00:00
|
|
|
#include <KWayland/Client/region.h>
|
|
|
|
#include <KWayland/Client/registry.h>
|
|
|
|
#include <KWayland/Client/seat.h>
|
|
|
|
#include <KWayland/Client/surface.h>
|
2018-06-09 13:19:50 +00:00
|
|
|
|
2022-03-23 10:13:38 +00:00
|
|
|
#include <QCursor>
|
2018-06-09 13:19:50 +00:00
|
|
|
#include <QGuiApplication>
|
|
|
|
#include <QQmlContext>
|
|
|
|
#include <QQmlEngine>
|
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QScopedPointer>
|
|
|
|
|
|
|
|
#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,
|
2022-03-23 10:13:38 +00:00
|
|
|
[this, registry](quint32 name, quint32 version) {
|
|
|
|
m_compositor = registry->createCompositor(name, version, this);
|
|
|
|
});
|
2018-06-09 13:19:50 +00:00
|
|
|
connect(registry, &Registry::seatAnnounced, this,
|
2022-03-23 10:13:38 +00:00
|
|
|
[this, registry](quint32 name, quint32 version) {
|
|
|
|
m_seat = registry->createSeat(name, version, this);
|
|
|
|
if (m_seat->hasPointer()) {
|
2018-06-09 13:19:50 +00:00
|
|
|
m_pointer = m_seat->createPointer(this);
|
|
|
|
}
|
2022-03-23 10:13:38 +00:00
|
|
|
connect(m_seat, &Seat::hasPointerChanged, this,
|
|
|
|
[this]() {
|
|
|
|
delete m_pointer;
|
|
|
|
m_pointer = m_seat->createPointer(this);
|
|
|
|
});
|
|
|
|
});
|
2018-06-09 13:19:50 +00:00
|
|
|
connect(registry, &Registry::pointerConstraintsUnstableV1Announced, this,
|
2022-03-23 10:13:38 +00:00
|
|
|
[this, registry](quint32 name, quint32 version) {
|
|
|
|
m_pointerConstraints = registry->createPointerConstraints(name, version, this);
|
|
|
|
});
|
2018-06-09 13:19:50 +00:00
|
|
|
connect(registry, &Registry::interfacesAnnounced, this,
|
2022-03-23 10:13:38 +00:00
|
|
|
[this] {
|
|
|
|
Q_ASSERT(m_compositor);
|
|
|
|
Q_ASSERT(m_seat);
|
|
|
|
Q_ASSERT(m_pointerConstraints);
|
|
|
|
});
|
2018-06-09 13:19:50 +00:00
|
|
|
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)
|
|
|
|
{
|
2022-03-23 10:13:38 +00:00
|
|
|
return persistent ? PointerConstraints::LifeTime::Persistent : PointerConstraints::LifeTime::OneShot;
|
2018-06-09 13:19:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
QScopedPointer<Surface> winSurface(Surface::fromWindow(view()));
|
|
|
|
QScopedPointer<Region> wlRegion(m_compositor->createRegion(this));
|
|
|
|
wlRegion->add(region);
|
|
|
|
|
|
|
|
auto *lockedPointer = m_pointerConstraints->lockPointer(winSurface.data(),
|
|
|
|
m_pointer,
|
|
|
|
wlRegion.data(),
|
|
|
|
lifeTime(persistent),
|
|
|
|
this);
|
|
|
|
|
|
|
|
if (!lockedPointer) {
|
|
|
|
qDebug() << "ERROR when receiving locked pointer!";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_lockedPointer = lockedPointer;
|
|
|
|
m_lockedPointerPersistent = persistent;
|
2018-07-18 07:06:56 +00:00
|
|
|
|
2018-06-09 13:19:50 +00:00
|
|
|
connect(lockedPointer, &LockedPointer::locked, this, [this]() {
|
|
|
|
qDebug() << "------ LOCKED! ------";
|
2022-03-23 10:13:38 +00:00
|
|
|
if (lockHint()) {
|
2018-07-18 07:06:56 +00:00
|
|
|
m_lockedPointer->setCursorPositionHint(QPointF(10., 10.));
|
2021-05-13 16:46:06 +00:00
|
|
|
Q_EMIT forceSurfaceCommit();
|
2018-07-18 07:06:56 +00:00
|
|
|
}
|
|
|
|
|
2018-06-09 13:19:50 +00:00
|
|
|
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;
|
|
|
|
QScopedPointer<Surface> winSurface(Surface::fromWindow(view()));
|
|
|
|
QScopedPointer<Region> wlRegion(m_compositor->createRegion(this));
|
|
|
|
wlRegion->add(region);
|
|
|
|
|
|
|
|
auto *confinedPointer = m_pointerConstraints->confinePointer(winSurface.data(),
|
|
|
|
m_pointer,
|
|
|
|
wlRegion.data(),
|
|
|
|
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);
|
Use nullptr everywhere
Summary:
Because KWin is a very old project, we use three kinds of null pointer
literals: 0, NULL, and nullptr. Since C++11, it's recommended to use
nullptr keyword.
This change converts all usages of 0 and NULL literal to nullptr. Even
though it breaks git history, we need to do it in order to have consistent
code as well to ease code reviews (it's very tempting for some people to
add unrelated changes to their patches, e.g. converting NULL to nullptr).
Test Plan: Compiles.
Reviewers: #kwin, davidedmundson, romangg
Reviewed By: #kwin, davidedmundson, romangg
Subscribers: romangg, kwin
Tags: #kwin
Differential Revision: https://phabricator.kde.org/D23618
2019-09-19 14:46:54 +00:00
|
|
|
m_xcbConn = xcb_connect(nullptr, nullptr);
|
2018-06-09 13:19:50 +00:00
|
|
|
if (!m_xcbConn) {
|
|
|
|
qDebug() << "Could not open XCB connection.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void XBackend::lockRequest(bool persistent, QRect region)
|
|
|
|
{
|
|
|
|
Q_UNUSED(persistent);
|
|
|
|
Q_UNUSED(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 */
|
2022-03-23 10:13:38 +00:00
|
|
|
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 */
|
|
|
|
);
|
2018-06-09 13:19:50 +00:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
Q_UNUSED(persistent);
|
|
|
|
Q_UNUSED(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();
|
|
|
|
|
2022-03-23 10:13:38 +00:00
|
|
|
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 */
|
|
|
|
);
|
2018-06-09 13:19:50 +00:00
|
|
|
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;
|
|
|
|
|
2022-03-23 10:13:38 +00:00
|
|
|
QQmlContext *context = view.engine()->rootContext();
|
2018-06-09 13:19:50 +00:00
|
|
|
context->setContextProperty(QStringLiteral("org_kde_kwin_tests_pointerconstraints_backend"), backend);
|
|
|
|
|
2022-03-23 10:13:38 +00:00
|
|
|
view.setSource(QUrl::fromLocalFile(QStringLiteral(DIR) + QStringLiteral("/pointerconstraintstest.qml")));
|
2018-06-09 13:19:50 +00:00
|
|
|
view.show();
|
|
|
|
|
|
|
|
backend->init(&view);
|
|
|
|
|
|
|
|
return app.exec();
|
|
|
|
}
|