1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #include "qevdevmousemanager_p.h"
41
42 #include <QtInputSupport/private/qevdevutil_p.h>
43
44 #include <QStringList>
45 #include <QGuiApplication>
46 #include <QScreen>
47 #include <QLoggingCategory>
48 #include <qpa/qwindowsysteminterface.h>
49 #include <QtDeviceDiscoverySupport/private/qdevicediscovery_p.h>
50 #include <private/qguiapplication_p.h>
51 #include <private/qinputdevicemanager_p_p.h>
52 #include <private/qhighdpiscaling_p.h>
53
54 QT_BEGIN_NAMESPACE
55
Q_DECLARE_LOGGING_CATEGORY(qLcEvdevMouse)56 Q_DECLARE_LOGGING_CATEGORY(qLcEvdevMouse)
57
58 QEvdevMouseManager::QEvdevMouseManager(const QString &key, const QString &specification, QObject *parent)
59 : QObject(parent), m_x(0), m_y(0), m_xoffset(0), m_yoffset(0)
60 {
61 Q_UNUSED(key);
62
63 QString spec = QString::fromLocal8Bit(qgetenv("QT_QPA_EVDEV_MOUSE_PARAMETERS"));
64
65 if (spec.isEmpty())
66 spec = specification;
67
68 auto parsed = QEvdevUtil::parseSpecification(spec);
69 m_spec = std::move(parsed.spec);
70
71 for (const QStringRef &arg : qAsConst(parsed.args)) {
72 if (arg.startsWith(QLatin1String("xoffset="))) {
73 m_xoffset = arg.mid(8).toInt();
74 } else if (arg.startsWith(QLatin1String("yoffset="))) {
75 m_yoffset = arg.mid(8).toInt();
76 }
77 }
78
79 // add all mice for devices specified in the argument list
80 for (const QString &device : qAsConst(parsed.devices))
81 addMouse(device);
82
83 if (parsed.devices.isEmpty()) {
84 qCDebug(qLcEvdevMouse, "evdevmouse: Using device discovery");
85 if (auto deviceDiscovery = QDeviceDiscovery::create(QDeviceDiscovery::Device_Mouse | QDeviceDiscovery::Device_Touchpad, this)) {
86 // scan and add already connected keyboards
87 const QStringList devices = deviceDiscovery->scanConnectedDevices();
88 for (const QString &device : devices)
89 addMouse(device);
90
91 connect(deviceDiscovery, &QDeviceDiscovery::deviceDetected,
92 this, &QEvdevMouseManager::addMouse);
93 connect(deviceDiscovery, &QDeviceDiscovery::deviceRemoved,
94 this, &QEvdevMouseManager::removeMouse);
95 }
96 }
97
98 QInputDeviceManager *manager = QGuiApplicationPrivate::inputDeviceManager();
99 connect(manager, &QInputDeviceManager::cursorPositionChangeRequested, [this](const QPoint &pos) {
100 m_x = pos.x();
101 m_y = pos.y();
102 clampPosition();
103 });
104 }
105
~QEvdevMouseManager()106 QEvdevMouseManager::~QEvdevMouseManager()
107 {
108 }
109
clampPosition()110 void QEvdevMouseManager::clampPosition()
111 {
112 // clamp to screen geometry
113 QScreen *primaryScreen = QGuiApplication::primaryScreen();
114 QRect g = QHighDpi::toNativePixels(primaryScreen->virtualGeometry(), primaryScreen);
115 if (m_x + m_xoffset < g.left())
116 m_x = g.left() - m_xoffset;
117 else if (m_x + m_xoffset > g.right())
118 m_x = g.right() - m_xoffset;
119
120 if (m_y + m_yoffset < g.top())
121 m_y = g.top() - m_yoffset;
122 else if (m_y + m_yoffset > g.bottom())
123 m_y = g.bottom() - m_yoffset;
124 }
125
handleMouseEvent(int x,int y,bool abs,Qt::MouseButtons buttons,Qt::MouseButton button,QEvent::Type type)126 void QEvdevMouseManager::handleMouseEvent(int x, int y, bool abs, Qt::MouseButtons buttons,
127 Qt::MouseButton button, QEvent::Type type)
128 {
129 // update current absolute coordinates
130 if (!abs) {
131 m_x += x;
132 m_y += y;
133 } else {
134 m_x = x;
135 m_y = y;
136 }
137
138 clampPosition();
139
140 QPoint pos(m_x + m_xoffset, m_y + m_yoffset);
141 // Cannot track the keyboard modifiers ourselves here. Instead, report the
142 // modifiers from the last key event that has been seen by QGuiApplication.
143 QWindowSystemInterface::handleMouseEvent(0, pos, pos, buttons, button, type, QGuiApplicationPrivate::inputDeviceManager()->keyboardModifiers());
144 }
145
handleWheelEvent(QPoint delta)146 void QEvdevMouseManager::handleWheelEvent(QPoint delta)
147 {
148 QPoint pos(m_x + m_xoffset, m_y + m_yoffset);
149 QWindowSystemInterface::handleWheelEvent(0, pos, pos, QPoint(), delta, QGuiApplicationPrivate::inputDeviceManager()->keyboardModifiers());
150 }
151
addMouse(const QString & deviceNode)152 void QEvdevMouseManager::addMouse(const QString &deviceNode)
153 {
154 qCDebug(qLcEvdevMouse, "Adding mouse at %ls", qUtf16Printable(deviceNode));
155 auto handler = QEvdevMouseHandler::create(deviceNode, m_spec);
156 if (handler) {
157 connect(handler.get(), &QEvdevMouseHandler::handleMouseEvent,
158 this, &QEvdevMouseManager::handleMouseEvent);
159 connect(handler.get(), &QEvdevMouseHandler::handleWheelEvent,
160 this, &QEvdevMouseManager::handleWheelEvent);
161 m_mice.add(deviceNode, std::move(handler));
162 updateDeviceCount();
163 } else {
164 qWarning("evdevmouse: Failed to open mouse device %ls", qUtf16Printable(deviceNode));
165 }
166 }
167
removeMouse(const QString & deviceNode)168 void QEvdevMouseManager::removeMouse(const QString &deviceNode)
169 {
170 if (m_mice.remove(deviceNode)) {
171 qCDebug(qLcEvdevMouse, "Removing mouse at %ls", qUtf16Printable(deviceNode));
172 updateDeviceCount();
173 }
174 }
175
updateDeviceCount()176 void QEvdevMouseManager::updateDeviceCount()
177 {
178 QInputDeviceManagerPrivate::get(QGuiApplicationPrivate::inputDeviceManager())->setDeviceCount(
179 QInputDeviceManager::DeviceTypePointer, m_mice.count());
180 }
181
182 QT_END_NAMESPACE
183