Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(540)

Side by Side Diff: content/browser/renderer_host/input/touch_emulator.cc

Issue 375863005: Touch emulator: allow multiple touch streams. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: moved to touch emulator Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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_ = false;
89 native_stream_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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 kMouseMoveDropIntervalSeconds) 141 kMouseMoveDropIntervalSeconds)
141 return true; 142 return true;
142 143
143 last_mouse_event_was_move_ = true; 144 last_mouse_event_was_move_ = true;
144 last_mouse_move_timestamp_ = mouse_event.timeStampSeconds; 145 last_mouse_move_timestamp_ = mouse_event.timeStampSeconds;
145 } else { 146 } else {
146 last_mouse_event_was_move_ = false; 147 last_mouse_event_was_move_ = false;
147 } 148 }
148 149
149 if (mouse_event.type == WebInputEvent::MouseDown) 150 if (mouse_event.type == WebInputEvent::MouseDown)
150 mouse_pressed_ = true; 151 mouse_pressed_ = true;
jdduke (slow) 2014/07/15 19:43:03 Is the mouse_pressed_ variable redundant with |emu
dgozman 2014/07/16 09:05:03 mouse_pressed_ is a generation flag, we always sen
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_;
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 // Do not allow middle-sequence event to pass through, if start was blocked.
199 // TODO(dgozman): Disable emulation when real touch events are available. 200 if (native_stream_active_ ||
jdduke (slow) 2014/07/15 19:43:03 Hmm, maybe split out the conditional? if (!emula
dgozman 2014/07/16 09:05:03 This code have changed now.
201 (!emulated_stream_active_ &&
202 WebTouchEventTraits::IsTouchSequenceStart(event))) {
203 native_stream_active_ = true;
204 return false;
205 }
200 return true; 206 return true;
201 } 207 }
202 208
209 void TouchEmulator::ForwardTouchEventToClient() {
210 // Block emulated event when native touch stream is active.
211 // Do not allow middle-sequence event to pass through, if start was blocked.
212 if (emulated_stream_active_ ||
213 (!native_stream_active_ &&
214 WebTouchEventTraits::IsTouchSequenceStart(touch_event_))) {
215 emulated_stream_active_ = true;
216 client_->ForwardEmulatedTouchEvent(touch_event_);
217 } else {
218 // Block emulated stream and ack immediately.
219 gesture_provider_.OnTouchEventAck(true);
jdduke (slow) 2014/07/15 19:43:03 Nit: For readability maybe add something like con
dgozman 2014/07/16 09:05:03 Done.
220 }
221 }
222
223 bool TouchEmulator::HandleTouchEventAck(
224 const blink::WebTouchEvent& event, InputEventAckState ack_result) {
225 if (emulated_stream_active_) {
226 if (WebTouchEventTraits::IsTouchSequenceEnd(event))
227 emulated_stream_active_ = false;
228
229 const bool event_consumed = ack_result == INPUT_EVENT_ACK_STATE_CONSUMED;
230 gesture_provider_.OnTouchEventAck(event_consumed);
231 return true;
232 }
233
234 DCHECK(native_stream_active_);
235 if (WebTouchEventTraits::IsTouchSequenceEnd(event))
236 native_stream_active_ = false;
237 return false;
238 }
239
203 void TouchEmulator::OnGestureEvent(const ui::GestureEventData& gesture) { 240 void TouchEmulator::OnGestureEvent(const ui::GestureEventData& gesture) {
204 WebGestureEvent gesture_event = 241 WebGestureEvent gesture_event =
205 CreateWebGestureEventFromGestureEventData(gesture); 242 CreateWebGestureEventFromGestureEventData(gesture);
206 243
207 switch (gesture_event.type) { 244 switch (gesture_event.type) {
208 case WebInputEvent::Undefined: 245 case WebInputEvent::Undefined:
209 NOTREACHED() << "Undefined WebInputEvent type"; 246 NOTREACHED() << "Undefined WebInputEvent type";
210 // Bail without sending the junk event to the client. 247 // Bail without sending the junk event to the client.
211 return; 248 return;
212 249
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 suppress_next_fling_cancel_ = false; 297 suppress_next_fling_cancel_ = false;
261 break; 298 break;
262 299
263 default: 300 default:
264 // Everything else goes through. 301 // Everything else goes through.
265 client_->ForwardGestureEvent(gesture_event); 302 client_->ForwardGestureEvent(gesture_event);
266 } 303 }
267 } 304 }
268 305
269 void TouchEmulator::CancelTouch() { 306 void TouchEmulator::CancelTouch() {
270 if (!touch_active_) 307 if (!emulated_stream_active_)
271 return; 308 return;
272 309
273 WebTouchEventTraits::ResetTypeAndTouchStates( 310 WebTouchEventTraits::ResetTypeAndTouchStates(
274 WebInputEvent::TouchCancel, 311 WebInputEvent::TouchCancel,
275 (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(), 312 (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(),
276 &touch_event_); 313 &touch_event_);
277 touch_active_ = false;
278 if (gesture_provider_.OnTouchEvent(MotionEventWeb(touch_event_))) 314 if (gesture_provider_.OnTouchEvent(MotionEventWeb(touch_event_)))
279 client_->ForwardTouchEvent(touch_event_); 315 ForwardTouchEventToClient();
280 } 316 }
281 317
282 void TouchEmulator::UpdateCursor() { 318 void TouchEmulator::UpdateCursor() {
283 if (!enabled_) 319 if (!enabled_)
284 client_->SetCursor(pointer_cursor_); 320 client_->SetCursor(pointer_cursor_);
285 else 321 else
286 client_->SetCursor(InPinchGestureMode() ? pinch_cursor_ : touch_cursor_); 322 client_->SetCursor(InPinchGestureMode() ? pinch_cursor_ : touch_cursor_);
287 } 323 }
288 324
289 bool TouchEmulator::UpdateShiftPressed(bool shift_pressed) { 325 bool TouchEmulator::UpdateShiftPressed(bool shift_pressed) {
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
345 if (mouse_event.type != WebInputEvent::MouseDown && 381 if (mouse_event.type != WebInputEvent::MouseDown &&
346 mouse_event.type != WebInputEvent::MouseMove && 382 mouse_event.type != WebInputEvent::MouseMove &&
347 mouse_event.type != WebInputEvent::MouseUp) { 383 mouse_event.type != WebInputEvent::MouseUp) {
348 return false; 384 return false;
349 } 385 }
350 386
351 WebInputEvent::Type eventType; 387 WebInputEvent::Type eventType;
352 switch (mouse_event.type) { 388 switch (mouse_event.type) {
353 case WebInputEvent::MouseDown: 389 case WebInputEvent::MouseDown:
354 eventType = WebInputEvent::TouchStart; 390 eventType = WebInputEvent::TouchStart;
355 touch_active_ = true;
356 break; 391 break;
357 case WebInputEvent::MouseMove: 392 case WebInputEvent::MouseMove:
358 eventType = WebInputEvent::TouchMove; 393 eventType = WebInputEvent::TouchMove;
359 break; 394 break;
360 case WebInputEvent::MouseUp: 395 case WebInputEvent::MouseUp:
361 eventType = WebInputEvent::TouchEnd; 396 eventType = WebInputEvent::TouchEnd;
362 touch_active_ = false;
363 break; 397 break;
364 default: 398 default:
365 eventType = WebInputEvent::Undefined; 399 eventType = WebInputEvent::Undefined;
366 NOTREACHED(); 400 NOTREACHED();
367 } 401 }
368 touch_event_.touchesLength = 1; 402 touch_event_.touchesLength = 1;
369 touch_event_.modifiers = mouse_event.modifiers; 403 touch_event_.modifiers = mouse_event.modifiers;
370 WebTouchEventTraits::ResetTypeAndTouchStates( 404 WebTouchEventTraits::ResetTypeAndTouchStates(
371 eventType, mouse_event.timeStampSeconds, &touch_event_); 405 eventType, mouse_event.timeStampSeconds, &touch_event_);
372 406
373 WebTouchPoint& point = touch_event_.touches[0]; 407 WebTouchPoint& point = touch_event_.touches[0];
374 point.id = 0; 408 point.id = 0;
375 point.radiusX = 0.5f * cursor_size_.width(); 409 point.radiusX = 0.5f * cursor_size_.width();
376 point.radiusY = 0.5f * cursor_size_.height(); 410 point.radiusY = 0.5f * cursor_size_.height();
377 point.force = 1.f; 411 point.force = 1.f;
378 point.rotationAngle = 0.f; 412 point.rotationAngle = 0.f;
379 point.position.x = mouse_event.x; 413 point.position.x = mouse_event.x;
380 point.screenPosition.x = mouse_event.globalX; 414 point.screenPosition.x = mouse_event.globalX;
381 point.position.y = mouse_event.y; 415 point.position.y = mouse_event.y;
382 point.screenPosition.y = mouse_event.globalY; 416 point.screenPosition.y = mouse_event.globalY;
383 417
384 return true; 418 return true;
385 } 419 }
386 420
387 bool TouchEmulator::InPinchGestureMode() const { 421 bool TouchEmulator::InPinchGestureMode() const {
388 return shift_pressed_ && allow_pinch_; 422 return shift_pressed_ && allow_pinch_;
389 } 423 }
390 424
391 } // namespace content 425 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698