1/****************************************************************************
2**
3** Copyright (C) 2019 The Qt Company Ltd.
4** Copyright (C) 2016 Jolla Ltd, author: <gunnar.sletta@jollamobile.com>
5** Contact: https://www.qt.io/licensing/
6**
7** This file is part of the plugins module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial License Usage
11** Licensees holding valid commercial Qt licenses may use this file in
12** accordance with the commercial license agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and The Qt Company. For licensing terms
15** and conditions see https://www.qt.io/terms-conditions. For further
16** information use the contact form at https://www.qt.io/contact-us.
17**
18** GNU Lesser General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU Lesser
20** General Public License version 3 as published by the Free Software
21** Foundation and appearing in the file LICENSE.LGPL3 included in the
22** packaging of this file. Please review the following information to
23** ensure the GNU Lesser General Public License version 3 requirements
24** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
25**
26** GNU General Public License Usage
27** Alternatively, this file may be used under the terms of the GNU
28** General Public License version 2.0 or (at your option) the GNU General
29** Public license version 3 or any later version approved by the KDE Free
30** Qt Foundation. The licenses are as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
32** included in the packaging of this file. Please review the following
33** information to ensure the GNU General Public License requirements will
34** be met: https://www.gnu.org/licenses/gpl-2.0.html and
35** https://www.gnu.org/licenses/gpl-3.0.html.
36**
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#include "qevdevtouchhandler_p.h"
42#include "qtouchoutputmapping_p.h"
43#include <QStringList>
44#include <QHash>
45#include <QSocketNotifier>
46#include <QGuiApplication>
47#include <QTouchDevice>
48#include <QLoggingCategory>
49#include <QtCore/private/qcore_unix_p.h>
50#include <QtGui/private/qhighdpiscaling_p.h>
51#include <QtGui/private/qguiapplication_p.h>
52
53#include <mutex>
54
55#ifdef Q_OS_FREEBSD
56#include <dev/evdev/input.h>
57#else
58#include <linux/input.h>
59#endif
60
61#ifndef input_event_sec
62#define input_event_sec time.tv_sec
63#endif
64
65#ifndef input_event_usec
66#define input_event_usec time.tv_usec
67#endif
68
69#include <math.h>
70
71#if QT_CONFIG(mtdev)
72extern "C" {
73#include <mtdev.h>
74}
75#endif
76
77QT_BEGIN_NAMESPACE
78
79Q_LOGGING_CATEGORY(qLcEvdevTouch, "qt.qpa.input")
80Q_LOGGING_CATEGORY(qLcEvents, "qt.qpa.input.events")
81
82/* android (and perhaps some other linux-derived stuff) don't define everything
83 * in linux/input.h, so we'll need to do that ourselves.
84 */
85#ifndef ABS_MT_TOUCH_MAJOR
86#define ABS_MT_TOUCH_MAJOR 0x30 /* Major axis of touching ellipse */
87#endif
88#ifndef ABS_MT_POSITION_X
89#define ABS_MT_POSITION_X 0x35 /* Center X ellipse position */
90#endif
91#ifndef ABS_MT_POSITION_Y
92#define ABS_MT_POSITION_Y 0x36 /* Center Y ellipse position */
93#endif
94#ifndef ABS_MT_SLOT
95#define ABS_MT_SLOT 0x2f
96#endif
97#ifndef ABS_CNT
98#define ABS_CNT (ABS_MAX+1)
99#endif
100#ifndef ABS_MT_TRACKING_ID
101#define ABS_MT_TRACKING_ID 0x39 /* Unique ID of initiated contact */
102#endif
103#ifndef ABS_MT_PRESSURE
104#define ABS_MT_PRESSURE 0x3a
105#endif
106#ifndef SYN_MT_REPORT
107#define SYN_MT_REPORT 2
108#endif
109
110class QEvdevTouchScreenData
111{
112public:
113 QEvdevTouchScreenData(QEvdevTouchScreenHandler *q_ptr, const QStringList &args);
114
115 void processInputEvent(input_event *data);
116 void assignIds();
117
118 QEvdevTouchScreenHandler *q;
119 int m_lastEventType;
120 QList<QWindowSystemInterface::TouchPoint> m_touchPoints;
121 QList<QWindowSystemInterface::TouchPoint> m_lastTouchPoints;
122
123 struct Contact {
124 int trackingId = -1;
125 int x = 0;
126 int y = 0;
127 int maj = -1;
128 int pressure = 0;
129 Qt::TouchPointState state = Qt::TouchPointPressed;
130 QTouchEvent::TouchPoint::InfoFlags flags;
131 };
132 QHash<int, Contact> m_contacts; // The key is a tracking id for type A, slot number for type B.
133 QHash<int, Contact> m_lastContacts;
134 Contact m_currentData;
135 int m_currentSlot;
136
137 double m_timeStamp;
138 double m_lastTimeStamp;
139
140 int findClosestContact(const QHash<int, Contact> &contacts, int x, int y, int *dist);
141 void addTouchPoint(const Contact &contact, Qt::TouchPointStates *combinedStates);
142 void reportPoints();
143 void loadMultiScreenMappings();
144
145 QRect screenGeometry() const;
146
147 int hw_range_x_min;
148 int hw_range_x_max;
149 int hw_range_y_min;
150 int hw_range_y_max;
151 int hw_pressure_min;
152 int hw_pressure_max;
153 QString hw_name;
154 QString deviceNode;
155 bool m_forceToActiveWindow;
156 bool m_typeB;
157 QTransform m_rotate;
158 bool m_singleTouch;
159 QString m_screenName;
160 mutable QPointer<QScreen> m_screen;
161
162 // Touch filtering and prediction are part of the same thing. The default
163 // prediction is 0ms, but sensible results can be achieved by setting it
164 // to, for instance, 16ms.
165 // For filtering to work well, the QPA plugin should provide a dead-steady
166 // implementation of QPlatformWindow::requestUpdate().
167 bool m_filtered;
168 int m_prediction;
169
170 // When filtering is enabled, protect the access to current and last
171 // timeStamp and touchPoints, as these are being read on the gui thread.
172 QMutex m_mutex;
173};
174
175QEvdevTouchScreenData::QEvdevTouchScreenData(QEvdevTouchScreenHandler *q_ptr, const QStringList &args)
176 : q(q_ptr),
177 m_lastEventType(-1),
178 m_currentSlot(0),
179 m_timeStamp(0), m_lastTimeStamp(0),
180 hw_range_x_min(0), hw_range_x_max(0),
181 hw_range_y_min(0), hw_range_y_max(0),
182 hw_pressure_min(0), hw_pressure_max(0),
183 m_forceToActiveWindow(false), m_typeB(false), m_singleTouch(false),
184 m_filtered(false), m_prediction(0)
185{
186 for (const QString &arg : args) {
187 if (arg == QStringLiteral("force_window"))
188 m_forceToActiveWindow = true;
189 else if (arg == QStringLiteral("filtered"))
190 m_filtered = true;
191 else if (arg.startsWith(QStringLiteral("prediction=")))
192 m_prediction = arg.mid(position: 11).toInt();
193 }
194}
195
196#define LONG_BITS (sizeof(long) << 3)
197#define NUM_LONGS(bits) (((bits) + LONG_BITS - 1) / LONG_BITS)
198
199#if !QT_CONFIG(mtdev)
200static inline bool testBit(long bit, const long *array)
201{
202 return (array[bit / LONG_BITS] >> bit % LONG_BITS) & 1;
203}
204#endif
205
206QEvdevTouchScreenHandler::QEvdevTouchScreenHandler(const QString &device, const QString &spec, QObject *parent)
207 : QObject(parent), m_notify(nullptr), m_fd(-1), d(nullptr), m_device(nullptr)
208#if QT_CONFIG(mtdev)
209 , m_mtdev(nullptr)
210#endif
211{
212 setObjectName(QLatin1String("Evdev Touch Handler"));
213
214 const QStringList args = spec.split(sep: QLatin1Char(':'));
215 int rotationAngle = 0;
216 bool invertx = false;
217 bool inverty = false;
218 for (int i = 0; i < args.count(); ++i) {
219 if (args.at(i).startsWith(s: QLatin1String("rotate"))) {
220 QString rotateArg = args.at(i).section(asep: QLatin1Char('='), astart: 1, aend: 1);
221 bool ok;
222 uint argValue = rotateArg.toUInt(ok: &ok);
223 if (ok) {
224 switch (argValue) {
225 case 90:
226 case 180:
227 case 270:
228 rotationAngle = argValue;
229 default:
230 break;
231 }
232 }
233 } else if (args.at(i) == QLatin1String("invertx")) {
234 invertx = true;
235 } else if (args.at(i) == QLatin1String("inverty")) {
236 inverty = true;
237 }
238 }
239
240 qCDebug(qLcEvdevTouch, "evdevtouch: Using device %ls", qUtf16Printable(device));
241
242 m_fd = QT_OPEN(pathname: device.toLocal8Bit().constData(), O_RDONLY | O_NDELAY, mode: 0);
243
244 if (m_fd >= 0) {
245 m_notify = new QSocketNotifier(m_fd, QSocketNotifier::Read, this);
246 connect(sender: m_notify, signal: &QSocketNotifier::activated, receiver: this, slot: &QEvdevTouchScreenHandler::readData);
247 } else {
248 qErrnoWarning(msg: "evdevtouch: Cannot open input device %ls", qUtf16Printable(device));
249 return;
250 }
251
252#if QT_CONFIG(mtdev)
253 m_mtdev = static_cast<mtdev *>(calloc(nmemb: 1, size: sizeof(mtdev)));
254 int mtdeverr = mtdev_open(dev: m_mtdev, fd: m_fd);
255 if (mtdeverr) {
256 qWarning(msg: "evdevtouch: mtdev_open failed: %d", mtdeverr);
257 QT_CLOSE(fd: m_fd);
258 free(ptr: m_mtdev);
259 return;
260 }
261#endif
262
263 d = new QEvdevTouchScreenData(this, args);
264
265#if QT_CONFIG(mtdev)
266 const char *mtdevStr = "(mtdev)";
267 d->m_typeB = true;
268#else
269 const char *mtdevStr = "";
270 long absbits[NUM_LONGS(ABS_CNT)];
271 if (ioctl(m_fd, EVIOCGBIT(EV_ABS, sizeof(absbits)), absbits) >= 0) {
272 d->m_typeB = testBit(ABS_MT_SLOT, absbits);
273 d->m_singleTouch = !testBit(ABS_MT_POSITION_X, absbits);
274 }
275#endif
276
277 d->deviceNode = device;
278 qCDebug(qLcEvdevTouch,
279 "evdevtouch: %ls: Protocol type %c %s (%s), filtered=%s",
280 qUtf16Printable(d->deviceNode),
281 d->m_typeB ? 'B' : 'A', mtdevStr,
282 d->m_singleTouch ? "single" : "multi",
283 d->m_filtered ? "yes" : "no");
284 if (d->m_filtered)
285 qCDebug(qLcEvdevTouch, " - prediction=%d", d->m_prediction);
286
287 input_absinfo absInfo;
288 memset(s: &absInfo, c: 0, n: sizeof(input_absinfo));
289 bool has_x_range = false, has_y_range = false;
290
291 if (ioctl(fd: m_fd, EVIOCGABS((d->m_singleTouch ? ABS_X : ABS_MT_POSITION_X)), &absInfo) >= 0) {
292 qCDebug(qLcEvdevTouch, "evdevtouch: %ls: min X: %d max X: %d", qUtf16Printable(device),
293 absInfo.minimum, absInfo.maximum);
294 d->hw_range_x_min = absInfo.minimum;
295 d->hw_range_x_max = absInfo.maximum;
296 has_x_range = true;
297 }
298
299 if (ioctl(fd: m_fd, EVIOCGABS((d->m_singleTouch ? ABS_Y : ABS_MT_POSITION_Y)), &absInfo) >= 0) {
300 qCDebug(qLcEvdevTouch, "evdevtouch: %ls: min Y: %d max Y: %d", qUtf16Printable(device),
301 absInfo.minimum, absInfo.maximum);
302 d->hw_range_y_min = absInfo.minimum;
303 d->hw_range_y_max = absInfo.maximum;
304 has_y_range = true;
305 }
306
307 if (!has_x_range || !has_y_range)
308 qWarning(msg: "evdevtouch: %ls: Invalid ABS limits, behavior unspecified", qUtf16Printable(device));
309
310 if (ioctl(fd: m_fd, EVIOCGABS(ABS_PRESSURE), &absInfo) >= 0) {
311 qCDebug(qLcEvdevTouch, "evdevtouch: %ls: min pressure: %d max pressure: %d", qUtf16Printable(device),
312 absInfo.minimum, absInfo.maximum);
313 if (absInfo.maximum > absInfo.minimum) {
314 d->hw_pressure_min = absInfo.minimum;
315 d->hw_pressure_max = absInfo.maximum;
316 }
317 }
318
319 char name[1024];
320 if (ioctl(fd: m_fd, EVIOCGNAME(sizeof(name) - 1), name) >= 0) {
321 d->hw_name = QString::fromLocal8Bit(str: name);
322 qCDebug(qLcEvdevTouch, "evdevtouch: %ls: device name: %s", qUtf16Printable(device), name);
323 }
324
325 // Fix up the coordinate ranges for am335x in case the kernel driver does not have them fixed.
326 if (d->hw_name == QLatin1String("ti-tsc")) {
327 if (d->hw_range_x_min == 0 && d->hw_range_x_max == 4095) {
328 d->hw_range_x_min = 165;
329 d->hw_range_x_max = 4016;
330 }
331 if (d->hw_range_y_min == 0 && d->hw_range_y_max == 4095) {
332 d->hw_range_y_min = 220;
333 d->hw_range_y_max = 3907;
334 }
335 qCDebug(qLcEvdevTouch, "evdevtouch: found ti-tsc, overriding: min X: %d max X: %d min Y: %d max Y: %d",
336 d->hw_range_x_min, d->hw_range_x_max, d->hw_range_y_min, d->hw_range_y_max);
337 }
338
339 bool grabSuccess = !ioctl(fd: m_fd, EVIOCGRAB, (void *) 1);
340 if (grabSuccess)
341 ioctl(fd: m_fd, EVIOCGRAB, (void *) 0);
342 else
343 qWarning(msg: "evdevtouch: The device is grabbed by another process. No events will be read.");
344
345 if (rotationAngle)
346 d->m_rotate = QTransform::fromTranslate(dx: 0.5, dy: 0.5).rotate(a: rotationAngle).translate(dx: -0.5, dy: -0.5);
347
348 if (invertx)
349 d->m_rotate *= QTransform::fromTranslate(dx: 0.5, dy: 0.5).scale(sx: -1.0, sy: 1.0).translate(dx: -0.5, dy: -0.5);
350
351 if (inverty)
352 d->m_rotate *= QTransform::fromTranslate(dx: 0.5, dy: 0.5).scale(sx: 1.0, sy: -1.0).translate(dx: -0.5, dy: -0.5);
353
354 QTouchOutputMapping mapping;
355 if (mapping.load()) {
356 d->m_screenName = mapping.screenNameForDeviceNode(deviceNode: d->deviceNode);
357 if (!d->m_screenName.isEmpty())
358 qCDebug(qLcEvdevTouch, "evdevtouch: Mapping device %ls to screen %ls",
359 qUtf16Printable(d->deviceNode), qUtf16Printable(d->m_screenName));
360 }
361
362 registerTouchDevice();
363}
364
365QEvdevTouchScreenHandler::~QEvdevTouchScreenHandler()
366{
367#if QT_CONFIG(mtdev)
368 if (m_mtdev) {
369 mtdev_close(dev: m_mtdev);
370 free(ptr: m_mtdev);
371 }
372#endif
373
374 if (m_fd >= 0)
375 QT_CLOSE(fd: m_fd);
376
377 delete d;
378
379 unregisterTouchDevice();
380}
381
382bool QEvdevTouchScreenHandler::isFiltered() const
383{
384 return d && d->m_filtered;
385}
386
387QTouchDevice *QEvdevTouchScreenHandler::touchDevice() const
388{
389 return m_device;
390}
391
392void QEvdevTouchScreenHandler::readData()
393{
394 ::input_event buffer[32];
395 int events = 0;
396
397#if QT_CONFIG(mtdev)
398 forever {
399 do {
400 events = mtdev_get(dev: m_mtdev, fd: m_fd, ev: buffer, ev_max: sizeof(buffer) / sizeof(::input_event));
401 // keep trying mtdev_get if we get interrupted. note that we do not
402 // (and should not) handle EAGAIN; EAGAIN means that reading would
403 // block and we'll get back here later to try again anyway.
404 } while (events == -1 && errno == EINTR);
405
406 // 0 events is EOF, -1 means error, handle both in the same place
407 if (events <= 0)
408 goto err;
409
410 // process our shiny new events
411 for (int i = 0; i < events; ++i)
412 d->processInputEvent(data: &buffer[i]);
413
414 // and try to get more
415 }
416#else
417 int n = 0;
418 for (; ;) {
419 events = QT_READ(m_fd, reinterpret_cast<char*>(buffer) + n, sizeof(buffer) - n);
420 if (events <= 0)
421 goto err;
422 n += events;
423 if (n % sizeof(::input_event) == 0)
424 break;
425 }
426
427 n /= sizeof(::input_event);
428
429 for (int i = 0; i < n; ++i)
430 d->processInputEvent(&buffer[i]);
431#endif
432 return;
433
434err:
435 if (!events) {
436 qWarning(msg: "evdevtouch: Got EOF from input device");
437 return;
438 } else if (events < 0) {
439 if (errno != EINTR && errno != EAGAIN) {
440 qErrnoWarning(msg: "evdevtouch: Could not read from input device");
441 if (errno == ENODEV) { // device got disconnected -> stop reading
442 delete m_notify;
443 m_notify = nullptr;
444
445 QT_CLOSE(fd: m_fd);
446 m_fd = -1;
447
448 unregisterTouchDevice();
449 }
450 return;
451 }
452 }
453}
454
455void QEvdevTouchScreenHandler::registerTouchDevice()
456{
457 if (m_device)
458 return;
459
460 m_device = new QTouchDevice;
461 m_device->setName(d->hw_name);
462 m_device->setType(QTouchDevice::TouchScreen);
463 m_device->setCapabilities(QTouchDevice::Position | QTouchDevice::Area);
464 if (d->hw_pressure_max > d->hw_pressure_min)
465 m_device->setCapabilities(m_device->capabilities() | QTouchDevice::Pressure);
466
467 QWindowSystemInterface::registerTouchDevice(device: m_device);
468}
469
470void QEvdevTouchScreenHandler::unregisterTouchDevice()
471{
472 if (!m_device)
473 return;
474
475 // At app exit the cleanup may have already been done, avoid
476 // double delete by checking the list first.
477 if (QWindowSystemInterface::isTouchDeviceRegistered(device: m_device)) {
478 QWindowSystemInterface::unregisterTouchDevice(device: m_device);
479 delete m_device;
480 }
481
482 m_device = nullptr;
483}
484
485void QEvdevTouchScreenData::addTouchPoint(const Contact &contact, Qt::TouchPointStates *combinedStates)
486{
487 QWindowSystemInterface::TouchPoint tp;
488 tp.id = contact.trackingId;
489 tp.flags = contact.flags;
490 tp.state = contact.state;
491 *combinedStates |= tp.state;
492
493 // Store the HW coordinates for now, will be updated later.
494 tp.area = QRectF(0, 0, contact.maj, contact.maj);
495 tp.area.moveCenter(p: QPoint(contact.x, contact.y));
496 tp.pressure = contact.pressure;
497
498 // Get a normalized position in range 0..1.
499 tp.normalPosition = QPointF((contact.x - hw_range_x_min) / qreal(hw_range_x_max - hw_range_x_min),
500 (contact.y - hw_range_y_min) / qreal(hw_range_y_max - hw_range_y_min));
501
502 if (!m_rotate.isIdentity())
503 tp.normalPosition = m_rotate.map(p: tp.normalPosition);
504
505 tp.rawPositions.append(t: QPointF(contact.x, contact.y));
506
507 m_touchPoints.append(t: tp);
508}
509
510void QEvdevTouchScreenData::processInputEvent(input_event *data)
511{
512 if (data->type == EV_ABS) {
513
514 if (data->code == ABS_MT_POSITION_X || (m_singleTouch && data->code == ABS_X)) {
515 m_currentData.x = qBound(min: hw_range_x_min, val: data->value, max: hw_range_x_max);
516 if (m_singleTouch)
517 m_contacts[m_currentSlot].x = m_currentData.x;
518 if (m_typeB) {
519 m_contacts[m_currentSlot].x = m_currentData.x;
520 if (m_contacts[m_currentSlot].state == Qt::TouchPointStationary)
521 m_contacts[m_currentSlot].state = Qt::TouchPointMoved;
522 }
523 } else if (data->code == ABS_MT_POSITION_Y || (m_singleTouch && data->code == ABS_Y)) {
524 m_currentData.y = qBound(min: hw_range_y_min, val: data->value, max: hw_range_y_max);
525 if (m_singleTouch)
526 m_contacts[m_currentSlot].y = m_currentData.y;
527 if (m_typeB) {
528 m_contacts[m_currentSlot].y = m_currentData.y;
529 if (m_contacts[m_currentSlot].state == Qt::TouchPointStationary)
530 m_contacts[m_currentSlot].state = Qt::TouchPointMoved;
531 }
532 } else if (data->code == ABS_MT_TRACKING_ID) {
533 m_currentData.trackingId = data->value;
534 if (m_typeB) {
535 if (m_currentData.trackingId == -1) {
536 m_contacts[m_currentSlot].state = Qt::TouchPointReleased;
537 } else {
538 m_contacts[m_currentSlot].state = Qt::TouchPointPressed;
539 m_contacts[m_currentSlot].trackingId = m_currentData.trackingId;
540 }
541 }
542 } else if (data->code == ABS_MT_TOUCH_MAJOR) {
543 m_currentData.maj = data->value;
544 if (data->value == 0)
545 m_currentData.state = Qt::TouchPointReleased;
546 if (m_typeB)
547 m_contacts[m_currentSlot].maj = m_currentData.maj;
548 } else if (data->code == ABS_PRESSURE || data->code == ABS_MT_PRESSURE) {
549 if (Q_UNLIKELY(qLcEvents().isDebugEnabled()))
550 qCDebug(qLcEvents, "EV_ABS code 0x%x: pressure %d; bounding to [%d,%d]",
551 data->code, data->value, hw_pressure_min, hw_pressure_max);
552 m_currentData.pressure = qBound(min: hw_pressure_min, val: data->value, max: hw_pressure_max);
553 if (m_typeB || m_singleTouch)
554 m_contacts[m_currentSlot].pressure = m_currentData.pressure;
555 } else if (data->code == ABS_MT_SLOT) {
556 m_currentSlot = data->value;
557 }
558
559 } else if (data->type == EV_KEY && !m_typeB) {
560 if (data->code == BTN_TOUCH && data->value == 0)
561 m_contacts[m_currentSlot].state = Qt::TouchPointReleased;
562 } else if (data->type == EV_SYN && data->code == SYN_MT_REPORT && m_lastEventType != EV_SYN) {
563
564 // If there is no tracking id, one will be generated later.
565 // Until that use a temporary key.
566 int key = m_currentData.trackingId;
567 if (key == -1)
568 key = m_contacts.count();
569
570 m_contacts.insert(akey: key, avalue: m_currentData);
571 m_currentData = Contact();
572
573 } else if (data->type == EV_SYN && data->code == SYN_REPORT) {
574
575 // Ensure valid IDs even when the driver does not report ABS_MT_TRACKING_ID.
576 if (!m_contacts.isEmpty() && m_contacts.constBegin().value().trackingId == -1)
577 assignIds();
578
579 std::unique_lock<QMutex> locker;
580 if (m_filtered)
581 locker = std::unique_lock<QMutex>{m_mutex};
582
583 // update timestamps
584 m_lastTimeStamp = m_timeStamp;
585 m_timeStamp = data->input_event_sec + data->input_event_usec / 1000000.0;
586
587 m_lastTouchPoints = m_touchPoints;
588 m_touchPoints.clear();
589 Qt::TouchPointStates combinedStates;
590 bool hasPressure = false;
591
592 for (auto i = m_contacts.begin(), end = m_contacts.end(); i != end; /*erasing*/) {
593 auto it = i++;
594
595 Contact &contact(it.value());
596
597 if (!contact.state)
598 continue;
599
600 int key = m_typeB ? it.key() : contact.trackingId;
601 if (!m_typeB && m_lastContacts.contains(akey: key)) {
602 const Contact &prev(m_lastContacts.value(akey: key));
603 if (contact.state == Qt::TouchPointReleased) {
604 // Copy over the previous values for released points, just in case.
605 contact.x = prev.x;
606 contact.y = prev.y;
607 contact.maj = prev.maj;
608 } else {
609 contact.state = (prev.x == contact.x && prev.y == contact.y)
610 ? Qt::TouchPointStationary : Qt::TouchPointMoved;
611 }
612 }
613
614 // Avoid reporting a contact in released state more than once.
615 if (!m_typeB && contact.state == Qt::TouchPointReleased
616 && !m_lastContacts.contains(akey: key)) {
617 m_contacts.erase(it);
618 continue;
619 }
620
621 if (contact.pressure)
622 hasPressure = true;
623
624 addTouchPoint(contact, combinedStates: &combinedStates);
625 }
626
627 // Now look for contacts that have disappeared since the last sync.
628 for (auto it = m_lastContacts.begin(), end = m_lastContacts.end(); it != end; ++it) {
629 Contact &contact(it.value());
630 int key = m_typeB ? it.key() : contact.trackingId;
631 if (m_typeB) {
632 if (contact.trackingId != m_contacts[key].trackingId && contact.state) {
633 contact.state = Qt::TouchPointReleased;
634 addTouchPoint(contact, combinedStates: &combinedStates);
635 }
636 } else {
637 if (!m_contacts.contains(akey: key)) {
638 contact.state = Qt::TouchPointReleased;
639 addTouchPoint(contact, combinedStates: &combinedStates);
640 }
641 }
642 }
643
644 // Remove contacts that have just been reported as released.
645 for (auto i = m_contacts.begin(), end = m_contacts.end(); i != end; /*erasing*/) {
646 auto it = i++;
647
648 Contact &contact(it.value());
649
650 if (!contact.state)
651 continue;
652
653 if (contact.state == Qt::TouchPointReleased) {
654 if (m_typeB)
655 contact.state = static_cast<Qt::TouchPointState>(0);
656 else
657 m_contacts.erase(it);
658 } else {
659 contact.state = Qt::TouchPointStationary;
660 }
661 }
662
663 m_lastContacts = m_contacts;
664 if (!m_typeB && !m_singleTouch)
665 m_contacts.clear();
666
667
668 if (!m_touchPoints.isEmpty() && (hasPressure || combinedStates != Qt::TouchPointStationary))
669 reportPoints();
670 }
671
672 m_lastEventType = data->type;
673}
674
675int QEvdevTouchScreenData::findClosestContact(const QHash<int, Contact> &contacts, int x, int y, int *dist)
676{
677 int minDist = -1, id = -1;
678 for (QHash<int, Contact>::const_iterator it = contacts.constBegin(), ite = contacts.constEnd();
679 it != ite; ++it) {
680 const Contact &contact(it.value());
681 int dx = x - contact.x;
682 int dy = y - contact.y;
683 int dist = dx * dx + dy * dy;
684 if (minDist == -1 || dist < minDist) {
685 minDist = dist;
686 id = contact.trackingId;
687 }
688 }
689 if (dist)
690 *dist = minDist;
691 return id;
692}
693
694void QEvdevTouchScreenData::assignIds()
695{
696 QHash<int, Contact> candidates = m_lastContacts, pending = m_contacts, newContacts;
697 int maxId = -1;
698 QHash<int, Contact>::iterator it, ite, bestMatch;
699 while (!pending.isEmpty() && !candidates.isEmpty()) {
700 int bestDist = -1, bestId = 0;
701 for (it = pending.begin(), ite = pending.end(); it != ite; ++it) {
702 int dist;
703 int id = findClosestContact(contacts: candidates, x: it->x, y: it->y, dist: &dist);
704 if (id >= 0 && (bestDist == -1 || dist < bestDist)) {
705 bestDist = dist;
706 bestId = id;
707 bestMatch = it;
708 }
709 }
710 if (bestDist >= 0) {
711 bestMatch->trackingId = bestId;
712 newContacts.insert(akey: bestId, avalue: *bestMatch);
713 candidates.remove(akey: bestId);
714 pending.erase(it: bestMatch);
715 if (bestId > maxId)
716 maxId = bestId;
717 }
718 }
719 if (candidates.isEmpty()) {
720 for (it = pending.begin(), ite = pending.end(); it != ite; ++it) {
721 it->trackingId = ++maxId;
722 newContacts.insert(akey: it->trackingId, avalue: *it);
723 }
724 }
725 m_contacts = newContacts;
726}
727
728QRect QEvdevTouchScreenData::screenGeometry() const
729{
730 if (m_forceToActiveWindow) {
731 QWindow *win = QGuiApplication::focusWindow();
732 return win ? QHighDpi::toNativePixels(value: win->geometry(), context: win) : QRect();
733 }
734
735 // Now it becomes tricky. Traditionally we picked the primaryScreen()
736 // and were done with it. But then, enter multiple screens, and
737 // suddenly it was all broken.
738 //
739 // For now we only support the display configuration of the KMS/DRM
740 // backends of eglfs. See QTouchOutputMapping.
741 //
742 // The good news it that once winRect refers to the correct screen
743 // geometry in the full virtual desktop space, there is nothing else
744 // left to do since qguiapp will handle the rest.
745 QScreen *screen = QGuiApplication::primaryScreen();
746 if (!m_screenName.isEmpty()) {
747 if (!m_screen) {
748 const QList<QScreen *> screens = QGuiApplication::screens();
749 for (QScreen *s : screens) {
750 if (s->name() == m_screenName) {
751 m_screen = s;
752 break;
753 }
754 }
755 }
756 if (m_screen)
757 screen = m_screen;
758 }
759 return screen ? QHighDpi::toNativePixels(value: screen->geometry(), context: screen) : QRect();
760}
761
762void QEvdevTouchScreenData::reportPoints()
763{
764 QRect winRect = screenGeometry();
765 if (winRect.isNull())
766 return;
767
768 const int hw_w = hw_range_x_max - hw_range_x_min;
769 const int hw_h = hw_range_y_max - hw_range_y_min;
770
771 // Map the coordinates based on the normalized position. QPA expects 'area'
772 // to be in screen coordinates.
773 const int pointCount = m_touchPoints.count();
774 for (int i = 0; i < pointCount; ++i) {
775 QWindowSystemInterface::TouchPoint &tp(m_touchPoints[i]);
776
777 // Generate a screen position that is always inside the active window
778 // or the primary screen. Even though we report this as a QRectF, internally
779 // Qt uses QRect/QPoint so we need to bound the size to winRect.size() - QSize(1, 1)
780 const qreal wx = winRect.left() + tp.normalPosition.x() * (winRect.width() - 1);
781 const qreal wy = winRect.top() + tp.normalPosition.y() * (winRect.height() - 1);
782 const qreal sizeRatio = (winRect.width() + winRect.height()) / qreal(hw_w + hw_h);
783 if (tp.area.width() == -1) // touch major was not provided
784 tp.area = QRectF(0, 0, 8, 8);
785 else
786 tp.area = QRectF(0, 0, tp.area.width() * sizeRatio, tp.area.height() * sizeRatio);
787 tp.area.moveCenter(p: QPointF(wx, wy));
788
789 // Calculate normalized pressure.
790 if (!hw_pressure_min && !hw_pressure_max)
791 tp.pressure = tp.state == Qt::TouchPointReleased ? 0 : 1;
792 else
793 tp.pressure = (tp.pressure - hw_pressure_min) / qreal(hw_pressure_max - hw_pressure_min);
794
795 if (Q_UNLIKELY(qLcEvents().isDebugEnabled()))
796 qCDebug(qLcEvents) << "reporting" << tp;
797 }
798
799 // Let qguiapp pick the target window.
800 if (m_filtered)
801 emit q->touchPointsUpdated();
802 else
803 QWindowSystemInterface::handleTouchEvent(window: nullptr, device: q->touchDevice(), points: m_touchPoints);
804}
805
806QEvdevTouchScreenHandlerThread::QEvdevTouchScreenHandlerThread(const QString &device, const QString &spec, QObject *parent)
807 : QDaemonThread(parent), m_device(device), m_spec(spec), m_handler(nullptr), m_touchDeviceRegistered(false)
808 , m_touchUpdatePending(false)
809 , m_filterWindow(nullptr)
810 , m_touchRate(-1)
811{
812 start();
813}
814
815QEvdevTouchScreenHandlerThread::~QEvdevTouchScreenHandlerThread()
816{
817 quit();
818 wait();
819}
820
821void QEvdevTouchScreenHandlerThread::run()
822{
823 m_handler = new QEvdevTouchScreenHandler(m_device, m_spec);
824
825 if (m_handler->isFiltered())
826 connect(sender: m_handler, signal: &QEvdevTouchScreenHandler::touchPointsUpdated, receiver: this, slot: &QEvdevTouchScreenHandlerThread::scheduleTouchPointUpdate);
827
828 // Report the registration to the parent thread by invoking the method asynchronously
829 QMetaObject::invokeMethod(obj: this, member: "notifyTouchDeviceRegistered", type: Qt::QueuedConnection);
830
831 exec();
832
833 delete m_handler;
834 m_handler = nullptr;
835}
836
837bool QEvdevTouchScreenHandlerThread::isTouchDeviceRegistered() const
838{
839 return m_touchDeviceRegistered;
840}
841
842void QEvdevTouchScreenHandlerThread::notifyTouchDeviceRegistered()
843{
844 m_touchDeviceRegistered = true;
845 emit touchDeviceRegistered();
846}
847
848void QEvdevTouchScreenHandlerThread::scheduleTouchPointUpdate()
849{
850 QWindow *window = QGuiApplication::focusWindow();
851 if (window != m_filterWindow) {
852 if (m_filterWindow)
853 m_filterWindow->removeEventFilter(obj: this);
854 m_filterWindow = window;
855 if (m_filterWindow)
856 m_filterWindow->installEventFilter(filterObj: this);
857 }
858 if (m_filterWindow) {
859 m_touchUpdatePending = true;
860 m_filterWindow->requestUpdate();
861 }
862}
863
864bool QEvdevTouchScreenHandlerThread::eventFilter(QObject *object, QEvent *event)
865{
866 if (m_touchUpdatePending && object == m_filterWindow && event->type() == QEvent::UpdateRequest) {
867 m_touchUpdatePending = false;
868 filterAndSendTouchPoints();
869 }
870 return false;
871}
872
873void QEvdevTouchScreenHandlerThread::filterAndSendTouchPoints()
874{
875 QRect winRect = m_handler->d->screenGeometry();
876 if (winRect.isNull())
877 return;
878
879 float vsyncDelta = 1.0f / QGuiApplication::primaryScreen()->refreshRate();
880
881 QHash<int, FilteredTouchPoint> filteredPoints;
882
883 m_handler->d->m_mutex.lock();
884
885 double time = m_handler->d->m_timeStamp;
886 double lastTime = m_handler->d->m_lastTimeStamp;
887 double touchDelta = time - lastTime;
888 if (m_touchRate < 0 || touchDelta > vsyncDelta) {
889 // We're at the very start, with nothing to go on, so make a guess
890 // that the touch rate will be somewhere in the range of half a vsync.
891 // This doesn't have to be accurate as we will calibrate it over time,
892 // but it gives us a better starting point so calibration will be
893 // slightly quicker. If, on the other hand, we already have an
894 // estimate, we'll leave it as is and keep it.
895 if (m_touchRate < 0)
896 m_touchRate = (1.0 / QGuiApplication::primaryScreen()->refreshRate()) / 2.0;
897
898 } else {
899 // Update our estimate for the touch rate. We're making the assumption
900 // that this value will be mostly accurate with the occational bump,
901 // so we're weighting the existing value high compared to the update.
902 const double ratio = 0.9;
903 m_touchRate = sqrt(x: m_touchRate * m_touchRate * ratio + touchDelta * touchDelta * (1.0 - ratio));
904 }
905
906 QList<QWindowSystemInterface::TouchPoint> points = m_handler->d->m_touchPoints;
907 QList<QWindowSystemInterface::TouchPoint> lastPoints = m_handler->d->m_lastTouchPoints;
908
909 m_handler->d->m_mutex.unlock();
910
911 for (int i=0; i<points.size(); ++i) {
912 QWindowSystemInterface::TouchPoint &tp = points[i];
913 QPointF pos = tp.normalPosition;
914 FilteredTouchPoint f;
915
916 QWindowSystemInterface::TouchPoint ltp;
917 ltp.id = -1;
918 for (int j=0; j<lastPoints.size(); ++j) {
919 if (lastPoints.at(i: j).id == tp.id) {
920 ltp = lastPoints.at(i: j);
921 break;
922 }
923 }
924
925 QPointF velocity;
926 if (lastTime != 0 && ltp.id >= 0)
927 velocity = (pos - ltp.normalPosition) / m_touchRate;
928 if (m_filteredPoints.contains(akey: tp.id)) {
929 f = m_filteredPoints.take(akey: tp.id);
930 f.x.update(pos: pos.x(), velocity: velocity.x(), dT: vsyncDelta);
931 f.y.update(pos: pos.y(), velocity: velocity.y(), dT: vsyncDelta);
932 pos = QPointF(f.x.position(), f.y.position());
933 } else {
934 f.x.initialize(pos: pos.x(), velocity: velocity.x());
935 f.y.initialize(pos: pos.y(), velocity: velocity.y());
936 // Make sure the first instance of a touch point we send has the
937 // 'pressed' state.
938 if (tp.state != Qt::TouchPointPressed)
939 tp.state = Qt::TouchPointPressed;
940 }
941
942 tp.velocity = QVector2D(f.x.velocity() * winRect.width(), f.y.velocity() * winRect.height());
943
944 qreal filteredNormalizedX = f.x.position() + f.x.velocity() * m_handler->d->m_prediction / 1000.0;
945 qreal filteredNormalizedY = f.y.position() + f.y.velocity() * m_handler->d->m_prediction / 1000.0;
946
947 // Clamp to the screen
948 tp.normalPosition = QPointF(qBound<qreal>(min: 0, val: filteredNormalizedX, max: 1),
949 qBound<qreal>(min: 0, val: filteredNormalizedY, max: 1));
950
951 qreal x = winRect.x() + (tp.normalPosition.x() * (winRect.width() - 1));
952 qreal y = winRect.y() + (tp.normalPosition.y() * (winRect.height() - 1));
953
954 tp.area.moveCenter(p: QPointF(x, y));
955
956 // Store the touch point for later so we can release it if we've
957 // missed the actual release between our last update and this.
958 f.touchPoint = tp;
959
960 // Don't store the point for future reference if it is a release.
961 if (tp.state != Qt::TouchPointReleased)
962 filteredPoints[tp.id] = f;
963 }
964
965 for (QHash<int, FilteredTouchPoint>::const_iterator it = m_filteredPoints.constBegin(), end = m_filteredPoints.constEnd(); it != end; ++it) {
966 const FilteredTouchPoint &f = it.value();
967 QWindowSystemInterface::TouchPoint tp = f.touchPoint;
968 tp.state = Qt::TouchPointReleased;
969 tp.velocity = QVector2D();
970 points.append(t: tp);
971 }
972
973 m_filteredPoints = filteredPoints;
974
975 QWindowSystemInterface::handleTouchEvent(window: nullptr,
976 device: m_handler->touchDevice(),
977 points);
978}
979
980
981QT_END_NAMESPACE
982

source code of qtbase/src/platformsupport/input/evdevtouch/qevdevtouchhandler.cpp