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/public/common/content_client.h"
11 #include "content/public/common/content_switches.h"
12 #include "grit/content_resources.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;
38 // Time between two consecutive mouse moves, during which second mouse move
39 // is not converted to touch.
40 const double kMouseMoveDropIntervalSeconds
= 5.f
/ 1000;
44 TouchEmulator::TouchEmulator(TouchEmulatorClient
* client
)
46 gesture_provider_(GetGestureProviderConfig(), this),
52 bool use_2x
= gfx::Screen::GetNativeScreen()->
53 GetPrimaryDisplay().device_scale_factor() > 1.5f
;
54 float cursor_scale_factor
= use_2x
? 2.f
: 1.f
;
55 InitCursorFromResource(&touch_cursor_
,
57 use_2x
? IDR_DEVTOOLS_TOUCH_CURSOR_ICON_2X
:
58 IDR_DEVTOOLS_TOUCH_CURSOR_ICON
);
59 InitCursorFromResource(&pinch_cursor_
,
61 use_2x
? IDR_DEVTOOLS_PINCH_CURSOR_ICON_2X
:
62 IDR_DEVTOOLS_PINCH_CURSOR_ICON
);
64 WebCursor::CursorInfo cursor_info
;
65 cursor_info
.type
= blink::WebCursorInfo::TypePointer
;
66 pointer_cursor_
.InitFromCursorInfo(cursor_info
);
68 // TODO(dgozman): Use synthetic secondary touch to support multi-touch.
69 gesture_provider_
.SetMultiTouchZoomSupportEnabled(false);
70 // TODO(dgozman): Enable double tap if requested by the renderer.
71 // TODO(dgozman): Don't break double-tap-based pinch with shift handling.
72 gesture_provider_
.SetDoubleTapSupportForPlatformEnabled(false);
75 TouchEmulator::~TouchEmulator() {
76 // We cannot cleanup properly in destructor, as we need roundtrip to the
77 // renderer for ack. Instead, the owner should call Disable, and only
78 // destroy this object when renderer is dead.
81 void TouchEmulator::ResetState() {
82 last_mouse_event_was_move_
= false;
83 last_mouse_move_timestamp_
= 0;
84 mouse_pressed_
= false;
85 shift_pressed_
= false;
86 touch_active_
= false;
87 suppress_next_fling_cancel_
= false;
89 pinch_gesture_active_
= false;
92 void TouchEmulator::Enable(bool allow_pinch
) {
97 allow_pinch_
= allow_pinch
;
101 void TouchEmulator::Disable() {
110 void TouchEmulator::InitCursorFromResource(
111 WebCursor
* cursor
, float scale
, int resource_id
) {
112 gfx::Image
& cursor_image
=
113 content::GetContentClient()->GetNativeImageNamed(resource_id
);
114 WebCursor::CursorInfo cursor_info
;
115 cursor_info
.type
= blink::WebCursorInfo::TypeCustom
;
116 cursor_info
.image_scale_factor
= scale
;
117 cursor_info
.custom_image
= cursor_image
.AsBitmap();
118 cursor_info
.hotspot
=
119 gfx::Point(cursor_image
.Width() / 2, cursor_image
.Height() / 2);
121 cursor_info
.external_handle
= 0;
124 cursor
->InitFromCursorInfo(cursor_info
);
127 bool TouchEmulator::HandleMouseEvent(const WebMouseEvent
& mouse_event
) {
131 if (mouse_event
.button
!= WebMouseEvent::ButtonLeft
)
134 if (mouse_event
.type
== WebInputEvent::MouseMove
) {
135 if (last_mouse_event_was_move_
&&
136 mouse_event
.timeStampSeconds
< last_mouse_move_timestamp_
+
137 kMouseMoveDropIntervalSeconds
)
140 last_mouse_event_was_move_
= true;
141 last_mouse_move_timestamp_
= mouse_event
.timeStampSeconds
;
143 last_mouse_event_was_move_
= false;
146 if (mouse_event
.type
== WebInputEvent::MouseDown
)
147 mouse_pressed_
= true;
148 else if (mouse_event
.type
== WebInputEvent::MouseUp
)
149 mouse_pressed_
= false;
151 UpdateShiftPressed((mouse_event
.modifiers
& WebInputEvent::ShiftKey
) != 0);
153 if (FillTouchEventAndPoint(mouse_event
) &&
154 gesture_provider_
.OnTouchEvent(MotionEventWeb(touch_event_
))) {
155 client_
->ForwardTouchEvent(touch_event_
);
158 // Do not pass mouse events to the renderer.
162 bool TouchEmulator::HandleMouseWheelEvent(const WebMouseWheelEvent
& event
) {
166 // No mouse wheel events for the renderer.
170 bool TouchEmulator::HandleKeyboardEvent(const WebKeyboardEvent
& event
) {
174 if (!UpdateShiftPressed((event
.modifiers
& WebInputEvent::ShiftKey
) != 0))
180 // Note: The necessary pinch events will be lazily inserted by
181 // |OnGestureEvent| depending on the state of |shift_pressed_|, using the
182 // scroll stream as the event driver.
183 if (shift_pressed_
) {
184 // TODO(dgozman): Add secondary touch point and set anchor.
186 // TODO(dgozman): Remove secondary touch point and anchor.
189 // Never block keyboard events.
193 bool TouchEmulator::HandleTouchEventAck(InputEventAckState ack_result
) {
194 const bool event_consumed
= ack_result
== INPUT_EVENT_ACK_STATE_CONSUMED
;
195 gesture_provider_
.OnTouchEventAck(event_consumed
);
196 // TODO(dgozman): Disable emulation when real touch events are available.
200 void TouchEmulator::OnGestureEvent(const ui::GestureEventData
& gesture
) {
201 WebGestureEvent gesture_event
=
202 CreateWebGestureEventFromGestureEventData(gesture
);
204 switch (gesture_event
.type
) {
205 case WebInputEvent::GestureScrollBegin
:
206 client_
->ForwardGestureEvent(gesture_event
);
207 // PinchBegin must always follow ScrollBegin.
208 if (InPinchGestureMode())
209 PinchBegin(gesture_event
);
212 case WebInputEvent::GestureScrollUpdate
:
213 if (InPinchGestureMode()) {
214 // Convert scrolls to pinches while shift is pressed.
215 if (!pinch_gesture_active_
)
216 PinchBegin(gesture_event
);
218 PinchUpdate(gesture_event
);
220 // Pass scroll update further. If shift was released, end the pinch.
221 if (pinch_gesture_active_
)
222 PinchEnd(gesture_event
);
223 client_
->ForwardGestureEvent(gesture_event
);
227 case WebInputEvent::GestureScrollEnd
:
228 // PinchEnd must precede ScrollEnd.
229 if (pinch_gesture_active_
)
230 PinchEnd(gesture_event
);
231 client_
->ForwardGestureEvent(gesture_event
);
234 case WebInputEvent::GestureFlingStart
:
235 // PinchEnd must precede FlingStart.
236 if (pinch_gesture_active_
)
237 PinchEnd(gesture_event
);
238 if (InPinchGestureMode()) {
239 // No fling in pinch mode. Forward scroll end instead of fling start.
240 suppress_next_fling_cancel_
= true;
241 ScrollEnd(gesture_event
);
243 suppress_next_fling_cancel_
= false;
244 client_
->ForwardGestureEvent(gesture_event
);
248 case WebInputEvent::GestureFlingCancel
:
249 // If fling start was suppressed, we should not send fling cancel either.
250 if (!suppress_next_fling_cancel_
)
251 client_
->ForwardGestureEvent(gesture_event
);
252 suppress_next_fling_cancel_
= false;
256 // Everything else goes through.
257 client_
->ForwardGestureEvent(gesture_event
);
261 void TouchEmulator::CancelTouch() {
265 WebTouchEventTraits::ResetTypeAndTouchStates(
266 WebInputEvent::TouchCancel
,
267 (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(),
269 touch_active_
= false;
270 if (gesture_provider_
.OnTouchEvent(MotionEventWeb(touch_event_
)))
271 client_
->ForwardTouchEvent(touch_event_
);
274 void TouchEmulator::UpdateCursor() {
276 client_
->SetCursor(pointer_cursor_
);
278 client_
->SetCursor(InPinchGestureMode() ? pinch_cursor_
: touch_cursor_
);
281 bool TouchEmulator::UpdateShiftPressed(bool shift_pressed
) {
282 if (shift_pressed_
== shift_pressed
)
284 shift_pressed_
= shift_pressed
;
289 void TouchEmulator::PinchBegin(const WebGestureEvent
& event
) {
290 DCHECK(InPinchGestureMode());
291 DCHECK(!pinch_gesture_active_
);
292 pinch_gesture_active_
= true;
293 pinch_anchor_
= gfx::Point(event
.x
, event
.y
);
295 FillPinchEvent(event
);
296 pinch_event_
.type
= WebInputEvent::GesturePinchBegin
;
297 client_
->ForwardGestureEvent(pinch_event_
);
300 void TouchEmulator::PinchUpdate(const WebGestureEvent
& event
) {
301 DCHECK(pinch_gesture_active_
);
302 int dy
= pinch_anchor_
.y() - event
.y
;
303 float scale
= exp(dy
* 0.002f
);
304 FillPinchEvent(event
);
305 pinch_event_
.type
= WebInputEvent::GesturePinchUpdate
;
306 pinch_event_
.data
.pinchUpdate
.scale
= scale
/ pinch_scale_
;
307 client_
->ForwardGestureEvent(pinch_event_
);
308 pinch_scale_
= scale
;
311 void TouchEmulator::PinchEnd(const WebGestureEvent
& event
) {
312 DCHECK(pinch_gesture_active_
);
313 pinch_gesture_active_
= false;
314 FillPinchEvent(event
);
315 pinch_event_
.type
= WebInputEvent::GesturePinchEnd
;
316 client_
->ForwardGestureEvent(pinch_event_
);
319 void TouchEmulator::FillPinchEvent(const WebInputEvent
& event
) {
320 pinch_event_
.timeStampSeconds
= event
.timeStampSeconds
;
321 pinch_event_
.modifiers
= event
.modifiers
;
322 pinch_event_
.sourceDevice
= blink::WebGestureEvent::Touchscreen
;
323 pinch_event_
.x
= pinch_anchor_
.x();
324 pinch_event_
.y
= pinch_anchor_
.y();
327 void TouchEmulator::ScrollEnd(const WebGestureEvent
& event
) {
328 WebGestureEvent scroll_event
;
329 scroll_event
.timeStampSeconds
= event
.timeStampSeconds
;
330 scroll_event
.modifiers
= event
.modifiers
;
331 scroll_event
.sourceDevice
= blink::WebGestureEvent::Touchscreen
;
332 scroll_event
.type
= WebInputEvent::GestureScrollEnd
;
333 client_
->ForwardGestureEvent(scroll_event
);
336 bool TouchEmulator::FillTouchEventAndPoint(const WebMouseEvent
& mouse_event
) {
337 if (mouse_event
.type
!= WebInputEvent::MouseDown
&&
338 mouse_event
.type
!= WebInputEvent::MouseMove
&&
339 mouse_event
.type
!= WebInputEvent::MouseUp
) {
343 WebInputEvent::Type eventType
;
344 switch (mouse_event
.type
) {
345 case WebInputEvent::MouseDown
:
346 eventType
= WebInputEvent::TouchStart
;
347 touch_active_
= true;
349 case WebInputEvent::MouseMove
:
350 eventType
= WebInputEvent::TouchMove
;
352 case WebInputEvent::MouseUp
:
353 eventType
= WebInputEvent::TouchEnd
;
356 eventType
= WebInputEvent::Undefined
;
359 touch_event_
.touchesLength
= 1;
360 touch_event_
.modifiers
= mouse_event
.modifiers
;
361 WebTouchEventTraits::ResetTypeAndTouchStates(
362 eventType
, mouse_event
.timeStampSeconds
, &touch_event_
);
364 WebTouchPoint
& point
= touch_event_
.touches
[0];
366 point
.radiusX
= point
.radiusY
= 1.f
;
368 point
.rotationAngle
= 0.f
;
369 point
.position
.x
= mouse_event
.x
;
370 point
.screenPosition
.x
= mouse_event
.globalX
;
371 point
.position
.y
= mouse_event
.y
;
372 point
.screenPosition
.y
= mouse_event
.globalY
;
377 bool TouchEmulator::InPinchGestureMode() const {
378 return shift_pressed_
&& allow_pinch_
;
381 } // namespace content