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),
51 emulated_stream_active_sequence_count_(0),
52 native_stream_active_sequence_count_(0) {
56 bool use_2x = gfx::Screen::GetNativeScreen()->
57 GetPrimaryDisplay().device_scale_factor() > 1.5f;
58 float cursor_scale_factor = use_2x ? 2.f : 1.f;
59 cursor_size_ = InitCursorFromResource(&touch_cursor_,
61 use_2x ? IDR_DEVTOOLS_TOUCH_CURSOR_ICON_2X :
62 IDR_DEVTOOLS_TOUCH_CURSOR_ICON);
63 InitCursorFromResource(&pinch_cursor_,
65 use_2x ? IDR_DEVTOOLS_PINCH_CURSOR_ICON_2X :
66 IDR_DEVTOOLS_PINCH_CURSOR_ICON);
68 WebCursor::CursorInfo cursor_info;
69 cursor_info.type = blink::WebCursorInfo::TypePointer;
70 pointer_cursor_.InitFromCursorInfo(cursor_info);
72 // TODO(dgozman): Use synthetic secondary touch to support multi-touch.
73 gesture_provider_.SetMultiTouchZoomSupportEnabled(false);
74 // TODO(dgozman): Enable double tap if requested by the renderer.
75 // TODO(dgozman): Don't break double-tap-based pinch with shift handling.
76 gesture_provider_.SetDoubleTapSupportForPlatformEnabled(false);
79 TouchEmulator::~TouchEmulator() {
80 // We cannot cleanup properly in destructor, as we need roundtrip to the
81 // renderer for ack. Instead, the owner should call Disable, and only
82 // destroy this object when renderer is dead.
85 void TouchEmulator::ResetState() {
86 last_mouse_event_was_move_ = false;
87 last_mouse_move_timestamp_ = 0;
88 mouse_pressed_ = false;
89 shift_pressed_ = false;
90 suppress_next_fling_cancel_ = false;
92 pinch_gesture_active_ = false;
95 void TouchEmulator::Enable(bool allow_pinch) {
100 allow_pinch_ = allow_pinch;
104 void TouchEmulator::Disable() {
113 gfx::SizeF TouchEmulator::InitCursorFromResource(
114 WebCursor* cursor, float scale, int resource_id) {
115 gfx::Image& cursor_image =
116 content::GetContentClient()->GetNativeImageNamed(resource_id);
117 WebCursor::CursorInfo cursor_info;
118 cursor_info.type = blink::WebCursorInfo::TypeCustom;
119 cursor_info.image_scale_factor = scale;
120 cursor_info.custom_image = cursor_image.AsBitmap();
121 cursor_info.hotspot =
122 gfx::Point(cursor_image.Width() / 2, cursor_image.Height() / 2);
124 cursor_info.external_handle = 0;
127 cursor->InitFromCursorInfo(cursor_info);
128 return gfx::ScaleSize(cursor_image.Size(), 1.f / scale);
131 bool TouchEmulator::HandleMouseEvent(const WebMouseEvent& mouse_event) {
135 if (mouse_event.button == WebMouseEvent::ButtonRight &&
136 mouse_event.type == WebInputEvent::MouseDown) {
137 client_->ShowContextMenuAtPoint(gfx::Point(mouse_event.x, mouse_event.y));
140 if (mouse_event.button != WebMouseEvent::ButtonLeft)
143 if (mouse_event.type == WebInputEvent::MouseMove) {
144 if (last_mouse_event_was_move_ &&
145 mouse_event.timeStampSeconds < last_mouse_move_timestamp_ +
146 kMouseMoveDropIntervalSeconds)
149 last_mouse_event_was_move_ = true;
150 last_mouse_move_timestamp_ = mouse_event.timeStampSeconds;
152 last_mouse_event_was_move_ = false;
155 if (mouse_event.type == WebInputEvent::MouseDown)
156 mouse_pressed_ = true;
157 else if (mouse_event.type == WebInputEvent::MouseUp)
158 mouse_pressed_ = false;
160 UpdateShiftPressed((mouse_event.modifiers & WebInputEvent::ShiftKey) != 0);
162 if (FillTouchEventAndPoint(mouse_event) &&
163 gesture_provider_.OnTouchEvent(MotionEventWeb(touch_event_))) {
164 ForwardTouchEventToClient();
167 // Do not pass mouse events to the renderer.
171 bool TouchEmulator::HandleMouseWheelEvent(const WebMouseWheelEvent& event) {
175 // Send mouse wheel for easy scrolling when there is no active touch.
176 return emulated_stream_active_sequence_count_ > 0;
179 bool TouchEmulator::HandleKeyboardEvent(const WebKeyboardEvent& event) {
183 if (!UpdateShiftPressed((event.modifiers & WebInputEvent::ShiftKey) != 0))
189 // Note: The necessary pinch events will be lazily inserted by
190 // |OnGestureEvent| depending on the state of |shift_pressed_|, using the
191 // scroll stream as the event driver.
192 if (shift_pressed_) {
193 // TODO(dgozman): Add secondary touch point and set anchor.
195 // TODO(dgozman): Remove secondary touch point and anchor.
198 // Never block keyboard events.
202 bool TouchEmulator::HandleTouchEvent(const blink::WebTouchEvent& event) {
203 // Block native event when emulated touch stream is active.
204 if (emulated_stream_active_sequence_count_)
207 bool is_sequence_start = WebTouchEventTraits::IsTouchSequenceStart(event);
208 // Do not allow middle-sequence event to pass through, if start was blocked.
209 if (!native_stream_active_sequence_count_ && !is_sequence_start)
212 if (is_sequence_start)
213 native_stream_active_sequence_count_++;
217 void TouchEmulator::ForwardTouchEventToClient() {
218 const bool event_consumed = true;
219 // Block emulated event when emulated native stream is active.
220 if (native_stream_active_sequence_count_) {
221 gesture_provider_.OnTouchEventAck(event_consumed);
225 bool is_sequence_start =
226 WebTouchEventTraits::IsTouchSequenceStart(touch_event_);
227 // Do not allow middle-sequence event to pass through, if start was blocked.
228 if (!emulated_stream_active_sequence_count_ && !is_sequence_start) {
229 gesture_provider_.OnTouchEventAck(event_consumed);
233 if (is_sequence_start)
234 emulated_stream_active_sequence_count_++;
235 client_->ForwardEmulatedTouchEvent(touch_event_);
238 bool TouchEmulator::HandleTouchEventAck(
239 const blink::WebTouchEvent& event, InputEventAckState ack_result) {
240 bool is_sequence_end = WebTouchEventTraits::IsTouchSequenceEnd(event);
241 if (emulated_stream_active_sequence_count_) {
243 emulated_stream_active_sequence_count_--;
245 const bool event_consumed = ack_result == INPUT_EVENT_ACK_STATE_CONSUMED;
246 gesture_provider_.OnTouchEventAck(event_consumed);
250 // We may have not seen native touch sequence start (when created in the
251 // middle of a sequence), so don't decrement sequence count below zero.
252 if (is_sequence_end && native_stream_active_sequence_count_)
253 native_stream_active_sequence_count_--;
257 void TouchEmulator::OnGestureEvent(const ui::GestureEventData& gesture) {
258 WebGestureEvent gesture_event =
259 CreateWebGestureEventFromGestureEventData(gesture);
261 switch (gesture_event.type) {
262 case WebInputEvent::Undefined:
263 NOTREACHED() << "Undefined WebInputEvent type";
264 // Bail without sending the junk event to the client.
267 case WebInputEvent::GestureScrollBegin:
268 client_->ForwardGestureEvent(gesture_event);
269 // PinchBegin must always follow ScrollBegin.
270 if (InPinchGestureMode())
271 PinchBegin(gesture_event);
274 case WebInputEvent::GestureScrollUpdate:
275 if (InPinchGestureMode()) {
276 // Convert scrolls to pinches while shift is pressed.
277 if (!pinch_gesture_active_)
278 PinchBegin(gesture_event);
280 PinchUpdate(gesture_event);
282 // Pass scroll update further. If shift was released, end the pinch.
283 if (pinch_gesture_active_)
284 PinchEnd(gesture_event);
285 client_->ForwardGestureEvent(gesture_event);
289 case WebInputEvent::GestureScrollEnd:
290 // PinchEnd must precede ScrollEnd.
291 if (pinch_gesture_active_)
292 PinchEnd(gesture_event);
293 client_->ForwardGestureEvent(gesture_event);
296 case WebInputEvent::GestureFlingStart:
297 // PinchEnd must precede FlingStart.
298 if (pinch_gesture_active_)
299 PinchEnd(gesture_event);
300 if (InPinchGestureMode()) {
301 // No fling in pinch mode. Forward scroll end instead of fling start.
302 suppress_next_fling_cancel_ = true;
303 ScrollEnd(gesture_event);
305 suppress_next_fling_cancel_ = false;
306 client_->ForwardGestureEvent(gesture_event);
310 case WebInputEvent::GestureFlingCancel:
311 // If fling start was suppressed, we should not send fling cancel either.
312 if (!suppress_next_fling_cancel_)
313 client_->ForwardGestureEvent(gesture_event);
314 suppress_next_fling_cancel_ = false;
318 // Everything else goes through.
319 client_->ForwardGestureEvent(gesture_event);
323 void TouchEmulator::CancelTouch() {
324 if (!emulated_stream_active_sequence_count_)
327 WebTouchEventTraits::ResetTypeAndTouchStates(
328 WebInputEvent::TouchCancel,
329 (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(),
331 if (gesture_provider_.OnTouchEvent(MotionEventWeb(touch_event_)))
332 ForwardTouchEventToClient();
335 void TouchEmulator::UpdateCursor() {
337 client_->SetCursor(pointer_cursor_);
339 client_->SetCursor(InPinchGestureMode() ? pinch_cursor_ : touch_cursor_);
342 bool TouchEmulator::UpdateShiftPressed(bool shift_pressed) {
343 if (shift_pressed_ == shift_pressed)
345 shift_pressed_ = shift_pressed;
350 void TouchEmulator::PinchBegin(const WebGestureEvent& event) {
351 DCHECK(InPinchGestureMode());
352 DCHECK(!pinch_gesture_active_);
353 pinch_gesture_active_ = true;
354 pinch_anchor_ = gfx::Point(event.x, event.y);
356 FillPinchEvent(event);
357 pinch_event_.type = WebInputEvent::GesturePinchBegin;
358 client_->ForwardGestureEvent(pinch_event_);
361 void TouchEmulator::PinchUpdate(const WebGestureEvent& event) {
362 DCHECK(pinch_gesture_active_);
363 int dy = pinch_anchor_.y() - event.y;
364 float scale = exp(dy * 0.002f);
365 FillPinchEvent(event);
366 pinch_event_.type = WebInputEvent::GesturePinchUpdate;
367 pinch_event_.data.pinchUpdate.scale = scale / pinch_scale_;
368 client_->ForwardGestureEvent(pinch_event_);
369 pinch_scale_ = scale;
372 void TouchEmulator::PinchEnd(const WebGestureEvent& event) {
373 DCHECK(pinch_gesture_active_);
374 pinch_gesture_active_ = false;
375 FillPinchEvent(event);
376 pinch_event_.type = WebInputEvent::GesturePinchEnd;
377 client_->ForwardGestureEvent(pinch_event_);
380 void TouchEmulator::FillPinchEvent(const WebInputEvent& event) {
381 pinch_event_.timeStampSeconds = event.timeStampSeconds;
382 pinch_event_.modifiers = event.modifiers;
383 pinch_event_.sourceDevice = blink::WebGestureDeviceTouchscreen;
384 pinch_event_.x = pinch_anchor_.x();
385 pinch_event_.y = pinch_anchor_.y();
388 void TouchEmulator::ScrollEnd(const WebGestureEvent& event) {
389 WebGestureEvent scroll_event;
390 scroll_event.timeStampSeconds = event.timeStampSeconds;
391 scroll_event.modifiers = event.modifiers;
392 scroll_event.sourceDevice = blink::WebGestureDeviceTouchscreen;
393 scroll_event.type = WebInputEvent::GestureScrollEnd;
394 client_->ForwardGestureEvent(scroll_event);
397 bool TouchEmulator::FillTouchEventAndPoint(const WebMouseEvent& mouse_event) {
398 if (mouse_event.type != WebInputEvent::MouseDown &&
399 mouse_event.type != WebInputEvent::MouseMove &&
400 mouse_event.type != WebInputEvent::MouseUp) {
404 WebInputEvent::Type eventType;
405 switch (mouse_event.type) {
406 case WebInputEvent::MouseDown:
407 eventType = WebInputEvent::TouchStart;
409 case WebInputEvent::MouseMove:
410 eventType = WebInputEvent::TouchMove;
412 case WebInputEvent::MouseUp:
413 eventType = WebInputEvent::TouchEnd;
416 eventType = WebInputEvent::Undefined;
419 touch_event_.touchesLength = 1;
420 touch_event_.modifiers = mouse_event.modifiers;
421 WebTouchEventTraits::ResetTypeAndTouchStates(
422 eventType, mouse_event.timeStampSeconds, &touch_event_);
424 WebTouchPoint& point = touch_event_.touches[0];
426 point.radiusX = 0.5f * cursor_size_.width();
427 point.radiusY = 0.5f * cursor_size_.height();
429 point.rotationAngle = 0.f;
430 point.position.x = mouse_event.x;
431 point.screenPosition.x = mouse_event.globalX;
432 point.position.y = mouse_event.y;
433 point.screenPosition.y = mouse_event.globalY;
438 bool TouchEmulator::InPinchGestureMode() const {
439 return shift_pressed_ && allow_pinch_;
442 } // namespace content