Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qvxmousehandler.cpp
Go to the documentation of this file.
1
2// Copyright (C) 2025 The Qt Company Ltd.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
6
7#include <QSocketNotifier>
8#include <QStringList>
9#include <QPoint>
10#include <QGuiApplication>
11#include <QScreen>
12#include <QLoggingCategory>
13#include <qpa/qwindowsysteminterface.h>
14
15#include <qplatformdefs.h>
16#include <private/qcore_unix_p.h> // overrides QT_OPEN
17
18#include <errno.h>
19#include <evdevLib.h>
20
21QT_BEGIN_NAMESPACE
22
23using namespace Qt::StringLiterals;
24
25Q_LOGGING_CATEGORY(qLcVxMouse, "qt.qpa.input")
26
27std::unique_ptr<QVxMouseHandler> QVxMouseHandler::create(const QString &device, const QString &specification)
28{
29 qCDebug(qLcVxMouse) << "create mouse handler for" << device << specification;
30
31 bool compression = true;
32 int jitterLimit = 0;
33 int grab = 0;
34 bool abs = false;
35
36 const auto args = QStringView{specification}.split(u':');
37 for (const auto &arg : args) {
38 if (arg == "nocompress"_L1)
39 compression = false;
40 else if (arg.startsWith("dejitter="_L1))
41 jitterLimit = arg.mid(9).toInt();
42 else if (arg.startsWith("grab="_L1))
43 grab = arg.mid(5).toInt();
44 }
45
46 int fd = qt_safe_open(device.toLocal8Bit().constData(), O_RDONLY | O_NONBLOCK, 0);
47 if (fd >= 0) {
48 return std::unique_ptr<QVxMouseHandler>(new QVxMouseHandler(device, fd, compression, jitterLimit));
49 } else {
50 qCWarning(qLcVxMouse) << "Cannot open mouse input device" << qPrintable(device) << ":" << strerror(errno);
51 return nullptr;
52 }
53}
54
55QVxMouseHandler::QVxMouseHandler(const QString &device, int fd, bool compression, int jitterLimit)
56 : m_device(device), m_fd(fd), m_compression(compression)
57{
58 setObjectName("Evdev Mouse Handler"_L1);
59
60 m_jitterLimitSquared = jitterLimit * jitterLimit;
61
62 // socket notifier for events on the mouse device
63 m_notify = new QSocketNotifier(m_fd, QSocketNotifier::Read, this);
64 connect(m_notify, &QSocketNotifier::activated,
66}
67
69{
70 if (m_fd >= 0)
71 qt_safe_close(m_fd);
72}
73
74void QVxMouseHandler::sendMouseEvent()
75{
76 int x;
77 int y;
78
79 x = m_x - m_prevx;
80 y = m_y - m_prevy;
81 if (m_prevInvalid) {
82 x = y = 0;
83 m_prevInvalid = false;
84 }
85
86 if (m_eventType == QEvent::MouseMove)
87 emit handleMouseEvent(x, y, m_buttons, Qt::NoButton, m_eventType);
88 else
89 emit handleMouseEvent(x, y, m_buttons, m_button, m_eventType);
90
91 m_prevx = m_x;
92 m_prevy = m_y;
93}
94
96{
97 EV_DEV_EVENT buffer[32];
98 int n = 0;
99 bool posChanged = false, btnChanged = false;
100 bool pendingMouseEvent = false;
101 forever {
102 int result = QT_READ(m_fd, reinterpret_cast<char *>(buffer) + n, sizeof(buffer) - n);
103
104 if (result == 0) {
105 qCWarning(qLcVxMouse)<<"evdevmouse: Got EOF from the input device";
106 return;
107 } else if (result < 0) {
108 if (errno != EINTR && errno != EAGAIN) {
109 qCWarning(qLcVxMouse)<<"evdevmouse: Could not read from input device"<<errno;
110 // If the device got disconnected, stop reading, otherwise we get flooded
111 // by the above error over and over again.
112 if (errno == ENODEV) {
113 delete m_notify;
114 m_notify = nullptr;
115 qt_safe_close(m_fd);
116 m_fd = -1;
117 }
118 return;
119 }
120 } else {
121 n += result;
122 if (n % sizeof(buffer[0]) == 0)
123 break;
124 }
125 }
126
127 n /= sizeof(buffer[0]);
128
129 for (int i = 0; i < n; ++i) {
130 EV_DEV_EVENT *data = &buffer[i];
131 if (data->type == EV_DEV_REL) {
132 if (data->code == EV_DEV_PTR_REL_X) {
133 m_x += data->value;
134 posChanged = true;
135 } else if (data->code == EV_DEV_PTR_REL_Y) {
136 m_y += data->value;
137 posChanged = true;
138 }
139 } else if (data->type == EV_DEV_KEY && data->code >= EV_DEV_PTR_BTN_LEFT) {
140 Qt::MouseButton button = Qt::NoButton;
141 // BTN_LEFT == 0x110 in kernel's input.h
142 // The range of possible mouse buttons ends just before BTN_JOYSTICK, value 0x120.
143 switch (data->code) {
144 case 0x110: button = Qt::LeftButton; break; // BTN_LEFT
145 case 0x111: button = Qt::RightButton; break;
146 case 0x112: button = Qt::MiddleButton; break;
147 case 0x113: button = Qt::ExtraButton1; break; // AKA Qt::BackButton
148 case 0x114: button = Qt::ExtraButton2; break; // AKA Qt::ForwardButton
149 case 0x115: button = Qt::ExtraButton3; break; // AKA Qt::TaskButton
150 case 0x116: button = Qt::ExtraButton4; break;
151 case 0x117: button = Qt::ExtraButton5; break;
152 case 0x118: button = Qt::ExtraButton6; break;
153 case 0x119: button = Qt::ExtraButton7; break;
154 case 0x11a: button = Qt::ExtraButton8; break;
155 case 0x11b: button = Qt::ExtraButton9; break;
156 case 0x11c: button = Qt::ExtraButton10; break;
157 case 0x11d: button = Qt::ExtraButton11; break;
158 case 0x11e: button = Qt::ExtraButton12; break;
159 case 0x11f: button = Qt::ExtraButton13; break;
160 }
161 m_buttons.setFlag(button, data->value);
162 m_button = button;
163 m_eventType = data->value == 0 ? QEvent::MouseButtonRelease : QEvent::MouseButtonPress;
164 btnChanged = true;
165 } else if (data->type == EV_DEV_SYN) {
166 if (btnChanged) {
167 btnChanged = posChanged = false;
168 sendMouseEvent();
169 pendingMouseEvent = false;
170 } else if (posChanged) {
171 m_eventType = QEvent::MouseMove;
172 posChanged = false;
173 if (m_compression) {
174 pendingMouseEvent = true;
175 } else {
176 sendMouseEvent();
177 }
178 }
179 }
180 }
181 if (m_compression && pendingMouseEvent) {
182 int distanceSquared = (m_x - m_prevx)*(m_x - m_prevx) + (m_y - m_prevy)*(m_y - m_prevy);
183 if (distanceSquared > m_jitterLimitSquared)
184 sendMouseEvent();
185 }
186}
187
188QT_END_NAMESPACE
189
190#include "moc_qvxmousehandler_p.cpp"
Q_LOGGING_CATEGORY(lcEventDispatcher, "qt.eventdispatcher")