1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/events/gestures/gesture_recognizer_impl.h"
9 #include "base/command_line.h"
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/time/time.h"
14 #include "ui/events/event.h"
15 #include "ui/events/event_constants.h"
16 #include "ui/events/event_switches.h"
17 #include "ui/events/event_utils.h"
18 #include "ui/events/gesture_detection/gesture_configuration.h"
19 #include "ui/events/gestures/gesture_types.h"
26 void TransferConsumer(GestureConsumer
* current_consumer
,
27 GestureConsumer
* new_consumer
,
28 std::map
<GestureConsumer
*, T
>* map
) {
29 if (map
->count(current_consumer
)) {
30 (*map
)[new_consumer
] = (*map
)[current_consumer
];
31 map
->erase(current_consumer
);
35 bool RemoveConsumerFromMap(GestureConsumer
* consumer
,
36 GestureRecognizerImpl::TouchIdToConsumerMap
* map
) {
37 bool consumer_removed
= false;
38 for (GestureRecognizerImpl::TouchIdToConsumerMap::iterator i
= map
->begin();
40 if (i
->second
== consumer
) {
42 consumer_removed
= true;
47 return consumer_removed
;
50 void TransferTouchIdToConsumerMap(
51 GestureConsumer
* old_consumer
,
52 GestureConsumer
* new_consumer
,
53 GestureRecognizerImpl::TouchIdToConsumerMap
* map
) {
54 for (GestureRecognizerImpl::TouchIdToConsumerMap::iterator i
= map
->begin();
55 i
!= map
->end(); ++i
) {
56 if (i
->second
== old_consumer
)
57 i
->second
= new_consumer
;
61 GestureProviderAura
* CreateGestureProvider(GestureProviderAuraClient
* client
) {
62 return new GestureProviderAura(client
);
67 ////////////////////////////////////////////////////////////////////////////////
68 // GestureRecognizerImpl, public:
70 GestureRecognizerImpl::GestureRecognizerImpl() {
73 GestureRecognizerImpl::~GestureRecognizerImpl() {
74 STLDeleteValues(&consumer_gesture_provider_
);
77 // Checks if this finger is already down, if so, returns the current target.
78 // Otherwise, returns NULL.
79 GestureConsumer
* GestureRecognizerImpl::GetTouchLockedTarget(
80 const TouchEvent
& event
) {
81 return touch_id_target_
[event
.touch_id()];
84 GestureConsumer
* GestureRecognizerImpl::GetTargetForGestureEvent(
85 const GestureEvent
& event
) {
86 int touch_id
= event
.details().oldest_touch_id();
87 if (!touch_id_target_for_gestures_
.count(touch_id
)) {
88 NOTREACHED() << "Touch ID does not map to a valid GestureConsumer.";
92 return touch_id_target_for_gestures_
.at(touch_id
);
95 GestureConsumer
* GestureRecognizerImpl::GetTargetForLocation(
96 const gfx::PointF
& location
, int source_device_id
) {
97 const float max_distance
=
98 GestureConfiguration::GetInstance()
99 ->max_separation_for_gesture_touches_in_pixels();
101 gfx::PointF closest_point
;
102 int closest_touch_id
= 0;
103 double closest_distance_squared
= std::numeric_limits
<double>::infinity();
105 std::map
<GestureConsumer
*, GestureProviderAura
*>::iterator i
;
106 for (i
= consumer_gesture_provider_
.begin();
107 i
!= consumer_gesture_provider_
.end();
109 const MotionEventAura
& pointer_state
= i
->second
->pointer_state();
110 for (size_t j
= 0; j
< pointer_state
.GetPointerCount(); ++j
) {
111 if (source_device_id
!= pointer_state
.GetSourceDeviceId(j
))
113 gfx::PointF
point(pointer_state
.GetX(j
), pointer_state
.GetY(j
));
114 // Relative distance is all we need here, so LengthSquared() is
115 // appropriate, and cheaper than Length().
116 double distance_squared
= (point
- location
).LengthSquared();
117 if (distance_squared
< closest_distance_squared
) {
118 closest_point
= point
;
119 closest_touch_id
= pointer_state
.GetPointerId(j
);
120 closest_distance_squared
= distance_squared
;
125 if (closest_distance_squared
< max_distance
* max_distance
)
126 return touch_id_target_
[closest_touch_id
];
130 void GestureRecognizerImpl::CancelActiveTouchesExcept(
131 GestureConsumer
* not_cancelled
) {
132 for (const auto& consumer_provider
: consumer_gesture_provider_
) {
133 if (consumer_provider
.first
== not_cancelled
)
135 CancelActiveTouches(consumer_provider
.first
);
139 void GestureRecognizerImpl::TransferEventsTo(GestureConsumer
* current_consumer
,
140 GestureConsumer
* new_consumer
) {
141 DCHECK(current_consumer
);
142 DCHECK(new_consumer
);
144 CancelActiveTouchesExcept(current_consumer
);
146 TransferTouchIdToConsumerMap(current_consumer
, new_consumer
,
148 TransferTouchIdToConsumerMap(current_consumer
, new_consumer
,
149 &touch_id_target_for_gestures_
);
150 TransferConsumer(current_consumer
, new_consumer
, &consumer_gesture_provider_
);
153 bool GestureRecognizerImpl::GetLastTouchPointForTarget(
154 GestureConsumer
* consumer
,
155 gfx::PointF
* point
) {
156 if (consumer_gesture_provider_
.count(consumer
) == 0)
158 const MotionEvent
& pointer_state
=
159 consumer_gesture_provider_
[consumer
]->pointer_state();
160 *point
= gfx::PointF(pointer_state
.GetX(), pointer_state
.GetY());
164 bool GestureRecognizerImpl::CancelActiveTouches(GestureConsumer
* consumer
) {
165 bool cancelled_touch
= false;
166 if (consumer_gesture_provider_
.count(consumer
) == 0)
168 const MotionEventAura
& pointer_state
=
169 consumer_gesture_provider_
[consumer
]->pointer_state();
170 if (pointer_state
.GetPointerCount() == 0)
172 // pointer_state is modified every time after DispatchCancelTouchEvent.
173 scoped_ptr
<MotionEvent
> pointer_state_clone
= pointer_state
.Clone();
174 for (size_t i
= 0; i
< pointer_state_clone
->GetPointerCount(); ++i
) {
175 gfx::PointF
point(pointer_state_clone
->GetX(i
),
176 pointer_state_clone
->GetY(i
));
177 TouchEvent
touch_event(ui::ET_TOUCH_CANCELLED
,
179 ui::EF_IS_SYNTHESIZED
,
180 pointer_state_clone
->GetPointerId(i
),
181 ui::EventTimeForNow(),
186 GestureEventHelper
* helper
= FindDispatchHelperForConsumer(consumer
);
188 helper
->DispatchCancelTouchEvent(&touch_event
);
189 cancelled_touch
= true;
191 return cancelled_touch
;
194 ////////////////////////////////////////////////////////////////////////////////
195 // GestureRecognizerImpl, private:
197 GestureProviderAura
* GestureRecognizerImpl::GetGestureProviderForConsumer(
198 GestureConsumer
* consumer
) {
199 GestureProviderAura
* gesture_provider
= consumer_gesture_provider_
[consumer
];
200 if (!gesture_provider
) {
201 gesture_provider
= CreateGestureProvider(this);
202 consumer_gesture_provider_
[consumer
] = gesture_provider
;
204 return gesture_provider
;
207 void GestureRecognizerImpl::SetupTargets(const TouchEvent
& event
,
208 GestureConsumer
* target
) {
209 if (event
.type() == ui::ET_TOUCH_RELEASED
||
210 event
.type() == ui::ET_TOUCH_CANCELLED
) {
211 touch_id_target_
.erase(event
.touch_id());
212 } else if (event
.type() == ui::ET_TOUCH_PRESSED
) {
213 touch_id_target_
[event
.touch_id()] = target
;
215 touch_id_target_for_gestures_
[event
.touch_id()] = target
;
219 void GestureRecognizerImpl::DispatchGestureEvent(GestureEvent
* event
) {
220 GestureConsumer
* consumer
= GetTargetForGestureEvent(*event
);
222 GestureEventHelper
* helper
= FindDispatchHelperForConsumer(consumer
);
224 helper
->DispatchGestureEvent(event
);
228 bool GestureRecognizerImpl::ProcessTouchEventPreDispatch(
230 GestureConsumer
* consumer
) {
231 SetupTargets(*event
, consumer
);
233 if (event
->result() & ER_CONSUMED
)
236 GestureProviderAura
* gesture_provider
=
237 GetGestureProviderForConsumer(consumer
);
238 return gesture_provider
->OnTouchEvent(event
);
241 // TODO(tdresser): we should take a unique_event_id here, and validate
242 // that the correct event is being acked. See crbug.com/457669 for
244 GestureRecognizer::Gestures
*
245 GestureRecognizerImpl::AckAsyncTouchEvent(
246 ui::EventResult result
,
247 GestureConsumer
* consumer
) {
248 GestureProviderAura
* gesture_provider
=
249 GetGestureProviderForConsumer(consumer
);
250 gesture_provider
->OnAsyncTouchEventAck(result
!= ER_UNHANDLED
);
251 return gesture_provider
->GetAndResetPendingGestures();
254 GestureRecognizer::Gestures
* GestureRecognizerImpl::AckSyncTouchEvent(
255 const uint64 unique_event_id
,
256 ui::EventResult result
,
257 GestureConsumer
* consumer
) {
258 GestureProviderAura
* gesture_provider
=
259 GetGestureProviderForConsumer(consumer
);
260 gesture_provider
->OnSyncTouchEventAck(unique_event_id
,
261 result
!= ER_UNHANDLED
);
262 return gesture_provider
->GetAndResetPendingGestures();
265 bool GestureRecognizerImpl::CleanupStateForConsumer(
266 GestureConsumer
* consumer
) {
267 bool state_cleaned_up
= false;
269 if (consumer_gesture_provider_
.count(consumer
)) {
270 state_cleaned_up
= true;
271 delete consumer_gesture_provider_
[consumer
];
272 consumer_gesture_provider_
.erase(consumer
);
275 state_cleaned_up
|= RemoveConsumerFromMap(consumer
, &touch_id_target_
);
277 RemoveConsumerFromMap(consumer
, &touch_id_target_for_gestures_
);
278 return state_cleaned_up
;
281 void GestureRecognizerImpl::AddGestureEventHelper(GestureEventHelper
* helper
) {
282 helpers_
.push_back(helper
);
285 void GestureRecognizerImpl::RemoveGestureEventHelper(
286 GestureEventHelper
* helper
) {
287 std::vector
<GestureEventHelper
*>::iterator it
= std::find(helpers_
.begin(),
288 helpers_
.end(), helper
);
289 if (it
!= helpers_
.end())
293 void GestureRecognizerImpl::OnGestureEvent(GestureEvent
* event
) {
294 DispatchGestureEvent(event
);
297 GestureEventHelper
* GestureRecognizerImpl::FindDispatchHelperForConsumer(
298 GestureConsumer
* consumer
) {
299 std::vector
<GestureEventHelper
*>::iterator it
;
300 for (it
= helpers_
.begin(); it
!= helpers_
.end(); ++it
) {
301 if ((*it
)->CanDispatchToConsumer(consumer
))
307 // GestureRecognizer, static
308 GestureRecognizer
* GestureRecognizer::Create() {
309 return new GestureRecognizerImpl();
312 static GestureRecognizerImpl
* g_gesture_recognizer_instance
= NULL
;
314 // GestureRecognizer, static
315 GestureRecognizer
* GestureRecognizer::Get() {
316 if (!g_gesture_recognizer_instance
)
317 g_gesture_recognizer_instance
= new GestureRecognizerImpl();
318 return g_gesture_recognizer_instance
;
321 // GestureRecognizer, static
322 void GestureRecognizer::Reset() {
323 delete g_gesture_recognizer_instance
;
324 g_gesture_recognizer_instance
= NULL
;
327 void SetGestureRecognizerForTesting(GestureRecognizer
* gesture_recognizer
) {
328 // Transfer helpers to the new GR.
329 std::vector
<GestureEventHelper
*>& helpers
=
330 g_gesture_recognizer_instance
->helpers();
331 std::vector
<GestureEventHelper
*>::iterator it
;
332 for (it
= helpers
.begin(); it
!= helpers
.end(); ++it
)
333 gesture_recognizer
->AddGestureEventHelper(*it
);
336 g_gesture_recognizer_instance
=
337 static_cast<GestureRecognizerImpl
*>(gesture_recognizer
);