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