blob: bd5b67b1d03deac582fc41006aa68eb0e0fe9460 [file] [log] [blame]
Jeff Brown5912f952013-07-01 19:10:31 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Input"
18//#define LOG_NDEBUG 0
19
chaviw09c8d2d2020-08-24 15:48:26 -070020#include <attestation/HmacKeyManager.h>
Garfield Tan84b087e2020-01-23 10:49:05 -080021#include <cutils/compiler.h>
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050022#include <inttypes.h>
Garfield Tan84b087e2020-01-23 10:49:05 -080023#include <string.h>
Michael Wright635422b2022-12-02 00:43:56 +000024#include <optional>
Jeff Brown5912f952013-07-01 19:10:31 -070025
Siarhei Vishniakou31977182022-09-30 08:51:23 -070026#include <android-base/file.h>
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +000027#include <android-base/logging.h>
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050028#include <android-base/stringprintf.h>
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +000029#include <cutils/compiler.h>
chaviw98318de2021-05-19 16:45:23 -050030#include <gui/constants.h>
Prabir Pradhan092f3a92021-11-25 10:53:27 -080031#include <input/DisplayViewport.h>
Jeff Brown5912f952013-07-01 19:10:31 -070032#include <input/Input.h>
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080033#include <input/InputDevice.h>
Michael Wright872db4f2014-04-22 15:03:51 -070034#include <input/InputEventLabels.h>
Jeff Brown5912f952013-07-01 19:10:31 -070035
Brett Chabotfaa986c2020-11-04 17:39:36 -080036#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -070037#include <binder/Parcel.h>
Brett Chabotfaa986c2020-11-04 17:39:36 -080038#endif
Siarhei Vishniakou63740b92022-10-20 10:28:08 -070039#if defined(__ANDROID__)
40#include <sys/random.h>
41#endif
Jeff Brown5912f952013-07-01 19:10:31 -070042
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050043using android::base::StringPrintf;
44
Jeff Brown5912f952013-07-01 19:10:31 -070045namespace android {
46
Prabir Pradhan6b384612021-05-14 16:56:25 -070047namespace {
48
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070049bool shouldDisregardTransformation(uint32_t source) {
Prabir Pradhan258e2b92022-06-24 18:37:04 +000050 // Do not apply any transformations to axes from joysticks, touchpads, or relative mice.
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070051 return isFromSource(source, AINPUT_SOURCE_CLASS_JOYSTICK) ||
Prabir Pradhan258e2b92022-06-24 18:37:04 +000052 isFromSource(source, AINPUT_SOURCE_CLASS_POSITION) ||
53 isFromSource(source, AINPUT_SOURCE_MOUSE_RELATIVE);
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070054}
55
56bool shouldDisregardOffset(uint32_t source) {
Prabir Pradhan9f388812021-05-13 16:54:53 -070057 // Pointer events are the only type of events that refer to absolute coordinates on the display,
58 // so we should apply the entire window transform. For other types of events, we should make
59 // sure to not apply the window translation/offset.
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070060 return !isFromSource(source, AINPUT_SOURCE_CLASS_POINTER);
Prabir Pradhan9f388812021-05-13 16:54:53 -070061}
62
Prabir Pradhan6b384612021-05-14 16:56:25 -070063} // namespace
64
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -080065const char* motionClassificationToString(MotionClassification classification) {
66 switch (classification) {
67 case MotionClassification::NONE:
68 return "NONE";
69 case MotionClassification::AMBIGUOUS_GESTURE:
70 return "AMBIGUOUS_GESTURE";
71 case MotionClassification::DEEP_PRESS:
72 return "DEEP_PRESS";
Harry Cutts2800fb02022-09-15 13:49:23 +000073 case MotionClassification::TWO_FINGER_SWIPE:
74 return "TWO_FINGER_SWIPE";
Harry Cuttsc5748d12022-12-02 17:30:18 +000075 case MotionClassification::MULTI_FINGER_SWIPE:
76 return "MULTI_FINGER_SWIPE";
Harry Cuttsb1e83552022-12-20 11:02:26 +000077 case MotionClassification::PINCH:
78 return "PINCH";
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -080079 }
80}
81
Garfield Tan84b087e2020-01-23 10:49:05 -080082// --- IdGenerator ---
Siarhei Vishniakou63740b92022-10-20 10:28:08 -070083#if defined(__ANDROID__)
84[[maybe_unused]]
85#endif
86static status_t
87getRandomBytes(uint8_t* data, size_t size) {
Siarhei Vishniakou31977182022-09-30 08:51:23 -070088 int ret = TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY | O_CLOEXEC | O_NOFOLLOW));
89 if (ret == -1) {
90 return -errno;
91 }
92
93 base::unique_fd fd(ret);
94 if (!base::ReadFully(fd, data, size)) {
95 return -errno;
96 }
97 return OK;
98}
99
Garfield Tan84b087e2020-01-23 10:49:05 -0800100IdGenerator::IdGenerator(Source source) : mSource(source) {}
101
102int32_t IdGenerator::nextId() const {
103 constexpr uint32_t SEQUENCE_NUMBER_MASK = ~SOURCE_MASK;
104 int32_t id = 0;
105
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700106#if defined(__ANDROID__)
107 // On device, prefer 'getrandom' to '/dev/urandom' because it's faster.
108 constexpr size_t BUF_LEN = sizeof(id);
109 size_t totalBytes = 0;
110 while (totalBytes < BUF_LEN) {
111 ssize_t bytes = TEMP_FAILURE_RETRY(getrandom(&id, BUF_LEN, GRND_NONBLOCK));
112 if (CC_UNLIKELY(bytes < 0)) {
113 ALOGW("Failed to fill in random number for sequence number: %s.", strerror(errno));
114 id = 0;
115 break;
116 }
117 totalBytes += bytes;
118 }
119#else
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700120#if defined(__linux__)
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700121 // On host, <sys/random.h> / GRND_NONBLOCK is not available
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700122 while (true) {
123 status_t result = getRandomBytes(reinterpret_cast<uint8_t*>(&id), sizeof(id));
124 if (result == OK) {
Garfield Tan84b087e2020-01-23 10:49:05 -0800125 break;
126 }
Garfield Tan84b087e2020-01-23 10:49:05 -0800127 }
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700128#endif // __linux__
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700129#endif // __ANDROID__
Garfield Tan84b087e2020-01-23 10:49:05 -0800130 return (id & SEQUENCE_NUMBER_MASK) | static_cast<int32_t>(mSource);
131}
132
Jeff Brown5912f952013-07-01 19:10:31 -0700133// --- InputEvent ---
134
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000135// Due to precision limitations when working with floating points, transforming - namely
136// scaling - floating points can lead to minute errors. We round transformed values to approximately
137// three decimal places so that values like 0.99997 show up as 1.0.
138inline float roundTransformedCoords(float val) {
139 // Use a power to two to approximate three decimal places to potentially reduce some cycles.
140 // This should be at least as precise as MotionEvent::ROUNDING_PRECISION.
141 return std::round(val * 1024.f) / 1024.f;
142}
143
144inline vec2 roundTransformedCoords(vec2 p) {
145 return {roundTransformedCoords(p.x), roundTransformedCoords(p.y)};
146}
147
Prabir Pradhande69f8a2021-11-18 16:40:34 +0000148vec2 transformWithoutTranslation(const ui::Transform& transform, const vec2& xy) {
149 const vec2 transformedXy = transform.transform(xy);
150 const vec2 transformedOrigin = transform.transform(0, 0);
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000151 return roundTransformedCoords(transformedXy - transformedOrigin);
Prabir Pradhande69f8a2021-11-18 16:40:34 +0000152}
153
Prabir Pradhane2e10b42022-11-17 20:59:36 +0000154float transformAngle(const ui::Transform& transform, float angleRadians) {
155 // Construct and transform a vector oriented at the specified clockwise angle from vertical.
156 // Coordinate system: down is increasing Y, right is increasing X.
157 float x = sinf(angleRadians);
158 float y = -cosf(angleRadians);
159 vec2 transformedPoint = transform.transform(x, y);
160
161 // Determine how the origin is transformed by the matrix so that we
162 // can transform orientation vectors.
163 const vec2 origin = transform.transform(0, 0);
164
165 transformedPoint.x -= origin.x;
166 transformedPoint.y -= origin.y;
167
168 // Derive the transformed vector's clockwise angle from vertical.
169 // The return value of atan2f is in range [-pi, pi] which conforms to the orientation API.
170 return atan2f(transformedPoint.x, -transformedPoint.y);
171}
172
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800173std::string inputEventSourceToString(int32_t source) {
174 if (source == AINPUT_SOURCE_UNKNOWN) {
175 return "UNKNOWN";
176 }
177 if (source == static_cast<int32_t>(AINPUT_SOURCE_ANY)) {
178 return "ANY";
179 }
180 static const std::map<int32_t, const char*> SOURCES{
181 {AINPUT_SOURCE_KEYBOARD, "KEYBOARD"},
182 {AINPUT_SOURCE_DPAD, "DPAD"},
183 {AINPUT_SOURCE_GAMEPAD, "GAMEPAD"},
184 {AINPUT_SOURCE_TOUCHSCREEN, "TOUCHSCREEN"},
185 {AINPUT_SOURCE_MOUSE, "MOUSE"},
186 {AINPUT_SOURCE_STYLUS, "STYLUS"},
187 {AINPUT_SOURCE_BLUETOOTH_STYLUS, "BLUETOOTH_STYLUS"},
188 {AINPUT_SOURCE_TRACKBALL, "TRACKBALL"},
189 {AINPUT_SOURCE_MOUSE_RELATIVE, "MOUSE_RELATIVE"},
190 {AINPUT_SOURCE_TOUCHPAD, "TOUCHPAD"},
191 {AINPUT_SOURCE_TOUCH_NAVIGATION, "TOUCH_NAVIGATION"},
192 {AINPUT_SOURCE_JOYSTICK, "JOYSTICK"},
193 {AINPUT_SOURCE_HDMI, "HDMI"},
194 {AINPUT_SOURCE_SENSOR, "SENSOR"},
195 {AINPUT_SOURCE_ROTARY_ENCODER, "ROTARY_ENCODER"},
196 };
197 std::string result;
198 for (const auto& [source_entry, str] : SOURCES) {
199 if ((source & source_entry) == source_entry) {
200 if (!result.empty()) {
201 result += " | ";
202 }
203 result += str;
204 }
205 }
206 if (result.empty()) {
207 result = StringPrintf("0x%08x", source);
208 }
209 return result;
210}
211
212bool isFromSource(uint32_t source, uint32_t test) {
213 return (source & test) == test;
214}
215
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700216bool isStylusToolType(ToolType toolType) {
217 return toolType == ToolType::STYLUS || toolType == ToolType::ERASER;
Prabir Pradhane5626962022-10-27 20:30:53 +0000218}
219
Siarhei Vishniakoudcc6e6e2023-10-18 09:20:07 -0700220bool isStylusEvent(uint32_t source, const std::vector<PointerProperties>& properties) {
221 if (!isFromSource(source, AINPUT_SOURCE_STYLUS)) {
222 return false;
223 }
224 // Need at least one stylus pointer for this event to be considered a stylus event
225 for (const PointerProperties& pointerProperties : properties) {
226 if (isStylusToolType(pointerProperties.toolType)) {
227 return true;
228 }
229 }
230 return false;
231}
232
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800233VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
234 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
235 event.getSource(), event.getDisplayId()},
236 event.getAction(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800237 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800238 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800239 event.getKeyCode(),
240 event.getScanCode(),
241 event.getMetaState(),
242 event.getRepeatCount()};
243}
244
245VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
246 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
247 event.getSource(), event.getDisplayId()},
248 event.getRawX(0),
249 event.getRawY(0),
250 event.getActionMasked(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800251 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800252 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800253 event.getMetaState(),
254 event.getButtonState()};
255}
256
Garfield Tan4cc839f2020-01-24 11:26:14 -0800257void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600258 std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800259 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700260 mDeviceId = deviceId;
261 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100262 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600263 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700264}
265
266void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800267 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700268 mDeviceId = from.mDeviceId;
269 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100270 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600271 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700272}
273
Garfield Tan4cc839f2020-01-24 11:26:14 -0800274int32_t InputEvent::nextId() {
275 static IdGenerator idGen(IdGenerator::Source::OTHER);
276 return idGen.nextId();
277}
278
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700279std::ostream& operator<<(std::ostream& out, const InputEvent& event) {
280 switch (event.getType()) {
281 case InputEventType::KEY: {
282 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event);
283 out << keyEvent;
284 return out;
285 }
286 case InputEventType::MOTION: {
287 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event);
288 out << motionEvent;
289 return out;
290 }
291 case InputEventType::FOCUS: {
292 out << "FocusEvent";
293 return out;
294 }
295 case InputEventType::CAPTURE: {
296 out << "CaptureEvent";
297 return out;
298 }
299 case InputEventType::DRAG: {
300 out << "DragEvent";
301 return out;
302 }
303 case InputEventType::TOUCH_MODE: {
304 out << "TouchModeEvent";
305 return out;
306 }
307 }
308}
309
Jeff Brown5912f952013-07-01 19:10:31 -0700310// --- KeyEvent ---
311
Michael Wright872db4f2014-04-22 15:03:51 -0700312const char* KeyEvent::getLabel(int32_t keyCode) {
Chris Ye4958d062020-08-20 13:21:10 -0700313 return InputEventLookup::getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700314}
315
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800316std::optional<int> KeyEvent::getKeyCodeFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700317 return InputEventLookup::getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700318}
319
Garfield Tan4cc839f2020-01-24 11:26:14 -0800320void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600321 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
322 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
323 nsecs_t downTime, nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800324 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700325 mAction = action;
326 mFlags = flags;
327 mKeyCode = keyCode;
328 mScanCode = scanCode;
329 mMetaState = metaState;
330 mRepeatCount = repeatCount;
331 mDownTime = downTime;
332 mEventTime = eventTime;
333}
334
335void KeyEvent::initialize(const KeyEvent& from) {
336 InputEvent::initialize(from);
337 mAction = from.mAction;
338 mFlags = from.mFlags;
339 mKeyCode = from.mKeyCode;
340 mScanCode = from.mScanCode;
341 mMetaState = from.mMetaState;
342 mRepeatCount = from.mRepeatCount;
343 mDownTime = from.mDownTime;
344 mEventTime = from.mEventTime;
345}
346
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700347const char* KeyEvent::actionToString(int32_t action) {
348 // Convert KeyEvent action to string
349 switch (action) {
350 case AKEY_EVENT_ACTION_DOWN:
351 return "DOWN";
352 case AKEY_EVENT_ACTION_UP:
353 return "UP";
354 case AKEY_EVENT_ACTION_MULTIPLE:
355 return "MULTIPLE";
356 }
357 return "UNKNOWN";
358}
Jeff Brown5912f952013-07-01 19:10:31 -0700359
Siarhei Vishniakoud010b012023-01-18 15:00:53 -0800360std::ostream& operator<<(std::ostream& out, const KeyEvent& event) {
361 out << "KeyEvent { action=" << KeyEvent::actionToString(event.getAction());
362
363 out << ", keycode=" << event.getKeyCode() << "(" << KeyEvent::getLabel(event.getKeyCode())
364 << ")";
365
366 if (event.getMetaState() != 0) {
367 out << ", metaState=" << event.getMetaState();
368 }
369
370 out << ", eventTime=" << event.getEventTime();
371 out << ", downTime=" << event.getDownTime();
372 out << ", flags=" << std::hex << event.getFlags() << std::dec;
373 out << ", repeatCount=" << event.getRepeatCount();
374 out << ", deviceId=" << event.getDeviceId();
375 out << ", source=" << inputEventSourceToString(event.getSource());
376 out << ", displayId=" << event.getDisplayId();
377 out << ", eventId=" << event.getId();
378 out << "}";
379 return out;
380}
381
Jeff Brown5912f952013-07-01 19:10:31 -0700382// --- PointerCoords ---
383
384float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700385 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700386 return 0;
387 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700388 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700389}
390
391status_t PointerCoords::setAxisValue(int32_t axis, float value) {
392 if (axis < 0 || axis > 63) {
393 return NAME_NOT_FOUND;
394 }
395
Michael Wright38dcdff2014-03-19 12:06:10 -0700396 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
397 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700398 if (value == 0) {
399 return OK; // axes with value 0 do not need to be stored
400 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700401
402 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700403 if (count >= MAX_AXES) {
404 tooManyAxes(axis);
405 return NO_MEMORY;
406 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700407 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700408 for (uint32_t i = count; i > index; i--) {
409 values[i] = values[i - 1];
410 }
411 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700412
Jeff Brown5912f952013-07-01 19:10:31 -0700413 values[index] = value;
414 return OK;
415}
416
417static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
418 float value = c.getAxisValue(axis);
419 if (value != 0) {
420 c.setAxisValue(axis, value * scaleFactor);
421 }
422}
423
Robert Carre07e1032018-11-26 12:55:53 -0800424void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700425 // No need to scale pressure or size since they are normalized.
426 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800427
428 // If there is a global scale factor, it is included in the windowX/YScale
429 // so we don't need to apply it twice to the X/Y axes.
430 // However we don't want to apply any windowXYScale not included in the global scale
431 // to the TOUCH_MAJOR/MINOR coordinates.
432 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
433 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
434 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
435 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
436 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
437 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
Prabir Pradhanc6523582021-05-14 18:02:55 -0700438 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_X, windowXScale);
439 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_Y, windowYScale);
Robert Carre07e1032018-11-26 12:55:53 -0800440}
441
Brett Chabotfaa986c2020-11-04 17:39:36 -0800442#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700443status_t PointerCoords::readFromParcel(Parcel* parcel) {
444 bits = parcel->readInt64();
445
Michael Wright38dcdff2014-03-19 12:06:10 -0700446 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700447 if (count > MAX_AXES) {
448 return BAD_VALUE;
449 }
450
451 for (uint32_t i = 0; i < count; i++) {
452 values[i] = parcel->readFloat();
453 }
Philip Quinnafb31282022-12-20 18:17:55 -0800454
455 isResampled = parcel->readBool();
Jeff Brown5912f952013-07-01 19:10:31 -0700456 return OK;
457}
458
459status_t PointerCoords::writeToParcel(Parcel* parcel) const {
460 parcel->writeInt64(bits);
461
Michael Wright38dcdff2014-03-19 12:06:10 -0700462 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700463 for (uint32_t i = 0; i < count; i++) {
464 parcel->writeFloat(values[i]);
465 }
Philip Quinnafb31282022-12-20 18:17:55 -0800466
467 parcel->writeBool(isResampled);
Jeff Brown5912f952013-07-01 19:10:31 -0700468 return OK;
469}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800470#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700471
472void PointerCoords::tooManyAxes(int axis) {
473 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
474 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
475}
476
477bool PointerCoords::operator==(const PointerCoords& other) const {
478 if (bits != other.bits) {
479 return false;
480 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700481 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700482 for (uint32_t i = 0; i < count; i++) {
483 if (values[i] != other.values[i]) {
484 return false;
485 }
486 }
Philip Quinnafb31282022-12-20 18:17:55 -0800487 if (isResampled != other.isResampled) {
488 return false;
489 }
Jeff Brown5912f952013-07-01 19:10:31 -0700490 return true;
491}
492
chaviwc01e1372020-07-01 12:37:31 -0700493void PointerCoords::transform(const ui::Transform& transform) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700494 const vec2 xy = transform.transform(getXYValue());
495 setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
496 setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
497
Prabir Pradhanc6523582021-05-14 18:02:55 -0700498 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_X) ||
499 BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_Y)) {
500 const ui::Transform rotation(transform.getOrientation());
501 const vec2 relativeXy = rotation.transform(getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
502 getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
503 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
504 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
505 }
506
Prabir Pradhan6b384612021-05-14 16:56:25 -0700507 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) {
508 const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
509 setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val));
510 }
chaviwc01e1372020-07-01 12:37:31 -0700511}
Jeff Brown5912f952013-07-01 19:10:31 -0700512
Jeff Brown5912f952013-07-01 19:10:31 -0700513// --- MotionEvent ---
514
Garfield Tan4cc839f2020-01-24 11:26:14 -0800515void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600516 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
517 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700518 int32_t buttonState, MotionClassification classification,
519 const ui::Transform& transform, float xPrecision, float yPrecision,
Evan Rosky84f07f02021-04-16 10:42:42 -0700520 float rawXCursorPosition, float rawYCursorPosition,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700521 const ui::Transform& rawTransform, nsecs_t downTime, nsecs_t eventTime,
Evan Rosky09576692021-07-01 12:22:09 -0700522 size_t pointerCount, const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700523 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800524 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700525 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100526 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700527 mFlags = flags;
528 mEdgeFlags = edgeFlags;
529 mMetaState = metaState;
530 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800531 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700532 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700533 mXPrecision = xPrecision;
534 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700535 mRawXCursorPosition = rawXCursorPosition;
536 mRawYCursorPosition = rawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700537 mRawTransform = rawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700538 mDownTime = downTime;
539 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800540 mPointerProperties.insert(mPointerProperties.end(), &pointerProperties[0],
541 &pointerProperties[pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700542 mSampleEventTimes.clear();
543 mSamplePointerCoords.clear();
544 addSample(eventTime, pointerCoords);
545}
546
547void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800548 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
549 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700550 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100551 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700552 mFlags = other->mFlags;
553 mEdgeFlags = other->mEdgeFlags;
554 mMetaState = other->mMetaState;
555 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800556 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700557 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700558 mXPrecision = other->mXPrecision;
559 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700560 mRawXCursorPosition = other->mRawXCursorPosition;
561 mRawYCursorPosition = other->mRawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700562 mRawTransform = other->mRawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700563 mDownTime = other->mDownTime;
564 mPointerProperties = other->mPointerProperties;
565
566 if (keepHistory) {
567 mSampleEventTimes = other->mSampleEventTimes;
568 mSamplePointerCoords = other->mSamplePointerCoords;
569 } else {
570 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500571 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700572 mSamplePointerCoords.clear();
573 size_t pointerCount = other->getPointerCount();
574 size_t historySize = other->getHistorySize();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800575 mSamplePointerCoords
576 .insert(mSamplePointerCoords.end(),
577 &other->mSamplePointerCoords[historySize * pointerCount],
578 &other->mSamplePointerCoords[historySize * pointerCount + pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700579 }
580}
581
582void MotionEvent::addSample(
583 int64_t eventTime,
584 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500585 mSampleEventTimes.push_back(eventTime);
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800586 mSamplePointerCoords.insert(mSamplePointerCoords.end(), &pointerCoords[0],
587 &pointerCoords[getPointerCount()]);
Jeff Brown5912f952013-07-01 19:10:31 -0700588}
589
Michael Wright635422b2022-12-02 00:43:56 +0000590std::optional<ui::Rotation> MotionEvent::getSurfaceRotation() const {
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800591 // The surface rotation is the rotation from the window's coordinate space to that of the
592 // display. Since the event's transform takes display space coordinates to window space, the
593 // returned surface rotation is the inverse of the rotation for the surface.
594 switch (mTransform.getOrientation()) {
595 case ui::Transform::ROT_0:
Michael Wright635422b2022-12-02 00:43:56 +0000596 return ui::ROTATION_0;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800597 case ui::Transform::ROT_90:
Michael Wright635422b2022-12-02 00:43:56 +0000598 return ui::ROTATION_270;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800599 case ui::Transform::ROT_180:
Michael Wright635422b2022-12-02 00:43:56 +0000600 return ui::ROTATION_180;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800601 case ui::Transform::ROT_270:
Michael Wright635422b2022-12-02 00:43:56 +0000602 return ui::ROTATION_90;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800603 default:
Michael Wright635422b2022-12-02 00:43:56 +0000604 return std::nullopt;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800605 }
606}
607
Garfield Tan00f511d2019-06-12 16:55:40 -0700608float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700609 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000610 return roundTransformedCoords(vals.x);
Garfield Tan00f511d2019-06-12 16:55:40 -0700611}
612
613float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700614 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000615 return roundTransformedCoords(vals.y);
Garfield Tan00f511d2019-06-12 16:55:40 -0700616}
617
Garfield Tan937bb832019-07-25 17:48:31 -0700618void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700619 ui::Transform inverse = mTransform.inverse();
620 vec2 vals = inverse.transform(x, y);
621 mRawXCursorPosition = vals.x;
622 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700623}
624
Jeff Brown5912f952013-07-01 19:10:31 -0700625const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000626 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
627 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
628 }
629 const size_t position = getHistorySize() * getPointerCount() + pointerIndex;
630 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
631 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
632 }
633 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700634}
635
636float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
Evan Rosky84f07f02021-04-16 10:42:42 -0700637 return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700638}
639
640float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700641 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700642}
643
644const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
645 size_t pointerIndex, size_t historicalIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000646 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
647 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
648 }
649 if (CC_UNLIKELY(historicalIndex < 0 || historicalIndex > getHistorySize())) {
650 LOG(FATAL) << __func__ << ": Invalid historical index " << historicalIndex << " for "
651 << *this;
652 }
653 const size_t position = historicalIndex * getPointerCount() + pointerIndex;
654 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
655 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
656 }
657 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700658}
659
660float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan6b384612021-05-14 16:56:25 -0700661 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700662 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
663 return calculateTransformedAxisValue(axis, mSource, mRawTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700664}
665
666float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan9f388812021-05-13 16:54:53 -0700667 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700668 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
669 return calculateTransformedAxisValue(axis, mSource, mTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700670}
671
672ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
673 size_t pointerCount = mPointerProperties.size();
674 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800675 if (mPointerProperties[i].id == pointerId) {
Jeff Brown5912f952013-07-01 19:10:31 -0700676 return i;
677 }
678 }
679 return -1;
680}
681
682void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700683 float currXOffset = mTransform.tx();
684 float currYOffset = mTransform.ty();
685 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700686}
687
Robert Carre07e1032018-11-26 12:55:53 -0800688void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700689 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700690 mRawTransform.set(mRawTransform.tx() * globalScaleFactor,
691 mRawTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800692 mXPrecision *= globalScaleFactor;
693 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700694
695 size_t numSamples = mSamplePointerCoords.size();
696 for (size_t i = 0; i < numSamples; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800697 mSamplePointerCoords[i].scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700698 }
699}
700
chaviw9eaa22c2020-07-01 16:21:27 -0700701void MotionEvent::transform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700702 // We want to preserve the raw axes values stored in the PointerCoords, so we just update the
703 // transform using the values passed in.
chaviw9eaa22c2020-07-01 16:21:27 -0700704 ui::Transform newTransform;
705 newTransform.set(matrix);
706 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700707}
708
Evan Roskyd4d4d802021-05-03 20:12:21 -0700709void MotionEvent::applyTransform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700710 ui::Transform transform;
711 transform.set(matrix);
Evan Roskyd4d4d802021-05-03 20:12:21 -0700712
713 // Apply the transformation to all samples.
Prabir Pradhan6b384612021-05-14 16:56:25 -0700714 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
715 [&transform](PointerCoords& c) { c.transform(transform); });
Prabir Pradhan4b19bd02021-06-01 17:34:59 -0700716
717 if (mRawXCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION &&
718 mRawYCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) {
719 const vec2 cursor = transform.transform(mRawXCursorPosition, mRawYCursorPosition);
720 mRawXCursorPosition = cursor.x;
721 mRawYCursorPosition = cursor.y;
722 }
Evan Roskyd4d4d802021-05-03 20:12:21 -0700723}
724
Brett Chabotfaa986c2020-11-04 17:39:36 -0800725#ifdef __linux__
chaviw9eaa22c2020-07-01 16:21:27 -0700726static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
727 float dsdx, dtdx, tx, dtdy, dsdy, ty;
728 status_t status = parcel.readFloat(&dsdx);
729 status |= parcel.readFloat(&dtdx);
730 status |= parcel.readFloat(&tx);
731 status |= parcel.readFloat(&dtdy);
732 status |= parcel.readFloat(&dsdy);
733 status |= parcel.readFloat(&ty);
734
735 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
736 return status;
737}
738
739static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
740 status_t status = parcel.writeFloat(transform.dsdx());
741 status |= parcel.writeFloat(transform.dtdx());
742 status |= parcel.writeFloat(transform.tx());
743 status |= parcel.writeFloat(transform.dtdy());
744 status |= parcel.writeFloat(transform.dsdy());
745 status |= parcel.writeFloat(transform.ty());
746 return status;
747}
748
Jeff Brown5912f952013-07-01 19:10:31 -0700749status_t MotionEvent::readFromParcel(Parcel* parcel) {
750 size_t pointerCount = parcel->readInt32();
751 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800752 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
753 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700754 return BAD_VALUE;
755 }
756
Garfield Tan4cc839f2020-01-24 11:26:14 -0800757 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700758 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600759 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800760 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600761 std::vector<uint8_t> hmac;
762 status_t result = parcel->readByteVector(&hmac);
763 if (result != OK || hmac.size() != 32) {
764 return BAD_VALUE;
765 }
766 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700767 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100768 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700769 mFlags = parcel->readInt32();
770 mEdgeFlags = parcel->readInt32();
771 mMetaState = parcel->readInt32();
772 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800773 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700774
775 result = android::readFromParcel(mTransform, *parcel);
776 if (result != OK) {
777 return result;
778 }
Jeff Brown5912f952013-07-01 19:10:31 -0700779 mXPrecision = parcel->readFloat();
780 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700781 mRawXCursorPosition = parcel->readFloat();
782 mRawYCursorPosition = parcel->readFloat();
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700783
784 result = android::readFromParcel(mRawTransform, *parcel);
785 if (result != OK) {
786 return result;
787 }
Jeff Brown5912f952013-07-01 19:10:31 -0700788 mDownTime = parcel->readInt64();
789
790 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800791 mPointerProperties.reserve(pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700792 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500793 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700794 mSamplePointerCoords.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800795 mSamplePointerCoords.reserve(sampleCount * pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700796
797 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800798 mPointerProperties.push_back({});
799 PointerProperties& properties = mPointerProperties.back();
Jeff Brown5912f952013-07-01 19:10:31 -0700800 properties.id = parcel->readInt32();
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700801 properties.toolType = static_cast<ToolType>(parcel->readInt32());
Jeff Brown5912f952013-07-01 19:10:31 -0700802 }
803
Dan Austinc94fc452015-09-22 14:22:41 -0700804 while (sampleCount > 0) {
805 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500806 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700807 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800808 mSamplePointerCoords.push_back({});
809 status_t status = mSamplePointerCoords.back().readFromParcel(parcel);
Jeff Brown5912f952013-07-01 19:10:31 -0700810 if (status) {
811 return status;
812 }
813 }
814 }
815 return OK;
816}
817
818status_t MotionEvent::writeToParcel(Parcel* parcel) const {
819 size_t pointerCount = mPointerProperties.size();
820 size_t sampleCount = mSampleEventTimes.size();
821
822 parcel->writeInt32(pointerCount);
823 parcel->writeInt32(sampleCount);
824
Garfield Tan4cc839f2020-01-24 11:26:14 -0800825 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700826 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600827 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800828 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600829 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
830 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700831 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100832 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700833 parcel->writeInt32(mFlags);
834 parcel->writeInt32(mEdgeFlags);
835 parcel->writeInt32(mMetaState);
836 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800837 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700838
839 status_t result = android::writeToParcel(mTransform, *parcel);
840 if (result != OK) {
841 return result;
842 }
Jeff Brown5912f952013-07-01 19:10:31 -0700843 parcel->writeFloat(mXPrecision);
844 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700845 parcel->writeFloat(mRawXCursorPosition);
846 parcel->writeFloat(mRawYCursorPosition);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700847
848 result = android::writeToParcel(mRawTransform, *parcel);
849 if (result != OK) {
850 return result;
851 }
Jeff Brown5912f952013-07-01 19:10:31 -0700852 parcel->writeInt64(mDownTime);
853
854 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800855 const PointerProperties& properties = mPointerProperties[i];
Jeff Brown5912f952013-07-01 19:10:31 -0700856 parcel->writeInt32(properties.id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700857 parcel->writeInt32(static_cast<int32_t>(properties.toolType));
Jeff Brown5912f952013-07-01 19:10:31 -0700858 }
859
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800860 const PointerCoords* pc = mSamplePointerCoords.data();
Jeff Brown5912f952013-07-01 19:10:31 -0700861 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500862 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700863 for (size_t i = 0; i < pointerCount; i++) {
864 status_t status = (pc++)->writeToParcel(parcel);
865 if (status) {
866 return status;
867 }
868 }
869 }
870 return OK;
871}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800872#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700873
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600874bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700875 if (isFromSource(source, AINPUT_SOURCE_CLASS_POINTER)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700876 // Specifically excludes HOVER_MOVE and SCROLL.
877 switch (action & AMOTION_EVENT_ACTION_MASK) {
878 case AMOTION_EVENT_ACTION_DOWN:
879 case AMOTION_EVENT_ACTION_MOVE:
880 case AMOTION_EVENT_ACTION_UP:
881 case AMOTION_EVENT_ACTION_POINTER_DOWN:
882 case AMOTION_EVENT_ACTION_POINTER_UP:
883 case AMOTION_EVENT_ACTION_CANCEL:
884 case AMOTION_EVENT_ACTION_OUTSIDE:
885 return true;
886 }
887 }
888 return false;
889}
890
Michael Wright872db4f2014-04-22 15:03:51 -0700891const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700892 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700893}
894
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800895std::optional<int> MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700896 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700897}
898
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500899std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700900 // Convert MotionEvent action to string
901 switch (action & AMOTION_EVENT_ACTION_MASK) {
902 case AMOTION_EVENT_ACTION_DOWN:
903 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700904 case AMOTION_EVENT_ACTION_UP:
905 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500906 case AMOTION_EVENT_ACTION_MOVE:
907 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700908 case AMOTION_EVENT_ACTION_CANCEL:
909 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500910 case AMOTION_EVENT_ACTION_OUTSIDE:
911 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700912 case AMOTION_EVENT_ACTION_POINTER_DOWN:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000913 return StringPrintf("POINTER_DOWN(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700914 case AMOTION_EVENT_ACTION_POINTER_UP:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000915 return StringPrintf("POINTER_UP(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500916 case AMOTION_EVENT_ACTION_HOVER_MOVE:
917 return "HOVER_MOVE";
918 case AMOTION_EVENT_ACTION_SCROLL:
919 return "SCROLL";
920 case AMOTION_EVENT_ACTION_HOVER_ENTER:
921 return "HOVER_ENTER";
922 case AMOTION_EVENT_ACTION_HOVER_EXIT:
923 return "HOVER_EXIT";
924 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
925 return "BUTTON_PRESS";
926 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
927 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700928 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500929 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700930}
931
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700932// Apply the given transformation to the point without checking whether the entire transform
933// should be disregarded altogether for the provided source.
934static inline vec2 calculateTransformedXYUnchecked(uint32_t source, const ui::Transform& transform,
935 const vec2& xy) {
936 return shouldDisregardOffset(source) ? transformWithoutTranslation(transform, xy)
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000937 : roundTransformedCoords(transform.transform(xy));
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700938}
939
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700940vec2 MotionEvent::calculateTransformedXY(uint32_t source, const ui::Transform& transform,
941 const vec2& xy) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700942 if (shouldDisregardTransformation(source)) {
943 return xy;
944 }
945 return calculateTransformedXYUnchecked(source, transform, xy);
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700946}
947
Prabir Pradhan8e6ce222022-02-24 09:08:54 -0800948// Keep in sync with calculateTransformedCoords.
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700949float MotionEvent::calculateTransformedAxisValue(int32_t axis, uint32_t source,
950 const ui::Transform& transform,
951 const PointerCoords& coords) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700952 if (shouldDisregardTransformation(source)) {
953 return coords.getAxisValue(axis);
954 }
955
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700956 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700957 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700958 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
959 return xy[axis];
960 }
961
962 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
963 const vec2 relativeXy =
964 transformWithoutTranslation(transform,
965 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
966 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
967 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
968 }
969
970 if (axis == AMOTION_EVENT_AXIS_ORIENTATION) {
971 return transformAngle(transform, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION));
972 }
973
974 return coords.getAxisValue(axis);
975}
976
Prabir Pradhan8e6ce222022-02-24 09:08:54 -0800977// Keep in sync with calculateTransformedAxisValue. This is an optimization of
978// calculateTransformedAxisValue for all PointerCoords axes.
979PointerCoords MotionEvent::calculateTransformedCoords(uint32_t source,
980 const ui::Transform& transform,
981 const PointerCoords& coords) {
982 if (shouldDisregardTransformation(source)) {
983 return coords;
984 }
985 PointerCoords out = coords;
986
987 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
988 out.setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
989 out.setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
990
991 const vec2 relativeXy =
992 transformWithoutTranslation(transform,
993 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
994 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
995 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
996 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
997
998 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
999 transformAngle(transform,
1000 coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)));
1001
1002 return out;
1003}
1004
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +00001005std::ostream& operator<<(std::ostream& out, const MotionEvent& event) {
1006 out << "MotionEvent { action=" << MotionEvent::actionToString(event.getAction());
1007 if (event.getActionButton() != 0) {
1008 out << ", actionButton=" << std::to_string(event.getActionButton());
1009 }
1010 const size_t pointerCount = event.getPointerCount();
hupeng3aa5a51a2022-09-02 16:00:18 +08001011 LOG_ALWAYS_FATAL_IF(pointerCount > MAX_POINTERS, "Too many pointers : pointerCount = %zu",
1012 pointerCount);
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +00001013 for (size_t i = 0; i < pointerCount; i++) {
1014 out << ", id[" << i << "]=" << event.getPointerId(i);
1015 float x = event.getX(i);
1016 float y = event.getY(i);
1017 if (x != 0 || y != 0) {
1018 out << ", x[" << i << "]=" << x;
1019 out << ", y[" << i << "]=" << y;
1020 }
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001021 ToolType toolType = event.getToolType(i);
1022 if (toolType != ToolType::FINGER) {
1023 out << ", toolType[" << i << "]=" << ftl::enum_string(toolType);
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +00001024 }
1025 }
1026 if (event.getButtonState() != 0) {
1027 out << ", buttonState=" << event.getButtonState();
1028 }
1029 if (event.getClassification() != MotionClassification::NONE) {
1030 out << ", classification=" << motionClassificationToString(event.getClassification());
1031 }
1032 if (event.getMetaState() != 0) {
1033 out << ", metaState=" << event.getMetaState();
1034 }
1035 if (event.getEdgeFlags() != 0) {
1036 out << ", edgeFlags=" << event.getEdgeFlags();
1037 }
1038 if (pointerCount != 1) {
1039 out << ", pointerCount=" << pointerCount;
1040 }
1041 if (event.getHistorySize() != 0) {
1042 out << ", historySize=" << event.getHistorySize();
1043 }
1044 out << ", eventTime=" << event.getEventTime();
1045 out << ", downTime=" << event.getDownTime();
1046 out << ", deviceId=" << event.getDeviceId();
1047 out << ", source=" << inputEventSourceToString(event.getSource());
1048 out << ", displayId=" << event.getDisplayId();
1049 out << ", eventId=" << event.getId();
1050 out << "}";
1051 return out;
1052}
1053
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001054// --- FocusEvent ---
1055
Antonio Kantek3cfec7b2021-11-05 18:26:17 -07001056void FocusEvent::initialize(int32_t id, bool hasFocus) {
Garfield Tan4cc839f2020-01-24 11:26:14 -08001057 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -06001058 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001059 mHasFocus = hasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001060}
1061
1062void FocusEvent::initialize(const FocusEvent& from) {
1063 InputEvent::initialize(from);
1064 mHasFocus = from.mHasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001065}
Jeff Brown5912f952013-07-01 19:10:31 -07001066
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001067// --- CaptureEvent ---
1068
1069void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) {
1070 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1071 ADISPLAY_ID_NONE, INVALID_HMAC);
1072 mPointerCaptureEnabled = pointerCaptureEnabled;
1073}
1074
1075void CaptureEvent::initialize(const CaptureEvent& from) {
1076 InputEvent::initialize(from);
1077 mPointerCaptureEnabled = from.mPointerCaptureEnabled;
1078}
1079
arthurhung7632c332020-12-30 16:58:01 +08001080// --- DragEvent ---
1081
1082void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) {
1083 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1084 ADISPLAY_ID_NONE, INVALID_HMAC);
1085 mIsExiting = isExiting;
1086 mX = x;
1087 mY = y;
1088}
1089
1090void DragEvent::initialize(const DragEvent& from) {
1091 InputEvent::initialize(from);
1092 mIsExiting = from.mIsExiting;
1093 mX = from.mX;
1094 mY = from.mY;
1095}
1096
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001097// --- TouchModeEvent ---
1098
1099void TouchModeEvent::initialize(int32_t id, bool isInTouchMode) {
1100 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1101 ADISPLAY_ID_NONE, INVALID_HMAC);
1102 mIsInTouchMode = isInTouchMode;
1103}
1104
1105void TouchModeEvent::initialize(const TouchModeEvent& from) {
1106 InputEvent::initialize(from);
1107 mIsInTouchMode = from.mIsInTouchMode;
1108}
1109
Jeff Brown5912f952013-07-01 19:10:31 -07001110// --- PooledInputEventFactory ---
1111
1112PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
1113 mMaxPoolSize(maxPoolSize) {
1114}
1115
1116PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -07001117}
1118
1119KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001120 if (mKeyEventPool.empty()) {
1121 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001122 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001123 KeyEvent* event = mKeyEventPool.front().release();
1124 mKeyEventPool.pop();
1125 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001126}
1127
1128MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001129 if (mMotionEventPool.empty()) {
1130 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001131 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001132 MotionEvent* event = mMotionEventPool.front().release();
1133 mMotionEventPool.pop();
1134 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001135}
1136
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001137FocusEvent* PooledInputEventFactory::createFocusEvent() {
1138 if (mFocusEventPool.empty()) {
1139 return new FocusEvent();
1140 }
1141 FocusEvent* event = mFocusEventPool.front().release();
1142 mFocusEventPool.pop();
1143 return event;
1144}
1145
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001146CaptureEvent* PooledInputEventFactory::createCaptureEvent() {
1147 if (mCaptureEventPool.empty()) {
1148 return new CaptureEvent();
1149 }
1150 CaptureEvent* event = mCaptureEventPool.front().release();
1151 mCaptureEventPool.pop();
1152 return event;
1153}
1154
arthurhung7632c332020-12-30 16:58:01 +08001155DragEvent* PooledInputEventFactory::createDragEvent() {
1156 if (mDragEventPool.empty()) {
1157 return new DragEvent();
1158 }
1159 DragEvent* event = mDragEventPool.front().release();
1160 mDragEventPool.pop();
1161 return event;
1162}
1163
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001164TouchModeEvent* PooledInputEventFactory::createTouchModeEvent() {
1165 if (mTouchModeEventPool.empty()) {
1166 return new TouchModeEvent();
1167 }
1168 TouchModeEvent* event = mTouchModeEventPool.front().release();
1169 mTouchModeEventPool.pop();
1170 return event;
1171}
1172
Jeff Brown5912f952013-07-01 19:10:31 -07001173void PooledInputEventFactory::recycle(InputEvent* event) {
1174 switch (event->getType()) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001175 case InputEventType::KEY: {
1176 if (mKeyEventPool.size() < mMaxPoolSize) {
1177 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
1178 return;
1179 }
1180 break;
Jeff Brown5912f952013-07-01 19:10:31 -07001181 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001182 case InputEventType::MOTION: {
1183 if (mMotionEventPool.size() < mMaxPoolSize) {
1184 mMotionEventPool.push(
1185 std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
1186 return;
1187 }
1188 break;
Jeff Brown5912f952013-07-01 19:10:31 -07001189 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001190 case InputEventType::FOCUS: {
1191 if (mFocusEventPool.size() < mMaxPoolSize) {
1192 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
1193 return;
1194 }
1195 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001196 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001197 case InputEventType::CAPTURE: {
1198 if (mCaptureEventPool.size() < mMaxPoolSize) {
1199 mCaptureEventPool.push(
1200 std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event)));
1201 return;
1202 }
1203 break;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001204 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001205 case InputEventType::DRAG: {
1206 if (mDragEventPool.size() < mMaxPoolSize) {
1207 mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event)));
1208 return;
1209 }
1210 break;
arthurhung7632c332020-12-30 16:58:01 +08001211 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001212 case InputEventType::TOUCH_MODE: {
1213 if (mTouchModeEventPool.size() < mMaxPoolSize) {
1214 mTouchModeEventPool.push(
1215 std::unique_ptr<TouchModeEvent>(static_cast<TouchModeEvent*>(event)));
1216 return;
1217 }
1218 break;
Antonio Kantekeb4a30c2021-09-28 17:49:49 -07001219 }
Jeff Brown5912f952013-07-01 19:10:31 -07001220 }
1221 delete event;
1222}
1223
1224} // namespace android