blob: 53b22cb883343e1de51c6e4ea7926c49bd8a7d20 [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
Siarhei Vishniakoud5fe5182022-07-20 23:28:40 +000082const char* motionToolTypeToString(int32_t toolType) {
83 switch (toolType) {
84 case AMOTION_EVENT_TOOL_TYPE_UNKNOWN:
85 return "UNKNOWN";
86 case AMOTION_EVENT_TOOL_TYPE_FINGER:
87 return "FINGER";
88 case AMOTION_EVENT_TOOL_TYPE_STYLUS:
89 return "STYLUS";
90 case AMOTION_EVENT_TOOL_TYPE_MOUSE:
91 return "MOUSE";
92 case AMOTION_EVENT_TOOL_TYPE_ERASER:
93 return "ERASER";
94 case AMOTION_EVENT_TOOL_TYPE_PALM:
95 return "PALM";
96 default:
97 return "INVALID";
98 }
99}
100
Garfield Tan84b087e2020-01-23 10:49:05 -0800101// --- IdGenerator ---
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700102#if defined(__ANDROID__)
103[[maybe_unused]]
104#endif
105static status_t
106getRandomBytes(uint8_t* data, size_t size) {
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700107 int ret = TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY | O_CLOEXEC | O_NOFOLLOW));
108 if (ret == -1) {
109 return -errno;
110 }
111
112 base::unique_fd fd(ret);
113 if (!base::ReadFully(fd, data, size)) {
114 return -errno;
115 }
116 return OK;
117}
118
Garfield Tan84b087e2020-01-23 10:49:05 -0800119IdGenerator::IdGenerator(Source source) : mSource(source) {}
120
121int32_t IdGenerator::nextId() const {
122 constexpr uint32_t SEQUENCE_NUMBER_MASK = ~SOURCE_MASK;
123 int32_t id = 0;
124
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700125#if defined(__ANDROID__)
126 // On device, prefer 'getrandom' to '/dev/urandom' because it's faster.
127 constexpr size_t BUF_LEN = sizeof(id);
128 size_t totalBytes = 0;
129 while (totalBytes < BUF_LEN) {
130 ssize_t bytes = TEMP_FAILURE_RETRY(getrandom(&id, BUF_LEN, GRND_NONBLOCK));
131 if (CC_UNLIKELY(bytes < 0)) {
132 ALOGW("Failed to fill in random number for sequence number: %s.", strerror(errno));
133 id = 0;
134 break;
135 }
136 totalBytes += bytes;
137 }
138#else
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700139#if defined(__linux__)
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700140 // On host, <sys/random.h> / GRND_NONBLOCK is not available
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700141 while (true) {
142 status_t result = getRandomBytes(reinterpret_cast<uint8_t*>(&id), sizeof(id));
143 if (result == OK) {
Garfield Tan84b087e2020-01-23 10:49:05 -0800144 break;
145 }
Garfield Tan84b087e2020-01-23 10:49:05 -0800146 }
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700147#endif // __linux__
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700148#endif // __ANDROID__
Garfield Tan84b087e2020-01-23 10:49:05 -0800149 return (id & SEQUENCE_NUMBER_MASK) | static_cast<int32_t>(mSource);
150}
151
Jeff Brown5912f952013-07-01 19:10:31 -0700152// --- InputEvent ---
153
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000154// Due to precision limitations when working with floating points, transforming - namely
155// scaling - floating points can lead to minute errors. We round transformed values to approximately
156// three decimal places so that values like 0.99997 show up as 1.0.
157inline float roundTransformedCoords(float val) {
158 // Use a power to two to approximate three decimal places to potentially reduce some cycles.
159 // This should be at least as precise as MotionEvent::ROUNDING_PRECISION.
160 return std::round(val * 1024.f) / 1024.f;
161}
162
163inline vec2 roundTransformedCoords(vec2 p) {
164 return {roundTransformedCoords(p.x), roundTransformedCoords(p.y)};
165}
166
Prabir Pradhande69f8a2021-11-18 16:40:34 +0000167vec2 transformWithoutTranslation(const ui::Transform& transform, const vec2& xy) {
168 const vec2 transformedXy = transform.transform(xy);
169 const vec2 transformedOrigin = transform.transform(0, 0);
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000170 return roundTransformedCoords(transformedXy - transformedOrigin);
Prabir Pradhande69f8a2021-11-18 16:40:34 +0000171}
172
Prabir Pradhane2e10b42022-11-17 20:59:36 +0000173float transformAngle(const ui::Transform& transform, float angleRadians) {
174 // Construct and transform a vector oriented at the specified clockwise angle from vertical.
175 // Coordinate system: down is increasing Y, right is increasing X.
176 float x = sinf(angleRadians);
177 float y = -cosf(angleRadians);
178 vec2 transformedPoint = transform.transform(x, y);
179
180 // Determine how the origin is transformed by the matrix so that we
181 // can transform orientation vectors.
182 const vec2 origin = transform.transform(0, 0);
183
184 transformedPoint.x -= origin.x;
185 transformedPoint.y -= origin.y;
186
187 // Derive the transformed vector's clockwise angle from vertical.
188 // The return value of atan2f is in range [-pi, pi] which conforms to the orientation API.
189 return atan2f(transformedPoint.x, -transformedPoint.y);
190}
191
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800192const char* inputEventTypeToString(int32_t type) {
193 switch (type) {
194 case AINPUT_EVENT_TYPE_KEY: {
195 return "KEY";
196 }
197 case AINPUT_EVENT_TYPE_MOTION: {
198 return "MOTION";
199 }
200 case AINPUT_EVENT_TYPE_FOCUS: {
201 return "FOCUS";
202 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800203 case AINPUT_EVENT_TYPE_CAPTURE: {
204 return "CAPTURE";
205 }
arthurhung7632c332020-12-30 16:58:01 +0800206 case AINPUT_EVENT_TYPE_DRAG: {
207 return "DRAG";
208 }
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700209 case AINPUT_EVENT_TYPE_TOUCH_MODE: {
210 return "TOUCH_MODE";
211 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800212 }
213 return "UNKNOWN";
214}
215
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800216std::string inputEventSourceToString(int32_t source) {
217 if (source == AINPUT_SOURCE_UNKNOWN) {
218 return "UNKNOWN";
219 }
220 if (source == static_cast<int32_t>(AINPUT_SOURCE_ANY)) {
221 return "ANY";
222 }
223 static const std::map<int32_t, const char*> SOURCES{
224 {AINPUT_SOURCE_KEYBOARD, "KEYBOARD"},
225 {AINPUT_SOURCE_DPAD, "DPAD"},
226 {AINPUT_SOURCE_GAMEPAD, "GAMEPAD"},
227 {AINPUT_SOURCE_TOUCHSCREEN, "TOUCHSCREEN"},
228 {AINPUT_SOURCE_MOUSE, "MOUSE"},
229 {AINPUT_SOURCE_STYLUS, "STYLUS"},
230 {AINPUT_SOURCE_BLUETOOTH_STYLUS, "BLUETOOTH_STYLUS"},
231 {AINPUT_SOURCE_TRACKBALL, "TRACKBALL"},
232 {AINPUT_SOURCE_MOUSE_RELATIVE, "MOUSE_RELATIVE"},
233 {AINPUT_SOURCE_TOUCHPAD, "TOUCHPAD"},
234 {AINPUT_SOURCE_TOUCH_NAVIGATION, "TOUCH_NAVIGATION"},
235 {AINPUT_SOURCE_JOYSTICK, "JOYSTICK"},
236 {AINPUT_SOURCE_HDMI, "HDMI"},
237 {AINPUT_SOURCE_SENSOR, "SENSOR"},
238 {AINPUT_SOURCE_ROTARY_ENCODER, "ROTARY_ENCODER"},
239 };
240 std::string result;
241 for (const auto& [source_entry, str] : SOURCES) {
242 if ((source & source_entry) == source_entry) {
243 if (!result.empty()) {
244 result += " | ";
245 }
246 result += str;
247 }
248 }
249 if (result.empty()) {
250 result = StringPrintf("0x%08x", source);
251 }
252 return result;
253}
254
255bool isFromSource(uint32_t source, uint32_t test) {
256 return (source & test) == test;
257}
258
Prabir Pradhane5626962022-10-27 20:30:53 +0000259bool isStylusToolType(uint32_t toolType) {
260 return toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS || toolType == AMOTION_EVENT_TOOL_TYPE_ERASER;
261}
262
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800263VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
264 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
265 event.getSource(), event.getDisplayId()},
266 event.getAction(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800267 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800268 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800269 event.getKeyCode(),
270 event.getScanCode(),
271 event.getMetaState(),
272 event.getRepeatCount()};
273}
274
275VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
276 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
277 event.getSource(), event.getDisplayId()},
278 event.getRawX(0),
279 event.getRawY(0),
280 event.getActionMasked(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800281 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800282 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800283 event.getMetaState(),
284 event.getButtonState()};
285}
286
Garfield Tan4cc839f2020-01-24 11:26:14 -0800287void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600288 std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800289 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700290 mDeviceId = deviceId;
291 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100292 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600293 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700294}
295
296void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800297 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700298 mDeviceId = from.mDeviceId;
299 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100300 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600301 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700302}
303
Garfield Tan4cc839f2020-01-24 11:26:14 -0800304int32_t InputEvent::nextId() {
305 static IdGenerator idGen(IdGenerator::Source::OTHER);
306 return idGen.nextId();
307}
308
Jeff Brown5912f952013-07-01 19:10:31 -0700309// --- KeyEvent ---
310
Michael Wright872db4f2014-04-22 15:03:51 -0700311const char* KeyEvent::getLabel(int32_t keyCode) {
Chris Ye4958d062020-08-20 13:21:10 -0700312 return InputEventLookup::getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700313}
314
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800315std::optional<int> KeyEvent::getKeyCodeFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700316 return InputEventLookup::getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700317}
318
Garfield Tan4cc839f2020-01-24 11:26:14 -0800319void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600320 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
321 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
322 nsecs_t downTime, nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800323 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700324 mAction = action;
325 mFlags = flags;
326 mKeyCode = keyCode;
327 mScanCode = scanCode;
328 mMetaState = metaState;
329 mRepeatCount = repeatCount;
330 mDownTime = downTime;
331 mEventTime = eventTime;
332}
333
334void KeyEvent::initialize(const KeyEvent& from) {
335 InputEvent::initialize(from);
336 mAction = from.mAction;
337 mFlags = from.mFlags;
338 mKeyCode = from.mKeyCode;
339 mScanCode = from.mScanCode;
340 mMetaState = from.mMetaState;
341 mRepeatCount = from.mRepeatCount;
342 mDownTime = from.mDownTime;
343 mEventTime = from.mEventTime;
344}
345
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700346const char* KeyEvent::actionToString(int32_t action) {
347 // Convert KeyEvent action to string
348 switch (action) {
349 case AKEY_EVENT_ACTION_DOWN:
350 return "DOWN";
351 case AKEY_EVENT_ACTION_UP:
352 return "UP";
353 case AKEY_EVENT_ACTION_MULTIPLE:
354 return "MULTIPLE";
355 }
356 return "UNKNOWN";
357}
Jeff Brown5912f952013-07-01 19:10:31 -0700358
Siarhei Vishniakoud010b012023-01-18 15:00:53 -0800359std::ostream& operator<<(std::ostream& out, const KeyEvent& event) {
360 out << "KeyEvent { action=" << KeyEvent::actionToString(event.getAction());
361
362 out << ", keycode=" << event.getKeyCode() << "(" << KeyEvent::getLabel(event.getKeyCode())
363 << ")";
364
365 if (event.getMetaState() != 0) {
366 out << ", metaState=" << event.getMetaState();
367 }
368
369 out << ", eventTime=" << event.getEventTime();
370 out << ", downTime=" << event.getDownTime();
371 out << ", flags=" << std::hex << event.getFlags() << std::dec;
372 out << ", repeatCount=" << event.getRepeatCount();
373 out << ", deviceId=" << event.getDeviceId();
374 out << ", source=" << inputEventSourceToString(event.getSource());
375 out << ", displayId=" << event.getDisplayId();
376 out << ", eventId=" << event.getId();
377 out << "}";
378 return out;
379}
380
Jeff Brown5912f952013-07-01 19:10:31 -0700381// --- PointerCoords ---
382
383float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700384 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700385 return 0;
386 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700387 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700388}
389
390status_t PointerCoords::setAxisValue(int32_t axis, float value) {
391 if (axis < 0 || axis > 63) {
392 return NAME_NOT_FOUND;
393 }
394
Michael Wright38dcdff2014-03-19 12:06:10 -0700395 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
396 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700397 if (value == 0) {
398 return OK; // axes with value 0 do not need to be stored
399 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700400
401 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700402 if (count >= MAX_AXES) {
403 tooManyAxes(axis);
404 return NO_MEMORY;
405 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700406 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700407 for (uint32_t i = count; i > index; i--) {
408 values[i] = values[i - 1];
409 }
410 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700411
Jeff Brown5912f952013-07-01 19:10:31 -0700412 values[index] = value;
413 return OK;
414}
415
416static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
417 float value = c.getAxisValue(axis);
418 if (value != 0) {
419 c.setAxisValue(axis, value * scaleFactor);
420 }
421}
422
Robert Carre07e1032018-11-26 12:55:53 -0800423void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700424 // No need to scale pressure or size since they are normalized.
425 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800426
427 // If there is a global scale factor, it is included in the windowX/YScale
428 // so we don't need to apply it twice to the X/Y axes.
429 // However we don't want to apply any windowXYScale not included in the global scale
430 // to the TOUCH_MAJOR/MINOR coordinates.
431 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
432 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
433 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
434 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
435 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
436 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
Prabir Pradhanc6523582021-05-14 18:02:55 -0700437 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_X, windowXScale);
438 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_Y, windowYScale);
Robert Carre07e1032018-11-26 12:55:53 -0800439}
440
Brett Chabotfaa986c2020-11-04 17:39:36 -0800441#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700442status_t PointerCoords::readFromParcel(Parcel* parcel) {
443 bits = parcel->readInt64();
444
Michael Wright38dcdff2014-03-19 12:06:10 -0700445 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700446 if (count > MAX_AXES) {
447 return BAD_VALUE;
448 }
449
450 for (uint32_t i = 0; i < count; i++) {
451 values[i] = parcel->readFloat();
452 }
Philip Quinnafb31282022-12-20 18:17:55 -0800453
454 isResampled = parcel->readBool();
Jeff Brown5912f952013-07-01 19:10:31 -0700455 return OK;
456}
457
458status_t PointerCoords::writeToParcel(Parcel* parcel) const {
459 parcel->writeInt64(bits);
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 parcel->writeFloat(values[i]);
464 }
Philip Quinnafb31282022-12-20 18:17:55 -0800465
466 parcel->writeBool(isResampled);
Jeff Brown5912f952013-07-01 19:10:31 -0700467 return OK;
468}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800469#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700470
471void PointerCoords::tooManyAxes(int axis) {
472 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
473 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
474}
475
476bool PointerCoords::operator==(const PointerCoords& other) const {
477 if (bits != other.bits) {
478 return false;
479 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700480 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700481 for (uint32_t i = 0; i < count; i++) {
482 if (values[i] != other.values[i]) {
483 return false;
484 }
485 }
Philip Quinnafb31282022-12-20 18:17:55 -0800486 if (isResampled != other.isResampled) {
487 return false;
488 }
Jeff Brown5912f952013-07-01 19:10:31 -0700489 return true;
490}
491
chaviwc01e1372020-07-01 12:37:31 -0700492void PointerCoords::transform(const ui::Transform& transform) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700493 const vec2 xy = transform.transform(getXYValue());
494 setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
495 setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
496
Prabir Pradhanc6523582021-05-14 18:02:55 -0700497 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_X) ||
498 BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_Y)) {
499 const ui::Transform rotation(transform.getOrientation());
500 const vec2 relativeXy = rotation.transform(getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
501 getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
502 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
503 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
504 }
505
Prabir Pradhan6b384612021-05-14 16:56:25 -0700506 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) {
507 const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
508 setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val));
509 }
chaviwc01e1372020-07-01 12:37:31 -0700510}
Jeff Brown5912f952013-07-01 19:10:31 -0700511
512// --- PointerProperties ---
513
514bool PointerProperties::operator==(const PointerProperties& other) const {
515 return id == other.id
516 && toolType == other.toolType;
517}
518
519void PointerProperties::copyFrom(const PointerProperties& other) {
520 id = other.id;
521 toolType = other.toolType;
522}
523
524
525// --- MotionEvent ---
526
Garfield Tan4cc839f2020-01-24 11:26:14 -0800527void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600528 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
529 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700530 int32_t buttonState, MotionClassification classification,
531 const ui::Transform& transform, float xPrecision, float yPrecision,
Evan Rosky84f07f02021-04-16 10:42:42 -0700532 float rawXCursorPosition, float rawYCursorPosition,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700533 const ui::Transform& rawTransform, nsecs_t downTime, nsecs_t eventTime,
Evan Rosky09576692021-07-01 12:22:09 -0700534 size_t pointerCount, const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700535 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800536 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700537 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100538 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700539 mFlags = flags;
540 mEdgeFlags = edgeFlags;
541 mMetaState = metaState;
542 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800543 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700544 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700545 mXPrecision = xPrecision;
546 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700547 mRawXCursorPosition = rawXCursorPosition;
548 mRawYCursorPosition = rawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700549 mRawTransform = rawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700550 mDownTime = downTime;
551 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800552 mPointerProperties.insert(mPointerProperties.end(), &pointerProperties[0],
553 &pointerProperties[pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700554 mSampleEventTimes.clear();
555 mSamplePointerCoords.clear();
556 addSample(eventTime, pointerCoords);
557}
558
559void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800560 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
561 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700562 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100563 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700564 mFlags = other->mFlags;
565 mEdgeFlags = other->mEdgeFlags;
566 mMetaState = other->mMetaState;
567 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800568 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700569 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700570 mXPrecision = other->mXPrecision;
571 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700572 mRawXCursorPosition = other->mRawXCursorPosition;
573 mRawYCursorPosition = other->mRawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700574 mRawTransform = other->mRawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700575 mDownTime = other->mDownTime;
576 mPointerProperties = other->mPointerProperties;
577
578 if (keepHistory) {
579 mSampleEventTimes = other->mSampleEventTimes;
580 mSamplePointerCoords = other->mSamplePointerCoords;
581 } else {
582 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500583 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700584 mSamplePointerCoords.clear();
585 size_t pointerCount = other->getPointerCount();
586 size_t historySize = other->getHistorySize();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800587 mSamplePointerCoords
588 .insert(mSamplePointerCoords.end(),
589 &other->mSamplePointerCoords[historySize * pointerCount],
590 &other->mSamplePointerCoords[historySize * pointerCount + pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700591 }
592}
593
594void MotionEvent::addSample(
595 int64_t eventTime,
596 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500597 mSampleEventTimes.push_back(eventTime);
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800598 mSamplePointerCoords.insert(mSamplePointerCoords.end(), &pointerCoords[0],
599 &pointerCoords[getPointerCount()]);
Jeff Brown5912f952013-07-01 19:10:31 -0700600}
601
Michael Wright635422b2022-12-02 00:43:56 +0000602std::optional<ui::Rotation> MotionEvent::getSurfaceRotation() const {
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800603 // The surface rotation is the rotation from the window's coordinate space to that of the
604 // display. Since the event's transform takes display space coordinates to window space, the
605 // returned surface rotation is the inverse of the rotation for the surface.
606 switch (mTransform.getOrientation()) {
607 case ui::Transform::ROT_0:
Michael Wright635422b2022-12-02 00:43:56 +0000608 return ui::ROTATION_0;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800609 case ui::Transform::ROT_90:
Michael Wright635422b2022-12-02 00:43:56 +0000610 return ui::ROTATION_270;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800611 case ui::Transform::ROT_180:
Michael Wright635422b2022-12-02 00:43:56 +0000612 return ui::ROTATION_180;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800613 case ui::Transform::ROT_270:
Michael Wright635422b2022-12-02 00:43:56 +0000614 return ui::ROTATION_90;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800615 default:
Michael Wright635422b2022-12-02 00:43:56 +0000616 return std::nullopt;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800617 }
618}
619
Garfield Tan00f511d2019-06-12 16:55:40 -0700620float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700621 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000622 return roundTransformedCoords(vals.x);
Garfield Tan00f511d2019-06-12 16:55:40 -0700623}
624
625float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700626 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000627 return roundTransformedCoords(vals.y);
Garfield Tan00f511d2019-06-12 16:55:40 -0700628}
629
Garfield Tan937bb832019-07-25 17:48:31 -0700630void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700631 ui::Transform inverse = mTransform.inverse();
632 vec2 vals = inverse.transform(x, y);
633 mRawXCursorPosition = vals.x;
634 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700635}
636
Jeff Brown5912f952013-07-01 19:10:31 -0700637const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000638 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
639 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
640 }
641 const size_t position = getHistorySize() * getPointerCount() + pointerIndex;
642 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
643 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
644 }
645 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700646}
647
648float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
Evan Rosky84f07f02021-04-16 10:42:42 -0700649 return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700650}
651
652float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700653 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700654}
655
656const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
657 size_t pointerIndex, size_t historicalIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000658 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
659 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
660 }
661 if (CC_UNLIKELY(historicalIndex < 0 || historicalIndex > getHistorySize())) {
662 LOG(FATAL) << __func__ << ": Invalid historical index " << historicalIndex << " for "
663 << *this;
664 }
665 const size_t position = historicalIndex * getPointerCount() + pointerIndex;
666 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
667 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
668 }
669 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700670}
671
672float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan6b384612021-05-14 16:56:25 -0700673 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700674 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
675 return calculateTransformedAxisValue(axis, mSource, mRawTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700676}
677
678float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan9f388812021-05-13 16:54:53 -0700679 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700680 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
681 return calculateTransformedAxisValue(axis, mSource, mTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700682}
683
684ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
685 size_t pointerCount = mPointerProperties.size();
686 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800687 if (mPointerProperties[i].id == pointerId) {
Jeff Brown5912f952013-07-01 19:10:31 -0700688 return i;
689 }
690 }
691 return -1;
692}
693
694void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700695 float currXOffset = mTransform.tx();
696 float currYOffset = mTransform.ty();
697 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700698}
699
Robert Carre07e1032018-11-26 12:55:53 -0800700void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700701 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700702 mRawTransform.set(mRawTransform.tx() * globalScaleFactor,
703 mRawTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800704 mXPrecision *= globalScaleFactor;
705 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700706
707 size_t numSamples = mSamplePointerCoords.size();
708 for (size_t i = 0; i < numSamples; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800709 mSamplePointerCoords[i].scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700710 }
711}
712
chaviw9eaa22c2020-07-01 16:21:27 -0700713void MotionEvent::transform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700714 // We want to preserve the raw axes values stored in the PointerCoords, so we just update the
715 // transform using the values passed in.
chaviw9eaa22c2020-07-01 16:21:27 -0700716 ui::Transform newTransform;
717 newTransform.set(matrix);
718 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700719}
720
Evan Roskyd4d4d802021-05-03 20:12:21 -0700721void MotionEvent::applyTransform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700722 ui::Transform transform;
723 transform.set(matrix);
Evan Roskyd4d4d802021-05-03 20:12:21 -0700724
725 // Apply the transformation to all samples.
Prabir Pradhan6b384612021-05-14 16:56:25 -0700726 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
727 [&transform](PointerCoords& c) { c.transform(transform); });
Prabir Pradhan4b19bd02021-06-01 17:34:59 -0700728
729 if (mRawXCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION &&
730 mRawYCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) {
731 const vec2 cursor = transform.transform(mRawXCursorPosition, mRawYCursorPosition);
732 mRawXCursorPosition = cursor.x;
733 mRawYCursorPosition = cursor.y;
734 }
Evan Roskyd4d4d802021-05-03 20:12:21 -0700735}
736
Brett Chabotfaa986c2020-11-04 17:39:36 -0800737#ifdef __linux__
chaviw9eaa22c2020-07-01 16:21:27 -0700738static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
739 float dsdx, dtdx, tx, dtdy, dsdy, ty;
740 status_t status = parcel.readFloat(&dsdx);
741 status |= parcel.readFloat(&dtdx);
742 status |= parcel.readFloat(&tx);
743 status |= parcel.readFloat(&dtdy);
744 status |= parcel.readFloat(&dsdy);
745 status |= parcel.readFloat(&ty);
746
747 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
748 return status;
749}
750
751static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
752 status_t status = parcel.writeFloat(transform.dsdx());
753 status |= parcel.writeFloat(transform.dtdx());
754 status |= parcel.writeFloat(transform.tx());
755 status |= parcel.writeFloat(transform.dtdy());
756 status |= parcel.writeFloat(transform.dsdy());
757 status |= parcel.writeFloat(transform.ty());
758 return status;
759}
760
Jeff Brown5912f952013-07-01 19:10:31 -0700761status_t MotionEvent::readFromParcel(Parcel* parcel) {
762 size_t pointerCount = parcel->readInt32();
763 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800764 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
765 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700766 return BAD_VALUE;
767 }
768
Garfield Tan4cc839f2020-01-24 11:26:14 -0800769 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700770 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600771 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800772 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600773 std::vector<uint8_t> hmac;
774 status_t result = parcel->readByteVector(&hmac);
775 if (result != OK || hmac.size() != 32) {
776 return BAD_VALUE;
777 }
778 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700779 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100780 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700781 mFlags = parcel->readInt32();
782 mEdgeFlags = parcel->readInt32();
783 mMetaState = parcel->readInt32();
784 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800785 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700786
787 result = android::readFromParcel(mTransform, *parcel);
788 if (result != OK) {
789 return result;
790 }
Jeff Brown5912f952013-07-01 19:10:31 -0700791 mXPrecision = parcel->readFloat();
792 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700793 mRawXCursorPosition = parcel->readFloat();
794 mRawYCursorPosition = parcel->readFloat();
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700795
796 result = android::readFromParcel(mRawTransform, *parcel);
797 if (result != OK) {
798 return result;
799 }
Jeff Brown5912f952013-07-01 19:10:31 -0700800 mDownTime = parcel->readInt64();
801
802 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800803 mPointerProperties.reserve(pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700804 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500805 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700806 mSamplePointerCoords.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800807 mSamplePointerCoords.reserve(sampleCount * pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700808
809 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800810 mPointerProperties.push_back({});
811 PointerProperties& properties = mPointerProperties.back();
Jeff Brown5912f952013-07-01 19:10:31 -0700812 properties.id = parcel->readInt32();
813 properties.toolType = parcel->readInt32();
814 }
815
Dan Austinc94fc452015-09-22 14:22:41 -0700816 while (sampleCount > 0) {
817 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500818 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700819 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800820 mSamplePointerCoords.push_back({});
821 status_t status = mSamplePointerCoords.back().readFromParcel(parcel);
Jeff Brown5912f952013-07-01 19:10:31 -0700822 if (status) {
823 return status;
824 }
825 }
826 }
827 return OK;
828}
829
830status_t MotionEvent::writeToParcel(Parcel* parcel) const {
831 size_t pointerCount = mPointerProperties.size();
832 size_t sampleCount = mSampleEventTimes.size();
833
834 parcel->writeInt32(pointerCount);
835 parcel->writeInt32(sampleCount);
836
Garfield Tan4cc839f2020-01-24 11:26:14 -0800837 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700838 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600839 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800840 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600841 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
842 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700843 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100844 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700845 parcel->writeInt32(mFlags);
846 parcel->writeInt32(mEdgeFlags);
847 parcel->writeInt32(mMetaState);
848 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800849 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700850
851 status_t result = android::writeToParcel(mTransform, *parcel);
852 if (result != OK) {
853 return result;
854 }
Jeff Brown5912f952013-07-01 19:10:31 -0700855 parcel->writeFloat(mXPrecision);
856 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700857 parcel->writeFloat(mRawXCursorPosition);
858 parcel->writeFloat(mRawYCursorPosition);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700859
860 result = android::writeToParcel(mRawTransform, *parcel);
861 if (result != OK) {
862 return result;
863 }
Jeff Brown5912f952013-07-01 19:10:31 -0700864 parcel->writeInt64(mDownTime);
865
866 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800867 const PointerProperties& properties = mPointerProperties[i];
Jeff Brown5912f952013-07-01 19:10:31 -0700868 parcel->writeInt32(properties.id);
869 parcel->writeInt32(properties.toolType);
870 }
871
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800872 const PointerCoords* pc = mSamplePointerCoords.data();
Jeff Brown5912f952013-07-01 19:10:31 -0700873 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500874 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700875 for (size_t i = 0; i < pointerCount; i++) {
876 status_t status = (pc++)->writeToParcel(parcel);
877 if (status) {
878 return status;
879 }
880 }
881 }
882 return OK;
883}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800884#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700885
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600886bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700887 if (isFromSource(source, AINPUT_SOURCE_CLASS_POINTER)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700888 // Specifically excludes HOVER_MOVE and SCROLL.
889 switch (action & AMOTION_EVENT_ACTION_MASK) {
890 case AMOTION_EVENT_ACTION_DOWN:
891 case AMOTION_EVENT_ACTION_MOVE:
892 case AMOTION_EVENT_ACTION_UP:
893 case AMOTION_EVENT_ACTION_POINTER_DOWN:
894 case AMOTION_EVENT_ACTION_POINTER_UP:
895 case AMOTION_EVENT_ACTION_CANCEL:
896 case AMOTION_EVENT_ACTION_OUTSIDE:
897 return true;
898 }
899 }
900 return false;
901}
902
Michael Wright872db4f2014-04-22 15:03:51 -0700903const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700904 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700905}
906
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800907std::optional<int> MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700908 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700909}
910
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500911std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700912 // Convert MotionEvent action to string
913 switch (action & AMOTION_EVENT_ACTION_MASK) {
914 case AMOTION_EVENT_ACTION_DOWN:
915 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700916 case AMOTION_EVENT_ACTION_UP:
917 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500918 case AMOTION_EVENT_ACTION_MOVE:
919 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700920 case AMOTION_EVENT_ACTION_CANCEL:
921 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500922 case AMOTION_EVENT_ACTION_OUTSIDE:
923 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700924 case AMOTION_EVENT_ACTION_POINTER_DOWN:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000925 return StringPrintf("POINTER_DOWN(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700926 case AMOTION_EVENT_ACTION_POINTER_UP:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000927 return StringPrintf("POINTER_UP(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500928 case AMOTION_EVENT_ACTION_HOVER_MOVE:
929 return "HOVER_MOVE";
930 case AMOTION_EVENT_ACTION_SCROLL:
931 return "SCROLL";
932 case AMOTION_EVENT_ACTION_HOVER_ENTER:
933 return "HOVER_ENTER";
934 case AMOTION_EVENT_ACTION_HOVER_EXIT:
935 return "HOVER_EXIT";
936 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
937 return "BUTTON_PRESS";
938 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
939 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700940 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500941 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700942}
943
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700944// Apply the given transformation to the point without checking whether the entire transform
945// should be disregarded altogether for the provided source.
946static inline vec2 calculateTransformedXYUnchecked(uint32_t source, const ui::Transform& transform,
947 const vec2& xy) {
948 return shouldDisregardOffset(source) ? transformWithoutTranslation(transform, xy)
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000949 : roundTransformedCoords(transform.transform(xy));
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700950}
951
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700952vec2 MotionEvent::calculateTransformedXY(uint32_t source, const ui::Transform& transform,
953 const vec2& xy) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700954 if (shouldDisregardTransformation(source)) {
955 return xy;
956 }
957 return calculateTransformedXYUnchecked(source, transform, xy);
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700958}
959
Prabir Pradhan8e6ce222022-02-24 09:08:54 -0800960// Keep in sync with calculateTransformedCoords.
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700961float MotionEvent::calculateTransformedAxisValue(int32_t axis, uint32_t source,
962 const ui::Transform& transform,
963 const PointerCoords& coords) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700964 if (shouldDisregardTransformation(source)) {
965 return coords.getAxisValue(axis);
966 }
967
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700968 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700969 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700970 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
971 return xy[axis];
972 }
973
974 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
975 const vec2 relativeXy =
976 transformWithoutTranslation(transform,
977 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
978 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
979 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
980 }
981
982 if (axis == AMOTION_EVENT_AXIS_ORIENTATION) {
983 return transformAngle(transform, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION));
984 }
985
986 return coords.getAxisValue(axis);
987}
988
Prabir Pradhan8e6ce222022-02-24 09:08:54 -0800989// Keep in sync with calculateTransformedAxisValue. This is an optimization of
990// calculateTransformedAxisValue for all PointerCoords axes.
991PointerCoords MotionEvent::calculateTransformedCoords(uint32_t source,
992 const ui::Transform& transform,
993 const PointerCoords& coords) {
994 if (shouldDisregardTransformation(source)) {
995 return coords;
996 }
997 PointerCoords out = coords;
998
999 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
1000 out.setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
1001 out.setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
1002
1003 const vec2 relativeXy =
1004 transformWithoutTranslation(transform,
1005 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
1006 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
1007 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
1008 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
1009
1010 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
1011 transformAngle(transform,
1012 coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)));
1013
1014 return out;
1015}
1016
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +00001017std::ostream& operator<<(std::ostream& out, const MotionEvent& event) {
1018 out << "MotionEvent { action=" << MotionEvent::actionToString(event.getAction());
1019 if (event.getActionButton() != 0) {
1020 out << ", actionButton=" << std::to_string(event.getActionButton());
1021 }
1022 const size_t pointerCount = event.getPointerCount();
hupeng3aa5a51a2022-09-02 16:00:18 +08001023 LOG_ALWAYS_FATAL_IF(pointerCount > MAX_POINTERS, "Too many pointers : pointerCount = %zu",
1024 pointerCount);
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +00001025 for (size_t i = 0; i < pointerCount; i++) {
1026 out << ", id[" << i << "]=" << event.getPointerId(i);
1027 float x = event.getX(i);
1028 float y = event.getY(i);
1029 if (x != 0 || y != 0) {
1030 out << ", x[" << i << "]=" << x;
1031 out << ", y[" << i << "]=" << y;
1032 }
1033 int toolType = event.getToolType(i);
1034 if (toolType != AMOTION_EVENT_TOOL_TYPE_FINGER) {
1035 out << ", toolType[" << i << "]=" << toolType;
1036 }
1037 }
1038 if (event.getButtonState() != 0) {
1039 out << ", buttonState=" << event.getButtonState();
1040 }
1041 if (event.getClassification() != MotionClassification::NONE) {
1042 out << ", classification=" << motionClassificationToString(event.getClassification());
1043 }
1044 if (event.getMetaState() != 0) {
1045 out << ", metaState=" << event.getMetaState();
1046 }
1047 if (event.getEdgeFlags() != 0) {
1048 out << ", edgeFlags=" << event.getEdgeFlags();
1049 }
1050 if (pointerCount != 1) {
1051 out << ", pointerCount=" << pointerCount;
1052 }
1053 if (event.getHistorySize() != 0) {
1054 out << ", historySize=" << event.getHistorySize();
1055 }
1056 out << ", eventTime=" << event.getEventTime();
1057 out << ", downTime=" << event.getDownTime();
1058 out << ", deviceId=" << event.getDeviceId();
1059 out << ", source=" << inputEventSourceToString(event.getSource());
1060 out << ", displayId=" << event.getDisplayId();
1061 out << ", eventId=" << event.getId();
1062 out << "}";
1063 return out;
1064}
1065
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001066// --- FocusEvent ---
1067
Antonio Kantek3cfec7b2021-11-05 18:26:17 -07001068void FocusEvent::initialize(int32_t id, bool hasFocus) {
Garfield Tan4cc839f2020-01-24 11:26:14 -08001069 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -06001070 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001071 mHasFocus = hasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001072}
1073
1074void FocusEvent::initialize(const FocusEvent& from) {
1075 InputEvent::initialize(from);
1076 mHasFocus = from.mHasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001077}
Jeff Brown5912f952013-07-01 19:10:31 -07001078
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001079// --- CaptureEvent ---
1080
1081void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) {
1082 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1083 ADISPLAY_ID_NONE, INVALID_HMAC);
1084 mPointerCaptureEnabled = pointerCaptureEnabled;
1085}
1086
1087void CaptureEvent::initialize(const CaptureEvent& from) {
1088 InputEvent::initialize(from);
1089 mPointerCaptureEnabled = from.mPointerCaptureEnabled;
1090}
1091
arthurhung7632c332020-12-30 16:58:01 +08001092// --- DragEvent ---
1093
1094void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) {
1095 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1096 ADISPLAY_ID_NONE, INVALID_HMAC);
1097 mIsExiting = isExiting;
1098 mX = x;
1099 mY = y;
1100}
1101
1102void DragEvent::initialize(const DragEvent& from) {
1103 InputEvent::initialize(from);
1104 mIsExiting = from.mIsExiting;
1105 mX = from.mX;
1106 mY = from.mY;
1107}
1108
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001109// --- TouchModeEvent ---
1110
1111void TouchModeEvent::initialize(int32_t id, bool isInTouchMode) {
1112 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1113 ADISPLAY_ID_NONE, INVALID_HMAC);
1114 mIsInTouchMode = isInTouchMode;
1115}
1116
1117void TouchModeEvent::initialize(const TouchModeEvent& from) {
1118 InputEvent::initialize(from);
1119 mIsInTouchMode = from.mIsInTouchMode;
1120}
1121
Jeff Brown5912f952013-07-01 19:10:31 -07001122// --- PooledInputEventFactory ---
1123
1124PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
1125 mMaxPoolSize(maxPoolSize) {
1126}
1127
1128PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -07001129}
1130
1131KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001132 if (mKeyEventPool.empty()) {
1133 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001134 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001135 KeyEvent* event = mKeyEventPool.front().release();
1136 mKeyEventPool.pop();
1137 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001138}
1139
1140MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001141 if (mMotionEventPool.empty()) {
1142 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001143 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001144 MotionEvent* event = mMotionEventPool.front().release();
1145 mMotionEventPool.pop();
1146 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001147}
1148
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001149FocusEvent* PooledInputEventFactory::createFocusEvent() {
1150 if (mFocusEventPool.empty()) {
1151 return new FocusEvent();
1152 }
1153 FocusEvent* event = mFocusEventPool.front().release();
1154 mFocusEventPool.pop();
1155 return event;
1156}
1157
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001158CaptureEvent* PooledInputEventFactory::createCaptureEvent() {
1159 if (mCaptureEventPool.empty()) {
1160 return new CaptureEvent();
1161 }
1162 CaptureEvent* event = mCaptureEventPool.front().release();
1163 mCaptureEventPool.pop();
1164 return event;
1165}
1166
arthurhung7632c332020-12-30 16:58:01 +08001167DragEvent* PooledInputEventFactory::createDragEvent() {
1168 if (mDragEventPool.empty()) {
1169 return new DragEvent();
1170 }
1171 DragEvent* event = mDragEventPool.front().release();
1172 mDragEventPool.pop();
1173 return event;
1174}
1175
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001176TouchModeEvent* PooledInputEventFactory::createTouchModeEvent() {
1177 if (mTouchModeEventPool.empty()) {
1178 return new TouchModeEvent();
1179 }
1180 TouchModeEvent* event = mTouchModeEventPool.front().release();
1181 mTouchModeEventPool.pop();
1182 return event;
1183}
1184
Jeff Brown5912f952013-07-01 19:10:31 -07001185void PooledInputEventFactory::recycle(InputEvent* event) {
1186 switch (event->getType()) {
1187 case AINPUT_EVENT_TYPE_KEY:
1188 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001189 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -07001190 return;
1191 }
1192 break;
1193 case AINPUT_EVENT_TYPE_MOTION:
1194 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001195 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -07001196 return;
1197 }
1198 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001199 case AINPUT_EVENT_TYPE_FOCUS:
1200 if (mFocusEventPool.size() < mMaxPoolSize) {
1201 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
1202 return;
1203 }
1204 break;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001205 case AINPUT_EVENT_TYPE_CAPTURE:
1206 if (mCaptureEventPool.size() < mMaxPoolSize) {
1207 mCaptureEventPool.push(
1208 std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event)));
1209 return;
1210 }
1211 break;
arthurhung7632c332020-12-30 16:58:01 +08001212 case AINPUT_EVENT_TYPE_DRAG:
1213 if (mDragEventPool.size() < mMaxPoolSize) {
1214 mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event)));
1215 return;
1216 }
1217 break;
Antonio Kantekeb4a30c2021-09-28 17:49:49 -07001218 case AINPUT_EVENT_TYPE_TOUCH_MODE:
1219 if (mTouchModeEventPool.size() < mMaxPoolSize) {
1220 mTouchModeEventPool.push(
1221 std::unique_ptr<TouchModeEvent>(static_cast<TouchModeEvent*>(event)));
1222 return;
1223 }
1224 break;
Jeff Brown5912f952013-07-01 19:10:31 -07001225 }
1226 delete event;
1227}
1228
1229} // namespace android