qevdevmousehandler.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/platformsupport/input/evdevmouse/qevdevmousehandler.cpp
Source codeSwitch to Preprocessed file
LineSourceCount
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 "qevdevmousehandler_p.h"-
41-
42#include <QSocketNotifier>-
43#include <QStringList>-
44#include <QPoint>-
45#include <QGuiApplication>-
46#include <QScreen>-
47#include <QLoggingCategory>-
48#include <qpa/qwindowsysteminterface.h>-
49-
50#include <qplatformdefs.h>-
51#include <private/qcore_unix_p.h> // overrides QT_OPEN-
52#include <private/qhighdpiscaling_p.h>-
53-
54#include <errno.h>-
55-
56#include <linux/kd.h>-
57#include <linux/input.h>-
58-
59#define TEST_BIT(array, bit) (array[bit/8] & (1<<(bit%8)))-
60-
61QT_BEGIN_NAMESPACE-
62-
63Q_LOGGING_CATEGORY(qLcEvdevMouse, "qt.qpa.input")
never executed: return category;
0
64-
65QEvdevMouseHandler *QEvdevMouseHandler::create(const QString &device, const QString &specification)-
66{-
67 qCDebug(qLcEvdevMouse) << "create mouse handler for" << device << specification;
never executed: QMessageLogger(__FILE__, 67, __PRETTY_FUNCTION__, qLcEvdevMouse().categoryName()).debug() << "create mouse handler for" << device << specification;
qt_category_enabledDescription
TRUEnever evaluated
FALSEnever evaluated
0
68-
69 bool compression = true;-
70 int jitterLimit = 0;-
71 int grab = 0;-
72 bool abs = false;-
73-
74 const auto args = specification.splitRef(QLatin1Char(':'));-
75 for (const QStringRef &arg : args) {-
76 if (arg == QLatin1String("nocompress"))
arg == QLatin1...("nocompress")Description
TRUEnever evaluated
FALSEnever evaluated
0
77 compression = false;
never executed: compression = false;
0
78 else if (arg.startsWith(QLatin1String("dejitter=")))
arg.startsWith...("dejitter="))Description
TRUEnever evaluated
FALSEnever evaluated
0
79 jitterLimit = arg.mid(9).toInt();
never executed: jitterLimit = arg.mid(9).toInt();
0
80 else if (arg.startsWith(QLatin1String("grab=")))
arg.startsWith...ring("grab="))Description
TRUEnever evaluated
FALSEnever evaluated
0
81 grab = arg.mid(5).toInt();
never executed: grab = arg.mid(5).toInt();
0
82 else if (arg == QLatin1String("abs"))
arg == QLatin1String("abs")Description
TRUEnever evaluated
FALSEnever evaluated
0
83 abs = true;
never executed: abs = true;
0
84 }
never executed: end of block
0
85-
86 int fd;-
87 fd = qt_safe_open(device.toLocal8Bit().constData(), O_RDONLY | O_NDELAY, 0);-
88 if (fd >= 0) {
fd >= 0Description
TRUEnever evaluated
FALSEnever evaluated
0
89 ::ioctl(fd, EVIOCGRAB, grab);-
90 return new QEvdevMouseHandler(device, fd, abs, compression, jitterLimit);
never executed: return new QEvdevMouseHandler(device, fd, abs, compression, jitterLimit);
0
91 } else {-
92 qErrnoWarning(errno, "Cannot open mouse input device %s", qPrintable(device));-
93 return 0;
never executed: return 0;
0
94 }-
95}-
96-
97QEvdevMouseHandler::QEvdevMouseHandler(const QString &device, int fd, bool abs, bool compression, int jitterLimit)-
98 : m_device(device), m_fd(fd), m_notify(0), m_x(0), m_y(0), m_prevx(0), m_prevy(0),-
99 m_abs(abs), m_compression(compression), m_buttons(0), m_prevInvalid(true)-
100{-
101 setObjectName(QLatin1String("Evdev Mouse Handler"));-
102-
103 m_jitterLimitSquared = jitterLimit * jitterLimit;-
104-
105 // Some touch screens present as mice with absolute coordinates.-
106 // These can not be differentiated from touchpads, so supplying abs to QT_QPA_EVDEV_MOUSE_PARAMETERS-
107 // will force qevdevmousehandler to treat the coordinates as absolute, scaled to the hardware maximums.-
108 // Turning this on will not affect mice as these do not report in absolute coordinates-
109 // but will make touchpads act like touch screens-
110 if (m_abs)
m_absDescription
TRUEnever evaluated
FALSEnever evaluated
0
111 m_abs = getHardwareMaximum();
never executed: m_abs = getHardwareMaximum();
0
112-
113 // socket notifier for events on the mouse device-
114 m_notify = new QSocketNotifier(m_fd, QSocketNotifier::Read, this);-
115 connect(m_notify, SIGNAL(activated(int)), this, SLOT(readMouseData()));-
116}
never executed: end of block
0
117-
118QEvdevMouseHandler::~QEvdevMouseHandler()-
119{-
120 if (m_fd >= 0)
m_fd >= 0Description
TRUEnever evaluated
FALSEnever evaluated
0
121 qt_safe_close(m_fd);
never executed: qt_safe_close(m_fd);
0
122}
never executed: end of block
0
123-
124// Ask touch screen hardware for information on coordinate maximums-
125// If any ioctls fail, revert to non abs mode-
126bool QEvdevMouseHandler::getHardwareMaximum()-
127{-
128 unsigned char absFeatures[(ABS_MAX / 8) + 1];-
129 memset(absFeatures, '\0', sizeof (absFeatures));-
130-
131 // test if ABS_X, ABS_Y are available-
132 if (ioctl(m_fd, EVIOCGBIT(EV_ABS, sizeof (absFeatures)), absFeatures) == -1)
ioctl(m_fd, ((...eatures) == -1Description
TRUEnever evaluated
FALSEnever evaluated
0
133 return false;
never executed: return false;
0
134-
135 if ((!TEST_BIT(absFeatures, ABS_X)) || (!TEST_BIT(absFeatures, ABS_Y)))
(!(absFeatures...1<<(0x00%8))))Description
TRUEnever evaluated
FALSEnever evaluated
(!(absFeatures...1<<(0x01%8))))Description
TRUEnever evaluated
FALSEnever evaluated
0
136 return false;
never executed: return false;
0
137-
138 // ask hardware for minimum and maximum values-
139 struct input_absinfo absInfo;-
140 if (ioctl(m_fd, EVIOCGABS(ABS_X), &absInfo) == -1)
ioctl(m_fd, ((...absInfo) == -1Description
TRUEnever evaluated
FALSEnever evaluated
0
141 return false;
never executed: return false;
0
142-
143 m_hardwareWidth = absInfo.maximum - absInfo.minimum;-
144-
145 if (ioctl(m_fd, EVIOCGABS(ABS_Y), &absInfo) == -1)
ioctl(m_fd, ((...absInfo) == -1Description
TRUEnever evaluated
FALSEnever evaluated
0
146 return false;
never executed: return false;
0
147-
148 m_hardwareHeight = absInfo.maximum - absInfo.minimum;-
149-
150 QScreen *primaryScreen = QGuiApplication::primaryScreen();-
151 QRect g = QHighDpi::toNativePixels(primaryScreen->virtualGeometry(), primaryScreen);-
152 m_hardwareScalerX = static_cast<qreal>(m_hardwareWidth) / (g.right() - g.left());-
153 m_hardwareScalerY = static_cast<qreal>(m_hardwareHeight) / (g.bottom() - g.top());-
154-
155 qCDebug(qLcEvdevMouse) << "Absolute pointing device"
never executed: QMessageLogger(__FILE__, 155, __PRETTY_FUNCTION__, qLcEvdevMouse().categoryName()).debug() << "Absolute pointing device" << "hardware max x" << m_hardwareWidth << "hardware max y" << m_hardwareHeight << "hardware scalers x" << m_hardwareScalerX << 'y' << m_hardwareScalerY;
qt_category_enabledDescription
TRUEnever evaluated
FALSEnever evaluated
0
156 << "hardware max x" << m_hardwareWidth
never executed: QMessageLogger(__FILE__, 155, __PRETTY_FUNCTION__, qLcEvdevMouse().categoryName()).debug() << "Absolute pointing device" << "hardware max x" << m_hardwareWidth << "hardware max y" << m_hardwareHeight << "hardware scalers x" << m_hardwareScalerX << 'y' << m_hardwareScalerY;
0
157 << "hardware max y" << m_hardwareHeight
never executed: QMessageLogger(__FILE__, 155, __PRETTY_FUNCTION__, qLcEvdevMouse().categoryName()).debug() << "Absolute pointing device" << "hardware max x" << m_hardwareWidth << "hardware max y" << m_hardwareHeight << "hardware scalers x" << m_hardwareScalerX << 'y' << m_hardwareScalerY;
0
158 << "hardware scalers x" << m_hardwareScalerX << 'y' << m_hardwareScalerY;
never executed: QMessageLogger(__FILE__, 155, __PRETTY_FUNCTION__, qLcEvdevMouse().categoryName()).debug() << "Absolute pointing device" << "hardware max x" << m_hardwareWidth << "hardware max y" << m_hardwareHeight << "hardware scalers x" << m_hardwareScalerX << 'y' << m_hardwareScalerY;
0
159-
160 return true;
never executed: return true;
0
161}-
162-
163void QEvdevMouseHandler::sendMouseEvent()-
164{-
165 int x;-
166 int y;-
167-
168 if (!m_abs) {
!m_absDescription
TRUEnever evaluated
FALSEnever evaluated
0
169 x = m_x - m_prevx;-
170 y = m_y - m_prevy;-
171 }
never executed: end of block
0
172 else {-
173 x = m_x / m_hardwareScalerX;-
174 y = m_y / m_hardwareScalerY;-
175 }
never executed: end of block
0
176-
177 if (m_prevInvalid) {
m_prevInvalidDescription
TRUEnever evaluated
FALSEnever evaluated
0
178 x = y = 0;-
179 m_prevInvalid = false;-
180 }
never executed: end of block
0
181-
182 emit handleMouseEvent(x, y, m_abs, m_buttons);-
183-
184 m_prevx = m_x;-
185 m_prevy = m_y;-
186}
never executed: end of block
0
187-
188void QEvdevMouseHandler::readMouseData()-
189{-
190 struct ::input_event buffer[32];-
191 int n = 0;-
192 bool posChanged = false, btnChanged = false;-
193 bool pendingMouseEvent = false;-
194 int eventCompressCount = 0;-
195 forever {-
196 int result = QT_READ(m_fd, reinterpret_cast<char *>(buffer) + n, sizeof(buffer) - n);-
197-
198 if (result == 0) {
result == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
199 qWarning("evdevmouse: Got EOF from the input device");-
200 return;
never executed: return;
0
201 } else if (result < 0) {
result < 0Description
TRUEnever evaluated
FALSEnever evaluated
0
202 if (errno != EINTR && errno != EAGAIN) {
(*__errno_location ()) != 4Description
TRUEnever evaluated
FALSEnever evaluated
(*__errno_location ()) != 11Description
TRUEnever evaluated
FALSEnever evaluated
0
203 qErrnoWarning(errno, "evdevmouse: Could not read from input device");-
204 // If the device got disconnected, stop reading, otherwise we get flooded-
205 // by the above error over and over again.-
206 if (errno == ENODEV) {
(*__errno_location ()) == 19Description
TRUEnever evaluated
FALSEnever evaluated
0
207 delete m_notify;-
208 m_notify = Q_NULLPTR;-
209 qt_safe_close(m_fd);-
210 m_fd = -1;-
211 }
never executed: end of block
0
212 return;
never executed: return;
0
213 }-
214 } else {
never executed: end of block
0
215 n += result;-
216 if (n % sizeof(buffer[0]) == 0)
n % sizeof(buffer[0]) == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
217 break;
never executed: break;
0
218 }
never executed: end of block
0
219 }-
220-
221 n /= sizeof(buffer[0]);-
222-
223 for (int i = 0; i < n; ++i) {
i < nDescription
TRUEnever evaluated
FALSEnever evaluated
0
224 struct ::input_event *data = &buffer[i];-
225 if (data->type == EV_ABS) {
data->type == 0x03Description
TRUEnever evaluated
FALSEnever evaluated
0
226 // Touchpads: store the absolute position for now, will calculate a relative one later.-
227 if (data->code == ABS_X && m_x != data->value) {
data->code == 0x00Description
TRUEnever evaluated
FALSEnever evaluated
m_x != data->valueDescription
TRUEnever evaluated
FALSEnever evaluated
0
228 m_x = data->value;-
229 posChanged = true;-
230 } else if (data->code == ABS_Y && m_y != data->value) {
never executed: end of block
data->code == 0x01Description
TRUEnever evaluated
FALSEnever evaluated
m_y != data->valueDescription
TRUEnever evaluated
FALSEnever evaluated
0
231 m_y = data->value;-
232 posChanged = true;-
233 }
never executed: end of block
0
234 } else if (data->type == EV_REL) {
never executed: end of block
data->type == 0x02Description
TRUEnever evaluated
FALSEnever evaluated
0
235 if (data->code == REL_X) {
data->code == 0x00Description
TRUEnever evaluated
FALSEnever evaluated
0
236 m_x += data->value;-
237 posChanged = true;-
238 } else if (data->code == REL_Y) {
never executed: end of block
data->code == 0x01Description
TRUEnever evaluated
FALSEnever evaluated
0
239 m_y += data->value;-
240 posChanged = true;-
241 } else if (data->code == ABS_WHEEL) { // vertical scroll
never executed: end of block
data->code == 0x08Description
TRUEnever evaluated
FALSEnever evaluated
0
242 // data->value: 1 == up, -1 == down-
243 const int delta = 120 * data->value;-
244 emit handleWheelEvent(delta, Qt::Vertical);-
245 } else if (data->code == ABS_THROTTLE) { // horizontal scroll
never executed: end of block
data->code == 0x06Description
TRUEnever evaluated
FALSEnever evaluated
0
246 // data->value: 1 == right, -1 == left-
247 const int delta = 120 * -data->value;-
248 emit handleWheelEvent(delta, Qt::Horizontal);-
249 }
never executed: end of block
0
250 } else if (data->type == EV_KEY && data->code == BTN_TOUCH) {
never executed: end of block
data->type == 0x01Description
TRUEnever evaluated
FALSEnever evaluated
data->code == 0x14aDescription
TRUEnever evaluated
FALSEnever evaluated
0
251 // We care about touchpads only, not touchscreens -> don't map to button press.-
252 // Need to invalidate prevx/y however to get proper relative pos.-
253 m_prevInvalid = true;-
254 } else if (data->type == EV_KEY && data->code >= BTN_LEFT && data->code <= BTN_JOYSTICK) {
never executed: end of block
data->type == 0x01Description
TRUEnever evaluated
FALSEnever evaluated
data->code >= 0x110Description
TRUEnever evaluated
FALSEnever evaluated
data->code <= 0x120Description
TRUEnever evaluated
FALSEnever evaluated
0
255 Qt::MouseButton button = Qt::NoButton;-
256 // BTN_LEFT == 0x110 in kernel's input.h-
257 // The range of possible mouse buttons ends just before BTN_JOYSTICK, value 0x120.-
258 switch (data->code) {-
259 case 0x110: button = Qt::LeftButton; break; // BTN_LEFT
never executed: break;
never executed: case 0x110:
0
260 case 0x111: button = Qt::RightButton; break;
never executed: break;
never executed: case 0x111:
0
261 case 0x112: button = Qt::MiddleButton; break;
never executed: break;
never executed: case 0x112:
0
262 case 0x113: button = Qt::ExtraButton1; break; // AKA Qt::BackButton
never executed: break;
never executed: case 0x113:
0
263 case 0x114: button = Qt::ExtraButton2; break; // AKA Qt::ForwardButton
never executed: break;
never executed: case 0x114:
0
264 case 0x115: button = Qt::ExtraButton3; break; // AKA Qt::TaskButton
never executed: break;
never executed: case 0x115:
0
265 case 0x116: button = Qt::ExtraButton4; break;
never executed: break;
never executed: case 0x116:
0
266 case 0x117: button = Qt::ExtraButton5; break;
never executed: break;
never executed: case 0x117:
0
267 case 0x118: button = Qt::ExtraButton6; break;
never executed: break;
never executed: case 0x118:
0
268 case 0x119: button = Qt::ExtraButton7; break;
never executed: break;
never executed: case 0x119:
0
269 case 0x11a: button = Qt::ExtraButton8; break;
never executed: break;
never executed: case 0x11a:
0
270 case 0x11b: button = Qt::ExtraButton9; break;
never executed: break;
never executed: case 0x11b:
0
271 case 0x11c: button = Qt::ExtraButton10; break;
never executed: break;
never executed: case 0x11c:
0
272 case 0x11d: button = Qt::ExtraButton11; break;
never executed: break;
never executed: case 0x11d:
0
273 case 0x11e: button = Qt::ExtraButton12; break;
never executed: break;
never executed: case 0x11e:
0
274 case 0x11f: button = Qt::ExtraButton13; break;
never executed: break;
never executed: case 0x11f:
0
275 }-
276 m_buttons.setFlag(button, data->value);-
277 btnChanged = true;-
278 } else if (data->type == EV_SYN && data->code == SYN_REPORT) {
never executed: end of block
data->type == 0x00Description
TRUEnever evaluated
FALSEnever evaluated
data->code == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
279 if (btnChanged) {
btnChangedDescription
TRUEnever evaluated
FALSEnever evaluated
0
280 btnChanged = posChanged = false;-
281 sendMouseEvent();-
282 pendingMouseEvent = false;-
283 } else if (posChanged) {
never executed: end of block
posChangedDescription
TRUEnever evaluated
FALSEnever evaluated
0
284 posChanged = false;-
285 if (m_compression) {
m_compressionDescription
TRUEnever evaluated
FALSEnever evaluated
0
286 pendingMouseEvent = true;-
287 eventCompressCount++;-
288 } else {
never executed: end of block
0
289 sendMouseEvent();-
290 }
never executed: end of block
0
291 }-
292 } else if (data->type == EV_MSC && data->code == MSC_SCAN) {
never executed: end of block
data->type == 0x04Description
TRUEnever evaluated
FALSEnever evaluated
data->code == 0x04Description
TRUEnever evaluated
FALSEnever evaluated
0
293 // kernel encountered an unmapped key - just ignore it-
294 continue;
never executed: continue;
0
295 }-
296 }
never executed: end of block
0
297 if (m_compression && pendingMouseEvent) {
m_compressionDescription
TRUEnever evaluated
FALSEnever evaluated
pendingMouseEventDescription
TRUEnever evaluated
FALSEnever evaluated
0
298 int distanceSquared = (m_x - m_prevx)*(m_x - m_prevx) + (m_y - m_prevy)*(m_y - m_prevy);-
299 if (distanceSquared > m_jitterLimitSquared)
distanceSquare...erLimitSquaredDescription
TRUEnever evaluated
FALSEnever evaluated
0
300 sendMouseEvent();
never executed: sendMouseEvent();
0
301 }
never executed: end of block
0
302}
never executed: end of block
0
303-
304QT_END_NAMESPACE-
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9