blob: 70ed438112677b4ba5da88a1b452de7ca902584e [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>
Jeff Brown5912f952013-07-01 19:10:31 -070023#include <limits.h>
Garfield Tan84b087e2020-01-23 10:49:05 -080024#include <string.h>
Jeff Brown5912f952013-07-01 19:10:31 -070025
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050026#include <android-base/stringprintf.h>
Jeff Brown5912f952013-07-01 19:10:31 -070027#include <input/Input.h>
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080028#include <input/InputDevice.h>
Michael Wright872db4f2014-04-22 15:03:51 -070029#include <input/InputEventLabels.h>
Jeff Brown5912f952013-07-01 19:10:31 -070030
Brett Chabotfaa986c2020-11-04 17:39:36 -080031#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -070032#include <binder/Parcel.h>
Brett Chabotfaa986c2020-11-04 17:39:36 -080033#endif
Brett Chabot58208522020-09-09 13:55:24 -070034#ifdef __ANDROID__
Garfield Tan84b087e2020-01-23 10:49:05 -080035#include <sys/random.h>
Jeff Brown5912f952013-07-01 19:10:31 -070036#endif
37
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050038using android::base::StringPrintf;
39
Jeff Brown5912f952013-07-01 19:10:31 -070040namespace android {
41
Prabir Pradhan6b384612021-05-14 16:56:25 -070042namespace {
43
44float transformAngle(const ui::Transform& transform, float angleRadians) {
45 // Construct and transform a vector oriented at the specified clockwise angle from vertical.
46 // Coordinate system: down is increasing Y, right is increasing X.
47 float x = sinf(angleRadians);
48 float y = -cosf(angleRadians);
49 vec2 transformedPoint = transform.transform(x, y);
50
51 // Determine how the origin is transformed by the matrix so that we
52 // can transform orientation vectors.
53 const vec2 origin = transform.transform(0, 0);
54
55 transformedPoint.x -= origin.x;
56 transformedPoint.y -= origin.y;
57
58 // Derive the transformed vector's clockwise angle from vertical.
59 float result = atan2f(transformedPoint.x, -transformedPoint.y);
60 if (result < -M_PI_2) {
61 result += M_PI;
62 } else if (result > M_PI_2) {
63 result -= M_PI;
64 }
65 return result;
66}
67
68// Rotates the given point to the transform's orientation. If the display width and height are
69// provided, the point is rotated in the screen space. Otherwise, the point is rotated about the
70// origin. This helper is used to avoid the extra overhead of creating new Transforms.
71vec2 rotatePoint(const ui::Transform& transform, float x, float y, int32_t displayWidth = 0,
72 int32_t displayHeight = 0) {
73 // 0x7 encapsulates all 3 rotations (see ui::Transform::RotationFlags)
74 static const int ALL_ROTATIONS_MASK = 0x7;
75 const uint32_t orientation = (transform.getOrientation() & ALL_ROTATIONS_MASK);
76 if (orientation == ui::Transform::ROT_0) {
77 return {x, y};
78 }
79
80 vec2 xy(x, y);
81 if (orientation == ui::Transform::ROT_90) {
82 xy.x = displayHeight - y;
83 xy.y = x;
84 } else if (orientation == ui::Transform::ROT_180) {
85 xy.x = displayWidth - x;
86 xy.y = displayHeight - y;
87 } else if (orientation == ui::Transform::ROT_270) {
88 xy.x = y;
89 xy.y = displayWidth - x;
90 }
91 return xy;
92}
93
94} // namespace
95
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -080096const char* motionClassificationToString(MotionClassification classification) {
97 switch (classification) {
98 case MotionClassification::NONE:
99 return "NONE";
100 case MotionClassification::AMBIGUOUS_GESTURE:
101 return "AMBIGUOUS_GESTURE";
102 case MotionClassification::DEEP_PRESS:
103 return "DEEP_PRESS";
104 }
105}
106
Garfield Tan84b087e2020-01-23 10:49:05 -0800107// --- IdGenerator ---
108IdGenerator::IdGenerator(Source source) : mSource(source) {}
109
110int32_t IdGenerator::nextId() const {
111 constexpr uint32_t SEQUENCE_NUMBER_MASK = ~SOURCE_MASK;
112 int32_t id = 0;
113
114// Avoid building against syscall getrandom(2) on host, which will fail build on Mac. Host doesn't
115// use sequence number so just always return mSource.
116#ifdef __ANDROID__
117 constexpr size_t BUF_LEN = sizeof(id);
118 size_t totalBytes = 0;
119 while (totalBytes < BUF_LEN) {
120 ssize_t bytes = TEMP_FAILURE_RETRY(getrandom(&id, BUF_LEN, GRND_NONBLOCK));
121 if (CC_UNLIKELY(bytes < 0)) {
122 ALOGW("Failed to fill in random number for sequence number: %s.", strerror(errno));
123 id = 0;
124 break;
125 }
126 totalBytes += bytes;
127 }
128#endif // __ANDROID__
129
130 return (id & SEQUENCE_NUMBER_MASK) | static_cast<int32_t>(mSource);
131}
132
Jeff Brown5912f952013-07-01 19:10:31 -0700133// --- InputEvent ---
134
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800135const char* inputEventTypeToString(int32_t type) {
136 switch (type) {
137 case AINPUT_EVENT_TYPE_KEY: {
138 return "KEY";
139 }
140 case AINPUT_EVENT_TYPE_MOTION: {
141 return "MOTION";
142 }
143 case AINPUT_EVENT_TYPE_FOCUS: {
144 return "FOCUS";
145 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800146 case AINPUT_EVENT_TYPE_CAPTURE: {
147 return "CAPTURE";
148 }
arthurhung7632c332020-12-30 16:58:01 +0800149 case AINPUT_EVENT_TYPE_DRAG: {
150 return "DRAG";
151 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800152 }
153 return "UNKNOWN";
154}
155
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800156VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
157 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
158 event.getSource(), event.getDisplayId()},
159 event.getAction(),
160 event.getDownTime(),
161 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
162 event.getKeyCode(),
163 event.getScanCode(),
164 event.getMetaState(),
165 event.getRepeatCount()};
166}
167
168VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
169 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
170 event.getSource(), event.getDisplayId()},
171 event.getRawX(0),
172 event.getRawY(0),
173 event.getActionMasked(),
174 event.getDownTime(),
175 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
176 event.getMetaState(),
177 event.getButtonState()};
178}
179
Garfield Tan4cc839f2020-01-24 11:26:14 -0800180void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600181 std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800182 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700183 mDeviceId = deviceId;
184 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100185 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600186 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700187}
188
189void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800190 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700191 mDeviceId = from.mDeviceId;
192 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100193 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600194 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700195}
196
Garfield Tan4cc839f2020-01-24 11:26:14 -0800197int32_t InputEvent::nextId() {
198 static IdGenerator idGen(IdGenerator::Source::OTHER);
199 return idGen.nextId();
200}
201
Jeff Brown5912f952013-07-01 19:10:31 -0700202// --- KeyEvent ---
203
Michael Wright872db4f2014-04-22 15:03:51 -0700204const char* KeyEvent::getLabel(int32_t keyCode) {
Chris Ye4958d062020-08-20 13:21:10 -0700205 return InputEventLookup::getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700206}
207
Michael Wright872db4f2014-04-22 15:03:51 -0700208int32_t KeyEvent::getKeyCodeFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700209 return InputEventLookup::getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700210}
211
Garfield Tan4cc839f2020-01-24 11:26:14 -0800212void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600213 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
214 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
215 nsecs_t downTime, nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800216 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700217 mAction = action;
218 mFlags = flags;
219 mKeyCode = keyCode;
220 mScanCode = scanCode;
221 mMetaState = metaState;
222 mRepeatCount = repeatCount;
223 mDownTime = downTime;
224 mEventTime = eventTime;
225}
226
227void KeyEvent::initialize(const KeyEvent& from) {
228 InputEvent::initialize(from);
229 mAction = from.mAction;
230 mFlags = from.mFlags;
231 mKeyCode = from.mKeyCode;
232 mScanCode = from.mScanCode;
233 mMetaState = from.mMetaState;
234 mRepeatCount = from.mRepeatCount;
235 mDownTime = from.mDownTime;
236 mEventTime = from.mEventTime;
237}
238
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700239const char* KeyEvent::actionToString(int32_t action) {
240 // Convert KeyEvent action to string
241 switch (action) {
242 case AKEY_EVENT_ACTION_DOWN:
243 return "DOWN";
244 case AKEY_EVENT_ACTION_UP:
245 return "UP";
246 case AKEY_EVENT_ACTION_MULTIPLE:
247 return "MULTIPLE";
248 }
249 return "UNKNOWN";
250}
Jeff Brown5912f952013-07-01 19:10:31 -0700251
252// --- PointerCoords ---
253
254float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700255 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700256 return 0;
257 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700258 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700259}
260
261status_t PointerCoords::setAxisValue(int32_t axis, float value) {
262 if (axis < 0 || axis > 63) {
263 return NAME_NOT_FOUND;
264 }
265
Michael Wright38dcdff2014-03-19 12:06:10 -0700266 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
267 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700268 if (value == 0) {
269 return OK; // axes with value 0 do not need to be stored
270 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700271
272 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700273 if (count >= MAX_AXES) {
274 tooManyAxes(axis);
275 return NO_MEMORY;
276 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700277 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700278 for (uint32_t i = count; i > index; i--) {
279 values[i] = values[i - 1];
280 }
281 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700282
Jeff Brown5912f952013-07-01 19:10:31 -0700283 values[index] = value;
284 return OK;
285}
286
287static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
288 float value = c.getAxisValue(axis);
289 if (value != 0) {
290 c.setAxisValue(axis, value * scaleFactor);
291 }
292}
293
Robert Carre07e1032018-11-26 12:55:53 -0800294void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700295 // No need to scale pressure or size since they are normalized.
296 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800297
298 // If there is a global scale factor, it is included in the windowX/YScale
299 // so we don't need to apply it twice to the X/Y axes.
300 // However we don't want to apply any windowXYScale not included in the global scale
301 // to the TOUCH_MAJOR/MINOR coordinates.
302 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
303 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
304 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
305 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
306 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
307 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
308}
309
310void PointerCoords::scale(float globalScaleFactor) {
311 scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700312}
313
Jeff Brownf086ddb2014-02-11 14:28:48 -0800314void PointerCoords::applyOffset(float xOffset, float yOffset) {
315 setAxisValue(AMOTION_EVENT_AXIS_X, getX() + xOffset);
316 setAxisValue(AMOTION_EVENT_AXIS_Y, getY() + yOffset);
317}
318
Brett Chabotfaa986c2020-11-04 17:39:36 -0800319#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700320status_t PointerCoords::readFromParcel(Parcel* parcel) {
321 bits = parcel->readInt64();
322
Michael Wright38dcdff2014-03-19 12:06:10 -0700323 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700324 if (count > MAX_AXES) {
325 return BAD_VALUE;
326 }
327
328 for (uint32_t i = 0; i < count; i++) {
329 values[i] = parcel->readFloat();
330 }
331 return OK;
332}
333
334status_t PointerCoords::writeToParcel(Parcel* parcel) const {
335 parcel->writeInt64(bits);
336
Michael Wright38dcdff2014-03-19 12:06:10 -0700337 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700338 for (uint32_t i = 0; i < count; i++) {
339 parcel->writeFloat(values[i]);
340 }
341 return OK;
342}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800343#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700344
345void PointerCoords::tooManyAxes(int axis) {
346 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
347 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
348}
349
350bool PointerCoords::operator==(const PointerCoords& other) const {
351 if (bits != other.bits) {
352 return false;
353 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700354 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700355 for (uint32_t i = 0; i < count; i++) {
356 if (values[i] != other.values[i]) {
357 return false;
358 }
359 }
360 return true;
361}
362
363void PointerCoords::copyFrom(const PointerCoords& other) {
364 bits = other.bits;
Michael Wright38dcdff2014-03-19 12:06:10 -0700365 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700366 for (uint32_t i = 0; i < count; i++) {
367 values[i] = other.values[i];
368 }
369}
370
chaviwc01e1372020-07-01 12:37:31 -0700371void PointerCoords::transform(const ui::Transform& transform) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700372 const vec2 xy = transform.transform(getXYValue());
373 setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
374 setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
375
376 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) {
377 const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
378 setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val));
379 }
chaviwc01e1372020-07-01 12:37:31 -0700380}
Jeff Brown5912f952013-07-01 19:10:31 -0700381
382// --- PointerProperties ---
383
384bool PointerProperties::operator==(const PointerProperties& other) const {
385 return id == other.id
386 && toolType == other.toolType;
387}
388
389void PointerProperties::copyFrom(const PointerProperties& other) {
390 id = other.id;
391 toolType = other.toolType;
392}
393
394
395// --- MotionEvent ---
396
Garfield Tan4cc839f2020-01-24 11:26:14 -0800397void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600398 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
399 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700400 int32_t buttonState, MotionClassification classification,
401 const ui::Transform& transform, float xPrecision, float yPrecision,
Evan Rosky84f07f02021-04-16 10:42:42 -0700402 float rawXCursorPosition, float rawYCursorPosition,
403 int32_t displayWidth, int32_t displayHeight, nsecs_t downTime,
chaviw9eaa22c2020-07-01 16:21:27 -0700404 nsecs_t eventTime, size_t pointerCount,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600405 const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700406 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800407 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700408 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100409 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700410 mFlags = flags;
411 mEdgeFlags = edgeFlags;
412 mMetaState = metaState;
413 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800414 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700415 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700416 mXPrecision = xPrecision;
417 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700418 mRawXCursorPosition = rawXCursorPosition;
419 mRawYCursorPosition = rawYCursorPosition;
Evan Rosky84f07f02021-04-16 10:42:42 -0700420 mDisplayWidth = displayWidth;
421 mDisplayHeight = displayHeight;
Jeff Brown5912f952013-07-01 19:10:31 -0700422 mDownTime = downTime;
423 mPointerProperties.clear();
424 mPointerProperties.appendArray(pointerProperties, pointerCount);
425 mSampleEventTimes.clear();
426 mSamplePointerCoords.clear();
427 addSample(eventTime, pointerCoords);
428}
429
430void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800431 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
432 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700433 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100434 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700435 mFlags = other->mFlags;
436 mEdgeFlags = other->mEdgeFlags;
437 mMetaState = other->mMetaState;
438 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800439 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700440 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700441 mXPrecision = other->mXPrecision;
442 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700443 mRawXCursorPosition = other->mRawXCursorPosition;
444 mRawYCursorPosition = other->mRawYCursorPosition;
Evan Rosky84f07f02021-04-16 10:42:42 -0700445 mDisplayWidth = other->mDisplayWidth;
446 mDisplayHeight = other->mDisplayHeight;
Jeff Brown5912f952013-07-01 19:10:31 -0700447 mDownTime = other->mDownTime;
448 mPointerProperties = other->mPointerProperties;
449
450 if (keepHistory) {
451 mSampleEventTimes = other->mSampleEventTimes;
452 mSamplePointerCoords = other->mSamplePointerCoords;
453 } else {
454 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500455 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700456 mSamplePointerCoords.clear();
457 size_t pointerCount = other->getPointerCount();
458 size_t historySize = other->getHistorySize();
459 mSamplePointerCoords.appendArray(other->mSamplePointerCoords.array()
460 + (historySize * pointerCount), pointerCount);
461 }
462}
463
464void MotionEvent::addSample(
465 int64_t eventTime,
466 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500467 mSampleEventTimes.push_back(eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -0700468 mSamplePointerCoords.appendArray(pointerCoords, getPointerCount());
469}
470
Garfield Tan00f511d2019-06-12 16:55:40 -0700471float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700472 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
473 return vals.x;
Garfield Tan00f511d2019-06-12 16:55:40 -0700474}
475
476float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700477 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
478 return vals.y;
Garfield Tan00f511d2019-06-12 16:55:40 -0700479}
480
Garfield Tan937bb832019-07-25 17:48:31 -0700481void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700482 ui::Transform inverse = mTransform.inverse();
483 vec2 vals = inverse.transform(x, y);
484 mRawXCursorPosition = vals.x;
485 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700486}
487
Jeff Brown5912f952013-07-01 19:10:31 -0700488const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
489 return &mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
490}
491
492float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
Evan Rosky84f07f02021-04-16 10:42:42 -0700493 return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700494}
495
496float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700497 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700498}
499
500const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
501 size_t pointerIndex, size_t historicalIndex) const {
502 return &mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
503}
504
505float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan6b384612021-05-14 16:56:25 -0700506 size_t historicalIndex) const {
507 const PointerCoords* coords = getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
508
509 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
510 // For compatibility, convert raw coordinates into "oriented screen space". Once app
511 // developers are educated about getRaw, we can consider removing this.
512 const vec2 xy = rotatePoint(mTransform, coords->getX(), coords->getY(), mDisplayWidth,
513 mDisplayHeight);
514 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
515 return xy[axis];
Evan Rosky84f07f02021-04-16 10:42:42 -0700516 }
517
Prabir Pradhan6b384612021-05-14 16:56:25 -0700518 return coords->getAxisValue(axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700519}
520
521float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
522 size_t historicalIndex) const {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700523 const PointerCoords* coords = getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
524
525 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
526 const vec2 xy = mTransform.transform(coords->getXYValue());
527 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
528 return xy[axis];
chaviw9eaa22c2020-07-01 16:21:27 -0700529 }
530
Prabir Pradhan6b384612021-05-14 16:56:25 -0700531 return coords->getAxisValue(axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700532}
533
534ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
535 size_t pointerCount = mPointerProperties.size();
536 for (size_t i = 0; i < pointerCount; i++) {
537 if (mPointerProperties.itemAt(i).id == pointerId) {
538 return i;
539 }
540 }
541 return -1;
542}
543
544void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700545 float currXOffset = mTransform.tx();
546 float currYOffset = mTransform.ty();
547 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700548}
549
Robert Carre07e1032018-11-26 12:55:53 -0800550void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700551 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800552 mXPrecision *= globalScaleFactor;
553 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700554
555 size_t numSamples = mSamplePointerCoords.size();
556 for (size_t i = 0; i < numSamples; i++) {
chaviw9eaa22c2020-07-01 16:21:27 -0700557 mSamplePointerCoords.editItemAt(i).scale(globalScaleFactor, globalScaleFactor,
558 globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700559 }
560}
561
chaviw9eaa22c2020-07-01 16:21:27 -0700562void MotionEvent::transform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700563 // We want to preserve the raw axes values stored in the PointerCoords, so we just update the
564 // transform using the values passed in.
chaviw9eaa22c2020-07-01 16:21:27 -0700565 ui::Transform newTransform;
566 newTransform.set(matrix);
567 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700568
Prabir Pradhan6b384612021-05-14 16:56:25 -0700569 // We need to update the AXIS_ORIENTATION value here to maintain the old behavior where the
570 // orientation angle is not affected by the initial transformation set in the MotionEvent.
571 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
572 [&newTransform](PointerCoords& c) {
573 float orientation = c.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
574 c.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
575 transformAngle(newTransform, orientation));
576 });
Jeff Brown5912f952013-07-01 19:10:31 -0700577}
578
Evan Roskyd4d4d802021-05-03 20:12:21 -0700579void MotionEvent::applyTransform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700580 ui::Transform transform;
581 transform.set(matrix);
Evan Roskyd4d4d802021-05-03 20:12:21 -0700582
583 // Apply the transformation to all samples.
Prabir Pradhan6b384612021-05-14 16:56:25 -0700584 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
585 [&transform](PointerCoords& c) { c.transform(transform); });
Evan Roskyd4d4d802021-05-03 20:12:21 -0700586}
587
Brett Chabotfaa986c2020-11-04 17:39:36 -0800588#ifdef __linux__
chaviw9eaa22c2020-07-01 16:21:27 -0700589static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
590 float dsdx, dtdx, tx, dtdy, dsdy, ty;
591 status_t status = parcel.readFloat(&dsdx);
592 status |= parcel.readFloat(&dtdx);
593 status |= parcel.readFloat(&tx);
594 status |= parcel.readFloat(&dtdy);
595 status |= parcel.readFloat(&dsdy);
596 status |= parcel.readFloat(&ty);
597
598 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
599 return status;
600}
601
602static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
603 status_t status = parcel.writeFloat(transform.dsdx());
604 status |= parcel.writeFloat(transform.dtdx());
605 status |= parcel.writeFloat(transform.tx());
606 status |= parcel.writeFloat(transform.dtdy());
607 status |= parcel.writeFloat(transform.dsdy());
608 status |= parcel.writeFloat(transform.ty());
609 return status;
610}
611
Jeff Brown5912f952013-07-01 19:10:31 -0700612status_t MotionEvent::readFromParcel(Parcel* parcel) {
613 size_t pointerCount = parcel->readInt32();
614 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800615 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
616 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700617 return BAD_VALUE;
618 }
619
Garfield Tan4cc839f2020-01-24 11:26:14 -0800620 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700621 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600622 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800623 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600624 std::vector<uint8_t> hmac;
625 status_t result = parcel->readByteVector(&hmac);
626 if (result != OK || hmac.size() != 32) {
627 return BAD_VALUE;
628 }
629 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700630 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100631 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700632 mFlags = parcel->readInt32();
633 mEdgeFlags = parcel->readInt32();
634 mMetaState = parcel->readInt32();
635 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800636 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700637
638 result = android::readFromParcel(mTransform, *parcel);
639 if (result != OK) {
640 return result;
641 }
Jeff Brown5912f952013-07-01 19:10:31 -0700642 mXPrecision = parcel->readFloat();
643 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700644 mRawXCursorPosition = parcel->readFloat();
645 mRawYCursorPosition = parcel->readFloat();
Evan Rosky84f07f02021-04-16 10:42:42 -0700646 mDisplayWidth = parcel->readInt32();
647 mDisplayHeight = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700648 mDownTime = parcel->readInt64();
649
650 mPointerProperties.clear();
651 mPointerProperties.setCapacity(pointerCount);
652 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500653 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700654 mSamplePointerCoords.clear();
655 mSamplePointerCoords.setCapacity(sampleCount * pointerCount);
656
657 for (size_t i = 0; i < pointerCount; i++) {
658 mPointerProperties.push();
659 PointerProperties& properties = mPointerProperties.editTop();
660 properties.id = parcel->readInt32();
661 properties.toolType = parcel->readInt32();
662 }
663
Dan Austinc94fc452015-09-22 14:22:41 -0700664 while (sampleCount > 0) {
665 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500666 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700667 for (size_t i = 0; i < pointerCount; i++) {
668 mSamplePointerCoords.push();
669 status_t status = mSamplePointerCoords.editTop().readFromParcel(parcel);
670 if (status) {
671 return status;
672 }
673 }
674 }
675 return OK;
676}
677
678status_t MotionEvent::writeToParcel(Parcel* parcel) const {
679 size_t pointerCount = mPointerProperties.size();
680 size_t sampleCount = mSampleEventTimes.size();
681
682 parcel->writeInt32(pointerCount);
683 parcel->writeInt32(sampleCount);
684
Garfield Tan4cc839f2020-01-24 11:26:14 -0800685 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700686 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600687 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800688 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600689 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
690 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700691 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100692 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700693 parcel->writeInt32(mFlags);
694 parcel->writeInt32(mEdgeFlags);
695 parcel->writeInt32(mMetaState);
696 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800697 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700698
699 status_t result = android::writeToParcel(mTransform, *parcel);
700 if (result != OK) {
701 return result;
702 }
Jeff Brown5912f952013-07-01 19:10:31 -0700703 parcel->writeFloat(mXPrecision);
704 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700705 parcel->writeFloat(mRawXCursorPosition);
706 parcel->writeFloat(mRawYCursorPosition);
Evan Rosky84f07f02021-04-16 10:42:42 -0700707 parcel->writeInt32(mDisplayWidth);
708 parcel->writeInt32(mDisplayHeight);
Jeff Brown5912f952013-07-01 19:10:31 -0700709 parcel->writeInt64(mDownTime);
710
711 for (size_t i = 0; i < pointerCount; i++) {
712 const PointerProperties& properties = mPointerProperties.itemAt(i);
713 parcel->writeInt32(properties.id);
714 parcel->writeInt32(properties.toolType);
715 }
716
717 const PointerCoords* pc = mSamplePointerCoords.array();
718 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500719 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700720 for (size_t i = 0; i < pointerCount; i++) {
721 status_t status = (pc++)->writeToParcel(parcel);
722 if (status) {
723 return status;
724 }
725 }
726 }
727 return OK;
728}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800729#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700730
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600731bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Jeff Brown5912f952013-07-01 19:10:31 -0700732 if (source & AINPUT_SOURCE_CLASS_POINTER) {
733 // Specifically excludes HOVER_MOVE and SCROLL.
734 switch (action & AMOTION_EVENT_ACTION_MASK) {
735 case AMOTION_EVENT_ACTION_DOWN:
736 case AMOTION_EVENT_ACTION_MOVE:
737 case AMOTION_EVENT_ACTION_UP:
738 case AMOTION_EVENT_ACTION_POINTER_DOWN:
739 case AMOTION_EVENT_ACTION_POINTER_UP:
740 case AMOTION_EVENT_ACTION_CANCEL:
741 case AMOTION_EVENT_ACTION_OUTSIDE:
742 return true;
743 }
744 }
745 return false;
746}
747
Michael Wright872db4f2014-04-22 15:03:51 -0700748const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700749 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700750}
751
752int32_t MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700753 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700754}
755
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500756std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700757 // Convert MotionEvent action to string
758 switch (action & AMOTION_EVENT_ACTION_MASK) {
759 case AMOTION_EVENT_ACTION_DOWN:
760 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700761 case AMOTION_EVENT_ACTION_UP:
762 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500763 case AMOTION_EVENT_ACTION_MOVE:
764 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700765 case AMOTION_EVENT_ACTION_CANCEL:
766 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500767 case AMOTION_EVENT_ACTION_OUTSIDE:
768 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700769 case AMOTION_EVENT_ACTION_POINTER_DOWN:
770 return "POINTER_DOWN";
771 case AMOTION_EVENT_ACTION_POINTER_UP:
772 return "POINTER_UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500773 case AMOTION_EVENT_ACTION_HOVER_MOVE:
774 return "HOVER_MOVE";
775 case AMOTION_EVENT_ACTION_SCROLL:
776 return "SCROLL";
777 case AMOTION_EVENT_ACTION_HOVER_ENTER:
778 return "HOVER_ENTER";
779 case AMOTION_EVENT_ACTION_HOVER_EXIT:
780 return "HOVER_EXIT";
781 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
782 return "BUTTON_PRESS";
783 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
784 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700785 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500786 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700787}
788
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800789// --- FocusEvent ---
790
Garfield Tan4cc839f2020-01-24 11:26:14 -0800791void FocusEvent::initialize(int32_t id, bool hasFocus, bool inTouchMode) {
792 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600793 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800794 mHasFocus = hasFocus;
795 mInTouchMode = inTouchMode;
796}
797
798void FocusEvent::initialize(const FocusEvent& from) {
799 InputEvent::initialize(from);
800 mHasFocus = from.mHasFocus;
801 mInTouchMode = from.mInTouchMode;
802}
Jeff Brown5912f952013-07-01 19:10:31 -0700803
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800804// --- CaptureEvent ---
805
806void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) {
807 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
808 ADISPLAY_ID_NONE, INVALID_HMAC);
809 mPointerCaptureEnabled = pointerCaptureEnabled;
810}
811
812void CaptureEvent::initialize(const CaptureEvent& from) {
813 InputEvent::initialize(from);
814 mPointerCaptureEnabled = from.mPointerCaptureEnabled;
815}
816
arthurhung7632c332020-12-30 16:58:01 +0800817// --- DragEvent ---
818
819void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) {
820 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
821 ADISPLAY_ID_NONE, INVALID_HMAC);
822 mIsExiting = isExiting;
823 mX = x;
824 mY = y;
825}
826
827void DragEvent::initialize(const DragEvent& from) {
828 InputEvent::initialize(from);
829 mIsExiting = from.mIsExiting;
830 mX = from.mX;
831 mY = from.mY;
832}
833
Jeff Brown5912f952013-07-01 19:10:31 -0700834// --- PooledInputEventFactory ---
835
836PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
837 mMaxPoolSize(maxPoolSize) {
838}
839
840PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -0700841}
842
843KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800844 if (mKeyEventPool.empty()) {
845 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700846 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800847 KeyEvent* event = mKeyEventPool.front().release();
848 mKeyEventPool.pop();
849 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700850}
851
852MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800853 if (mMotionEventPool.empty()) {
854 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700855 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800856 MotionEvent* event = mMotionEventPool.front().release();
857 mMotionEventPool.pop();
858 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700859}
860
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800861FocusEvent* PooledInputEventFactory::createFocusEvent() {
862 if (mFocusEventPool.empty()) {
863 return new FocusEvent();
864 }
865 FocusEvent* event = mFocusEventPool.front().release();
866 mFocusEventPool.pop();
867 return event;
868}
869
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800870CaptureEvent* PooledInputEventFactory::createCaptureEvent() {
871 if (mCaptureEventPool.empty()) {
872 return new CaptureEvent();
873 }
874 CaptureEvent* event = mCaptureEventPool.front().release();
875 mCaptureEventPool.pop();
876 return event;
877}
878
arthurhung7632c332020-12-30 16:58:01 +0800879DragEvent* PooledInputEventFactory::createDragEvent() {
880 if (mDragEventPool.empty()) {
881 return new DragEvent();
882 }
883 DragEvent* event = mDragEventPool.front().release();
884 mDragEventPool.pop();
885 return event;
886}
887
Jeff Brown5912f952013-07-01 19:10:31 -0700888void PooledInputEventFactory::recycle(InputEvent* event) {
889 switch (event->getType()) {
890 case AINPUT_EVENT_TYPE_KEY:
891 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800892 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700893 return;
894 }
895 break;
896 case AINPUT_EVENT_TYPE_MOTION:
897 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800898 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700899 return;
900 }
901 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800902 case AINPUT_EVENT_TYPE_FOCUS:
903 if (mFocusEventPool.size() < mMaxPoolSize) {
904 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
905 return;
906 }
907 break;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800908 case AINPUT_EVENT_TYPE_CAPTURE:
909 if (mCaptureEventPool.size() < mMaxPoolSize) {
910 mCaptureEventPool.push(
911 std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event)));
912 return;
913 }
914 break;
arthurhung7632c332020-12-30 16:58:01 +0800915 case AINPUT_EVENT_TYPE_DRAG:
916 if (mDragEventPool.size() < mMaxPoolSize) {
917 mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event)));
918 return;
919 }
920 break;
Jeff Brown5912f952013-07-01 19:10:31 -0700921 }
922 delete event;
923}
924
925} // namespace android