Roll src/third_party/WebKit 88cc93e:f6ae725 (svn 200125:200141)
[chromium-blink-merge.git] / ui / events / gestures / motion_event_aura.cc
blob8c345f893eeb87c12f8b3214cc7ed5fc154e7050
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"
10 namespace ui {
11 namespace {
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;
39 } // namespace
41 MotionEventAura::MotionEventAura() {
42 set_action_index(-1);
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(),
60 touch.time_stamp());
61 OnTouch(cancel);
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
67 // them.
68 return false;
71 if (touch.type() == ET_TOUCH_MOVED && touch.x() == GetX(index) &&
72 touch.y() == GetY(index)) {
73 return false;
76 switch (touch.type()) {
77 case ET_TOUCH_PRESSED:
78 AddTouch(touch);
79 break;
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.
85 UpdateTouch(touch);
86 break;
87 case ET_TOUCH_MOVED:
88 UpdateTouch(touch);
89 break;
90 default:
91 NOTREACHED();
92 return false;
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());
99 return true;
102 void MotionEventAura::CleanupRemovedTouchPoints(const TouchEvent& event) {
103 if (event.type() != ET_TOUCH_RELEASED &&
104 event.type() != ET_TOUCH_CANCELLED) {
105 return;
108 DCHECK(GetPointerCount());
109 int index_to_delete = GetIndexFromId(event.touch_id());
110 set_action_index(0);
111 pointer(index_to_delete) = pointer(GetPointerCount() - 1);
112 PopPointer();
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)
122 return;
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);
138 } else {
139 set_action(ACTION_POINTER_DOWN);
140 set_action_index(GetIndexFromId(touch.touch_id()));
142 break;
143 case ET_TOUCH_RELEASED:
144 if (GetPointerCount() == 1) {
145 set_action(ACTION_UP);
146 } else {
147 set_action(ACTION_POINTER_UP);
148 set_action_index(GetIndexFromId(touch.touch_id()));
150 break;
151 case ET_TOUCH_CANCELLED:
152 set_action(ACTION_CANCEL);
153 break;
154 case ET_TOUCH_MOVED:
155 set_action(ACTION_MOVE);
156 break;
157 default:
158 NOTREACHED();
159 break;
163 int MotionEventAura::GetIndexFromId(int id) const {
164 int index = FindPointerIndexOfId(id);
165 DCHECK_GE(index, 0);
166 DCHECK_LT(index, static_cast<int>(GetPointerCount()));
167 return index;
170 } // namespace ui