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.
5 // MSVC++ requires this to be set before any other includes to get M_PI.
6 #define _USE_MATH_DEFINES
8 #include "content/browser/renderer_host/input/motion_event_web.h"
12 #include "base/logging.h"
13 #include "content/browser/renderer_host/input/web_input_event_util.h"
14 #include "content/common/input/web_touch_event_traits.h"
16 using blink::WebInputEvent
;
17 using blink::WebTouchEvent
;
18 using blink::WebTouchPoint
;
23 ui::MotionEvent::Action
GetActionFrom(const WebTouchEvent
& event
) {
24 DCHECK(event
.touchesLength
);
26 case WebInputEvent::TouchStart
:
27 if (WebTouchEventTraits::AllTouchPointsHaveState(
28 event
, WebTouchPoint::StatePressed
))
29 return ui::MotionEvent::ACTION_DOWN
;
31 return ui::MotionEvent::ACTION_POINTER_DOWN
;
32 case WebInputEvent::TouchEnd
:
33 if (WebTouchEventTraits::AllTouchPointsHaveState(
34 event
, WebTouchPoint::StateReleased
))
35 return ui::MotionEvent::ACTION_UP
;
37 return ui::MotionEvent::ACTION_POINTER_UP
;
38 case WebInputEvent::TouchCancel
:
39 DCHECK(WebTouchEventTraits::AllTouchPointsHaveState(
40 event
, WebTouchPoint::StateCancelled
));
41 return ui::MotionEvent::ACTION_CANCEL
;
42 case WebInputEvent::TouchMove
:
43 return ui::MotionEvent::ACTION_MOVE
;
48 << "Unable to derive a valid MotionEvent::Action from the WebTouchEvent.";
49 return ui::MotionEvent::ACTION_CANCEL
;
52 int GetActionIndexFrom(const WebTouchEvent
& event
) {
53 for (size_t i
= 0; i
< event
.touchesLength
; ++i
) {
54 if (event
.touches
[i
].state
!= WebTouchPoint::StateUndefined
&&
55 event
.touches
[i
].state
!= WebTouchPoint::StateStationary
)
63 MotionEventWeb::MotionEventWeb(const WebTouchEvent
& event
)
65 cached_action_(GetActionFrom(event
)),
66 cached_action_index_(GetActionIndexFrom(event
)),
67 unique_event_id_(event
.uniqueTouchEventId
) {
68 DCHECK_GT(GetPointerCount(), 0U);
71 MotionEventWeb::~MotionEventWeb() {}
73 uint32
MotionEventWeb::GetUniqueEventId() const {
74 return unique_event_id_
;
77 MotionEventWeb::Action
MotionEventWeb::GetAction() const {
78 return cached_action_
;
81 int MotionEventWeb::GetActionIndex() const {
82 DCHECK(cached_action_
== ACTION_POINTER_UP
||
83 cached_action_
== ACTION_POINTER_DOWN
)
84 << "Invalid action for GetActionIndex(): " << cached_action_
;
85 DCHECK_GE(cached_action_index_
, 0);
86 DCHECK_LT(cached_action_index_
, static_cast<int>(event_
.touchesLength
));
87 return cached_action_index_
;
90 size_t MotionEventWeb::GetPointerCount() const { return event_
.touchesLength
; }
92 int MotionEventWeb::GetPointerId(size_t pointer_index
) const {
93 DCHECK_LT(pointer_index
, GetPointerCount());
94 return event_
.touches
[pointer_index
].id
;
97 float MotionEventWeb::GetX(size_t pointer_index
) const {
98 DCHECK_LT(pointer_index
, GetPointerCount());
99 return event_
.touches
[pointer_index
].position
.x
;
102 float MotionEventWeb::GetY(size_t pointer_index
) const {
103 DCHECK_LT(pointer_index
, GetPointerCount());
104 return event_
.touches
[pointer_index
].position
.y
;
107 float MotionEventWeb::GetRawX(size_t pointer_index
) const {
108 DCHECK_LT(pointer_index
, GetPointerCount());
109 return event_
.touches
[pointer_index
].screenPosition
.x
;
112 float MotionEventWeb::GetRawY(size_t pointer_index
) const {
113 DCHECK_LT(pointer_index
, GetPointerCount());
114 return event_
.touches
[pointer_index
].screenPosition
.y
;
117 float MotionEventWeb::GetTouchMajor(size_t pointer_index
) const {
118 DCHECK_LT(pointer_index
, GetPointerCount());
119 return 2.f
* std::max(event_
.touches
[pointer_index
].radiusX
,
120 event_
.touches
[pointer_index
].radiusY
);
123 float MotionEventWeb::GetTouchMinor(size_t pointer_index
) const {
124 DCHECK_LT(pointer_index
, GetPointerCount());
125 return 2.f
* std::min(event_
.touches
[pointer_index
].radiusX
,
126 event_
.touches
[pointer_index
].radiusY
);
129 float MotionEventWeb::GetOrientation(size_t pointer_index
) const {
130 DCHECK_LT(pointer_index
, GetPointerCount());
132 float rotation_angle_rad
= event_
.touches
[pointer_index
].rotationAngle
134 DCHECK(0 <= rotation_angle_rad
&& rotation_angle_rad
<= M_PI_2
)
135 << "Unexpected touch rotation angle";
137 if (event_
.touches
[pointer_index
].radiusX
138 > event_
.touches
[pointer_index
].radiusY
) {
139 // The case radiusX == radiusY is omitted from here on purpose: for circles,
140 // we want to pass the angle (which could be any value in such cases but
141 // always seem to be set to zero) unchanged.
142 rotation_angle_rad
-= (float) M_PI_2
;
145 return rotation_angle_rad
;
148 float MotionEventWeb::GetPressure(size_t pointer_index
) const {
152 base::TimeTicks
MotionEventWeb::GetEventTime() const {
153 return base::TimeTicks() +
154 base::TimeDelta::FromMicroseconds(event_
.timeStampSeconds
*
155 base::Time::kMicrosecondsPerSecond
);
158 ui::MotionEvent::ToolType
MotionEventWeb::GetToolType(
159 size_t pointer_index
) const {
160 // TODO(jdduke): Plumb tool type from the platform event, crbug.com/404128.
161 DCHECK_LT(pointer_index
, GetPointerCount());
162 return TOOL_TYPE_UNKNOWN
;
165 int MotionEventWeb::GetButtonState() const {
169 int MotionEventWeb::GetFlags() const {
170 return WebEventModifiersToEventFlags(event_
.modifiers
);
173 } // namespace content