Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/renderer_host/input/touch_emulator.h" | 5 #include "content/browser/renderer_host/input/touch_emulator.h" |
| 6 | 6 |
| 7 #include "content/browser/renderer_host/input/motion_event_web.h" | 7 #include "content/browser/renderer_host/input/motion_event_web.h" |
| 8 #include "content/browser/renderer_host/input/web_input_event_util.h" | 8 #include "content/browser/renderer_host/input/web_input_event_util.h" |
| 9 #include "content/common/input/web_touch_event_traits.h" | 9 #include "content/common/input/web_touch_event_traits.h" |
| 10 #include "content/public/common/content_client.h" | 10 #include "content/public/common/content_client.h" |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 40 // Time between two consecutive mouse moves, during which second mouse move | 40 // Time between two consecutive mouse moves, during which second mouse move |
| 41 // is not converted to touch. | 41 // is not converted to touch. |
| 42 const double kMouseMoveDropIntervalSeconds = 5.f / 1000; | 42 const double kMouseMoveDropIntervalSeconds = 5.f / 1000; |
| 43 | 43 |
| 44 } // namespace | 44 } // namespace |
| 45 | 45 |
| 46 TouchEmulator::TouchEmulator(TouchEmulatorClient* client) | 46 TouchEmulator::TouchEmulator(TouchEmulatorClient* client) |
| 47 : client_(client), | 47 : client_(client), |
| 48 gesture_provider_(GetGestureProviderConfig(), this), | 48 gesture_provider_(GetGestureProviderConfig(), this), |
| 49 enabled_(false), | 49 enabled_(false), |
| 50 allow_pinch_(false) { | 50 allow_pinch_(false), |
| 51 emulated_stream_active_sequence_count_(0), | |
|
jdduke (slow)
2014/07/18 15:01:05
Good catch.
| |
| 52 native_stream_active_sequence_count_(0) { | |
| 51 DCHECK(client_); | 53 DCHECK(client_); |
| 52 ResetState(); | 54 ResetState(); |
| 53 | 55 |
| 54 bool use_2x = gfx::Screen::GetNativeScreen()-> | 56 bool use_2x = gfx::Screen::GetNativeScreen()-> |
| 55 GetPrimaryDisplay().device_scale_factor() > 1.5f; | 57 GetPrimaryDisplay().device_scale_factor() > 1.5f; |
| 56 float cursor_scale_factor = use_2x ? 2.f : 1.f; | 58 float cursor_scale_factor = use_2x ? 2.f : 1.f; |
| 57 cursor_size_ = InitCursorFromResource(&touch_cursor_, | 59 cursor_size_ = InitCursorFromResource(&touch_cursor_, |
| 58 cursor_scale_factor, | 60 cursor_scale_factor, |
| 59 use_2x ? IDR_DEVTOOLS_TOUCH_CURSOR_ICON_2X : | 61 use_2x ? IDR_DEVTOOLS_TOUCH_CURSOR_ICON_2X : |
| 60 IDR_DEVTOOLS_TOUCH_CURSOR_ICON); | 62 IDR_DEVTOOLS_TOUCH_CURSOR_ICON); |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 78 // We cannot cleanup properly in destructor, as we need roundtrip to the | 80 // We cannot cleanup properly in destructor, as we need roundtrip to the |
| 79 // renderer for ack. Instead, the owner should call Disable, and only | 81 // renderer for ack. Instead, the owner should call Disable, and only |
| 80 // destroy this object when renderer is dead. | 82 // destroy this object when renderer is dead. |
| 81 } | 83 } |
| 82 | 84 |
| 83 void TouchEmulator::ResetState() { | 85 void TouchEmulator::ResetState() { |
| 84 last_mouse_event_was_move_ = false; | 86 last_mouse_event_was_move_ = false; |
| 85 last_mouse_move_timestamp_ = 0; | 87 last_mouse_move_timestamp_ = 0; |
| 86 mouse_pressed_ = false; | 88 mouse_pressed_ = false; |
| 87 shift_pressed_ = false; | 89 shift_pressed_ = false; |
| 88 touch_active_ = false; | |
| 89 suppress_next_fling_cancel_ = false; | 90 suppress_next_fling_cancel_ = false; |
| 90 pinch_scale_ = 1.f; | 91 pinch_scale_ = 1.f; |
| 91 pinch_gesture_active_ = false; | 92 pinch_gesture_active_ = false; |
| 92 } | 93 } |
| 93 | 94 |
| 94 void TouchEmulator::Enable(bool allow_pinch) { | 95 void TouchEmulator::Enable(bool allow_pinch) { |
| 95 if (!enabled_) { | 96 if (!enabled_) { |
| 96 enabled_ = true; | 97 enabled_ = true; |
| 97 ResetState(); | 98 ResetState(); |
| 98 } | 99 } |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 148 | 149 |
| 149 if (mouse_event.type == WebInputEvent::MouseDown) | 150 if (mouse_event.type == WebInputEvent::MouseDown) |
| 150 mouse_pressed_ = true; | 151 mouse_pressed_ = true; |
| 151 else if (mouse_event.type == WebInputEvent::MouseUp) | 152 else if (mouse_event.type == WebInputEvent::MouseUp) |
| 152 mouse_pressed_ = false; | 153 mouse_pressed_ = false; |
| 153 | 154 |
| 154 UpdateShiftPressed((mouse_event.modifiers & WebInputEvent::ShiftKey) != 0); | 155 UpdateShiftPressed((mouse_event.modifiers & WebInputEvent::ShiftKey) != 0); |
| 155 | 156 |
| 156 if (FillTouchEventAndPoint(mouse_event) && | 157 if (FillTouchEventAndPoint(mouse_event) && |
| 157 gesture_provider_.OnTouchEvent(MotionEventWeb(touch_event_))) { | 158 gesture_provider_.OnTouchEvent(MotionEventWeb(touch_event_))) { |
| 158 client_->ForwardTouchEvent(touch_event_); | 159 ForwardTouchEventToClient(); |
| 159 } | 160 } |
| 160 | 161 |
| 161 // Do not pass mouse events to the renderer. | 162 // Do not pass mouse events to the renderer. |
| 162 return true; | 163 return true; |
| 163 } | 164 } |
| 164 | 165 |
| 165 bool TouchEmulator::HandleMouseWheelEvent(const WebMouseWheelEvent& event) { | 166 bool TouchEmulator::HandleMouseWheelEvent(const WebMouseWheelEvent& event) { |
| 166 if (!enabled_) | 167 if (!enabled_) |
| 167 return false; | 168 return false; |
| 168 | 169 |
| 169 // Send mouse wheel for easy scrolling when there is no active touch. | 170 // Send mouse wheel for easy scrolling when there is no active touch. |
| 170 return touch_active_; | 171 return emulated_stream_active_sequence_count_ > 0; |
| 171 } | 172 } |
| 172 | 173 |
| 173 bool TouchEmulator::HandleKeyboardEvent(const WebKeyboardEvent& event) { | 174 bool TouchEmulator::HandleKeyboardEvent(const WebKeyboardEvent& event) { |
| 174 if (!enabled_) | 175 if (!enabled_) |
| 175 return false; | 176 return false; |
| 176 | 177 |
| 177 if (!UpdateShiftPressed((event.modifiers & WebInputEvent::ShiftKey) != 0)) | 178 if (!UpdateShiftPressed((event.modifiers & WebInputEvent::ShiftKey) != 0)) |
| 178 return false; | 179 return false; |
| 179 | 180 |
| 180 if (!mouse_pressed_) | 181 if (!mouse_pressed_) |
| 181 return false; | 182 return false; |
| 182 | 183 |
| 183 // Note: The necessary pinch events will be lazily inserted by | 184 // Note: The necessary pinch events will be lazily inserted by |
| 184 // |OnGestureEvent| depending on the state of |shift_pressed_|, using the | 185 // |OnGestureEvent| depending on the state of |shift_pressed_|, using the |
| 185 // scroll stream as the event driver. | 186 // scroll stream as the event driver. |
| 186 if (shift_pressed_) { | 187 if (shift_pressed_) { |
| 187 // TODO(dgozman): Add secondary touch point and set anchor. | 188 // TODO(dgozman): Add secondary touch point and set anchor. |
| 188 } else { | 189 } else { |
| 189 // TODO(dgozman): Remove secondary touch point and anchor. | 190 // TODO(dgozman): Remove secondary touch point and anchor. |
| 190 } | 191 } |
| 191 | 192 |
| 192 // Never block keyboard events. | 193 // Never block keyboard events. |
| 193 return false; | 194 return false; |
| 194 } | 195 } |
| 195 | 196 |
| 196 bool TouchEmulator::HandleTouchEventAck(InputEventAckState ack_result) { | 197 bool TouchEmulator::HandleTouchEvent(const blink::WebTouchEvent& event) { |
| 197 const bool event_consumed = ack_result == INPUT_EVENT_ACK_STATE_CONSUMED; | 198 // Block native event when emulated touch stream is active. |
| 198 gesture_provider_.OnTouchEventAck(event_consumed); | 199 if (emulated_stream_active_sequence_count_) |
| 199 // TODO(dgozman): Disable emulation when real touch events are available. | 200 return true; |
| 200 return true; | 201 |
| 202 bool is_sequence_start = WebTouchEventTraits::IsTouchSequenceStart(event); | |
| 203 // Do not allow middle-sequence event to pass through, if start was blocked. | |
| 204 if (!native_stream_active_sequence_count_ && !is_sequence_start) | |
| 205 return true; | |
| 206 | |
| 207 if (is_sequence_start) | |
| 208 native_stream_active_sequence_count_++; | |
| 209 return false; | |
| 210 } | |
| 211 | |
| 212 void TouchEmulator::ForwardTouchEventToClient() { | |
| 213 const bool event_consumed = true; | |
| 214 // Block emulated event when emulated native stream is active. | |
| 215 if (native_stream_active_sequence_count_) { | |
| 216 gesture_provider_.OnTouchEventAck(event_consumed); | |
| 217 return; | |
| 218 } | |
| 219 | |
| 220 bool is_sequence_start = | |
| 221 WebTouchEventTraits::IsTouchSequenceStart(touch_event_); | |
| 222 // Do not allow middle-sequence event to pass through, if start was blocked. | |
| 223 if (!emulated_stream_active_sequence_count_ && !is_sequence_start) { | |
| 224 gesture_provider_.OnTouchEventAck(event_consumed); | |
| 225 return; | |
| 226 } | |
| 227 | |
| 228 if (is_sequence_start) | |
| 229 emulated_stream_active_sequence_count_++; | |
| 230 client_->ForwardEmulatedTouchEvent(touch_event_); | |
| 231 } | |
| 232 | |
| 233 bool TouchEmulator::HandleTouchEventAck( | |
| 234 const blink::WebTouchEvent& event, InputEventAckState ack_result) { | |
| 235 bool is_sequence_end = WebTouchEventTraits::IsTouchSequenceEnd(event); | |
| 236 if (emulated_stream_active_sequence_count_) { | |
| 237 if (is_sequence_end) | |
| 238 emulated_stream_active_sequence_count_--; | |
| 239 | |
| 240 const bool event_consumed = ack_result == INPUT_EVENT_ACK_STATE_CONSUMED; | |
| 241 gesture_provider_.OnTouchEventAck(event_consumed); | |
| 242 return true; | |
| 243 } | |
| 244 | |
| 245 // We may have not seen native touch sequence start (when created in the | |
| 246 // middle of a sequence), so don't decrement sequence count below zero. | |
| 247 if (is_sequence_end && native_stream_active_sequence_count_) | |
| 248 native_stream_active_sequence_count_--; | |
| 249 return false; | |
| 201 } | 250 } |
| 202 | 251 |
| 203 void TouchEmulator::OnGestureEvent(const ui::GestureEventData& gesture) { | 252 void TouchEmulator::OnGestureEvent(const ui::GestureEventData& gesture) { |
| 204 WebGestureEvent gesture_event = | 253 WebGestureEvent gesture_event = |
| 205 CreateWebGestureEventFromGestureEventData(gesture); | 254 CreateWebGestureEventFromGestureEventData(gesture); |
| 206 | 255 |
| 207 switch (gesture_event.type) { | 256 switch (gesture_event.type) { |
| 208 case WebInputEvent::Undefined: | 257 case WebInputEvent::Undefined: |
| 209 NOTREACHED() << "Undefined WebInputEvent type"; | 258 NOTREACHED() << "Undefined WebInputEvent type"; |
| 210 // Bail without sending the junk event to the client. | 259 // Bail without sending the junk event to the client. |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 260 suppress_next_fling_cancel_ = false; | 309 suppress_next_fling_cancel_ = false; |
| 261 break; | 310 break; |
| 262 | 311 |
| 263 default: | 312 default: |
| 264 // Everything else goes through. | 313 // Everything else goes through. |
| 265 client_->ForwardGestureEvent(gesture_event); | 314 client_->ForwardGestureEvent(gesture_event); |
| 266 } | 315 } |
| 267 } | 316 } |
| 268 | 317 |
| 269 void TouchEmulator::CancelTouch() { | 318 void TouchEmulator::CancelTouch() { |
| 270 if (!touch_active_) | 319 if (!emulated_stream_active_sequence_count_) |
| 271 return; | 320 return; |
| 272 | 321 |
| 273 WebTouchEventTraits::ResetTypeAndTouchStates( | 322 WebTouchEventTraits::ResetTypeAndTouchStates( |
| 274 WebInputEvent::TouchCancel, | 323 WebInputEvent::TouchCancel, |
| 275 (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(), | 324 (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(), |
| 276 &touch_event_); | 325 &touch_event_); |
| 277 touch_active_ = false; | |
| 278 if (gesture_provider_.OnTouchEvent(MotionEventWeb(touch_event_))) | 326 if (gesture_provider_.OnTouchEvent(MotionEventWeb(touch_event_))) |
| 279 client_->ForwardTouchEvent(touch_event_); | 327 ForwardTouchEventToClient(); |
| 280 } | 328 } |
| 281 | 329 |
| 282 void TouchEmulator::UpdateCursor() { | 330 void TouchEmulator::UpdateCursor() { |
| 283 if (!enabled_) | 331 if (!enabled_) |
| 284 client_->SetCursor(pointer_cursor_); | 332 client_->SetCursor(pointer_cursor_); |
| 285 else | 333 else |
| 286 client_->SetCursor(InPinchGestureMode() ? pinch_cursor_ : touch_cursor_); | 334 client_->SetCursor(InPinchGestureMode() ? pinch_cursor_ : touch_cursor_); |
| 287 } | 335 } |
| 288 | 336 |
| 289 bool TouchEmulator::UpdateShiftPressed(bool shift_pressed) { | 337 bool TouchEmulator::UpdateShiftPressed(bool shift_pressed) { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 345 if (mouse_event.type != WebInputEvent::MouseDown && | 393 if (mouse_event.type != WebInputEvent::MouseDown && |
| 346 mouse_event.type != WebInputEvent::MouseMove && | 394 mouse_event.type != WebInputEvent::MouseMove && |
| 347 mouse_event.type != WebInputEvent::MouseUp) { | 395 mouse_event.type != WebInputEvent::MouseUp) { |
| 348 return false; | 396 return false; |
| 349 } | 397 } |
| 350 | 398 |
| 351 WebInputEvent::Type eventType; | 399 WebInputEvent::Type eventType; |
| 352 switch (mouse_event.type) { | 400 switch (mouse_event.type) { |
| 353 case WebInputEvent::MouseDown: | 401 case WebInputEvent::MouseDown: |
| 354 eventType = WebInputEvent::TouchStart; | 402 eventType = WebInputEvent::TouchStart; |
| 355 touch_active_ = true; | |
| 356 break; | 403 break; |
| 357 case WebInputEvent::MouseMove: | 404 case WebInputEvent::MouseMove: |
| 358 eventType = WebInputEvent::TouchMove; | 405 eventType = WebInputEvent::TouchMove; |
| 359 break; | 406 break; |
| 360 case WebInputEvent::MouseUp: | 407 case WebInputEvent::MouseUp: |
| 361 eventType = WebInputEvent::TouchEnd; | 408 eventType = WebInputEvent::TouchEnd; |
| 362 touch_active_ = false; | |
| 363 break; | 409 break; |
| 364 default: | 410 default: |
| 365 eventType = WebInputEvent::Undefined; | 411 eventType = WebInputEvent::Undefined; |
| 366 NOTREACHED(); | 412 NOTREACHED(); |
| 367 } | 413 } |
| 368 touch_event_.touchesLength = 1; | 414 touch_event_.touchesLength = 1; |
| 369 touch_event_.modifiers = mouse_event.modifiers; | 415 touch_event_.modifiers = mouse_event.modifiers; |
| 370 WebTouchEventTraits::ResetTypeAndTouchStates( | 416 WebTouchEventTraits::ResetTypeAndTouchStates( |
| 371 eventType, mouse_event.timeStampSeconds, &touch_event_); | 417 eventType, mouse_event.timeStampSeconds, &touch_event_); |
| 372 | 418 |
| 373 WebTouchPoint& point = touch_event_.touches[0]; | 419 WebTouchPoint& point = touch_event_.touches[0]; |
| 374 point.id = 0; | 420 point.id = 0; |
| 375 point.radiusX = 0.5f * cursor_size_.width(); | 421 point.radiusX = 0.5f * cursor_size_.width(); |
| 376 point.radiusY = 0.5f * cursor_size_.height(); | 422 point.radiusY = 0.5f * cursor_size_.height(); |
| 377 point.force = 1.f; | 423 point.force = 1.f; |
| 378 point.rotationAngle = 0.f; | 424 point.rotationAngle = 0.f; |
| 379 point.position.x = mouse_event.x; | 425 point.position.x = mouse_event.x; |
| 380 point.screenPosition.x = mouse_event.globalX; | 426 point.screenPosition.x = mouse_event.globalX; |
| 381 point.position.y = mouse_event.y; | 427 point.position.y = mouse_event.y; |
| 382 point.screenPosition.y = mouse_event.globalY; | 428 point.screenPosition.y = mouse_event.globalY; |
| 383 | 429 |
| 384 return true; | 430 return true; |
| 385 } | 431 } |
| 386 | 432 |
| 387 bool TouchEmulator::InPinchGestureMode() const { | 433 bool TouchEmulator::InPinchGestureMode() const { |
| 388 return shift_pressed_ && allow_pinch_; | 434 return shift_pressed_ && allow_pinch_; |
| 389 } | 435 } |
| 390 | 436 |
| 391 } // namespace content | 437 } // namespace content |
| OLD | NEW |