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/gesture_detection/gesture_config_helper.h"
15 #include "ui/gfx/image/image.h"
16 #include "ui/gfx/screen.h"
18 using blink::WebGestureEvent
;
19 using blink::WebInputEvent
;
20 using blink::WebKeyboardEvent
;
21 using blink::WebMouseEvent
;
22 using blink::WebMouseWheelEvent
;
23 using blink::WebTouchEvent
;
24 using blink::WebTouchPoint
;
30 ui::GestureProvider::Config
GetGestureProviderConfig() {
31 // TODO(dgozman): Use different configs to emulate mobile/desktop as
32 // requested by renderer.
33 ui::GestureProvider::Config config
= ui::DefaultGestureProviderConfig();
34 config
.gesture_begin_end_types_enabled
= false;
35 config
.gesture_detector_config
.swipe_enabled
= false;
36 config
.gesture_detector_config
.two_finger_tap_enabled
= false;
40 // Time between two consecutive mouse moves, during which second mouse move
41 // is not converted to touch.
42 const double kMouseMoveDropIntervalSeconds
= 5.f
/ 1000;
46 TouchEmulator::TouchEmulator(TouchEmulatorClient
* client
)
48 gesture_provider_(GetGestureProviderConfig(), this),
50 emulated_stream_active_sequence_count_(0),
51 native_stream_active_sequence_count_(0) {
55 bool use_2x
= gfx::Screen::GetNativeScreen()->
56 GetPrimaryDisplay().device_scale_factor() > 1.5f
;
57 float cursor_scale_factor
= use_2x
? 2.f
: 1.f
;
58 cursor_size_
= InitCursorFromResource(&touch_cursor_
,
60 use_2x
? IDR_DEVTOOLS_TOUCH_CURSOR_ICON_2X
:
61 IDR_DEVTOOLS_TOUCH_CURSOR_ICON
);
62 InitCursorFromResource(&pinch_cursor_
,
64 use_2x
? IDR_DEVTOOLS_PINCH_CURSOR_ICON_2X
:
65 IDR_DEVTOOLS_PINCH_CURSOR_ICON
);
67 WebCursor::CursorInfo cursor_info
;
68 cursor_info
.type
= blink::WebCursorInfo::TypePointer
;
69 pointer_cursor_
.InitFromCursorInfo(cursor_info
);
71 // TODO(dgozman): Use synthetic secondary touch to support multi-touch.
72 gesture_provider_
.SetMultiTouchZoomSupportEnabled(false);
73 // TODO(dgozman): Enable double tap if requested by the renderer.
74 // TODO(dgozman): Don't break double-tap-based pinch with shift handling.
75 gesture_provider_
.SetDoubleTapSupportForPlatformEnabled(false);
78 TouchEmulator::~TouchEmulator() {
79 // We cannot cleanup properly in destructor, as we need roundtrip to the
80 // renderer for ack. Instead, the owner should call Disable, and only
81 // destroy this object when renderer is dead.
84 void TouchEmulator::ResetState() {
85 last_mouse_event_was_move_
= false;
86 last_mouse_move_timestamp_
= 0;
87 mouse_pressed_
= false;
88 shift_pressed_
= false;
89 suppress_next_fling_cancel_
= false;
91 pinch_gesture_active_
= false;
94 void TouchEmulator::Enable() {
102 void TouchEmulator::Disable() {
111 gfx::SizeF
TouchEmulator::InitCursorFromResource(
112 WebCursor
* cursor
, float scale
, int resource_id
) {
113 gfx::Image
& cursor_image
=
114 content::GetContentClient()->GetNativeImageNamed(resource_id
);
115 WebCursor::CursorInfo cursor_info
;
116 cursor_info
.type
= blink::WebCursorInfo::TypeCustom
;
117 cursor_info
.image_scale_factor
= scale
;
118 cursor_info
.custom_image
= cursor_image
.AsBitmap();
119 cursor_info
.hotspot
=
120 gfx::Point(cursor_image
.Width() / 2, cursor_image
.Height() / 2);
122 cursor_info
.external_handle
= 0;
125 cursor
->InitFromCursorInfo(cursor_info
);
126 return gfx::ScaleSize(cursor_image
.Size(), 1.f
/ scale
);
129 bool TouchEmulator::HandleMouseEvent(const WebMouseEvent
& mouse_event
) {
133 if (mouse_event
.button
== WebMouseEvent::ButtonRight
&&
134 mouse_event
.type
== WebInputEvent::MouseDown
) {
135 client_
->ShowContextMenuAtPoint(gfx::Point(mouse_event
.x
, mouse_event
.y
));
138 if (mouse_event
.button
!= WebMouseEvent::ButtonLeft
)
141 if (mouse_event
.type
== WebInputEvent::MouseMove
) {
142 if (last_mouse_event_was_move_
&&
143 mouse_event
.timeStampSeconds
< last_mouse_move_timestamp_
+
144 kMouseMoveDropIntervalSeconds
)
147 last_mouse_event_was_move_
= true;
148 last_mouse_move_timestamp_
= mouse_event
.timeStampSeconds
;
150 last_mouse_event_was_move_
= false;
153 if (mouse_event
.type
== WebInputEvent::MouseDown
)
154 mouse_pressed_
= true;
155 else if (mouse_event
.type
== WebInputEvent::MouseUp
)
156 mouse_pressed_
= false;
158 UpdateShiftPressed((mouse_event
.modifiers
& WebInputEvent::ShiftKey
) != 0);
160 if (FillTouchEventAndPoint(mouse_event
) &&
161 gesture_provider_
.OnTouchEvent(MotionEventWeb(touch_event_
))) {
162 ForwardTouchEventToClient();
165 // Do not pass mouse events to the renderer.
169 bool TouchEmulator::HandleMouseWheelEvent(const WebMouseWheelEvent
& event
) {
173 // Send mouse wheel for easy scrolling when there is no active touch.
174 return emulated_stream_active_sequence_count_
> 0;
177 bool TouchEmulator::HandleKeyboardEvent(const WebKeyboardEvent
& event
) {
181 if (!UpdateShiftPressed((event
.modifiers
& WebInputEvent::ShiftKey
) != 0))
187 // Note: The necessary pinch events will be lazily inserted by
188 // |OnGestureEvent| depending on the state of |shift_pressed_|, using the
189 // scroll stream as the event driver.
190 if (shift_pressed_
) {
191 // TODO(dgozman): Add secondary touch point and set anchor.
193 // TODO(dgozman): Remove secondary touch point and anchor.
196 // Never block keyboard events.
200 bool TouchEmulator::HandleTouchEvent(const blink::WebTouchEvent
& event
) {
201 // Block native event when emulated touch stream is active.
202 if (emulated_stream_active_sequence_count_
)
205 bool is_sequence_start
= WebTouchEventTraits::IsTouchSequenceStart(event
);
206 // Do not allow middle-sequence event to pass through, if start was blocked.
207 if (!native_stream_active_sequence_count_
&& !is_sequence_start
)
210 if (is_sequence_start
)
211 native_stream_active_sequence_count_
++;
215 void TouchEmulator::ForwardTouchEventToClient() {
216 const bool event_consumed
= true;
217 // Block emulated event when emulated native stream is active.
218 if (native_stream_active_sequence_count_
) {
219 gesture_provider_
.OnTouchEventAck(event_consumed
);
223 bool is_sequence_start
=
224 WebTouchEventTraits::IsTouchSequenceStart(touch_event_
);
225 // Do not allow middle-sequence event to pass through, if start was blocked.
226 if (!emulated_stream_active_sequence_count_
&& !is_sequence_start
) {
227 gesture_provider_
.OnTouchEventAck(event_consumed
);
231 if (is_sequence_start
)
232 emulated_stream_active_sequence_count_
++;
233 client_
->ForwardEmulatedTouchEvent(touch_event_
);
236 bool TouchEmulator::HandleTouchEventAck(
237 const blink::WebTouchEvent
& event
, InputEventAckState ack_result
) {
238 bool is_sequence_end
= WebTouchEventTraits::IsTouchSequenceEnd(event
);
239 if (emulated_stream_active_sequence_count_
) {
241 emulated_stream_active_sequence_count_
--;
243 const bool event_consumed
= ack_result
== INPUT_EVENT_ACK_STATE_CONSUMED
;
244 gesture_provider_
.OnTouchEventAck(event_consumed
);
248 // We may have not seen native touch sequence start (when created in the
249 // middle of a sequence), so don't decrement sequence count below zero.
250 if (is_sequence_end
&& native_stream_active_sequence_count_
)
251 native_stream_active_sequence_count_
--;
255 void TouchEmulator::OnGestureEvent(const ui::GestureEventData
& gesture
) {
256 WebGestureEvent gesture_event
=
257 CreateWebGestureEventFromGestureEventData(gesture
);
259 switch (gesture_event
.type
) {
260 case WebInputEvent::Undefined
:
261 NOTREACHED() << "Undefined WebInputEvent type";
262 // Bail without sending the junk event to the client.
265 case WebInputEvent::GestureScrollBegin
:
266 client_
->ForwardGestureEvent(gesture_event
);
267 // PinchBegin must always follow ScrollBegin.
268 if (InPinchGestureMode())
269 PinchBegin(gesture_event
);
272 case WebInputEvent::GestureScrollUpdate
:
273 if (InPinchGestureMode()) {
274 // Convert scrolls to pinches while shift is pressed.
275 if (!pinch_gesture_active_
)
276 PinchBegin(gesture_event
);
278 PinchUpdate(gesture_event
);
280 // Pass scroll update further. If shift was released, end the pinch.
281 if (pinch_gesture_active_
)
282 PinchEnd(gesture_event
);
283 client_
->ForwardGestureEvent(gesture_event
);
287 case WebInputEvent::GestureScrollEnd
:
288 // PinchEnd must precede ScrollEnd.
289 if (pinch_gesture_active_
)
290 PinchEnd(gesture_event
);
291 client_
->ForwardGestureEvent(gesture_event
);
294 case WebInputEvent::GestureFlingStart
:
295 // PinchEnd must precede FlingStart.
296 if (pinch_gesture_active_
)
297 PinchEnd(gesture_event
);
298 if (InPinchGestureMode()) {
299 // No fling in pinch mode. Forward scroll end instead of fling start.
300 suppress_next_fling_cancel_
= true;
301 ScrollEnd(gesture_event
);
303 suppress_next_fling_cancel_
= false;
304 client_
->ForwardGestureEvent(gesture_event
);
308 case WebInputEvent::GestureFlingCancel
:
309 // If fling start was suppressed, we should not send fling cancel either.
310 if (!suppress_next_fling_cancel_
)
311 client_
->ForwardGestureEvent(gesture_event
);
312 suppress_next_fling_cancel_
= false;
316 // Everything else goes through.
317 client_
->ForwardGestureEvent(gesture_event
);
321 void TouchEmulator::CancelTouch() {
322 if (!emulated_stream_active_sequence_count_
)
325 WebTouchEventTraits::ResetTypeAndTouchStates(
326 WebInputEvent::TouchCancel
,
327 (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(),
329 if (gesture_provider_
.GetCurrentDownEvent() &&
330 gesture_provider_
.OnTouchEvent(MotionEventWeb(touch_event_
)))
331 ForwardTouchEventToClient();
334 void TouchEmulator::UpdateCursor() {
336 client_
->SetCursor(pointer_cursor_
);
338 client_
->SetCursor(InPinchGestureMode() ? pinch_cursor_
: touch_cursor_
);
341 bool TouchEmulator::UpdateShiftPressed(bool shift_pressed
) {
342 if (shift_pressed_
== shift_pressed
)
344 shift_pressed_
= shift_pressed
;
349 void TouchEmulator::PinchBegin(const WebGestureEvent
& event
) {
350 DCHECK(InPinchGestureMode());
351 DCHECK(!pinch_gesture_active_
);
352 pinch_gesture_active_
= true;
353 pinch_anchor_
= gfx::Point(event
.x
, event
.y
);
355 FillPinchEvent(event
);
356 pinch_event_
.type
= WebInputEvent::GesturePinchBegin
;
357 client_
->ForwardGestureEvent(pinch_event_
);
360 void TouchEmulator::PinchUpdate(const WebGestureEvent
& event
) {
361 DCHECK(pinch_gesture_active_
);
362 int dy
= pinch_anchor_
.y() - event
.y
;
363 float scale
= exp(dy
* 0.002f
);
364 FillPinchEvent(event
);
365 pinch_event_
.type
= WebInputEvent::GesturePinchUpdate
;
366 pinch_event_
.data
.pinchUpdate
.scale
= scale
/ pinch_scale_
;
367 client_
->ForwardGestureEvent(pinch_event_
);
368 pinch_scale_
= scale
;
371 void TouchEmulator::PinchEnd(const WebGestureEvent
& event
) {
372 DCHECK(pinch_gesture_active_
);
373 pinch_gesture_active_
= false;
374 FillPinchEvent(event
);
375 pinch_event_
.type
= WebInputEvent::GesturePinchEnd
;
376 client_
->ForwardGestureEvent(pinch_event_
);
379 void TouchEmulator::FillPinchEvent(const WebInputEvent
& event
) {
380 pinch_event_
.timeStampSeconds
= event
.timeStampSeconds
;
381 pinch_event_
.modifiers
= event
.modifiers
;
382 pinch_event_
.sourceDevice
= blink::WebGestureDeviceTouchscreen
;
383 pinch_event_
.x
= pinch_anchor_
.x();
384 pinch_event_
.y
= pinch_anchor_
.y();
387 void TouchEmulator::ScrollEnd(const WebGestureEvent
& event
) {
388 WebGestureEvent scroll_event
;
389 scroll_event
.timeStampSeconds
= event
.timeStampSeconds
;
390 scroll_event
.modifiers
= event
.modifiers
;
391 scroll_event
.sourceDevice
= blink::WebGestureDeviceTouchscreen
;
392 scroll_event
.type
= WebInputEvent::GestureScrollEnd
;
393 client_
->ForwardGestureEvent(scroll_event
);
396 bool TouchEmulator::FillTouchEventAndPoint(const WebMouseEvent
& mouse_event
) {
397 if (mouse_event
.type
!= WebInputEvent::MouseDown
&&
398 mouse_event
.type
!= WebInputEvent::MouseMove
&&
399 mouse_event
.type
!= WebInputEvent::MouseUp
) {
403 WebInputEvent::Type eventType
;
404 switch (mouse_event
.type
) {
405 case WebInputEvent::MouseDown
:
406 eventType
= WebInputEvent::TouchStart
;
408 case WebInputEvent::MouseMove
:
409 eventType
= WebInputEvent::TouchMove
;
411 case WebInputEvent::MouseUp
:
412 eventType
= WebInputEvent::TouchEnd
;
415 eventType
= WebInputEvent::Undefined
;
418 touch_event_
.touchesLength
= 1;
419 touch_event_
.modifiers
= mouse_event
.modifiers
;
420 WebTouchEventTraits::ResetTypeAndTouchStates(
421 eventType
, mouse_event
.timeStampSeconds
, &touch_event_
);
423 WebTouchPoint
& point
= touch_event_
.touches
[0];
425 point
.radiusX
= 0.5f
* cursor_size_
.width();
426 point
.radiusY
= 0.5f
* cursor_size_
.height();
428 point
.rotationAngle
= 0.f
;
429 point
.position
.x
= mouse_event
.x
;
430 point
.screenPosition
.x
= mouse_event
.globalX
;
431 point
.position
.y
= mouse_event
.y
;
432 point
.screenPosition
.y
= mouse_event
.globalY
;
437 bool TouchEmulator::InPinchGestureMode() const {
438 return shift_pressed_
;
441 } // namespace content