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 "ui/events/gestures/motion_event_aura.h"
7 #include "base/logging.h"
8 #include "ui/events/gesture_detection/gesture_configuration.h"
13 PointerProperties
GetPointerPropertiesFromTouchEvent(const TouchEvent
& touch
) {
14 PointerProperties pointer_properties
;
15 pointer_properties
.x
= touch
.x();
16 pointer_properties
.y
= touch
.y();
17 pointer_properties
.raw_x
= touch
.root_location_f().x();
18 pointer_properties
.raw_y
= touch
.root_location_f().y();
19 pointer_properties
.id
= touch
.touch_id();
20 pointer_properties
.pressure
= touch
.force();
21 pointer_properties
.source_device_id
= touch
.source_device_id();
23 pointer_properties
.SetAxesAndOrientation(touch
.radius_x(), touch
.radius_y(),
24 touch
.rotation_angle());
25 if (!pointer_properties
.touch_major
) {
26 pointer_properties
.touch_major
=
27 2.f
* GestureConfiguration::GetInstance()->default_radius();
28 pointer_properties
.touch_minor
=
29 2.f
* GestureConfiguration::GetInstance()->default_radius();
30 pointer_properties
.orientation
= 0;
33 // TODO(jdduke): Plumb tool type from the platform, crbug.com/404128.
34 pointer_properties
.tool_type
= MotionEvent::TOOL_TYPE_UNKNOWN
;
36 return pointer_properties
;
41 MotionEventAura::MotionEventAura() {
45 MotionEventAura::~MotionEventAura() {
48 bool MotionEventAura::OnTouch(const TouchEvent
& touch
) {
49 int index
= FindPointerIndexOfId(touch
.touch_id());
50 bool pointer_id_is_active
= index
!= -1;
52 if (touch
.type() == ET_TOUCH_PRESSED
&& pointer_id_is_active
) {
53 // TODO(tdresser): This should return false (or NOTREACHED()), and
54 // ignore the touch; however, there is at least one case where we
55 // need to allow a touch press from a currently used touch id. See
56 // crbug.com/446852 for details.
58 // Cancel the existing touch, before handling the touch press.
59 TouchEvent
cancel(ET_TOUCH_CANCELLED
, touch
.location(), touch
.touch_id(),
62 CleanupRemovedTouchPoints(cancel
);
63 DCHECK_EQ(-1, FindPointerIndexOfId(touch
.touch_id()));
64 } else if (touch
.type() != ET_TOUCH_PRESSED
&& !pointer_id_is_active
) {
65 // We could have an active touch stream transfered to us, resulting in touch
66 // move or touch up events without associated touch down events. Ignore
71 if (touch
.type() == ET_TOUCH_MOVED
&& touch
.x() == GetX(index
) &&
72 touch
.y() == GetY(index
)) {
76 switch (touch
.type()) {
77 case ET_TOUCH_PRESSED
:
80 case ET_TOUCH_RELEASED
:
81 case ET_TOUCH_CANCELLED
:
82 // Removing these touch points needs to be postponed until after the
83 // MotionEvent has been dispatched. This cleanup occurs in
84 // CleanupRemovedTouchPoints.
95 UpdateCachedAction(touch
);
96 set_unique_event_id(touch
.unique_event_id());
97 set_flags(touch
.flags());
98 set_event_time(touch
.time_stamp() + base::TimeTicks());
102 void MotionEventAura::CleanupRemovedTouchPoints(const TouchEvent
& event
) {
103 if (event
.type() != ET_TOUCH_RELEASED
&&
104 event
.type() != ET_TOUCH_CANCELLED
) {
108 DCHECK(GetPointerCount());
109 int index_to_delete
= GetIndexFromId(event
.touch_id());
111 pointer(index_to_delete
) = pointer(GetPointerCount() - 1);
115 int MotionEventAura::GetSourceDeviceId(size_t pointer_index
) const {
116 DCHECK_LT(pointer_index
, GetPointerCount());
117 return pointer(pointer_index
).source_device_id
;
120 void MotionEventAura::AddTouch(const TouchEvent
& touch
) {
121 if (GetPointerCount() == MotionEvent::MAX_TOUCH_POINT_COUNT
)
124 PushPointer(GetPointerPropertiesFromTouchEvent(touch
));
127 void MotionEventAura::UpdateTouch(const TouchEvent
& touch
) {
128 pointer(GetIndexFromId(touch
.touch_id())) =
129 GetPointerPropertiesFromTouchEvent(touch
);
132 void MotionEventAura::UpdateCachedAction(const TouchEvent
& touch
) {
133 DCHECK(GetPointerCount());
134 switch (touch
.type()) {
135 case ET_TOUCH_PRESSED
:
136 if (GetPointerCount() == 1) {
137 set_action(ACTION_DOWN
);
139 set_action(ACTION_POINTER_DOWN
);
140 set_action_index(GetIndexFromId(touch
.touch_id()));
143 case ET_TOUCH_RELEASED
:
144 if (GetPointerCount() == 1) {
145 set_action(ACTION_UP
);
147 set_action(ACTION_POINTER_UP
);
148 set_action_index(GetIndexFromId(touch
.touch_id()));
151 case ET_TOUCH_CANCELLED
:
152 set_action(ACTION_CANCEL
);
155 set_action(ACTION_MOVE
);
163 int MotionEventAura::GetIndexFromId(int id
) const {
164 int index
= FindPointerIndexOfId(id
);
166 DCHECK_LT(index
, static_cast<int>(GetPointerCount()));