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