blob: 906e3e5c149960259f38079109c78932122ca79f [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>
chaviw3277faf2021-05-19 16:45:23 -050027#include <gui/constants.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
45float transformAngle(const ui::Transform& transform, float angleRadians) {
46 // Construct and transform a vector oriented at the specified clockwise angle from vertical.
47 // Coordinate system: down is increasing Y, right is increasing X.
48 float x = sinf(angleRadians);
49 float y = -cosf(angleRadians);
50 vec2 transformedPoint = transform.transform(x, y);
51
52 // Determine how the origin is transformed by the matrix so that we
53 // can transform orientation vectors.
54 const vec2 origin = transform.transform(0, 0);
55
56 transformedPoint.x -= origin.x;
57 transformedPoint.y -= origin.y;
58
59 // Derive the transformed vector's clockwise angle from vertical.
60 float result = atan2f(transformedPoint.x, -transformedPoint.y);
61 if (result < -M_PI_2) {
62 result += M_PI;
63 } else if (result > M_PI_2) {
64 result -= M_PI;
65 }
66 return result;
67}
68
69// Rotates the given point to the transform's orientation. If the display width and height are
70// provided, the point is rotated in the screen space. Otherwise, the point is rotated about the
71// origin. This helper is used to avoid the extra overhead of creating new Transforms.
72vec2 rotatePoint(const ui::Transform& transform, float x, float y, int32_t displayWidth = 0,
73 int32_t displayHeight = 0) {
74 // 0x7 encapsulates all 3 rotations (see ui::Transform::RotationFlags)
75 static const int ALL_ROTATIONS_MASK = 0x7;
76 const uint32_t orientation = (transform.getOrientation() & ALL_ROTATIONS_MASK);
77 if (orientation == ui::Transform::ROT_0) {
78 return {x, y};
79 }
80
81 vec2 xy(x, y);
82 if (orientation == ui::Transform::ROT_90) {
83 xy.x = displayHeight - y;
84 xy.y = x;
85 } else if (orientation == ui::Transform::ROT_180) {
86 xy.x = displayWidth - x;
87 xy.y = displayHeight - y;
88 } else if (orientation == ui::Transform::ROT_270) {
89 xy.x = y;
90 xy.y = displayWidth - x;
91 }
92 return xy;
93}
94
Prabir Pradhan9f388812021-05-13 16:54:53 -070095vec2 applyTransformWithoutTranslation(const ui::Transform& transform, float x, float y) {
96 const vec2 transformedXy = transform.transform(x, y);
97 const vec2 transformedOrigin = transform.transform(0, 0);
98 return transformedXy - transformedOrigin;
99}
100
101bool shouldDisregardWindowTranslation(uint32_t source) {
102 // Pointer events are the only type of events that refer to absolute coordinates on the display,
103 // so we should apply the entire window transform. For other types of events, we should make
104 // sure to not apply the window translation/offset.
105 return (source & AINPUT_SOURCE_CLASS_POINTER) == 0;
106}
107
Prabir Pradhan6b384612021-05-14 16:56:25 -0700108} // namespace
109
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800110const char* motionClassificationToString(MotionClassification classification) {
111 switch (classification) {
112 case MotionClassification::NONE:
113 return "NONE";
114 case MotionClassification::AMBIGUOUS_GESTURE:
115 return "AMBIGUOUS_GESTURE";
116 case MotionClassification::DEEP_PRESS:
117 return "DEEP_PRESS";
118 }
119}
120
Garfield Tan84b087e2020-01-23 10:49:05 -0800121// --- IdGenerator ---
122IdGenerator::IdGenerator(Source source) : mSource(source) {}
123
124int32_t IdGenerator::nextId() const {
125 constexpr uint32_t SEQUENCE_NUMBER_MASK = ~SOURCE_MASK;
126 int32_t id = 0;
127
128// Avoid building against syscall getrandom(2) on host, which will fail build on Mac. Host doesn't
129// use sequence number so just always return mSource.
130#ifdef __ANDROID__
131 constexpr size_t BUF_LEN = sizeof(id);
132 size_t totalBytes = 0;
133 while (totalBytes < BUF_LEN) {
134 ssize_t bytes = TEMP_FAILURE_RETRY(getrandom(&id, BUF_LEN, GRND_NONBLOCK));
135 if (CC_UNLIKELY(bytes < 0)) {
136 ALOGW("Failed to fill in random number for sequence number: %s.", strerror(errno));
137 id = 0;
138 break;
139 }
140 totalBytes += bytes;
141 }
142#endif // __ANDROID__
143
144 return (id & SEQUENCE_NUMBER_MASK) | static_cast<int32_t>(mSource);
145}
146
Jeff Brown5912f952013-07-01 19:10:31 -0700147// --- InputEvent ---
148
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800149const char* inputEventTypeToString(int32_t type) {
150 switch (type) {
151 case AINPUT_EVENT_TYPE_KEY: {
152 return "KEY";
153 }
154 case AINPUT_EVENT_TYPE_MOTION: {
155 return "MOTION";
156 }
157 case AINPUT_EVENT_TYPE_FOCUS: {
158 return "FOCUS";
159 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800160 case AINPUT_EVENT_TYPE_CAPTURE: {
161 return "CAPTURE";
162 }
arthurhung7632c332020-12-30 16:58:01 +0800163 case AINPUT_EVENT_TYPE_DRAG: {
164 return "DRAG";
165 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800166 }
167 return "UNKNOWN";
168}
169
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800170VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
171 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
172 event.getSource(), event.getDisplayId()},
173 event.getAction(),
174 event.getDownTime(),
175 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
176 event.getKeyCode(),
177 event.getScanCode(),
178 event.getMetaState(),
179 event.getRepeatCount()};
180}
181
182VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
183 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
184 event.getSource(), event.getDisplayId()},
185 event.getRawX(0),
186 event.getRawY(0),
187 event.getActionMasked(),
188 event.getDownTime(),
189 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
190 event.getMetaState(),
191 event.getButtonState()};
192}
193
Garfield Tan4cc839f2020-01-24 11:26:14 -0800194void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600195 std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800196 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700197 mDeviceId = deviceId;
198 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100199 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600200 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700201}
202
203void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800204 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700205 mDeviceId = from.mDeviceId;
206 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100207 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600208 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700209}
210
Garfield Tan4cc839f2020-01-24 11:26:14 -0800211int32_t InputEvent::nextId() {
212 static IdGenerator idGen(IdGenerator::Source::OTHER);
213 return idGen.nextId();
214}
215
Jeff Brown5912f952013-07-01 19:10:31 -0700216// --- KeyEvent ---
217
Michael Wright872db4f2014-04-22 15:03:51 -0700218const char* KeyEvent::getLabel(int32_t keyCode) {
Chris Ye4958d062020-08-20 13:21:10 -0700219 return InputEventLookup::getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700220}
221
Michael Wright872db4f2014-04-22 15:03:51 -0700222int32_t KeyEvent::getKeyCodeFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700223 return InputEventLookup::getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700224}
225
Garfield Tan4cc839f2020-01-24 11:26:14 -0800226void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600227 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
228 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
229 nsecs_t downTime, nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800230 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700231 mAction = action;
232 mFlags = flags;
233 mKeyCode = keyCode;
234 mScanCode = scanCode;
235 mMetaState = metaState;
236 mRepeatCount = repeatCount;
237 mDownTime = downTime;
238 mEventTime = eventTime;
239}
240
241void KeyEvent::initialize(const KeyEvent& from) {
242 InputEvent::initialize(from);
243 mAction = from.mAction;
244 mFlags = from.mFlags;
245 mKeyCode = from.mKeyCode;
246 mScanCode = from.mScanCode;
247 mMetaState = from.mMetaState;
248 mRepeatCount = from.mRepeatCount;
249 mDownTime = from.mDownTime;
250 mEventTime = from.mEventTime;
251}
252
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700253const char* KeyEvent::actionToString(int32_t action) {
254 // Convert KeyEvent action to string
255 switch (action) {
256 case AKEY_EVENT_ACTION_DOWN:
257 return "DOWN";
258 case AKEY_EVENT_ACTION_UP:
259 return "UP";
260 case AKEY_EVENT_ACTION_MULTIPLE:
261 return "MULTIPLE";
262 }
263 return "UNKNOWN";
264}
Jeff Brown5912f952013-07-01 19:10:31 -0700265
266// --- PointerCoords ---
267
268float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700269 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700270 return 0;
271 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700272 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700273}
274
275status_t PointerCoords::setAxisValue(int32_t axis, float value) {
276 if (axis < 0 || axis > 63) {
277 return NAME_NOT_FOUND;
278 }
279
Michael Wright38dcdff2014-03-19 12:06:10 -0700280 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
281 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700282 if (value == 0) {
283 return OK; // axes with value 0 do not need to be stored
284 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700285
286 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700287 if (count >= MAX_AXES) {
288 tooManyAxes(axis);
289 return NO_MEMORY;
290 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700291 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700292 for (uint32_t i = count; i > index; i--) {
293 values[i] = values[i - 1];
294 }
295 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700296
Jeff Brown5912f952013-07-01 19:10:31 -0700297 values[index] = value;
298 return OK;
299}
300
301static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
302 float value = c.getAxisValue(axis);
303 if (value != 0) {
304 c.setAxisValue(axis, value * scaleFactor);
305 }
306}
307
Robert Carre07e1032018-11-26 12:55:53 -0800308void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700309 // No need to scale pressure or size since they are normalized.
310 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800311
312 // If there is a global scale factor, it is included in the windowX/YScale
313 // so we don't need to apply it twice to the X/Y axes.
314 // However we don't want to apply any windowXYScale not included in the global scale
315 // to the TOUCH_MAJOR/MINOR coordinates.
316 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
317 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
318 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
319 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
320 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
321 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
Prabir Pradhanc6523582021-05-14 18:02:55 -0700322 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_X, windowXScale);
323 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_Y, windowYScale);
Robert Carre07e1032018-11-26 12:55:53 -0800324}
325
326void PointerCoords::scale(float globalScaleFactor) {
327 scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700328}
329
Jeff Brownf086ddb2014-02-11 14:28:48 -0800330void PointerCoords::applyOffset(float xOffset, float yOffset) {
331 setAxisValue(AMOTION_EVENT_AXIS_X, getX() + xOffset);
332 setAxisValue(AMOTION_EVENT_AXIS_Y, getY() + yOffset);
333}
334
Brett Chabotfaa986c2020-11-04 17:39:36 -0800335#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700336status_t PointerCoords::readFromParcel(Parcel* parcel) {
337 bits = parcel->readInt64();
338
Michael Wright38dcdff2014-03-19 12:06:10 -0700339 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700340 if (count > MAX_AXES) {
341 return BAD_VALUE;
342 }
343
344 for (uint32_t i = 0; i < count; i++) {
345 values[i] = parcel->readFloat();
346 }
347 return OK;
348}
349
350status_t PointerCoords::writeToParcel(Parcel* parcel) const {
351 parcel->writeInt64(bits);
352
Michael Wright38dcdff2014-03-19 12:06:10 -0700353 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700354 for (uint32_t i = 0; i < count; i++) {
355 parcel->writeFloat(values[i]);
356 }
357 return OK;
358}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800359#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700360
361void PointerCoords::tooManyAxes(int axis) {
362 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
363 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
364}
365
366bool PointerCoords::operator==(const PointerCoords& other) const {
367 if (bits != other.bits) {
368 return false;
369 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700370 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700371 for (uint32_t i = 0; i < count; i++) {
372 if (values[i] != other.values[i]) {
373 return false;
374 }
375 }
376 return true;
377}
378
379void PointerCoords::copyFrom(const PointerCoords& other) {
380 bits = other.bits;
Michael Wright38dcdff2014-03-19 12:06:10 -0700381 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700382 for (uint32_t i = 0; i < count; i++) {
383 values[i] = other.values[i];
384 }
385}
386
chaviwc01e1372020-07-01 12:37:31 -0700387void PointerCoords::transform(const ui::Transform& transform) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700388 const vec2 xy = transform.transform(getXYValue());
389 setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
390 setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
391
Prabir Pradhanc6523582021-05-14 18:02:55 -0700392 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_X) ||
393 BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_Y)) {
394 const ui::Transform rotation(transform.getOrientation());
395 const vec2 relativeXy = rotation.transform(getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
396 getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
397 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
398 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
399 }
400
Prabir Pradhan6b384612021-05-14 16:56:25 -0700401 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) {
402 const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
403 setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val));
404 }
chaviwc01e1372020-07-01 12:37:31 -0700405}
Jeff Brown5912f952013-07-01 19:10:31 -0700406
407// --- PointerProperties ---
408
409bool PointerProperties::operator==(const PointerProperties& other) const {
410 return id == other.id
411 && toolType == other.toolType;
412}
413
414void PointerProperties::copyFrom(const PointerProperties& other) {
415 id = other.id;
416 toolType = other.toolType;
417}
418
419
420// --- MotionEvent ---
421
Garfield Tan4cc839f2020-01-24 11:26:14 -0800422void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600423 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
424 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700425 int32_t buttonState, MotionClassification classification,
426 const ui::Transform& transform, float xPrecision, float yPrecision,
Evan Rosky84f07f02021-04-16 10:42:42 -0700427 float rawXCursorPosition, float rawYCursorPosition,
428 int32_t displayWidth, int32_t displayHeight, nsecs_t downTime,
chaviw9eaa22c2020-07-01 16:21:27 -0700429 nsecs_t eventTime, size_t pointerCount,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600430 const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700431 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800432 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700433 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100434 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700435 mFlags = flags;
436 mEdgeFlags = edgeFlags;
437 mMetaState = metaState;
438 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800439 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700440 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700441 mXPrecision = xPrecision;
442 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700443 mRawXCursorPosition = rawXCursorPosition;
444 mRawYCursorPosition = rawYCursorPosition;
Evan Rosky84f07f02021-04-16 10:42:42 -0700445 mDisplayWidth = displayWidth;
446 mDisplayHeight = displayHeight;
Jeff Brown5912f952013-07-01 19:10:31 -0700447 mDownTime = downTime;
448 mPointerProperties.clear();
449 mPointerProperties.appendArray(pointerProperties, pointerCount);
450 mSampleEventTimes.clear();
451 mSamplePointerCoords.clear();
452 addSample(eventTime, pointerCoords);
453}
454
455void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800456 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
457 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700458 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100459 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700460 mFlags = other->mFlags;
461 mEdgeFlags = other->mEdgeFlags;
462 mMetaState = other->mMetaState;
463 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800464 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700465 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700466 mXPrecision = other->mXPrecision;
467 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700468 mRawXCursorPosition = other->mRawXCursorPosition;
469 mRawYCursorPosition = other->mRawYCursorPosition;
Evan Rosky84f07f02021-04-16 10:42:42 -0700470 mDisplayWidth = other->mDisplayWidth;
471 mDisplayHeight = other->mDisplayHeight;
Jeff Brown5912f952013-07-01 19:10:31 -0700472 mDownTime = other->mDownTime;
473 mPointerProperties = other->mPointerProperties;
474
475 if (keepHistory) {
476 mSampleEventTimes = other->mSampleEventTimes;
477 mSamplePointerCoords = other->mSamplePointerCoords;
478 } else {
479 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500480 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700481 mSamplePointerCoords.clear();
482 size_t pointerCount = other->getPointerCount();
483 size_t historySize = other->getHistorySize();
484 mSamplePointerCoords.appendArray(other->mSamplePointerCoords.array()
485 + (historySize * pointerCount), pointerCount);
486 }
487}
488
489void MotionEvent::addSample(
490 int64_t eventTime,
491 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500492 mSampleEventTimes.push_back(eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -0700493 mSamplePointerCoords.appendArray(pointerCoords, getPointerCount());
494}
495
Garfield Tan00f511d2019-06-12 16:55:40 -0700496float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700497 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
498 return vals.x;
Garfield Tan00f511d2019-06-12 16:55:40 -0700499}
500
501float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700502 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
503 return vals.y;
Garfield Tan00f511d2019-06-12 16:55:40 -0700504}
505
Garfield Tan937bb832019-07-25 17:48:31 -0700506void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700507 ui::Transform inverse = mTransform.inverse();
508 vec2 vals = inverse.transform(x, y);
509 mRawXCursorPosition = vals.x;
510 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700511}
512
Jeff Brown5912f952013-07-01 19:10:31 -0700513const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
514 return &mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
515}
516
517float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
Evan Rosky84f07f02021-04-16 10:42:42 -0700518 return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700519}
520
521float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700522 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700523}
524
525const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
526 size_t pointerIndex, size_t historicalIndex) const {
527 return &mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
528}
529
530float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan6b384612021-05-14 16:56:25 -0700531 size_t historicalIndex) const {
532 const PointerCoords* coords = getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
533
534 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
535 // For compatibility, convert raw coordinates into "oriented screen space". Once app
536 // developers are educated about getRaw, we can consider removing this.
Prabir Pradhan9f388812021-05-13 16:54:53 -0700537 const vec2 xy = shouldDisregardWindowTranslation(mSource)
538 ? rotatePoint(mTransform, coords->getX(), coords->getY())
539 : rotatePoint(mTransform, coords->getX(), coords->getY(), mDisplayWidth,
540 mDisplayHeight);
Prabir Pradhan6b384612021-05-14 16:56:25 -0700541 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
542 return xy[axis];
Evan Rosky84f07f02021-04-16 10:42:42 -0700543 }
544
Prabir Pradhanc6523582021-05-14 18:02:55 -0700545 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
546 // For compatibility, since we convert raw coordinates into "oriented screen space", we
547 // need to convert the relative axes into the same orientation for consistency.
548 const vec2 relativeXy =
549 rotatePoint(mTransform, coords->getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
550 coords->getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
551 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
552 }
553
Prabir Pradhan6b384612021-05-14 16:56:25 -0700554 return coords->getAxisValue(axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700555}
556
557float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan9f388812021-05-13 16:54:53 -0700558 size_t historicalIndex) const {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700559 const PointerCoords* coords = getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
560
561 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
Prabir Pradhan9f388812021-05-13 16:54:53 -0700562 const vec2 xy = shouldDisregardWindowTranslation(mSource)
563 ? applyTransformWithoutTranslation(mTransform, coords->getX(), coords->getY())
564 : mTransform.transform(coords->getXYValue());
Prabir Pradhan6b384612021-05-14 16:56:25 -0700565 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
566 return xy[axis];
chaviw9eaa22c2020-07-01 16:21:27 -0700567 }
568
Prabir Pradhanc6523582021-05-14 18:02:55 -0700569 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
570 const vec2 relativeXy =
571 applyTransformWithoutTranslation(mTransform,
572 coords->getAxisValue(
573 AMOTION_EVENT_AXIS_RELATIVE_X),
574 coords->getAxisValue(
575 AMOTION_EVENT_AXIS_RELATIVE_Y));
576 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
577 }
578
Prabir Pradhan6b384612021-05-14 16:56:25 -0700579 return coords->getAxisValue(axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700580}
581
582ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
583 size_t pointerCount = mPointerProperties.size();
584 for (size_t i = 0; i < pointerCount; i++) {
585 if (mPointerProperties.itemAt(i).id == pointerId) {
586 return i;
587 }
588 }
589 return -1;
590}
591
592void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700593 float currXOffset = mTransform.tx();
594 float currYOffset = mTransform.ty();
595 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700596}
597
Robert Carre07e1032018-11-26 12:55:53 -0800598void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700599 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800600 mXPrecision *= globalScaleFactor;
601 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700602
603 size_t numSamples = mSamplePointerCoords.size();
604 for (size_t i = 0; i < numSamples; i++) {
chaviw9eaa22c2020-07-01 16:21:27 -0700605 mSamplePointerCoords.editItemAt(i).scale(globalScaleFactor, globalScaleFactor,
606 globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700607 }
608}
609
chaviw9eaa22c2020-07-01 16:21:27 -0700610void MotionEvent::transform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700611 // We want to preserve the raw axes values stored in the PointerCoords, so we just update the
612 // transform using the values passed in.
chaviw9eaa22c2020-07-01 16:21:27 -0700613 ui::Transform newTransform;
614 newTransform.set(matrix);
615 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700616
Prabir Pradhan6b384612021-05-14 16:56:25 -0700617 // We need to update the AXIS_ORIENTATION value here to maintain the old behavior where the
618 // orientation angle is not affected by the initial transformation set in the MotionEvent.
619 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
620 [&newTransform](PointerCoords& c) {
621 float orientation = c.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
622 c.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
623 transformAngle(newTransform, orientation));
624 });
Jeff Brown5912f952013-07-01 19:10:31 -0700625}
626
Evan Roskyd4d4d802021-05-03 20:12:21 -0700627void MotionEvent::applyTransform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700628 ui::Transform transform;
629 transform.set(matrix);
Evan Roskyd4d4d802021-05-03 20:12:21 -0700630
631 // Apply the transformation to all samples.
Prabir Pradhan6b384612021-05-14 16:56:25 -0700632 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
633 [&transform](PointerCoords& c) { c.transform(transform); });
Prabir Pradhan4b19bd02021-06-01 17:34:59 -0700634
635 if (mRawXCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION &&
636 mRawYCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) {
637 const vec2 cursor = transform.transform(mRawXCursorPosition, mRawYCursorPosition);
638 mRawXCursorPosition = cursor.x;
639 mRawYCursorPosition = cursor.y;
640 }
Evan Roskyd4d4d802021-05-03 20:12:21 -0700641}
642
Brett Chabotfaa986c2020-11-04 17:39:36 -0800643#ifdef __linux__
chaviw9eaa22c2020-07-01 16:21:27 -0700644static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
645 float dsdx, dtdx, tx, dtdy, dsdy, ty;
646 status_t status = parcel.readFloat(&dsdx);
647 status |= parcel.readFloat(&dtdx);
648 status |= parcel.readFloat(&tx);
649 status |= parcel.readFloat(&dtdy);
650 status |= parcel.readFloat(&dsdy);
651 status |= parcel.readFloat(&ty);
652
653 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
654 return status;
655}
656
657static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
658 status_t status = parcel.writeFloat(transform.dsdx());
659 status |= parcel.writeFloat(transform.dtdx());
660 status |= parcel.writeFloat(transform.tx());
661 status |= parcel.writeFloat(transform.dtdy());
662 status |= parcel.writeFloat(transform.dsdy());
663 status |= parcel.writeFloat(transform.ty());
664 return status;
665}
666
Jeff Brown5912f952013-07-01 19:10:31 -0700667status_t MotionEvent::readFromParcel(Parcel* parcel) {
668 size_t pointerCount = parcel->readInt32();
669 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800670 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
671 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700672 return BAD_VALUE;
673 }
674
Garfield Tan4cc839f2020-01-24 11:26:14 -0800675 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700676 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600677 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800678 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600679 std::vector<uint8_t> hmac;
680 status_t result = parcel->readByteVector(&hmac);
681 if (result != OK || hmac.size() != 32) {
682 return BAD_VALUE;
683 }
684 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700685 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100686 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700687 mFlags = parcel->readInt32();
688 mEdgeFlags = parcel->readInt32();
689 mMetaState = parcel->readInt32();
690 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800691 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700692
693 result = android::readFromParcel(mTransform, *parcel);
694 if (result != OK) {
695 return result;
696 }
Jeff Brown5912f952013-07-01 19:10:31 -0700697 mXPrecision = parcel->readFloat();
698 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700699 mRawXCursorPosition = parcel->readFloat();
700 mRawYCursorPosition = parcel->readFloat();
Evan Rosky84f07f02021-04-16 10:42:42 -0700701 mDisplayWidth = parcel->readInt32();
702 mDisplayHeight = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700703 mDownTime = parcel->readInt64();
704
705 mPointerProperties.clear();
706 mPointerProperties.setCapacity(pointerCount);
707 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500708 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700709 mSamplePointerCoords.clear();
710 mSamplePointerCoords.setCapacity(sampleCount * pointerCount);
711
712 for (size_t i = 0; i < pointerCount; i++) {
713 mPointerProperties.push();
714 PointerProperties& properties = mPointerProperties.editTop();
715 properties.id = parcel->readInt32();
716 properties.toolType = parcel->readInt32();
717 }
718
Dan Austinc94fc452015-09-22 14:22:41 -0700719 while (sampleCount > 0) {
720 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500721 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700722 for (size_t i = 0; i < pointerCount; i++) {
723 mSamplePointerCoords.push();
724 status_t status = mSamplePointerCoords.editTop().readFromParcel(parcel);
725 if (status) {
726 return status;
727 }
728 }
729 }
730 return OK;
731}
732
733status_t MotionEvent::writeToParcel(Parcel* parcel) const {
734 size_t pointerCount = mPointerProperties.size();
735 size_t sampleCount = mSampleEventTimes.size();
736
737 parcel->writeInt32(pointerCount);
738 parcel->writeInt32(sampleCount);
739
Garfield Tan4cc839f2020-01-24 11:26:14 -0800740 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700741 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600742 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800743 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600744 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
745 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700746 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100747 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700748 parcel->writeInt32(mFlags);
749 parcel->writeInt32(mEdgeFlags);
750 parcel->writeInt32(mMetaState);
751 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800752 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700753
754 status_t result = android::writeToParcel(mTransform, *parcel);
755 if (result != OK) {
756 return result;
757 }
Jeff Brown5912f952013-07-01 19:10:31 -0700758 parcel->writeFloat(mXPrecision);
759 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700760 parcel->writeFloat(mRawXCursorPosition);
761 parcel->writeFloat(mRawYCursorPosition);
Evan Rosky84f07f02021-04-16 10:42:42 -0700762 parcel->writeInt32(mDisplayWidth);
763 parcel->writeInt32(mDisplayHeight);
Jeff Brown5912f952013-07-01 19:10:31 -0700764 parcel->writeInt64(mDownTime);
765
766 for (size_t i = 0; i < pointerCount; i++) {
767 const PointerProperties& properties = mPointerProperties.itemAt(i);
768 parcel->writeInt32(properties.id);
769 parcel->writeInt32(properties.toolType);
770 }
771
772 const PointerCoords* pc = mSamplePointerCoords.array();
773 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500774 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700775 for (size_t i = 0; i < pointerCount; i++) {
776 status_t status = (pc++)->writeToParcel(parcel);
777 if (status) {
778 return status;
779 }
780 }
781 }
782 return OK;
783}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800784#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700785
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600786bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Jeff Brown5912f952013-07-01 19:10:31 -0700787 if (source & AINPUT_SOURCE_CLASS_POINTER) {
788 // Specifically excludes HOVER_MOVE and SCROLL.
789 switch (action & AMOTION_EVENT_ACTION_MASK) {
790 case AMOTION_EVENT_ACTION_DOWN:
791 case AMOTION_EVENT_ACTION_MOVE:
792 case AMOTION_EVENT_ACTION_UP:
793 case AMOTION_EVENT_ACTION_POINTER_DOWN:
794 case AMOTION_EVENT_ACTION_POINTER_UP:
795 case AMOTION_EVENT_ACTION_CANCEL:
796 case AMOTION_EVENT_ACTION_OUTSIDE:
797 return true;
798 }
799 }
800 return false;
801}
802
Michael Wright872db4f2014-04-22 15:03:51 -0700803const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700804 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700805}
806
807int32_t MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700808 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700809}
810
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500811std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700812 // Convert MotionEvent action to string
813 switch (action & AMOTION_EVENT_ACTION_MASK) {
814 case AMOTION_EVENT_ACTION_DOWN:
815 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700816 case AMOTION_EVENT_ACTION_UP:
817 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500818 case AMOTION_EVENT_ACTION_MOVE:
819 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700820 case AMOTION_EVENT_ACTION_CANCEL:
821 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500822 case AMOTION_EVENT_ACTION_OUTSIDE:
823 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700824 case AMOTION_EVENT_ACTION_POINTER_DOWN:
825 return "POINTER_DOWN";
826 case AMOTION_EVENT_ACTION_POINTER_UP:
827 return "POINTER_UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500828 case AMOTION_EVENT_ACTION_HOVER_MOVE:
829 return "HOVER_MOVE";
830 case AMOTION_EVENT_ACTION_SCROLL:
831 return "SCROLL";
832 case AMOTION_EVENT_ACTION_HOVER_ENTER:
833 return "HOVER_ENTER";
834 case AMOTION_EVENT_ACTION_HOVER_EXIT:
835 return "HOVER_EXIT";
836 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
837 return "BUTTON_PRESS";
838 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
839 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700840 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500841 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700842}
843
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800844// --- FocusEvent ---
845
Garfield Tan4cc839f2020-01-24 11:26:14 -0800846void FocusEvent::initialize(int32_t id, bool hasFocus, bool inTouchMode) {
847 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600848 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800849 mHasFocus = hasFocus;
850 mInTouchMode = inTouchMode;
851}
852
853void FocusEvent::initialize(const FocusEvent& from) {
854 InputEvent::initialize(from);
855 mHasFocus = from.mHasFocus;
856 mInTouchMode = from.mInTouchMode;
857}
Jeff Brown5912f952013-07-01 19:10:31 -0700858
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800859// --- CaptureEvent ---
860
861void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) {
862 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
863 ADISPLAY_ID_NONE, INVALID_HMAC);
864 mPointerCaptureEnabled = pointerCaptureEnabled;
865}
866
867void CaptureEvent::initialize(const CaptureEvent& from) {
868 InputEvent::initialize(from);
869 mPointerCaptureEnabled = from.mPointerCaptureEnabled;
870}
871
arthurhung7632c332020-12-30 16:58:01 +0800872// --- DragEvent ---
873
874void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) {
875 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
876 ADISPLAY_ID_NONE, INVALID_HMAC);
877 mIsExiting = isExiting;
878 mX = x;
879 mY = y;
880}
881
882void DragEvent::initialize(const DragEvent& from) {
883 InputEvent::initialize(from);
884 mIsExiting = from.mIsExiting;
885 mX = from.mX;
886 mY = from.mY;
887}
888
Jeff Brown5912f952013-07-01 19:10:31 -0700889// --- PooledInputEventFactory ---
890
891PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
892 mMaxPoolSize(maxPoolSize) {
893}
894
895PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -0700896}
897
898KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800899 if (mKeyEventPool.empty()) {
900 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700901 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800902 KeyEvent* event = mKeyEventPool.front().release();
903 mKeyEventPool.pop();
904 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700905}
906
907MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800908 if (mMotionEventPool.empty()) {
909 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700910 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800911 MotionEvent* event = mMotionEventPool.front().release();
912 mMotionEventPool.pop();
913 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700914}
915
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800916FocusEvent* PooledInputEventFactory::createFocusEvent() {
917 if (mFocusEventPool.empty()) {
918 return new FocusEvent();
919 }
920 FocusEvent* event = mFocusEventPool.front().release();
921 mFocusEventPool.pop();
922 return event;
923}
924
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800925CaptureEvent* PooledInputEventFactory::createCaptureEvent() {
926 if (mCaptureEventPool.empty()) {
927 return new CaptureEvent();
928 }
929 CaptureEvent* event = mCaptureEventPool.front().release();
930 mCaptureEventPool.pop();
931 return event;
932}
933
arthurhung7632c332020-12-30 16:58:01 +0800934DragEvent* PooledInputEventFactory::createDragEvent() {
935 if (mDragEventPool.empty()) {
936 return new DragEvent();
937 }
938 DragEvent* event = mDragEventPool.front().release();
939 mDragEventPool.pop();
940 return event;
941}
942
Jeff Brown5912f952013-07-01 19:10:31 -0700943void PooledInputEventFactory::recycle(InputEvent* event) {
944 switch (event->getType()) {
945 case AINPUT_EVENT_TYPE_KEY:
946 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800947 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700948 return;
949 }
950 break;
951 case AINPUT_EVENT_TYPE_MOTION:
952 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800953 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700954 return;
955 }
956 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800957 case AINPUT_EVENT_TYPE_FOCUS:
958 if (mFocusEventPool.size() < mMaxPoolSize) {
959 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
960 return;
961 }
962 break;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800963 case AINPUT_EVENT_TYPE_CAPTURE:
964 if (mCaptureEventPool.size() < mMaxPoolSize) {
965 mCaptureEventPool.push(
966 std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event)));
967 return;
968 }
969 break;
arthurhung7632c332020-12-30 16:58:01 +0800970 case AINPUT_EVENT_TYPE_DRAG:
971 if (mDragEventPool.size() < mMaxPoolSize) {
972 mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event)));
973 return;
974 }
975 break;
Jeff Brown5912f952013-07-01 19:10:31 -0700976 }
977 delete event;
978}
979
980} // namespace android