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

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

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

Powered by Google App Engine
This is Rietveld 408576698