blob: e8c05e517153ddb6755cde0dd8c69f324d34d7fe [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
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -080042const char* motionClassificationToString(MotionClassification classification) {
43 switch (classification) {
44 case MotionClassification::NONE:
45 return "NONE";
46 case MotionClassification::AMBIGUOUS_GESTURE:
47 return "AMBIGUOUS_GESTURE";
48 case MotionClassification::DEEP_PRESS:
49 return "DEEP_PRESS";
50 }
51}
52
Garfield Tan84b087e2020-01-23 10:49:05 -080053// --- IdGenerator ---
54IdGenerator::IdGenerator(Source source) : mSource(source) {}
55
56int32_t IdGenerator::nextId() const {
57 constexpr uint32_t SEQUENCE_NUMBER_MASK = ~SOURCE_MASK;
58 int32_t id = 0;
59
60// Avoid building against syscall getrandom(2) on host, which will fail build on Mac. Host doesn't
61// use sequence number so just always return mSource.
62#ifdef __ANDROID__
63 constexpr size_t BUF_LEN = sizeof(id);
64 size_t totalBytes = 0;
65 while (totalBytes < BUF_LEN) {
66 ssize_t bytes = TEMP_FAILURE_RETRY(getrandom(&id, BUF_LEN, GRND_NONBLOCK));
67 if (CC_UNLIKELY(bytes < 0)) {
68 ALOGW("Failed to fill in random number for sequence number: %s.", strerror(errno));
69 id = 0;
70 break;
71 }
72 totalBytes += bytes;
73 }
74#endif // __ANDROID__
75
76 return (id & SEQUENCE_NUMBER_MASK) | static_cast<int32_t>(mSource);
77}
78
Jeff Brown5912f952013-07-01 19:10:31 -070079// --- InputEvent ---
80
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080081const char* inputEventTypeToString(int32_t type) {
82 switch (type) {
83 case AINPUT_EVENT_TYPE_KEY: {
84 return "KEY";
85 }
86 case AINPUT_EVENT_TYPE_MOTION: {
87 return "MOTION";
88 }
89 case AINPUT_EVENT_TYPE_FOCUS: {
90 return "FOCUS";
91 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -080092 case AINPUT_EVENT_TYPE_CAPTURE: {
93 return "CAPTURE";
94 }
arthurhung7632c332020-12-30 16:58:01 +080095 case AINPUT_EVENT_TYPE_DRAG: {
96 return "DRAG";
97 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080098 }
99 return "UNKNOWN";
100}
101
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800102VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
103 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
104 event.getSource(), event.getDisplayId()},
105 event.getAction(),
106 event.getDownTime(),
107 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
108 event.getKeyCode(),
109 event.getScanCode(),
110 event.getMetaState(),
111 event.getRepeatCount()};
112}
113
114VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
115 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
116 event.getSource(), event.getDisplayId()},
117 event.getRawX(0),
118 event.getRawY(0),
119 event.getActionMasked(),
120 event.getDownTime(),
121 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
122 event.getMetaState(),
123 event.getButtonState()};
124}
125
Garfield Tan4cc839f2020-01-24 11:26:14 -0800126void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600127 std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800128 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700129 mDeviceId = deviceId;
130 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100131 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600132 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700133}
134
135void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800136 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700137 mDeviceId = from.mDeviceId;
138 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100139 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600140 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700141}
142
Garfield Tan4cc839f2020-01-24 11:26:14 -0800143int32_t InputEvent::nextId() {
144 static IdGenerator idGen(IdGenerator::Source::OTHER);
145 return idGen.nextId();
146}
147
Jeff Brown5912f952013-07-01 19:10:31 -0700148// --- KeyEvent ---
149
Michael Wright872db4f2014-04-22 15:03:51 -0700150const char* KeyEvent::getLabel(int32_t keyCode) {
Chris Ye4958d062020-08-20 13:21:10 -0700151 return InputEventLookup::getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700152}
153
Michael Wright872db4f2014-04-22 15:03:51 -0700154int32_t KeyEvent::getKeyCodeFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700155 return InputEventLookup::getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700156}
157
Garfield Tan4cc839f2020-01-24 11:26:14 -0800158void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600159 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
160 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
161 nsecs_t downTime, nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800162 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700163 mAction = action;
164 mFlags = flags;
165 mKeyCode = keyCode;
166 mScanCode = scanCode;
167 mMetaState = metaState;
168 mRepeatCount = repeatCount;
169 mDownTime = downTime;
170 mEventTime = eventTime;
171}
172
173void KeyEvent::initialize(const KeyEvent& from) {
174 InputEvent::initialize(from);
175 mAction = from.mAction;
176 mFlags = from.mFlags;
177 mKeyCode = from.mKeyCode;
178 mScanCode = from.mScanCode;
179 mMetaState = from.mMetaState;
180 mRepeatCount = from.mRepeatCount;
181 mDownTime = from.mDownTime;
182 mEventTime = from.mEventTime;
183}
184
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700185const char* KeyEvent::actionToString(int32_t action) {
186 // Convert KeyEvent action to string
187 switch (action) {
188 case AKEY_EVENT_ACTION_DOWN:
189 return "DOWN";
190 case AKEY_EVENT_ACTION_UP:
191 return "UP";
192 case AKEY_EVENT_ACTION_MULTIPLE:
193 return "MULTIPLE";
194 }
195 return "UNKNOWN";
196}
Jeff Brown5912f952013-07-01 19:10:31 -0700197
198// --- PointerCoords ---
199
200float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700201 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700202 return 0;
203 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700204 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700205}
206
207status_t PointerCoords::setAxisValue(int32_t axis, float value) {
208 if (axis < 0 || axis > 63) {
209 return NAME_NOT_FOUND;
210 }
211
Michael Wright38dcdff2014-03-19 12:06:10 -0700212 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
213 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700214 if (value == 0) {
215 return OK; // axes with value 0 do not need to be stored
216 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700217
218 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700219 if (count >= MAX_AXES) {
220 tooManyAxes(axis);
221 return NO_MEMORY;
222 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700223 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700224 for (uint32_t i = count; i > index; i--) {
225 values[i] = values[i - 1];
226 }
227 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700228
Jeff Brown5912f952013-07-01 19:10:31 -0700229 values[index] = value;
230 return OK;
231}
232
233static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
234 float value = c.getAxisValue(axis);
235 if (value != 0) {
236 c.setAxisValue(axis, value * scaleFactor);
237 }
238}
239
Robert Carre07e1032018-11-26 12:55:53 -0800240void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700241 // No need to scale pressure or size since they are normalized.
242 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800243
244 // If there is a global scale factor, it is included in the windowX/YScale
245 // so we don't need to apply it twice to the X/Y axes.
246 // However we don't want to apply any windowXYScale not included in the global scale
247 // to the TOUCH_MAJOR/MINOR coordinates.
248 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
249 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
250 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
251 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
252 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
253 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
254}
255
Jeff Brownf086ddb2014-02-11 14:28:48 -0800256void PointerCoords::applyOffset(float xOffset, float yOffset) {
257 setAxisValue(AMOTION_EVENT_AXIS_X, getX() + xOffset);
258 setAxisValue(AMOTION_EVENT_AXIS_Y, getY() + yOffset);
259}
260
Brett Chabotfaa986c2020-11-04 17:39:36 -0800261#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700262status_t PointerCoords::readFromParcel(Parcel* parcel) {
263 bits = parcel->readInt64();
264
Michael Wright38dcdff2014-03-19 12:06:10 -0700265 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700266 if (count > MAX_AXES) {
267 return BAD_VALUE;
268 }
269
270 for (uint32_t i = 0; i < count; i++) {
271 values[i] = parcel->readFloat();
272 }
273 return OK;
274}
275
276status_t PointerCoords::writeToParcel(Parcel* parcel) const {
277 parcel->writeInt64(bits);
278
Michael Wright38dcdff2014-03-19 12:06:10 -0700279 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700280 for (uint32_t i = 0; i < count; i++) {
281 parcel->writeFloat(values[i]);
282 }
283 return OK;
284}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800285#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700286
287void PointerCoords::tooManyAxes(int axis) {
288 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
289 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
290}
291
292bool PointerCoords::operator==(const PointerCoords& other) const {
293 if (bits != other.bits) {
294 return false;
295 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700296 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700297 for (uint32_t i = 0; i < count; i++) {
298 if (values[i] != other.values[i]) {
299 return false;
300 }
301 }
302 return true;
303}
304
305void PointerCoords::copyFrom(const PointerCoords& other) {
306 bits = other.bits;
Michael Wright38dcdff2014-03-19 12:06:10 -0700307 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700308 for (uint32_t i = 0; i < count; i++) {
309 values[i] = other.values[i];
310 }
311}
312
chaviwc01e1372020-07-01 12:37:31 -0700313void PointerCoords::transform(const ui::Transform& transform) {
314 vec2 newCoords = transform.transform(getX(), getY());
315 setAxisValue(AMOTION_EVENT_AXIS_X, newCoords.x);
316 setAxisValue(AMOTION_EVENT_AXIS_Y, newCoords.y);
317}
Jeff Brown5912f952013-07-01 19:10:31 -0700318
319// --- PointerProperties ---
320
321bool PointerProperties::operator==(const PointerProperties& other) const {
322 return id == other.id
323 && toolType == other.toolType;
324}
325
326void PointerProperties::copyFrom(const PointerProperties& other) {
327 id = other.id;
328 toolType = other.toolType;
329}
330
331
332// --- MotionEvent ---
333
Garfield Tan4cc839f2020-01-24 11:26:14 -0800334void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600335 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
336 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700337 int32_t buttonState, MotionClassification classification,
338 const ui::Transform& transform, float xPrecision, float yPrecision,
Evan Rosky84f07f02021-04-16 10:42:42 -0700339 float rawXCursorPosition, float rawYCursorPosition,
340 int32_t displayWidth, int32_t displayHeight, nsecs_t downTime,
chaviw9eaa22c2020-07-01 16:21:27 -0700341 nsecs_t eventTime, size_t pointerCount,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600342 const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700343 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800344 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700345 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100346 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700347 mFlags = flags;
348 mEdgeFlags = edgeFlags;
349 mMetaState = metaState;
350 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800351 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700352 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700353 mXPrecision = xPrecision;
354 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700355 mRawXCursorPosition = rawXCursorPosition;
356 mRawYCursorPosition = rawYCursorPosition;
Evan Rosky84f07f02021-04-16 10:42:42 -0700357 mDisplayWidth = displayWidth;
358 mDisplayHeight = displayHeight;
Jeff Brown5912f952013-07-01 19:10:31 -0700359 mDownTime = downTime;
360 mPointerProperties.clear();
361 mPointerProperties.appendArray(pointerProperties, pointerCount);
362 mSampleEventTimes.clear();
363 mSamplePointerCoords.clear();
364 addSample(eventTime, pointerCoords);
365}
366
367void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800368 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
369 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700370 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100371 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700372 mFlags = other->mFlags;
373 mEdgeFlags = other->mEdgeFlags;
374 mMetaState = other->mMetaState;
375 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800376 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700377 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700378 mXPrecision = other->mXPrecision;
379 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700380 mRawXCursorPosition = other->mRawXCursorPosition;
381 mRawYCursorPosition = other->mRawYCursorPosition;
Evan Rosky84f07f02021-04-16 10:42:42 -0700382 mDisplayWidth = other->mDisplayWidth;
383 mDisplayHeight = other->mDisplayHeight;
Jeff Brown5912f952013-07-01 19:10:31 -0700384 mDownTime = other->mDownTime;
385 mPointerProperties = other->mPointerProperties;
386
387 if (keepHistory) {
388 mSampleEventTimes = other->mSampleEventTimes;
389 mSamplePointerCoords = other->mSamplePointerCoords;
390 } else {
391 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500392 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700393 mSamplePointerCoords.clear();
394 size_t pointerCount = other->getPointerCount();
395 size_t historySize = other->getHistorySize();
396 mSamplePointerCoords.appendArray(other->mSamplePointerCoords.array()
397 + (historySize * pointerCount), pointerCount);
398 }
399}
400
401void MotionEvent::addSample(
402 int64_t eventTime,
403 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500404 mSampleEventTimes.push_back(eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -0700405 mSamplePointerCoords.appendArray(pointerCoords, getPointerCount());
406}
407
Garfield Tan00f511d2019-06-12 16:55:40 -0700408float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700409 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
410 return vals.x;
Garfield Tan00f511d2019-06-12 16:55:40 -0700411}
412
413float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700414 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
415 return vals.y;
Garfield Tan00f511d2019-06-12 16:55:40 -0700416}
417
Garfield Tan937bb832019-07-25 17:48:31 -0700418void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700419 ui::Transform inverse = mTransform.inverse();
420 vec2 vals = inverse.transform(x, y);
421 mRawXCursorPosition = vals.x;
422 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700423}
424
Jeff Brown5912f952013-07-01 19:10:31 -0700425const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
426 return &mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
427}
428
429float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
Evan Rosky84f07f02021-04-16 10:42:42 -0700430 return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700431}
432
433float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700434 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700435}
436
437const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
438 size_t pointerIndex, size_t historicalIndex) const {
439 return &mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
440}
441
442float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
443 size_t historicalIndex) const {
Evan Rosky84f07f02021-04-16 10:42:42 -0700444 if (axis != AMOTION_EVENT_AXIS_X && axis != AMOTION_EVENT_AXIS_Y) {
445 return getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis);
446 }
447 // 0x7 encapsulates all 3 rotations (see ui::Transform::RotationFlags)
448 static const int ALL_ROTATIONS_MASK = 0x7;
449 uint32_t orientation = (mTransform.getOrientation() & ALL_ROTATIONS_MASK);
450 if (orientation == ui::Transform::ROT_0) {
451 return getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis);
452 }
453
454 // For compatibility, convert raw coordinates into "oriented screen space". Once app developers
455 // are educated about getRaw, we can consider removing this.
456 vec2 xy = getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getXYValue();
457 const float unrotatedX = xy.x;
458 if (orientation == ui::Transform::ROT_90) {
459 xy.x = mDisplayHeight - xy.y;
460 xy.y = unrotatedX;
461 } else if (orientation == ui::Transform::ROT_180) {
462 xy.x = mDisplayWidth - xy.x;
463 xy.y = mDisplayHeight - xy.y;
464 } else if (orientation == ui::Transform::ROT_270) {
465 xy.x = xy.y;
466 xy.y = mDisplayWidth - unrotatedX;
467 }
468 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
469 return xy[axis];
Jeff Brown5912f952013-07-01 19:10:31 -0700470}
471
472float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
473 size_t historicalIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700474 if (axis != AMOTION_EVENT_AXIS_X && axis != AMOTION_EVENT_AXIS_Y) {
475 return getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis);
476 }
477
Evan Rosky84f07f02021-04-16 10:42:42 -0700478 vec2 vals = mTransform.transform(
479 getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getXYValue());
480 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
481 return vals[axis];
Jeff Brown5912f952013-07-01 19:10:31 -0700482}
483
484ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
485 size_t pointerCount = mPointerProperties.size();
486 for (size_t i = 0; i < pointerCount; i++) {
487 if (mPointerProperties.itemAt(i).id == pointerId) {
488 return i;
489 }
490 }
491 return -1;
492}
493
494void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700495 float currXOffset = mTransform.tx();
496 float currYOffset = mTransform.ty();
497 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700498}
499
Robert Carre07e1032018-11-26 12:55:53 -0800500void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700501 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800502 mXPrecision *= globalScaleFactor;
503 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700504
505 size_t numSamples = mSamplePointerCoords.size();
506 for (size_t i = 0; i < numSamples; i++) {
chaviw9eaa22c2020-07-01 16:21:27 -0700507 mSamplePointerCoords.editItemAt(i).scale(globalScaleFactor, globalScaleFactor,
508 globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700509 }
510}
511
chaviw9eaa22c2020-07-01 16:21:27 -0700512static vec2 transformPoint(const std::array<float, 9>& matrix, float x, float y) {
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700513 // Apply perspective transform like Skia.
514 float newX = matrix[0] * x + matrix[1] * y + matrix[2];
515 float newY = matrix[3] * x + matrix[4] * y + matrix[5];
516 float newZ = matrix[6] * x + matrix[7] * y + matrix[8];
517 if (newZ) {
518 newZ = 1.0f / newZ;
519 }
chaviw9eaa22c2020-07-01 16:21:27 -0700520 vec2 transformedPoint;
521 transformedPoint.x = newX * newZ;
522 transformedPoint.y = newY * newZ;
523 return transformedPoint;
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700524}
525
chaviw9eaa22c2020-07-01 16:21:27 -0700526static float transformAngle(const std::array<float, 9>& matrix, float angleRadians, float originX,
527 float originY) {
Jeff Brown5912f952013-07-01 19:10:31 -0700528 // Construct and transform a vector oriented at the specified clockwise angle from vertical.
529 // Coordinate system: down is increasing Y, right is increasing X.
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700530 float x = sinf(angleRadians);
531 float y = -cosf(angleRadians);
chaviw9eaa22c2020-07-01 16:21:27 -0700532 vec2 transformedPoint = transformPoint(matrix, x, y);
533
534 transformedPoint.x -= originX;
535 transformedPoint.y -= originY;
Jeff Brown5912f952013-07-01 19:10:31 -0700536
537 // Derive the transformed vector's clockwise angle from vertical.
chaviw9eaa22c2020-07-01 16:21:27 -0700538 float result = atan2f(transformedPoint.x, -transformedPoint.y);
Jeff Brown5912f952013-07-01 19:10:31 -0700539 if (result < - M_PI_2) {
540 result += M_PI;
541 } else if (result > M_PI_2) {
542 result -= M_PI;
543 }
544 return result;
545}
546
chaviw9eaa22c2020-07-01 16:21:27 -0700547void MotionEvent::transform(const std::array<float, 9>& matrix) {
548 // We want to preserve the rawX and rawY so we just update the transform
549 // using the values of the transform passed in
550 ui::Transform newTransform;
551 newTransform.set(matrix);
552 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700553
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700554 // Determine how the origin is transformed by the matrix so that we
555 // can transform orientation vectors.
chaviw9eaa22c2020-07-01 16:21:27 -0700556 vec2 origin = transformPoint(matrix, 0, 0);
Garfield Tan00f511d2019-06-12 16:55:40 -0700557
Jeff Brown5912f952013-07-01 19:10:31 -0700558 // Apply the transformation to all samples.
559 size_t numSamples = mSamplePointerCoords.size();
560 for (size_t i = 0; i < numSamples; i++) {
561 PointerCoords& c = mSamplePointerCoords.editItemAt(i);
Jeff Brown5912f952013-07-01 19:10:31 -0700562 float orientation = c.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700563 c.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
chaviw9eaa22c2020-07-01 16:21:27 -0700564 transformAngle(matrix, orientation, origin.x, origin.y));
Jeff Brown5912f952013-07-01 19:10:31 -0700565 }
566}
567
Evan Roskyd4d4d802021-05-03 20:12:21 -0700568void MotionEvent::applyTransform(const std::array<float, 9>& matrix) {
569 // Determine how the origin is transformed by the matrix so that we
570 // can transform orientation vectors.
571 vec2 origin = transformPoint(matrix, 0, 0);
572
573 // Apply the transformation to all samples.
574 size_t numSamples = mSamplePointerCoords.size();
575 for (size_t i = 0; i < numSamples; i++) {
576 PointerCoords& c = mSamplePointerCoords.editItemAt(i);
577 float orientation = c.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
578 c.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
579 transformAngle(matrix, orientation, origin.x, origin.y));
580 vec2 xy = transformPoint(matrix, c.getX(), c.getY());
581 c.setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
582 c.setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
583 }
584}
585
Brett Chabotfaa986c2020-11-04 17:39:36 -0800586#ifdef __linux__
chaviw9eaa22c2020-07-01 16:21:27 -0700587static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
588 float dsdx, dtdx, tx, dtdy, dsdy, ty;
589 status_t status = parcel.readFloat(&dsdx);
590 status |= parcel.readFloat(&dtdx);
591 status |= parcel.readFloat(&tx);
592 status |= parcel.readFloat(&dtdy);
593 status |= parcel.readFloat(&dsdy);
594 status |= parcel.readFloat(&ty);
595
596 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
597 return status;
598}
599
600static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
601 status_t status = parcel.writeFloat(transform.dsdx());
602 status |= parcel.writeFloat(transform.dtdx());
603 status |= parcel.writeFloat(transform.tx());
604 status |= parcel.writeFloat(transform.dtdy());
605 status |= parcel.writeFloat(transform.dsdy());
606 status |= parcel.writeFloat(transform.ty());
607 return status;
608}
609
Jeff Brown5912f952013-07-01 19:10:31 -0700610status_t MotionEvent::readFromParcel(Parcel* parcel) {
611 size_t pointerCount = parcel->readInt32();
612 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800613 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
614 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700615 return BAD_VALUE;
616 }
617
Garfield Tan4cc839f2020-01-24 11:26:14 -0800618 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700619 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600620 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800621 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600622 std::vector<uint8_t> hmac;
623 status_t result = parcel->readByteVector(&hmac);
624 if (result != OK || hmac.size() != 32) {
625 return BAD_VALUE;
626 }
627 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700628 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100629 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700630 mFlags = parcel->readInt32();
631 mEdgeFlags = parcel->readInt32();
632 mMetaState = parcel->readInt32();
633 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800634 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700635
636 result = android::readFromParcel(mTransform, *parcel);
637 if (result != OK) {
638 return result;
639 }
Jeff Brown5912f952013-07-01 19:10:31 -0700640 mXPrecision = parcel->readFloat();
641 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700642 mRawXCursorPosition = parcel->readFloat();
643 mRawYCursorPosition = parcel->readFloat();
Evan Rosky84f07f02021-04-16 10:42:42 -0700644 mDisplayWidth = parcel->readInt32();
645 mDisplayHeight = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700646 mDownTime = parcel->readInt64();
647
648 mPointerProperties.clear();
649 mPointerProperties.setCapacity(pointerCount);
650 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500651 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700652 mSamplePointerCoords.clear();
653 mSamplePointerCoords.setCapacity(sampleCount * pointerCount);
654
655 for (size_t i = 0; i < pointerCount; i++) {
656 mPointerProperties.push();
657 PointerProperties& properties = mPointerProperties.editTop();
658 properties.id = parcel->readInt32();
659 properties.toolType = parcel->readInt32();
660 }
661
Dan Austinc94fc452015-09-22 14:22:41 -0700662 while (sampleCount > 0) {
663 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500664 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700665 for (size_t i = 0; i < pointerCount; i++) {
666 mSamplePointerCoords.push();
667 status_t status = mSamplePointerCoords.editTop().readFromParcel(parcel);
668 if (status) {
669 return status;
670 }
671 }
672 }
673 return OK;
674}
675
676status_t MotionEvent::writeToParcel(Parcel* parcel) const {
677 size_t pointerCount = mPointerProperties.size();
678 size_t sampleCount = mSampleEventTimes.size();
679
680 parcel->writeInt32(pointerCount);
681 parcel->writeInt32(sampleCount);
682
Garfield Tan4cc839f2020-01-24 11:26:14 -0800683 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700684 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600685 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800686 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600687 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
688 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700689 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100690 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700691 parcel->writeInt32(mFlags);
692 parcel->writeInt32(mEdgeFlags);
693 parcel->writeInt32(mMetaState);
694 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800695 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700696
697 status_t result = android::writeToParcel(mTransform, *parcel);
698 if (result != OK) {
699 return result;
700 }
Jeff Brown5912f952013-07-01 19:10:31 -0700701 parcel->writeFloat(mXPrecision);
702 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700703 parcel->writeFloat(mRawXCursorPosition);
704 parcel->writeFloat(mRawYCursorPosition);
Evan Rosky84f07f02021-04-16 10:42:42 -0700705 parcel->writeInt32(mDisplayWidth);
706 parcel->writeInt32(mDisplayHeight);
Jeff Brown5912f952013-07-01 19:10:31 -0700707 parcel->writeInt64(mDownTime);
708
709 for (size_t i = 0; i < pointerCount; i++) {
710 const PointerProperties& properties = mPointerProperties.itemAt(i);
711 parcel->writeInt32(properties.id);
712 parcel->writeInt32(properties.toolType);
713 }
714
715 const PointerCoords* pc = mSamplePointerCoords.array();
716 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500717 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700718 for (size_t i = 0; i < pointerCount; i++) {
719 status_t status = (pc++)->writeToParcel(parcel);
720 if (status) {
721 return status;
722 }
723 }
724 }
725 return OK;
726}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800727#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700728
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600729bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Jeff Brown5912f952013-07-01 19:10:31 -0700730 if (source & AINPUT_SOURCE_CLASS_POINTER) {
731 // Specifically excludes HOVER_MOVE and SCROLL.
732 switch (action & AMOTION_EVENT_ACTION_MASK) {
733 case AMOTION_EVENT_ACTION_DOWN:
734 case AMOTION_EVENT_ACTION_MOVE:
735 case AMOTION_EVENT_ACTION_UP:
736 case AMOTION_EVENT_ACTION_POINTER_DOWN:
737 case AMOTION_EVENT_ACTION_POINTER_UP:
738 case AMOTION_EVENT_ACTION_CANCEL:
739 case AMOTION_EVENT_ACTION_OUTSIDE:
740 return true;
741 }
742 }
743 return false;
744}
745
Michael Wright872db4f2014-04-22 15:03:51 -0700746const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700747 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700748}
749
750int32_t MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700751 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700752}
753
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500754std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700755 // Convert MotionEvent action to string
756 switch (action & AMOTION_EVENT_ACTION_MASK) {
757 case AMOTION_EVENT_ACTION_DOWN:
758 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700759 case AMOTION_EVENT_ACTION_UP:
760 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500761 case AMOTION_EVENT_ACTION_MOVE:
762 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700763 case AMOTION_EVENT_ACTION_CANCEL:
764 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500765 case AMOTION_EVENT_ACTION_OUTSIDE:
766 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700767 case AMOTION_EVENT_ACTION_POINTER_DOWN:
768 return "POINTER_DOWN";
769 case AMOTION_EVENT_ACTION_POINTER_UP:
770 return "POINTER_UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500771 case AMOTION_EVENT_ACTION_HOVER_MOVE:
772 return "HOVER_MOVE";
773 case AMOTION_EVENT_ACTION_SCROLL:
774 return "SCROLL";
775 case AMOTION_EVENT_ACTION_HOVER_ENTER:
776 return "HOVER_ENTER";
777 case AMOTION_EVENT_ACTION_HOVER_EXIT:
778 return "HOVER_EXIT";
779 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
780 return "BUTTON_PRESS";
781 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
782 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700783 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500784 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700785}
786
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800787// --- FocusEvent ---
788
Garfield Tan4cc839f2020-01-24 11:26:14 -0800789void FocusEvent::initialize(int32_t id, bool hasFocus, bool inTouchMode) {
790 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600791 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800792 mHasFocus = hasFocus;
793 mInTouchMode = inTouchMode;
794}
795
796void FocusEvent::initialize(const FocusEvent& from) {
797 InputEvent::initialize(from);
798 mHasFocus = from.mHasFocus;
799 mInTouchMode = from.mInTouchMode;
800}
Jeff Brown5912f952013-07-01 19:10:31 -0700801
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800802// --- CaptureEvent ---
803
804void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) {
805 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
806 ADISPLAY_ID_NONE, INVALID_HMAC);
807 mPointerCaptureEnabled = pointerCaptureEnabled;
808}
809
810void CaptureEvent::initialize(const CaptureEvent& from) {
811 InputEvent::initialize(from);
812 mPointerCaptureEnabled = from.mPointerCaptureEnabled;
813}
814
arthurhung7632c332020-12-30 16:58:01 +0800815// --- DragEvent ---
816
817void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) {
818 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
819 ADISPLAY_ID_NONE, INVALID_HMAC);
820 mIsExiting = isExiting;
821 mX = x;
822 mY = y;
823}
824
825void DragEvent::initialize(const DragEvent& from) {
826 InputEvent::initialize(from);
827 mIsExiting = from.mIsExiting;
828 mX = from.mX;
829 mY = from.mY;
830}
831
Jeff Brown5912f952013-07-01 19:10:31 -0700832// --- PooledInputEventFactory ---
833
834PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
835 mMaxPoolSize(maxPoolSize) {
836}
837
838PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -0700839}
840
841KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800842 if (mKeyEventPool.empty()) {
843 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700844 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800845 KeyEvent* event = mKeyEventPool.front().release();
846 mKeyEventPool.pop();
847 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700848}
849
850MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800851 if (mMotionEventPool.empty()) {
852 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700853 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800854 MotionEvent* event = mMotionEventPool.front().release();
855 mMotionEventPool.pop();
856 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700857}
858
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800859FocusEvent* PooledInputEventFactory::createFocusEvent() {
860 if (mFocusEventPool.empty()) {
861 return new FocusEvent();
862 }
863 FocusEvent* event = mFocusEventPool.front().release();
864 mFocusEventPool.pop();
865 return event;
866}
867
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800868CaptureEvent* PooledInputEventFactory::createCaptureEvent() {
869 if (mCaptureEventPool.empty()) {
870 return new CaptureEvent();
871 }
872 CaptureEvent* event = mCaptureEventPool.front().release();
873 mCaptureEventPool.pop();
874 return event;
875}
876
arthurhung7632c332020-12-30 16:58:01 +0800877DragEvent* PooledInputEventFactory::createDragEvent() {
878 if (mDragEventPool.empty()) {
879 return new DragEvent();
880 }
881 DragEvent* event = mDragEventPool.front().release();
882 mDragEventPool.pop();
883 return event;
884}
885
Jeff Brown5912f952013-07-01 19:10:31 -0700886void PooledInputEventFactory::recycle(InputEvent* event) {
887 switch (event->getType()) {
888 case AINPUT_EVENT_TYPE_KEY:
889 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800890 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700891 return;
892 }
893 break;
894 case AINPUT_EVENT_TYPE_MOTION:
895 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800896 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700897 return;
898 }
899 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800900 case AINPUT_EVENT_TYPE_FOCUS:
901 if (mFocusEventPool.size() < mMaxPoolSize) {
902 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
903 return;
904 }
905 break;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800906 case AINPUT_EVENT_TYPE_CAPTURE:
907 if (mCaptureEventPool.size() < mMaxPoolSize) {
908 mCaptureEventPool.push(
909 std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event)));
910 return;
911 }
912 break;
arthurhung7632c332020-12-30 16:58:01 +0800913 case AINPUT_EVENT_TYPE_DRAG:
914 if (mDragEventPool.size() < mMaxPoolSize) {
915 mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event)));
916 return;
917 }
918 break;
Jeff Brown5912f952013-07-01 19:10:31 -0700919 }
920 delete event;
921}
922
923} // namespace android