blob: 30e5d5b0bcf270b2c198ac70425b383fc6e91eb1 [file] [log] [blame]
Jeff Brown5912f952013-07-01 19:10:31 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Input"
18//#define LOG_NDEBUG 0
19
chaviw09c8d2d2020-08-24 15:48:26 -070020#include <attestation/HmacKeyManager.h>
Garfield Tan84b087e2020-01-23 10:49:05 -080021#include <cutils/compiler.h>
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050022#include <inttypes.h>
Jeff Brown5912f952013-07-01 19:10:31 -070023#include <limits.h>
Garfield Tan84b087e2020-01-23 10:49:05 -080024#include <string.h>
Jeff Brown5912f952013-07-01 19:10:31 -070025
Prabir Pradhan7e1443f2021-07-23 21:01:55 +000026#include <android-base/properties.h>
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050027#include <android-base/stringprintf.h>
chaviw98318de2021-05-19 16:45:23 -050028#include <gui/constants.h>
Jeff Brown5912f952013-07-01 19:10:31 -070029#include <input/Input.h>
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080030#include <input/InputDevice.h>
Michael Wright872db4f2014-04-22 15:03:51 -070031#include <input/InputEventLabels.h>
Jeff Brown5912f952013-07-01 19:10:31 -070032
Brett Chabotfaa986c2020-11-04 17:39:36 -080033#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -070034#include <binder/Parcel.h>
Brett Chabotfaa986c2020-11-04 17:39:36 -080035#endif
Brett Chabot58208522020-09-09 13:55:24 -070036#ifdef __ANDROID__
Garfield Tan84b087e2020-01-23 10:49:05 -080037#include <sys/random.h>
Jeff Brown5912f952013-07-01 19:10:31 -070038#endif
39
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050040using android::base::StringPrintf;
41
Jeff Brown5912f952013-07-01 19:10:31 -070042namespace android {
43
Prabir Pradhan6b384612021-05-14 16:56:25 -070044namespace {
45
Prabir Pradhan7e1443f2021-07-23 21:01:55 +000046// When per-window-input-rotation is enabled, InputFlinger works in the un-rotated display
47// coordinates and SurfaceFlinger includes the display rotation in the input window transforms.
48bool isPerWindowInputRotationEnabled() {
49 static const bool PER_WINDOW_INPUT_ROTATION =
Vadim Tryshev7719c7d2021-08-27 17:28:43 +000050 base::GetBoolProperty("persist.debug.per_window_input_rotation", false);
Prabir Pradhan7e1443f2021-07-23 21:01:55 +000051
52 return PER_WINDOW_INPUT_ROTATION;
53}
54
Prabir Pradhan6b384612021-05-14 16:56:25 -070055float transformAngle(const ui::Transform& transform, float angleRadians) {
56 // Construct and transform a vector oriented at the specified clockwise angle from vertical.
57 // Coordinate system: down is increasing Y, right is increasing X.
58 float x = sinf(angleRadians);
59 float y = -cosf(angleRadians);
60 vec2 transformedPoint = transform.transform(x, y);
61
62 // Determine how the origin is transformed by the matrix so that we
63 // can transform orientation vectors.
64 const vec2 origin = transform.transform(0, 0);
65
66 transformedPoint.x -= origin.x;
67 transformedPoint.y -= origin.y;
68
69 // Derive the transformed vector's clockwise angle from vertical.
70 float result = atan2f(transformedPoint.x, -transformedPoint.y);
71 if (result < -M_PI_2) {
72 result += M_PI;
73 } else if (result > M_PI_2) {
74 result -= M_PI;
75 }
76 return result;
77}
78
Prabir Pradhanb9b18502021-08-26 12:30:32 -070079vec2 transformWithoutTranslation(const ui::Transform& transform, float x, float y) {
Prabir Pradhan9f388812021-05-13 16:54:53 -070080 const vec2 transformedXy = transform.transform(x, y);
81 const vec2 transformedOrigin = transform.transform(0, 0);
82 return transformedXy - transformedOrigin;
83}
84
Prabir Pradhanb9b18502021-08-26 12:30:32 -070085bool shouldDisregardTranslation(uint32_t source) {
Prabir Pradhan9f388812021-05-13 16:54:53 -070086 // Pointer events are the only type of events that refer to absolute coordinates on the display,
87 // so we should apply the entire window transform. For other types of events, we should make
88 // sure to not apply the window translation/offset.
89 return (source & AINPUT_SOURCE_CLASS_POINTER) == 0;
90}
91
Prabir Pradhan6b384612021-05-14 16:56:25 -070092} // namespace
93
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -080094const char* motionClassificationToString(MotionClassification classification) {
95 switch (classification) {
96 case MotionClassification::NONE:
97 return "NONE";
98 case MotionClassification::AMBIGUOUS_GESTURE:
99 return "AMBIGUOUS_GESTURE";
100 case MotionClassification::DEEP_PRESS:
101 return "DEEP_PRESS";
102 }
103}
104
Garfield Tan84b087e2020-01-23 10:49:05 -0800105// --- IdGenerator ---
106IdGenerator::IdGenerator(Source source) : mSource(source) {}
107
108int32_t IdGenerator::nextId() const {
109 constexpr uint32_t SEQUENCE_NUMBER_MASK = ~SOURCE_MASK;
110 int32_t id = 0;
111
112// Avoid building against syscall getrandom(2) on host, which will fail build on Mac. Host doesn't
113// use sequence number so just always return mSource.
114#ifdef __ANDROID__
115 constexpr size_t BUF_LEN = sizeof(id);
116 size_t totalBytes = 0;
117 while (totalBytes < BUF_LEN) {
118 ssize_t bytes = TEMP_FAILURE_RETRY(getrandom(&id, BUF_LEN, GRND_NONBLOCK));
119 if (CC_UNLIKELY(bytes < 0)) {
120 ALOGW("Failed to fill in random number for sequence number: %s.", strerror(errno));
121 id = 0;
122 break;
123 }
124 totalBytes += bytes;
125 }
126#endif // __ANDROID__
127
128 return (id & SEQUENCE_NUMBER_MASK) | static_cast<int32_t>(mSource);
129}
130
Jeff Brown5912f952013-07-01 19:10:31 -0700131// --- InputEvent ---
132
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800133const char* inputEventTypeToString(int32_t type) {
134 switch (type) {
135 case AINPUT_EVENT_TYPE_KEY: {
136 return "KEY";
137 }
138 case AINPUT_EVENT_TYPE_MOTION: {
139 return "MOTION";
140 }
141 case AINPUT_EVENT_TYPE_FOCUS: {
142 return "FOCUS";
143 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800144 case AINPUT_EVENT_TYPE_CAPTURE: {
145 return "CAPTURE";
146 }
arthurhung7632c332020-12-30 16:58:01 +0800147 case AINPUT_EVENT_TYPE_DRAG: {
148 return "DRAG";
149 }
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700150 case AINPUT_EVENT_TYPE_TOUCH_MODE: {
151 return "TOUCH_MODE";
152 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800153 }
154 return "UNKNOWN";
155}
156
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800157VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
158 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
159 event.getSource(), event.getDisplayId()},
160 event.getAction(),
161 event.getDownTime(),
162 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
163 event.getKeyCode(),
164 event.getScanCode(),
165 event.getMetaState(),
166 event.getRepeatCount()};
167}
168
169VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
170 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
171 event.getSource(), event.getDisplayId()},
172 event.getRawX(0),
173 event.getRawY(0),
174 event.getActionMasked(),
175 event.getDownTime(),
176 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
177 event.getMetaState(),
178 event.getButtonState()};
179}
180
Garfield Tan4cc839f2020-01-24 11:26:14 -0800181void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600182 std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800183 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700184 mDeviceId = deviceId;
185 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100186 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600187 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700188}
189
190void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800191 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700192 mDeviceId = from.mDeviceId;
193 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100194 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600195 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700196}
197
Garfield Tan4cc839f2020-01-24 11:26:14 -0800198int32_t InputEvent::nextId() {
199 static IdGenerator idGen(IdGenerator::Source::OTHER);
200 return idGen.nextId();
201}
202
Jeff Brown5912f952013-07-01 19:10:31 -0700203// --- KeyEvent ---
204
Michael Wright872db4f2014-04-22 15:03:51 -0700205const char* KeyEvent::getLabel(int32_t keyCode) {
Chris Ye4958d062020-08-20 13:21:10 -0700206 return InputEventLookup::getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700207}
208
Michael Wright872db4f2014-04-22 15:03:51 -0700209int32_t KeyEvent::getKeyCodeFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700210 return InputEventLookup::getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700211}
212
Garfield Tan4cc839f2020-01-24 11:26:14 -0800213void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600214 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
215 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
216 nsecs_t downTime, nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800217 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700218 mAction = action;
219 mFlags = flags;
220 mKeyCode = keyCode;
221 mScanCode = scanCode;
222 mMetaState = metaState;
223 mRepeatCount = repeatCount;
224 mDownTime = downTime;
225 mEventTime = eventTime;
226}
227
228void KeyEvent::initialize(const KeyEvent& from) {
229 InputEvent::initialize(from);
230 mAction = from.mAction;
231 mFlags = from.mFlags;
232 mKeyCode = from.mKeyCode;
233 mScanCode = from.mScanCode;
234 mMetaState = from.mMetaState;
235 mRepeatCount = from.mRepeatCount;
236 mDownTime = from.mDownTime;
237 mEventTime = from.mEventTime;
238}
239
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700240const char* KeyEvent::actionToString(int32_t action) {
241 // Convert KeyEvent action to string
242 switch (action) {
243 case AKEY_EVENT_ACTION_DOWN:
244 return "DOWN";
245 case AKEY_EVENT_ACTION_UP:
246 return "UP";
247 case AKEY_EVENT_ACTION_MULTIPLE:
248 return "MULTIPLE";
249 }
250 return "UNKNOWN";
251}
Jeff Brown5912f952013-07-01 19:10:31 -0700252
253// --- PointerCoords ---
254
255float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700256 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700257 return 0;
258 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700259 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700260}
261
262status_t PointerCoords::setAxisValue(int32_t axis, float value) {
263 if (axis < 0 || axis > 63) {
264 return NAME_NOT_FOUND;
265 }
266
Michael Wright38dcdff2014-03-19 12:06:10 -0700267 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
268 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700269 if (value == 0) {
270 return OK; // axes with value 0 do not need to be stored
271 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700272
273 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700274 if (count >= MAX_AXES) {
275 tooManyAxes(axis);
276 return NO_MEMORY;
277 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700278 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700279 for (uint32_t i = count; i > index; i--) {
280 values[i] = values[i - 1];
281 }
282 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700283
Jeff Brown5912f952013-07-01 19:10:31 -0700284 values[index] = value;
285 return OK;
286}
287
288static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
289 float value = c.getAxisValue(axis);
290 if (value != 0) {
291 c.setAxisValue(axis, value * scaleFactor);
292 }
293}
294
Robert Carre07e1032018-11-26 12:55:53 -0800295void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700296 // No need to scale pressure or size since they are normalized.
297 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800298
299 // If there is a global scale factor, it is included in the windowX/YScale
300 // so we don't need to apply it twice to the X/Y axes.
301 // However we don't want to apply any windowXYScale not included in the global scale
302 // to the TOUCH_MAJOR/MINOR coordinates.
303 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
304 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
305 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
306 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
307 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
308 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
Prabir Pradhanc6523582021-05-14 18:02:55 -0700309 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_X, windowXScale);
310 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_Y, windowYScale);
Robert Carre07e1032018-11-26 12:55:53 -0800311}
312
Jeff Brownf086ddb2014-02-11 14:28:48 -0800313void PointerCoords::applyOffset(float xOffset, float yOffset) {
314 setAxisValue(AMOTION_EVENT_AXIS_X, getX() + xOffset);
315 setAxisValue(AMOTION_EVENT_AXIS_Y, getY() + yOffset);
316}
317
Brett Chabotfaa986c2020-11-04 17:39:36 -0800318#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700319status_t PointerCoords::readFromParcel(Parcel* parcel) {
320 bits = parcel->readInt64();
321
Michael Wright38dcdff2014-03-19 12:06:10 -0700322 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700323 if (count > MAX_AXES) {
324 return BAD_VALUE;
325 }
326
327 for (uint32_t i = 0; i < count; i++) {
328 values[i] = parcel->readFloat();
329 }
330 return OK;
331}
332
333status_t PointerCoords::writeToParcel(Parcel* parcel) const {
334 parcel->writeInt64(bits);
335
Michael Wright38dcdff2014-03-19 12:06:10 -0700336 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700337 for (uint32_t i = 0; i < count; i++) {
338 parcel->writeFloat(values[i]);
339 }
340 return OK;
341}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800342#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700343
344void PointerCoords::tooManyAxes(int axis) {
345 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
346 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
347}
348
349bool PointerCoords::operator==(const PointerCoords& other) const {
350 if (bits != other.bits) {
351 return false;
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 if (values[i] != other.values[i]) {
356 return false;
357 }
358 }
359 return true;
360}
361
362void PointerCoords::copyFrom(const PointerCoords& other) {
363 bits = other.bits;
Michael Wright38dcdff2014-03-19 12:06:10 -0700364 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700365 for (uint32_t i = 0; i < count; i++) {
366 values[i] = other.values[i];
367 }
368}
369
chaviwc01e1372020-07-01 12:37:31 -0700370void PointerCoords::transform(const ui::Transform& transform) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700371 const vec2 xy = transform.transform(getXYValue());
372 setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
373 setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
374
Prabir Pradhanc6523582021-05-14 18:02:55 -0700375 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_X) ||
376 BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_Y)) {
377 const ui::Transform rotation(transform.getOrientation());
378 const vec2 relativeXy = rotation.transform(getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
379 getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
380 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
381 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
382 }
383
Prabir Pradhan6b384612021-05-14 16:56:25 -0700384 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) {
385 const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
386 setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val));
387 }
chaviwc01e1372020-07-01 12:37:31 -0700388}
Jeff Brown5912f952013-07-01 19:10:31 -0700389
390// --- PointerProperties ---
391
392bool PointerProperties::operator==(const PointerProperties& other) const {
393 return id == other.id
394 && toolType == other.toolType;
395}
396
397void PointerProperties::copyFrom(const PointerProperties& other) {
398 id = other.id;
399 toolType = other.toolType;
400}
401
402
403// --- MotionEvent ---
404
Garfield Tan4cc839f2020-01-24 11:26:14 -0800405void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600406 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
407 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700408 int32_t buttonState, MotionClassification classification,
409 const ui::Transform& transform, float xPrecision, float yPrecision,
Evan Rosky84f07f02021-04-16 10:42:42 -0700410 float rawXCursorPosition, float rawYCursorPosition,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700411 const ui::Transform& rawTransform, nsecs_t downTime, nsecs_t eventTime,
Evan Rosky09576692021-07-01 12:22:09 -0700412 size_t pointerCount, const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700413 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800414 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700415 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100416 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700417 mFlags = flags;
418 mEdgeFlags = edgeFlags;
419 mMetaState = metaState;
420 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800421 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700422 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700423 mXPrecision = xPrecision;
424 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700425 mRawXCursorPosition = rawXCursorPosition;
426 mRawYCursorPosition = rawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700427 mRawTransform = rawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700428 mDownTime = downTime;
429 mPointerProperties.clear();
430 mPointerProperties.appendArray(pointerProperties, pointerCount);
431 mSampleEventTimes.clear();
432 mSamplePointerCoords.clear();
433 addSample(eventTime, pointerCoords);
434}
435
436void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800437 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
438 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700439 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100440 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700441 mFlags = other->mFlags;
442 mEdgeFlags = other->mEdgeFlags;
443 mMetaState = other->mMetaState;
444 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800445 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700446 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700447 mXPrecision = other->mXPrecision;
448 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700449 mRawXCursorPosition = other->mRawXCursorPosition;
450 mRawYCursorPosition = other->mRawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700451 mRawTransform = other->mRawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700452 mDownTime = other->mDownTime;
453 mPointerProperties = other->mPointerProperties;
454
455 if (keepHistory) {
456 mSampleEventTimes = other->mSampleEventTimes;
457 mSamplePointerCoords = other->mSamplePointerCoords;
458 } else {
459 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500460 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700461 mSamplePointerCoords.clear();
462 size_t pointerCount = other->getPointerCount();
463 size_t historySize = other->getHistorySize();
464 mSamplePointerCoords.appendArray(other->mSamplePointerCoords.array()
465 + (historySize * pointerCount), pointerCount);
466 }
467}
468
469void MotionEvent::addSample(
470 int64_t eventTime,
471 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500472 mSampleEventTimes.push_back(eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -0700473 mSamplePointerCoords.appendArray(pointerCoords, getPointerCount());
474}
475
Garfield Tan00f511d2019-06-12 16:55:40 -0700476float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700477 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
478 return vals.x;
Garfield Tan00f511d2019-06-12 16:55:40 -0700479}
480
481float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700482 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
483 return vals.y;
Garfield Tan00f511d2019-06-12 16:55:40 -0700484}
485
Garfield Tan937bb832019-07-25 17:48:31 -0700486void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700487 ui::Transform inverse = mTransform.inverse();
488 vec2 vals = inverse.transform(x, y);
489 mRawXCursorPosition = vals.x;
490 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700491}
492
Jeff Brown5912f952013-07-01 19:10:31 -0700493const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
494 return &mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
495}
496
497float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
Evan Rosky84f07f02021-04-16 10:42:42 -0700498 return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700499}
500
501float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700502 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700503}
504
505const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
506 size_t pointerIndex, size_t historicalIndex) const {
507 return &mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
508}
509
510float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan6b384612021-05-14 16:56:25 -0700511 size_t historicalIndex) const {
512 const PointerCoords* coords = getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
513
Prabir Pradhan7e1443f2021-07-23 21:01:55 +0000514 if (!isPerWindowInputRotationEnabled()) return coords->getAxisValue(axis);
515
Prabir Pradhan6b384612021-05-14 16:56:25 -0700516 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700517 // For compatibility, convert raw coordinates into logical display space.
518 const vec2 xy = shouldDisregardTranslation(mSource)
519 ? transformWithoutTranslation(mRawTransform, coords->getX(), coords->getY())
520 : mRawTransform.transform(coords->getX(), coords->getY());
Prabir Pradhan6b384612021-05-14 16:56:25 -0700521 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
522 return xy[axis];
Evan Rosky84f07f02021-04-16 10:42:42 -0700523 }
524
Prabir Pradhanc6523582021-05-14 18:02:55 -0700525 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700526 // For compatibility, since we report raw coordinates in logical display space, we
Prabir Pradhanc6523582021-05-14 18:02:55 -0700527 // need to convert the relative axes into the same orientation for consistency.
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700528 const vec2 relativeXy =
529 transformWithoutTranslation(mRawTransform,
Evan Rosky09576692021-07-01 12:22:09 -0700530 coords->getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
531 coords->getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
Prabir Pradhanc6523582021-05-14 18:02:55 -0700532 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
533 }
534
Prabir Pradhan6b384612021-05-14 16:56:25 -0700535 return coords->getAxisValue(axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700536}
537
538float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan9f388812021-05-13 16:54:53 -0700539 size_t historicalIndex) const {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700540 const PointerCoords* coords = getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
541
542 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700543 const vec2 xy = shouldDisregardTranslation(mSource)
544 ? transformWithoutTranslation(mTransform, coords->getX(), coords->getY())
Prabir Pradhan9f388812021-05-13 16:54:53 -0700545 : mTransform.transform(coords->getXYValue());
Prabir Pradhan6b384612021-05-14 16:56:25 -0700546 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
547 return xy[axis];
chaviw9eaa22c2020-07-01 16:21:27 -0700548 }
549
Prabir Pradhanc6523582021-05-14 18:02:55 -0700550 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
551 const vec2 relativeXy =
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700552 transformWithoutTranslation(mTransform,
553 coords->getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
554 coords->getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
Prabir Pradhanc6523582021-05-14 18:02:55 -0700555 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
556 }
557
Prabir Pradhan6b384612021-05-14 16:56:25 -0700558 return coords->getAxisValue(axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700559}
560
561ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
562 size_t pointerCount = mPointerProperties.size();
563 for (size_t i = 0; i < pointerCount; i++) {
564 if (mPointerProperties.itemAt(i).id == pointerId) {
565 return i;
566 }
567 }
568 return -1;
569}
570
571void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700572 float currXOffset = mTransform.tx();
573 float currYOffset = mTransform.ty();
574 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700575}
576
Robert Carre07e1032018-11-26 12:55:53 -0800577void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700578 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700579 mRawTransform.set(mRawTransform.tx() * globalScaleFactor,
580 mRawTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800581 mXPrecision *= globalScaleFactor;
582 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700583
584 size_t numSamples = mSamplePointerCoords.size();
585 for (size_t i = 0; i < numSamples; i++) {
chaviw9eaa22c2020-07-01 16:21:27 -0700586 mSamplePointerCoords.editItemAt(i).scale(globalScaleFactor, globalScaleFactor,
587 globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700588 }
589}
590
chaviw9eaa22c2020-07-01 16:21:27 -0700591void MotionEvent::transform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700592 // We want to preserve the raw axes values stored in the PointerCoords, so we just update the
593 // transform using the values passed in.
chaviw9eaa22c2020-07-01 16:21:27 -0700594 ui::Transform newTransform;
595 newTransform.set(matrix);
596 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700597
Prabir Pradhan6b384612021-05-14 16:56:25 -0700598 // We need to update the AXIS_ORIENTATION value here to maintain the old behavior where the
599 // orientation angle is not affected by the initial transformation set in the MotionEvent.
600 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
601 [&newTransform](PointerCoords& c) {
602 float orientation = c.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
603 c.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
604 transformAngle(newTransform, orientation));
605 });
Jeff Brown5912f952013-07-01 19:10:31 -0700606}
607
Evan Roskyd4d4d802021-05-03 20:12:21 -0700608void MotionEvent::applyTransform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700609 ui::Transform transform;
610 transform.set(matrix);
Evan Roskyd4d4d802021-05-03 20:12:21 -0700611
612 // Apply the transformation to all samples.
Prabir Pradhan6b384612021-05-14 16:56:25 -0700613 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
614 [&transform](PointerCoords& c) { c.transform(transform); });
Prabir Pradhan4b19bd02021-06-01 17:34:59 -0700615
616 if (mRawXCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION &&
617 mRawYCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) {
618 const vec2 cursor = transform.transform(mRawXCursorPosition, mRawYCursorPosition);
619 mRawXCursorPosition = cursor.x;
620 mRawYCursorPosition = cursor.y;
621 }
Evan Roskyd4d4d802021-05-03 20:12:21 -0700622}
623
Brett Chabotfaa986c2020-11-04 17:39:36 -0800624#ifdef __linux__
chaviw9eaa22c2020-07-01 16:21:27 -0700625static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
626 float dsdx, dtdx, tx, dtdy, dsdy, ty;
627 status_t status = parcel.readFloat(&dsdx);
628 status |= parcel.readFloat(&dtdx);
629 status |= parcel.readFloat(&tx);
630 status |= parcel.readFloat(&dtdy);
631 status |= parcel.readFloat(&dsdy);
632 status |= parcel.readFloat(&ty);
633
634 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
635 return status;
636}
637
638static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
639 status_t status = parcel.writeFloat(transform.dsdx());
640 status |= parcel.writeFloat(transform.dtdx());
641 status |= parcel.writeFloat(transform.tx());
642 status |= parcel.writeFloat(transform.dtdy());
643 status |= parcel.writeFloat(transform.dsdy());
644 status |= parcel.writeFloat(transform.ty());
645 return status;
646}
647
Jeff Brown5912f952013-07-01 19:10:31 -0700648status_t MotionEvent::readFromParcel(Parcel* parcel) {
649 size_t pointerCount = parcel->readInt32();
650 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800651 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
652 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700653 return BAD_VALUE;
654 }
655
Garfield Tan4cc839f2020-01-24 11:26:14 -0800656 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700657 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600658 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800659 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600660 std::vector<uint8_t> hmac;
661 status_t result = parcel->readByteVector(&hmac);
662 if (result != OK || hmac.size() != 32) {
663 return BAD_VALUE;
664 }
665 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700666 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100667 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700668 mFlags = parcel->readInt32();
669 mEdgeFlags = parcel->readInt32();
670 mMetaState = parcel->readInt32();
671 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800672 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700673
674 result = android::readFromParcel(mTransform, *parcel);
675 if (result != OK) {
676 return result;
677 }
Jeff Brown5912f952013-07-01 19:10:31 -0700678 mXPrecision = parcel->readFloat();
679 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700680 mRawXCursorPosition = parcel->readFloat();
681 mRawYCursorPosition = parcel->readFloat();
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700682
683 result = android::readFromParcel(mRawTransform, *parcel);
684 if (result != OK) {
685 return result;
686 }
Jeff Brown5912f952013-07-01 19:10:31 -0700687 mDownTime = parcel->readInt64();
688
689 mPointerProperties.clear();
690 mPointerProperties.setCapacity(pointerCount);
691 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500692 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700693 mSamplePointerCoords.clear();
694 mSamplePointerCoords.setCapacity(sampleCount * pointerCount);
695
696 for (size_t i = 0; i < pointerCount; i++) {
697 mPointerProperties.push();
698 PointerProperties& properties = mPointerProperties.editTop();
699 properties.id = parcel->readInt32();
700 properties.toolType = parcel->readInt32();
701 }
702
Dan Austinc94fc452015-09-22 14:22:41 -0700703 while (sampleCount > 0) {
704 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500705 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700706 for (size_t i = 0; i < pointerCount; i++) {
707 mSamplePointerCoords.push();
708 status_t status = mSamplePointerCoords.editTop().readFromParcel(parcel);
709 if (status) {
710 return status;
711 }
712 }
713 }
714 return OK;
715}
716
717status_t MotionEvent::writeToParcel(Parcel* parcel) const {
718 size_t pointerCount = mPointerProperties.size();
719 size_t sampleCount = mSampleEventTimes.size();
720
721 parcel->writeInt32(pointerCount);
722 parcel->writeInt32(sampleCount);
723
Garfield Tan4cc839f2020-01-24 11:26:14 -0800724 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700725 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600726 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800727 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600728 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
729 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700730 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100731 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700732 parcel->writeInt32(mFlags);
733 parcel->writeInt32(mEdgeFlags);
734 parcel->writeInt32(mMetaState);
735 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800736 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700737
738 status_t result = android::writeToParcel(mTransform, *parcel);
739 if (result != OK) {
740 return result;
741 }
Jeff Brown5912f952013-07-01 19:10:31 -0700742 parcel->writeFloat(mXPrecision);
743 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700744 parcel->writeFloat(mRawXCursorPosition);
745 parcel->writeFloat(mRawYCursorPosition);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700746
747 result = android::writeToParcel(mRawTransform, *parcel);
748 if (result != OK) {
749 return result;
750 }
Jeff Brown5912f952013-07-01 19:10:31 -0700751 parcel->writeInt64(mDownTime);
752
753 for (size_t i = 0; i < pointerCount; i++) {
754 const PointerProperties& properties = mPointerProperties.itemAt(i);
755 parcel->writeInt32(properties.id);
756 parcel->writeInt32(properties.toolType);
757 }
758
759 const PointerCoords* pc = mSamplePointerCoords.array();
760 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500761 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700762 for (size_t i = 0; i < pointerCount; i++) {
763 status_t status = (pc++)->writeToParcel(parcel);
764 if (status) {
765 return status;
766 }
767 }
768 }
769 return OK;
770}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800771#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700772
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600773bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Jeff Brown5912f952013-07-01 19:10:31 -0700774 if (source & AINPUT_SOURCE_CLASS_POINTER) {
775 // Specifically excludes HOVER_MOVE and SCROLL.
776 switch (action & AMOTION_EVENT_ACTION_MASK) {
777 case AMOTION_EVENT_ACTION_DOWN:
778 case AMOTION_EVENT_ACTION_MOVE:
779 case AMOTION_EVENT_ACTION_UP:
780 case AMOTION_EVENT_ACTION_POINTER_DOWN:
781 case AMOTION_EVENT_ACTION_POINTER_UP:
782 case AMOTION_EVENT_ACTION_CANCEL:
783 case AMOTION_EVENT_ACTION_OUTSIDE:
784 return true;
785 }
786 }
787 return false;
788}
789
Michael Wright872db4f2014-04-22 15:03:51 -0700790const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700791 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700792}
793
794int32_t MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700795 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700796}
797
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500798std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700799 // Convert MotionEvent action to string
800 switch (action & AMOTION_EVENT_ACTION_MASK) {
801 case AMOTION_EVENT_ACTION_DOWN:
802 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700803 case AMOTION_EVENT_ACTION_UP:
804 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500805 case AMOTION_EVENT_ACTION_MOVE:
806 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700807 case AMOTION_EVENT_ACTION_CANCEL:
808 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500809 case AMOTION_EVENT_ACTION_OUTSIDE:
810 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700811 case AMOTION_EVENT_ACTION_POINTER_DOWN:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000812 return StringPrintf("POINTER_DOWN(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700813 case AMOTION_EVENT_ACTION_POINTER_UP:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000814 return StringPrintf("POINTER_UP(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500815 case AMOTION_EVENT_ACTION_HOVER_MOVE:
816 return "HOVER_MOVE";
817 case AMOTION_EVENT_ACTION_SCROLL:
818 return "SCROLL";
819 case AMOTION_EVENT_ACTION_HOVER_ENTER:
820 return "HOVER_ENTER";
821 case AMOTION_EVENT_ACTION_HOVER_EXIT:
822 return "HOVER_EXIT";
823 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
824 return "BUTTON_PRESS";
825 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
826 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700827 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500828 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700829}
830
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800831// --- FocusEvent ---
832
Garfield Tan4cc839f2020-01-24 11:26:14 -0800833void FocusEvent::initialize(int32_t id, bool hasFocus, bool inTouchMode) {
834 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600835 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800836 mHasFocus = hasFocus;
837 mInTouchMode = inTouchMode;
838}
839
840void FocusEvent::initialize(const FocusEvent& from) {
841 InputEvent::initialize(from);
842 mHasFocus = from.mHasFocus;
843 mInTouchMode = from.mInTouchMode;
844}
Jeff Brown5912f952013-07-01 19:10:31 -0700845
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800846// --- CaptureEvent ---
847
848void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) {
849 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
850 ADISPLAY_ID_NONE, INVALID_HMAC);
851 mPointerCaptureEnabled = pointerCaptureEnabled;
852}
853
854void CaptureEvent::initialize(const CaptureEvent& from) {
855 InputEvent::initialize(from);
856 mPointerCaptureEnabled = from.mPointerCaptureEnabled;
857}
858
arthurhung7632c332020-12-30 16:58:01 +0800859// --- DragEvent ---
860
861void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) {
862 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
863 ADISPLAY_ID_NONE, INVALID_HMAC);
864 mIsExiting = isExiting;
865 mX = x;
866 mY = y;
867}
868
869void DragEvent::initialize(const DragEvent& from) {
870 InputEvent::initialize(from);
871 mIsExiting = from.mIsExiting;
872 mX = from.mX;
873 mY = from.mY;
874}
875
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700876// --- TouchModeEvent ---
877
878void TouchModeEvent::initialize(int32_t id, bool isInTouchMode) {
879 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
880 ADISPLAY_ID_NONE, INVALID_HMAC);
881 mIsInTouchMode = isInTouchMode;
882}
883
884void TouchModeEvent::initialize(const TouchModeEvent& from) {
885 InputEvent::initialize(from);
886 mIsInTouchMode = from.mIsInTouchMode;
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
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700943TouchModeEvent* PooledInputEventFactory::createTouchModeEvent() {
944 if (mTouchModeEventPool.empty()) {
945 return new TouchModeEvent();
946 }
947 TouchModeEvent* event = mTouchModeEventPool.front().release();
948 mTouchModeEventPool.pop();
949 return event;
950}
951
Jeff Brown5912f952013-07-01 19:10:31 -0700952void PooledInputEventFactory::recycle(InputEvent* event) {
953 switch (event->getType()) {
954 case AINPUT_EVENT_TYPE_KEY:
955 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800956 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700957 return;
958 }
959 break;
960 case AINPUT_EVENT_TYPE_MOTION:
961 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800962 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700963 return;
964 }
965 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800966 case AINPUT_EVENT_TYPE_FOCUS:
967 if (mFocusEventPool.size() < mMaxPoolSize) {
968 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
969 return;
970 }
971 break;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800972 case AINPUT_EVENT_TYPE_CAPTURE:
973 if (mCaptureEventPool.size() < mMaxPoolSize) {
974 mCaptureEventPool.push(
975 std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event)));
976 return;
977 }
978 break;
arthurhung7632c332020-12-30 16:58:01 +0800979 case AINPUT_EVENT_TYPE_DRAG:
980 if (mDragEventPool.size() < mMaxPoolSize) {
981 mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event)));
982 return;
983 }
984 break;
Jeff Brown5912f952013-07-01 19:10:31 -0700985 }
986 delete event;
987}
988
989} // namespace android