blob: 4dbf575490a6b1e4110eca29e5250024937e5202 [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 Vishniakou7feb2ea2019-11-25 15:11:23 -0800173const char* inputEventTypeToString(int32_t type) {
174 switch (type) {
175 case AINPUT_EVENT_TYPE_KEY: {
176 return "KEY";
177 }
178 case AINPUT_EVENT_TYPE_MOTION: {
179 return "MOTION";
180 }
181 case AINPUT_EVENT_TYPE_FOCUS: {
182 return "FOCUS";
183 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800184 case AINPUT_EVENT_TYPE_CAPTURE: {
185 return "CAPTURE";
186 }
arthurhung7632c332020-12-30 16:58:01 +0800187 case AINPUT_EVENT_TYPE_DRAG: {
188 return "DRAG";
189 }
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700190 case AINPUT_EVENT_TYPE_TOUCH_MODE: {
191 return "TOUCH_MODE";
192 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800193 }
194 return "UNKNOWN";
195}
196
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800197std::string inputEventSourceToString(int32_t source) {
198 if (source == AINPUT_SOURCE_UNKNOWN) {
199 return "UNKNOWN";
200 }
201 if (source == static_cast<int32_t>(AINPUT_SOURCE_ANY)) {
202 return "ANY";
203 }
204 static const std::map<int32_t, const char*> SOURCES{
205 {AINPUT_SOURCE_KEYBOARD, "KEYBOARD"},
206 {AINPUT_SOURCE_DPAD, "DPAD"},
207 {AINPUT_SOURCE_GAMEPAD, "GAMEPAD"},
208 {AINPUT_SOURCE_TOUCHSCREEN, "TOUCHSCREEN"},
209 {AINPUT_SOURCE_MOUSE, "MOUSE"},
210 {AINPUT_SOURCE_STYLUS, "STYLUS"},
211 {AINPUT_SOURCE_BLUETOOTH_STYLUS, "BLUETOOTH_STYLUS"},
212 {AINPUT_SOURCE_TRACKBALL, "TRACKBALL"},
213 {AINPUT_SOURCE_MOUSE_RELATIVE, "MOUSE_RELATIVE"},
214 {AINPUT_SOURCE_TOUCHPAD, "TOUCHPAD"},
215 {AINPUT_SOURCE_TOUCH_NAVIGATION, "TOUCH_NAVIGATION"},
216 {AINPUT_SOURCE_JOYSTICK, "JOYSTICK"},
217 {AINPUT_SOURCE_HDMI, "HDMI"},
218 {AINPUT_SOURCE_SENSOR, "SENSOR"},
219 {AINPUT_SOURCE_ROTARY_ENCODER, "ROTARY_ENCODER"},
220 };
221 std::string result;
222 for (const auto& [source_entry, str] : SOURCES) {
223 if ((source & source_entry) == source_entry) {
224 if (!result.empty()) {
225 result += " | ";
226 }
227 result += str;
228 }
229 }
230 if (result.empty()) {
231 result = StringPrintf("0x%08x", source);
232 }
233 return result;
234}
235
236bool isFromSource(uint32_t source, uint32_t test) {
237 return (source & test) == test;
238}
239
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700240bool isStylusToolType(ToolType toolType) {
241 return toolType == ToolType::STYLUS || toolType == ToolType::ERASER;
Prabir Pradhane5626962022-10-27 20:30:53 +0000242}
243
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800244VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
245 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
246 event.getSource(), event.getDisplayId()},
247 event.getAction(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800248 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800249 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800250 event.getKeyCode(),
251 event.getScanCode(),
252 event.getMetaState(),
253 event.getRepeatCount()};
254}
255
256VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
257 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
258 event.getSource(), event.getDisplayId()},
259 event.getRawX(0),
260 event.getRawY(0),
261 event.getActionMasked(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800262 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800263 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800264 event.getMetaState(),
265 event.getButtonState()};
266}
267
Garfield Tan4cc839f2020-01-24 11:26:14 -0800268void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600269 std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800270 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700271 mDeviceId = deviceId;
272 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100273 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600274 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700275}
276
277void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800278 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700279 mDeviceId = from.mDeviceId;
280 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100281 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600282 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700283}
284
Garfield Tan4cc839f2020-01-24 11:26:14 -0800285int32_t InputEvent::nextId() {
286 static IdGenerator idGen(IdGenerator::Source::OTHER);
287 return idGen.nextId();
288}
289
Jeff Brown5912f952013-07-01 19:10:31 -0700290// --- KeyEvent ---
291
Michael Wright872db4f2014-04-22 15:03:51 -0700292const char* KeyEvent::getLabel(int32_t keyCode) {
Chris Ye4958d062020-08-20 13:21:10 -0700293 return InputEventLookup::getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700294}
295
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800296std::optional<int> KeyEvent::getKeyCodeFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700297 return InputEventLookup::getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700298}
299
Garfield Tan4cc839f2020-01-24 11:26:14 -0800300void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600301 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
302 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
303 nsecs_t downTime, nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800304 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700305 mAction = action;
306 mFlags = flags;
307 mKeyCode = keyCode;
308 mScanCode = scanCode;
309 mMetaState = metaState;
310 mRepeatCount = repeatCount;
311 mDownTime = downTime;
312 mEventTime = eventTime;
313}
314
315void KeyEvent::initialize(const KeyEvent& from) {
316 InputEvent::initialize(from);
317 mAction = from.mAction;
318 mFlags = from.mFlags;
319 mKeyCode = from.mKeyCode;
320 mScanCode = from.mScanCode;
321 mMetaState = from.mMetaState;
322 mRepeatCount = from.mRepeatCount;
323 mDownTime = from.mDownTime;
324 mEventTime = from.mEventTime;
325}
326
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700327const char* KeyEvent::actionToString(int32_t action) {
328 // Convert KeyEvent action to string
329 switch (action) {
330 case AKEY_EVENT_ACTION_DOWN:
331 return "DOWN";
332 case AKEY_EVENT_ACTION_UP:
333 return "UP";
334 case AKEY_EVENT_ACTION_MULTIPLE:
335 return "MULTIPLE";
336 }
337 return "UNKNOWN";
338}
Jeff Brown5912f952013-07-01 19:10:31 -0700339
Siarhei Vishniakoud010b012023-01-18 15:00:53 -0800340std::ostream& operator<<(std::ostream& out, const KeyEvent& event) {
341 out << "KeyEvent { action=" << KeyEvent::actionToString(event.getAction());
342
343 out << ", keycode=" << event.getKeyCode() << "(" << KeyEvent::getLabel(event.getKeyCode())
344 << ")";
345
346 if (event.getMetaState() != 0) {
347 out << ", metaState=" << event.getMetaState();
348 }
349
350 out << ", eventTime=" << event.getEventTime();
351 out << ", downTime=" << event.getDownTime();
352 out << ", flags=" << std::hex << event.getFlags() << std::dec;
353 out << ", repeatCount=" << event.getRepeatCount();
354 out << ", deviceId=" << event.getDeviceId();
355 out << ", source=" << inputEventSourceToString(event.getSource());
356 out << ", displayId=" << event.getDisplayId();
357 out << ", eventId=" << event.getId();
358 out << "}";
359 return out;
360}
361
Jeff Brown5912f952013-07-01 19:10:31 -0700362// --- PointerCoords ---
363
364float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700365 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700366 return 0;
367 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700368 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700369}
370
371status_t PointerCoords::setAxisValue(int32_t axis, float value) {
372 if (axis < 0 || axis > 63) {
373 return NAME_NOT_FOUND;
374 }
375
Michael Wright38dcdff2014-03-19 12:06:10 -0700376 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
377 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700378 if (value == 0) {
379 return OK; // axes with value 0 do not need to be stored
380 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700381
382 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700383 if (count >= MAX_AXES) {
384 tooManyAxes(axis);
385 return NO_MEMORY;
386 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700387 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700388 for (uint32_t i = count; i > index; i--) {
389 values[i] = values[i - 1];
390 }
391 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700392
Jeff Brown5912f952013-07-01 19:10:31 -0700393 values[index] = value;
394 return OK;
395}
396
397static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
398 float value = c.getAxisValue(axis);
399 if (value != 0) {
400 c.setAxisValue(axis, value * scaleFactor);
401 }
402}
403
Robert Carre07e1032018-11-26 12:55:53 -0800404void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700405 // No need to scale pressure or size since they are normalized.
406 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800407
408 // If there is a global scale factor, it is included in the windowX/YScale
409 // so we don't need to apply it twice to the X/Y axes.
410 // However we don't want to apply any windowXYScale not included in the global scale
411 // to the TOUCH_MAJOR/MINOR coordinates.
412 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
413 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
414 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
415 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
416 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
417 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
Prabir Pradhanc6523582021-05-14 18:02:55 -0700418 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_X, windowXScale);
419 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_Y, windowYScale);
Robert Carre07e1032018-11-26 12:55:53 -0800420}
421
Brett Chabotfaa986c2020-11-04 17:39:36 -0800422#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700423status_t PointerCoords::readFromParcel(Parcel* parcel) {
424 bits = parcel->readInt64();
425
Michael Wright38dcdff2014-03-19 12:06:10 -0700426 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700427 if (count > MAX_AXES) {
428 return BAD_VALUE;
429 }
430
431 for (uint32_t i = 0; i < count; i++) {
432 values[i] = parcel->readFloat();
433 }
Philip Quinnafb31282022-12-20 18:17:55 -0800434
435 isResampled = parcel->readBool();
Jeff Brown5912f952013-07-01 19:10:31 -0700436 return OK;
437}
438
439status_t PointerCoords::writeToParcel(Parcel* parcel) const {
440 parcel->writeInt64(bits);
441
Michael Wright38dcdff2014-03-19 12:06:10 -0700442 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700443 for (uint32_t i = 0; i < count; i++) {
444 parcel->writeFloat(values[i]);
445 }
Philip Quinnafb31282022-12-20 18:17:55 -0800446
447 parcel->writeBool(isResampled);
Jeff Brown5912f952013-07-01 19:10:31 -0700448 return OK;
449}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800450#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700451
452void PointerCoords::tooManyAxes(int axis) {
453 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
454 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
455}
456
457bool PointerCoords::operator==(const PointerCoords& other) const {
458 if (bits != other.bits) {
459 return false;
460 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700461 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700462 for (uint32_t i = 0; i < count; i++) {
463 if (values[i] != other.values[i]) {
464 return false;
465 }
466 }
Philip Quinnafb31282022-12-20 18:17:55 -0800467 if (isResampled != other.isResampled) {
468 return false;
469 }
Jeff Brown5912f952013-07-01 19:10:31 -0700470 return true;
471}
472
chaviwc01e1372020-07-01 12:37:31 -0700473void PointerCoords::transform(const ui::Transform& transform) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700474 const vec2 xy = transform.transform(getXYValue());
475 setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
476 setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
477
Prabir Pradhanc6523582021-05-14 18:02:55 -0700478 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_X) ||
479 BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_Y)) {
480 const ui::Transform rotation(transform.getOrientation());
481 const vec2 relativeXy = rotation.transform(getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
482 getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
483 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
484 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
485 }
486
Prabir Pradhan6b384612021-05-14 16:56:25 -0700487 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) {
488 const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
489 setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val));
490 }
chaviwc01e1372020-07-01 12:37:31 -0700491}
Jeff Brown5912f952013-07-01 19:10:31 -0700492
493// --- PointerProperties ---
494
495bool PointerProperties::operator==(const PointerProperties& other) const {
496 return id == other.id
497 && toolType == other.toolType;
498}
499
500void PointerProperties::copyFrom(const PointerProperties& other) {
501 id = other.id;
502 toolType = other.toolType;
503}
504
505
506// --- MotionEvent ---
507
Garfield Tan4cc839f2020-01-24 11:26:14 -0800508void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600509 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
510 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700511 int32_t buttonState, MotionClassification classification,
512 const ui::Transform& transform, float xPrecision, float yPrecision,
Evan Rosky84f07f02021-04-16 10:42:42 -0700513 float rawXCursorPosition, float rawYCursorPosition,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700514 const ui::Transform& rawTransform, nsecs_t downTime, nsecs_t eventTime,
Evan Rosky09576692021-07-01 12:22:09 -0700515 size_t pointerCount, const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700516 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800517 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700518 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100519 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700520 mFlags = flags;
521 mEdgeFlags = edgeFlags;
522 mMetaState = metaState;
523 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800524 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700525 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700526 mXPrecision = xPrecision;
527 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700528 mRawXCursorPosition = rawXCursorPosition;
529 mRawYCursorPosition = rawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700530 mRawTransform = rawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700531 mDownTime = downTime;
532 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800533 mPointerProperties.insert(mPointerProperties.end(), &pointerProperties[0],
534 &pointerProperties[pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700535 mSampleEventTimes.clear();
536 mSamplePointerCoords.clear();
537 addSample(eventTime, pointerCoords);
538}
539
540void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800541 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
542 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700543 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100544 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700545 mFlags = other->mFlags;
546 mEdgeFlags = other->mEdgeFlags;
547 mMetaState = other->mMetaState;
548 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800549 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700550 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700551 mXPrecision = other->mXPrecision;
552 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700553 mRawXCursorPosition = other->mRawXCursorPosition;
554 mRawYCursorPosition = other->mRawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700555 mRawTransform = other->mRawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700556 mDownTime = other->mDownTime;
557 mPointerProperties = other->mPointerProperties;
558
559 if (keepHistory) {
560 mSampleEventTimes = other->mSampleEventTimes;
561 mSamplePointerCoords = other->mSamplePointerCoords;
562 } else {
563 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500564 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700565 mSamplePointerCoords.clear();
566 size_t pointerCount = other->getPointerCount();
567 size_t historySize = other->getHistorySize();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800568 mSamplePointerCoords
569 .insert(mSamplePointerCoords.end(),
570 &other->mSamplePointerCoords[historySize * pointerCount],
571 &other->mSamplePointerCoords[historySize * pointerCount + pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700572 }
573}
574
575void MotionEvent::addSample(
576 int64_t eventTime,
577 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500578 mSampleEventTimes.push_back(eventTime);
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800579 mSamplePointerCoords.insert(mSamplePointerCoords.end(), &pointerCoords[0],
580 &pointerCoords[getPointerCount()]);
Jeff Brown5912f952013-07-01 19:10:31 -0700581}
582
Michael Wright635422b2022-12-02 00:43:56 +0000583std::optional<ui::Rotation> MotionEvent::getSurfaceRotation() const {
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800584 // The surface rotation is the rotation from the window's coordinate space to that of the
585 // display. Since the event's transform takes display space coordinates to window space, the
586 // returned surface rotation is the inverse of the rotation for the surface.
587 switch (mTransform.getOrientation()) {
588 case ui::Transform::ROT_0:
Michael Wright635422b2022-12-02 00:43:56 +0000589 return ui::ROTATION_0;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800590 case ui::Transform::ROT_90:
Michael Wright635422b2022-12-02 00:43:56 +0000591 return ui::ROTATION_270;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800592 case ui::Transform::ROT_180:
Michael Wright635422b2022-12-02 00:43:56 +0000593 return ui::ROTATION_180;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800594 case ui::Transform::ROT_270:
Michael Wright635422b2022-12-02 00:43:56 +0000595 return ui::ROTATION_90;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800596 default:
Michael Wright635422b2022-12-02 00:43:56 +0000597 return std::nullopt;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800598 }
599}
600
Garfield Tan00f511d2019-06-12 16:55:40 -0700601float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700602 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000603 return roundTransformedCoords(vals.x);
Garfield Tan00f511d2019-06-12 16:55:40 -0700604}
605
606float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700607 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000608 return roundTransformedCoords(vals.y);
Garfield Tan00f511d2019-06-12 16:55:40 -0700609}
610
Garfield Tan937bb832019-07-25 17:48:31 -0700611void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700612 ui::Transform inverse = mTransform.inverse();
613 vec2 vals = inverse.transform(x, y);
614 mRawXCursorPosition = vals.x;
615 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700616}
617
Jeff Brown5912f952013-07-01 19:10:31 -0700618const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000619 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
620 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
621 }
622 const size_t position = getHistorySize() * getPointerCount() + pointerIndex;
623 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
624 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
625 }
626 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700627}
628
629float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
Evan Rosky84f07f02021-04-16 10:42:42 -0700630 return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700631}
632
633float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700634 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700635}
636
637const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
638 size_t pointerIndex, size_t historicalIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000639 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
640 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
641 }
642 if (CC_UNLIKELY(historicalIndex < 0 || historicalIndex > getHistorySize())) {
643 LOG(FATAL) << __func__ << ": Invalid historical index " << historicalIndex << " for "
644 << *this;
645 }
646 const size_t position = historicalIndex * getPointerCount() + pointerIndex;
647 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
648 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
649 }
650 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700651}
652
653float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan6b384612021-05-14 16:56:25 -0700654 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700655 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
656 return calculateTransformedAxisValue(axis, mSource, mRawTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700657}
658
659float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan9f388812021-05-13 16:54:53 -0700660 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700661 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
662 return calculateTransformedAxisValue(axis, mSource, mTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700663}
664
665ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
666 size_t pointerCount = mPointerProperties.size();
667 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800668 if (mPointerProperties[i].id == pointerId) {
Jeff Brown5912f952013-07-01 19:10:31 -0700669 return i;
670 }
671 }
672 return -1;
673}
674
675void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700676 float currXOffset = mTransform.tx();
677 float currYOffset = mTransform.ty();
678 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700679}
680
Robert Carre07e1032018-11-26 12:55:53 -0800681void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700682 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700683 mRawTransform.set(mRawTransform.tx() * globalScaleFactor,
684 mRawTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800685 mXPrecision *= globalScaleFactor;
686 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700687
688 size_t numSamples = mSamplePointerCoords.size();
689 for (size_t i = 0; i < numSamples; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800690 mSamplePointerCoords[i].scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700691 }
692}
693
chaviw9eaa22c2020-07-01 16:21:27 -0700694void MotionEvent::transform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700695 // We want to preserve the raw axes values stored in the PointerCoords, so we just update the
696 // transform using the values passed in.
chaviw9eaa22c2020-07-01 16:21:27 -0700697 ui::Transform newTransform;
698 newTransform.set(matrix);
699 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700700}
701
Evan Roskyd4d4d802021-05-03 20:12:21 -0700702void MotionEvent::applyTransform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700703 ui::Transform transform;
704 transform.set(matrix);
Evan Roskyd4d4d802021-05-03 20:12:21 -0700705
706 // Apply the transformation to all samples.
Prabir Pradhan6b384612021-05-14 16:56:25 -0700707 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
708 [&transform](PointerCoords& c) { c.transform(transform); });
Prabir Pradhan4b19bd02021-06-01 17:34:59 -0700709
710 if (mRawXCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION &&
711 mRawYCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) {
712 const vec2 cursor = transform.transform(mRawXCursorPosition, mRawYCursorPosition);
713 mRawXCursorPosition = cursor.x;
714 mRawYCursorPosition = cursor.y;
715 }
Evan Roskyd4d4d802021-05-03 20:12:21 -0700716}
717
Brett Chabotfaa986c2020-11-04 17:39:36 -0800718#ifdef __linux__
chaviw9eaa22c2020-07-01 16:21:27 -0700719static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
720 float dsdx, dtdx, tx, dtdy, dsdy, ty;
721 status_t status = parcel.readFloat(&dsdx);
722 status |= parcel.readFloat(&dtdx);
723 status |= parcel.readFloat(&tx);
724 status |= parcel.readFloat(&dtdy);
725 status |= parcel.readFloat(&dsdy);
726 status |= parcel.readFloat(&ty);
727
728 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
729 return status;
730}
731
732static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
733 status_t status = parcel.writeFloat(transform.dsdx());
734 status |= parcel.writeFloat(transform.dtdx());
735 status |= parcel.writeFloat(transform.tx());
736 status |= parcel.writeFloat(transform.dtdy());
737 status |= parcel.writeFloat(transform.dsdy());
738 status |= parcel.writeFloat(transform.ty());
739 return status;
740}
741
Jeff Brown5912f952013-07-01 19:10:31 -0700742status_t MotionEvent::readFromParcel(Parcel* parcel) {
743 size_t pointerCount = parcel->readInt32();
744 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800745 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
746 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700747 return BAD_VALUE;
748 }
749
Garfield Tan4cc839f2020-01-24 11:26:14 -0800750 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700751 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600752 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800753 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600754 std::vector<uint8_t> hmac;
755 status_t result = parcel->readByteVector(&hmac);
756 if (result != OK || hmac.size() != 32) {
757 return BAD_VALUE;
758 }
759 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700760 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100761 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700762 mFlags = parcel->readInt32();
763 mEdgeFlags = parcel->readInt32();
764 mMetaState = parcel->readInt32();
765 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800766 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700767
768 result = android::readFromParcel(mTransform, *parcel);
769 if (result != OK) {
770 return result;
771 }
Jeff Brown5912f952013-07-01 19:10:31 -0700772 mXPrecision = parcel->readFloat();
773 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700774 mRawXCursorPosition = parcel->readFloat();
775 mRawYCursorPosition = parcel->readFloat();
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700776
777 result = android::readFromParcel(mRawTransform, *parcel);
778 if (result != OK) {
779 return result;
780 }
Jeff Brown5912f952013-07-01 19:10:31 -0700781 mDownTime = parcel->readInt64();
782
783 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800784 mPointerProperties.reserve(pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700785 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500786 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700787 mSamplePointerCoords.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800788 mSamplePointerCoords.reserve(sampleCount * pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700789
790 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800791 mPointerProperties.push_back({});
792 PointerProperties& properties = mPointerProperties.back();
Jeff Brown5912f952013-07-01 19:10:31 -0700793 properties.id = parcel->readInt32();
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700794 properties.toolType = static_cast<ToolType>(parcel->readInt32());
Jeff Brown5912f952013-07-01 19:10:31 -0700795 }
796
Dan Austinc94fc452015-09-22 14:22:41 -0700797 while (sampleCount > 0) {
798 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500799 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700800 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800801 mSamplePointerCoords.push_back({});
802 status_t status = mSamplePointerCoords.back().readFromParcel(parcel);
Jeff Brown5912f952013-07-01 19:10:31 -0700803 if (status) {
804 return status;
805 }
806 }
807 }
808 return OK;
809}
810
811status_t MotionEvent::writeToParcel(Parcel* parcel) const {
812 size_t pointerCount = mPointerProperties.size();
813 size_t sampleCount = mSampleEventTimes.size();
814
815 parcel->writeInt32(pointerCount);
816 parcel->writeInt32(sampleCount);
817
Garfield Tan4cc839f2020-01-24 11:26:14 -0800818 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700819 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600820 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800821 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600822 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
823 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700824 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100825 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700826 parcel->writeInt32(mFlags);
827 parcel->writeInt32(mEdgeFlags);
828 parcel->writeInt32(mMetaState);
829 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800830 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700831
832 status_t result = android::writeToParcel(mTransform, *parcel);
833 if (result != OK) {
834 return result;
835 }
Jeff Brown5912f952013-07-01 19:10:31 -0700836 parcel->writeFloat(mXPrecision);
837 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700838 parcel->writeFloat(mRawXCursorPosition);
839 parcel->writeFloat(mRawYCursorPosition);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700840
841 result = android::writeToParcel(mRawTransform, *parcel);
842 if (result != OK) {
843 return result;
844 }
Jeff Brown5912f952013-07-01 19:10:31 -0700845 parcel->writeInt64(mDownTime);
846
847 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800848 const PointerProperties& properties = mPointerProperties[i];
Jeff Brown5912f952013-07-01 19:10:31 -0700849 parcel->writeInt32(properties.id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700850 parcel->writeInt32(static_cast<int32_t>(properties.toolType));
Jeff Brown5912f952013-07-01 19:10:31 -0700851 }
852
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800853 const PointerCoords* pc = mSamplePointerCoords.data();
Jeff Brown5912f952013-07-01 19:10:31 -0700854 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500855 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700856 for (size_t i = 0; i < pointerCount; i++) {
857 status_t status = (pc++)->writeToParcel(parcel);
858 if (status) {
859 return status;
860 }
861 }
862 }
863 return OK;
864}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800865#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700866
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600867bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700868 if (isFromSource(source, AINPUT_SOURCE_CLASS_POINTER)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700869 // Specifically excludes HOVER_MOVE and SCROLL.
870 switch (action & AMOTION_EVENT_ACTION_MASK) {
871 case AMOTION_EVENT_ACTION_DOWN:
872 case AMOTION_EVENT_ACTION_MOVE:
873 case AMOTION_EVENT_ACTION_UP:
874 case AMOTION_EVENT_ACTION_POINTER_DOWN:
875 case AMOTION_EVENT_ACTION_POINTER_UP:
876 case AMOTION_EVENT_ACTION_CANCEL:
877 case AMOTION_EVENT_ACTION_OUTSIDE:
878 return true;
879 }
880 }
881 return false;
882}
883
Michael Wright872db4f2014-04-22 15:03:51 -0700884const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700885 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700886}
887
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800888std::optional<int> MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700889 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700890}
891
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500892std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700893 // Convert MotionEvent action to string
894 switch (action & AMOTION_EVENT_ACTION_MASK) {
895 case AMOTION_EVENT_ACTION_DOWN:
896 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700897 case AMOTION_EVENT_ACTION_UP:
898 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500899 case AMOTION_EVENT_ACTION_MOVE:
900 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700901 case AMOTION_EVENT_ACTION_CANCEL:
902 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500903 case AMOTION_EVENT_ACTION_OUTSIDE:
904 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700905 case AMOTION_EVENT_ACTION_POINTER_DOWN:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000906 return StringPrintf("POINTER_DOWN(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700907 case AMOTION_EVENT_ACTION_POINTER_UP:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000908 return StringPrintf("POINTER_UP(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500909 case AMOTION_EVENT_ACTION_HOVER_MOVE:
910 return "HOVER_MOVE";
911 case AMOTION_EVENT_ACTION_SCROLL:
912 return "SCROLL";
913 case AMOTION_EVENT_ACTION_HOVER_ENTER:
914 return "HOVER_ENTER";
915 case AMOTION_EVENT_ACTION_HOVER_EXIT:
916 return "HOVER_EXIT";
917 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
918 return "BUTTON_PRESS";
919 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
920 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700921 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500922 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700923}
924
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700925// Apply the given transformation to the point without checking whether the entire transform
926// should be disregarded altogether for the provided source.
927static inline vec2 calculateTransformedXYUnchecked(uint32_t source, const ui::Transform& transform,
928 const vec2& xy) {
929 return shouldDisregardOffset(source) ? transformWithoutTranslation(transform, xy)
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000930 : roundTransformedCoords(transform.transform(xy));
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700931}
932
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700933vec2 MotionEvent::calculateTransformedXY(uint32_t source, const ui::Transform& transform,
934 const vec2& xy) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700935 if (shouldDisregardTransformation(source)) {
936 return xy;
937 }
938 return calculateTransformedXYUnchecked(source, transform, xy);
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700939}
940
Prabir Pradhan8e6ce222022-02-24 09:08:54 -0800941// Keep in sync with calculateTransformedCoords.
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700942float MotionEvent::calculateTransformedAxisValue(int32_t axis, uint32_t source,
943 const ui::Transform& transform,
944 const PointerCoords& coords) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700945 if (shouldDisregardTransformation(source)) {
946 return coords.getAxisValue(axis);
947 }
948
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700949 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700950 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700951 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
952 return xy[axis];
953 }
954
955 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
956 const vec2 relativeXy =
957 transformWithoutTranslation(transform,
958 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
959 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
960 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
961 }
962
963 if (axis == AMOTION_EVENT_AXIS_ORIENTATION) {
964 return transformAngle(transform, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION));
965 }
966
967 return coords.getAxisValue(axis);
968}
969
Prabir Pradhan8e6ce222022-02-24 09:08:54 -0800970// Keep in sync with calculateTransformedAxisValue. This is an optimization of
971// calculateTransformedAxisValue for all PointerCoords axes.
972PointerCoords MotionEvent::calculateTransformedCoords(uint32_t source,
973 const ui::Transform& transform,
974 const PointerCoords& coords) {
975 if (shouldDisregardTransformation(source)) {
976 return coords;
977 }
978 PointerCoords out = coords;
979
980 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
981 out.setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
982 out.setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
983
984 const vec2 relativeXy =
985 transformWithoutTranslation(transform,
986 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
987 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
988 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
989 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
990
991 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
992 transformAngle(transform,
993 coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)));
994
995 return out;
996}
997
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000998std::ostream& operator<<(std::ostream& out, const MotionEvent& event) {
999 out << "MotionEvent { action=" << MotionEvent::actionToString(event.getAction());
1000 if (event.getActionButton() != 0) {
1001 out << ", actionButton=" << std::to_string(event.getActionButton());
1002 }
1003 const size_t pointerCount = event.getPointerCount();
hupeng3aa5a51a2022-09-02 16:00:18 +08001004 LOG_ALWAYS_FATAL_IF(pointerCount > MAX_POINTERS, "Too many pointers : pointerCount = %zu",
1005 pointerCount);
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +00001006 for (size_t i = 0; i < pointerCount; i++) {
1007 out << ", id[" << i << "]=" << event.getPointerId(i);
1008 float x = event.getX(i);
1009 float y = event.getY(i);
1010 if (x != 0 || y != 0) {
1011 out << ", x[" << i << "]=" << x;
1012 out << ", y[" << i << "]=" << y;
1013 }
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001014 ToolType toolType = event.getToolType(i);
1015 if (toolType != ToolType::FINGER) {
1016 out << ", toolType[" << i << "]=" << ftl::enum_string(toolType);
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +00001017 }
1018 }
1019 if (event.getButtonState() != 0) {
1020 out << ", buttonState=" << event.getButtonState();
1021 }
1022 if (event.getClassification() != MotionClassification::NONE) {
1023 out << ", classification=" << motionClassificationToString(event.getClassification());
1024 }
1025 if (event.getMetaState() != 0) {
1026 out << ", metaState=" << event.getMetaState();
1027 }
1028 if (event.getEdgeFlags() != 0) {
1029 out << ", edgeFlags=" << event.getEdgeFlags();
1030 }
1031 if (pointerCount != 1) {
1032 out << ", pointerCount=" << pointerCount;
1033 }
1034 if (event.getHistorySize() != 0) {
1035 out << ", historySize=" << event.getHistorySize();
1036 }
1037 out << ", eventTime=" << event.getEventTime();
1038 out << ", downTime=" << event.getDownTime();
1039 out << ", deviceId=" << event.getDeviceId();
1040 out << ", source=" << inputEventSourceToString(event.getSource());
1041 out << ", displayId=" << event.getDisplayId();
1042 out << ", eventId=" << event.getId();
1043 out << "}";
1044 return out;
1045}
1046
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001047// --- FocusEvent ---
1048
Antonio Kantek3cfec7b2021-11-05 18:26:17 -07001049void FocusEvent::initialize(int32_t id, bool hasFocus) {
Garfield Tan4cc839f2020-01-24 11:26:14 -08001050 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -06001051 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001052 mHasFocus = hasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001053}
1054
1055void FocusEvent::initialize(const FocusEvent& from) {
1056 InputEvent::initialize(from);
1057 mHasFocus = from.mHasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001058}
Jeff Brown5912f952013-07-01 19:10:31 -07001059
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001060// --- CaptureEvent ---
1061
1062void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) {
1063 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1064 ADISPLAY_ID_NONE, INVALID_HMAC);
1065 mPointerCaptureEnabled = pointerCaptureEnabled;
1066}
1067
1068void CaptureEvent::initialize(const CaptureEvent& from) {
1069 InputEvent::initialize(from);
1070 mPointerCaptureEnabled = from.mPointerCaptureEnabled;
1071}
1072
arthurhung7632c332020-12-30 16:58:01 +08001073// --- DragEvent ---
1074
1075void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) {
1076 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1077 ADISPLAY_ID_NONE, INVALID_HMAC);
1078 mIsExiting = isExiting;
1079 mX = x;
1080 mY = y;
1081}
1082
1083void DragEvent::initialize(const DragEvent& from) {
1084 InputEvent::initialize(from);
1085 mIsExiting = from.mIsExiting;
1086 mX = from.mX;
1087 mY = from.mY;
1088}
1089
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001090// --- TouchModeEvent ---
1091
1092void TouchModeEvent::initialize(int32_t id, bool isInTouchMode) {
1093 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1094 ADISPLAY_ID_NONE, INVALID_HMAC);
1095 mIsInTouchMode = isInTouchMode;
1096}
1097
1098void TouchModeEvent::initialize(const TouchModeEvent& from) {
1099 InputEvent::initialize(from);
1100 mIsInTouchMode = from.mIsInTouchMode;
1101}
1102
Jeff Brown5912f952013-07-01 19:10:31 -07001103// --- PooledInputEventFactory ---
1104
1105PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
1106 mMaxPoolSize(maxPoolSize) {
1107}
1108
1109PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -07001110}
1111
1112KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001113 if (mKeyEventPool.empty()) {
1114 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001115 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001116 KeyEvent* event = mKeyEventPool.front().release();
1117 mKeyEventPool.pop();
1118 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001119}
1120
1121MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001122 if (mMotionEventPool.empty()) {
1123 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001124 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001125 MotionEvent* event = mMotionEventPool.front().release();
1126 mMotionEventPool.pop();
1127 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001128}
1129
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001130FocusEvent* PooledInputEventFactory::createFocusEvent() {
1131 if (mFocusEventPool.empty()) {
1132 return new FocusEvent();
1133 }
1134 FocusEvent* event = mFocusEventPool.front().release();
1135 mFocusEventPool.pop();
1136 return event;
1137}
1138
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001139CaptureEvent* PooledInputEventFactory::createCaptureEvent() {
1140 if (mCaptureEventPool.empty()) {
1141 return new CaptureEvent();
1142 }
1143 CaptureEvent* event = mCaptureEventPool.front().release();
1144 mCaptureEventPool.pop();
1145 return event;
1146}
1147
arthurhung7632c332020-12-30 16:58:01 +08001148DragEvent* PooledInputEventFactory::createDragEvent() {
1149 if (mDragEventPool.empty()) {
1150 return new DragEvent();
1151 }
1152 DragEvent* event = mDragEventPool.front().release();
1153 mDragEventPool.pop();
1154 return event;
1155}
1156
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001157TouchModeEvent* PooledInputEventFactory::createTouchModeEvent() {
1158 if (mTouchModeEventPool.empty()) {
1159 return new TouchModeEvent();
1160 }
1161 TouchModeEvent* event = mTouchModeEventPool.front().release();
1162 mTouchModeEventPool.pop();
1163 return event;
1164}
1165
Jeff Brown5912f952013-07-01 19:10:31 -07001166void PooledInputEventFactory::recycle(InputEvent* event) {
1167 switch (event->getType()) {
1168 case AINPUT_EVENT_TYPE_KEY:
1169 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001170 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -07001171 return;
1172 }
1173 break;
1174 case AINPUT_EVENT_TYPE_MOTION:
1175 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001176 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -07001177 return;
1178 }
1179 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001180 case AINPUT_EVENT_TYPE_FOCUS:
1181 if (mFocusEventPool.size() < mMaxPoolSize) {
1182 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
1183 return;
1184 }
1185 break;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001186 case AINPUT_EVENT_TYPE_CAPTURE:
1187 if (mCaptureEventPool.size() < mMaxPoolSize) {
1188 mCaptureEventPool.push(
1189 std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event)));
1190 return;
1191 }
1192 break;
arthurhung7632c332020-12-30 16:58:01 +08001193 case AINPUT_EVENT_TYPE_DRAG:
1194 if (mDragEventPool.size() < mMaxPoolSize) {
1195 mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event)));
1196 return;
1197 }
1198 break;
Antonio Kantekeb4a30c2021-09-28 17:49:49 -07001199 case AINPUT_EVENT_TYPE_TOUCH_MODE:
1200 if (mTouchModeEventPool.size() < mMaxPoolSize) {
1201 mTouchModeEventPool.push(
1202 std::unique_ptr<TouchModeEvent>(static_cast<TouchModeEvent*>(event)));
1203 return;
1204 }
1205 break;
Jeff Brown5912f952013-07-01 19:10:31 -07001206 }
1207 delete event;
1208}
1209
1210} // namespace android