blob: c1274110d397f34d61abfe7c95c1d80bacd88d15 [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 Vishniakou54d3e182020-01-15 17:38:38 -0800220VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
221 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
222 event.getSource(), event.getDisplayId()},
223 event.getAction(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800224 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800225 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800226 event.getKeyCode(),
227 event.getScanCode(),
228 event.getMetaState(),
229 event.getRepeatCount()};
230}
231
232VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
233 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
234 event.getSource(), event.getDisplayId()},
235 event.getRawX(0),
236 event.getRawY(0),
237 event.getActionMasked(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800238 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800239 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800240 event.getMetaState(),
241 event.getButtonState()};
242}
243
Garfield Tan4cc839f2020-01-24 11:26:14 -0800244void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600245 std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800246 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700247 mDeviceId = deviceId;
248 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100249 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600250 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700251}
252
253void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800254 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700255 mDeviceId = from.mDeviceId;
256 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100257 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600258 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700259}
260
Garfield Tan4cc839f2020-01-24 11:26:14 -0800261int32_t InputEvent::nextId() {
262 static IdGenerator idGen(IdGenerator::Source::OTHER);
263 return idGen.nextId();
264}
265
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700266std::ostream& operator<<(std::ostream& out, const InputEvent& event) {
267 switch (event.getType()) {
268 case InputEventType::KEY: {
269 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event);
270 out << keyEvent;
271 return out;
272 }
273 case InputEventType::MOTION: {
274 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event);
275 out << motionEvent;
276 return out;
277 }
278 case InputEventType::FOCUS: {
279 out << "FocusEvent";
280 return out;
281 }
282 case InputEventType::CAPTURE: {
283 out << "CaptureEvent";
284 return out;
285 }
286 case InputEventType::DRAG: {
287 out << "DragEvent";
288 return out;
289 }
290 case InputEventType::TOUCH_MODE: {
291 out << "TouchModeEvent";
292 return out;
293 }
294 }
295}
296
Jeff Brown5912f952013-07-01 19:10:31 -0700297// --- KeyEvent ---
298
Michael Wright872db4f2014-04-22 15:03:51 -0700299const char* KeyEvent::getLabel(int32_t keyCode) {
Chris Ye4958d062020-08-20 13:21:10 -0700300 return InputEventLookup::getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700301}
302
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800303std::optional<int> KeyEvent::getKeyCodeFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700304 return InputEventLookup::getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700305}
306
Garfield Tan4cc839f2020-01-24 11:26:14 -0800307void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600308 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
309 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
310 nsecs_t downTime, nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800311 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700312 mAction = action;
313 mFlags = flags;
314 mKeyCode = keyCode;
315 mScanCode = scanCode;
316 mMetaState = metaState;
317 mRepeatCount = repeatCount;
318 mDownTime = downTime;
319 mEventTime = eventTime;
320}
321
322void KeyEvent::initialize(const KeyEvent& from) {
323 InputEvent::initialize(from);
324 mAction = from.mAction;
325 mFlags = from.mFlags;
326 mKeyCode = from.mKeyCode;
327 mScanCode = from.mScanCode;
328 mMetaState = from.mMetaState;
329 mRepeatCount = from.mRepeatCount;
330 mDownTime = from.mDownTime;
331 mEventTime = from.mEventTime;
332}
333
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700334const char* KeyEvent::actionToString(int32_t action) {
335 // Convert KeyEvent action to string
336 switch (action) {
337 case AKEY_EVENT_ACTION_DOWN:
338 return "DOWN";
339 case AKEY_EVENT_ACTION_UP:
340 return "UP";
341 case AKEY_EVENT_ACTION_MULTIPLE:
342 return "MULTIPLE";
343 }
344 return "UNKNOWN";
345}
Jeff Brown5912f952013-07-01 19:10:31 -0700346
Siarhei Vishniakoud010b012023-01-18 15:00:53 -0800347std::ostream& operator<<(std::ostream& out, const KeyEvent& event) {
348 out << "KeyEvent { action=" << KeyEvent::actionToString(event.getAction());
349
350 out << ", keycode=" << event.getKeyCode() << "(" << KeyEvent::getLabel(event.getKeyCode())
351 << ")";
352
353 if (event.getMetaState() != 0) {
354 out << ", metaState=" << event.getMetaState();
355 }
356
357 out << ", eventTime=" << event.getEventTime();
358 out << ", downTime=" << event.getDownTime();
359 out << ", flags=" << std::hex << event.getFlags() << std::dec;
360 out << ", repeatCount=" << event.getRepeatCount();
361 out << ", deviceId=" << event.getDeviceId();
362 out << ", source=" << inputEventSourceToString(event.getSource());
363 out << ", displayId=" << event.getDisplayId();
364 out << ", eventId=" << event.getId();
365 out << "}";
366 return out;
367}
368
Jeff Brown5912f952013-07-01 19:10:31 -0700369// --- PointerCoords ---
370
371float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700372 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700373 return 0;
374 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700375 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700376}
377
378status_t PointerCoords::setAxisValue(int32_t axis, float value) {
379 if (axis < 0 || axis > 63) {
380 return NAME_NOT_FOUND;
381 }
382
Michael Wright38dcdff2014-03-19 12:06:10 -0700383 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
384 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700385 if (value == 0) {
386 return OK; // axes with value 0 do not need to be stored
387 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700388
389 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700390 if (count >= MAX_AXES) {
391 tooManyAxes(axis);
392 return NO_MEMORY;
393 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700394 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700395 for (uint32_t i = count; i > index; i--) {
396 values[i] = values[i - 1];
397 }
398 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700399
Jeff Brown5912f952013-07-01 19:10:31 -0700400 values[index] = value;
401 return OK;
402}
403
404static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
405 float value = c.getAxisValue(axis);
406 if (value != 0) {
407 c.setAxisValue(axis, value * scaleFactor);
408 }
409}
410
Robert Carre07e1032018-11-26 12:55:53 -0800411void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700412 // No need to scale pressure or size since they are normalized.
413 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800414
415 // If there is a global scale factor, it is included in the windowX/YScale
416 // so we don't need to apply it twice to the X/Y axes.
417 // However we don't want to apply any windowXYScale not included in the global scale
418 // to the TOUCH_MAJOR/MINOR coordinates.
419 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
420 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
421 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
422 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
423 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
424 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
Prabir Pradhanc6523582021-05-14 18:02:55 -0700425 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_X, windowXScale);
426 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_Y, windowYScale);
Robert Carre07e1032018-11-26 12:55:53 -0800427}
428
Brett Chabotfaa986c2020-11-04 17:39:36 -0800429#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700430status_t PointerCoords::readFromParcel(Parcel* parcel) {
431 bits = parcel->readInt64();
432
Michael Wright38dcdff2014-03-19 12:06:10 -0700433 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700434 if (count > MAX_AXES) {
435 return BAD_VALUE;
436 }
437
438 for (uint32_t i = 0; i < count; i++) {
439 values[i] = parcel->readFloat();
440 }
Philip Quinnafb31282022-12-20 18:17:55 -0800441
442 isResampled = parcel->readBool();
Jeff Brown5912f952013-07-01 19:10:31 -0700443 return OK;
444}
445
446status_t PointerCoords::writeToParcel(Parcel* parcel) const {
447 parcel->writeInt64(bits);
448
Michael Wright38dcdff2014-03-19 12:06:10 -0700449 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700450 for (uint32_t i = 0; i < count; i++) {
451 parcel->writeFloat(values[i]);
452 }
Philip Quinnafb31282022-12-20 18:17:55 -0800453
454 parcel->writeBool(isResampled);
Jeff Brown5912f952013-07-01 19:10:31 -0700455 return OK;
456}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800457#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700458
459void PointerCoords::tooManyAxes(int axis) {
460 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
461 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
462}
463
464bool PointerCoords::operator==(const PointerCoords& other) const {
465 if (bits != other.bits) {
466 return false;
467 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700468 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700469 for (uint32_t i = 0; i < count; i++) {
470 if (values[i] != other.values[i]) {
471 return false;
472 }
473 }
Philip Quinnafb31282022-12-20 18:17:55 -0800474 if (isResampled != other.isResampled) {
475 return false;
476 }
Jeff Brown5912f952013-07-01 19:10:31 -0700477 return true;
478}
479
chaviwc01e1372020-07-01 12:37:31 -0700480void PointerCoords::transform(const ui::Transform& transform) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700481 const vec2 xy = transform.transform(getXYValue());
482 setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
483 setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
484
Prabir Pradhanc6523582021-05-14 18:02:55 -0700485 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_X) ||
486 BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_Y)) {
487 const ui::Transform rotation(transform.getOrientation());
488 const vec2 relativeXy = rotation.transform(getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
489 getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
490 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
491 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
492 }
493
Prabir Pradhan6b384612021-05-14 16:56:25 -0700494 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) {
495 const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
496 setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val));
497 }
chaviwc01e1372020-07-01 12:37:31 -0700498}
Jeff Brown5912f952013-07-01 19:10:31 -0700499
Jeff Brown5912f952013-07-01 19:10:31 -0700500// --- MotionEvent ---
501
Garfield Tan4cc839f2020-01-24 11:26:14 -0800502void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600503 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
504 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700505 int32_t buttonState, MotionClassification classification,
506 const ui::Transform& transform, float xPrecision, float yPrecision,
Evan Rosky84f07f02021-04-16 10:42:42 -0700507 float rawXCursorPosition, float rawYCursorPosition,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700508 const ui::Transform& rawTransform, nsecs_t downTime, nsecs_t eventTime,
Evan Rosky09576692021-07-01 12:22:09 -0700509 size_t pointerCount, const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700510 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800511 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700512 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100513 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700514 mFlags = flags;
515 mEdgeFlags = edgeFlags;
516 mMetaState = metaState;
517 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800518 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700519 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700520 mXPrecision = xPrecision;
521 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700522 mRawXCursorPosition = rawXCursorPosition;
523 mRawYCursorPosition = rawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700524 mRawTransform = rawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700525 mDownTime = downTime;
526 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800527 mPointerProperties.insert(mPointerProperties.end(), &pointerProperties[0],
528 &pointerProperties[pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700529 mSampleEventTimes.clear();
530 mSamplePointerCoords.clear();
531 addSample(eventTime, pointerCoords);
532}
533
534void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800535 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
536 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700537 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100538 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700539 mFlags = other->mFlags;
540 mEdgeFlags = other->mEdgeFlags;
541 mMetaState = other->mMetaState;
542 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800543 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700544 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700545 mXPrecision = other->mXPrecision;
546 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700547 mRawXCursorPosition = other->mRawXCursorPosition;
548 mRawYCursorPosition = other->mRawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700549 mRawTransform = other->mRawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700550 mDownTime = other->mDownTime;
551 mPointerProperties = other->mPointerProperties;
552
553 if (keepHistory) {
554 mSampleEventTimes = other->mSampleEventTimes;
555 mSamplePointerCoords = other->mSamplePointerCoords;
556 } else {
557 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500558 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700559 mSamplePointerCoords.clear();
560 size_t pointerCount = other->getPointerCount();
561 size_t historySize = other->getHistorySize();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800562 mSamplePointerCoords
563 .insert(mSamplePointerCoords.end(),
564 &other->mSamplePointerCoords[historySize * pointerCount],
565 &other->mSamplePointerCoords[historySize * pointerCount + pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700566 }
567}
568
569void MotionEvent::addSample(
570 int64_t eventTime,
571 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500572 mSampleEventTimes.push_back(eventTime);
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800573 mSamplePointerCoords.insert(mSamplePointerCoords.end(), &pointerCoords[0],
574 &pointerCoords[getPointerCount()]);
Jeff Brown5912f952013-07-01 19:10:31 -0700575}
576
Michael Wright635422b2022-12-02 00:43:56 +0000577std::optional<ui::Rotation> MotionEvent::getSurfaceRotation() const {
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800578 // The surface rotation is the rotation from the window's coordinate space to that of the
579 // display. Since the event's transform takes display space coordinates to window space, the
580 // returned surface rotation is the inverse of the rotation for the surface.
581 switch (mTransform.getOrientation()) {
582 case ui::Transform::ROT_0:
Michael Wright635422b2022-12-02 00:43:56 +0000583 return ui::ROTATION_0;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800584 case ui::Transform::ROT_90:
Michael Wright635422b2022-12-02 00:43:56 +0000585 return ui::ROTATION_270;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800586 case ui::Transform::ROT_180:
Michael Wright635422b2022-12-02 00:43:56 +0000587 return ui::ROTATION_180;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800588 case ui::Transform::ROT_270:
Michael Wright635422b2022-12-02 00:43:56 +0000589 return ui::ROTATION_90;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800590 default:
Michael Wright635422b2022-12-02 00:43:56 +0000591 return std::nullopt;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800592 }
593}
594
Garfield Tan00f511d2019-06-12 16:55:40 -0700595float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700596 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000597 return roundTransformedCoords(vals.x);
Garfield Tan00f511d2019-06-12 16:55:40 -0700598}
599
600float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700601 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000602 return roundTransformedCoords(vals.y);
Garfield Tan00f511d2019-06-12 16:55:40 -0700603}
604
Garfield Tan937bb832019-07-25 17:48:31 -0700605void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700606 ui::Transform inverse = mTransform.inverse();
607 vec2 vals = inverse.transform(x, y);
608 mRawXCursorPosition = vals.x;
609 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700610}
611
Jeff Brown5912f952013-07-01 19:10:31 -0700612const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000613 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
614 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
615 }
616 const size_t position = getHistorySize() * getPointerCount() + pointerIndex;
617 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
618 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
619 }
620 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700621}
622
623float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
Evan Rosky84f07f02021-04-16 10:42:42 -0700624 return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700625}
626
627float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700628 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700629}
630
631const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
632 size_t pointerIndex, size_t historicalIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000633 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
634 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
635 }
636 if (CC_UNLIKELY(historicalIndex < 0 || historicalIndex > getHistorySize())) {
637 LOG(FATAL) << __func__ << ": Invalid historical index " << historicalIndex << " for "
638 << *this;
639 }
640 const size_t position = historicalIndex * getPointerCount() + pointerIndex;
641 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
642 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
643 }
644 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700645}
646
647float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan6b384612021-05-14 16:56:25 -0700648 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700649 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
650 return calculateTransformedAxisValue(axis, mSource, mRawTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700651}
652
653float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan9f388812021-05-13 16:54:53 -0700654 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700655 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
656 return calculateTransformedAxisValue(axis, mSource, mTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700657}
658
659ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
660 size_t pointerCount = mPointerProperties.size();
661 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800662 if (mPointerProperties[i].id == pointerId) {
Jeff Brown5912f952013-07-01 19:10:31 -0700663 return i;
664 }
665 }
666 return -1;
667}
668
669void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700670 float currXOffset = mTransform.tx();
671 float currYOffset = mTransform.ty();
672 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700673}
674
Robert Carre07e1032018-11-26 12:55:53 -0800675void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700676 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700677 mRawTransform.set(mRawTransform.tx() * globalScaleFactor,
678 mRawTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800679 mXPrecision *= globalScaleFactor;
680 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700681
682 size_t numSamples = mSamplePointerCoords.size();
683 for (size_t i = 0; i < numSamples; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800684 mSamplePointerCoords[i].scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700685 }
686}
687
chaviw9eaa22c2020-07-01 16:21:27 -0700688void MotionEvent::transform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700689 // We want to preserve the raw axes values stored in the PointerCoords, so we just update the
690 // transform using the values passed in.
chaviw9eaa22c2020-07-01 16:21:27 -0700691 ui::Transform newTransform;
692 newTransform.set(matrix);
693 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700694}
695
Evan Roskyd4d4d802021-05-03 20:12:21 -0700696void MotionEvent::applyTransform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700697 ui::Transform transform;
698 transform.set(matrix);
Evan Roskyd4d4d802021-05-03 20:12:21 -0700699
700 // Apply the transformation to all samples.
Prabir Pradhan6b384612021-05-14 16:56:25 -0700701 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
702 [&transform](PointerCoords& c) { c.transform(transform); });
Prabir Pradhan4b19bd02021-06-01 17:34:59 -0700703
704 if (mRawXCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION &&
705 mRawYCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) {
706 const vec2 cursor = transform.transform(mRawXCursorPosition, mRawYCursorPosition);
707 mRawXCursorPosition = cursor.x;
708 mRawYCursorPosition = cursor.y;
709 }
Evan Roskyd4d4d802021-05-03 20:12:21 -0700710}
711
Brett Chabotfaa986c2020-11-04 17:39:36 -0800712#ifdef __linux__
chaviw9eaa22c2020-07-01 16:21:27 -0700713static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
714 float dsdx, dtdx, tx, dtdy, dsdy, ty;
715 status_t status = parcel.readFloat(&dsdx);
716 status |= parcel.readFloat(&dtdx);
717 status |= parcel.readFloat(&tx);
718 status |= parcel.readFloat(&dtdy);
719 status |= parcel.readFloat(&dsdy);
720 status |= parcel.readFloat(&ty);
721
722 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
723 return status;
724}
725
726static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
727 status_t status = parcel.writeFloat(transform.dsdx());
728 status |= parcel.writeFloat(transform.dtdx());
729 status |= parcel.writeFloat(transform.tx());
730 status |= parcel.writeFloat(transform.dtdy());
731 status |= parcel.writeFloat(transform.dsdy());
732 status |= parcel.writeFloat(transform.ty());
733 return status;
734}
735
Jeff Brown5912f952013-07-01 19:10:31 -0700736status_t MotionEvent::readFromParcel(Parcel* parcel) {
737 size_t pointerCount = parcel->readInt32();
738 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800739 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
740 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700741 return BAD_VALUE;
742 }
743
Garfield Tan4cc839f2020-01-24 11:26:14 -0800744 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700745 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600746 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800747 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600748 std::vector<uint8_t> hmac;
749 status_t result = parcel->readByteVector(&hmac);
750 if (result != OK || hmac.size() != 32) {
751 return BAD_VALUE;
752 }
753 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700754 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100755 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700756 mFlags = parcel->readInt32();
757 mEdgeFlags = parcel->readInt32();
758 mMetaState = parcel->readInt32();
759 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800760 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700761
762 result = android::readFromParcel(mTransform, *parcel);
763 if (result != OK) {
764 return result;
765 }
Jeff Brown5912f952013-07-01 19:10:31 -0700766 mXPrecision = parcel->readFloat();
767 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700768 mRawXCursorPosition = parcel->readFloat();
769 mRawYCursorPosition = parcel->readFloat();
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700770
771 result = android::readFromParcel(mRawTransform, *parcel);
772 if (result != OK) {
773 return result;
774 }
Jeff Brown5912f952013-07-01 19:10:31 -0700775 mDownTime = parcel->readInt64();
776
777 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800778 mPointerProperties.reserve(pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700779 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500780 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700781 mSamplePointerCoords.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800782 mSamplePointerCoords.reserve(sampleCount * pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700783
784 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800785 mPointerProperties.push_back({});
786 PointerProperties& properties = mPointerProperties.back();
Jeff Brown5912f952013-07-01 19:10:31 -0700787 properties.id = parcel->readInt32();
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700788 properties.toolType = static_cast<ToolType>(parcel->readInt32());
Jeff Brown5912f952013-07-01 19:10:31 -0700789 }
790
Dan Austinc94fc452015-09-22 14:22:41 -0700791 while (sampleCount > 0) {
792 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500793 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700794 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800795 mSamplePointerCoords.push_back({});
796 status_t status = mSamplePointerCoords.back().readFromParcel(parcel);
Jeff Brown5912f952013-07-01 19:10:31 -0700797 if (status) {
798 return status;
799 }
800 }
801 }
802 return OK;
803}
804
805status_t MotionEvent::writeToParcel(Parcel* parcel) const {
806 size_t pointerCount = mPointerProperties.size();
807 size_t sampleCount = mSampleEventTimes.size();
808
809 parcel->writeInt32(pointerCount);
810 parcel->writeInt32(sampleCount);
811
Garfield Tan4cc839f2020-01-24 11:26:14 -0800812 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700813 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600814 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800815 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600816 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
817 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700818 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100819 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700820 parcel->writeInt32(mFlags);
821 parcel->writeInt32(mEdgeFlags);
822 parcel->writeInt32(mMetaState);
823 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800824 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700825
826 status_t result = android::writeToParcel(mTransform, *parcel);
827 if (result != OK) {
828 return result;
829 }
Jeff Brown5912f952013-07-01 19:10:31 -0700830 parcel->writeFloat(mXPrecision);
831 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700832 parcel->writeFloat(mRawXCursorPosition);
833 parcel->writeFloat(mRawYCursorPosition);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700834
835 result = android::writeToParcel(mRawTransform, *parcel);
836 if (result != OK) {
837 return result;
838 }
Jeff Brown5912f952013-07-01 19:10:31 -0700839 parcel->writeInt64(mDownTime);
840
841 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800842 const PointerProperties& properties = mPointerProperties[i];
Jeff Brown5912f952013-07-01 19:10:31 -0700843 parcel->writeInt32(properties.id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700844 parcel->writeInt32(static_cast<int32_t>(properties.toolType));
Jeff Brown5912f952013-07-01 19:10:31 -0700845 }
846
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800847 const PointerCoords* pc = mSamplePointerCoords.data();
Jeff Brown5912f952013-07-01 19:10:31 -0700848 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500849 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700850 for (size_t i = 0; i < pointerCount; i++) {
851 status_t status = (pc++)->writeToParcel(parcel);
852 if (status) {
853 return status;
854 }
855 }
856 }
857 return OK;
858}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800859#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700860
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600861bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700862 if (isFromSource(source, AINPUT_SOURCE_CLASS_POINTER)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700863 // Specifically excludes HOVER_MOVE and SCROLL.
864 switch (action & AMOTION_EVENT_ACTION_MASK) {
865 case AMOTION_EVENT_ACTION_DOWN:
866 case AMOTION_EVENT_ACTION_MOVE:
867 case AMOTION_EVENT_ACTION_UP:
868 case AMOTION_EVENT_ACTION_POINTER_DOWN:
869 case AMOTION_EVENT_ACTION_POINTER_UP:
870 case AMOTION_EVENT_ACTION_CANCEL:
871 case AMOTION_EVENT_ACTION_OUTSIDE:
872 return true;
873 }
874 }
875 return false;
876}
877
Michael Wright872db4f2014-04-22 15:03:51 -0700878const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700879 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700880}
881
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800882std::optional<int> MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700883 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700884}
885
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500886std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700887 // Convert MotionEvent action to string
888 switch (action & AMOTION_EVENT_ACTION_MASK) {
889 case AMOTION_EVENT_ACTION_DOWN:
890 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700891 case AMOTION_EVENT_ACTION_UP:
892 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500893 case AMOTION_EVENT_ACTION_MOVE:
894 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700895 case AMOTION_EVENT_ACTION_CANCEL:
896 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500897 case AMOTION_EVENT_ACTION_OUTSIDE:
898 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700899 case AMOTION_EVENT_ACTION_POINTER_DOWN:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000900 return StringPrintf("POINTER_DOWN(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700901 case AMOTION_EVENT_ACTION_POINTER_UP:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000902 return StringPrintf("POINTER_UP(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500903 case AMOTION_EVENT_ACTION_HOVER_MOVE:
904 return "HOVER_MOVE";
905 case AMOTION_EVENT_ACTION_SCROLL:
906 return "SCROLL";
907 case AMOTION_EVENT_ACTION_HOVER_ENTER:
908 return "HOVER_ENTER";
909 case AMOTION_EVENT_ACTION_HOVER_EXIT:
910 return "HOVER_EXIT";
911 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
912 return "BUTTON_PRESS";
913 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
914 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700915 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500916 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700917}
918
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700919// Apply the given transformation to the point without checking whether the entire transform
920// should be disregarded altogether for the provided source.
921static inline vec2 calculateTransformedXYUnchecked(uint32_t source, const ui::Transform& transform,
922 const vec2& xy) {
923 return shouldDisregardOffset(source) ? transformWithoutTranslation(transform, xy)
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000924 : roundTransformedCoords(transform.transform(xy));
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700925}
926
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700927vec2 MotionEvent::calculateTransformedXY(uint32_t source, const ui::Transform& transform,
928 const vec2& xy) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700929 if (shouldDisregardTransformation(source)) {
930 return xy;
931 }
932 return calculateTransformedXYUnchecked(source, transform, xy);
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700933}
934
Prabir Pradhan8e6ce222022-02-24 09:08:54 -0800935// Keep in sync with calculateTransformedCoords.
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700936float MotionEvent::calculateTransformedAxisValue(int32_t axis, uint32_t source,
937 const ui::Transform& transform,
938 const PointerCoords& coords) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700939 if (shouldDisregardTransformation(source)) {
940 return coords.getAxisValue(axis);
941 }
942
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700943 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700944 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700945 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
946 return xy[axis];
947 }
948
949 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
950 const vec2 relativeXy =
951 transformWithoutTranslation(transform,
952 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
953 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
954 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
955 }
956
957 if (axis == AMOTION_EVENT_AXIS_ORIENTATION) {
958 return transformAngle(transform, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION));
959 }
960
961 return coords.getAxisValue(axis);
962}
963
Prabir Pradhan8e6ce222022-02-24 09:08:54 -0800964// Keep in sync with calculateTransformedAxisValue. This is an optimization of
965// calculateTransformedAxisValue for all PointerCoords axes.
966PointerCoords MotionEvent::calculateTransformedCoords(uint32_t source,
967 const ui::Transform& transform,
968 const PointerCoords& coords) {
969 if (shouldDisregardTransformation(source)) {
970 return coords;
971 }
972 PointerCoords out = coords;
973
974 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
975 out.setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
976 out.setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
977
978 const vec2 relativeXy =
979 transformWithoutTranslation(transform,
980 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
981 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
982 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
983 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
984
985 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
986 transformAngle(transform,
987 coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)));
988
989 return out;
990}
991
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000992std::ostream& operator<<(std::ostream& out, const MotionEvent& event) {
993 out << "MotionEvent { action=" << MotionEvent::actionToString(event.getAction());
994 if (event.getActionButton() != 0) {
995 out << ", actionButton=" << std::to_string(event.getActionButton());
996 }
997 const size_t pointerCount = event.getPointerCount();
hupeng3aa5a51a2022-09-02 16:00:18 +0800998 LOG_ALWAYS_FATAL_IF(pointerCount > MAX_POINTERS, "Too many pointers : pointerCount = %zu",
999 pointerCount);
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +00001000 for (size_t i = 0; i < pointerCount; i++) {
1001 out << ", id[" << i << "]=" << event.getPointerId(i);
1002 float x = event.getX(i);
1003 float y = event.getY(i);
1004 if (x != 0 || y != 0) {
1005 out << ", x[" << i << "]=" << x;
1006 out << ", y[" << i << "]=" << y;
1007 }
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001008 ToolType toolType = event.getToolType(i);
1009 if (toolType != ToolType::FINGER) {
1010 out << ", toolType[" << i << "]=" << ftl::enum_string(toolType);
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +00001011 }
1012 }
1013 if (event.getButtonState() != 0) {
1014 out << ", buttonState=" << event.getButtonState();
1015 }
1016 if (event.getClassification() != MotionClassification::NONE) {
1017 out << ", classification=" << motionClassificationToString(event.getClassification());
1018 }
1019 if (event.getMetaState() != 0) {
1020 out << ", metaState=" << event.getMetaState();
1021 }
1022 if (event.getEdgeFlags() != 0) {
1023 out << ", edgeFlags=" << event.getEdgeFlags();
1024 }
1025 if (pointerCount != 1) {
1026 out << ", pointerCount=" << pointerCount;
1027 }
1028 if (event.getHistorySize() != 0) {
1029 out << ", historySize=" << event.getHistorySize();
1030 }
1031 out << ", eventTime=" << event.getEventTime();
1032 out << ", downTime=" << event.getDownTime();
1033 out << ", deviceId=" << event.getDeviceId();
1034 out << ", source=" << inputEventSourceToString(event.getSource());
1035 out << ", displayId=" << event.getDisplayId();
1036 out << ", eventId=" << event.getId();
1037 out << "}";
1038 return out;
1039}
1040
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001041// --- FocusEvent ---
1042
Antonio Kantek3cfec7b2021-11-05 18:26:17 -07001043void FocusEvent::initialize(int32_t id, bool hasFocus) {
Garfield Tan4cc839f2020-01-24 11:26:14 -08001044 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -06001045 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001046 mHasFocus = hasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001047}
1048
1049void FocusEvent::initialize(const FocusEvent& from) {
1050 InputEvent::initialize(from);
1051 mHasFocus = from.mHasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001052}
Jeff Brown5912f952013-07-01 19:10:31 -07001053
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001054// --- CaptureEvent ---
1055
1056void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) {
1057 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1058 ADISPLAY_ID_NONE, INVALID_HMAC);
1059 mPointerCaptureEnabled = pointerCaptureEnabled;
1060}
1061
1062void CaptureEvent::initialize(const CaptureEvent& from) {
1063 InputEvent::initialize(from);
1064 mPointerCaptureEnabled = from.mPointerCaptureEnabled;
1065}
1066
arthurhung7632c332020-12-30 16:58:01 +08001067// --- DragEvent ---
1068
1069void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) {
1070 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1071 ADISPLAY_ID_NONE, INVALID_HMAC);
1072 mIsExiting = isExiting;
1073 mX = x;
1074 mY = y;
1075}
1076
1077void DragEvent::initialize(const DragEvent& from) {
1078 InputEvent::initialize(from);
1079 mIsExiting = from.mIsExiting;
1080 mX = from.mX;
1081 mY = from.mY;
1082}
1083
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001084// --- TouchModeEvent ---
1085
1086void TouchModeEvent::initialize(int32_t id, bool isInTouchMode) {
1087 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1088 ADISPLAY_ID_NONE, INVALID_HMAC);
1089 mIsInTouchMode = isInTouchMode;
1090}
1091
1092void TouchModeEvent::initialize(const TouchModeEvent& from) {
1093 InputEvent::initialize(from);
1094 mIsInTouchMode = from.mIsInTouchMode;
1095}
1096
Jeff Brown5912f952013-07-01 19:10:31 -07001097// --- PooledInputEventFactory ---
1098
1099PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
1100 mMaxPoolSize(maxPoolSize) {
1101}
1102
1103PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -07001104}
1105
1106KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001107 if (mKeyEventPool.empty()) {
1108 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001109 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001110 KeyEvent* event = mKeyEventPool.front().release();
1111 mKeyEventPool.pop();
1112 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001113}
1114
1115MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001116 if (mMotionEventPool.empty()) {
1117 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001118 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001119 MotionEvent* event = mMotionEventPool.front().release();
1120 mMotionEventPool.pop();
1121 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001122}
1123
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001124FocusEvent* PooledInputEventFactory::createFocusEvent() {
1125 if (mFocusEventPool.empty()) {
1126 return new FocusEvent();
1127 }
1128 FocusEvent* event = mFocusEventPool.front().release();
1129 mFocusEventPool.pop();
1130 return event;
1131}
1132
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001133CaptureEvent* PooledInputEventFactory::createCaptureEvent() {
1134 if (mCaptureEventPool.empty()) {
1135 return new CaptureEvent();
1136 }
1137 CaptureEvent* event = mCaptureEventPool.front().release();
1138 mCaptureEventPool.pop();
1139 return event;
1140}
1141
arthurhung7632c332020-12-30 16:58:01 +08001142DragEvent* PooledInputEventFactory::createDragEvent() {
1143 if (mDragEventPool.empty()) {
1144 return new DragEvent();
1145 }
1146 DragEvent* event = mDragEventPool.front().release();
1147 mDragEventPool.pop();
1148 return event;
1149}
1150
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001151TouchModeEvent* PooledInputEventFactory::createTouchModeEvent() {
1152 if (mTouchModeEventPool.empty()) {
1153 return new TouchModeEvent();
1154 }
1155 TouchModeEvent* event = mTouchModeEventPool.front().release();
1156 mTouchModeEventPool.pop();
1157 return event;
1158}
1159
Jeff Brown5912f952013-07-01 19:10:31 -07001160void PooledInputEventFactory::recycle(InputEvent* event) {
1161 switch (event->getType()) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001162 case InputEventType::KEY: {
1163 if (mKeyEventPool.size() < mMaxPoolSize) {
1164 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
1165 return;
1166 }
1167 break;
Jeff Brown5912f952013-07-01 19:10:31 -07001168 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001169 case InputEventType::MOTION: {
1170 if (mMotionEventPool.size() < mMaxPoolSize) {
1171 mMotionEventPool.push(
1172 std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
1173 return;
1174 }
1175 break;
Jeff Brown5912f952013-07-01 19:10:31 -07001176 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001177 case InputEventType::FOCUS: {
1178 if (mFocusEventPool.size() < mMaxPoolSize) {
1179 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
1180 return;
1181 }
1182 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001183 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001184 case InputEventType::CAPTURE: {
1185 if (mCaptureEventPool.size() < mMaxPoolSize) {
1186 mCaptureEventPool.push(
1187 std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event)));
1188 return;
1189 }
1190 break;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001191 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001192 case InputEventType::DRAG: {
1193 if (mDragEventPool.size() < mMaxPoolSize) {
1194 mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event)));
1195 return;
1196 }
1197 break;
arthurhung7632c332020-12-30 16:58:01 +08001198 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001199 case InputEventType::TOUCH_MODE: {
1200 if (mTouchModeEventPool.size() < mMaxPoolSize) {
1201 mTouchModeEventPool.push(
1202 std::unique_ptr<TouchModeEvent>(static_cast<TouchModeEvent*>(event)));
1203 return;
1204 }
1205 break;
Antonio Kantekeb4a30c2021-09-28 17:49:49 -07001206 }
Jeff Brown5912f952013-07-01 19:10:31 -07001207 }
1208 delete event;
1209}
1210
1211} // namespace android