Debian Codegraph @

src/LomiriGestures/touchregistry_p.h

raw symbols SCIP
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 #ifndef TOUCHREGISTRY_P_H
18 #define TOUCHREGISTRY_P_H
19
20 #include <utility>
21
22 #include <QtCore/QLoggingCategory>
23 #include <QtCore/QObject>
24 #include <QtCore/QPointer>
25 #include <QtCore/QVector>
26 #include <QtGui/QTouchEvent>
27 #include <QtQuick/QQuickItem>
28
29 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
30 #include <LomiriGestures-Qt6/lomirigesturesglobal.h>
31 #else
32 #include <LomiriGestures/lomirigesturesglobal.h>
33 #endif
34 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
35 #include <LomiriGestures-Qt6/private/candidateinactivitytimer_p.h>
36 #else
37 #include <LomiriGestures/private/candidateinactivitytimer_p.h>
38 #endif
39 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
40 #include <LomiriGestures-Qt6/private/timer_p.h>
41 #else
42 #include <LomiriGestures/private/timer_p.h>
43 #endif
44 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
45 #include <LomiriGestures-Qt6/private/pool_p.h>
46 #else
47 #include <LomiriGestures/private/pool_p.h>
48 #endif
49
50 UG_FORWARD_DECLARE_CLASS(AbstractTimerFactory)
51
52 /*
53 Where the ownership of touches is registered.
54
55 Singleton used for adding a touch point ownership model analogous to the one
56 described in the XInput 2.2 protocol[1] on top of the existing input dispatch logic in QQuickWindow.
57
58 It provides a much more flexible and powerful way of dealing with pointer ownership than the existing
59 mechanisms in Qt. Namely QQuickItem::grabTouchPoints, QuickItem::keepTouchGrab,
60 QQuickItem::setFiltersChildMouseEvents, QQuickItem::ungrabTouchPoints and QQuickItem::touchUngrabEvent.
61
62 Usage:
63
64 1- An item receives a a new touch point. If he's not sure whether he wants it yet, he calls:
65 TouchRegistry::instance()->addCandidateOwnerForTouch(touchId, this);
66 touchEvent->ignore();
67 Ignoring the event is crucial so that it can be seen by other interested parties, which will
68 behave similarly.
69
70 2- That item will then start receiving UnownedTouchEvents for that touch from step 1. Once he's
71 made a decision he calls either:
72 TouchRegistry::instance()->requestTouchOwnership(touchId, this);
73 If he wants the touch point or:
74 TouchRegistry::instance()->removeCandidateOwnerForTouch(touchId, this);
75 if he does not want it.
76
77 Candidates are put in a priority queue. The first one to call addCandidateOwnerForTouch() will
78 take precedence over the others for receiving ownership over the touch point (from now on called
79 simply top-candidate).
80
81 If the top-candidate calls requestTouchOwnership() he will immediately receive a
82 TouchOwnershipEvent(gained=true) for that touch point. He can then safely call
83 QQuickItem::grabTouchPoints to actually get the owned touch points. The other candidates
84 will receive TouchOwnershipEvent(gained=false) and will no longer receive UnownedTouchEvents
85 for that touch point. They will have to undo whatever action they were performing with that
86 touch point.
87
88 But if the top-candidate calls removeCandidateOwnerForTouch() instead, he's popped from the
89 candidacy queue and ownership is given to the new top-most candidate if he has already
90 made his decision, that is.
91
92 The TouchRegistry cannot enforce the results of this pointer ownership negotiation (i.e.,
93 who gets to grab the touch points) as that would clash with QQuickWindow's input event
94 dispatching logic. The candidates have to respect the decision and grab the touch points
95 themselves.
96
97 If an item wants ownership over touches as soon as he receives the TouchBegin for them, his step 1
98 would be instead:
99 TouchRegistry::instance()->requestTouchOwnership(touchId, this);
100 touchEvent->accept();
101 He won't get any UnownedTouchEvent for that touch as he is already the interim owner (ie, QQuickWindow
102 will keep sending touch updates to him already). Eventually he will be notified once ownership has
103 been granted to him (from TouchRegistry perspective), from which point onwards he could safely assume
104 other TouchRegistry users wouldn't snatch this touch away from him.
105
106 Items oblivious to TouchRegistry will lose their touch points without warning, just like in plain Qt.
107
108 [1] - http://www.x.org/releases/X11R7.7/doc/inputproto/XI2proto.txt (see multitouch-ownership)
109 */
110
111 UG_NAMESPACE_BEGIN
112
113 class LOMIRIGESTURES_EXPORT TouchRegistry : public QObject
114 {
115 Q_OBJECT
116 public:
117 virtual ~TouchRegistry();
118
119 // Returns a pointer to the application's TouchRegistry instance.
120 static TouchRegistry *instance();
121
122 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
123 void update(QTouchEvent *event);
124 #else
125 void update(const QTouchEvent *event);
126 #endif
127
128 // Calls update() if the given event is a QTouchEvent
129 bool eventFilter(QObject *watched, QEvent *event) override;
130
131 // An item that might later request ownership over the given touch point.
132 // He will be kept informed about that touch point through UnownedTouchEvents
133 // All candidates must eventually decide whether they want to own the touch point
134 // or not. That decision is informed through requestTouchOwnership() or
135 // removeCandidateOwnerForTouch()
136 void addCandidateOwnerForTouch(int id, QQuickItem *candidate);
137
138 // The same as rejecting ownership of a touch
139 void removeCandidateOwnerForTouch(int id, QQuickItem *candidate);
140
141 // The candidate object wants to be the owner of the touch with the given id.
142 // If he's currently the oldest/top-most candidate, he will get an ownership
143 // event immediately. If not, he will get ownership if (or once) he becomes the
144 // top-most candidate.
145 void requestTouchOwnership(int id, QQuickItem *candidate);
146
147 // An item that has no interest (effective or potential) in owning a touch point
148 // but would nonetheless like to be kept up-to-date on its state.
149 void addTouchWatcher(int touchId, QQuickItem *watcherItem);
150
151 // Useful for tests, where you should use fake timers
152 void setTimerFactory(AbstractTimerFactory *timerFactory);
153
154 private Q_SLOTS:
155 void rejectCandidateOwnerForTouch(int id, QQuickItem *candidate);
156
157 private:
158 // Only instance() can cronstruct one
159 TouchRegistry(QObject *parent = nullptr);
160
161 class CandidateInfo {
162 public:
163 enum {
164 // A candidate owner that doesn't yet know for sure whether he wants the touch point
165 // (gesture recognition is stilll going on)
166 Undecided = 0,
167 // A candidate owner that wants the touch but hasn't been granted it yet,
168 // most likely because there's an undecided candidate with higher priority
169 Requested = 1,
170 // An item that is the interim owner of the touch, receiving QTouchEvents of it
171 // from QQuickWindow. Ie, it's the actual touch owner from Qt's point of view.
172 // It wants to keep its touch ownership but hasn't been granted it by TouchRegistry
173 // yet because of undecided candidates higher up.
174 InterimOwner = 2
175 } state;
176 QPointer<QQuickItem> item;
177 QPointer<UG_PREPEND_NAMESPACE(CandidateInactivityTimer)> inactivityTimer;
178 };
179
180 class TouchInfo {
181 public:
182 TouchInfo() : id(-1), physicallyEnded(false) {}
183 TouchInfo(int id);
184 bool isValid() const { return id >= 0; }
185 void reset();
186 void init(int id);
187 int id;
188 bool physicallyEnded;
189 bool isOwned() const;
190 bool ended() const;
191 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
192 void notifyCandidatesOfOwnershipResolution(QPointerEvent *pointerEvent,
193 const QEventPoint *point);
194 #else
195 void notifyCandidatesOfOwnershipResolution();
196 #endif
197
198 // TODO optimize storage (s/QList/Pool)
199 QList<CandidateInfo> candidates;
200 QList<QPointer<QQuickItem>> watchers;
201 };
202
203 void pruneNullCandidatesForTouch(int touchId);
204 void removeCandidateOwnerForTouchByIndex(Pool<TouchInfo>::Iterator &touchInfo, int candidateIndex);
205 void removeCandidateHelper(Pool<TouchInfo>::Iterator &touchInfo, int candidateIndex);
206
207 Pool<TouchInfo>::Iterator findTouchInfo(int id);
208
209 void deliverTouchUpdatesToUndecidedCandidatesAndWatchers(const QTouchEvent *event);
210
211 static void translateTouchPointFromScreenToWindowCoords(QTouchEvent::TouchPoint &touchPoint);
212
213 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
214 void dispatchPointsToItem(const QTouchEvent *event, const QList<int> &touchIds,
215 QQuickItem *item);
216 #else
217 static void dispatchPointsToItem(const QTouchEvent *event, const QList<int> &touchIds,
218 QQuickItem *item);
219 #endif
220 void freeEndedTouchInfos();
221
222 Pool<TouchInfo> m_touchInfoPool;
223
224 // the singleton instance
225 static TouchRegistry *m_instance;
226
227 bool m_inDispatchLoop;
228
229 AbstractTimerFactory *m_timerFactory;
230
231 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
232 QList<QPointerEvent *> m_eventsInDelivery;
233
234 std::pair<QPointerEvent *, const QEventPoint *>
235 findMostRelavantEventForId(int id, QQuickItem *item);
236 #endif
237
238 friend class tst_TouchRegistry;
239 friend class tst_DirectionalDragArea;
240 };
241
242 UG_NAMESPACE_END
243
244 Q_DECLARE_LOGGING_CATEGORY(ugTouchRegistry)
245
246 #endif // TOUCHREGISTRY_P_H