Chromium Code Reviews| Index: content/browser/renderer_host/input/touch_emulator.cc |
| diff --git a/content/browser/renderer_host/input/touch_emulator.cc b/content/browser/renderer_host/input/touch_emulator.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b0904e98975c11125f9fb6f780fad6d362e2a6fb |
| --- /dev/null |
| +++ b/content/browser/renderer_host/input/touch_emulator.cc |
| @@ -0,0 +1,293 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/browser/renderer_host/input/touch_emulator.h" |
| + |
| +#include "content/browser/renderer_host/input/motion_event_web.h" |
| +#include "content/browser/renderer_host/input/web_input_event_util.h" |
| +#include "content/browser/renderer_host/render_widget_host_impl.h" |
| +#include "content/public/browser/native_web_keyboard_event.h" |
| +#include "content/public/common/content_client.h" |
| +#include "content/public/common/content_switches.h" |
| +#include "grit/content_resources.h" |
| +#include "third_party/WebKit/public/platform/WebCursorInfo.h" |
| +#include "ui/events/gesture_detection/gesture_config_helper.h" |
| +#include "ui/gfx/image/image.h" |
| + |
| +using blink::WebGestureEvent; |
| +using blink::WebInputEvent; |
| +using blink::WebMouseEvent; |
| +using blink::WebMouseWheelEvent; |
| +using blink::WebTouchEvent; |
| +using blink::WebTouchPoint; |
| + |
| +namespace content { |
| + |
| +TouchEmulator::TouchEmulator(RenderWidgetHostImpl* host) |
| + : host_(host), |
| + gesture_provider_(ui::DefaultGestureProviderConfig(), this), |
| + mouse_pressed_(false), |
| + shift_pressed_(false), |
| + touch_active_(false), |
| + scroll_gesture_active_(false), |
| + pinch_scale_(1.f), |
| + pinch_gesture_active_(false) { |
| + DCHECK(host); |
| + InitCursorFromResource(&touch_cursor_, IDR_DEVTOOLS_TOUCH_CURSOR_ICON); |
| + InitCursorFromResource(&pinch_cursor_, IDR_DEVTOOLS_PINCH_CURSOR_ICON); |
|
jdduke (slow)
2014/04/04 16:11:28
I noticed that the icons would occasionally revert
dgozman
2014/04/07 16:54:55
I've fixed that in blink, where sometimes cursor w
|
| + UpdateCursor(); |
| + // TODO(dgozman): Use synthetic secondary touch to support multi-touch. |
| + gesture_provider_.SetMultiTouchSupportEnabled(false); |
| + // TODO(dgozman): Enable double tap if requested by the renderer. |
| + // TODO(dgozman): Don't break double-tap-based pinch with shift handling. |
| + gesture_provider_.SetDoubleTapSupportForPlatformEnabled(false); |
| +} |
| + |
| +TouchEmulator::~TouchEmulator() { |
| + WebCursor::CursorInfo cursor_info; |
| + cursor_info.type = blink::WebCursorInfo::TypePointer; |
| + WebCursor cursor; |
| + cursor.InitFromCursorInfo(cursor_info); |
| + host_->SetCursor(cursor); |
| + |
| + if (pinch_gesture_active_) { |
| + pinch_event_.timeStampSeconds = base::Time::Now().ToDoubleT(); |
| + PinchEnd(pinch_event_); |
| + } |
| + if (scroll_gesture_active_) { |
| + pinch_event_.timeStampSeconds = base::Time::Now().ToDoubleT(); |
| + ScrollEnd(pinch_event_); |
| + } |
| + // TODO(dgozman): Should we also send TouchCancel? |
|
jdduke (slow)
2014/04/04 16:11:28
You should be able to accomplish the pinch/scroll
dgozman
2014/04/07 16:54:55
Looks like canceling touch does not initiate a syn
jdduke (slow)
2014/04/07 18:07:26
It should, but only if there's an active touch (mo
|
| +} |
| + |
| +void TouchEmulator::InitCursorFromResource(WebCursor* cursor, int resource_id) { |
| + gfx::Image& cursor_image = |
| + content::GetContentClient()->GetNativeImageNamed(resource_id); |
| + WebCursor::CursorInfo cursor_info; |
| + cursor_info.type = blink::WebCursorInfo::TypeCustom; |
| + // TODO(dgozman): Add HiDPI cursors. |
| + cursor_info.image_scale_factor = 1.f; |
| + cursor_info.custom_image = cursor_image.AsBitmap(); |
| + cursor_info.hotspot = |
| + gfx::Point(cursor_image.Width() / 2, cursor_image.Height() / 2); |
| +#if defined(OS_WIN) |
| + cursor_info.external_handle = 0; |
| +#endif |
| + |
| + cursor->InitFromCursorInfo(cursor_info); |
| +} |
| + |
| +bool TouchEmulator::HandleMouseEvent(const WebMouseEvent& mouse_event) { |
| + if (mouse_event.button != WebMouseEvent::ButtonLeft) |
| + return true; |
| + |
| + if (mouse_event.type == WebInputEvent::MouseDown) |
| + mouse_pressed_ = true; |
| + if (mouse_event.type == WebInputEvent::MouseUp) |
| + mouse_pressed_ = false; |
| + |
| + UpdateShiftPressed((mouse_event.modifiers & WebInputEvent::ShiftKey) != 0); |
| + |
| + if (FillTouchEventAndPoint(mouse_event) && |
| + gesture_provider_.OnTouchEvent(MotionEventWeb(touch_event_))) { |
| + host_->ForwardTouchEventWithLatencyInfo(touch_event_, ui::LatencyInfo()); |
| + } |
| + |
| + // Do not pass mouse events to the renderer. |
| + return true; |
| +} |
| + |
| +bool TouchEmulator::HandleMouseWheelEvent(const WebMouseWheelEvent& event) { |
| + // No mouse wheel events for the renderer. |
| + return true; |
| +} |
| + |
| +bool TouchEmulator::HandleKeyboardEvent(const NativeWebKeyboardEvent& event) { |
|
jdduke (slow)
2014/04/04 16:21:11
Let's use a "blink::WebKeyboardEvent" type here to
dgozman
2014/04/07 16:54:55
Done.
|
| + if (!UpdateShiftPressed((event.modifiers & WebInputEvent::ShiftKey) != 0)) |
| + return false; |
| + |
| + if (!mouse_pressed_) |
| + return false; |
| + |
| + // Note: The necessary pinch events will be lazily inserted by |
| + // |OnGestureEvent| depending on the state of |shift_pressed_|, using the |
| + // scroll stream as the event driver. |
| + if (shift_pressed_) { |
| + // TODO(dgozman): Add secondary touch point and set anchor. |
| + } else { |
| + // TODO(dgozman): Remove secondary touch point and anchor. |
| + } |
| + |
| + // Never block keyboard events. |
| + return false; |
| +} |
| + |
| +bool TouchEmulator::HandleTouchEventAck(InputEventAckState ack_result) { |
| + const bool event_consumed = ack_result == INPUT_EVENT_ACK_STATE_CONSUMED; |
| + gesture_provider_.OnTouchEventAck(event_consumed); |
| + // TODO(dgozman): Disable emulation when real touch events are available. |
| + return true; |
| +} |
| + |
| +void TouchEmulator::OnGestureEvent(const ui::GestureEventData& gesture) { |
| + WebGestureEvent gesture_event = |
| + CreateWebGestureEventFromGestureEventData(gesture, 1); |
| + |
| + // Convert scrolls to pinches while shift is pressed. |
| + if (gesture_event.type == WebInputEvent::GestureScrollUpdate) { |
| + if (InPinchGestureMode()) { |
| + if (!pinch_gesture_active_) |
| + PinchBegin(gesture_event); |
| + else |
| + PinchUpdate(gesture_event); |
| + return; |
| + } else { |
| + if (pinch_gesture_active_) |
| + PinchEnd(gesture_event); |
| + } |
| + } |
| + |
| + if ((gesture_event.type == WebInputEvent::GestureScrollEnd || |
| + gesture_event.type == WebInputEvent::GestureFlingStart)) { |
| + scroll_gesture_active_ = false; |
| + // PinchEnd must always precede ScrollEnd/FlingStart. |
| + if (pinch_gesture_active_) |
| + PinchEnd(gesture_event); |
| + } |
| + |
| + if ((gesture_event.type == WebInputEvent::GestureFlingStart || |
| + gesture_event.type == WebInputEvent::GestureFlingCancel) && |
| + InPinchGestureMode()) { |
| + // No fling in pinch mode. Forward scroll end instead of fling start and |
| + // drop fling cancel. |
| + if (gesture_event.type == WebInputEvent::GestureFlingStart) |
| + ScrollEnd(gesture_event); |
| + } else { |
| + host_->ForwardGestureEvent(gesture_event); |
| + } |
| + |
| + if (gesture_event.type == WebInputEvent::GestureScrollBegin) { |
| + scroll_gesture_active_ = true; |
| + // PinchBegin must always follow ScrollBegin. |
| + if (InPinchGestureMode()) |
| + PinchBegin(gesture_event); |
| + } |
| +} |
| + |
| +void TouchEmulator::CancelTouch() { |
| + touch_event_.timeStampSeconds = base::Time::Now().ToDoubleT(); |
| + touch_event_.type = WebInputEvent::TouchCancel; |
| + touch_event_.touches[0].state = WebTouchPoint::StateCancelled; |
| + touch_active_ = false; |
| + if (gesture_provider_.OnTouchEvent(MotionEventWeb(touch_event_))) |
| + host_->ForwardTouchEventWithLatencyInfo(touch_event_, ui::LatencyInfo()); |
| +} |
| + |
| +void TouchEmulator::UpdateCursor() { |
| + host_->SetCursor(InPinchGestureMode() ? pinch_cursor_ : touch_cursor_); |
| +} |
| + |
| +bool TouchEmulator::UpdateShiftPressed(bool shift_pressed) { |
| + if (shift_pressed_ == shift_pressed) |
| + return false; |
| + shift_pressed_ = shift_pressed; |
| + UpdateCursor(); |
| + return true; |
| +} |
| + |
| +void TouchEmulator::PinchBegin(const WebGestureEvent& event) { |
| + DCHECK(InPinchGestureMode()); |
| + DCHECK(!pinch_gesture_active_); |
| + pinch_gesture_active_ = true; |
| + pinch_anchor_ = gfx::Point(event.x, event.y); |
| + pinch_scale_ = 1.f; |
| + FillPinchEvent(event); |
| + pinch_event_.type = WebInputEvent::GesturePinchBegin; |
| + host_->ForwardGestureEvent(pinch_event_); |
| +} |
| + |
| +void TouchEmulator::PinchUpdate(const WebGestureEvent& event) { |
| + DCHECK(pinch_gesture_active_); |
| + int dy = pinch_anchor_.y() - event.y; |
| + float scale = exp(dy * 0.002f); |
| + FillPinchEvent(event); |
| + pinch_event_.type = WebInputEvent::GesturePinchUpdate; |
| + pinch_event_.data.pinchUpdate.scale = scale / pinch_scale_; |
| + host_->ForwardGestureEvent(pinch_event_); |
| + pinch_scale_ = scale; |
| +} |
| + |
| +void TouchEmulator::PinchEnd(const WebGestureEvent& event) { |
| + DCHECK(pinch_gesture_active_); |
| + pinch_gesture_active_ = false; |
| + FillPinchEvent(event); |
| + pinch_event_.type = WebInputEvent::GesturePinchEnd; |
| + host_->ForwardGestureEvent(pinch_event_); |
| +} |
| + |
| +void TouchEmulator::FillPinchEvent(const WebInputEvent& event) { |
| + pinch_event_.timeStampSeconds = event.timeStampSeconds; |
| + pinch_event_.modifiers = event.modifiers; |
| + pinch_event_.sourceDevice = blink::WebGestureEvent::Touchscreen; |
| + pinch_event_.x = pinch_anchor_.x(); |
| + pinch_event_.y = pinch_anchor_.y(); |
| +} |
| + |
| +void TouchEmulator::ScrollEnd(const WebGestureEvent& event) { |
| + WebGestureEvent scroll_event; |
| + scroll_event.timeStampSeconds = event.timeStampSeconds; |
| + scroll_event.modifiers = event.modifiers; |
| + scroll_event.sourceDevice = blink::WebGestureEvent::Touchscreen; |
| + scroll_event.type = WebInputEvent::GestureScrollEnd; |
| + host_->ForwardGestureEvent(scroll_event); |
| +} |
| + |
| +bool TouchEmulator::FillTouchEventAndPoint(const WebMouseEvent& mouse_event) { |
| + if (mouse_event.type != WebInputEvent::MouseDown && |
| + mouse_event.type != WebInputEvent::MouseMove && |
| + mouse_event.type != WebInputEvent::MouseUp) { |
| + return false; |
| + } |
| + |
| + touch_event_.touchesLength = 1; |
| + touch_event_.timeStampSeconds = mouse_event.timeStampSeconds; |
| + touch_event_.modifiers = mouse_event.modifiers; |
| + |
| + WebTouchPoint& point = touch_event_.touches[0]; |
| + point.id = 0; |
| + point.radiusX = point.radiusY = 1.f; |
| + point.force = 1.f; |
| + point.rotationAngle = 0.f; |
| + point.position.x = mouse_event.x; |
| + point.screenPosition.x = mouse_event.globalX; |
| + point.position.y = mouse_event.y; |
| + point.screenPosition.y = mouse_event.globalY; |
| + |
| + switch (mouse_event.type) { |
| + case WebInputEvent::MouseDown: |
| + touch_event_.type = WebInputEvent::TouchStart; |
| + touch_active_ = true; |
| + point.state = WebTouchPoint::StatePressed; |
| + break; |
| + case WebInputEvent::MouseMove: |
| + touch_event_.type = WebInputEvent::TouchMove; |
| + point.state = WebTouchPoint::StateMoved; |
| + break; |
| + case WebInputEvent::MouseUp: |
| + touch_event_.type = WebInputEvent::TouchEnd; |
| + touch_active_ = false; |
| + point.state = WebTouchPoint::StateReleased; |
| + break; |
| + default: |
| + NOTREACHED(); |
| + } |
| + return true; |
| +} |
| + |
| +bool TouchEmulator::InPinchGestureMode() const { |
| + return shift_pressed_; |
| +} |
| + |
| +} // namespace content |