| 1 | /* |
| 2 | * Copyright 2015 Canonical Ltd. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Lesser General Public License as published by |
| 6 | * the Free Software Foundation; version 3. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU Lesser General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU Lesser General Public License |
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | */ |
| 16 | |
| 17 | #include "touchregistry_p.h" |
| 18 | |
| 19 | #include <QtCore/QCoreApplication> |
| 20 | #include <QtCore/QDebug> |
| 21 | #include <QtQuick/private/qquickitem_p.h> |
| 22 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 23 | #include <private/qeventpoint_p.h> |
| 24 | #endif |
| 25 | |
| 26 | #include "candidateinactivitytimer_p.h" |
| 27 | #include "timer_p.h" |
| 28 | #include "touchownershipevent_p.h" |
| 29 | #include "unownedtouchevent_p.h" |
| 30 | |
| 31 | Q_LOGGING_CATEGORY(ugTouchRegistry, "liblomirigestures.TouchRegistry", QtMsgType::QtWarningMsg) |
| 32 | |
| 33 | #define UG_DEBUG qCDebug(ugTouchRegistry) << "[TouchRegistry]" |
| 34 | |
| 35 | UG_NAMESPACE_BEGIN |
| 36 | |
| 37 | TouchRegistry *TouchRegistry::m_instance = nullptr; |
| 38 | |
| 39 | TouchRegistry::TouchRegistry(QObject *parent) |
| 40 | : QObject(parent) |
| 41 | , m_inDispatchLoop(false) |
| 42 | , m_timerFactory(new TimerFactory) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | TouchRegistry::~TouchRegistry() |
| 47 | { |
| 48 | Q_ASSERT(m_instance != nullptr); |
| 49 | m_instance = nullptr; |
| 50 | delete m_timerFactory; |
| 51 | } |
| 52 | |
| 53 | TouchRegistry *TouchRegistry::instance() |
| 54 | { |
| 55 | if (m_instance == nullptr) { |
| 56 | m_instance = new TouchRegistry; |
| 57 | } |
| 58 | return m_instance; |
| 59 | } |
| 60 | |
| 61 | void TouchRegistry::setTimerFactory(AbstractTimerFactory *timerFactory) |
| 62 | { |
| 63 | delete m_timerFactory; |
| 64 | m_timerFactory = timerFactory; |
| 65 | } |
| 66 | |
| 67 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 68 | void TouchRegistry::update(QTouchEvent *event) |
| 69 | #else |
| 70 | void TouchRegistry::update(const QTouchEvent *event) |
| 71 | #endif |
| 72 | { |
| 73 | UG_DEBUG << "got" << event; |
| 74 | |
| 75 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 76 | m_eventsInDelivery.append(event); |
| 77 | #endif |
| 78 | |
| 79 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 80 | const QList<QTouchEvent::TouchPoint> &touchPoints = event->points(); |
| 81 | #else |
| 82 | const QList<QTouchEvent::TouchPoint> &touchPoints = event->touchPoints(); |
| 83 | #endif |
| 84 | for (int i = 0; i < touchPoints.count(); ++i) { |
| 85 | const QTouchEvent::TouchPoint &touchPoint = touchPoints.at(i); |
| 86 | if (touchPoint.state() == Qt::TouchPointPressed) { |
| 87 | TouchInfo &touchInfo = m_touchInfoPool.getEmptySlot(); |
| 88 | touchInfo.init(touchPoint.id()); |
| 89 | } else if (touchPoint.state() == Qt::TouchPointReleased) { |
| 90 | Pool<TouchInfo>::Iterator touchInfo = findTouchInfo(touchPoint.id()); |
| 91 | if (!touchInfo) { |
| 92 | // Possibly an early touch before our eventFilter is installed. |
| 93 | continue; |
| 94 | } |
| 95 | |
| 96 | touchInfo->physicallyEnded = true; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | deliverTouchUpdatesToUndecidedCandidatesAndWatchers(event); |
| 101 | |
| 102 | freeEndedTouchInfos(); |
| 103 | |
| 104 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 105 | m_eventsInDelivery.takeLast(); |
| 106 | #endif |
| 107 | } |
| 108 | |
| 109 | void TouchRegistry::deliverTouchUpdatesToUndecidedCandidatesAndWatchers(const QTouchEvent *event) |
| 110 | { |
| 111 | // TODO: Look into how we could optimize this whole thing. |
| 112 | // Although it's not really a problem as we should have at most two candidates |
| 113 | // for each point and there should not be many active points at any given moment. |
| 114 | // But having three nested for-loops does scare. |
| 115 | |
| 116 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 117 | const QList<QTouchEvent::TouchPoint> &updatedTouchPoints = event->points(); |
| 118 | #else |
| 119 | const QList<QTouchEvent::TouchPoint> &updatedTouchPoints = event->touchPoints(); |
| 120 | #endif |
| 121 | // Maps an item to the touches in this event he should be informed about. |
| 122 | // E.g.: a QTouchEvent might have three touches but a given item might be interested in only |
| 123 | // one of them. So he will get a UnownedTouchEvent from this QTouchEvent containing only that |
| 124 | // touch point. |
| 125 | QHash<QQuickItem*, QList<int>> touchIdsForItems; |
| 126 | |
| 127 | // Build touchIdsForItems |
| 128 | m_touchInfoPool.forEach([&](Pool<TouchInfo>::Iterator &touchInfo) { |
| 129 | if (touchInfo->isOwned() && touchInfo->watchers.isEmpty()) |
| 130 | return true; |
| 131 | |
| 132 | for (int j = 0; j < updatedTouchPoints.count(); ++j) { |
| 133 | if (updatedTouchPoints[j].id() == touchInfo->id) { |
| 134 | if (!touchInfo->isOwned()) { |
| 135 | for (int i = 0; i < touchInfo->candidates.count(); ++i) { |
| 136 | CandidateInfo &candidate = touchInfo->candidates[i]; |
| 137 | Q_ASSERT(!candidate.item.isNull()); |
| 138 | if (candidate.state != CandidateInfo::InterimOwner) { |
| 139 | touchIdsForItems[candidate.item.data()].append(touchInfo->id); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | const QList<QPointer<QQuickItem>> &watchers = touchInfo->watchers; |
| 145 | for (int i = 0; i < watchers.count(); ++i) { |
| 146 | if (!watchers[i].isNull()) { |
| 147 | touchIdsForItems[watchers[i].data()].append(touchInfo->id); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | return true; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | return true; |
| 156 | }); |
| 157 | |
| 158 | // TODO: Consider what happens if an item calls any of TouchRegistry's public methods |
| 159 | // from the event handler callback. |
| 160 | m_inDispatchLoop = true; |
| 161 | auto it = touchIdsForItems.constBegin(); |
| 162 | while (it != touchIdsForItems.constEnd()) { |
| 163 | QQuickItem *item = it.key(); |
| 164 | const QList<int> &touchIds = it.value(); |
| 165 | dispatchPointsToItem(event, touchIds, item); |
| 166 | ++it; |
| 167 | }; |
| 168 | m_inDispatchLoop = false; |
| 169 | } |
| 170 | |
| 171 | void TouchRegistry::freeEndedTouchInfos() |
| 172 | { |
| 173 | m_touchInfoPool.forEach([&](Pool<TouchInfo>::Iterator &touchInfo) { |
| 174 | if (touchInfo->ended()) { |
| 175 | m_touchInfoPool.freeSlot(touchInfo); |
| 176 | } |
| 177 | return true; |
| 178 | }); |
| 179 | } |
| 180 | |
| 181 | /* |
| 182 | Extracts the touches with the given touchIds from event and send them in a |
| 183 | UnownedTouchEvent to the given item |
| 184 | */ |
| 185 | void TouchRegistry::dispatchPointsToItem(const QTouchEvent *event, const QList<int> &touchIds, |
| 186 | QQuickItem *item) |
| 187 | { |
| 188 | #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) |
| 189 | Qt::TouchPointStates touchPointStates; |
| 190 | #endif |
| 191 | QList<QTouchEvent::TouchPoint> touchPoints; |
| 192 | |
| 193 | const QList<QTouchEvent::TouchPoint> &allTouchPoints = event->touchPoints(); |
| 194 | |
| 195 | QTransform windowToCandidateTransform = QQuickItemPrivate::get(item)->windowToItemTransform(); |
| 196 | QMatrix4x4 windowToCandidateMatrix(windowToCandidateTransform); |
| 197 | |
| 198 | for (int i = 0; i < allTouchPoints.count(); ++i) { |
| 199 | const QTouchEvent::TouchPoint &originalTouchPoint = allTouchPoints[i]; |
| 200 | if (touchIds.contains(originalTouchPoint.id())) { |
| 201 | QTouchEvent::TouchPoint touchPoint = originalTouchPoint; |
| 202 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 203 | QMutableEventPoint::detach(touchPoint); |
| 204 | #endif |
| 205 | |
| 206 | translateTouchPointFromScreenToWindowCoords(touchPoint); |
| 207 | |
| 208 | // Set the point's local coordinates to that of the item |
| 209 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 210 | QMutableEventPoint::setPosition(touchPoint, windowToCandidateTransform.map(touchPoint.scenePos())); |
| 211 | #else |
| 212 | touchPoint.setPos(windowToCandidateTransform.map(touchPoint.scenePos())); |
| 213 | #endif |
| 214 | |
| 215 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 216 | QMutableEventPoint::setGlobalPressPosition(touchPoint, windowToCandidateTransform.map(touchPoint.pressPosition())); |
| 217 | QMutableEventPoint::setGlobalLastPosition(touchPoint, windowToCandidateTransform.map(touchPoint.lastPosition())); |
| 218 | QMutableEventPoint::setVelocity(touchPoint, windowToCandidateMatrix.mapVector(touchPoint.velocity().toVector3D()).toVector2D()); |
| 219 | #else |
| 220 | |
| 221 | touchPoint.setStartPos(windowToCandidateTransform.map(touchPoint.startScenePos())); |
| 222 | touchPoint.setLastPos(windowToCandidateTransform.map(touchPoint.lastScenePos())); |
| 223 | touchPoint.setVelocity(windowToCandidateMatrix.mapVector(touchPoint.velocity()).toVector2D()); |
| 224 | #endif |
| 225 | touchPoints.append(touchPoint); |
| 226 | #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) |
| 227 | touchPointStates |= touchPoint.state(); |
| 228 | #endif |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 233 | QTouchEvent *eventForItem = new QTouchEvent(event->type(), |
| 234 | event->pointingDevice(), |
| 235 | event->modifiers(), |
| 236 | touchPoints); |
| 237 | |
| 238 | eventForItem->setTimestamp(event->timestamp()); |
| 239 | QMutableTouchEvent::setTarget(eventForItem, event->target()); |
| 240 | // window() is moved to QEventPoint. |
| 241 | #else |
| 242 | QTouchEvent *eventForItem = new QTouchEvent(event->type(), |
| 243 | event->device(), |
| 244 | event->modifiers(), |
| 245 | touchPointStates, |
| 246 | touchPoints); |
| 247 | eventForItem->setWindow(event->window()); |
| 248 | eventForItem->setTimestamp(event->timestamp()); |
| 249 | eventForItem->setTarget(event->target()); |
| 250 | #endif |
| 251 | |
| 252 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 253 | m_eventsInDelivery.append(eventForItem); |
| 254 | #endif |
| 255 | |
| 256 | UnownedTouchEvent unownedTouchEvent(eventForItem); |
| 257 | |
| 258 | UG_DEBUG << "Sending unowned" << eventForItem |
| 259 | << "to" << item; |
| 260 | |
| 261 | QCoreApplication::sendEvent(item, &unownedTouchEvent); |
| 262 | |
| 263 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 264 | m_eventsInDelivery.takeLast(); |
| 265 | #endif |
| 266 | // `eventForItem` is freed by `unownedTouchEvent`. |
| 267 | } |
| 268 | |
| 269 | void TouchRegistry::translateTouchPointFromScreenToWindowCoords(QTouchEvent::TouchPoint &touchPoint) |
| 270 | { |
| 271 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 272 | QMutableEventPoint::setGlobalPosition(touchPoint, touchPoint.scenePosition()); |
| 273 | QMutableEventPoint::setGlobalPressPosition(touchPoint, touchPoint.scenePressPosition()); |
| 274 | QMutableEventPoint::setGlobalLastPosition(touchPoint, touchPoint.sceneLastPosition()); |
| 275 | |
| 276 | QMutableEventPoint::setScenePosition(touchPoint, touchPoint.position()); |
| 277 | // Qt 6 now calculates scene{Press,Last}Position from global{Press,Last}Position, |
| 278 | // globalPosition, and scenePosition. |
| 279 | #else |
| 280 | touchPoint.setScreenPos(touchPoint.scenePos()); |
| 281 | touchPoint.setStartScreenPos(touchPoint.startScenePos()); |
| 282 | touchPoint.setLastScreenPos(touchPoint.lastScenePos()); |
| 283 | |
| 284 | touchPoint.setScenePos(touchPoint.pos()); |
| 285 | touchPoint.setStartScenePos(touchPoint.startPos()); |
| 286 | touchPoint.setLastScenePos(touchPoint.lastPos()); |
| 287 | #endif |
| 288 | } |
| 289 | |
| 290 | bool TouchRegistry::eventFilter(QObject *watched, QEvent *event) |
| 291 | { |
| 292 | Q_UNUSED(watched); |
| 293 | |
| 294 | switch (event->type()) { |
| 295 | case QEvent::TouchBegin: |
| 296 | case QEvent::TouchUpdate: |
| 297 | case QEvent::TouchEnd: |
| 298 | case QEvent::TouchCancel: |
| 299 | update(static_cast<QTouchEvent*>(event)); |
| 300 | break; |
| 301 | default: |
| 302 | // do nothing |
| 303 | break; |
| 304 | } |
| 305 | |
| 306 | // Do not filter out the event. i.e., let it be handled further as |
| 307 | // we're just monitoring events |
| 308 | return false; |
| 309 | } |
| 310 | |
| 311 | void TouchRegistry::addCandidateOwnerForTouch(int id, QQuickItem *candidate) |
| 312 | { |
| 313 | UG_DEBUG << "addCandidateOwnerForTouch id" << id << "candidate" << candidate; |
| 314 | |
| 315 | Pool<TouchInfo>::Iterator touchInfo = findTouchInfo(id); |
| 316 | if (!touchInfo) { |
| 317 | qCritical("TouchRegistry: Failed to find TouchInfo"); |
| 318 | return; |
| 319 | } |
| 320 | |
| 321 | if (touchInfo->isOwned()) { |
| 322 | qWarning("TouchRegistry: trying to add candidate owner for a touch that's already owned"); |
| 323 | return; |
| 324 | } |
| 325 | |
| 326 | // TODO: Check if candidate already exists |
| 327 | |
| 328 | CandidateInfo candidateInfo; |
| 329 | candidateInfo.state = CandidateInfo::Undecided; |
| 330 | candidateInfo.item = candidate; |
| 331 | candidateInfo.inactivityTimer = new CandidateInactivityTimer(id, candidate, |
| 332 | m_timerFactory->createTimer(), |
| 333 | this); |
| 334 | connect(candidateInfo.inactivityTimer.data(), &CandidateInactivityTimer::candidateDefaulted, |
| 335 | this, &TouchRegistry::rejectCandidateOwnerForTouch); |
| 336 | |
| 337 | touchInfo->candidates.append(candidateInfo); |
| 338 | |
| 339 | connect(candidate, &QObject::destroyed, this, [=](){ pruneNullCandidatesForTouch(id); }); |
| 340 | } |
| 341 | |
| 342 | void TouchRegistry::addTouchWatcher(int touchId, QQuickItem *watcher) |
| 343 | { |
| 344 | UG_DEBUG << "addTouchWatcher id" << touchId << "watcher" << watcher; |
| 345 | |
| 346 | Pool<TouchInfo>::Iterator touchInfo = findTouchInfo(touchId); |
| 347 | if (!touchInfo) { |
| 348 | qCritical("TouchRegistry: Failed to find TouchInfo"); |
| 349 | return; |
| 350 | } |
| 351 | |
| 352 | // TODO: Check if watcher already exists |
| 353 | |
| 354 | touchInfo->watchers.append(watcher); |
| 355 | } |
| 356 | |
| 357 | void TouchRegistry::removeCandidateOwnerForTouch(int id, QQuickItem *candidate) |
| 358 | { |
| 359 | UG_DEBUG << "removeCandidateOwnerForTouch id" << id << "candidate" << candidate; |
| 360 | |
| 361 | Pool<TouchInfo>::Iterator touchInfo = findTouchInfo(id); |
| 362 | if (!touchInfo) { |
| 363 | qCritical("TouchRegistry: Failed to find TouchInfo"); |
| 364 | return; |
| 365 | } |
| 366 | |
| 367 | // TODO: check if the candidate is in fact the owner of the touch |
| 368 | |
| 369 | bool removed = false; |
| 370 | for (int i = 0; i < touchInfo->candidates.count() && !removed; ++i) { |
| 371 | if (touchInfo->candidates[i].item == candidate) { |
| 372 | removeCandidateOwnerForTouchByIndex(touchInfo, i); |
| 373 | removed = true; |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | void TouchRegistry::pruneNullCandidatesForTouch(int touchId) |
| 379 | { |
| 380 | UG_DEBUG << "pruneNullCandidatesForTouch touchId" << touchId; |
| 381 | |
| 382 | Pool<TouchInfo>::Iterator touchInfo = findTouchInfo(touchId); |
| 383 | if (!touchInfo) { |
| 384 | // doesn't matter as touch is already gone. |
| 385 | return; |
| 386 | } |
| 387 | |
| 388 | int i = 0; |
| 389 | while (i < touchInfo->candidates.count()) { |
| 390 | if (touchInfo->candidates[i].item.isNull()) { |
| 391 | removeCandidateOwnerForTouchByIndex(touchInfo, i); |
| 392 | } else { |
| 393 | ++i; |
| 394 | } |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | void TouchRegistry::removeCandidateOwnerForTouchByIndex(Pool<TouchRegistry::TouchInfo>::Iterator &touchInfo, |
| 399 | int candidateIndex) |
| 400 | { |
| 401 | // TODO: check if the candidate is in fact the owner of the touch |
| 402 | |
| 403 | Q_ASSERT(candidateIndex < touchInfo->candidates.count()); |
| 404 | |
| 405 | if (candidateIndex == 0 && touchInfo->candidates[candidateIndex].state != CandidateInfo::Undecided) { |
| 406 | qCritical("TouchRegistry: touch owner is being removed."); |
| 407 | } |
| 408 | |
| 409 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 410 | QQuickItem * candidateItem = touchInfo->candidates[candidateIndex].item; |
| 411 | #endif |
| 412 | |
| 413 | removeCandidateHelper(touchInfo, candidateIndex); |
| 414 | |
| 415 | if (candidateIndex == 0 && touchInfo->isOwned()) { |
| 416 | // the top candidate has been removed. if the new top candidate |
| 417 | // wants the touch let him know he's now the owner. |
| 418 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 419 | auto [pointerEvent, point] = findMostRelavantEventForId( |
| 420 | touchInfo->id, candidateItem); |
| 421 | touchInfo->notifyCandidatesOfOwnershipResolution(pointerEvent, point); |
| 422 | #else |
| 423 | touchInfo->notifyCandidatesOfOwnershipResolution(); |
| 424 | #endif |
| 425 | } |
| 426 | |
| 427 | if (!m_inDispatchLoop && touchInfo->ended()) { |
| 428 | m_touchInfoPool.freeSlot(touchInfo); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | void TouchRegistry::requestTouchOwnership(int id, QQuickItem *candidate) |
| 433 | { |
| 434 | UG_DEBUG << "requestTouchOwnership id " << id << "candidate" << candidate; |
| 435 | |
| 436 | Pool<TouchInfo>::Iterator touchInfo = findTouchInfo(id); |
| 437 | if (!touchInfo) { |
| 438 | qCritical("TouchRegistry: Failed to find TouchInfo"); |
| 439 | return; |
| 440 | } |
| 441 | |
| 442 | Q_ASSERT(!touchInfo->isOwned()); |
| 443 | |
| 444 | int candidateIndex = -1; |
| 445 | for (int i = 0; i < touchInfo->candidates.count(); ++i) { |
| 446 | CandidateInfo &candidateInfo = touchInfo->candidates[i]; |
| 447 | if (candidateInfo.item == candidate) { |
| 448 | candidateInfo.state = CandidateInfo::Requested; |
| 449 | delete candidateInfo.inactivityTimer; |
| 450 | candidateInfo.inactivityTimer = nullptr; |
| 451 | candidateIndex = i; |
| 452 | break; |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | // add it as a candidate if not present yet |
| 457 | if (candidateIndex < 0) { |
| 458 | CandidateInfo candidateInfo; |
| 459 | candidateInfo.state = CandidateInfo::InterimOwner; |
| 460 | candidateInfo.item = candidate; |
| 461 | candidateInfo.inactivityTimer = nullptr; |
| 462 | touchInfo->candidates.append(candidateInfo); |
| 463 | // it's the last one |
| 464 | candidateIndex = touchInfo->candidates.count() - 1; |
| 465 | connect(candidate, &QObject::destroyed, this, [=](){ pruneNullCandidatesForTouch(id); }); |
| 466 | } |
| 467 | |
| 468 | // If it's the top candidate it means it's now the owner. Let |
| 469 | // it know about it. |
| 470 | if (candidateIndex == 0) { |
| 471 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 472 | auto [pointerEvent, point] = findMostRelavantEventForId(id, candidate); |
| 473 | touchInfo->notifyCandidatesOfOwnershipResolution(pointerEvent, point); |
| 474 | #else |
| 475 | touchInfo->notifyCandidatesOfOwnershipResolution(); |
| 476 | #endif |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | Pool<TouchRegistry::TouchInfo>::Iterator TouchRegistry::findTouchInfo(int id) |
| 481 | { |
| 482 | Pool<TouchInfo>::Iterator touchInfo; |
| 483 | |
| 484 | m_touchInfoPool.forEach([&](Pool<TouchInfo>::Iterator &someTouchInfo) -> bool { |
| 485 | if (someTouchInfo->id == id) { |
| 486 | touchInfo = someTouchInfo; |
| 487 | return false; |
| 488 | } else { |
| 489 | return true; |
| 490 | } |
| 491 | }); |
| 492 | |
| 493 | return touchInfo; |
| 494 | } |
| 495 | |
| 496 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 497 | static const QEventPoint * findPointInEvent(const QPointerEvent *event, int id) |
| 498 | { |
| 499 | if (!event) |
| 500 | return nullptr; |
| 501 | |
| 502 | const auto & points = event->points(); |
| 503 | |
| 504 | for (const auto & point : points) { |
| 505 | if (point.id() == id) |
| 506 | return &point; |
| 507 | } |
| 508 | |
| 509 | return nullptr; |
| 510 | } |
| 511 | |
| 512 | std::pair<QPointerEvent *, const QEventPoint *> |
| 513 | TouchRegistry::findMostRelavantEventForId(int id, QQuickItem *item) |
| 514 | { |
| 515 | QPointerEvent *event = nullptr; |
| 516 | const QEventPoint *point = nullptr; |
| 517 | |
| 518 | for (int i = m_eventsInDelivery.size() - 1; i >= 0; i--) { |
| 519 | point = findPointInEvent(m_eventsInDelivery[i], id); |
| 520 | if (point) { |
| 521 | event = m_eventsInDelivery[i]; |
| 522 | break; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | if (!event && item) { |
| 527 | #if QT_VERSION >= QT_VERSION_CHECK(6, 1, 0) |
| 528 | QQuickItemPrivate *itemPriv = QQuickItemPrivate::get(item); |
| 529 | auto *dap = itemPriv->deliveryAgentPrivate(); |
| 530 | auto quickEventInDelivery = dap ? dap->eventInDelivery() : nullptr; |
| 531 | #else |
| 532 | QQuickWindowPrivate *windowPriv = QQuickWindowPrivate::get(window()); |
| 533 | auto quickEventInDelivery = windowPriv->eventInDelivery(); |
| 534 | #endif |
| 535 | |
| 536 | point = findPointInEvent(quickEventInDelivery, id); |
| 537 | if (point) { |
| 538 | event = quickEventInDelivery; |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | return { event, point }; |
| 543 | } |
| 544 | #endif |
| 545 | |
| 546 | void TouchRegistry::rejectCandidateOwnerForTouch(int id, QQuickItem *candidate) |
| 547 | { |
| 548 | // NB: It's technically possible that candidate is a dangling pointer at this point. |
| 549 | // Although that would most likely be due to a bug in our code. |
| 550 | // In any case, only dereference it after it's confirmed that it indeed exists. |
| 551 | |
| 552 | UG_DEBUG << "rejectCandidateOwnerForTouch id" << id << "candidate" << (void*)candidate; |
| 553 | |
| 554 | Pool<TouchInfo>::Iterator touchInfo = findTouchInfo(id); |
| 555 | if (!touchInfo) { |
| 556 | UG_DEBUG << "Failed to find TouchInfo for id" << id; |
| 557 | return; |
| 558 | } |
| 559 | |
| 560 | int rejectedCandidateIndex = -1; |
| 561 | |
| 562 | // Check if the given candidate is valid and still undecided |
| 563 | for (int i = 0; i < touchInfo->candidates.count() && rejectedCandidateIndex == -1; ++i) { |
| 564 | CandidateInfo &candidateInfo = touchInfo->candidates[i]; |
| 565 | if (candidateInfo.item == candidate) { |
| 566 | Q_ASSERT(i > 0 || candidateInfo.state == CandidateInfo::Undecided); |
| 567 | if (i == 0 && candidateInfo.state != CandidateInfo::Undecided) { |
| 568 | qCritical() << "TouchRegistry: Can't reject item (" << (void*)candidate |
| 569 | << ") as it already owns touch" << id; |
| 570 | return; |
| 571 | } else { |
| 572 | // we found the guy and it's all fine. |
| 573 | rejectedCandidateIndex = i; |
| 574 | } |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | // If we reached this point it's because the given candidate exists and is indeed undecided. |
| 579 | |
| 580 | Q_ASSERT(rejectedCandidateIndex >= 0 && rejectedCandidateIndex < touchInfo->candidates.size()); |
| 581 | |
| 582 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 583 | auto [pointerEvent, point] = findMostRelavantEventForId(id, candidate); |
| 584 | if (!pointerEvent) { |
| 585 | qWarning() << "Can't find a QPointerEvent for touch id" << id; |
| 586 | // XXX: proceed to send TouchOwnershipEvent; even without pointerEvent |
| 587 | // the item losing focus might still have to do internal rollback. |
| 588 | } |
| 589 | #endif |
| 590 | |
| 591 | { |
| 592 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 593 | TouchOwnershipEvent lostOwnershipEvent( |
| 594 | id, false /*gained*/, pointerEvent, point); |
| 595 | #else |
| 596 | TouchOwnershipEvent lostOwnershipEvent(id, false /*gained*/); |
| 597 | #endif |
| 598 | QCoreApplication::sendEvent(candidate, &lostOwnershipEvent); |
| 599 | } |
| 600 | |
| 601 | removeCandidateHelper(touchInfo, rejectedCandidateIndex); |
| 602 | |
| 603 | if (rejectedCandidateIndex == 0 && touchInfo->isOwned()) { |
| 604 | // the top candidate has been removed. if the new top candidate |
| 605 | // wants the touch let him know he's now the owner. |
| 606 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 607 | touchInfo->notifyCandidatesOfOwnershipResolution(pointerEvent, point); |
| 608 | #else |
| 609 | touchInfo->notifyCandidatesOfOwnershipResolution(); |
| 610 | #endif |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | void TouchRegistry::removeCandidateHelper(Pool<TouchInfo>::Iterator &touchInfo, int candidateIndex) |
| 615 | { |
| 616 | { |
| 617 | CandidateInfo &candidateInfo = touchInfo->candidates[candidateIndex]; |
| 618 | |
| 619 | delete candidateInfo.inactivityTimer; |
| 620 | candidateInfo.inactivityTimer = nullptr; |
| 621 | |
| 622 | if (candidateInfo.item) { |
| 623 | disconnect(candidateInfo.item.data(), nullptr, this, nullptr); |
| 624 | } |
| 625 | } |
| 626 | touchInfo->candidates.removeAt(candidateIndex); |
| 627 | } |
| 628 | |
| 629 | ////////////////////////////////////// TouchRegistry::TouchInfo //////////////////////////////////// |
| 630 | |
| 631 | TouchRegistry::TouchInfo::TouchInfo(int id) |
| 632 | { |
| 633 | init(id); |
| 634 | } |
| 635 | |
| 636 | void TouchRegistry::TouchInfo::reset() |
| 637 | { |
| 638 | id = -1; |
| 639 | |
| 640 | for (int i = 0; i < candidates.count(); ++i) { |
| 641 | CandidateInfo &candidate = candidates[i]; |
| 642 | delete candidate.inactivityTimer; |
| 643 | candidate.inactivityTimer.clear(); // shoundn't be needed but anyway... |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | void TouchRegistry::TouchInfo::init(int id) |
| 648 | { |
| 649 | this->id = id; |
| 650 | physicallyEnded = false; |
| 651 | candidates.clear(); |
| 652 | watchers.clear(); |
| 653 | } |
| 654 | |
| 655 | bool TouchRegistry::TouchInfo::isOwned() const |
| 656 | { |
| 657 | return !candidates.isEmpty() && candidates.first().state != CandidateInfo::Undecided; |
| 658 | } |
| 659 | |
| 660 | bool TouchRegistry::TouchInfo::ended() const |
| 661 | { |
| 662 | Q_ASSERT(isValid()); |
| 663 | return physicallyEnded && (isOwned() || candidates.isEmpty()); |
| 664 | } |
| 665 | |
| 666 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 667 | void TouchRegistry::TouchInfo::notifyCandidatesOfOwnershipResolution( |
| 668 | QPointerEvent *pointerEvent, const QEventPoint *point) |
| 669 | #else |
| 670 | void TouchRegistry::TouchInfo::notifyCandidatesOfOwnershipResolution() |
| 671 | #endif |
| 672 | { |
| 673 | Q_ASSERT(isOwned()); |
| 674 | |
| 675 | UG_DEBUG << "sending TouchOwnershipEvent(id =" << id |
| 676 | << " gained) to candidate" << candidates[0].item; |
| 677 | |
| 678 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 679 | if (!pointerEvent) { |
| 680 | qWarning() << "Can't find a QPointerEvent for touch id" << id |
| 681 | << ", candidate" << candidates[0].item |
| 682 | << "might not be able to grab the point"; |
| 683 | } |
| 684 | #endif |
| 685 | |
| 686 | // need to take a copy of the item list in case |
| 687 | // we call back in to remove candidate during the lost ownership event. |
| 688 | QList<QPointer<QQuickItem>> items; |
| 689 | Q_FOREACH(const CandidateInfo& info, candidates) { |
| 690 | items << info.item; |
| 691 | } |
| 692 | |
| 693 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 694 | TouchOwnershipEvent gainedOwnershipEvent( |
| 695 | id, true /*gained*/, pointerEvent, point); |
| 696 | #else |
| 697 | TouchOwnershipEvent gainedOwnershipEvent(id, true /*gained*/); |
| 698 | #endif |
| 699 | QCoreApplication::sendEvent(items[0], &gainedOwnershipEvent); |
| 700 | |
| 701 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 702 | TouchOwnershipEvent lostOwnershipEvent( |
| 703 | id, false /*gained*/, pointerEvent, point); |
| 704 | #else |
| 705 | TouchOwnershipEvent lostOwnershipEvent(id, false /*gained*/); |
| 706 | #endif |
| 707 | |
| 708 | for (int i = 1; i < items.count(); ++i) { |
| 709 | UG_DEBUG << "sending TouchOwnershipEvent(id =" << id << " lost) to candidate" |
| 710 | << items[i]; |
| 711 | QCoreApplication::sendEvent(items[i], &lostOwnershipEvent); |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | UG_NAMESPACE_END |