blob: eb7a06046ca7a8d68a3743e244ad16afa2b4535a [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);
Prabir Pradhanc6523582021-05-14 18:02:55 -0700321 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_X, windowXScale);
322 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_Y, windowYScale);
Robert Carre07e1032018-11-26 12:55:53 -0800323}
324
Jeff Brownf086ddb2014-02-11 14:28:48 -0800325void PointerCoords::applyOffset(float xOffset, float yOffset) {
326 setAxisValue(AMOTION_EVENT_AXIS_X, getX() + xOffset);
327 setAxisValue(AMOTION_EVENT_AXIS_Y, getY() + yOffset);
328}
329
Brett Chabotfaa986c2020-11-04 17:39:36 -0800330#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700331status_t PointerCoords::readFromParcel(Parcel* parcel) {
332 bits = parcel->readInt64();
333
Michael Wright38dcdff2014-03-19 12:06:10 -0700334 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700335 if (count > MAX_AXES) {
336 return BAD_VALUE;
337 }
338
339 for (uint32_t i = 0; i < count; i++) {
340 values[i] = parcel->readFloat();
341 }
342 return OK;
343}
344
345status_t PointerCoords::writeToParcel(Parcel* parcel) const {
346 parcel->writeInt64(bits);
347
Michael Wright38dcdff2014-03-19 12:06:10 -0700348 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700349 for (uint32_t i = 0; i < count; i++) {
350 parcel->writeFloat(values[i]);
351 }
352 return OK;
353}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800354#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700355
356void PointerCoords::tooManyAxes(int axis) {
357 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
358 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
359}
360
361bool PointerCoords::operator==(const PointerCoords& other) const {
362 if (bits != other.bits) {
363 return false;
364 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700365 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700366 for (uint32_t i = 0; i < count; i++) {
367 if (values[i] != other.values[i]) {
368 return false;
369 }
370 }
371 return true;
372}
373
374void PointerCoords::copyFrom(const PointerCoords& other) {
375 bits = other.bits;
Michael Wright38dcdff2014-03-19 12:06:10 -0700376 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700377 for (uint32_t i = 0; i < count; i++) {
378 values[i] = other.values[i];
379 }
380}
381
chaviwc01e1372020-07-01 12:37:31 -0700382void PointerCoords::transform(const ui::Transform& transform) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700383 const vec2 xy = transform.transform(getXYValue());
384 setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
385 setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
386
Prabir Pradhanc6523582021-05-14 18:02:55 -0700387 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_X) ||
388 BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_Y)) {
389 const ui::Transform rotation(transform.getOrientation());
390 const vec2 relativeXy = rotation.transform(getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
391 getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
392 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
393 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
394 }
395
Prabir Pradhan6b384612021-05-14 16:56:25 -0700396 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) {
397 const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
398 setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val));
399 }
chaviwc01e1372020-07-01 12:37:31 -0700400}
Jeff Brown5912f952013-07-01 19:10:31 -0700401
402// --- PointerProperties ---
403
404bool PointerProperties::operator==(const PointerProperties& other) const {
405 return id == other.id
406 && toolType == other.toolType;
407}
408
409void PointerProperties::copyFrom(const PointerProperties& other) {
410 id = other.id;
411 toolType = other.toolType;
412}
413
414
415// --- MotionEvent ---
416
Garfield Tan4cc839f2020-01-24 11:26:14 -0800417void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600418 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
419 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700420 int32_t buttonState, MotionClassification classification,
421 const ui::Transform& transform, float xPrecision, float yPrecision,
Evan Rosky84f07f02021-04-16 10:42:42 -0700422 float rawXCursorPosition, float rawYCursorPosition,
423 int32_t displayWidth, int32_t displayHeight, nsecs_t downTime,
chaviw9eaa22c2020-07-01 16:21:27 -0700424 nsecs_t eventTime, size_t pointerCount,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600425 const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700426 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800427 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700428 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100429 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700430 mFlags = flags;
431 mEdgeFlags = edgeFlags;
432 mMetaState = metaState;
433 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800434 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700435 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700436 mXPrecision = xPrecision;
437 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700438 mRawXCursorPosition = rawXCursorPosition;
439 mRawYCursorPosition = rawYCursorPosition;
Evan Rosky84f07f02021-04-16 10:42:42 -0700440 mDisplayWidth = displayWidth;
441 mDisplayHeight = displayHeight;
Jeff Brown5912f952013-07-01 19:10:31 -0700442 mDownTime = downTime;
443 mPointerProperties.clear();
444 mPointerProperties.appendArray(pointerProperties, pointerCount);
445 mSampleEventTimes.clear();
446 mSamplePointerCoords.clear();
447 addSample(eventTime, pointerCoords);
448}
449
450void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800451 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
452 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700453 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100454 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700455 mFlags = other->mFlags;
456 mEdgeFlags = other->mEdgeFlags;
457 mMetaState = other->mMetaState;
458 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800459 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700460 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700461 mXPrecision = other->mXPrecision;
462 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700463 mRawXCursorPosition = other->mRawXCursorPosition;
464 mRawYCursorPosition = other->mRawYCursorPosition;
Evan Rosky84f07f02021-04-16 10:42:42 -0700465 mDisplayWidth = other->mDisplayWidth;
466 mDisplayHeight = other->mDisplayHeight;
Jeff Brown5912f952013-07-01 19:10:31 -0700467 mDownTime = other->mDownTime;
468 mPointerProperties = other->mPointerProperties;
469
470 if (keepHistory) {
471 mSampleEventTimes = other->mSampleEventTimes;
472 mSamplePointerCoords = other->mSamplePointerCoords;
473 } else {
474 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500475 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700476 mSamplePointerCoords.clear();
477 size_t pointerCount = other->getPointerCount();
478 size_t historySize = other->getHistorySize();
479 mSamplePointerCoords.appendArray(other->mSamplePointerCoords.array()
480 + (historySize * pointerCount), pointerCount);
481 }
482}
483
484void MotionEvent::addSample(
485 int64_t eventTime,
486 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500487 mSampleEventTimes.push_back(eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -0700488 mSamplePointerCoords.appendArray(pointerCoords, getPointerCount());
489}
490
Garfield Tan00f511d2019-06-12 16:55:40 -0700491float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700492 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
493 return vals.x;
Garfield Tan00f511d2019-06-12 16:55:40 -0700494}
495
496float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700497 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
498 return vals.y;
Garfield Tan00f511d2019-06-12 16:55:40 -0700499}
500
Garfield Tan937bb832019-07-25 17:48:31 -0700501void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700502 ui::Transform inverse = mTransform.inverse();
503 vec2 vals = inverse.transform(x, y);
504 mRawXCursorPosition = vals.x;
505 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700506}
507
Jeff Brown5912f952013-07-01 19:10:31 -0700508const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
509 return &mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
510}
511
512float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
Evan Rosky84f07f02021-04-16 10:42:42 -0700513 return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700514}
515
516float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700517 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700518}
519
520const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
521 size_t pointerIndex, size_t historicalIndex) const {
522 return &mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
523}
524
525float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan6b384612021-05-14 16:56:25 -0700526 size_t historicalIndex) const {
527 const PointerCoords* coords = getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
528
529 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
530 // For compatibility, convert raw coordinates into "oriented screen space". Once app
531 // developers are educated about getRaw, we can consider removing this.
Prabir Pradhan9f388812021-05-13 16:54:53 -0700532 const vec2 xy = shouldDisregardWindowTranslation(mSource)
533 ? rotatePoint(mTransform, coords->getX(), coords->getY())
534 : rotatePoint(mTransform, coords->getX(), coords->getY(), mDisplayWidth,
535 mDisplayHeight);
Prabir Pradhan6b384612021-05-14 16:56:25 -0700536 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
537 return xy[axis];
Evan Rosky84f07f02021-04-16 10:42:42 -0700538 }
539
Prabir Pradhanc6523582021-05-14 18:02:55 -0700540 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
541 // For compatibility, since we convert raw coordinates into "oriented screen space", we
542 // need to convert the relative axes into the same orientation for consistency.
543 const vec2 relativeXy =
544 rotatePoint(mTransform, coords->getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
545 coords->getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
546 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
547 }
548
Prabir Pradhan6b384612021-05-14 16:56:25 -0700549 return coords->getAxisValue(axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700550}
551
552float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan9f388812021-05-13 16:54:53 -0700553 size_t historicalIndex) const {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700554 const PointerCoords* coords = getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
555
556 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
Prabir Pradhan9f388812021-05-13 16:54:53 -0700557 const vec2 xy = shouldDisregardWindowTranslation(mSource)
558 ? applyTransformWithoutTranslation(mTransform, coords->getX(), coords->getY())
559 : mTransform.transform(coords->getXYValue());
Prabir Pradhan6b384612021-05-14 16:56:25 -0700560 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
561 return xy[axis];
chaviw9eaa22c2020-07-01 16:21:27 -0700562 }
563
Prabir Pradhanc6523582021-05-14 18:02:55 -0700564 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
565 const vec2 relativeXy =
566 applyTransformWithoutTranslation(mTransform,
567 coords->getAxisValue(
568 AMOTION_EVENT_AXIS_RELATIVE_X),
569 coords->getAxisValue(
570 AMOTION_EVENT_AXIS_RELATIVE_Y));
571 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
572 }
573
Prabir Pradhan6b384612021-05-14 16:56:25 -0700574 return coords->getAxisValue(axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700575}
576
577ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
578 size_t pointerCount = mPointerProperties.size();
579 for (size_t i = 0; i < pointerCount; i++) {
580 if (mPointerProperties.itemAt(i).id == pointerId) {
581 return i;
582 }
583 }
584 return -1;
585}
586
587void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700588 float currXOffset = mTransform.tx();
589 float currYOffset = mTransform.ty();
590 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700591}
592
Robert Carre07e1032018-11-26 12:55:53 -0800593void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700594 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800595 mXPrecision *= globalScaleFactor;
596 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700597
598 size_t numSamples = mSamplePointerCoords.size();
599 for (size_t i = 0; i < numSamples; i++) {
chaviw9eaa22c2020-07-01 16:21:27 -0700600 mSamplePointerCoords.editItemAt(i).scale(globalScaleFactor, globalScaleFactor,
601 globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700602 }
603}
604
chaviw9eaa22c2020-07-01 16:21:27 -0700605void MotionEvent::transform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700606 // We want to preserve the raw axes values stored in the PointerCoords, so we just update the
607 // transform using the values passed in.
chaviw9eaa22c2020-07-01 16:21:27 -0700608 ui::Transform newTransform;
609 newTransform.set(matrix);
610 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700611
Prabir Pradhan6b384612021-05-14 16:56:25 -0700612 // We need to update the AXIS_ORIENTATION value here to maintain the old behavior where the
613 // orientation angle is not affected by the initial transformation set in the MotionEvent.
614 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
615 [&newTransform](PointerCoords& c) {
616 float orientation = c.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
617 c.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
618 transformAngle(newTransform, orientation));
619 });
Jeff Brown5912f952013-07-01 19:10:31 -0700620}
621
Evan Roskyd4d4d802021-05-03 20:12:21 -0700622void MotionEvent::applyTransform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700623 ui::Transform transform;
624 transform.set(matrix);
Evan Roskyd4d4d802021-05-03 20:12:21 -0700625
626 // Apply the transformation to all samples.
Prabir Pradhan6b384612021-05-14 16:56:25 -0700627 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
628 [&transform](PointerCoords& c) { c.transform(transform); });
Prabir Pradhan4b19bd02021-06-01 17:34:59 -0700629
630 if (mRawXCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION &&
631 mRawYCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) {
632 const vec2 cursor = transform.transform(mRawXCursorPosition, mRawYCursorPosition);
633 mRawXCursorPosition = cursor.x;
634 mRawYCursorPosition = cursor.y;
635 }
Evan Roskyd4d4d802021-05-03 20:12:21 -0700636}
637
Brett Chabotfaa986c2020-11-04 17:39:36 -0800638#ifdef __linux__
chaviw9eaa22c2020-07-01 16:21:27 -0700639static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
640 float dsdx, dtdx, tx, dtdy, dsdy, ty;
641 status_t status = parcel.readFloat(&dsdx);
642 status |= parcel.readFloat(&dtdx);
643 status |= parcel.readFloat(&tx);
644 status |= parcel.readFloat(&dtdy);
645 status |= parcel.readFloat(&dsdy);
646 status |= parcel.readFloat(&ty);
647
648 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
649 return status;
650}
651
652static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
653 status_t status = parcel.writeFloat(transform.dsdx());
654 status |= parcel.writeFloat(transform.dtdx());
655 status |= parcel.writeFloat(transform.tx());
656 status |= parcel.writeFloat(transform.dtdy());
657 status |= parcel.writeFloat(transform.dsdy());
658 status |= parcel.writeFloat(transform.ty());
659 return status;
660}
661
Jeff Brown5912f952013-07-01 19:10:31 -0700662status_t MotionEvent::readFromParcel(Parcel* parcel) {
663 size_t pointerCount = parcel->readInt32();
664 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800665 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
666 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700667 return BAD_VALUE;
668 }
669
Garfield Tan4cc839f2020-01-24 11:26:14 -0800670 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700671 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600672 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800673 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600674 std::vector<uint8_t> hmac;
675 status_t result = parcel->readByteVector(&hmac);
676 if (result != OK || hmac.size() != 32) {
677 return BAD_VALUE;
678 }
679 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700680 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100681 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700682 mFlags = parcel->readInt32();
683 mEdgeFlags = parcel->readInt32();
684 mMetaState = parcel->readInt32();
685 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800686 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700687
688 result = android::readFromParcel(mTransform, *parcel);
689 if (result != OK) {
690 return result;
691 }
Jeff Brown5912f952013-07-01 19:10:31 -0700692 mXPrecision = parcel->readFloat();
693 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700694 mRawXCursorPosition = parcel->readFloat();
695 mRawYCursorPosition = parcel->readFloat();
Evan Rosky84f07f02021-04-16 10:42:42 -0700696 mDisplayWidth = parcel->readInt32();
697 mDisplayHeight = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700698 mDownTime = parcel->readInt64();
699
700 mPointerProperties.clear();
701 mPointerProperties.setCapacity(pointerCount);
702 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500703 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700704 mSamplePointerCoords.clear();
705 mSamplePointerCoords.setCapacity(sampleCount * pointerCount);
706
707 for (size_t i = 0; i < pointerCount; i++) {
708 mPointerProperties.push();
709 PointerProperties& properties = mPointerProperties.editTop();
710 properties.id = parcel->readInt32();
711 properties.toolType = parcel->readInt32();
712 }
713
Dan Austinc94fc452015-09-22 14:22:41 -0700714 while (sampleCount > 0) {
715 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500716 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700717 for (size_t i = 0; i < pointerCount; i++) {
718 mSamplePointerCoords.push();
719 status_t status = mSamplePointerCoords.editTop().readFromParcel(parcel);
720 if (status) {
721 return status;
722 }
723 }
724 }
725 return OK;
726}
727
728status_t MotionEvent::writeToParcel(Parcel* parcel) const {
729 size_t pointerCount = mPointerProperties.size();
730 size_t sampleCount = mSampleEventTimes.size();
731
732 parcel->writeInt32(pointerCount);
733 parcel->writeInt32(sampleCount);
734
Garfield Tan4cc839f2020-01-24 11:26:14 -0800735 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700736 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600737 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800738 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600739 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
740 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700741 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100742 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700743 parcel->writeInt32(mFlags);
744 parcel->writeInt32(mEdgeFlags);
745 parcel->writeInt32(mMetaState);
746 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800747 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700748
749 status_t result = android::writeToParcel(mTransform, *parcel);
750 if (result != OK) {
751 return result;
752 }
Jeff Brown5912f952013-07-01 19:10:31 -0700753 parcel->writeFloat(mXPrecision);
754 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700755 parcel->writeFloat(mRawXCursorPosition);
756 parcel->writeFloat(mRawYCursorPosition);
Evan Rosky84f07f02021-04-16 10:42:42 -0700757 parcel->writeInt32(mDisplayWidth);
758 parcel->writeInt32(mDisplayHeight);
Jeff Brown5912f952013-07-01 19:10:31 -0700759 parcel->writeInt64(mDownTime);
760
761 for (size_t i = 0; i < pointerCount; i++) {
762 const PointerProperties& properties = mPointerProperties.itemAt(i);
763 parcel->writeInt32(properties.id);
764 parcel->writeInt32(properties.toolType);
765 }
766
767 const PointerCoords* pc = mSamplePointerCoords.array();
768 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500769 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700770 for (size_t i = 0; i < pointerCount; i++) {
771 status_t status = (pc++)->writeToParcel(parcel);
772 if (status) {
773 return status;
774 }
775 }
776 }
777 return OK;
778}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800779#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700780
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600781bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Jeff Brown5912f952013-07-01 19:10:31 -0700782 if (source & AINPUT_SOURCE_CLASS_POINTER) {
783 // Specifically excludes HOVER_MOVE and SCROLL.
784 switch (action & AMOTION_EVENT_ACTION_MASK) {
785 case AMOTION_EVENT_ACTION_DOWN:
786 case AMOTION_EVENT_ACTION_MOVE:
787 case AMOTION_EVENT_ACTION_UP:
788 case AMOTION_EVENT_ACTION_POINTER_DOWN:
789 case AMOTION_EVENT_ACTION_POINTER_UP:
790 case AMOTION_EVENT_ACTION_CANCEL:
791 case AMOTION_EVENT_ACTION_OUTSIDE:
792 return true;
793 }
794 }
795 return false;
796}
797
Michael Wright872db4f2014-04-22 15:03:51 -0700798const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700799 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700800}
801
802int32_t MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700803 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700804}
805
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500806std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700807 // Convert MotionEvent action to string
808 switch (action & AMOTION_EVENT_ACTION_MASK) {
809 case AMOTION_EVENT_ACTION_DOWN:
810 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700811 case AMOTION_EVENT_ACTION_UP:
812 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500813 case AMOTION_EVENT_ACTION_MOVE:
814 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700815 case AMOTION_EVENT_ACTION_CANCEL:
816 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500817 case AMOTION_EVENT_ACTION_OUTSIDE:
818 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700819 case AMOTION_EVENT_ACTION_POINTER_DOWN:
820 return "POINTER_DOWN";
821 case AMOTION_EVENT_ACTION_POINTER_UP:
822 return "POINTER_UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500823 case AMOTION_EVENT_ACTION_HOVER_MOVE:
824 return "HOVER_MOVE";
825 case AMOTION_EVENT_ACTION_SCROLL:
826 return "SCROLL";
827 case AMOTION_EVENT_ACTION_HOVER_ENTER:
828 return "HOVER_ENTER";
829 case AMOTION_EVENT_ACTION_HOVER_EXIT:
830 return "HOVER_EXIT";
831 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
832 return "BUTTON_PRESS";
833 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
834 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700835 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500836 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700837}
838
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800839// --- FocusEvent ---
840
Garfield Tan4cc839f2020-01-24 11:26:14 -0800841void FocusEvent::initialize(int32_t id, bool hasFocus, bool inTouchMode) {
842 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600843 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800844 mHasFocus = hasFocus;
845 mInTouchMode = inTouchMode;
846}
847
848void FocusEvent::initialize(const FocusEvent& from) {
849 InputEvent::initialize(from);
850 mHasFocus = from.mHasFocus;
851 mInTouchMode = from.mInTouchMode;
852}
Jeff Brown5912f952013-07-01 19:10:31 -0700853
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800854// --- CaptureEvent ---
855
856void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) {
857 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
858 ADISPLAY_ID_NONE, INVALID_HMAC);
859 mPointerCaptureEnabled = pointerCaptureEnabled;
860}
861
862void CaptureEvent::initialize(const CaptureEvent& from) {
863 InputEvent::initialize(from);
864 mPointerCaptureEnabled = from.mPointerCaptureEnabled;
865}
866
arthurhung7632c332020-12-30 16:58:01 +0800867// --- DragEvent ---
868
869void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) {
870 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
871 ADISPLAY_ID_NONE, INVALID_HMAC);
872 mIsExiting = isExiting;
873 mX = x;
874 mY = y;
875}
876
877void DragEvent::initialize(const DragEvent& from) {
878 InputEvent::initialize(from);
879 mIsExiting = from.mIsExiting;
880 mX = from.mX;
881 mY = from.mY;
882}
883
Jeff Brown5912f952013-07-01 19:10:31 -0700884// --- PooledInputEventFactory ---
885
886PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
887 mMaxPoolSize(maxPoolSize) {
888}
889
890PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -0700891}
892
893KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800894 if (mKeyEventPool.empty()) {
895 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700896 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800897 KeyEvent* event = mKeyEventPool.front().release();
898 mKeyEventPool.pop();
899 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700900}
901
902MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800903 if (mMotionEventPool.empty()) {
904 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700905 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800906 MotionEvent* event = mMotionEventPool.front().release();
907 mMotionEventPool.pop();
908 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700909}
910
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800911FocusEvent* PooledInputEventFactory::createFocusEvent() {
912 if (mFocusEventPool.empty()) {
913 return new FocusEvent();
914 }
915 FocusEvent* event = mFocusEventPool.front().release();
916 mFocusEventPool.pop();
917 return event;
918}
919
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800920CaptureEvent* PooledInputEventFactory::createCaptureEvent() {
921 if (mCaptureEventPool.empty()) {
922 return new CaptureEvent();
923 }
924 CaptureEvent* event = mCaptureEventPool.front().release();
925 mCaptureEventPool.pop();
926 return event;
927}
928
arthurhung7632c332020-12-30 16:58:01 +0800929DragEvent* PooledInputEventFactory::createDragEvent() {
930 if (mDragEventPool.empty()) {
931 return new DragEvent();
932 }
933 DragEvent* event = mDragEventPool.front().release();
934 mDragEventPool.pop();
935 return event;
936}
937
Jeff Brown5912f952013-07-01 19:10:31 -0700938void PooledInputEventFactory::recycle(InputEvent* event) {
939 switch (event->getType()) {
940 case AINPUT_EVENT_TYPE_KEY:
941 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800942 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700943 return;
944 }
945 break;
946 case AINPUT_EVENT_TYPE_MOTION:
947 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800948 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700949 return;
950 }
951 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800952 case AINPUT_EVENT_TYPE_FOCUS:
953 if (mFocusEventPool.size() < mMaxPoolSize) {
954 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
955 return;
956 }
957 break;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800958 case AINPUT_EVENT_TYPE_CAPTURE:
959 if (mCaptureEventPool.size() < mMaxPoolSize) {
960 mCaptureEventPool.push(
961 std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event)));
962 return;
963 }
964 break;
arthurhung7632c332020-12-30 16:58:01 +0800965 case AINPUT_EVENT_TYPE_DRAG:
966 if (mDragEventPool.size() < mMaxPoolSize) {
967 mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event)));
968 return;
969 }
970 break;
Jeff Brown5912f952013-07-01 19:10:31 -0700971 }
972 delete event;
973}
974
975} // namespace android