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;
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),
54 bool use_2x = gfx::Screen::GetNativeScreen()->
55 GetPrimaryDisplay().device_scale_factor() > 1.5f;
56 float cursor_scale_factor = use_2x ? 2.f : 1.f;
57 InitCursorFromResource(&touch_cursor_,
59 use_2x ? IDR_DEVTOOLS_TOUCH_CURSOR_ICON_2X :
60 IDR_DEVTOOLS_TOUCH_CURSOR_ICON);
61 InitCursorFromResource(&pinch_cursor_,
63 use_2x ? IDR_DEVTOOLS_PINCH_CURSOR_ICON_2X :
64 IDR_DEVTOOLS_PINCH_CURSOR_ICON);
66 WebCursor::CursorInfo cursor_info;
67 cursor_info.type = blink::WebCursorInfo::TypePointer;
68 pointer_cursor_.InitFromCursorInfo(cursor_info);
70 // TODO(dgozman): Use synthetic secondary touch to support multi-touch.
71 gesture_provider_.SetMultiTouchZoomSupportEnabled(false);
72 // TODO(dgozman): Enable double tap if requested by the renderer.
73 // TODO(dgozman): Don't break double-tap-based pinch with shift handling.
74 gesture_provider_.SetDoubleTapSupportForPlatformEnabled(false);
77 TouchEmulator::~TouchEmulator() {
78 // We cannot cleanup properly in destructor, as we need roundtrip to the
79 // renderer for ack. Instead, the owner should call Disable, and only
80 // destroy this object when renderer is dead.
83 void TouchEmulator::ResetState() {
84 last_mouse_event_was_move_ = false;
85 last_mouse_move_timestamp_ = 0;
86 mouse_pressed_ = false;
87 shift_pressed_ = false;
88 touch_active_ = false;
89 suppress_next_fling_cancel_ = false;
91 pinch_gesture_active_ = false;
94 void TouchEmulator::Enable(bool allow_pinch) {
99 allow_pinch_ = allow_pinch;
103 void TouchEmulator::Disable() {
112 void TouchEmulator::InitCursorFromResource(
113 WebCursor* cursor, float scale, int resource_id) {
114 gfx::Image& cursor_image =
115 content::GetContentClient()->GetNativeImageNamed(resource_id);
116 WebCursor::CursorInfo cursor_info;
117 cursor_info.type = blink::WebCursorInfo::TypeCustom;
118 cursor_info.image_scale_factor = scale;
119 cursor_info.custom_image = cursor_image.AsBitmap();
120 cursor_info.hotspot =
121 gfx::Point(cursor_image.Width() / 2, cursor_image.Height() / 2);
123 cursor_info.external_handle = 0;
126 cursor->InitFromCursorInfo(cursor_info);
129 bool TouchEmulator::HandleMouseEvent(const WebMouseEvent& mouse_event) {
133 if (mouse_event.button != WebMouseEvent::ButtonLeft)
136 if (mouse_event.type == WebInputEvent::MouseMove) {
137 if (last_mouse_event_was_move_ &&
138 mouse_event.timeStampSeconds < last_mouse_move_timestamp_ +
139 kMouseMoveDropIntervalSeconds)
142 last_mouse_event_was_move_ = true;
143 last_mouse_move_timestamp_ = mouse_event.timeStampSeconds;
145 last_mouse_event_was_move_ = false;
148 if (mouse_event.type == WebInputEvent::MouseDown)
149 mouse_pressed_ = true;
150 else if (mouse_event.type == WebInputEvent::MouseUp)
151 mouse_pressed_ = false;
153 UpdateShiftPressed((mouse_event.modifiers & WebInputEvent::ShiftKey) != 0);
155 if (FillTouchEventAndPoint(mouse_event) &&
156 gesture_provider_.OnTouchEvent(MotionEventWeb(touch_event_))) {
157 client_->ForwardTouchEvent(touch_event_);
160 // Do not pass mouse events to the renderer.
164 bool TouchEmulator::HandleMouseWheelEvent(const WebMouseWheelEvent& event) {
168 // Send mouse wheel for easy scrolling when there is no active touch.
169 return touch_active_;
172 bool TouchEmulator::HandleKeyboardEvent(const WebKeyboardEvent& event) {
176 if (!UpdateShiftPressed((event.modifiers & WebInputEvent::ShiftKey) != 0))
182 // Note: The necessary pinch events will be lazily inserted by
183 // |OnGestureEvent| depending on the state of |shift_pressed_|, using the
184 // scroll stream as the event driver.
185 if (shift_pressed_) {
186 // TODO(dgozman): Add secondary touch point and set anchor.
188 // TODO(dgozman): Remove secondary touch point and anchor.
191 // Never block keyboard events.
195 bool TouchEmulator::HandleTouchEventAck(InputEventAckState ack_result) {
196 const bool event_consumed = ack_result == INPUT_EVENT_ACK_STATE_CONSUMED;
197 gesture_provider_.OnTouchEventAck(event_consumed);
198 // TODO(dgozman): Disable emulation when real touch events are available.
202 void TouchEmulator::OnGestureEvent(const ui::GestureEventData& gesture) {
203 WebGestureEvent gesture_event =
204 CreateWebGestureEventFromGestureEventData(gesture);
206 switch (gesture_event.type) {
207 case WebInputEvent::Undefined:
208 NOTREACHED() << "Undefined WebInputEvent type";
209 // Bail without sending the junk event to the client.
212 case WebInputEvent::GestureScrollBegin:
213 client_->ForwardGestureEvent(gesture_event);
214 // PinchBegin must always follow ScrollBegin.
215 if (InPinchGestureMode())
216 PinchBegin(gesture_event);
219 case WebInputEvent::GestureScrollUpdate:
220 if (InPinchGestureMode()) {
221 // Convert scrolls to pinches while shift is pressed.
222 if (!pinch_gesture_active_)
223 PinchBegin(gesture_event);
225 PinchUpdate(gesture_event);
227 // Pass scroll update further. If shift was released, end the pinch.
228 if (pinch_gesture_active_)
229 PinchEnd(gesture_event);
230 client_->ForwardGestureEvent(gesture_event);
234 case WebInputEvent::GestureScrollEnd:
235 // PinchEnd must precede ScrollEnd.
236 if (pinch_gesture_active_)
237 PinchEnd(gesture_event);
238 client_->ForwardGestureEvent(gesture_event);
241 case WebInputEvent::GestureFlingStart:
242 // PinchEnd must precede FlingStart.
243 if (pinch_gesture_active_)
244 PinchEnd(gesture_event);
245 if (InPinchGestureMode()) {
246 // No fling in pinch mode. Forward scroll end instead of fling start.
247 suppress_next_fling_cancel_ = true;
248 ScrollEnd(gesture_event);
250 suppress_next_fling_cancel_ = false;
251 client_->ForwardGestureEvent(gesture_event);
255 case WebInputEvent::GestureFlingCancel:
256 // If fling start was suppressed, we should not send fling cancel either.
257 if (!suppress_next_fling_cancel_)
258 client_->ForwardGestureEvent(gesture_event);
259 suppress_next_fling_cancel_ = false;
263 // Everything else goes through.
264 client_->ForwardGestureEvent(gesture_event);
268 void TouchEmulator::CancelTouch() {
272 WebTouchEventTraits::ResetTypeAndTouchStates(
273 WebInputEvent::TouchCancel,
274 (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(),
276 touch_active_ = false;
277 if (gesture_provider_.OnTouchEvent(MotionEventWeb(touch_event_)))
278 client_->ForwardTouchEvent(touch_event_);
281 void TouchEmulator::UpdateCursor() {
283 client_->SetCursor(pointer_cursor_);
285 client_->SetCursor(InPinchGestureMode() ? pinch_cursor_ : touch_cursor_);
288 bool TouchEmulator::UpdateShiftPressed(bool shift_pressed) {
289 if (shift_pressed_ == shift_pressed)
291 shift_pressed_ = shift_pressed;
296 void TouchEmulator::PinchBegin(const WebGestureEvent& event) {
297 DCHECK(InPinchGestureMode());
298 DCHECK(!pinch_gesture_active_);
299 pinch_gesture_active_ = true;
300 pinch_anchor_ = gfx::Point(event.x, event.y);
302 FillPinchEvent(event);
303 pinch_event_.type = WebInputEvent::GesturePinchBegin;
304 client_->ForwardGestureEvent(pinch_event_);
307 void TouchEmulator::PinchUpdate(const WebGestureEvent& event) {
308 DCHECK(pinch_gesture_active_);
309 int dy = pinch_anchor_.y() - event.y;
310 float scale = exp(dy * 0.002f);
311 FillPinchEvent(event);
312 pinch_event_.type = WebInputEvent::GesturePinchUpdate;
313 pinch_event_.data.pinchUpdate.scale = scale / pinch_scale_;
314 client_->ForwardGestureEvent(pinch_event_);
315 pinch_scale_ = scale;
318 void TouchEmulator::PinchEnd(const WebGestureEvent& event) {
319 DCHECK(pinch_gesture_active_);
320 pinch_gesture_active_ = false;
321 FillPinchEvent(event);
322 pinch_event_.type = WebInputEvent::GesturePinchEnd;
323 client_->ForwardGestureEvent(pinch_event_);
326 void TouchEmulator::FillPinchEvent(const WebInputEvent& event) {
327 pinch_event_.timeStampSeconds = event.timeStampSeconds;
328 pinch_event_.modifiers = event.modifiers;
329 pinch_event_.sourceDevice = blink::WebGestureDeviceTouchscreen;
330 pinch_event_.x = pinch_anchor_.x();
331 pinch_event_.y = pinch_anchor_.y();
334 void TouchEmulator::ScrollEnd(const WebGestureEvent& event) {
335 WebGestureEvent scroll_event;
336 scroll_event.timeStampSeconds = event.timeStampSeconds;
337 scroll_event.modifiers = event.modifiers;
338 scroll_event.sourceDevice = blink::WebGestureDeviceTouchscreen;
339 scroll_event.type = WebInputEvent::GestureScrollEnd;
340 client_->ForwardGestureEvent(scroll_event);
343 bool TouchEmulator::FillTouchEventAndPoint(const WebMouseEvent& mouse_event) {
344 if (mouse_event.type != WebInputEvent::MouseDown &&
345 mouse_event.type != WebInputEvent::MouseMove &&
346 mouse_event.type != WebInputEvent::MouseUp) {
350 WebInputEvent::Type eventType;
351 switch (mouse_event.type) {
352 case WebInputEvent::MouseDown:
353 eventType = WebInputEvent::TouchStart;
354 touch_active_ = true;
356 case WebInputEvent::MouseMove:
357 eventType = WebInputEvent::TouchMove;
359 case WebInputEvent::MouseUp:
360 eventType = WebInputEvent::TouchEnd;
361 touch_active_ = false;
364 eventType = WebInputEvent::Undefined;
367 touch_event_.touchesLength = 1;
368 touch_event_.modifiers = mouse_event.modifiers;
369 WebTouchEventTraits::ResetTypeAndTouchStates(
370 eventType, mouse_event.timeStampSeconds, &touch_event_);
372 WebTouchPoint& point = touch_event_.touches[0];
374 point.radiusX = point.radiusY = 1.f;
376 point.rotationAngle = 0.f;
377 point.position.x = mouse_event.x;
378 point.screenPosition.x = mouse_event.globalX;
379 point.position.y = mouse_event.y;
380 point.screenPosition.y = mouse_event.globalY;
385 bool TouchEmulator::InPinchGestureMode() const {
386 return shift_pressed_ && allow_pinch_;
389 } // namespace content