1 // Copyright 2014 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 "content/browser/renderer_host/input/touch_emulator.h"
7 #include "content/browser/renderer_host/input/motion_event_web.h"
8 #include "content/browser/renderer_host/input/web_input_event_util.h"
9 #include "content/common/input/web_touch_event_traits.h"
10 #include "content/grit/content_resources.h"
11 #include "content/public/common/content_client.h"
12 #include "content/public/common/content_switches.h"
13 #include "third_party/WebKit/public/platform/WebCursorInfo.h"
14 #include "ui/events/base_event_utils.h"
15 #include "ui/events/blink/blink_event_util.h"
16 #include "ui/events/gesture_detection/gesture_provider_config_helper.h"
17 #include "ui/gfx/image/image.h"
19 using blink::WebGestureEvent
;
20 using blink::WebInputEvent
;
21 using blink::WebKeyboardEvent
;
22 using blink::WebMouseEvent
;
23 using blink::WebMouseWheelEvent
;
24 using blink::WebTouchEvent
;
25 using blink::WebTouchPoint
;
31 ui::GestureProvider::Config
GetEmulatorGestureProviderConfig(
32 ui::GestureProviderConfigType config_type
) {
33 ui::GestureProvider::Config config
=
34 ui::GetGestureProviderConfig(config_type
);
35 config
.gesture_begin_end_types_enabled
= false;
36 config
.gesture_detector_config
.swipe_enabled
= false;
37 config
.gesture_detector_config
.two_finger_tap_enabled
= false;
41 // Time between two consecutive mouse moves, during which second mouse move
42 // is not converted to touch.
43 const double kMouseMoveDropIntervalSeconds
= 5.f
/ 1000;
47 TouchEmulator::TouchEmulator(TouchEmulatorClient
* client
,
48 float device_scale_factor
)
50 gesture_provider_config_type_(
51 ui::GestureProviderConfigType::CURRENT_PLATFORM
),
52 double_tap_enabled_(true),
53 emulated_stream_active_sequence_count_(0),
54 native_stream_active_sequence_count_(0) {
58 bool use_2x
= device_scale_factor
> 1.5f
;
59 float cursor_scale_factor
= use_2x
? 2.f
: 1.f
;
60 cursor_size_
= InitCursorFromResource(&touch_cursor_
,
62 use_2x
? IDR_DEVTOOLS_TOUCH_CURSOR_ICON_2X
:
63 IDR_DEVTOOLS_TOUCH_CURSOR_ICON
);
64 InitCursorFromResource(&pinch_cursor_
,
66 use_2x
? IDR_DEVTOOLS_PINCH_CURSOR_ICON_2X
:
67 IDR_DEVTOOLS_PINCH_CURSOR_ICON
);
69 WebCursor::CursorInfo cursor_info
;
70 cursor_info
.type
= blink::WebCursorInfo::TypePointer
;
71 pointer_cursor_
.InitFromCursorInfo(cursor_info
);
74 TouchEmulator::~TouchEmulator() {
75 // We cannot cleanup properly in destructor, as we need roundtrip to the
76 // renderer for ack. Instead, the owner should call Disable, and only
77 // destroy this object when renderer is dead.
80 void TouchEmulator::ResetState() {
81 last_mouse_event_was_move_
= false;
82 last_mouse_move_timestamp_
= 0;
83 mouse_pressed_
= false;
84 shift_pressed_
= false;
85 suppress_next_fling_cancel_
= false;
87 pinch_gesture_active_
= false;
90 void TouchEmulator::Enable(ui::GestureProviderConfigType config_type
) {
91 if (!gesture_provider_
|| gesture_provider_config_type_
!= config_type
) {
92 gesture_provider_config_type_
= config_type
;
93 gesture_provider_
.reset(new ui::FilteredGestureProvider(
94 GetEmulatorGestureProviderConfig(config_type
), this));
95 // TODO(dgozman): Use synthetic secondary touch to support multi-touch.
96 gesture_provider_
->SetMultiTouchZoomSupportEnabled(false);
97 gesture_provider_
->SetDoubleTapSupportForPageEnabled(double_tap_enabled_
);
102 void TouchEmulator::Disable() {
107 gesture_provider_
.reset();
112 void TouchEmulator::SetDoubleTapSupportForPageEnabled(bool enabled
) {
113 double_tap_enabled_
= enabled
;
114 if (gesture_provider_
)
115 gesture_provider_
->SetDoubleTapSupportForPageEnabled(enabled
);
118 gfx::SizeF
TouchEmulator::InitCursorFromResource(
119 WebCursor
* cursor
, float scale
, int resource_id
) {
120 gfx::Image
& cursor_image
=
121 content::GetContentClient()->GetNativeImageNamed(resource_id
);
122 WebCursor::CursorInfo cursor_info
;
123 cursor_info
.type
= blink::WebCursorInfo::TypeCustom
;
124 cursor_info
.image_scale_factor
= scale
;
125 cursor_info
.custom_image
= cursor_image
.AsBitmap();
126 cursor_info
.hotspot
=
127 gfx::Point(cursor_image
.Width() / 2, cursor_image
.Height() / 2);
129 cursor_info
.external_handle
= 0;
132 cursor
->InitFromCursorInfo(cursor_info
);
133 return gfx::ScaleSize(cursor_image
.Size(), 1.f
/ scale
);
136 bool TouchEmulator::HandleMouseEvent(const WebMouseEvent
& mouse_event
) {
140 if (mouse_event
.button
== WebMouseEvent::ButtonRight
&&
141 mouse_event
.type
== WebInputEvent::MouseDown
) {
142 client_
->ShowContextMenuAtPoint(gfx::Point(mouse_event
.x
, mouse_event
.y
));
145 if (mouse_event
.button
!= WebMouseEvent::ButtonLeft
)
148 if (mouse_event
.type
== WebInputEvent::MouseMove
) {
149 if (last_mouse_event_was_move_
&&
150 mouse_event
.timeStampSeconds
< last_mouse_move_timestamp_
+
151 kMouseMoveDropIntervalSeconds
)
154 last_mouse_event_was_move_
= true;
155 last_mouse_move_timestamp_
= mouse_event
.timeStampSeconds
;
157 last_mouse_event_was_move_
= false;
160 if (mouse_event
.type
== WebInputEvent::MouseDown
)
161 mouse_pressed_
= true;
162 else if (mouse_event
.type
== WebInputEvent::MouseUp
)
163 mouse_pressed_
= false;
165 UpdateShiftPressed((mouse_event
.modifiers
& WebInputEvent::ShiftKey
) != 0);
167 if (mouse_event
.type
!= WebInputEvent::MouseDown
&&
168 mouse_event
.type
!= WebInputEvent::MouseMove
&&
169 mouse_event
.type
!= WebInputEvent::MouseUp
) {
173 FillTouchEventAndPoint(mouse_event
);
174 HandleEmulatedTouchEvent(touch_event_
);
176 // Do not pass mouse events to the renderer.
180 bool TouchEmulator::HandleMouseWheelEvent(const WebMouseWheelEvent
& event
) {
184 // Send mouse wheel for easy scrolling when there is no active touch.
185 return emulated_stream_active_sequence_count_
> 0;
188 bool TouchEmulator::HandleKeyboardEvent(const WebKeyboardEvent
& event
) {
192 if (!UpdateShiftPressed((event
.modifiers
& WebInputEvent::ShiftKey
) != 0))
198 // Note: The necessary pinch events will be lazily inserted by
199 // |OnGestureEvent| depending on the state of |shift_pressed_|, using the
200 // scroll stream as the event driver.
201 if (shift_pressed_
) {
202 // TODO(dgozman): Add secondary touch point and set anchor.
204 // TODO(dgozman): Remove secondary touch point and anchor.
207 // Never block keyboard events.
211 bool TouchEmulator::HandleTouchEvent(const blink::WebTouchEvent
& event
) {
212 // Block native event when emulated touch stream is active.
213 if (emulated_stream_active_sequence_count_
)
216 bool is_sequence_start
= WebTouchEventTraits::IsTouchSequenceStart(event
);
217 // Do not allow middle-sequence event to pass through, if start was blocked.
218 if (!native_stream_active_sequence_count_
&& !is_sequence_start
)
221 if (is_sequence_start
)
222 native_stream_active_sequence_count_
++;
226 void TouchEmulator::HandleEmulatedTouchEvent(blink::WebTouchEvent event
) {
227 DCHECK(gesture_provider_
);
228 event
.uniqueTouchEventId
= ui::GetNextTouchEventId();
229 auto result
= gesture_provider_
->OnTouchEvent(MotionEventWeb(event
));
230 if (!result
.succeeded
)
233 const bool event_consumed
= true;
234 // Block emulated event when emulated native stream is active.
235 if (native_stream_active_sequence_count_
) {
236 gesture_provider_
->OnTouchEventAck(event
.uniqueTouchEventId
,
241 bool is_sequence_start
= WebTouchEventTraits::IsTouchSequenceStart(event
);
242 // Do not allow middle-sequence event to pass through, if start was blocked.
243 if (!emulated_stream_active_sequence_count_
&& !is_sequence_start
) {
244 gesture_provider_
->OnTouchEventAck(event
.uniqueTouchEventId
,
249 if (is_sequence_start
)
250 emulated_stream_active_sequence_count_
++;
252 event
.causesScrollingIfUncanceled
= result
.did_generate_scroll
;
253 client_
->ForwardEmulatedTouchEvent(event
);
256 bool TouchEmulator::HandleTouchEventAck(
257 const blink::WebTouchEvent
& event
, InputEventAckState ack_result
) {
258 bool is_sequence_end
= WebTouchEventTraits::IsTouchSequenceEnd(event
);
259 if (emulated_stream_active_sequence_count_
) {
261 emulated_stream_active_sequence_count_
--;
263 const bool event_consumed
= ack_result
== INPUT_EVENT_ACK_STATE_CONSUMED
;
264 if (gesture_provider_
)
265 gesture_provider_
->OnTouchEventAck(event
.uniqueTouchEventId
,
270 // We may have not seen native touch sequence start (when created in the
271 // middle of a sequence), so don't decrement sequence count below zero.
272 if (is_sequence_end
&& native_stream_active_sequence_count_
)
273 native_stream_active_sequence_count_
--;
277 void TouchEmulator::OnGestureEvent(const ui::GestureEventData
& gesture
) {
278 WebGestureEvent gesture_event
=
279 ui::CreateWebGestureEventFromGestureEventData(gesture
);
281 switch (gesture_event
.type
) {
282 case WebInputEvent::Undefined
:
283 NOTREACHED() << "Undefined WebInputEvent type";
284 // Bail without sending the junk event to the client.
287 case WebInputEvent::GestureScrollBegin
:
288 client_
->ForwardGestureEvent(gesture_event
);
289 // PinchBegin must always follow ScrollBegin.
290 if (InPinchGestureMode())
291 PinchBegin(gesture_event
);
294 case WebInputEvent::GestureScrollUpdate
:
295 if (InPinchGestureMode()) {
296 // Convert scrolls to pinches while shift is pressed.
297 if (!pinch_gesture_active_
)
298 PinchBegin(gesture_event
);
300 PinchUpdate(gesture_event
);
302 // Pass scroll update further. If shift was released, end the pinch.
303 if (pinch_gesture_active_
)
304 PinchEnd(gesture_event
);
305 client_
->ForwardGestureEvent(gesture_event
);
309 case WebInputEvent::GestureScrollEnd
:
310 // PinchEnd must precede ScrollEnd.
311 if (pinch_gesture_active_
)
312 PinchEnd(gesture_event
);
313 client_
->ForwardGestureEvent(gesture_event
);
316 case WebInputEvent::GestureFlingStart
:
317 // PinchEnd must precede FlingStart.
318 if (pinch_gesture_active_
)
319 PinchEnd(gesture_event
);
320 if (InPinchGestureMode()) {
321 // No fling in pinch mode. Forward scroll end instead of fling start.
322 suppress_next_fling_cancel_
= true;
323 ScrollEnd(gesture_event
);
325 suppress_next_fling_cancel_
= false;
326 client_
->ForwardGestureEvent(gesture_event
);
330 case WebInputEvent::GestureFlingCancel
:
331 // If fling start was suppressed, we should not send fling cancel either.
332 if (!suppress_next_fling_cancel_
)
333 client_
->ForwardGestureEvent(gesture_event
);
334 suppress_next_fling_cancel_
= false;
338 // Everything else goes through.
339 client_
->ForwardGestureEvent(gesture_event
);
343 void TouchEmulator::CancelTouch() {
344 if (!emulated_stream_active_sequence_count_
|| !enabled())
347 WebTouchEventTraits::ResetTypeAndTouchStates(
348 WebInputEvent::TouchCancel
,
349 (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(),
351 DCHECK(gesture_provider_
);
352 if (gesture_provider_
->GetCurrentDownEvent())
353 HandleEmulatedTouchEvent(touch_event_
);
356 void TouchEmulator::UpdateCursor() {
358 client_
->SetCursor(pointer_cursor_
);
360 client_
->SetCursor(InPinchGestureMode() ? pinch_cursor_
: touch_cursor_
);
363 bool TouchEmulator::UpdateShiftPressed(bool shift_pressed
) {
364 if (shift_pressed_
== shift_pressed
)
366 shift_pressed_
= shift_pressed
;
371 void TouchEmulator::PinchBegin(const WebGestureEvent
& event
) {
372 DCHECK(InPinchGestureMode());
373 DCHECK(!pinch_gesture_active_
);
374 pinch_gesture_active_
= true;
375 pinch_anchor_
= gfx::Point(event
.x
, event
.y
);
377 FillPinchEvent(event
);
378 pinch_event_
.type
= WebInputEvent::GesturePinchBegin
;
379 client_
->ForwardGestureEvent(pinch_event_
);
382 void TouchEmulator::PinchUpdate(const WebGestureEvent
& event
) {
383 DCHECK(pinch_gesture_active_
);
384 int dy
= pinch_anchor_
.y() - event
.y
;
385 float scale
= exp(dy
* 0.002f
);
386 FillPinchEvent(event
);
387 pinch_event_
.type
= WebInputEvent::GesturePinchUpdate
;
388 pinch_event_
.data
.pinchUpdate
.scale
= scale
/ pinch_scale_
;
389 client_
->ForwardGestureEvent(pinch_event_
);
390 pinch_scale_
= scale
;
393 void TouchEmulator::PinchEnd(const WebGestureEvent
& event
) {
394 DCHECK(pinch_gesture_active_
);
395 pinch_gesture_active_
= false;
396 FillPinchEvent(event
);
397 pinch_event_
.type
= WebInputEvent::GesturePinchEnd
;
398 client_
->ForwardGestureEvent(pinch_event_
);
401 void TouchEmulator::FillPinchEvent(const WebInputEvent
& event
) {
402 pinch_event_
.timeStampSeconds
= event
.timeStampSeconds
;
403 pinch_event_
.modifiers
= event
.modifiers
;
404 pinch_event_
.sourceDevice
= blink::WebGestureDeviceTouchscreen
;
405 pinch_event_
.x
= pinch_anchor_
.x();
406 pinch_event_
.y
= pinch_anchor_
.y();
409 void TouchEmulator::ScrollEnd(const WebGestureEvent
& event
) {
410 WebGestureEvent scroll_event
;
411 scroll_event
.timeStampSeconds
= event
.timeStampSeconds
;
412 scroll_event
.modifiers
= event
.modifiers
;
413 scroll_event
.sourceDevice
= blink::WebGestureDeviceTouchscreen
;
414 scroll_event
.type
= WebInputEvent::GestureScrollEnd
;
415 client_
->ForwardGestureEvent(scroll_event
);
418 void TouchEmulator::FillTouchEventAndPoint(const WebMouseEvent
& mouse_event
) {
419 WebInputEvent::Type eventType
;
420 switch (mouse_event
.type
) {
421 case WebInputEvent::MouseDown
:
422 eventType
= WebInputEvent::TouchStart
;
424 case WebInputEvent::MouseMove
:
425 eventType
= WebInputEvent::TouchMove
;
427 case WebInputEvent::MouseUp
:
428 eventType
= WebInputEvent::TouchEnd
;
431 eventType
= WebInputEvent::Undefined
;
432 NOTREACHED() << "Invalid event for touch emulation: " << mouse_event
.type
;
434 touch_event_
.touchesLength
= 1;
435 touch_event_
.modifiers
= mouse_event
.modifiers
;
436 WebTouchEventTraits::ResetTypeAndTouchStates(
437 eventType
, mouse_event
.timeStampSeconds
, &touch_event_
);
439 WebTouchPoint
& point
= touch_event_
.touches
[0];
441 point
.radiusX
= 0.5f
* cursor_size_
.width();
442 point
.radiusY
= 0.5f
* cursor_size_
.height();
444 point
.rotationAngle
= 0.f
;
445 point
.position
.x
= mouse_event
.x
;
446 point
.screenPosition
.x
= mouse_event
.globalX
;
447 point
.position
.y
= mouse_event
.y
;
448 point
.screenPosition
.y
= mouse_event
.globalY
;
451 bool TouchEmulator::InPinchGestureMode() const {
452 return shift_pressed_
;
455 } // namespace content