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 "qevdevmousehandler_p.h"
5
6#include <QSocketNotifier>
7#include <QStringList>
8#include <QPoint>
9#include <QGuiApplication>
10#include <QScreen>
11#include <QLoggingCategory>
12#include <qpa/qwindowsysteminterface.h>
13
14#include <qplatformdefs.h>
15#include <private/qcore_unix_p.h> // overrides QT_OPEN
16#include <private/qhighdpiscaling_p.h>
17
18#include <errno.h>
19
20#ifdef Q_OS_FREEBSD
21#include <dev/evdev/input.h>
22#else
23#include <linux/kd.h>
24#include <linux/input.h>
25#endif
26
27#define TEST_BIT(array, bit) (array[bit/8] & (1<<(bit%8)))
28
29QT_BEGIN_NAMESPACE
30
31using namespace Qt::StringLiterals;
32
33Q_LOGGING_CATEGORY(qLcEvdevMouse, "qt.qpa.input")
34
35std::unique_ptr<QEvdevMouseHandler> QEvdevMouseHandler::create(const QString &device, const QString &specification)
36{
37 qCDebug(qLcEvdevMouse) << "create mouse handler for" << device << specification;
38
39 bool compression = true;
40 int jitterLimit = 0;
41 int grab = 0;
42 bool abs = false;
43
44 const auto args = QStringView{specification}.split(sep: u':');
45 for (const auto &arg : args) {
46 if (arg == "nocompress"_L1)
47 compression = false;
48 else if (arg.startsWith(s: "dejitter="_L1))
49 jitterLimit = arg.mid(pos: 9).toInt();
50 else if (arg.startsWith(s: "grab="_L1))
51 grab = arg.mid(pos: 5).toInt();
52 else if (arg == "abs"_L1)
53 abs = true;
54 }
55
56 int fd;
57 fd = qt_safe_open(pathname: device.toLocal8Bit().constData(), O_RDONLY | O_NDELAY, mode: 0);
58 if (fd >= 0) {
59 ::ioctl(fd: fd, EVIOCGRAB, grab);
60 return std::unique_ptr<QEvdevMouseHandler>(new QEvdevMouseHandler(device, fd, abs, compression, jitterLimit));
61 } else {
62 qErrnoWarning(errno, msg: "Cannot open mouse input device %s", qPrintable(device));
63 return nullptr;
64 }
65}
66
67QEvdevMouseHandler::QEvdevMouseHandler(const QString &device, int fd, bool abs, bool compression, int jitterLimit)
68 : m_device(device), m_fd(fd), m_abs(abs), m_compression(compression)
69{
70 setObjectName("Evdev Mouse Handler"_L1);
71
72 m_jitterLimitSquared = jitterLimit * jitterLimit;
73
74 // Some touch screens present as mice with absolute coordinates.
75 // These can not be differentiated from touchpads, so supplying abs to QT_QPA_EVDEV_MOUSE_PARAMETERS
76 // will force qevdevmousehandler to treat the coordinates as absolute, scaled to the hardware maximums.
77 // Turning this on will not affect mice as these do not report in absolute coordinates
78 // but will make touchpads act like touch screens
79 if (m_abs)
80 m_abs = getHardwareMaximum();
81
82 detectHiResWheelSupport();
83
84 // socket notifier for events on the mouse device
85 m_notify = new QSocketNotifier(m_fd, QSocketNotifier::Read, this);
86 connect(sender: m_notify, signal: &QSocketNotifier::activated,
87 context: this, slot: &QEvdevMouseHandler::readMouseData);
88}
89
90QEvdevMouseHandler::~QEvdevMouseHandler()
91{
92 if (m_fd >= 0)
93 qt_safe_close(fd: m_fd);
94}
95
96void QEvdevMouseHandler::detectHiResWheelSupport()
97{
98#if defined(REL_WHEEL_HI_RES) || defined(REL_HWHEEL_HI_RES)
99 // Check if we can expect hires events as we will get both
100 // legacy and hires event and needs to know if we should
101 // ignore the legacy events.
102 unsigned char relFeatures[(REL_MAX / 8) + 1]{};
103 if (ioctl(fd: m_fd, EVIOCGBIT(EV_REL, sizeof (relFeatures)), relFeatures) == -1)
104 return;
105
106#if defined(REL_WHEEL_HI_RES)
107 m_hiResWheel = TEST_BIT(relFeatures, REL_WHEEL_HI_RES);
108#endif
109#if defined(REL_HWHEEL_HI_RES)
110 m_hiResHWheel = TEST_BIT(relFeatures, REL_HWHEEL_HI_RES);
111#endif
112#endif
113}
114
115// Ask touch screen hardware for information on coordinate maximums
116// If any ioctls fail, revert to non abs mode
117bool QEvdevMouseHandler::getHardwareMaximum()
118{
119 unsigned char absFeatures[(ABS_MAX / 8) + 1];
120 memset(s: absFeatures, c: '\0', n: sizeof (absFeatures));
121
122 // test if ABS_X, ABS_Y are available
123 if (ioctl(fd: m_fd, EVIOCGBIT(EV_ABS, sizeof (absFeatures)), absFeatures) == -1)
124 return false;
125
126 if ((!TEST_BIT(absFeatures, ABS_X)) || (!TEST_BIT(absFeatures, ABS_Y)))
127 return false;
128
129 // ask hardware for minimum and maximum values
130 struct input_absinfo absInfo;
131 if (ioctl(fd: m_fd, EVIOCGABS(ABS_X), &absInfo) == -1)
132 return false;
133
134 m_hardwareWidth = absInfo.maximum - absInfo.minimum;
135
136 if (ioctl(fd: m_fd, EVIOCGABS(ABS_Y), &absInfo) == -1)
137 return false;
138
139 m_hardwareHeight = absInfo.maximum - absInfo.minimum;
140
141 QScreen *primaryScreen = QGuiApplication::primaryScreen();
142 QRect g = QHighDpi::toNativePixels(value: primaryScreen->virtualGeometry(), context: primaryScreen);
143 m_hardwareScalerX = static_cast<qreal>(m_hardwareWidth) / (g.right() - g.left());
144 m_hardwareScalerY = static_cast<qreal>(m_hardwareHeight) / (g.bottom() - g.top());
145
146 qCDebug(qLcEvdevMouse) << "Absolute pointing device"
147 << "hardware max x" << m_hardwareWidth
148 << "hardware max y" << m_hardwareHeight
149 << "hardware scalers x" << m_hardwareScalerX << 'y' << m_hardwareScalerY;
150
151 return true;
152}
153
154void QEvdevMouseHandler::sendMouseEvent()
155{
156 int x;
157 int y;
158
159 if (!m_abs) {
160 x = m_x - m_prevx;
161 y = m_y - m_prevy;
162 }
163 else {
164 x = m_x / m_hardwareScalerX;
165 y = m_y / m_hardwareScalerY;
166 }
167
168 if (m_prevInvalid) {
169 x = y = 0;
170 m_prevInvalid = false;
171 }
172
173 if (m_eventType == QEvent::MouseMove)
174 emit handleMouseEvent(x, y, abs: m_abs, buttons: m_buttons, button: Qt::NoButton, type: m_eventType);
175 else
176 emit handleMouseEvent(x, y, abs: m_abs, buttons: m_buttons, button: m_button, type: m_eventType);
177
178 m_prevx = m_x;
179 m_prevy = m_y;
180}
181
182void QEvdevMouseHandler::readMouseData()
183{
184 struct ::input_event buffer[32];
185 int n = 0;
186 bool posChanged = false, btnChanged = false;
187 bool pendingMouseEvent = false;
188 forever {
189 int result = QT_READ(fd: m_fd, data: reinterpret_cast<char *>(buffer) + n, maxlen: sizeof(buffer) - n);
190
191 if (result == 0) {
192 qWarning(msg: "evdevmouse: Got EOF from the input device");
193 return;
194 } else if (result < 0) {
195 if (errno != EINTR && errno != EAGAIN) {
196 qErrnoWarning(errno, msg: "evdevmouse: Could not read from input device");
197 // If the device got disconnected, stop reading, otherwise we get flooded
198 // by the above error over and over again.
199 if (errno == ENODEV) {
200 delete m_notify;
201 m_notify = nullptr;
202 qt_safe_close(fd: m_fd);
203 m_fd = -1;
204 }
205 return;
206 }
207 } else {
208 n += result;
209 if (n % sizeof(buffer[0]) == 0)
210 break;
211 }
212 }
213
214 n /= sizeof(buffer[0]);
215
216 for (int i = 0; i < n; ++i) {
217 struct ::input_event *data = &buffer[i];
218 if (data->type == EV_ABS) {
219 // Touchpads: store the absolute position for now, will calculate a relative one later.
220 if (data->code == ABS_X && m_x != data->value) {
221 m_x = data->value;
222 posChanged = true;
223 } else if (data->code == ABS_Y && m_y != data->value) {
224 m_y = data->value;
225 posChanged = true;
226 }
227 } else if (data->type == EV_REL) {
228 QPoint delta;
229 if (data->code == REL_X) {
230 m_x += data->value;
231 posChanged = true;
232 } else if (data->code == REL_Y) {
233 m_y += data->value;
234 posChanged = true;
235 } else if (!m_hiResWheel && data->code == REL_WHEEL) {
236 // data->value: positive == up, negative == down
237 delta.setY(120 * data->value);
238 emit handleWheelEvent(delta);
239#ifdef REL_WHEEL_HI_RES
240 } else if (data->code == REL_WHEEL_HI_RES) {
241 delta.setY(data->value);
242 emit handleWheelEvent(delta);
243#endif
244 } else if (!m_hiResHWheel && data->code == REL_HWHEEL) {
245 // data->value: positive == right, negative == left
246 delta.setX(-120 * data->value);
247 emit handleWheelEvent(delta);
248#ifdef REL_HWHEEL_HI_RES
249 } else if (data->code == REL_HWHEEL_HI_RES) {
250 delta.setX(-data->value);
251 emit handleWheelEvent(delta);
252#endif
253 }
254 } else if (data->type == EV_KEY && data->code == BTN_TOUCH) {
255 // We care about touchpads only, not touchscreens -> don't map to button press.
256 // Need to invalidate prevx/y however to get proper relative pos.
257 m_prevInvalid = true;
258 } else if (data->type == EV_KEY && data->code >= BTN_LEFT && data->code <= BTN_JOYSTICK) {
259 Qt::MouseButton button = Qt::NoButton;
260 // BTN_LEFT == 0x110 in kernel's input.h
261 // The range of possible mouse buttons ends just before BTN_JOYSTICK, value 0x120.
262 switch (data->code) {
263 case 0x110: button = Qt::LeftButton; break; // BTN_LEFT
264 case 0x111: button = Qt::RightButton; break;
265 case 0x112: button = Qt::MiddleButton; break;
266 case 0x113: button = Qt::ExtraButton1; break; // AKA Qt::BackButton
267 case 0x114: button = Qt::ExtraButton2; break; // AKA Qt::ForwardButton
268 case 0x115: button = Qt::ExtraButton3; break; // AKA Qt::TaskButton
269 case 0x116: button = Qt::ExtraButton4; break;
270 case 0x117: button = Qt::ExtraButton5; break;
271 case 0x118: button = Qt::ExtraButton6; break;
272 case 0x119: button = Qt::ExtraButton7; break;
273 case 0x11a: button = Qt::ExtraButton8; break;
274 case 0x11b: button = Qt::ExtraButton9; break;
275 case 0x11c: button = Qt::ExtraButton10; break;
276 case 0x11d: button = Qt::ExtraButton11; break;
277 case 0x11e: button = Qt::ExtraButton12; break;
278 case 0x11f: button = Qt::ExtraButton13; break;
279 }
280 m_buttons.setFlag(flag: button, on: data->value);
281 m_button = button;
282 m_eventType = data->value == 0 ? QEvent::MouseButtonRelease : QEvent::MouseButtonPress;
283 btnChanged = true;
284 } else if (data->type == EV_SYN && data->code == SYN_REPORT) {
285 if (btnChanged) {
286 btnChanged = posChanged = false;
287 sendMouseEvent();
288 pendingMouseEvent = false;
289 } else if (posChanged) {
290 m_eventType = QEvent::MouseMove;
291 posChanged = false;
292 if (m_compression) {
293 pendingMouseEvent = true;
294 } else {
295 sendMouseEvent();
296 }
297 }
298 } else if (data->type == EV_MSC && data->code == MSC_SCAN) {
299 // kernel encountered an unmapped key - just ignore it
300 continue;
301 }
302 }
303 if (m_compression && pendingMouseEvent) {
304 int distanceSquared = (m_x - m_prevx)*(m_x - m_prevx) + (m_y - m_prevy)*(m_y - m_prevy);
305 if (distanceSquared > m_jitterLimitSquared)
306 sendMouseEvent();
307 }
308}
309
310QT_END_NAMESPACE
311
312#include "moc_qevdevmousehandler_p.cpp"
313

source code of qtbase/src/platformsupport/input/evdevmouse/qevdevmousehandler.cpp