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