blob: e83341ac86b8ace37df040c383fbb95d64dabec2 [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
Prabir Pradhan9f388812021-05-13 16:54:53 -070094vec2 applyTransformWithoutTranslation(const ui::Transform& transform, float x, float y) {
95 const vec2 transformedXy = transform.transform(x, y);
96 const vec2 transformedOrigin = transform.transform(0, 0);
97 return transformedXy - transformedOrigin;
98}
99
100bool shouldDisregardWindowTranslation(uint32_t source) {
101 // Pointer events are the only type of events that refer to absolute coordinates on the display,
102 // so we should apply the entire window transform. For other types of events, we should make
103 // sure to not apply the window translation/offset.
104 return (source & AINPUT_SOURCE_CLASS_POINTER) == 0;
105}
106
Prabir Pradhan6b384612021-05-14 16:56:25 -0700107} // namespace
108
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800109const char* motionClassificationToString(MotionClassification classification) {
110 switch (classification) {
111 case MotionClassification::NONE:
112 return "NONE";
113 case MotionClassification::AMBIGUOUS_GESTURE:
114 return "AMBIGUOUS_GESTURE";
115 case MotionClassification::DEEP_PRESS:
116 return "DEEP_PRESS";
117 }
118}
119
Garfield Tan84b087e2020-01-23 10:49:05 -0800120// --- IdGenerator ---
121IdGenerator::IdGenerator(Source source) : mSource(source) {}
122
123int32_t IdGenerator::nextId() const {
124 constexpr uint32_t SEQUENCE_NUMBER_MASK = ~SOURCE_MASK;
125 int32_t id = 0;
126
127// Avoid building against syscall getrandom(2) on host, which will fail build on Mac. Host doesn't
128// use sequence number so just always return mSource.
129#ifdef __ANDROID__
130 constexpr size_t BUF_LEN = sizeof(id);
131 size_t totalBytes = 0;
132 while (totalBytes < BUF_LEN) {
133 ssize_t bytes = TEMP_FAILURE_RETRY(getrandom(&id, BUF_LEN, GRND_NONBLOCK));
134 if (CC_UNLIKELY(bytes < 0)) {
135 ALOGW("Failed to fill in random number for sequence number: %s.", strerror(errno));
136 id = 0;
137 break;
138 }
139 totalBytes += bytes;
140 }
141#endif // __ANDROID__
142
143 return (id & SEQUENCE_NUMBER_MASK) | static_cast<int32_t>(mSource);
144}
145
Jeff Brown5912f952013-07-01 19:10:31 -0700146// --- InputEvent ---
147
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800148const char* inputEventTypeToString(int32_t type) {
149 switch (type) {
150 case AINPUT_EVENT_TYPE_KEY: {
151 return "KEY";
152 }
153 case AINPUT_EVENT_TYPE_MOTION: {
154 return "MOTION";
155 }
156 case AINPUT_EVENT_TYPE_FOCUS: {
157 return "FOCUS";
158 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800159 case AINPUT_EVENT_TYPE_CAPTURE: {
160 return "CAPTURE";
161 }
arthurhung7632c332020-12-30 16:58:01 +0800162 case AINPUT_EVENT_TYPE_DRAG: {
163 return "DRAG";
164 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800165 }
166 return "UNKNOWN";
167}
168
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800169VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
170 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
171 event.getSource(), event.getDisplayId()},
172 event.getAction(),
173 event.getDownTime(),
174 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
175 event.getKeyCode(),
176 event.getScanCode(),
177 event.getMetaState(),
178 event.getRepeatCount()};
179}
180
181VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
182 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
183 event.getSource(), event.getDisplayId()},
184 event.getRawX(0),
185 event.getRawY(0),
186 event.getActionMasked(),
187 event.getDownTime(),
188 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
189 event.getMetaState(),
190 event.getButtonState()};
191}
192
Garfield Tan4cc839f2020-01-24 11:26:14 -0800193void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600194 std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800195 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700196 mDeviceId = deviceId;
197 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100198 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600199 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700200}
201
202void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800203 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700204 mDeviceId = from.mDeviceId;
205 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100206 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600207 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700208}
209
Garfield Tan4cc839f2020-01-24 11:26:14 -0800210int32_t InputEvent::nextId() {
211 static IdGenerator idGen(IdGenerator::Source::OTHER);
212 return idGen.nextId();
213}
214
Jeff Brown5912f952013-07-01 19:10:31 -0700215// --- KeyEvent ---
216
Michael Wright872db4f2014-04-22 15:03:51 -0700217const char* KeyEvent::getLabel(int32_t keyCode) {
Chris Ye4958d062020-08-20 13:21:10 -0700218 return InputEventLookup::getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700219}
220
Michael Wright872db4f2014-04-22 15:03:51 -0700221int32_t KeyEvent::getKeyCodeFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700222 return InputEventLookup::getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700223}
224
Garfield Tan4cc839f2020-01-24 11:26:14 -0800225void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600226 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
227 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
228 nsecs_t downTime, nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800229 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700230 mAction = action;
231 mFlags = flags;
232 mKeyCode = keyCode;
233 mScanCode = scanCode;
234 mMetaState = metaState;
235 mRepeatCount = repeatCount;
236 mDownTime = downTime;
237 mEventTime = eventTime;
238}
239
240void KeyEvent::initialize(const KeyEvent& from) {
241 InputEvent::initialize(from);
242 mAction = from.mAction;
243 mFlags = from.mFlags;
244 mKeyCode = from.mKeyCode;
245 mScanCode = from.mScanCode;
246 mMetaState = from.mMetaState;
247 mRepeatCount = from.mRepeatCount;
248 mDownTime = from.mDownTime;
249 mEventTime = from.mEventTime;
250}
251
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700252const char* KeyEvent::actionToString(int32_t action) {
253 // Convert KeyEvent action to string
254 switch (action) {
255 case AKEY_EVENT_ACTION_DOWN:
256 return "DOWN";
257 case AKEY_EVENT_ACTION_UP:
258 return "UP";
259 case AKEY_EVENT_ACTION_MULTIPLE:
260 return "MULTIPLE";
261 }
262 return "UNKNOWN";
263}
Jeff Brown5912f952013-07-01 19:10:31 -0700264
265// --- PointerCoords ---
266
267float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700268 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700269 return 0;
270 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700271 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700272}
273
274status_t PointerCoords::setAxisValue(int32_t axis, float value) {
275 if (axis < 0 || axis > 63) {
276 return NAME_NOT_FOUND;
277 }
278
Michael Wright38dcdff2014-03-19 12:06:10 -0700279 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
280 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700281 if (value == 0) {
282 return OK; // axes with value 0 do not need to be stored
283 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700284
285 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700286 if (count >= MAX_AXES) {
287 tooManyAxes(axis);
288 return NO_MEMORY;
289 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700290 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700291 for (uint32_t i = count; i > index; i--) {
292 values[i] = values[i - 1];
293 }
294 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700295
Jeff Brown5912f952013-07-01 19:10:31 -0700296 values[index] = value;
297 return OK;
298}
299
300static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
301 float value = c.getAxisValue(axis);
302 if (value != 0) {
303 c.setAxisValue(axis, value * scaleFactor);
304 }
305}
306
Robert Carre07e1032018-11-26 12:55:53 -0800307void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700308 // No need to scale pressure or size since they are normalized.
309 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800310
311 // If there is a global scale factor, it is included in the windowX/YScale
312 // so we don't need to apply it twice to the X/Y axes.
313 // However we don't want to apply any windowXYScale not included in the global scale
314 // to the TOUCH_MAJOR/MINOR coordinates.
315 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
316 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
317 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
318 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
319 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
320 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
321}
322
323void PointerCoords::scale(float globalScaleFactor) {
324 scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700325}
326
Jeff Brownf086ddb2014-02-11 14:28:48 -0800327void PointerCoords::applyOffset(float xOffset, float yOffset) {
328 setAxisValue(AMOTION_EVENT_AXIS_X, getX() + xOffset);
329 setAxisValue(AMOTION_EVENT_AXIS_Y, getY() + yOffset);
330}
331
Brett Chabotfaa986c2020-11-04 17:39:36 -0800332#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700333status_t PointerCoords::readFromParcel(Parcel* parcel) {
334 bits = parcel->readInt64();
335
Michael Wright38dcdff2014-03-19 12:06:10 -0700336 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700337 if (count > MAX_AXES) {
338 return BAD_VALUE;
339 }
340
341 for (uint32_t i = 0; i < count; i++) {
342 values[i] = parcel->readFloat();
343 }
344 return OK;
345}
346
347status_t PointerCoords::writeToParcel(Parcel* parcel) const {
348 parcel->writeInt64(bits);
349
Michael Wright38dcdff2014-03-19 12:06:10 -0700350 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700351 for (uint32_t i = 0; i < count; i++) {
352 parcel->writeFloat(values[i]);
353 }
354 return OK;
355}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800356#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700357
358void PointerCoords::tooManyAxes(int axis) {
359 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
360 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
361}
362
363bool PointerCoords::operator==(const PointerCoords& other) const {
364 if (bits != other.bits) {
365 return false;
366 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700367 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700368 for (uint32_t i = 0; i < count; i++) {
369 if (values[i] != other.values[i]) {
370 return false;
371 }
372 }
373 return true;
374}
375
376void PointerCoords::copyFrom(const PointerCoords& other) {
377 bits = other.bits;
Michael Wright38dcdff2014-03-19 12:06:10 -0700378 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700379 for (uint32_t i = 0; i < count; i++) {
380 values[i] = other.values[i];
381 }
382}
383
chaviwc01e1372020-07-01 12:37:31 -0700384void PointerCoords::transform(const ui::Transform& transform) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700385 const vec2 xy = transform.transform(getXYValue());
386 setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
387 setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
388
389 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) {
390 const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
391 setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val));
392 }
chaviwc01e1372020-07-01 12:37:31 -0700393}
Jeff Brown5912f952013-07-01 19:10:31 -0700394
395// --- PointerProperties ---
396
397bool PointerProperties::operator==(const PointerProperties& other) const {
398 return id == other.id
399 && toolType == other.toolType;
400}
401
402void PointerProperties::copyFrom(const PointerProperties& other) {
403 id = other.id;
404 toolType = other.toolType;
405}
406
407
408// --- MotionEvent ---
409
Garfield Tan4cc839f2020-01-24 11:26:14 -0800410void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600411 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
412 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700413 int32_t buttonState, MotionClassification classification,
414 const ui::Transform& transform, float xPrecision, float yPrecision,
Evan Rosky84f07f02021-04-16 10:42:42 -0700415 float rawXCursorPosition, float rawYCursorPosition,
416 int32_t displayWidth, int32_t displayHeight, nsecs_t downTime,
chaviw9eaa22c2020-07-01 16:21:27 -0700417 nsecs_t eventTime, size_t pointerCount,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600418 const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700419 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800420 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700421 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100422 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700423 mFlags = flags;
424 mEdgeFlags = edgeFlags;
425 mMetaState = metaState;
426 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800427 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700428 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700429 mXPrecision = xPrecision;
430 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700431 mRawXCursorPosition = rawXCursorPosition;
432 mRawYCursorPosition = rawYCursorPosition;
Evan Rosky84f07f02021-04-16 10:42:42 -0700433 mDisplayWidth = displayWidth;
434 mDisplayHeight = displayHeight;
Jeff Brown5912f952013-07-01 19:10:31 -0700435 mDownTime = downTime;
436 mPointerProperties.clear();
437 mPointerProperties.appendArray(pointerProperties, pointerCount);
438 mSampleEventTimes.clear();
439 mSamplePointerCoords.clear();
440 addSample(eventTime, pointerCoords);
441}
442
443void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800444 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
445 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700446 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100447 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700448 mFlags = other->mFlags;
449 mEdgeFlags = other->mEdgeFlags;
450 mMetaState = other->mMetaState;
451 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800452 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700453 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700454 mXPrecision = other->mXPrecision;
455 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700456 mRawXCursorPosition = other->mRawXCursorPosition;
457 mRawYCursorPosition = other->mRawYCursorPosition;
Evan Rosky84f07f02021-04-16 10:42:42 -0700458 mDisplayWidth = other->mDisplayWidth;
459 mDisplayHeight = other->mDisplayHeight;
Jeff Brown5912f952013-07-01 19:10:31 -0700460 mDownTime = other->mDownTime;
461 mPointerProperties = other->mPointerProperties;
462
463 if (keepHistory) {
464 mSampleEventTimes = other->mSampleEventTimes;
465 mSamplePointerCoords = other->mSamplePointerCoords;
466 } else {
467 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500468 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700469 mSamplePointerCoords.clear();
470 size_t pointerCount = other->getPointerCount();
471 size_t historySize = other->getHistorySize();
472 mSamplePointerCoords.appendArray(other->mSamplePointerCoords.array()
473 + (historySize * pointerCount), pointerCount);
474 }
475}
476
477void MotionEvent::addSample(
478 int64_t eventTime,
479 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500480 mSampleEventTimes.push_back(eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -0700481 mSamplePointerCoords.appendArray(pointerCoords, getPointerCount());
482}
483
Garfield Tan00f511d2019-06-12 16:55:40 -0700484float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700485 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
486 return vals.x;
Garfield Tan00f511d2019-06-12 16:55:40 -0700487}
488
489float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700490 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
491 return vals.y;
Garfield Tan00f511d2019-06-12 16:55:40 -0700492}
493
Garfield Tan937bb832019-07-25 17:48:31 -0700494void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700495 ui::Transform inverse = mTransform.inverse();
496 vec2 vals = inverse.transform(x, y);
497 mRawXCursorPosition = vals.x;
498 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700499}
500
Jeff Brown5912f952013-07-01 19:10:31 -0700501const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
502 return &mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
503}
504
505float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
Evan Rosky84f07f02021-04-16 10:42:42 -0700506 return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700507}
508
509float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700510 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700511}
512
513const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
514 size_t pointerIndex, size_t historicalIndex) const {
515 return &mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
516}
517
518float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan6b384612021-05-14 16:56:25 -0700519 size_t historicalIndex) const {
520 const PointerCoords* coords = getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
521
522 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
523 // For compatibility, convert raw coordinates into "oriented screen space". Once app
524 // developers are educated about getRaw, we can consider removing this.
Prabir Pradhan9f388812021-05-13 16:54:53 -0700525 const vec2 xy = shouldDisregardWindowTranslation(mSource)
526 ? rotatePoint(mTransform, coords->getX(), coords->getY())
527 : rotatePoint(mTransform, coords->getX(), coords->getY(), mDisplayWidth,
528 mDisplayHeight);
Prabir Pradhan6b384612021-05-14 16:56:25 -0700529 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
530 return xy[axis];
Evan Rosky84f07f02021-04-16 10:42:42 -0700531 }
532
Prabir Pradhan6b384612021-05-14 16:56:25 -0700533 return coords->getAxisValue(axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700534}
535
536float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan9f388812021-05-13 16:54:53 -0700537 size_t historicalIndex) const {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700538 const PointerCoords* coords = getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
539
540 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
Prabir Pradhan9f388812021-05-13 16:54:53 -0700541 const vec2 xy = shouldDisregardWindowTranslation(mSource)
542 ? applyTransformWithoutTranslation(mTransform, coords->getX(), coords->getY())
543 : mTransform.transform(coords->getXYValue());
Prabir Pradhan6b384612021-05-14 16:56:25 -0700544 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
545 return xy[axis];
chaviw9eaa22c2020-07-01 16:21:27 -0700546 }
547
Prabir Pradhan6b384612021-05-14 16:56:25 -0700548 return coords->getAxisValue(axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700549}
550
551ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
552 size_t pointerCount = mPointerProperties.size();
553 for (size_t i = 0; i < pointerCount; i++) {
554 if (mPointerProperties.itemAt(i).id == pointerId) {
555 return i;
556 }
557 }
558 return -1;
559}
560
561void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700562 float currXOffset = mTransform.tx();
563 float currYOffset = mTransform.ty();
564 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700565}
566
Robert Carre07e1032018-11-26 12:55:53 -0800567void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700568 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800569 mXPrecision *= globalScaleFactor;
570 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700571
572 size_t numSamples = mSamplePointerCoords.size();
573 for (size_t i = 0; i < numSamples; i++) {
chaviw9eaa22c2020-07-01 16:21:27 -0700574 mSamplePointerCoords.editItemAt(i).scale(globalScaleFactor, globalScaleFactor,
575 globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700576 }
577}
578
chaviw9eaa22c2020-07-01 16:21:27 -0700579void MotionEvent::transform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700580 // We want to preserve the raw axes values stored in the PointerCoords, so we just update the
581 // transform using the values passed in.
chaviw9eaa22c2020-07-01 16:21:27 -0700582 ui::Transform newTransform;
583 newTransform.set(matrix);
584 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700585
Prabir Pradhan6b384612021-05-14 16:56:25 -0700586 // We need to update the AXIS_ORIENTATION value here to maintain the old behavior where the
587 // orientation angle is not affected by the initial transformation set in the MotionEvent.
588 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
589 [&newTransform](PointerCoords& c) {
590 float orientation = c.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
591 c.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
592 transformAngle(newTransform, orientation));
593 });
Jeff Brown5912f952013-07-01 19:10:31 -0700594}
595
Evan Roskyd4d4d802021-05-03 20:12:21 -0700596void MotionEvent::applyTransform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700597 ui::Transform transform;
598 transform.set(matrix);
Evan Roskyd4d4d802021-05-03 20:12:21 -0700599
600 // Apply the transformation to all samples.
Prabir Pradhan6b384612021-05-14 16:56:25 -0700601 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
602 [&transform](PointerCoords& c) { c.transform(transform); });
Evan Roskyd4d4d802021-05-03 20:12:21 -0700603}
604
Brett Chabotfaa986c2020-11-04 17:39:36 -0800605#ifdef __linux__
chaviw9eaa22c2020-07-01 16:21:27 -0700606static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
607 float dsdx, dtdx, tx, dtdy, dsdy, ty;
608 status_t status = parcel.readFloat(&dsdx);
609 status |= parcel.readFloat(&dtdx);
610 status |= parcel.readFloat(&tx);
611 status |= parcel.readFloat(&dtdy);
612 status |= parcel.readFloat(&dsdy);
613 status |= parcel.readFloat(&ty);
614
615 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
616 return status;
617}
618
619static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
620 status_t status = parcel.writeFloat(transform.dsdx());
621 status |= parcel.writeFloat(transform.dtdx());
622 status |= parcel.writeFloat(transform.tx());
623 status |= parcel.writeFloat(transform.dtdy());
624 status |= parcel.writeFloat(transform.dsdy());
625 status |= parcel.writeFloat(transform.ty());
626 return status;
627}
628
Jeff Brown5912f952013-07-01 19:10:31 -0700629status_t MotionEvent::readFromParcel(Parcel* parcel) {
630 size_t pointerCount = parcel->readInt32();
631 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800632 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
633 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700634 return BAD_VALUE;
635 }
636
Garfield Tan4cc839f2020-01-24 11:26:14 -0800637 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700638 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600639 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800640 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600641 std::vector<uint8_t> hmac;
642 status_t result = parcel->readByteVector(&hmac);
643 if (result != OK || hmac.size() != 32) {
644 return BAD_VALUE;
645 }
646 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700647 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100648 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700649 mFlags = parcel->readInt32();
650 mEdgeFlags = parcel->readInt32();
651 mMetaState = parcel->readInt32();
652 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800653 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700654
655 result = android::readFromParcel(mTransform, *parcel);
656 if (result != OK) {
657 return result;
658 }
Jeff Brown5912f952013-07-01 19:10:31 -0700659 mXPrecision = parcel->readFloat();
660 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700661 mRawXCursorPosition = parcel->readFloat();
662 mRawYCursorPosition = parcel->readFloat();
Evan Rosky84f07f02021-04-16 10:42:42 -0700663 mDisplayWidth = parcel->readInt32();
664 mDisplayHeight = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700665 mDownTime = parcel->readInt64();
666
667 mPointerProperties.clear();
668 mPointerProperties.setCapacity(pointerCount);
669 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500670 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700671 mSamplePointerCoords.clear();
672 mSamplePointerCoords.setCapacity(sampleCount * pointerCount);
673
674 for (size_t i = 0; i < pointerCount; i++) {
675 mPointerProperties.push();
676 PointerProperties& properties = mPointerProperties.editTop();
677 properties.id = parcel->readInt32();
678 properties.toolType = parcel->readInt32();
679 }
680
Dan Austinc94fc452015-09-22 14:22:41 -0700681 while (sampleCount > 0) {
682 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500683 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700684 for (size_t i = 0; i < pointerCount; i++) {
685 mSamplePointerCoords.push();
686 status_t status = mSamplePointerCoords.editTop().readFromParcel(parcel);
687 if (status) {
688 return status;
689 }
690 }
691 }
692 return OK;
693}
694
695status_t MotionEvent::writeToParcel(Parcel* parcel) const {
696 size_t pointerCount = mPointerProperties.size();
697 size_t sampleCount = mSampleEventTimes.size();
698
699 parcel->writeInt32(pointerCount);
700 parcel->writeInt32(sampleCount);
701
Garfield Tan4cc839f2020-01-24 11:26:14 -0800702 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700703 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600704 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800705 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600706 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
707 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700708 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100709 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700710 parcel->writeInt32(mFlags);
711 parcel->writeInt32(mEdgeFlags);
712 parcel->writeInt32(mMetaState);
713 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800714 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700715
716 status_t result = android::writeToParcel(mTransform, *parcel);
717 if (result != OK) {
718 return result;
719 }
Jeff Brown5912f952013-07-01 19:10:31 -0700720 parcel->writeFloat(mXPrecision);
721 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700722 parcel->writeFloat(mRawXCursorPosition);
723 parcel->writeFloat(mRawYCursorPosition);
Evan Rosky84f07f02021-04-16 10:42:42 -0700724 parcel->writeInt32(mDisplayWidth);
725 parcel->writeInt32(mDisplayHeight);
Jeff Brown5912f952013-07-01 19:10:31 -0700726 parcel->writeInt64(mDownTime);
727
728 for (size_t i = 0; i < pointerCount; i++) {
729 const PointerProperties& properties = mPointerProperties.itemAt(i);
730 parcel->writeInt32(properties.id);
731 parcel->writeInt32(properties.toolType);
732 }
733
734 const PointerCoords* pc = mSamplePointerCoords.array();
735 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500736 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700737 for (size_t i = 0; i < pointerCount; i++) {
738 status_t status = (pc++)->writeToParcel(parcel);
739 if (status) {
740 return status;
741 }
742 }
743 }
744 return OK;
745}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800746#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700747
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600748bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Jeff Brown5912f952013-07-01 19:10:31 -0700749 if (source & AINPUT_SOURCE_CLASS_POINTER) {
750 // Specifically excludes HOVER_MOVE and SCROLL.
751 switch (action & AMOTION_EVENT_ACTION_MASK) {
752 case AMOTION_EVENT_ACTION_DOWN:
753 case AMOTION_EVENT_ACTION_MOVE:
754 case AMOTION_EVENT_ACTION_UP:
755 case AMOTION_EVENT_ACTION_POINTER_DOWN:
756 case AMOTION_EVENT_ACTION_POINTER_UP:
757 case AMOTION_EVENT_ACTION_CANCEL:
758 case AMOTION_EVENT_ACTION_OUTSIDE:
759 return true;
760 }
761 }
762 return false;
763}
764
Michael Wright872db4f2014-04-22 15:03:51 -0700765const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700766 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700767}
768
769int32_t MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700770 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700771}
772
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500773std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700774 // Convert MotionEvent action to string
775 switch (action & AMOTION_EVENT_ACTION_MASK) {
776 case AMOTION_EVENT_ACTION_DOWN:
777 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700778 case AMOTION_EVENT_ACTION_UP:
779 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500780 case AMOTION_EVENT_ACTION_MOVE:
781 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700782 case AMOTION_EVENT_ACTION_CANCEL:
783 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500784 case AMOTION_EVENT_ACTION_OUTSIDE:
785 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700786 case AMOTION_EVENT_ACTION_POINTER_DOWN:
787 return "POINTER_DOWN";
788 case AMOTION_EVENT_ACTION_POINTER_UP:
789 return "POINTER_UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500790 case AMOTION_EVENT_ACTION_HOVER_MOVE:
791 return "HOVER_MOVE";
792 case AMOTION_EVENT_ACTION_SCROLL:
793 return "SCROLL";
794 case AMOTION_EVENT_ACTION_HOVER_ENTER:
795 return "HOVER_ENTER";
796 case AMOTION_EVENT_ACTION_HOVER_EXIT:
797 return "HOVER_EXIT";
798 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
799 return "BUTTON_PRESS";
800 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
801 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700802 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500803 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700804}
805
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800806// --- FocusEvent ---
807
Garfield Tan4cc839f2020-01-24 11:26:14 -0800808void FocusEvent::initialize(int32_t id, bool hasFocus, bool inTouchMode) {
809 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600810 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800811 mHasFocus = hasFocus;
812 mInTouchMode = inTouchMode;
813}
814
815void FocusEvent::initialize(const FocusEvent& from) {
816 InputEvent::initialize(from);
817 mHasFocus = from.mHasFocus;
818 mInTouchMode = from.mInTouchMode;
819}
Jeff Brown5912f952013-07-01 19:10:31 -0700820
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800821// --- CaptureEvent ---
822
823void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) {
824 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
825 ADISPLAY_ID_NONE, INVALID_HMAC);
826 mPointerCaptureEnabled = pointerCaptureEnabled;
827}
828
829void CaptureEvent::initialize(const CaptureEvent& from) {
830 InputEvent::initialize(from);
831 mPointerCaptureEnabled = from.mPointerCaptureEnabled;
832}
833
arthurhung7632c332020-12-30 16:58:01 +0800834// --- DragEvent ---
835
836void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) {
837 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
838 ADISPLAY_ID_NONE, INVALID_HMAC);
839 mIsExiting = isExiting;
840 mX = x;
841 mY = y;
842}
843
844void DragEvent::initialize(const DragEvent& from) {
845 InputEvent::initialize(from);
846 mIsExiting = from.mIsExiting;
847 mX = from.mX;
848 mY = from.mY;
849}
850
Jeff Brown5912f952013-07-01 19:10:31 -0700851// --- PooledInputEventFactory ---
852
853PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
854 mMaxPoolSize(maxPoolSize) {
855}
856
857PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -0700858}
859
860KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800861 if (mKeyEventPool.empty()) {
862 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700863 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800864 KeyEvent* event = mKeyEventPool.front().release();
865 mKeyEventPool.pop();
866 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700867}
868
869MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800870 if (mMotionEventPool.empty()) {
871 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700872 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800873 MotionEvent* event = mMotionEventPool.front().release();
874 mMotionEventPool.pop();
875 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700876}
877
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800878FocusEvent* PooledInputEventFactory::createFocusEvent() {
879 if (mFocusEventPool.empty()) {
880 return new FocusEvent();
881 }
882 FocusEvent* event = mFocusEventPool.front().release();
883 mFocusEventPool.pop();
884 return event;
885}
886
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800887CaptureEvent* PooledInputEventFactory::createCaptureEvent() {
888 if (mCaptureEventPool.empty()) {
889 return new CaptureEvent();
890 }
891 CaptureEvent* event = mCaptureEventPool.front().release();
892 mCaptureEventPool.pop();
893 return event;
894}
895
arthurhung7632c332020-12-30 16:58:01 +0800896DragEvent* PooledInputEventFactory::createDragEvent() {
897 if (mDragEventPool.empty()) {
898 return new DragEvent();
899 }
900 DragEvent* event = mDragEventPool.front().release();
901 mDragEventPool.pop();
902 return event;
903}
904
Jeff Brown5912f952013-07-01 19:10:31 -0700905void PooledInputEventFactory::recycle(InputEvent* event) {
906 switch (event->getType()) {
907 case AINPUT_EVENT_TYPE_KEY:
908 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800909 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700910 return;
911 }
912 break;
913 case AINPUT_EVENT_TYPE_MOTION:
914 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800915 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700916 return;
917 }
918 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800919 case AINPUT_EVENT_TYPE_FOCUS:
920 if (mFocusEventPool.size() < mMaxPoolSize) {
921 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
922 return;
923 }
924 break;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800925 case AINPUT_EVENT_TYPE_CAPTURE:
926 if (mCaptureEventPool.size() < mMaxPoolSize) {
927 mCaptureEventPool.push(
928 std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event)));
929 return;
930 }
931 break;
arthurhung7632c332020-12-30 16:58:01 +0800932 case AINPUT_EVENT_TYPE_DRAG:
933 if (mDragEventPool.size() < mMaxPoolSize) {
934 mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event)));
935 return;
936 }
937 break;
Jeff Brown5912f952013-07-01 19:10:31 -0700938 }
939 delete event;
940}
941
942} // namespace android