Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / input / touch_emulator.cc
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/common/input/web_touch_event_traits.h"
10 #include "content/public/common/content_client.h"
11 #include "content/public/common/content_switches.h"
12 #include "grit/content_resources.h"
13 #include "third_party/WebKit/public/platform/WebCursorInfo.h"
14 #include "ui/events/gesture_detection/gesture_config_helper.h"
15 #include "ui/gfx/image/image.h"
16 #include "ui/gfx/screen.h"
17
18 using blink::WebGestureEvent;
19 using blink::WebInputEvent;
20 using blink::WebKeyboardEvent;
21 using blink::WebMouseEvent;
22 using blink::WebMouseWheelEvent;
23 using blink::WebTouchEvent;
24 using blink::WebTouchPoint;
25
26 namespace content {
27
28 namespace {
29
30 ui::GestureProvider::Config GetGestureProviderConfig() {
31   // TODO(dgozman): Use different configs to emulate mobile/desktop as
32   // requested by renderer.
33   ui::GestureProvider::Config config = ui::DefaultGestureProviderConfig();
34   config.gesture_begin_end_types_enabled = false;
35   return config;
36 }
37
38 // Time between two consecutive mouse moves, during which second mouse move
39 // is not converted to touch.
40 const double kMouseMoveDropIntervalSeconds = 5.f / 1000;
41
42 } // namespace
43
44 TouchEmulator::TouchEmulator(TouchEmulatorClient* client)
45     : client_(client),
46       gesture_provider_(GetGestureProviderConfig(), this),
47       enabled_(false),
48       allow_pinch_(false) {
49   DCHECK(client_);
50   ResetState();
51
52   bool use_2x = gfx::Screen::GetNativeScreen()->
53       GetPrimaryDisplay().device_scale_factor() > 1.5f;
54   float cursor_scale_factor = use_2x ? 2.f : 1.f;
55   InitCursorFromResource(&touch_cursor_,
56       cursor_scale_factor,
57       use_2x ? IDR_DEVTOOLS_TOUCH_CURSOR_ICON_2X :
58           IDR_DEVTOOLS_TOUCH_CURSOR_ICON);
59   InitCursorFromResource(&pinch_cursor_,
60       cursor_scale_factor,
61       use_2x ? IDR_DEVTOOLS_PINCH_CURSOR_ICON_2X :
62           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_.SetMultiTouchZoomSupportEnabled(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(
111     WebCursor* cursor, float scale, int resource_id) {
112   gfx::Image& cursor_image =
113       content::GetContentClient()->GetNativeImageNamed(resource_id);
114   WebCursor::CursorInfo cursor_info;
115   cursor_info.type = blink::WebCursorInfo::TypeCustom;
116   cursor_info.image_scale_factor = scale;
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   // Send mouse wheel for easy scrolling when there is no active touch.
167   return touch_active_;
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);
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   WebTouchEventTraits::ResetTypeAndTouchStates(
266       WebInputEvent::TouchCancel,
267       (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(),
268       &touch_event_);
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   WebInputEvent::Type eventType;
344   switch (mouse_event.type) {
345     case WebInputEvent::MouseDown:
346       eventType = WebInputEvent::TouchStart;
347       touch_active_ = true;
348       break;
349     case WebInputEvent::MouseMove:
350       eventType = WebInputEvent::TouchMove;
351       break;
352     case WebInputEvent::MouseUp:
353       eventType = WebInputEvent::TouchEnd;
354       touch_active_ = false;
355       break;
356     default:
357       eventType = WebInputEvent::Undefined;
358       NOTREACHED();
359   }
360   touch_event_.touchesLength = 1;
361   touch_event_.modifiers = mouse_event.modifiers;
362   WebTouchEventTraits::ResetTypeAndTouchStates(
363       eventType, mouse_event.timeStampSeconds, &touch_event_);
364
365   WebTouchPoint& point = touch_event_.touches[0];
366   point.id = 0;
367   point.radiusX = point.radiusY = 1.f;
368   point.force = 1.f;
369   point.rotationAngle = 0.f;
370   point.position.x = mouse_event.x;
371   point.screenPosition.x = mouse_event.globalX;
372   point.position.y = mouse_event.y;
373   point.screenPosition.y = mouse_event.globalY;
374
375   return true;
376 }
377
378 bool TouchEmulator::InPinchGestureMode() const {
379   return shift_pressed_ && allow_pinch_;
380 }
381
382 }  // namespace content