From 428115340bee4357d6821eee78e1fff0ae910712 Mon Sep 17 00:00:00 2001 From: Mike Krus Date: Fri, 22 May 2020 15:38:24 +0100 Subject: Fix UITouch event handling on tvOS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On tvOS touchesEnded: occasionally gets called with touches that have not been passed via the touchesBegan:. When this happens previously cached touch event (that HAVE been passed to touchesBegan:) are no longer valid. This causes a crash when testing if new touches contain old ones (since NSSet dereferences the needle which is no longer valid). Fix uses the unique (unsigned int) hash that UIKIT assigns to the UITouch instance so cached copies are never accessed. Furthermore, tvOS only supports single touch so now just clearing cache when touch has ended. Task-number: QTBUG-84383 Pick-to: 5.15 Pick-to: 5.12 Change-Id: I7592cdde74ce834285e7b14196171f6b57736cc8 Reviewed-by: Tor Arne Vestbø --- src/plugins/platforms/ios/quiview.mm | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) (limited to 'src/plugins/platforms/ios/quiview.mm') diff --git a/src/plugins/platforms/ios/quiview.mm b/src/plugins/platforms/ios/quiview.mm index 172d6ebcef..0043016fb0 100644 --- a/src/plugins/platforms/ios/quiview.mm +++ b/src/plugins/platforms/ios/quiview.mm @@ -57,7 +57,7 @@ Q_LOGGING_CATEGORY(lcQpaTablet, "qt.qpa.input.tablet") @implementation QUIView { - QHash m_activeTouches; + QHash m_activeTouches; UITouch *m_activePencilTouch; int m_nextTouchId; NSMutableArray *m_accessibleElements; @@ -404,9 +404,19 @@ Q_LOGGING_CATEGORY(lcQpaTablet, "qt.qpa.input.tablet") } #endif - for (UITouch *uiTouch : m_activeTouches.keys()) { - QWindowSystemInterface::TouchPoint &touchPoint = m_activeTouches[uiTouch]; - if (![touches containsObject:uiTouch]) { + if (m_activeTouches.isEmpty()) + return; + for (auto it = m_activeTouches.begin(); it != m_activeTouches.end(); ++it) { + auto hash = it.key(); + QWindowSystemInterface::TouchPoint &touchPoint = it.value(); + UITouch *uiTouch = nil; + for (UITouch *touch in touches) { + if (touch.hash == hash) { + uiTouch = touch; + break; + } + } + if (!uiTouch) { touchPoint.state = Qt::TouchPointStationary; } else { touchPoint.state = state; @@ -438,8 +448,6 @@ Q_LOGGING_CATEGORY(lcQpaTablet, "qt.qpa.input.tablet") } } } - if (m_activeTouches.isEmpty()) - return; if ([self.window isKindOfClass:[QUIWindow class]] && !static_cast(self.window).sendingEvent) { @@ -475,9 +483,9 @@ Q_LOGGING_CATEGORY(lcQpaTablet, "qt.qpa.input.tablet") m_activePencilTouch = touch; } else { - Q_ASSERT(!m_activeTouches.contains(touch)); + Q_ASSERT(!m_activeTouches.contains(touch.hash)); #endif - m_activeTouches[touch].id = m_nextTouchId++; + m_activeTouches[touch.hash].id = m_nextTouchId++; #if QT_CONFIG(tabletevent) } #endif @@ -504,6 +512,7 @@ Q_LOGGING_CATEGORY(lcQpaTablet, "qt.qpa.input.tablet") [self handleTouches:touches withEvent:event withState:Qt::TouchPointReleased withTimestamp:ulong(event.timestamp * 1000)]; // Remove ended touch points from the active set: +#ifndef Q_OS_TVOS for (UITouch *touch in touches) { #if QT_CONFIG(tabletevent) if (touch.type == UITouchTypeStylus) { @@ -511,9 +520,14 @@ Q_LOGGING_CATEGORY(lcQpaTablet, "qt.qpa.input.tablet") } else #endif { - m_activeTouches.remove(touch); + m_activeTouches.remove(touch.hash); } } +#else + // tvOS only supports single touch + m_activeTouches.clear(); +#endif + if (m_activeTouches.isEmpty() && !m_activePencilTouch) m_nextTouchId = 0; } -- cgit v1.2.3