File: pointerlocker.cpp

package info (click to toggle)
kdeconnect 25.04.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,584 kB
  • sloc: cpp: 22,922; xml: 520; python: 92; sh: 25; makefile: 5
file content (74 lines) | stat: -rw-r--r-- 1,807 bytes parent folder | download
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
/*
    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"

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

#include <QDebug>

void AbstractPointerLocker::setWindow(QWindow *window)
{
    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)
{
    if (m_isLocked == lock) {
        return;
    }
    m_isLocked = lock;

    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
{
    return m_isLocked;
}

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;
    Q_EMIT pointerMoved({dist.x(), dist.y()});
    QCursor::setPos(m_originalPosition);

    return true;
}

#include "moc_pointerlocker.cpp"