From 8984c42d1779b13fd29d95274af2d01d32528e52 Mon Sep 17 00:00:00 2001 From: Jan Arve Saether Date: Tue, 23 Aug 2016 10:44:20 +0200 Subject: Combine device and point id into 32 bit point id This is second attempt of change cd26e66c2e8ddde06b5e22 This allows us to not have conflicts between the point ids between different devices for QtQuick pointer handlers. We do this in QtGui because we can then safely compare point ids from QTouchEvent::TouchPoint and QQuickEventPoint. (Point ids that QtQuick pointer handlers use will be based on the point ids provided by QTouchEvent::TouchPoint::id) [ChangeLog][QtGui][QTouchEvent][Important Behavior Changes] Touch point ids are now unique even between different devices. As a consequence of that, you cannot anymore assume that QTouchEvent::TouchPoint::id has the same value as given by the native platform nor the same value as given by synthesized touch points. Change-Id: Iad2fd8c6a43ccc571a227a01134a1e8f829dfaf4 Reviewed-by: Frederik Gladhorn --- src/gui/kernel/qguiapplication.cpp | 4 ++- src/gui/kernel/qtouchdevice.h | 1 + src/gui/kernel/qtouchdevice_p.h | 7 ++++- src/gui/kernel/qwindowsysteminterface.cpp | 44 ++++++++++++++++++++++++++++--- src/gui/kernel/qwindowsysteminterface_p.h | 2 +- 5 files changed, 52 insertions(+), 6 deletions(-) (limited to 'src/gui/kernel') diff --git a/src/gui/kernel/qguiapplication.cpp b/src/gui/kernel/qguiapplication.cpp index d8457bc041..76386a30a0 100644 --- a/src/gui/kernel/qguiapplication.cpp +++ b/src/gui/kernel/qguiapplication.cpp @@ -84,6 +84,7 @@ #include "private/qcursor_p.h" #include "private/qopenglcontext_p.h" #include "private/qinputdevicemanager_p.h" +#include "private/qtouchdevice_p.h" #include "private/qdnd_p.h" #include @@ -1944,7 +1945,8 @@ void QGuiApplicationPrivate::processMouseEvent(QWindowSystemInterfacePrivate::Mo points << point; QEvent::Type type; - QList touchPoints = QWindowSystemInterfacePrivate::fromNativeTouchPoints(points, window, &type); + QList touchPoints = + QWindowSystemInterfacePrivate::fromNativeTouchPoints(points, window, QTouchDevicePrivate::get(m_fakeTouchDevice)->id, &type); QWindowSystemInterfacePrivate::TouchEvent fake(window, e->timestamp, type, m_fakeTouchDevice, touchPoints, e->modifiers); fake.flags |= QWindowSystemInterfacePrivate::WindowSystemEvent::Synthetic; diff --git a/src/gui/kernel/qtouchdevice.h b/src/gui/kernel/qtouchdevice.h index 0fb24e47bf..c98aa69236 100644 --- a/src/gui/kernel/qtouchdevice.h +++ b/src/gui/kernel/qtouchdevice.h @@ -87,6 +87,7 @@ public: private: QTouchDevicePrivate *d; + friend class QTouchDevicePrivate; }; Q_DECLARE_OPERATORS_FOR_FLAGS(QTouchDevice::Capabilities) diff --git a/src/gui/kernel/qtouchdevice_p.h b/src/gui/kernel/qtouchdevice_p.h index 203d9fca74..18d2af46a7 100644 --- a/src/gui/kernel/qtouchdevice_p.h +++ b/src/gui/kernel/qtouchdevice_p.h @@ -64,16 +64,21 @@ public: : type(QTouchDevice::TouchScreen), caps(QTouchDevice::Position), maxTouchPoints(1) - { } + { + static quint8 nextId = 2; // device 0 is not used, device 1 is for mouse device + id = nextId++; + } QTouchDevice::DeviceType type; QTouchDevice::Capabilities caps; QString name; int maxTouchPoints; + quint8 id; static void registerDevice(const QTouchDevice *dev); static void unregisterDevice(const QTouchDevice *dev); static bool isRegistered(const QTouchDevice *dev); + static QTouchDevicePrivate *get(QTouchDevice *q) { return q->d; } }; QT_END_NAMESPACE diff --git a/src/gui/kernel/qwindowsysteminterface.cpp b/src/gui/kernel/qwindowsysteminterface.cpp index b799f75090..08aa625112 100644 --- a/src/gui/kernel/qwindowsysteminterface.cpp +++ b/src/gui/kernel/qwindowsysteminterface.cpp @@ -502,9 +502,41 @@ bool QWindowSystemInterface::isTouchDeviceRegistered(const QTouchDevice *device) return QTouchDevicePrivate::isRegistered(device); } +static int g_nextPointId = 1; + +// map from device-independent point id (arbitrary) to "Qt point" ids +typedef QMap PointIdMap; +Q_GLOBAL_STATIC(PointIdMap, g_pointIdMap) + +/*! + \internal + This function maps potentially arbitrary point ids \a pointId in the 32 bit + value space to start from 1 and increase incrementally for each touch point + held down. If all touch points are released it will reset the id back to 1 + for the following touch point. + + We can then assume that the touch points ids will never become too large, + and it will then put the device identifier \a deviceId in the upper 8 bits. + This leaves us with max 255 devices, and 16.7M taps without full release + before we run out of value space. +*/ +static int acquireCombinedPointId(quint8 deviceId, int pointId) +{ + quint64 combinedId64 = (quint64(deviceId) << 32) + pointId; + auto it = g_pointIdMap->constFind(combinedId64); + int uid; + if (it == g_pointIdMap->constEnd()) { + uid = g_nextPointId++; + g_pointIdMap->insert(combinedId64, uid); + } else { + uid = *it; + } + return (deviceId << 24) + uid; +} + QList QWindowSystemInterfacePrivate::fromNativeTouchPoints(const QList &points, - const QWindow *window, + const QWindow *window, quint8 deviceId, QEvent::Type *type) { QList touchPoints; @@ -515,7 +547,7 @@ QList QList::const_iterator point = points.constBegin(); QList::const_iterator end = points.constEnd(); while (point != end) { - p.setId(point->id); + p.setId(acquireCombinedPointId(deviceId, point->id)); if (point->uniqueId >= 0) p.setUniqueId(point->uniqueId); p.setPressure(point->pressure); @@ -548,6 +580,11 @@ QList *type = QEvent::TouchEnd; } + if (states == Qt::TouchPointReleased) { + g_nextPointId = 1; + g_pointIdMap->clear(); + } + return touchPoints; } @@ -589,7 +626,8 @@ QT_DEFINE_QPA_EVENT_HANDLER(void, handleTouchEvent, QWindow *tlw, ulong timestam return; QEvent::Type type; - QList touchPoints = QWindowSystemInterfacePrivate::fromNativeTouchPoints(points, tlw, &type); + QList touchPoints = + QWindowSystemInterfacePrivate::fromNativeTouchPoints(points, tlw, QTouchDevicePrivate::get(device)->id, &type); QWindowSystemInterfacePrivate::TouchEvent *e = new QWindowSystemInterfacePrivate::TouchEvent(tlw, timestamp, type, device, touchPoints, mods); diff --git a/src/gui/kernel/qwindowsysteminterface_p.h b/src/gui/kernel/qwindowsysteminterface_p.h index 7c9b1f2852..ced2d01f73 100644 --- a/src/gui/kernel/qwindowsysteminterface_p.h +++ b/src/gui/kernel/qwindowsysteminterface_p.h @@ -508,7 +508,7 @@ public: static QList fromNativeTouchPoints(const QList &points, - const QWindow *window, QEvent::Type *type = Q_NULLPTR); + const QWindow *window, quint8 deviceId, QEvent::Type *type = Q_NULLPTR); static QList toNativeTouchPoints(const QList& pointList, const QWindow *window); -- cgit v1.2.3