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
.pointer_details().force();
21 pointer_properties
.source_device_id
= touch
.source_device_id();
23 pointer_properties
.SetAxesAndOrientation(touch
.pointer_details().radius_x(),
24 touch
.pointer_details().radius_y(),
25 touch
.rotation_angle());
26 if (!pointer_properties
.touch_major
) {
27 pointer_properties
.touch_major
=
28 2.f
* GestureConfiguration::GetInstance()->default_radius();
29 pointer_properties
.touch_minor
=
30 2.f
* GestureConfiguration::GetInstance()->default_radius();
31 pointer_properties
.orientation
= 0;
34 // TODO(jdduke): Plumb tool type from the platform, crbug.com/404128.
35 pointer_properties
.tool_type
= MotionEvent::TOOL_TYPE_UNKNOWN
;
37 return pointer_properties
;
42 MotionEventAura::MotionEventAura() {}
44 MotionEventAura::~MotionEventAura() {}
46 bool MotionEventAura::OnTouch(const TouchEvent
& touch
) {
47 int index
= FindPointerIndexOfId(touch
.touch_id());
48 bool pointer_id_is_active
= index
!= -1;
50 if (touch
.type() == ET_TOUCH_PRESSED
&& pointer_id_is_active
) {
51 // TODO(tdresser): This should return false (or NOTREACHED()), and
52 // ignore the touch; however, there is at least one case where we
53 // need to allow a touch press from a currently used touch id. See
54 // crbug.com/446852 for details.
56 // Cancel the existing touch, before handling the touch press.
57 TouchEvent
cancel(ET_TOUCH_CANCELLED
, touch
.location(), touch
.touch_id(),
60 CleanupRemovedTouchPoints(cancel
);
61 DCHECK_EQ(-1, FindPointerIndexOfId(touch
.touch_id()));
62 } else if (touch
.type() != ET_TOUCH_PRESSED
&& !pointer_id_is_active
) {
63 // We could have an active touch stream transfered to us, resulting in touch
64 // move or touch up events without associated touch down events. Ignore
69 if (touch
.type() == ET_TOUCH_MOVED
&& touch
.x() == GetX(index
) &&
70 touch
.y() == GetY(index
)) {
74 switch (touch
.type()) {
75 case ET_TOUCH_PRESSED
:
78 case ET_TOUCH_RELEASED
:
79 case ET_TOUCH_CANCELLED
:
80 // Removing these touch points needs to be postponed until after the
81 // MotionEvent has been dispatched. This cleanup occurs in
82 // CleanupRemovedTouchPoints.
93 UpdateCachedAction(touch
);
94 set_unique_event_id(touch
.unique_event_id());
95 set_flags(touch
.flags());
96 set_event_time(touch
.time_stamp() + base::TimeTicks());
100 void MotionEventAura::CleanupRemovedTouchPoints(const TouchEvent
& event
) {
101 if (event
.type() != ET_TOUCH_RELEASED
&&
102 event
.type() != ET_TOUCH_CANCELLED
) {
106 DCHECK(GetPointerCount());
107 int index_to_delete
= GetIndexFromId(event
.touch_id());
108 set_action_index(-1);
109 set_action(MotionEvent::ACTION_NONE
);
110 pointer(index_to_delete
) = pointer(GetPointerCount() - 1);
114 int MotionEventAura::GetSourceDeviceId(size_t pointer_index
) const {
115 DCHECK_LT(pointer_index
, GetPointerCount());
116 return pointer(pointer_index
).source_device_id
;
119 bool MotionEventAura::AddTouch(const TouchEvent
& touch
) {
120 if (GetPointerCount() == MotionEvent::MAX_TOUCH_POINT_COUNT
)
123 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
);
165 // TODO(tdresser): remove these checks once crbug.com/525189 is fixed.
167 CHECK_LT(index
, static_cast<int>(GetPointerCount()));