Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / ui / events / gestures / motion_event_aura.cc
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.
4
5 #include "ui/events/gestures/motion_event_aura.h"
6
7 #include "base/logging.h"
8 #include "ui/events/gestures/gesture_configuration.h"
9
10 namespace ui {
11
12 MotionEventAura::MotionEventAura()
13     : pointer_count_(0), cached_action_index_(-1) {
14 }
15
16 MotionEventAura::MotionEventAura(
17     size_t pointer_count,
18     const base::TimeTicks& last_touch_time,
19     Action cached_action,
20     int cached_action_index,
21     const PointData (&active_touches)[GestureSequence::kMaxGesturePoints])
22     : pointer_count_(pointer_count),
23       last_touch_time_(last_touch_time),
24       cached_action_(cached_action),
25       cached_action_index_(cached_action_index) {
26   DCHECK(pointer_count_);
27   for (size_t i = 0; i < pointer_count; ++i)
28     active_touches_[i] = active_touches[i];
29 }
30
31 MotionEventAura::~MotionEventAura() {}
32
33 MotionEventAura::PointData MotionEventAura::GetPointDataFromTouchEvent(
34     const TouchEvent& touch) {
35   PointData point_data;
36   point_data.x = touch.x();
37   point_data.y = touch.y();
38   point_data.touch_id = touch.touch_id();
39   point_data.pressure = touch.force();
40   point_data.source_device_id = touch.source_device_id();
41
42   // TODO(tdresser): at some point we should start using both radii if they are
43   // available, but for now we use the max.
44   point_data.major_radius = std::max(touch.radius_x(), touch.radius_y());
45   if (!point_data.major_radius)
46     point_data.major_radius = GestureConfiguration::default_radius();
47   return point_data;
48 }
49
50 void MotionEventAura::OnTouch(const TouchEvent& touch) {
51   switch (touch.type()) {
52     case ET_TOUCH_PRESSED:
53       AddTouch(touch);
54       break;
55     case ET_TOUCH_RELEASED:
56     case ET_TOUCH_CANCELLED:
57       // Removing these touch points needs to be postponed until after the
58       // MotionEvent has been dispatched. This cleanup occurs in
59       // CleanupRemovedTouchPoints.
60       UpdateTouch(touch);
61       break;
62     case ET_TOUCH_MOVED:
63       UpdateTouch(touch);
64       break;
65     default:
66       NOTREACHED();
67       break;
68   }
69
70   UpdateCachedAction(touch);
71   last_touch_time_ = touch.time_stamp() + base::TimeTicks();
72 }
73
74 int MotionEventAura::GetId() const {
75   return GetPointerId(0);
76 }
77
78 MotionEvent::Action MotionEventAura::GetAction() const {
79   return cached_action_;
80 }
81
82 int MotionEventAura::GetActionIndex() const {
83   DCHECK(cached_action_ == ACTION_POINTER_DOWN ||
84          cached_action_ == ACTION_POINTER_UP);
85   DCHECK_GE(cached_action_index_, 0);
86   DCHECK_LE(cached_action_index_, static_cast<int>(pointer_count_));
87   return cached_action_index_;
88 }
89
90 size_t MotionEventAura::GetPointerCount() const { return pointer_count_; }
91
92 int MotionEventAura::GetPointerId(size_t pointer_index) const {
93   DCHECK_LE(pointer_index, pointer_count_);
94   return active_touches_[pointer_index].touch_id;
95 }
96
97 float MotionEventAura::GetX(size_t pointer_index) const {
98   DCHECK_LE(pointer_index, pointer_count_);
99   return active_touches_[pointer_index].x;
100 }
101
102 float MotionEventAura::GetY(size_t pointer_index) const {
103   DCHECK_LE(pointer_index, pointer_count_);
104   return active_touches_[pointer_index].y;
105 }
106
107 float MotionEventAura::GetTouchMajor(size_t pointer_index) const {
108   DCHECK_LE(pointer_index, pointer_count_);
109   return active_touches_[pointer_index].major_radius * 2;
110 }
111
112 float MotionEventAura::GetPressure(size_t pointer_index) const {
113   DCHECK_LE(pointer_index, pointer_count_);
114   return active_touches_[pointer_index].pressure;
115 }
116
117 base::TimeTicks MotionEventAura::GetEventTime() const {
118   return last_touch_time_;
119 }
120
121 size_t MotionEventAura::GetHistorySize() const { return 0; }
122
123 base::TimeTicks MotionEventAura::GetHistoricalEventTime(
124     size_t historical_index) const {
125   NOTIMPLEMENTED();
126   return base::TimeTicks();
127 }
128
129 float MotionEventAura::GetHistoricalTouchMajor(size_t pointer_index,
130                                              size_t historical_index) const {
131   NOTIMPLEMENTED();
132   return 0;
133 }
134
135 float MotionEventAura::GetHistoricalX(size_t pointer_index,
136                                     size_t historical_index) const {
137   NOTIMPLEMENTED();
138   return 0;
139 }
140
141 float MotionEventAura::GetHistoricalY(size_t pointer_index,
142                                     size_t historical_index) const {
143   NOTIMPLEMENTED();
144   return 0;
145 }
146
147 scoped_ptr<MotionEvent> MotionEventAura::Clone() const {
148   return scoped_ptr<MotionEvent>(new MotionEventAura(pointer_count_,
149                                                      last_touch_time_,
150                                                      cached_action_,
151                                                      cached_action_index_,
152                                                      active_touches_));
153 }
154 scoped_ptr<MotionEvent> MotionEventAura::Cancel() const {
155   return scoped_ptr<MotionEvent>(new MotionEventAura(
156       pointer_count_, last_touch_time_, ACTION_CANCEL, -1, active_touches_));
157 }
158
159 void MotionEventAura::CleanupRemovedTouchPoints(const TouchEvent& event) {
160   if (event.type() != ET_TOUCH_RELEASED &&
161       event.type() != ET_TOUCH_CANCELLED) {
162     return;
163   }
164
165   int index_to_delete = static_cast<int>(GetIndexFromId(event.touch_id()));
166   pointer_count_--;
167   active_touches_[index_to_delete] = active_touches_[pointer_count_];
168 }
169
170 MotionEventAura::PointData::PointData()
171     : x(0),
172       y(0),
173       touch_id(0),
174       pressure(0),
175       source_device_id(0),
176       major_radius(0) {
177 }
178
179 int MotionEventAura::GetSourceDeviceId(size_t pointer_index) const {
180   DCHECK_LE(pointer_index, pointer_count_);
181   return active_touches_[pointer_index].source_device_id;
182 }
183
184 void MotionEventAura::AddTouch(const TouchEvent& touch) {
185   if (pointer_count_ == static_cast<size_t>(GestureSequence::kMaxGesturePoints))
186     return;
187
188   active_touches_[pointer_count_] = GetPointDataFromTouchEvent(touch);
189   pointer_count_++;
190 }
191
192
193 void MotionEventAura::UpdateTouch(const TouchEvent& touch) {
194   active_touches_[GetIndexFromId(touch.touch_id())] =
195       GetPointDataFromTouchEvent(touch);
196 }
197
198 void MotionEventAura::UpdateCachedAction(const TouchEvent& touch) {
199   DCHECK(pointer_count_);
200   switch (touch.type()) {
201     case ET_TOUCH_PRESSED:
202       if (pointer_count_ == 1) {
203         cached_action_ = ACTION_DOWN;
204       } else {
205         cached_action_ = ACTION_POINTER_DOWN;
206         cached_action_index_ =
207             static_cast<int>(GetIndexFromId(touch.touch_id()));
208       }
209       break;
210     case ET_TOUCH_RELEASED:
211       if (pointer_count_ == 1) {
212         cached_action_ = ACTION_UP;
213       } else {
214         cached_action_ = ACTION_POINTER_UP;
215         cached_action_index_ =
216             static_cast<int>(GetIndexFromId(touch.touch_id()));
217         DCHECK_LE(cached_action_index_, static_cast<int>(pointer_count_));
218       }
219       break;
220     case ET_TOUCH_CANCELLED:
221       cached_action_ = ACTION_CANCEL;
222       break;
223     case ET_TOUCH_MOVED:
224       cached_action_ = ACTION_MOVE;
225       break;
226     default:
227       NOTREACHED();
228       break;
229   }
230 }
231
232 size_t MotionEventAura::GetIndexFromId(int id) const {
233   for (size_t i = 0; i < pointer_count_; ++i) {
234     if (active_touches_[i].touch_id == id)
235       return i;
236   }
237   NOTREACHED();
238   return 0;
239 }
240
241 }  // namespace ui