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

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

Powered by Google App Engine
This is Rietveld 408576698