| 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 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 // We cannot cleanup properly in destructor, as we need roundtrip to the | 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 | 79 // renderer for ack. Instead, the owner should call Disable, and only |
| 80 // destroy this object when renderer is dead. | 80 // destroy this object when renderer is dead. |
| 81 } | 81 } |
| 82 | 82 |
| 83 void TouchEmulator::ResetState() { | 83 void TouchEmulator::ResetState() { |
| 84 last_mouse_event_was_move_ = false; | 84 last_mouse_event_was_move_ = false; |
| 85 last_mouse_move_timestamp_ = 0; | 85 last_mouse_move_timestamp_ = 0; |
| 86 mouse_pressed_ = false; | 86 mouse_pressed_ = false; |
| 87 shift_pressed_ = false; | 87 shift_pressed_ = false; |
| 88 touch_active_ = false; | 88 emulated_stream_active_sequence_count_ = 0; |
| 89 native_stream_active_sequence_count_ = 0; |
| 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 DCHECK(native_stream_active_sequence_count_); |
| 246 if (is_sequence_end) |
| 247 native_stream_active_sequence_count_--; |
| 248 return false; |
| 201 } | 249 } |
| 202 | 250 |
| 203 void TouchEmulator::OnGestureEvent(const ui::GestureEventData& gesture) { | 251 void TouchEmulator::OnGestureEvent(const ui::GestureEventData& gesture) { |
| 204 WebGestureEvent gesture_event = | 252 WebGestureEvent gesture_event = |
| 205 CreateWebGestureEventFromGestureEventData(gesture); | 253 CreateWebGestureEventFromGestureEventData(gesture); |
| 206 | 254 |
| 207 switch (gesture_event.type) { | 255 switch (gesture_event.type) { |
| 208 case WebInputEvent::Undefined: | 256 case WebInputEvent::Undefined: |
| 209 NOTREACHED() << "Undefined WebInputEvent type"; | 257 NOTREACHED() << "Undefined WebInputEvent type"; |
| 210 // Bail without sending the junk event to the client. | 258 // 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; | 308 suppress_next_fling_cancel_ = false; |
| 261 break; | 309 break; |
| 262 | 310 |
| 263 default: | 311 default: |
| 264 // Everything else goes through. | 312 // Everything else goes through. |
| 265 client_->ForwardGestureEvent(gesture_event); | 313 client_->ForwardGestureEvent(gesture_event); |
| 266 } | 314 } |
| 267 } | 315 } |
| 268 | 316 |
| 269 void TouchEmulator::CancelTouch() { | 317 void TouchEmulator::CancelTouch() { |
| 270 if (!touch_active_) | 318 if (!emulated_stream_active_sequence_count_) |
| 271 return; | 319 return; |
| 272 | 320 |
| 273 WebTouchEventTraits::ResetTypeAndTouchStates( | 321 WebTouchEventTraits::ResetTypeAndTouchStates( |
| 274 WebInputEvent::TouchCancel, | 322 WebInputEvent::TouchCancel, |
| 275 (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(), | 323 (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(), |
| 276 &touch_event_); | 324 &touch_event_); |
| 277 touch_active_ = false; | |
| 278 if (gesture_provider_.OnTouchEvent(MotionEventWeb(touch_event_))) | 325 if (gesture_provider_.OnTouchEvent(MotionEventWeb(touch_event_))) |
| 279 client_->ForwardTouchEvent(touch_event_); | 326 ForwardTouchEventToClient(); |
| 280 } | 327 } |
| 281 | 328 |
| 282 void TouchEmulator::UpdateCursor() { | 329 void TouchEmulator::UpdateCursor() { |
| 283 if (!enabled_) | 330 if (!enabled_) |
| 284 client_->SetCursor(pointer_cursor_); | 331 client_->SetCursor(pointer_cursor_); |
| 285 else | 332 else |
| 286 client_->SetCursor(InPinchGestureMode() ? pinch_cursor_ : touch_cursor_); | 333 client_->SetCursor(InPinchGestureMode() ? pinch_cursor_ : touch_cursor_); |
| 287 } | 334 } |
| 288 | 335 |
| 289 bool TouchEmulator::UpdateShiftPressed(bool shift_pressed) { | 336 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 && | 392 if (mouse_event.type != WebInputEvent::MouseDown && |
| 346 mouse_event.type != WebInputEvent::MouseMove && | 393 mouse_event.type != WebInputEvent::MouseMove && |
| 347 mouse_event.type != WebInputEvent::MouseUp) { | 394 mouse_event.type != WebInputEvent::MouseUp) { |
| 348 return false; | 395 return false; |
| 349 } | 396 } |
| 350 | 397 |
| 351 WebInputEvent::Type eventType; | 398 WebInputEvent::Type eventType; |
| 352 switch (mouse_event.type) { | 399 switch (mouse_event.type) { |
| 353 case WebInputEvent::MouseDown: | 400 case WebInputEvent::MouseDown: |
| 354 eventType = WebInputEvent::TouchStart; | 401 eventType = WebInputEvent::TouchStart; |
| 355 touch_active_ = true; | |
| 356 break; | 402 break; |
| 357 case WebInputEvent::MouseMove: | 403 case WebInputEvent::MouseMove: |
| 358 eventType = WebInputEvent::TouchMove; | 404 eventType = WebInputEvent::TouchMove; |
| 359 break; | 405 break; |
| 360 case WebInputEvent::MouseUp: | 406 case WebInputEvent::MouseUp: |
| 361 eventType = WebInputEvent::TouchEnd; | 407 eventType = WebInputEvent::TouchEnd; |
| 362 touch_active_ = false; | |
| 363 break; | 408 break; |
| 364 default: | 409 default: |
| 365 eventType = WebInputEvent::Undefined; | 410 eventType = WebInputEvent::Undefined; |
| 366 NOTREACHED(); | 411 NOTREACHED(); |
| 367 } | 412 } |
| 368 touch_event_.touchesLength = 1; | 413 touch_event_.touchesLength = 1; |
| 369 touch_event_.modifiers = mouse_event.modifiers; | 414 touch_event_.modifiers = mouse_event.modifiers; |
| 370 WebTouchEventTraits::ResetTypeAndTouchStates( | 415 WebTouchEventTraits::ResetTypeAndTouchStates( |
| 371 eventType, mouse_event.timeStampSeconds, &touch_event_); | 416 eventType, mouse_event.timeStampSeconds, &touch_event_); |
| 372 | 417 |
| 373 WebTouchPoint& point = touch_event_.touches[0]; | 418 WebTouchPoint& point = touch_event_.touches[0]; |
| 374 point.id = 0; | 419 point.id = 0; |
| 375 point.radiusX = 0.5f * cursor_size_.width(); | 420 point.radiusX = 0.5f * cursor_size_.width(); |
| 376 point.radiusY = 0.5f * cursor_size_.height(); | 421 point.radiusY = 0.5f * cursor_size_.height(); |
| 377 point.force = 1.f; | 422 point.force = 1.f; |
| 378 point.rotationAngle = 0.f; | 423 point.rotationAngle = 0.f; |
| 379 point.position.x = mouse_event.x; | 424 point.position.x = mouse_event.x; |
| 380 point.screenPosition.x = mouse_event.globalX; | 425 point.screenPosition.x = mouse_event.globalX; |
| 381 point.position.y = mouse_event.y; | 426 point.position.y = mouse_event.y; |
| 382 point.screenPosition.y = mouse_event.globalY; | 427 point.screenPosition.y = mouse_event.globalY; |
| 383 | 428 |
| 384 return true; | 429 return true; |
| 385 } | 430 } |
| 386 | 431 |
| 387 bool TouchEmulator::InPinchGestureMode() const { | 432 bool TouchEmulator::InPinchGestureMode() const { |
| 388 return shift_pressed_ && allow_pinch_; | 433 return shift_pressed_ && allow_pinch_; |
| 389 } | 434 } |
| 390 | 435 |
| 391 } // namespace content | 436 } // namespace content |
| OLD | NEW |