blob: d954d235071f560f0f079bcdb9666d1d8d5b08af [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
Prabir Pradhan7e1443f2021-07-23 21:01:55 +000026#include <android-base/properties.h>
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050027#include <android-base/stringprintf.h>
Jeff Brown5912f952013-07-01 19:10:31 -070028#include <input/Input.h>
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080029#include <input/InputDevice.h>
Michael Wright872db4f2014-04-22 15:03:51 -070030#include <input/InputEventLabels.h>
Jeff Brown5912f952013-07-01 19:10:31 -070031
Brett Chabotfaa986c2020-11-04 17:39:36 -080032#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -070033#include <binder/Parcel.h>
Brett Chabotfaa986c2020-11-04 17:39:36 -080034#endif
Brett Chabot58208522020-09-09 13:55:24 -070035#ifdef __ANDROID__
Garfield Tan84b087e2020-01-23 10:49:05 -080036#include <sys/random.h>
Jeff Brown5912f952013-07-01 19:10:31 -070037#endif
38
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050039using android::base::StringPrintf;
40
Jeff Brown5912f952013-07-01 19:10:31 -070041namespace android {
42
Prabir Pradhan6b384612021-05-14 16:56:25 -070043namespace {
44
Prabir Pradhan7e1443f2021-07-23 21:01:55 +000045// When per-window-input-rotation is enabled, InputFlinger works in the un-rotated display
46// coordinates and SurfaceFlinger includes the display rotation in the input window transforms.
47bool isPerWindowInputRotationEnabled() {
48 static const bool PER_WINDOW_INPUT_ROTATION =
49 base::GetBoolProperty("persist.debug.per_window_input_rotation", false);
50
51 return PER_WINDOW_INPUT_ROTATION;
52}
53
Prabir Pradhan6b384612021-05-14 16:56:25 -070054float transformAngle(const ui::Transform& transform, float angleRadians) {
55 // Construct and transform a vector oriented at the specified clockwise angle from vertical.
56 // Coordinate system: down is increasing Y, right is increasing X.
57 float x = sinf(angleRadians);
58 float y = -cosf(angleRadians);
59 vec2 transformedPoint = transform.transform(x, y);
60
61 // Determine how the origin is transformed by the matrix so that we
62 // can transform orientation vectors.
63 const vec2 origin = transform.transform(0, 0);
64
65 transformedPoint.x -= origin.x;
66 transformedPoint.y -= origin.y;
67
68 // Derive the transformed vector's clockwise angle from vertical.
69 float result = atan2f(transformedPoint.x, -transformedPoint.y);
70 if (result < -M_PI_2) {
71 result += M_PI;
72 } else if (result > M_PI_2) {
73 result -= M_PI;
74 }
75 return result;
76}
77
78// Rotates the given point to the transform's orientation. If the display width and height are
79// provided, the point is rotated in the screen space. Otherwise, the point is rotated about the
80// origin. This helper is used to avoid the extra overhead of creating new Transforms.
81vec2 rotatePoint(const ui::Transform& transform, float x, float y, int32_t displayWidth = 0,
82 int32_t displayHeight = 0) {
83 // 0x7 encapsulates all 3 rotations (see ui::Transform::RotationFlags)
84 static const int ALL_ROTATIONS_MASK = 0x7;
85 const uint32_t orientation = (transform.getOrientation() & ALL_ROTATIONS_MASK);
86 if (orientation == ui::Transform::ROT_0) {
87 return {x, y};
88 }
89
90 vec2 xy(x, y);
91 if (orientation == ui::Transform::ROT_90) {
92 xy.x = displayHeight - y;
93 xy.y = x;
94 } else if (orientation == ui::Transform::ROT_180) {
95 xy.x = displayWidth - x;
96 xy.y = displayHeight - y;
97 } else if (orientation == ui::Transform::ROT_270) {
98 xy.x = y;
99 xy.y = displayWidth - x;
100 }
101 return xy;
102}
103
104} // namespace
105
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800106const char* motionClassificationToString(MotionClassification classification) {
107 switch (classification) {
108 case MotionClassification::NONE:
109 return "NONE";
110 case MotionClassification::AMBIGUOUS_GESTURE:
111 return "AMBIGUOUS_GESTURE";
112 case MotionClassification::DEEP_PRESS:
113 return "DEEP_PRESS";
114 }
115}
116
Garfield Tan84b087e2020-01-23 10:49:05 -0800117// --- IdGenerator ---
118IdGenerator::IdGenerator(Source source) : mSource(source) {}
119
120int32_t IdGenerator::nextId() const {
121 constexpr uint32_t SEQUENCE_NUMBER_MASK = ~SOURCE_MASK;
122 int32_t id = 0;
123
124// Avoid building against syscall getrandom(2) on host, which will fail build on Mac. Host doesn't
125// use sequence number so just always return mSource.
126#ifdef __ANDROID__
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#endif // __ANDROID__
139
140 return (id & SEQUENCE_NUMBER_MASK) | static_cast<int32_t>(mSource);
141}
142
Jeff Brown5912f952013-07-01 19:10:31 -0700143// --- InputEvent ---
144
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800145const char* inputEventTypeToString(int32_t type) {
146 switch (type) {
147 case AINPUT_EVENT_TYPE_KEY: {
148 return "KEY";
149 }
150 case AINPUT_EVENT_TYPE_MOTION: {
151 return "MOTION";
152 }
153 case AINPUT_EVENT_TYPE_FOCUS: {
154 return "FOCUS";
155 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800156 case AINPUT_EVENT_TYPE_CAPTURE: {
157 return "CAPTURE";
158 }
arthurhung7632c332020-12-30 16:58:01 +0800159 case AINPUT_EVENT_TYPE_DRAG: {
160 return "DRAG";
161 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800162 }
163 return "UNKNOWN";
164}
165
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800166VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
167 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
168 event.getSource(), event.getDisplayId()},
169 event.getAction(),
170 event.getDownTime(),
171 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
172 event.getKeyCode(),
173 event.getScanCode(),
174 event.getMetaState(),
175 event.getRepeatCount()};
176}
177
178VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
179 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
180 event.getSource(), event.getDisplayId()},
181 event.getRawX(0),
182 event.getRawY(0),
183 event.getActionMasked(),
184 event.getDownTime(),
185 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
186 event.getMetaState(),
187 event.getButtonState()};
188}
189
Garfield Tan4cc839f2020-01-24 11:26:14 -0800190void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600191 std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800192 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700193 mDeviceId = deviceId;
194 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100195 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600196 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700197}
198
199void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800200 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700201 mDeviceId = from.mDeviceId;
202 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100203 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600204 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700205}
206
Garfield Tan4cc839f2020-01-24 11:26:14 -0800207int32_t InputEvent::nextId() {
208 static IdGenerator idGen(IdGenerator::Source::OTHER);
209 return idGen.nextId();
210}
211
Jeff Brown5912f952013-07-01 19:10:31 -0700212// --- KeyEvent ---
213
Michael Wright872db4f2014-04-22 15:03:51 -0700214const char* KeyEvent::getLabel(int32_t keyCode) {
Chris Ye4958d062020-08-20 13:21:10 -0700215 return InputEventLookup::getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700216}
217
Michael Wright872db4f2014-04-22 15:03:51 -0700218int32_t KeyEvent::getKeyCodeFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700219 return InputEventLookup::getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700220}
221
Garfield Tan4cc839f2020-01-24 11:26:14 -0800222void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600223 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
224 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
225 nsecs_t downTime, nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800226 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700227 mAction = action;
228 mFlags = flags;
229 mKeyCode = keyCode;
230 mScanCode = scanCode;
231 mMetaState = metaState;
232 mRepeatCount = repeatCount;
233 mDownTime = downTime;
234 mEventTime = eventTime;
235}
236
237void KeyEvent::initialize(const KeyEvent& from) {
238 InputEvent::initialize(from);
239 mAction = from.mAction;
240 mFlags = from.mFlags;
241 mKeyCode = from.mKeyCode;
242 mScanCode = from.mScanCode;
243 mMetaState = from.mMetaState;
244 mRepeatCount = from.mRepeatCount;
245 mDownTime = from.mDownTime;
246 mEventTime = from.mEventTime;
247}
248
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700249const char* KeyEvent::actionToString(int32_t action) {
250 // Convert KeyEvent action to string
251 switch (action) {
252 case AKEY_EVENT_ACTION_DOWN:
253 return "DOWN";
254 case AKEY_EVENT_ACTION_UP:
255 return "UP";
256 case AKEY_EVENT_ACTION_MULTIPLE:
257 return "MULTIPLE";
258 }
259 return "UNKNOWN";
260}
Jeff Brown5912f952013-07-01 19:10:31 -0700261
262// --- PointerCoords ---
263
264float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700265 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700266 return 0;
267 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700268 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700269}
270
271status_t PointerCoords::setAxisValue(int32_t axis, float value) {
272 if (axis < 0 || axis > 63) {
273 return NAME_NOT_FOUND;
274 }
275
Michael Wright38dcdff2014-03-19 12:06:10 -0700276 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
277 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700278 if (value == 0) {
279 return OK; // axes with value 0 do not need to be stored
280 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700281
282 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700283 if (count >= MAX_AXES) {
284 tooManyAxes(axis);
285 return NO_MEMORY;
286 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700287 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700288 for (uint32_t i = count; i > index; i--) {
289 values[i] = values[i - 1];
290 }
291 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700292
Jeff Brown5912f952013-07-01 19:10:31 -0700293 values[index] = value;
294 return OK;
295}
296
297static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
298 float value = c.getAxisValue(axis);
299 if (value != 0) {
300 c.setAxisValue(axis, value * scaleFactor);
301 }
302}
303
Robert Carre07e1032018-11-26 12:55:53 -0800304void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700305 // No need to scale pressure or size since they are normalized.
306 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800307
308 // If there is a global scale factor, it is included in the windowX/YScale
309 // so we don't need to apply it twice to the X/Y axes.
310 // However we don't want to apply any windowXYScale not included in the global scale
311 // to the TOUCH_MAJOR/MINOR coordinates.
312 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
313 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
314 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
315 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
316 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
317 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
318}
319
320void PointerCoords::scale(float globalScaleFactor) {
321 scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700322}
323
Jeff Brownf086ddb2014-02-11 14:28:48 -0800324void PointerCoords::applyOffset(float xOffset, float yOffset) {
325 setAxisValue(AMOTION_EVENT_AXIS_X, getX() + xOffset);
326 setAxisValue(AMOTION_EVENT_AXIS_Y, getY() + yOffset);
327}
328
Brett Chabotfaa986c2020-11-04 17:39:36 -0800329#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700330status_t PointerCoords::readFromParcel(Parcel* parcel) {
331 bits = parcel->readInt64();
332
Michael Wright38dcdff2014-03-19 12:06:10 -0700333 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700334 if (count > MAX_AXES) {
335 return BAD_VALUE;
336 }
337
338 for (uint32_t i = 0; i < count; i++) {
339 values[i] = parcel->readFloat();
340 }
341 return OK;
342}
343
344status_t PointerCoords::writeToParcel(Parcel* parcel) const {
345 parcel->writeInt64(bits);
346
Michael Wright38dcdff2014-03-19 12:06:10 -0700347 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700348 for (uint32_t i = 0; i < count; i++) {
349 parcel->writeFloat(values[i]);
350 }
351 return OK;
352}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800353#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700354
355void PointerCoords::tooManyAxes(int axis) {
356 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
357 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
358}
359
360bool PointerCoords::operator==(const PointerCoords& other) const {
361 if (bits != other.bits) {
362 return false;
363 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700364 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700365 for (uint32_t i = 0; i < count; i++) {
366 if (values[i] != other.values[i]) {
367 return false;
368 }
369 }
370 return true;
371}
372
373void PointerCoords::copyFrom(const PointerCoords& other) {
374 bits = other.bits;
Michael Wright38dcdff2014-03-19 12:06:10 -0700375 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700376 for (uint32_t i = 0; i < count; i++) {
377 values[i] = other.values[i];
378 }
379}
380
chaviwc01e1372020-07-01 12:37:31 -0700381void PointerCoords::transform(const ui::Transform& transform) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700382 const vec2 xy = transform.transform(getXYValue());
383 setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
384 setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
385
386 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) {
387 const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
388 setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val));
389 }
chaviwc01e1372020-07-01 12:37:31 -0700390}
Jeff Brown5912f952013-07-01 19:10:31 -0700391
392// --- PointerProperties ---
393
394bool PointerProperties::operator==(const PointerProperties& other) const {
395 return id == other.id
396 && toolType == other.toolType;
397}
398
399void PointerProperties::copyFrom(const PointerProperties& other) {
400 id = other.id;
401 toolType = other.toolType;
402}
403
404
405// --- MotionEvent ---
406
Garfield Tan4cc839f2020-01-24 11:26:14 -0800407void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600408 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
409 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700410 int32_t buttonState, MotionClassification classification,
411 const ui::Transform& transform, float xPrecision, float yPrecision,
Evan Rosky84f07f02021-04-16 10:42:42 -0700412 float rawXCursorPosition, float rawYCursorPosition,
413 int32_t displayWidth, int32_t displayHeight, nsecs_t downTime,
chaviw9eaa22c2020-07-01 16:21:27 -0700414 nsecs_t eventTime, size_t pointerCount,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600415 const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700416 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800417 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700418 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100419 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700420 mFlags = flags;
421 mEdgeFlags = edgeFlags;
422 mMetaState = metaState;
423 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800424 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700425 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700426 mXPrecision = xPrecision;
427 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700428 mRawXCursorPosition = rawXCursorPosition;
429 mRawYCursorPosition = rawYCursorPosition;
Evan Rosky84f07f02021-04-16 10:42:42 -0700430 mDisplayWidth = displayWidth;
431 mDisplayHeight = displayHeight;
Jeff Brown5912f952013-07-01 19:10:31 -0700432 mDownTime = downTime;
433 mPointerProperties.clear();
434 mPointerProperties.appendArray(pointerProperties, pointerCount);
435 mSampleEventTimes.clear();
436 mSamplePointerCoords.clear();
437 addSample(eventTime, pointerCoords);
438}
439
440void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800441 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
442 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700443 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100444 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700445 mFlags = other->mFlags;
446 mEdgeFlags = other->mEdgeFlags;
447 mMetaState = other->mMetaState;
448 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800449 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700450 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700451 mXPrecision = other->mXPrecision;
452 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700453 mRawXCursorPosition = other->mRawXCursorPosition;
454 mRawYCursorPosition = other->mRawYCursorPosition;
Evan Rosky84f07f02021-04-16 10:42:42 -0700455 mDisplayWidth = other->mDisplayWidth;
456 mDisplayHeight = other->mDisplayHeight;
Jeff Brown5912f952013-07-01 19:10:31 -0700457 mDownTime = other->mDownTime;
458 mPointerProperties = other->mPointerProperties;
459
460 if (keepHistory) {
461 mSampleEventTimes = other->mSampleEventTimes;
462 mSamplePointerCoords = other->mSamplePointerCoords;
463 } else {
464 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500465 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700466 mSamplePointerCoords.clear();
467 size_t pointerCount = other->getPointerCount();
468 size_t historySize = other->getHistorySize();
469 mSamplePointerCoords.appendArray(other->mSamplePointerCoords.array()
470 + (historySize * pointerCount), pointerCount);
471 }
472}
473
474void MotionEvent::addSample(
475 int64_t eventTime,
476 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500477 mSampleEventTimes.push_back(eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -0700478 mSamplePointerCoords.appendArray(pointerCoords, getPointerCount());
479}
480
Garfield Tan00f511d2019-06-12 16:55:40 -0700481float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700482 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
483 return vals.x;
Garfield Tan00f511d2019-06-12 16:55:40 -0700484}
485
486float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700487 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
488 return vals.y;
Garfield Tan00f511d2019-06-12 16:55:40 -0700489}
490
Garfield Tan937bb832019-07-25 17:48:31 -0700491void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700492 ui::Transform inverse = mTransform.inverse();
493 vec2 vals = inverse.transform(x, y);
494 mRawXCursorPosition = vals.x;
495 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700496}
497
Jeff Brown5912f952013-07-01 19:10:31 -0700498const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
499 return &mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
500}
501
502float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
Evan Rosky84f07f02021-04-16 10:42:42 -0700503 return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700504}
505
506float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700507 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700508}
509
510const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
511 size_t pointerIndex, size_t historicalIndex) const {
512 return &mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
513}
514
515float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan6b384612021-05-14 16:56:25 -0700516 size_t historicalIndex) const {
517 const PointerCoords* coords = getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
518
Prabir Pradhan7e1443f2021-07-23 21:01:55 +0000519 if (!isPerWindowInputRotationEnabled()) return coords->getAxisValue(axis);
520
Prabir Pradhan6b384612021-05-14 16:56:25 -0700521 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
522 // For compatibility, convert raw coordinates into "oriented screen space". Once app
523 // developers are educated about getRaw, we can consider removing this.
524 const vec2 xy = rotatePoint(mTransform, coords->getX(), coords->getY(), mDisplayWidth,
525 mDisplayHeight);
526 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
527 return xy[axis];
Evan Rosky84f07f02021-04-16 10:42:42 -0700528 }
529
Prabir Pradhan6b384612021-05-14 16:56:25 -0700530 return coords->getAxisValue(axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700531}
532
533float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
534 size_t historicalIndex) const {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700535 const PointerCoords* coords = getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
536
537 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
538 const vec2 xy = mTransform.transform(coords->getXYValue());
539 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
540 return xy[axis];
chaviw9eaa22c2020-07-01 16:21:27 -0700541 }
542
Prabir Pradhan6b384612021-05-14 16:56:25 -0700543 return coords->getAxisValue(axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700544}
545
546ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
547 size_t pointerCount = mPointerProperties.size();
548 for (size_t i = 0; i < pointerCount; i++) {
549 if (mPointerProperties.itemAt(i).id == pointerId) {
550 return i;
551 }
552 }
553 return -1;
554}
555
556void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700557 float currXOffset = mTransform.tx();
558 float currYOffset = mTransform.ty();
559 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700560}
561
Robert Carre07e1032018-11-26 12:55:53 -0800562void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700563 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800564 mXPrecision *= globalScaleFactor;
565 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700566
567 size_t numSamples = mSamplePointerCoords.size();
568 for (size_t i = 0; i < numSamples; i++) {
chaviw9eaa22c2020-07-01 16:21:27 -0700569 mSamplePointerCoords.editItemAt(i).scale(globalScaleFactor, globalScaleFactor,
570 globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700571 }
572}
573
chaviw9eaa22c2020-07-01 16:21:27 -0700574void MotionEvent::transform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700575 // We want to preserve the raw axes values stored in the PointerCoords, so we just update the
576 // transform using the values passed in.
chaviw9eaa22c2020-07-01 16:21:27 -0700577 ui::Transform newTransform;
578 newTransform.set(matrix);
579 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700580
Prabir Pradhan6b384612021-05-14 16:56:25 -0700581 // We need to update the AXIS_ORIENTATION value here to maintain the old behavior where the
582 // orientation angle is not affected by the initial transformation set in the MotionEvent.
583 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
584 [&newTransform](PointerCoords& c) {
585 float orientation = c.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
586 c.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
587 transformAngle(newTransform, orientation));
588 });
Jeff Brown5912f952013-07-01 19:10:31 -0700589}
590
Evan Roskyd4d4d802021-05-03 20:12:21 -0700591void MotionEvent::applyTransform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700592 ui::Transform transform;
593 transform.set(matrix);
Evan Roskyd4d4d802021-05-03 20:12:21 -0700594
595 // Apply the transformation to all samples.
Prabir Pradhan6b384612021-05-14 16:56:25 -0700596 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
597 [&transform](PointerCoords& c) { c.transform(transform); });
Evan Roskyd4d4d802021-05-03 20:12:21 -0700598}
599
Brett Chabotfaa986c2020-11-04 17:39:36 -0800600#ifdef __linux__
chaviw9eaa22c2020-07-01 16:21:27 -0700601static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
602 float dsdx, dtdx, tx, dtdy, dsdy, ty;
603 status_t status = parcel.readFloat(&dsdx);
604 status |= parcel.readFloat(&dtdx);
605 status |= parcel.readFloat(&tx);
606 status |= parcel.readFloat(&dtdy);
607 status |= parcel.readFloat(&dsdy);
608 status |= parcel.readFloat(&ty);
609
610 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
611 return status;
612}
613
614static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
615 status_t status = parcel.writeFloat(transform.dsdx());
616 status |= parcel.writeFloat(transform.dtdx());
617 status |= parcel.writeFloat(transform.tx());
618 status |= parcel.writeFloat(transform.dtdy());
619 status |= parcel.writeFloat(transform.dsdy());
620 status |= parcel.writeFloat(transform.ty());
621 return status;
622}
623
Jeff Brown5912f952013-07-01 19:10:31 -0700624status_t MotionEvent::readFromParcel(Parcel* parcel) {
625 size_t pointerCount = parcel->readInt32();
626 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800627 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
628 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700629 return BAD_VALUE;
630 }
631
Garfield Tan4cc839f2020-01-24 11:26:14 -0800632 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700633 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600634 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800635 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600636 std::vector<uint8_t> hmac;
637 status_t result = parcel->readByteVector(&hmac);
638 if (result != OK || hmac.size() != 32) {
639 return BAD_VALUE;
640 }
641 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700642 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100643 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700644 mFlags = parcel->readInt32();
645 mEdgeFlags = parcel->readInt32();
646 mMetaState = parcel->readInt32();
647 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800648 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700649
650 result = android::readFromParcel(mTransform, *parcel);
651 if (result != OK) {
652 return result;
653 }
Jeff Brown5912f952013-07-01 19:10:31 -0700654 mXPrecision = parcel->readFloat();
655 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700656 mRawXCursorPosition = parcel->readFloat();
657 mRawYCursorPosition = parcel->readFloat();
Evan Rosky84f07f02021-04-16 10:42:42 -0700658 mDisplayWidth = parcel->readInt32();
659 mDisplayHeight = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700660 mDownTime = parcel->readInt64();
661
662 mPointerProperties.clear();
663 mPointerProperties.setCapacity(pointerCount);
664 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500665 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700666 mSamplePointerCoords.clear();
667 mSamplePointerCoords.setCapacity(sampleCount * pointerCount);
668
669 for (size_t i = 0; i < pointerCount; i++) {
670 mPointerProperties.push();
671 PointerProperties& properties = mPointerProperties.editTop();
672 properties.id = parcel->readInt32();
673 properties.toolType = parcel->readInt32();
674 }
675
Dan Austinc94fc452015-09-22 14:22:41 -0700676 while (sampleCount > 0) {
677 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500678 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700679 for (size_t i = 0; i < pointerCount; i++) {
680 mSamplePointerCoords.push();
681 status_t status = mSamplePointerCoords.editTop().readFromParcel(parcel);
682 if (status) {
683 return status;
684 }
685 }
686 }
687 return OK;
688}
689
690status_t MotionEvent::writeToParcel(Parcel* parcel) const {
691 size_t pointerCount = mPointerProperties.size();
692 size_t sampleCount = mSampleEventTimes.size();
693
694 parcel->writeInt32(pointerCount);
695 parcel->writeInt32(sampleCount);
696
Garfield Tan4cc839f2020-01-24 11:26:14 -0800697 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700698 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600699 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800700 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600701 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
702 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700703 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100704 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700705 parcel->writeInt32(mFlags);
706 parcel->writeInt32(mEdgeFlags);
707 parcel->writeInt32(mMetaState);
708 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800709 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700710
711 status_t result = android::writeToParcel(mTransform, *parcel);
712 if (result != OK) {
713 return result;
714 }
Jeff Brown5912f952013-07-01 19:10:31 -0700715 parcel->writeFloat(mXPrecision);
716 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700717 parcel->writeFloat(mRawXCursorPosition);
718 parcel->writeFloat(mRawYCursorPosition);
Evan Rosky84f07f02021-04-16 10:42:42 -0700719 parcel->writeInt32(mDisplayWidth);
720 parcel->writeInt32(mDisplayHeight);
Jeff Brown5912f952013-07-01 19:10:31 -0700721 parcel->writeInt64(mDownTime);
722
723 for (size_t i = 0; i < pointerCount; i++) {
724 const PointerProperties& properties = mPointerProperties.itemAt(i);
725 parcel->writeInt32(properties.id);
726 parcel->writeInt32(properties.toolType);
727 }
728
729 const PointerCoords* pc = mSamplePointerCoords.array();
730 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500731 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700732 for (size_t i = 0; i < pointerCount; i++) {
733 status_t status = (pc++)->writeToParcel(parcel);
734 if (status) {
735 return status;
736 }
737 }
738 }
739 return OK;
740}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800741#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700742
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600743bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Jeff Brown5912f952013-07-01 19:10:31 -0700744 if (source & AINPUT_SOURCE_CLASS_POINTER) {
745 // Specifically excludes HOVER_MOVE and SCROLL.
746 switch (action & AMOTION_EVENT_ACTION_MASK) {
747 case AMOTION_EVENT_ACTION_DOWN:
748 case AMOTION_EVENT_ACTION_MOVE:
749 case AMOTION_EVENT_ACTION_UP:
750 case AMOTION_EVENT_ACTION_POINTER_DOWN:
751 case AMOTION_EVENT_ACTION_POINTER_UP:
752 case AMOTION_EVENT_ACTION_CANCEL:
753 case AMOTION_EVENT_ACTION_OUTSIDE:
754 return true;
755 }
756 }
757 return false;
758}
759
Michael Wright872db4f2014-04-22 15:03:51 -0700760const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700761 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700762}
763
764int32_t MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700765 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700766}
767
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500768std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700769 // Convert MotionEvent action to string
770 switch (action & AMOTION_EVENT_ACTION_MASK) {
771 case AMOTION_EVENT_ACTION_DOWN:
772 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700773 case AMOTION_EVENT_ACTION_UP:
774 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500775 case AMOTION_EVENT_ACTION_MOVE:
776 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700777 case AMOTION_EVENT_ACTION_CANCEL:
778 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500779 case AMOTION_EVENT_ACTION_OUTSIDE:
780 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700781 case AMOTION_EVENT_ACTION_POINTER_DOWN:
782 return "POINTER_DOWN";
783 case AMOTION_EVENT_ACTION_POINTER_UP:
784 return "POINTER_UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500785 case AMOTION_EVENT_ACTION_HOVER_MOVE:
786 return "HOVER_MOVE";
787 case AMOTION_EVENT_ACTION_SCROLL:
788 return "SCROLL";
789 case AMOTION_EVENT_ACTION_HOVER_ENTER:
790 return "HOVER_ENTER";
791 case AMOTION_EVENT_ACTION_HOVER_EXIT:
792 return "HOVER_EXIT";
793 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
794 return "BUTTON_PRESS";
795 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
796 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700797 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500798 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700799}
800
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800801// --- FocusEvent ---
802
Garfield Tan4cc839f2020-01-24 11:26:14 -0800803void FocusEvent::initialize(int32_t id, bool hasFocus, bool inTouchMode) {
804 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600805 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800806 mHasFocus = hasFocus;
807 mInTouchMode = inTouchMode;
808}
809
810void FocusEvent::initialize(const FocusEvent& from) {
811 InputEvent::initialize(from);
812 mHasFocus = from.mHasFocus;
813 mInTouchMode = from.mInTouchMode;
814}
Jeff Brown5912f952013-07-01 19:10:31 -0700815
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800816// --- CaptureEvent ---
817
818void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) {
819 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
820 ADISPLAY_ID_NONE, INVALID_HMAC);
821 mPointerCaptureEnabled = pointerCaptureEnabled;
822}
823
824void CaptureEvent::initialize(const CaptureEvent& from) {
825 InputEvent::initialize(from);
826 mPointerCaptureEnabled = from.mPointerCaptureEnabled;
827}
828
arthurhung7632c332020-12-30 16:58:01 +0800829// --- DragEvent ---
830
831void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) {
832 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
833 ADISPLAY_ID_NONE, INVALID_HMAC);
834 mIsExiting = isExiting;
835 mX = x;
836 mY = y;
837}
838
839void DragEvent::initialize(const DragEvent& from) {
840 InputEvent::initialize(from);
841 mIsExiting = from.mIsExiting;
842 mX = from.mX;
843 mY = from.mY;
844}
845
Jeff Brown5912f952013-07-01 19:10:31 -0700846// --- PooledInputEventFactory ---
847
848PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
849 mMaxPoolSize(maxPoolSize) {
850}
851
852PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -0700853}
854
855KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800856 if (mKeyEventPool.empty()) {
857 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700858 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800859 KeyEvent* event = mKeyEventPool.front().release();
860 mKeyEventPool.pop();
861 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700862}
863
864MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800865 if (mMotionEventPool.empty()) {
866 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700867 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800868 MotionEvent* event = mMotionEventPool.front().release();
869 mMotionEventPool.pop();
870 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700871}
872
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800873FocusEvent* PooledInputEventFactory::createFocusEvent() {
874 if (mFocusEventPool.empty()) {
875 return new FocusEvent();
876 }
877 FocusEvent* event = mFocusEventPool.front().release();
878 mFocusEventPool.pop();
879 return event;
880}
881
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800882CaptureEvent* PooledInputEventFactory::createCaptureEvent() {
883 if (mCaptureEventPool.empty()) {
884 return new CaptureEvent();
885 }
886 CaptureEvent* event = mCaptureEventPool.front().release();
887 mCaptureEventPool.pop();
888 return event;
889}
890
arthurhung7632c332020-12-30 16:58:01 +0800891DragEvent* PooledInputEventFactory::createDragEvent() {
892 if (mDragEventPool.empty()) {
893 return new DragEvent();
894 }
895 DragEvent* event = mDragEventPool.front().release();
896 mDragEventPool.pop();
897 return event;
898}
899
Jeff Brown5912f952013-07-01 19:10:31 -0700900void PooledInputEventFactory::recycle(InputEvent* event) {
901 switch (event->getType()) {
902 case AINPUT_EVENT_TYPE_KEY:
903 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800904 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700905 return;
906 }
907 break;
908 case AINPUT_EVENT_TYPE_MOTION:
909 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800910 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700911 return;
912 }
913 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800914 case AINPUT_EVENT_TYPE_FOCUS:
915 if (mFocusEventPool.size() < mMaxPoolSize) {
916 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
917 return;
918 }
919 break;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800920 case AINPUT_EVENT_TYPE_CAPTURE:
921 if (mCaptureEventPool.size() < mMaxPoolSize) {
922 mCaptureEventPool.push(
923 std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event)));
924 return;
925 }
926 break;
arthurhung7632c332020-12-30 16:58:01 +0800927 case AINPUT_EVENT_TYPE_DRAG:
928 if (mDragEventPool.size() < mMaxPoolSize) {
929 mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event)));
930 return;
931 }
932 break;
Jeff Brown5912f952013-07-01 19:10:31 -0700933 }
934 delete event;
935}
936
937} // namespace android