2021-07-23 19:28:22 +01:00
|
|
|
/*
|
|
|
|
SPDX-FileCopyrightText: 2018 Roman Gilg <subdiff@gmail.com>
|
|
|
|
SPDX-FileCopyrightText: 2021 Aleix Pol Gonzalez <aleixpol@kde.org>
|
|
|
|
|
|
|
|
SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pointerlocker.h"
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QCursor>
|
2021-07-23 19:28:22 +01:00
|
|
|
#include <QGuiApplication>
|
|
|
|
#include <QQmlContext>
|
|
|
|
#include <QQmlEngine>
|
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QScopedPointer>
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void AbstractPointerLocker::setWindow(QWindow *window)
|
2021-07-23 19:28:22 +01:00
|
|
|
{
|
|
|
|
if (m_window == window) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_window = window;
|
|
|
|
Q_EMIT windowChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
PointerLockerQt::PointerLockerQt(QObject *parent)
|
|
|
|
: AbstractPointerLocker(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
PointerLockerQt::~PointerLockerQt() = default;
|
|
|
|
|
|
|
|
void PointerLockerQt::setLocked(bool lock)
|
|
|
|
{
|
2021-08-04 18:35:14 +01:00
|
|
|
if (m_isLocked == lock) {
|
2021-07-23 19:28:22 +01:00
|
|
|
return;
|
|
|
|
}
|
2021-08-04 18:35:14 +01:00
|
|
|
m_isLocked = lock;
|
2021-07-23 19:28:22 +01:00
|
|
|
|
|
|
|
if (lock) {
|
|
|
|
/* Cursor needs to be hidden such that Xwayland emulates warps. */
|
|
|
|
QGuiApplication::setOverrideCursor(QCursor(Qt::BlankCursor));
|
|
|
|
m_originalPosition = QCursor::pos();
|
|
|
|
m_window->installEventFilter(this);
|
|
|
|
Q_EMIT lockedChanged(true);
|
|
|
|
Q_EMIT lockEffectiveChanged(true);
|
|
|
|
} else {
|
|
|
|
m_window->removeEventFilter(this);
|
|
|
|
QGuiApplication::restoreOverrideCursor();
|
|
|
|
Q_EMIT lockedChanged(false);
|
|
|
|
Q_EMIT lockEffectiveChanged(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PointerLockerQt::isLocked() const
|
|
|
|
{
|
2021-08-04 18:35:14 +01:00
|
|
|
return m_isLocked;
|
2021-07-23 19:28:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PointerLockerQt::eventFilter(QObject *watched, QEvent *event)
|
|
|
|
{
|
|
|
|
if (watched != m_window || event->type() != QEvent::MouseMove || !isLocked()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto newPos = QCursor::pos();
|
|
|
|
const QPointF dist = newPos - m_originalPosition;
|
2022-09-10 22:23:52 +01:00
|
|
|
Q_EMIT pointerMoved({dist.x(), dist.y()});
|
2021-07-23 19:28:22 +01:00
|
|
|
QCursor::setPos(m_originalPosition);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|