on b63fa92 help sign in
unity8/plugins/Ubuntu/Gestures/TouchGate.cpp
251 lines 7.5 kB history raw
1/*
2 * Copyright (C) 2014 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU 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 General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
17#include "TouchGate.h"
19#include <QCoreApplication>
20#include <QDebug>
22#include <TouchOwnershipEvent.h>
23#include <TouchRegistry.h>
25#if TOUCHGATE_DEBUG
26#include <DebugHelpers.h>
27#endif
29bool TouchGate::event(QEvent *e)
31 if (e->type() == TouchOwnershipEvent::touchOwnershipEventType()) {
32 touchOwnershipEvent(static_cast<TouchOwnershipEvent *>(e));
33 return true;
34 } else {
35 return QQuickItem::event(e);
36 }
39void TouchGate::touchEvent(QTouchEvent *event)
41 #if TOUCHGATE_DEBUG
42 qDebug() << "[TouchGate] got touch event" << qPrintable(touchEventToString(event));
43 #endif
44 event->accept();
46 const QList<QTouchEvent::TouchPoint> &touchPoints = event->touchPoints();
47 bool goodToGo = true;
48 for (int i = 0; i < touchPoints.count(); ++i) {
49 const QTouchEvent::TouchPoint &touchPoint = touchPoints[i];
51 if (touchPoint.state() == Qt::TouchPointPressed) {
52 Q_ASSERT(!m_touchInfoMap.contains(touchPoint.id()));
53 m_touchInfoMap[touchPoint.id()].ownership = OwnershipRequested;
54 m_touchInfoMap[touchPoint.id()].ended = false;
55 TouchRegistry::instance()->requestTouchOwnership(touchPoint.id(), this);
56 }
58 goodToGo &= m_touchInfoMap.contains(touchPoint.id())
59 && m_touchInfoMap[touchPoint.id()].ownership == OwnershipGranted;
61 if (touchPoint.state() == Qt::TouchPointReleased && m_touchInfoMap.contains(touchPoint.id())) {
62 m_touchInfoMap[touchPoint.id()].ended = true;
63 }
65 }
67 if (goodToGo) {
68 if (m_storedEvents.isEmpty()) {
69 // let it pass through
70 dispatchTouchEventToTarget(event);
71 } else {
72 // Retain the event to ensure TouchGate dispatches them in order.
73 // Otherwise the current event would come before the stored ones, which are older.
74 #if TOUCHGATE_DEBUG
75 qDebug("[TouchGate] Storing event because thouches %s are still pending ownership.",
76 qPrintable(oldestPendingTouchIdsString()));
77 #endif
78 storeTouchEvent(event);
79 }
80 } else {
81 // Retain events that have unowned touches
82 storeTouchEvent(event);
83 }
86void TouchGate::touchOwnershipEvent(TouchOwnershipEvent *event)
88 // TODO: Optimization: batch those actions as TouchOwnershipEvents
89 // might come one right after the other.
91 Q_ASSERT(m_touchInfoMap.contains(event->touchId()));
93 TouchInfo &touchInfo = m_touchInfoMap[event->touchId()];
95 if (event->gained()) {
96 #if TOUCHGATE_DEBUG
97 qDebug() << "[TouchGate] Got ownership of touch " << event->touchId();
98 #endif
99 touchInfo.ownership = OwnershipGranted;
100 } else {
101 #if TOUCHGATE_DEBUG
102 qDebug() << "[TouchGate] Lost ownership of touch " << event->touchId();
103 #endif
104 m_touchInfoMap.remove(event->touchId());
105 removeTouchFromStoredEvents(event->touchId());
106 }
108 dispatchFullyOwnedEvents();
111bool TouchGate::isTouchPointOwned(int touchId) const
113 return m_touchInfoMap[touchId].ownership == OwnershipGranted;
116void TouchGate::storeTouchEvent(const QTouchEvent *event)
118 #if TOUCHGATE_DEBUG
119 qDebug() << "[TouchGate] Storing" << qPrintable(touchEventToString(event));
120 #endif
122 TouchEvent clonedEvent(event);
123 m_storedEvents.append(std::move(clonedEvent));
126void TouchGate::removeTouchFromStoredEvents(int touchId)
128 int i = 0;
129 while (i < m_storedEvents.count()) {
130 TouchEvent &event = m_storedEvents[i];
131 bool removed = event.removeTouch(touchId);
133 if (removed && event.touchPoints.isEmpty()) {
134 m_storedEvents.removeAt(i);
135 } else {
136 ++i;
137 }
138 }
141void TouchGate::dispatchFullyOwnedEvents()
143 while (!m_storedEvents.isEmpty() && eventIsFullyOwned(m_storedEvents.first())) {
144 TouchEvent event = m_storedEvents.takeFirst();
145 dispatchTouchEventToTarget(event);
146 }
149#if TOUCHGATE_DEBUG
150QString TouchGate::oldestPendingTouchIdsString()
152 Q_ASSERT(!m_storedEvents.isEmpty());
154 QString str;
156 const auto &touchPoints = m_storedEvents.first().touchPoints;
157 for (int i = 0; i < touchPoints.count(); ++i) {
158 if (!isTouchPointOwned(touchPoints[i].id())) {
159 if (!str.isEmpty()) {
160 str.append(", ");
161 }
162 str.append(QString::number(touchPoints[i].id()));
163 }
164 }
166 return str;
168#endif
170bool TouchGate::eventIsFullyOwned(const TouchGate::TouchEvent &event) const
172 for (int i = 0; i < event.touchPoints.count(); ++i) {
173 if (!isTouchPointOwned(event.touchPoints[i].id())) {
174 return false;
175 }
176 }
178 return true;
181void TouchGate::setTargetItem(QQuickItem *item)
183 // TODO: changing the target item while dispatch of touch events is taking place will
184 // create a mess
186 if (item == m_dispatcher.targetItem())
187 return;
189 m_dispatcher.setTargetItem(item);
190 Q_EMIT targetItemChanged(item);
193void TouchGate::dispatchTouchEventToTarget(const TouchEvent &event)
195 removeTouchInfoForEndedTouches(event.touchPoints);
196 m_dispatcher.dispatch(event.eventType,
197 event.device,
198 event.modifiers,
199 event.touchPoints,
200 event.window,
201 event.timestamp);
204void TouchGate::dispatchTouchEventToTarget(QTouchEvent* event)
206 removeTouchInfoForEndedTouches(event->touchPoints());
207 m_dispatcher.dispatch(event->type(),
208 event->device(),
209 event->modifiers(),
210 event->touchPoints(),
211 event->window(),
212 event->timestamp());
215void TouchGate::removeTouchInfoForEndedTouches(const QList<QTouchEvent::TouchPoint> &touchPoints)
217 for (int i = 0; i < touchPoints.size(); ++i) {\
218 const QTouchEvent::TouchPoint &touchPoint = touchPoints.at(i);
220 if (touchPoint.state() == Qt::TouchPointReleased) {
221 Q_ASSERT(m_touchInfoMap.contains(touchPoint.id()));
222 Q_ASSERT(m_touchInfoMap[touchPoint.id()].ended);
223 Q_ASSERT(m_touchInfoMap[touchPoint.id()].ownership == OwnershipGranted);
224 m_touchInfoMap.remove(touchPoint.id());
225 }
226 }
229TouchGate::TouchEvent::TouchEvent(const QTouchEvent *event)
230 : eventType(event->type())
231 , device(event->device())
232 , modifiers(event->modifiers())
233 , touchPoints(event->touchPoints())
234 , target(qobject_cast<QQuickItem*>(event->target()))
235 , window(event->window())
236 , timestamp(event->timestamp())
240bool TouchGate::TouchEvent::removeTouch(int touchId)
242 bool removed = false;
243 for (int i = 0; i < touchPoints.count() && !removed; ++i) {
244 if (touchPoints[i].id() == touchId) {
245 touchPoints.removeAt(i);
246 removed = true;
247 }
248 }
250 return removed;