Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "content/browser/renderer_host/input/touch_emulator.h" | |
| 6 | |
| 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/public/common/content_client.h" | |
| 10 #include "content/public/common/content_switches.h" | |
| 11 #include "grit/content_resources.h" | |
| 12 #include "third_party/WebKit/public/platform/WebCursorInfo.h" | |
| 13 #include "ui/events/gesture_detection/gesture_config_helper.h" | |
| 14 #include "ui/gfx/image/image.h" | |
| 15 | |
| 16 using blink::WebGestureEvent; | |
| 17 using blink::WebInputEvent; | |
| 18 using blink::WebKeyboardEvent; | |
| 19 using blink::WebMouseEvent; | |
| 20 using blink::WebMouseWheelEvent; | |
| 21 using blink::WebTouchEvent; | |
| 22 using blink::WebTouchPoint; | |
| 23 | |
| 24 namespace content { | |
| 25 | |
| 26 namespace { | |
| 27 | |
| 28 ui::GestureProvider::Config GetGestureProviderConfig() { | |
| 29 // TODO(dgozman): Use different configs to emulate mobile/desktop as | |
| 30 // requested by renderer. | |
| 31 ui::GestureProvider::Config config = ui::DefaultGestureProviderConfig(); | |
| 32 config.gesture_begin_end_types_enabled = false; | |
| 33 return config; | |
| 34 } | |
| 35 | |
| 36 } // namespace | |
| 37 | |
| 38 TouchEmulator::TouchEmulator(TouchEmulatorClient* client) | |
| 39 : client_(client), | |
| 40 gesture_provider_(GetGestureProviderConfig(), this) { | |
| 41 Init(); | |
| 42 } | |
| 43 | |
| 44 TouchEmulator::TouchEmulator(TouchEmulatorClient* client, | |
| 45 ui::GestureProvider::Config gesture_config) | |
| 46 : client_(client), | |
| 47 gesture_provider_(gesture_config, this) { | |
| 48 Init(); | |
| 49 } | |
| 50 | |
| 51 void TouchEmulator::Init() { | |
| 52 DCHECK(client_); | |
| 53 enabled_ = false; | |
| 54 allow_pinch_ = false; | |
| 55 mouse_pressed_ = false; | |
| 56 shift_pressed_ = false; | |
| 57 touch_active_ = false; | |
| 58 suppress_next_fling_cancel_ = false; | |
| 59 pinch_scale_ = 1.f; | |
| 60 pinch_gesture_active_ = false; | |
| 61 | |
| 62 InitCursorFromResource(&touch_cursor_, IDR_DEVTOOLS_TOUCH_CURSOR_ICON); | |
| 63 InitCursorFromResource(&pinch_cursor_, IDR_DEVTOOLS_PINCH_CURSOR_ICON); | |
| 64 | |
| 65 WebCursor::CursorInfo cursor_info; | |
| 66 cursor_info.type = blink::WebCursorInfo::TypePointer; | |
| 67 pointer_cursor_.InitFromCursorInfo(cursor_info); | |
| 68 | |
| 69 // TODO(dgozman): Use synthetic secondary touch to support multi-touch. | |
| 70 gesture_provider_.SetMultiTouchSupportEnabled(false); | |
| 71 // TODO(dgozman): Enable double tap if requested by the renderer. | |
| 72 // TODO(dgozman): Don't break double-tap-based pinch with shift handling. | |
| 73 gesture_provider_.SetDoubleTapSupportForPlatformEnabled(false); | |
| 74 } | |
| 75 | |
| 76 TouchEmulator::~TouchEmulator() { | |
| 77 // We cannot cleanup properly in destructor, as we need roundtrip to the | |
| 78 // renderer for ack. Instead, the owner should call Disable, and only | |
| 79 // destroy this object when renderer is dead. | |
| 80 } | |
| 81 | |
| 82 void TouchEmulator::Enable(bool allow_pinch) { | |
| 83 if (!enabled_) { | |
| 84 enabled_ = true; | |
| 85 shift_pressed_ = false; | |
| 86 mouse_pressed_ = false; | |
| 87 touch_active_ = false; | |
| 88 suppress_next_fling_cancel_ = false; | |
| 89 pinch_gesture_active_ = false; | |
| 90 } | |
| 91 allow_pinch_ = allow_pinch; | |
| 92 UpdateCursor(); | |
| 93 } | |
| 94 | |
| 95 void TouchEmulator::Disable() { | |
| 96 if (!enabled_) | |
| 97 return; | |
| 98 | |
| 99 enabled_ = false; | |
| 100 UpdateCursor(); | |
| 101 CancelTouch(); | |
| 102 } | |
| 103 | |
| 104 void TouchEmulator::InitCursorFromResource(WebCursor* cursor, int resource_id) { | |
| 105 gfx::Image& cursor_image = | |
| 106 content::GetContentClient()->GetNativeImageNamed(resource_id); | |
| 107 WebCursor::CursorInfo cursor_info; | |
| 108 cursor_info.type = blink::WebCursorInfo::TypeCustom; | |
| 109 // TODO(dgozman): Add HiDPI cursors. | |
| 110 cursor_info.image_scale_factor = 1.f; | |
| 111 cursor_info.custom_image = cursor_image.AsBitmap(); | |
| 112 cursor_info.hotspot = | |
| 113 gfx::Point(cursor_image.Width() / 2, cursor_image.Height() / 2); | |
| 114 #if defined(OS_WIN) | |
| 115 cursor_info.external_handle = 0; | |
| 116 #endif | |
| 117 | |
| 118 cursor->InitFromCursorInfo(cursor_info); | |
| 119 } | |
| 120 | |
| 121 bool TouchEmulator::HandleMouseEvent(const WebMouseEvent& mouse_event) { | |
| 122 if (!enabled_) | |
| 123 return false; | |
| 124 | |
| 125 if (mouse_event.button != WebMouseEvent::ButtonLeft) | |
| 126 return true; | |
| 127 | |
| 128 if (mouse_event.type == WebInputEvent::MouseDown) | |
| 129 mouse_pressed_ = true; | |
| 130 else if (mouse_event.type == WebInputEvent::MouseUp) | |
| 131 mouse_pressed_ = false; | |
| 132 | |
| 133 UpdateShiftPressed((mouse_event.modifiers & WebInputEvent::ShiftKey) != 0); | |
| 134 | |
| 135 if (FillTouchEventAndPoint(mouse_event) && | |
| 136 gesture_provider_.OnTouchEvent(MotionEventWeb(touch_event_))) { | |
| 137 client_->ForwardTouchEvent(touch_event_); | |
| 138 } | |
| 139 | |
| 140 // Do not pass mouse events to the renderer. | |
| 141 return true; | |
| 142 } | |
| 143 | |
| 144 bool TouchEmulator::HandleMouseWheelEvent(const WebMouseWheelEvent& event) { | |
| 145 if (!enabled_) | |
| 146 return false; | |
| 147 | |
| 148 // No mouse wheel events for the renderer. | |
| 149 return true; | |
| 150 } | |
| 151 | |
| 152 bool TouchEmulator::HandleKeyboardEvent(const WebKeyboardEvent& event) { | |
| 153 if (!enabled_) | |
| 154 return false; | |
| 155 | |
| 156 if (!UpdateShiftPressed((event.modifiers & WebInputEvent::ShiftKey) != 0)) | |
| 157 return false; | |
| 158 | |
| 159 if (!mouse_pressed_) | |
| 160 return false; | |
| 161 | |
| 162 // Note: The necessary pinch events will be lazily inserted by | |
| 163 // |OnGestureEvent| depending on the state of |shift_pressed_|, using the | |
| 164 // scroll stream as the event driver. | |
| 165 if (shift_pressed_) { | |
| 166 // TODO(dgozman): Add secondary touch point and set anchor. | |
| 167 } else { | |
| 168 // TODO(dgozman): Remove secondary touch point and anchor. | |
| 169 } | |
| 170 | |
| 171 // Never block keyboard events. | |
| 172 return false; | |
| 173 } | |
| 174 | |
| 175 bool TouchEmulator::HandleTouchEventAck(InputEventAckState ack_result) { | |
| 176 const bool event_consumed = ack_result == INPUT_EVENT_ACK_STATE_CONSUMED; | |
| 177 gesture_provider_.OnTouchEventAck(event_consumed); | |
| 178 // TODO(dgozman): Disable emulation when real touch events are available. | |
| 179 return true; | |
| 180 } | |
| 181 | |
| 182 void TouchEmulator::OnGestureEvent(const ui::GestureEventData& gesture) { | |
| 183 WebGestureEvent gesture_event = | |
| 184 CreateWebGestureEventFromGestureEventData(gesture, 1); | |
| 185 | |
| 186 switch (gesture_event.type) { | |
| 187 case WebInputEvent::GestureScrollBegin: | |
| 188 client_->ForwardGestureEvent(gesture_event); | |
| 189 // PinchBegin must always follow ScrollBegin. | |
| 190 if (InPinchGestureMode()) | |
| 191 PinchBegin(gesture_event); | |
| 192 break; | |
| 193 | |
| 194 case WebInputEvent::GestureScrollUpdate: | |
| 195 if (InPinchGestureMode()) { | |
| 196 // Convert scrolls to pinches while shift is pressed. | |
| 197 if (!pinch_gesture_active_) | |
| 198 PinchBegin(gesture_event); | |
| 199 else | |
| 200 PinchUpdate(gesture_event); | |
| 201 } else { | |
| 202 // Pass scroll update further. If shift was released, end the pinch. | |
| 203 if (pinch_gesture_active_) | |
| 204 PinchEnd(gesture_event); | |
| 205 client_->ForwardGestureEvent(gesture_event); | |
| 206 } | |
| 207 break; | |
| 208 | |
| 209 case WebInputEvent::GestureScrollEnd: | |
| 210 // PinchEnd must precede ScrollEnd. | |
| 211 if (pinch_gesture_active_) | |
| 212 PinchEnd(gesture_event); | |
| 213 client_->ForwardGestureEvent(gesture_event); | |
| 214 break; | |
| 215 | |
| 216 case WebInputEvent::GestureFlingStart: | |
| 217 // PinchEnd must precede FlingStart. | |
| 218 if (pinch_gesture_active_) | |
| 219 PinchEnd(gesture_event); | |
| 220 if (InPinchGestureMode()) { | |
| 221 // No fling in pinch mode. Forward scroll end instead of fling start. | |
| 222 suppress_next_fling_cancel_ = true; | |
| 223 ScrollEnd(gesture_event); | |
| 224 } else { | |
| 225 suppress_next_fling_cancel_ = false; | |
| 226 client_->ForwardGestureEvent(gesture_event); | |
| 227 } | |
| 228 break; | |
| 229 | |
| 230 case WebInputEvent::GestureFlingCancel: | |
| 231 // If fling start was suppressed, we should not send fling cancel either. | |
| 232 if (!suppress_next_fling_cancel_) | |
| 233 client_->ForwardGestureEvent(gesture_event); | |
| 234 suppress_next_fling_cancel_ = false; | |
| 235 break; | |
| 236 | |
| 237 default: | |
| 238 // Everything else goes through. | |
| 239 client_->ForwardGestureEvent(gesture_event); | |
| 240 } | |
| 241 } | |
| 242 | |
| 243 void TouchEmulator::CancelTouch() { | |
| 244 if (!touch_active_) | |
| 245 return; | |
| 246 | |
| 247 touch_event_.timeStampSeconds = base::Time::Now().ToDoubleT(); | |
|
jdduke (slow)
2014/04/10 15:58:31
We should use base::TimeTicks::Now(): (base::TimeT
dgozman
2014/04/10 16:33:29
Done.
| |
| 248 touch_event_.type = WebInputEvent::TouchCancel; | |
| 249 touch_event_.touches[0].state = WebTouchPoint::StateCancelled; | |
| 250 touch_active_ = false; | |
| 251 if (gesture_provider_.OnTouchEvent(MotionEventWeb(touch_event_))) | |
| 252 client_->ForwardTouchEvent(touch_event_); | |
| 253 } | |
| 254 | |
| 255 void TouchEmulator::UpdateCursor() { | |
| 256 if (!enabled_) | |
| 257 client_->SetCursor(pointer_cursor_); | |
| 258 else | |
| 259 client_->SetCursor(InPinchGestureMode() ? pinch_cursor_ : touch_cursor_); | |
| 260 } | |
| 261 | |
| 262 bool TouchEmulator::UpdateShiftPressed(bool shift_pressed) { | |
| 263 if (shift_pressed_ == shift_pressed) | |
| 264 return false; | |
| 265 shift_pressed_ = shift_pressed; | |
| 266 UpdateCursor(); | |
| 267 return true; | |
| 268 } | |
| 269 | |
| 270 void TouchEmulator::PinchBegin(const WebGestureEvent& event) { | |
| 271 DCHECK(InPinchGestureMode()); | |
| 272 DCHECK(!pinch_gesture_active_); | |
| 273 pinch_gesture_active_ = true; | |
| 274 pinch_anchor_ = gfx::Point(event.x, event.y); | |
| 275 pinch_scale_ = 1.f; | |
| 276 FillPinchEvent(event); | |
| 277 pinch_event_.type = WebInputEvent::GesturePinchBegin; | |
| 278 client_->ForwardGestureEvent(pinch_event_); | |
| 279 } | |
| 280 | |
| 281 void TouchEmulator::PinchUpdate(const WebGestureEvent& event) { | |
| 282 DCHECK(pinch_gesture_active_); | |
| 283 int dy = pinch_anchor_.y() - event.y; | |
| 284 float scale = exp(dy * 0.002f); | |
| 285 FillPinchEvent(event); | |
| 286 pinch_event_.type = WebInputEvent::GesturePinchUpdate; | |
| 287 pinch_event_.data.pinchUpdate.scale = scale / pinch_scale_; | |
| 288 client_->ForwardGestureEvent(pinch_event_); | |
| 289 pinch_scale_ = scale; | |
| 290 } | |
| 291 | |
| 292 void TouchEmulator::PinchEnd(const WebGestureEvent& event) { | |
| 293 DCHECK(pinch_gesture_active_); | |
| 294 pinch_gesture_active_ = false; | |
| 295 FillPinchEvent(event); | |
| 296 pinch_event_.type = WebInputEvent::GesturePinchEnd; | |
| 297 client_->ForwardGestureEvent(pinch_event_); | |
| 298 } | |
| 299 | |
| 300 void TouchEmulator::FillPinchEvent(const WebInputEvent& event) { | |
| 301 pinch_event_.timeStampSeconds = event.timeStampSeconds; | |
| 302 pinch_event_.modifiers = event.modifiers; | |
| 303 pinch_event_.sourceDevice = blink::WebGestureEvent::Touchscreen; | |
| 304 pinch_event_.x = pinch_anchor_.x(); | |
| 305 pinch_event_.y = pinch_anchor_.y(); | |
| 306 } | |
| 307 | |
| 308 void TouchEmulator::ScrollEnd(const WebGestureEvent& event) { | |
| 309 WebGestureEvent scroll_event; | |
| 310 scroll_event.timeStampSeconds = event.timeStampSeconds; | |
| 311 scroll_event.modifiers = event.modifiers; | |
| 312 scroll_event.sourceDevice = blink::WebGestureEvent::Touchscreen; | |
| 313 scroll_event.type = WebInputEvent::GestureScrollEnd; | |
| 314 client_->ForwardGestureEvent(scroll_event); | |
| 315 } | |
| 316 | |
| 317 bool TouchEmulator::FillTouchEventAndPoint(const WebMouseEvent& mouse_event) { | |
| 318 if (mouse_event.type != WebInputEvent::MouseDown && | |
| 319 mouse_event.type != WebInputEvent::MouseMove && | |
| 320 mouse_event.type != WebInputEvent::MouseUp) { | |
| 321 return false; | |
| 322 } | |
| 323 | |
| 324 touch_event_.touchesLength = 1; | |
| 325 touch_event_.timeStampSeconds = mouse_event.timeStampSeconds; | |
| 326 touch_event_.modifiers = mouse_event.modifiers; | |
| 327 | |
| 328 WebTouchPoint& point = touch_event_.touches[0]; | |
| 329 point.id = 0; | |
| 330 point.radiusX = point.radiusY = 1.f; | |
| 331 point.force = 1.f; | |
| 332 point.rotationAngle = 0.f; | |
| 333 point.position.x = mouse_event.x; | |
| 334 point.screenPosition.x = mouse_event.globalX; | |
| 335 point.position.y = mouse_event.y; | |
| 336 point.screenPosition.y = mouse_event.globalY; | |
| 337 | |
| 338 switch (mouse_event.type) { | |
| 339 case WebInputEvent::MouseDown: | |
| 340 touch_event_.type = WebInputEvent::TouchStart; | |
| 341 touch_active_ = true; | |
| 342 point.state = WebTouchPoint::StatePressed; | |
| 343 break; | |
| 344 case WebInputEvent::MouseMove: | |
| 345 touch_event_.type = WebInputEvent::TouchMove; | |
| 346 point.state = WebTouchPoint::StateMoved; | |
| 347 break; | |
| 348 case WebInputEvent::MouseUp: | |
| 349 touch_event_.type = WebInputEvent::TouchEnd; | |
| 350 touch_active_ = false; | |
| 351 point.state = WebTouchPoint::StateReleased; | |
| 352 break; | |
| 353 default: | |
| 354 NOTREACHED(); | |
| 355 } | |
| 356 return true; | |
| 357 } | |
| 358 | |
| 359 bool TouchEmulator::InPinchGestureMode() const { | |
| 360 return shift_pressed_ && allow_pinch_; | |
| 361 } | |
| 362 | |
| 363 } // namespace content | |
| OLD | NEW |