| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qevdevmousemanager_p.h" |
| 5 | |
| 6 | #include <QtInputSupport/private/qevdevutil_p.h> |
| 7 | |
| 8 | #include <QStringList> |
| 9 | #include <QGuiApplication> |
| 10 | #include <QScreen> |
| 11 | #include <QLoggingCategory> |
| 12 | #include <qpa/qwindowsysteminterface.h> |
| 13 | #include <QtDeviceDiscoverySupport/private/qdevicediscovery_p.h> |
| 14 | #include <private/qguiapplication_p.h> |
| 15 | #include <private/qinputdevicemanager_p_p.h> |
| 16 | #include <private/qhighdpiscaling_p.h> |
| 17 | |
| 18 | QT_BEGIN_NAMESPACE |
| 19 | |
| 20 | using namespace Qt::StringLiterals; |
| 21 | |
| 22 | QEvdevMouseManager::QEvdevMouseManager(const QString &key, const QString &specification, QObject *parent) |
| 23 | : QObject(parent), m_x(0), m_y(0), m_xoffset(0), m_yoffset(0) |
| 24 | { |
| 25 | Q_UNUSED(key); |
| 26 | |
| 27 | QString spec = qEnvironmentVariable(varName: "QT_QPA_EVDEV_MOUSE_PARAMETERS" ); |
| 28 | |
| 29 | if (spec.isEmpty()) |
| 30 | spec = specification; |
| 31 | |
| 32 | auto parsed = QEvdevUtil::parseSpecification(specification: spec); |
| 33 | m_spec = std::move(parsed.spec); |
| 34 | |
| 35 | for (const auto &arg : std::as_const(t&: parsed.args)) { |
| 36 | if (arg.startsWith(s: "xoffset="_L1 )) { |
| 37 | m_xoffset = arg.mid(pos: 8).toInt(); |
| 38 | } else if (arg.startsWith(s: "yoffset="_L1 )) { |
| 39 | m_yoffset = arg.mid(pos: 8).toInt(); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // add all mice for devices specified in the argument list |
| 44 | for (const QString &device : std::as_const(t&: parsed.devices)) |
| 45 | addMouse(deviceNode: device); |
| 46 | |
| 47 | if (parsed.devices.isEmpty()) { |
| 48 | qCDebug(qLcEvdevMouse, "evdevmouse: Using device discovery" ); |
| 49 | if (auto deviceDiscovery = QDeviceDiscovery::create(type: QDeviceDiscovery::Device_Mouse | QDeviceDiscovery::Device_Touchpad, parent: this)) { |
| 50 | // scan and add already connected keyboards |
| 51 | const QStringList devices = deviceDiscovery->scanConnectedDevices(); |
| 52 | for (const QString &device : devices) |
| 53 | addMouse(deviceNode: device); |
| 54 | |
| 55 | connect(sender: deviceDiscovery, signal: &QDeviceDiscovery::deviceDetected, |
| 56 | context: this, slot: &QEvdevMouseManager::addMouse); |
| 57 | connect(sender: deviceDiscovery, signal: &QDeviceDiscovery::deviceRemoved, |
| 58 | context: this, slot: &QEvdevMouseManager::removeMouse); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | QInputDeviceManager *manager = QGuiApplicationPrivate::inputDeviceManager(); |
| 63 | connect(sender: manager, signal: &QInputDeviceManager::cursorPositionChangeRequested, context: this, slot: [this](const QPoint &pos) { |
| 64 | m_x = pos.x(); |
| 65 | m_y = pos.y(); |
| 66 | clampPosition(); |
| 67 | }); |
| 68 | } |
| 69 | |
| 70 | QEvdevMouseManager::~QEvdevMouseManager() |
| 71 | { |
| 72 | } |
| 73 | |
| 74 | void QEvdevMouseManager::clampPosition() |
| 75 | { |
| 76 | // clamp to screen geometry |
| 77 | QScreen *primaryScreen = QGuiApplication::primaryScreen(); |
| 78 | QRect g = QHighDpi::toNativePixels(value: primaryScreen->virtualGeometry(), context: primaryScreen); |
| 79 | if (m_x + m_xoffset < g.left()) |
| 80 | m_x = g.left() - m_xoffset; |
| 81 | else if (m_x + m_xoffset > g.right()) |
| 82 | m_x = g.right() - m_xoffset; |
| 83 | |
| 84 | if (m_y + m_yoffset < g.top()) |
| 85 | m_y = g.top() - m_yoffset; |
| 86 | else if (m_y + m_yoffset > g.bottom()) |
| 87 | m_y = g.bottom() - m_yoffset; |
| 88 | } |
| 89 | |
| 90 | void QEvdevMouseManager::handleMouseEvent(int x, int y, bool abs, Qt::MouseButtons buttons, |
| 91 | Qt::MouseButton button, QEvent::Type type) |
| 92 | { |
| 93 | // update current absolute coordinates |
| 94 | if (!abs) { |
| 95 | m_x += x; |
| 96 | m_y += y; |
| 97 | } else { |
| 98 | m_x = x; |
| 99 | m_y = y; |
| 100 | } |
| 101 | |
| 102 | clampPosition(); |
| 103 | |
| 104 | QPoint pos(m_x + m_xoffset, m_y + m_yoffset); |
| 105 | // Cannot track the keyboard modifiers ourselves here. Instead, report the |
| 106 | // modifiers from the last key event that has been seen by QGuiApplication. |
| 107 | QWindowSystemInterface::handleMouseEvent(window: 0, local: pos, global: pos, state: buttons, button, type, mods: QGuiApplicationPrivate::inputDeviceManager()->keyboardModifiers()); |
| 108 | } |
| 109 | |
| 110 | void QEvdevMouseManager::handleWheelEvent(QPoint delta) |
| 111 | { |
| 112 | QPoint pos(m_x + m_xoffset, m_y + m_yoffset); |
| 113 | QWindowSystemInterface::handleWheelEvent(window: 0, local: pos, global: pos, pixelDelta: QPoint(), angleDelta: delta, mods: QGuiApplicationPrivate::inputDeviceManager()->keyboardModifiers()); |
| 114 | } |
| 115 | |
| 116 | void QEvdevMouseManager::addMouse(const QString &deviceNode) |
| 117 | { |
| 118 | qCDebug(qLcEvdevMouse, "Adding mouse at %ls" , qUtf16Printable(deviceNode)); |
| 119 | auto handler = QEvdevMouseHandler::create(device: deviceNode, specification: m_spec); |
| 120 | if (handler) { |
| 121 | connect(sender: handler.get(), signal: &QEvdevMouseHandler::handleMouseEvent, |
| 122 | context: this, slot: &QEvdevMouseManager::handleMouseEvent); |
| 123 | connect(sender: handler.get(), signal: &QEvdevMouseHandler::handleWheelEvent, |
| 124 | context: this, slot: &QEvdevMouseManager::handleWheelEvent); |
| 125 | m_mice.add(deviceNode, handler: std::move(handler)); |
| 126 | updateDeviceCount(); |
| 127 | } else { |
| 128 | qWarning(msg: "evdevmouse: Failed to open mouse device %ls" , qUtf16Printable(deviceNode)); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | void QEvdevMouseManager::removeMouse(const QString &deviceNode) |
| 133 | { |
| 134 | if (m_mice.remove(deviceNode)) { |
| 135 | qCDebug(qLcEvdevMouse, "Removing mouse at %ls" , qUtf16Printable(deviceNode)); |
| 136 | updateDeviceCount(); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | void QEvdevMouseManager::updateDeviceCount() |
| 141 | { |
| 142 | QInputDeviceManagerPrivate::get(mgr: QGuiApplicationPrivate::inputDeviceManager())->setDeviceCount( |
| 143 | type: QInputDeviceManager::DeviceTypePointer, count: m_mice.count()); |
| 144 | } |
| 145 | |
| 146 | QT_END_NAMESPACE |
| 147 | |