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 #include "content/browser/renderer_host/input/motion_event_web.h"
7 #include "base/logging.h"
8 #include "content/common/input/web_touch_event_traits.h"
10 using blink::WebInputEvent;
11 using blink::WebTouchEvent;
12 using blink::WebTouchPoint;
17 ui::MotionEvent::Action GetActionFrom(const WebTouchEvent& event) {
18 DCHECK(event.touchesLength);
20 case WebInputEvent::TouchStart:
21 if (WebTouchEventTraits::AllTouchPointsHaveState(
22 event, WebTouchPoint::StatePressed))
23 return ui::MotionEvent::ACTION_DOWN;
25 return ui::MotionEvent::ACTION_POINTER_DOWN;
26 case WebInputEvent::TouchEnd:
27 if (WebTouchEventTraits::AllTouchPointsHaveState(
28 event, WebTouchPoint::StateReleased))
29 return ui::MotionEvent::ACTION_UP;
31 return ui::MotionEvent::ACTION_POINTER_UP;
32 case WebInputEvent::TouchCancel:
33 DCHECK(WebTouchEventTraits::AllTouchPointsHaveState(
34 event, WebTouchPoint::StateCancelled));
35 return ui::MotionEvent::ACTION_CANCEL;
36 case WebInputEvent::TouchMove:
37 return ui::MotionEvent::ACTION_MOVE;
42 << "Unable to derive a valid MotionEvent::Action from the WebTouchEvent.";
43 return ui::MotionEvent::ACTION_CANCEL;
46 int GetActionIndexFrom(const WebTouchEvent& event) {
47 for (size_t i = 0; i < event.touchesLength; ++i) {
48 if (event.touches[i].state != WebTouchPoint::StateUndefined &&
49 event.touches[i].state != WebTouchPoint::StateStationary)
57 MotionEventWeb::MotionEventWeb(const WebTouchEvent& event)
59 cached_action_(GetActionFrom(event)),
60 cached_action_index_(GetActionIndexFrom(event)) {
61 DCHECK_GT(GetPointerCount(), 0U);
64 MotionEventWeb::~MotionEventWeb() {}
66 int MotionEventWeb::GetId() const {
70 MotionEventWeb::Action MotionEventWeb::GetAction() const {
71 return cached_action_;
74 int MotionEventWeb::GetActionIndex() const { return cached_action_index_; }
76 size_t MotionEventWeb::GetPointerCount() const { return event_.touchesLength; }
78 int MotionEventWeb::GetPointerId(size_t pointer_index) const {
79 DCHECK_LT(pointer_index, GetPointerCount());
80 return event_.touches[pointer_index].id;
83 float MotionEventWeb::GetX(size_t pointer_index) const {
84 DCHECK_LT(pointer_index, GetPointerCount());
85 return event_.touches[pointer_index].position.x;
88 float MotionEventWeb::GetY(size_t pointer_index) const {
89 DCHECK_LT(pointer_index, GetPointerCount());
90 return event_.touches[pointer_index].position.y;
93 float MotionEventWeb::GetRawX(size_t pointer_index) const {
94 DCHECK_LT(pointer_index, GetPointerCount());
95 return event_.touches[pointer_index].screenPosition.x;
98 float MotionEventWeb::GetRawY(size_t pointer_index) const {
99 DCHECK_LT(pointer_index, GetPointerCount());
100 return event_.touches[pointer_index].screenPosition.y;
103 float MotionEventWeb::GetTouchMajor(size_t pointer_index) const {
104 DCHECK_LT(pointer_index, GetPointerCount());
105 // TODO(jdduke): We should be a bit more careful about axes here.
106 return 2.f * std::max(event_.touches[pointer_index].radiusX,
107 event_.touches[pointer_index].radiusY);
110 float MotionEventWeb::GetPressure(size_t pointer_index) const {
114 base::TimeTicks MotionEventWeb::GetEventTime() const {
115 return base::TimeTicks() +
116 base::TimeDelta::FromMicroseconds(event_.timeStampSeconds *
117 base::Time::kMicrosecondsPerSecond);
120 size_t MotionEventWeb::GetHistorySize() const { return 0; }
122 base::TimeTicks MotionEventWeb::GetHistoricalEventTime(
123 size_t historical_index) const {
125 return base::TimeTicks();
128 float MotionEventWeb::GetHistoricalTouchMajor(size_t pointer_index,
129 size_t historical_index) const {
134 float MotionEventWeb::GetHistoricalX(size_t pointer_index,
135 size_t historical_index) const {
140 float MotionEventWeb::GetHistoricalY(size_t pointer_index,
141 size_t historical_index) const {
146 ui::MotionEvent::ToolType MotionEventWeb::GetToolType(
147 size_t pointer_index) const {
149 return TOOL_TYPE_UNKNOWN;
152 int MotionEventWeb::GetButtonState() const {
157 scoped_ptr<ui::MotionEvent> MotionEventWeb::Clone() const {
158 return scoped_ptr<MotionEvent>(new MotionEventWeb(event_));
161 scoped_ptr<ui::MotionEvent> MotionEventWeb::Cancel() const {
162 WebTouchEvent cancel_event(event_);
163 WebTouchEventTraits::ResetTypeAndTouchStates(
164 blink::WebInputEvent::TouchCancel,
165 // TODO(rbyers): Shouldn't we use a fresh timestamp?
166 event_.timeStampSeconds,
168 return scoped_ptr<MotionEvent>(new MotionEventWeb(cancel_event));
171 } // namespace content