1 // Copyright 2013 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.
5 #include "content/common/input/web_input_event_traits.h"
10 #include "base/logging.h"
11 #include "base/strings/stringprintf.h"
13 using base::StringAppendF
;
14 using base::SStringPrintf
;
15 using blink::WebGestureEvent
;
16 using blink::WebInputEvent
;
17 using blink::WebKeyboardEvent
;
18 using blink::WebMouseEvent
;
19 using blink::WebMouseWheelEvent
;
20 using blink::WebTouchEvent
;
21 using blink::WebTouchPoint
;
22 using std::numeric_limits
;
27 const int kInvalidTouchIndex
= -1;
29 void ApppendEventDetails(const WebKeyboardEvent
& event
, std::string
* result
) {
31 "{\n WinCode: %d\n NativeCode: %d\n IsSystem: %d\n"
32 " Text: %s\n UnmodifiedText: %s\n KeyIdentifier: %s\n}",
36 reinterpret_cast<const char*>(event
.text
),
37 reinterpret_cast<const char*>(event
.unmodifiedText
),
38 reinterpret_cast<const char*>(event
.keyIdentifier
));
41 void ApppendEventDetails(const WebMouseEvent
& event
, std::string
* result
) {
43 "{\n Button: %d\n Pos: (%d, %d)\n GlobalPos: (%d, %d)\n"
44 " Movement: (%d, %d)\n Clicks: %d\n}",
55 void ApppendEventDetails(const WebMouseWheelEvent
& event
, std::string
* result
) {
57 "{\n Delta: (%f, %f)\n WheelTicks: (%f, %f)\n Accel: (%f, %f)\n"
58 " ScrollByPage: %d\n HasPreciseScrollingDeltas: %d\n"
59 " Phase: (%d, %d)\n CanRubberband: (%d, %d)\n CanScroll: %d\n}",
64 event
.accelerationRatioX
,
65 event
.accelerationRatioY
,
67 event
.hasPreciseScrollingDeltas
,
70 event
.canRubberbandLeft
,
71 event
.canRubberbandRight
,
75 void ApppendEventDetails(const WebGestureEvent
& event
, std::string
* result
) {
77 "{\n Pos: (%d, %d)\n GlobalPos: (%d, %d)\n SourceDevice: %d\n"
78 " RawData: (%f, %f, %f, %f, %d)\n}",
84 event
.data
.scrollUpdate
.deltaX
,
85 event
.data
.scrollUpdate
.deltaY
,
86 event
.data
.scrollUpdate
.velocityX
,
87 event
.data
.scrollUpdate
.velocityY
,
88 event
.data
.scrollUpdate
.previousUpdateInSequencePrevented
);
91 void ApppendTouchPointDetails(const WebTouchPoint
& point
, std::string
* result
) {
93 " (ID: %d, State: %d, ScreenPos: (%f, %f), Pos: (%f, %f),"
94 " Radius: (%f, %f), Rot: %f, Force: %f),\n",
97 point
.screenPosition
.x
,
98 point
.screenPosition
.y
,
107 void ApppendEventDetails(const WebTouchEvent
& event
, std::string
* result
) {
108 StringAppendF(result
,
109 "{\n Touches: %u, Cancelable: %d, CausesScrolling: %d,"
110 " uniqueTouchEventId: %u\n[\n",
111 event
.touchesLength
, event
.cancelable
,
112 event
.causesScrollingIfUncanceled
, event
.uniqueTouchEventId
);
113 for (unsigned i
= 0; i
< event
.touchesLength
; ++i
)
114 ApppendTouchPointDetails(event
.touches
[i
], result
);
115 result
->append(" ]\n}");
118 bool CanCoalesce(const WebKeyboardEvent
& event_to_coalesce
,
119 const WebKeyboardEvent
& event
) {
123 void Coalesce(const WebKeyboardEvent
& event_to_coalesce
,
124 WebKeyboardEvent
* event
) {
125 DCHECK(CanCoalesce(event_to_coalesce
, *event
));
128 bool CanCoalesce(const WebMouseEvent
& event_to_coalesce
,
129 const WebMouseEvent
& event
) {
130 return event
.type
== event_to_coalesce
.type
&&
131 event
.type
== WebInputEvent::MouseMove
;
134 void Coalesce(const WebMouseEvent
& event_to_coalesce
, WebMouseEvent
* event
) {
135 DCHECK(CanCoalesce(event_to_coalesce
, *event
));
136 // Accumulate movement deltas.
137 int x
= event
->movementX
;
138 int y
= event
->movementY
;
139 *event
= event_to_coalesce
;
140 event
->movementX
+= x
;
141 event
->movementY
+= y
;
144 bool CanCoalesce(const WebMouseWheelEvent
& event_to_coalesce
,
145 const WebMouseWheelEvent
& event
) {
146 return event
.modifiers
== event_to_coalesce
.modifiers
&&
147 event
.scrollByPage
== event_to_coalesce
.scrollByPage
&&
148 event
.phase
== event_to_coalesce
.phase
&&
149 event
.momentumPhase
== event_to_coalesce
.momentumPhase
&&
150 event
.hasPreciseScrollingDeltas
==
151 event_to_coalesce
.hasPreciseScrollingDeltas
&&
152 event
.canScroll
== event_to_coalesce
.canScroll
;
155 float GetUnacceleratedDelta(float accelerated_delta
, float acceleration_ratio
) {
156 return accelerated_delta
* acceleration_ratio
;
159 float GetAccelerationRatio(float accelerated_delta
, float unaccelerated_delta
) {
160 if (unaccelerated_delta
== 0.f
|| accelerated_delta
== 0.f
)
162 return unaccelerated_delta
/ accelerated_delta
;
165 void Coalesce(const WebMouseWheelEvent
& event_to_coalesce
,
166 WebMouseWheelEvent
* event
) {
167 DCHECK(CanCoalesce(event_to_coalesce
, *event
));
168 float unaccelerated_x
=
169 GetUnacceleratedDelta(event
->deltaX
,
170 event
->accelerationRatioX
) +
171 GetUnacceleratedDelta(event_to_coalesce
.deltaX
,
172 event_to_coalesce
.accelerationRatioX
);
173 float unaccelerated_y
=
174 GetUnacceleratedDelta(event
->deltaY
,
175 event
->accelerationRatioY
) +
176 GetUnacceleratedDelta(event_to_coalesce
.deltaY
,
177 event_to_coalesce
.accelerationRatioY
);
178 event
->deltaX
+= event_to_coalesce
.deltaX
;
179 event
->deltaY
+= event_to_coalesce
.deltaY
;
180 event
->wheelTicksX
+= event_to_coalesce
.wheelTicksX
;
181 event
->wheelTicksY
+= event_to_coalesce
.wheelTicksY
;
182 event
->accelerationRatioX
=
183 GetAccelerationRatio(event
->deltaX
, unaccelerated_x
);
184 event
->accelerationRatioY
=
185 GetAccelerationRatio(event
->deltaY
, unaccelerated_y
);
186 DCHECK_GE(event_to_coalesce
.timeStampSeconds
, event
->timeStampSeconds
);
187 event
->timeStampSeconds
= event_to_coalesce
.timeStampSeconds
;
190 // Returns |kInvalidTouchIndex| iff |event| lacks a touch with an ID of |id|.
191 int GetIndexOfTouchID(const WebTouchEvent
& event
, int id
) {
192 for (unsigned i
= 0; i
< event
.touchesLength
; ++i
) {
193 if (event
.touches
[i
].id
== id
)
196 return kInvalidTouchIndex
;
199 bool CanCoalesce(const WebTouchEvent
& event_to_coalesce
,
200 const WebTouchEvent
& event
) {
201 if (event
.type
!= event_to_coalesce
.type
||
202 event
.type
!= WebInputEvent::TouchMove
||
203 event
.modifiers
!= event_to_coalesce
.modifiers
||
204 event
.touchesLength
!= event_to_coalesce
.touchesLength
||
205 event
.touchesLength
> WebTouchEvent::touchesLengthCap
)
208 static_assert(WebTouchEvent::touchesLengthCap
<= sizeof(int32_t) * 8U,
209 "suboptimal touchesLengthCap size");
210 // Ensure that we have a 1-to-1 mapping of pointer ids between touches.
211 std::bitset
<WebTouchEvent::touchesLengthCap
> unmatched_event_touches(
212 (1 << event
.touchesLength
) - 1);
213 for (unsigned i
= 0; i
< event_to_coalesce
.touchesLength
; ++i
) {
214 int event_touch_index
=
215 GetIndexOfTouchID(event
, event_to_coalesce
.touches
[i
].id
);
216 if (event_touch_index
== kInvalidTouchIndex
)
218 if (!unmatched_event_touches
[event_touch_index
])
220 unmatched_event_touches
[event_touch_index
] = false;
222 return unmatched_event_touches
.none();
225 void Coalesce(const WebTouchEvent
& event_to_coalesce
, WebTouchEvent
* event
) {
226 DCHECK(CanCoalesce(event_to_coalesce
, *event
));
227 // The WebTouchPoints include absolute position information. So it is
228 // sufficient to simply replace the previous event with the new event->
229 // However, it is necessary to make sure that all the points have the
230 // correct state, i.e. the touch-points that moved in the last event, but
231 // didn't change in the current event, will have Stationary state. It is
232 // necessary to change them back to Moved state.
233 WebTouchEvent old_event
= *event
;
234 *event
= event_to_coalesce
;
235 for (unsigned i
= 0; i
< event
->touchesLength
; ++i
) {
236 int i_old
= GetIndexOfTouchID(old_event
, event
->touches
[i
].id
);
237 if (old_event
.touches
[i_old
].state
== blink::WebTouchPoint::StateMoved
)
238 event
->touches
[i
].state
= blink::WebTouchPoint::StateMoved
;
240 event
->causesScrollingIfUncanceled
|= old_event
.causesScrollingIfUncanceled
;
243 bool CanCoalesce(const WebGestureEvent
& event_to_coalesce
,
244 const WebGestureEvent
& event
) {
245 if (event
.type
!= event_to_coalesce
.type
||
246 event
.sourceDevice
!= event_to_coalesce
.sourceDevice
||
247 event
.modifiers
!= event_to_coalesce
.modifiers
)
250 if (event
.type
== WebInputEvent::GestureScrollUpdate
)
253 // GesturePinchUpdate scales can be combined only if they share a focal point,
254 // e.g., with double-tap drag zoom.
255 if (event
.type
== WebInputEvent::GesturePinchUpdate
&&
256 event
.x
== event_to_coalesce
.x
&&
257 event
.y
== event_to_coalesce
.y
)
263 void Coalesce(const WebGestureEvent
& event_to_coalesce
,
264 WebGestureEvent
* event
) {
265 DCHECK(CanCoalesce(event_to_coalesce
, *event
));
266 if (event
->type
== WebInputEvent::GestureScrollUpdate
) {
267 event
->data
.scrollUpdate
.deltaX
+=
268 event_to_coalesce
.data
.scrollUpdate
.deltaX
;
269 event
->data
.scrollUpdate
.deltaY
+=
270 event_to_coalesce
.data
.scrollUpdate
.deltaY
;
272 event
->data
.scrollUpdate
.previousUpdateInSequencePrevented
,
273 event_to_coalesce
.data
.scrollUpdate
.previousUpdateInSequencePrevented
);
274 } else if (event
->type
== WebInputEvent::GesturePinchUpdate
) {
275 event
->data
.pinchUpdate
.scale
*= event_to_coalesce
.data
.pinchUpdate
.scale
;
276 // Ensure the scale remains bounded above 0 and below Infinity so that
277 // we can reliably perform operations like log on the values.
278 if (event
->data
.pinchUpdate
.scale
< numeric_limits
<float>::min())
279 event
->data
.pinchUpdate
.scale
= numeric_limits
<float>::min();
280 else if (event
->data
.pinchUpdate
.scale
> numeric_limits
<float>::max())
281 event
->data
.pinchUpdate
.scale
= numeric_limits
<float>::max();
285 struct WebInputEventToString
{
286 template <class EventType
>
287 bool Execute(const WebInputEvent
& event
, std::string
* result
) const {
288 SStringPrintf(result
, "%s (Time: %lf, Modifiers: %d)\n",
289 WebInputEventTraits::GetName(event
.type
),
290 event
.timeStampSeconds
,
292 const EventType
& typed_event
= static_cast<const EventType
&>(event
);
293 ApppendEventDetails(typed_event
, result
);
298 struct WebInputEventSize
{
299 template <class EventType
>
300 bool Execute(WebInputEvent::Type
/* type */, size_t* type_size
) const {
301 *type_size
= sizeof(EventType
);
306 struct WebInputEventClone
{
307 template <class EventType
>
308 bool Execute(const WebInputEvent
& event
,
309 ScopedWebInputEvent
* scoped_event
) const {
310 DCHECK_EQ(sizeof(EventType
), event
.size
);
311 *scoped_event
= ScopedWebInputEvent(
312 new EventType(static_cast<const EventType
&>(event
)));
317 struct WebInputEventDelete
{
318 template <class EventType
>
319 bool Execute(WebInputEvent
* event
, bool* /* dummy_var */) const {
322 DCHECK_EQ(sizeof(EventType
), event
->size
);
323 delete static_cast<EventType
*>(event
);
328 struct WebInputEventCanCoalesce
{
329 template <class EventType
>
330 bool Execute(const WebInputEvent
& event_to_coalesce
,
331 const WebInputEvent
* event
) const {
332 if (event_to_coalesce
.type
!= event
->type
)
334 DCHECK_EQ(sizeof(EventType
), event
->size
);
335 DCHECK_EQ(sizeof(EventType
), event_to_coalesce
.size
);
336 return CanCoalesce(static_cast<const EventType
&>(event_to_coalesce
),
337 *static_cast<const EventType
*>(event
));
341 struct WebInputEventCoalesce
{
342 template <class EventType
>
343 bool Execute(const WebInputEvent
& event_to_coalesce
,
344 WebInputEvent
* event
) const {
345 Coalesce(static_cast<const EventType
&>(event_to_coalesce
),
346 static_cast<EventType
*>(event
));
351 template <typename Operator
, typename ArgIn
, typename ArgOut
>
352 bool Apply(Operator op
,
353 WebInputEvent::Type type
,
356 if (WebInputEvent::isMouseEventType(type
))
357 return op
.template Execute
<WebMouseEvent
>(arg_in
, arg_out
);
358 else if (type
== WebInputEvent::MouseWheel
)
359 return op
.template Execute
<WebMouseWheelEvent
>(arg_in
, arg_out
);
360 else if (WebInputEvent::isKeyboardEventType(type
))
361 return op
.template Execute
<WebKeyboardEvent
>(arg_in
, arg_out
);
362 else if (WebInputEvent::isTouchEventType(type
))
363 return op
.template Execute
<WebTouchEvent
>(arg_in
, arg_out
);
364 else if (WebInputEvent::isGestureEventType(type
))
365 return op
.template Execute
<WebGestureEvent
>(arg_in
, arg_out
);
367 NOTREACHED() << "Unknown webkit event type " << type
;
373 const char* WebInputEventTraits::GetName(WebInputEvent::Type type
) {
374 #define CASE_TYPE(t) case WebInputEvent::t: return #t
376 CASE_TYPE(Undefined
);
377 CASE_TYPE(MouseDown
);
379 CASE_TYPE(MouseMove
);
380 CASE_TYPE(MouseEnter
);
381 CASE_TYPE(MouseLeave
);
382 CASE_TYPE(ContextMenu
);
383 CASE_TYPE(MouseWheel
);
384 CASE_TYPE(RawKeyDown
);
388 CASE_TYPE(GestureScrollBegin
);
389 CASE_TYPE(GestureScrollEnd
);
390 CASE_TYPE(GestureScrollUpdate
);
391 CASE_TYPE(GestureFlingStart
);
392 CASE_TYPE(GestureFlingCancel
);
393 CASE_TYPE(GestureShowPress
);
394 CASE_TYPE(GestureTap
);
395 CASE_TYPE(GestureTapUnconfirmed
);
396 CASE_TYPE(GestureTapDown
);
397 CASE_TYPE(GestureTapCancel
);
398 CASE_TYPE(GestureDoubleTap
);
399 CASE_TYPE(GestureTwoFingerTap
);
400 CASE_TYPE(GestureLongPress
);
401 CASE_TYPE(GestureLongTap
);
402 CASE_TYPE(GesturePinchBegin
);
403 CASE_TYPE(GesturePinchEnd
);
404 CASE_TYPE(GesturePinchUpdate
);
405 CASE_TYPE(TouchStart
);
406 CASE_TYPE(TouchMove
);
408 CASE_TYPE(TouchCancel
);
410 // Must include default to let blink::WebInputEvent add new event types
411 // before they're added here.
413 "Unhandled WebInputEvent type in WebInputEventTraits::GetName.\n";
420 std::string
WebInputEventTraits::ToString(const WebInputEvent
& event
) {
422 Apply(WebInputEventToString(), event
.type
, event
, &result
);
426 size_t WebInputEventTraits::GetSize(WebInputEvent::Type type
) {
428 Apply(WebInputEventSize(), type
, type
, &size
);
432 ScopedWebInputEvent
WebInputEventTraits::Clone(const WebInputEvent
& event
) {
433 ScopedWebInputEvent scoped_event
;
434 Apply(WebInputEventClone(), event
.type
, event
, &scoped_event
);
435 return scoped_event
.Pass();
438 void WebInputEventTraits::Delete(WebInputEvent
* event
) {
441 bool dummy_var
= false;
442 Apply(WebInputEventDelete(), event
->type
, event
, &dummy_var
);
445 bool WebInputEventTraits::CanCoalesce(const WebInputEvent
& event_to_coalesce
,
446 const WebInputEvent
& event
) {
447 // Early out before casting.
448 if (event_to_coalesce
.type
!= event
.type
)
450 return Apply(WebInputEventCanCoalesce(),
456 void WebInputEventTraits::Coalesce(const WebInputEvent
& event_to_coalesce
,
457 WebInputEvent
* event
) {
459 Apply(WebInputEventCoalesce(), event
->type
, event_to_coalesce
, event
);
462 bool WebInputEventTraits::WillReceiveAckFromRenderer(
463 const WebInputEvent
& event
) {
464 switch (event
.type
) {
465 case WebInputEvent::MouseDown
:
466 case WebInputEvent::MouseUp
:
467 case WebInputEvent::MouseEnter
:
468 case WebInputEvent::MouseLeave
:
469 case WebInputEvent::ContextMenu
:
470 case WebInputEvent::GestureScrollBegin
:
471 case WebInputEvent::GestureScrollEnd
:
472 case WebInputEvent::GestureShowPress
:
473 case WebInputEvent::GestureTapUnconfirmed
:
474 case WebInputEvent::GestureTapDown
:
475 case WebInputEvent::GestureTapCancel
:
476 case WebInputEvent::GesturePinchBegin
:
477 case WebInputEvent::GesturePinchEnd
:
478 case WebInputEvent::TouchCancel
:
480 case WebInputEvent::TouchStart
:
481 case WebInputEvent::TouchEnd
:
482 return static_cast<const WebTouchEvent
&>(event
).cancelable
;
488 uint32
WebInputEventTraits::GetUniqueTouchEventId(const WebInputEvent
& event
) {
489 if (WebInputEvent::isTouchEventType(event
.type
)) {
490 return static_cast<const WebTouchEvent
&>(event
).uniqueTouchEventId
;
495 } // namespace content