blob: 35209f7a0727256de55b8437913d9ed87ef051f5 [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>
chaviw3277faf2021-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
Evan Rosky09576692021-07-01 12:22:09 -070079// Rotates the given point to the specified orientation. If the display width and height are
Prabir Pradhan6b384612021-05-14 16:56:25 -070080// provided, the point is rotated in the screen space. Otherwise, the point is rotated about the
81// origin. This helper is used to avoid the extra overhead of creating new Transforms.
Evan Rosky09576692021-07-01 12:22:09 -070082vec2 rotatePoint(uint32_t orientation, float x, float y, int32_t displayWidth = 0,
Prabir Pradhan6b384612021-05-14 16:56:25 -070083 int32_t displayHeight = 0) {
Prabir Pradhan6b384612021-05-14 16:56:25 -070084 if (orientation == ui::Transform::ROT_0) {
85 return {x, y};
86 }
87
88 vec2 xy(x, y);
89 if (orientation == ui::Transform::ROT_90) {
90 xy.x = displayHeight - y;
91 xy.y = x;
92 } else if (orientation == ui::Transform::ROT_180) {
93 xy.x = displayWidth - x;
94 xy.y = displayHeight - y;
95 } else if (orientation == ui::Transform::ROT_270) {
96 xy.x = y;
97 xy.y = displayWidth - x;
98 }
99 return xy;
100}
101
Prabir Pradhan9f388812021-05-13 16:54:53 -0700102vec2 applyTransformWithoutTranslation(const ui::Transform& transform, float x, float y) {
103 const vec2 transformedXy = transform.transform(x, y);
104 const vec2 transformedOrigin = transform.transform(0, 0);
105 return transformedXy - transformedOrigin;
106}
107
108bool shouldDisregardWindowTranslation(uint32_t source) {
109 // Pointer events are the only type of events that refer to absolute coordinates on the display,
110 // so we should apply the entire window transform. For other types of events, we should make
111 // sure to not apply the window translation/offset.
112 return (source & AINPUT_SOURCE_CLASS_POINTER) == 0;
113}
114
Prabir Pradhan6b384612021-05-14 16:56:25 -0700115} // namespace
116
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800117const char* motionClassificationToString(MotionClassification classification) {
118 switch (classification) {
119 case MotionClassification::NONE:
120 return "NONE";
121 case MotionClassification::AMBIGUOUS_GESTURE:
122 return "AMBIGUOUS_GESTURE";
123 case MotionClassification::DEEP_PRESS:
124 return "DEEP_PRESS";
125 }
126}
127
Garfield Tan84b087e2020-01-23 10:49:05 -0800128// --- IdGenerator ---
129IdGenerator::IdGenerator(Source source) : mSource(source) {}
130
131int32_t IdGenerator::nextId() const {
132 constexpr uint32_t SEQUENCE_NUMBER_MASK = ~SOURCE_MASK;
133 int32_t id = 0;
134
135// Avoid building against syscall getrandom(2) on host, which will fail build on Mac. Host doesn't
136// use sequence number so just always return mSource.
137#ifdef __ANDROID__
138 constexpr size_t BUF_LEN = sizeof(id);
139 size_t totalBytes = 0;
140 while (totalBytes < BUF_LEN) {
141 ssize_t bytes = TEMP_FAILURE_RETRY(getrandom(&id, BUF_LEN, GRND_NONBLOCK));
142 if (CC_UNLIKELY(bytes < 0)) {
143 ALOGW("Failed to fill in random number for sequence number: %s.", strerror(errno));
144 id = 0;
145 break;
146 }
147 totalBytes += bytes;
148 }
149#endif // __ANDROID__
150
151 return (id & SEQUENCE_NUMBER_MASK) | static_cast<int32_t>(mSource);
152}
153
Jeff Brown5912f952013-07-01 19:10:31 -0700154// --- InputEvent ---
155
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800156const char* inputEventTypeToString(int32_t type) {
157 switch (type) {
158 case AINPUT_EVENT_TYPE_KEY: {
159 return "KEY";
160 }
161 case AINPUT_EVENT_TYPE_MOTION: {
162 return "MOTION";
163 }
164 case AINPUT_EVENT_TYPE_FOCUS: {
165 return "FOCUS";
166 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800167 case AINPUT_EVENT_TYPE_CAPTURE: {
168 return "CAPTURE";
169 }
arthurhung7632c332020-12-30 16:58:01 +0800170 case AINPUT_EVENT_TYPE_DRAG: {
171 return "DRAG";
172 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800173 }
174 return "UNKNOWN";
175}
176
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800177VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
178 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
179 event.getSource(), event.getDisplayId()},
180 event.getAction(),
181 event.getDownTime(),
182 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
183 event.getKeyCode(),
184 event.getScanCode(),
185 event.getMetaState(),
186 event.getRepeatCount()};
187}
188
189VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
190 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
191 event.getSource(), event.getDisplayId()},
192 event.getRawX(0),
193 event.getRawY(0),
194 event.getActionMasked(),
195 event.getDownTime(),
196 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
197 event.getMetaState(),
198 event.getButtonState()};
199}
200
Garfield Tan4cc839f2020-01-24 11:26:14 -0800201void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600202 std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800203 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700204 mDeviceId = deviceId;
205 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100206 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600207 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700208}
209
210void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800211 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700212 mDeviceId = from.mDeviceId;
213 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100214 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600215 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700216}
217
Garfield Tan4cc839f2020-01-24 11:26:14 -0800218int32_t InputEvent::nextId() {
219 static IdGenerator idGen(IdGenerator::Source::OTHER);
220 return idGen.nextId();
221}
222
Jeff Brown5912f952013-07-01 19:10:31 -0700223// --- KeyEvent ---
224
Michael Wright872db4f2014-04-22 15:03:51 -0700225const char* KeyEvent::getLabel(int32_t keyCode) {
Chris Ye4958d062020-08-20 13:21:10 -0700226 return InputEventLookup::getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700227}
228
Michael Wright872db4f2014-04-22 15:03:51 -0700229int32_t KeyEvent::getKeyCodeFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700230 return InputEventLookup::getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700231}
232
Garfield Tan4cc839f2020-01-24 11:26:14 -0800233void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600234 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
235 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
236 nsecs_t downTime, nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800237 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700238 mAction = action;
239 mFlags = flags;
240 mKeyCode = keyCode;
241 mScanCode = scanCode;
242 mMetaState = metaState;
243 mRepeatCount = repeatCount;
244 mDownTime = downTime;
245 mEventTime = eventTime;
246}
247
248void KeyEvent::initialize(const KeyEvent& from) {
249 InputEvent::initialize(from);
250 mAction = from.mAction;
251 mFlags = from.mFlags;
252 mKeyCode = from.mKeyCode;
253 mScanCode = from.mScanCode;
254 mMetaState = from.mMetaState;
255 mRepeatCount = from.mRepeatCount;
256 mDownTime = from.mDownTime;
257 mEventTime = from.mEventTime;
258}
259
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700260const char* KeyEvent::actionToString(int32_t action) {
261 // Convert KeyEvent action to string
262 switch (action) {
263 case AKEY_EVENT_ACTION_DOWN:
264 return "DOWN";
265 case AKEY_EVENT_ACTION_UP:
266 return "UP";
267 case AKEY_EVENT_ACTION_MULTIPLE:
268 return "MULTIPLE";
269 }
270 return "UNKNOWN";
271}
Jeff Brown5912f952013-07-01 19:10:31 -0700272
273// --- PointerCoords ---
274
275float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700276 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700277 return 0;
278 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700279 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700280}
281
282status_t PointerCoords::setAxisValue(int32_t axis, float value) {
283 if (axis < 0 || axis > 63) {
284 return NAME_NOT_FOUND;
285 }
286
Michael Wright38dcdff2014-03-19 12:06:10 -0700287 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
288 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700289 if (value == 0) {
290 return OK; // axes with value 0 do not need to be stored
291 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700292
293 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700294 if (count >= MAX_AXES) {
295 tooManyAxes(axis);
296 return NO_MEMORY;
297 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700298 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700299 for (uint32_t i = count; i > index; i--) {
300 values[i] = values[i - 1];
301 }
302 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700303
Jeff Brown5912f952013-07-01 19:10:31 -0700304 values[index] = value;
305 return OK;
306}
307
308static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
309 float value = c.getAxisValue(axis);
310 if (value != 0) {
311 c.setAxisValue(axis, value * scaleFactor);
312 }
313}
314
Robert Carre07e1032018-11-26 12:55:53 -0800315void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700316 // No need to scale pressure or size since they are normalized.
317 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800318
319 // If there is a global scale factor, it is included in the windowX/YScale
320 // so we don't need to apply it twice to the X/Y axes.
321 // However we don't want to apply any windowXYScale not included in the global scale
322 // to the TOUCH_MAJOR/MINOR coordinates.
323 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
324 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
325 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
326 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
327 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
328 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
Prabir Pradhanc6523582021-05-14 18:02:55 -0700329 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_X, windowXScale);
330 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_Y, windowYScale);
Robert Carre07e1032018-11-26 12:55:53 -0800331}
332
333void PointerCoords::scale(float globalScaleFactor) {
334 scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700335}
336
Jeff Brownf086ddb2014-02-11 14:28:48 -0800337void PointerCoords::applyOffset(float xOffset, float yOffset) {
338 setAxisValue(AMOTION_EVENT_AXIS_X, getX() + xOffset);
339 setAxisValue(AMOTION_EVENT_AXIS_Y, getY() + yOffset);
340}
341
Brett Chabotfaa986c2020-11-04 17:39:36 -0800342#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700343status_t PointerCoords::readFromParcel(Parcel* parcel) {
344 bits = parcel->readInt64();
345
Michael Wright38dcdff2014-03-19 12:06:10 -0700346 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700347 if (count > MAX_AXES) {
348 return BAD_VALUE;
349 }
350
351 for (uint32_t i = 0; i < count; i++) {
352 values[i] = parcel->readFloat();
353 }
354 return OK;
355}
356
357status_t PointerCoords::writeToParcel(Parcel* parcel) const {
358 parcel->writeInt64(bits);
359
Michael Wright38dcdff2014-03-19 12:06:10 -0700360 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700361 for (uint32_t i = 0; i < count; i++) {
362 parcel->writeFloat(values[i]);
363 }
364 return OK;
365}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800366#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700367
368void PointerCoords::tooManyAxes(int axis) {
369 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
370 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
371}
372
373bool PointerCoords::operator==(const PointerCoords& other) const {
374 if (bits != other.bits) {
375 return false;
376 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700377 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700378 for (uint32_t i = 0; i < count; i++) {
379 if (values[i] != other.values[i]) {
380 return false;
381 }
382 }
383 return true;
384}
385
386void PointerCoords::copyFrom(const PointerCoords& other) {
387 bits = other.bits;
Michael Wright38dcdff2014-03-19 12:06:10 -0700388 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700389 for (uint32_t i = 0; i < count; i++) {
390 values[i] = other.values[i];
391 }
392}
393
chaviwc01e1372020-07-01 12:37:31 -0700394void PointerCoords::transform(const ui::Transform& transform) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700395 const vec2 xy = transform.transform(getXYValue());
396 setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
397 setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
398
Prabir Pradhanc6523582021-05-14 18:02:55 -0700399 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_X) ||
400 BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_Y)) {
401 const ui::Transform rotation(transform.getOrientation());
402 const vec2 relativeXy = rotation.transform(getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
403 getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
404 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
405 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
406 }
407
Prabir Pradhan6b384612021-05-14 16:56:25 -0700408 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) {
409 const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
410 setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val));
411 }
chaviwc01e1372020-07-01 12:37:31 -0700412}
Jeff Brown5912f952013-07-01 19:10:31 -0700413
414// --- PointerProperties ---
415
416bool PointerProperties::operator==(const PointerProperties& other) const {
417 return id == other.id
418 && toolType == other.toolType;
419}
420
421void PointerProperties::copyFrom(const PointerProperties& other) {
422 id = other.id;
423 toolType = other.toolType;
424}
425
426
427// --- MotionEvent ---
428
Garfield Tan4cc839f2020-01-24 11:26:14 -0800429void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600430 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
431 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700432 int32_t buttonState, MotionClassification classification,
433 const ui::Transform& transform, float xPrecision, float yPrecision,
Evan Rosky84f07f02021-04-16 10:42:42 -0700434 float rawXCursorPosition, float rawYCursorPosition,
Evan Rosky09576692021-07-01 12:22:09 -0700435 uint32_t displayOrientation, int32_t displayWidth,
436 int32_t displayHeight, nsecs_t downTime, nsecs_t eventTime,
437 size_t pointerCount, const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700438 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800439 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700440 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100441 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700442 mFlags = flags;
443 mEdgeFlags = edgeFlags;
444 mMetaState = metaState;
445 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800446 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700447 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700448 mXPrecision = xPrecision;
449 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700450 mRawXCursorPosition = rawXCursorPosition;
451 mRawYCursorPosition = rawYCursorPosition;
Evan Rosky09576692021-07-01 12:22:09 -0700452 mDisplayOrientation = displayOrientation;
Evan Rosky84f07f02021-04-16 10:42:42 -0700453 mDisplayWidth = displayWidth;
454 mDisplayHeight = displayHeight;
Jeff Brown5912f952013-07-01 19:10:31 -0700455 mDownTime = downTime;
456 mPointerProperties.clear();
457 mPointerProperties.appendArray(pointerProperties, pointerCount);
458 mSampleEventTimes.clear();
459 mSamplePointerCoords.clear();
460 addSample(eventTime, pointerCoords);
461}
462
463void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800464 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
465 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700466 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100467 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700468 mFlags = other->mFlags;
469 mEdgeFlags = other->mEdgeFlags;
470 mMetaState = other->mMetaState;
471 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800472 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700473 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700474 mXPrecision = other->mXPrecision;
475 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700476 mRawXCursorPosition = other->mRawXCursorPosition;
477 mRawYCursorPosition = other->mRawYCursorPosition;
Evan Rosky09576692021-07-01 12:22:09 -0700478 mDisplayOrientation = other->mDisplayOrientation;
Evan Rosky84f07f02021-04-16 10:42:42 -0700479 mDisplayWidth = other->mDisplayWidth;
480 mDisplayHeight = other->mDisplayHeight;
Jeff Brown5912f952013-07-01 19:10:31 -0700481 mDownTime = other->mDownTime;
482 mPointerProperties = other->mPointerProperties;
483
484 if (keepHistory) {
485 mSampleEventTimes = other->mSampleEventTimes;
486 mSamplePointerCoords = other->mSamplePointerCoords;
487 } else {
488 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500489 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700490 mSamplePointerCoords.clear();
491 size_t pointerCount = other->getPointerCount();
492 size_t historySize = other->getHistorySize();
493 mSamplePointerCoords.appendArray(other->mSamplePointerCoords.array()
494 + (historySize * pointerCount), pointerCount);
495 }
496}
497
498void MotionEvent::addSample(
499 int64_t eventTime,
500 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500501 mSampleEventTimes.push_back(eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -0700502 mSamplePointerCoords.appendArray(pointerCoords, getPointerCount());
503}
504
Garfield Tan00f511d2019-06-12 16:55:40 -0700505float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700506 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
507 return vals.x;
Garfield Tan00f511d2019-06-12 16:55:40 -0700508}
509
510float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700511 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
512 return vals.y;
Garfield Tan00f511d2019-06-12 16:55:40 -0700513}
514
Garfield Tan937bb832019-07-25 17:48:31 -0700515void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700516 ui::Transform inverse = mTransform.inverse();
517 vec2 vals = inverse.transform(x, y);
518 mRawXCursorPosition = vals.x;
519 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700520}
521
Jeff Brown5912f952013-07-01 19:10:31 -0700522const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
523 return &mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
524}
525
526float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
Evan Rosky84f07f02021-04-16 10:42:42 -0700527 return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700528}
529
530float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700531 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700532}
533
534const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
535 size_t pointerIndex, size_t historicalIndex) const {
536 return &mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
537}
538
539float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan6b384612021-05-14 16:56:25 -0700540 size_t historicalIndex) const {
541 const PointerCoords* coords = getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
542
Prabir Pradhan7e1443f2021-07-23 21:01:55 +0000543 if (!isPerWindowInputRotationEnabled()) return coords->getAxisValue(axis);
544
Prabir Pradhan6b384612021-05-14 16:56:25 -0700545 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
546 // For compatibility, convert raw coordinates into "oriented screen space". Once app
547 // developers are educated about getRaw, we can consider removing this.
Prabir Pradhan9f388812021-05-13 16:54:53 -0700548 const vec2 xy = shouldDisregardWindowTranslation(mSource)
Evan Rosky09576692021-07-01 12:22:09 -0700549 ? rotatePoint(mDisplayOrientation, coords->getX(), coords->getY())
550 : rotatePoint(mDisplayOrientation, coords->getX(), coords->getY(), mDisplayWidth,
Prabir Pradhan9f388812021-05-13 16:54:53 -0700551 mDisplayHeight);
Prabir Pradhan6b384612021-05-14 16:56:25 -0700552 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
553 return xy[axis];
Evan Rosky84f07f02021-04-16 10:42:42 -0700554 }
555
Prabir Pradhanc6523582021-05-14 18:02:55 -0700556 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
557 // For compatibility, since we convert raw coordinates into "oriented screen space", we
558 // need to convert the relative axes into the same orientation for consistency.
Evan Rosky09576692021-07-01 12:22:09 -0700559 const vec2 relativeXy = rotatePoint(mDisplayOrientation,
560 coords->getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
561 coords->getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
Prabir Pradhanc6523582021-05-14 18:02:55 -0700562 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
563 }
564
Prabir Pradhan6b384612021-05-14 16:56:25 -0700565 return coords->getAxisValue(axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700566}
567
568float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan9f388812021-05-13 16:54:53 -0700569 size_t historicalIndex) const {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700570 const PointerCoords* coords = getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
571
572 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
Prabir Pradhan9f388812021-05-13 16:54:53 -0700573 const vec2 xy = shouldDisregardWindowTranslation(mSource)
574 ? applyTransformWithoutTranslation(mTransform, coords->getX(), coords->getY())
575 : mTransform.transform(coords->getXYValue());
Prabir Pradhan6b384612021-05-14 16:56:25 -0700576 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
577 return xy[axis];
chaviw9eaa22c2020-07-01 16:21:27 -0700578 }
579
Prabir Pradhanc6523582021-05-14 18:02:55 -0700580 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
581 const vec2 relativeXy =
582 applyTransformWithoutTranslation(mTransform,
583 coords->getAxisValue(
584 AMOTION_EVENT_AXIS_RELATIVE_X),
585 coords->getAxisValue(
586 AMOTION_EVENT_AXIS_RELATIVE_Y));
587 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
588 }
589
Prabir Pradhan6b384612021-05-14 16:56:25 -0700590 return coords->getAxisValue(axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700591}
592
593ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
594 size_t pointerCount = mPointerProperties.size();
595 for (size_t i = 0; i < pointerCount; i++) {
596 if (mPointerProperties.itemAt(i).id == pointerId) {
597 return i;
598 }
599 }
600 return -1;
601}
602
603void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700604 float currXOffset = mTransform.tx();
605 float currYOffset = mTransform.ty();
606 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700607}
608
Robert Carre07e1032018-11-26 12:55:53 -0800609void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700610 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800611 mXPrecision *= globalScaleFactor;
612 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700613
614 size_t numSamples = mSamplePointerCoords.size();
615 for (size_t i = 0; i < numSamples; i++) {
chaviw9eaa22c2020-07-01 16:21:27 -0700616 mSamplePointerCoords.editItemAt(i).scale(globalScaleFactor, globalScaleFactor,
617 globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700618 }
619}
620
chaviw9eaa22c2020-07-01 16:21:27 -0700621void MotionEvent::transform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700622 // We want to preserve the raw axes values stored in the PointerCoords, so we just update the
623 // transform using the values passed in.
chaviw9eaa22c2020-07-01 16:21:27 -0700624 ui::Transform newTransform;
625 newTransform.set(matrix);
626 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700627
Prabir Pradhan6b384612021-05-14 16:56:25 -0700628 // We need to update the AXIS_ORIENTATION value here to maintain the old behavior where the
629 // orientation angle is not affected by the initial transformation set in the MotionEvent.
630 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
631 [&newTransform](PointerCoords& c) {
632 float orientation = c.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
633 c.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
634 transformAngle(newTransform, orientation));
635 });
Jeff Brown5912f952013-07-01 19:10:31 -0700636}
637
Evan Roskyd4d4d802021-05-03 20:12:21 -0700638void MotionEvent::applyTransform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700639 ui::Transform transform;
640 transform.set(matrix);
Evan Roskyd4d4d802021-05-03 20:12:21 -0700641
642 // Apply the transformation to all samples.
Prabir Pradhan6b384612021-05-14 16:56:25 -0700643 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
644 [&transform](PointerCoords& c) { c.transform(transform); });
Prabir Pradhan4b19bd02021-06-01 17:34:59 -0700645
646 if (mRawXCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION &&
647 mRawYCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) {
648 const vec2 cursor = transform.transform(mRawXCursorPosition, mRawYCursorPosition);
649 mRawXCursorPosition = cursor.x;
650 mRawYCursorPosition = cursor.y;
651 }
Evan Roskyd4d4d802021-05-03 20:12:21 -0700652}
653
Brett Chabotfaa986c2020-11-04 17:39:36 -0800654#ifdef __linux__
chaviw9eaa22c2020-07-01 16:21:27 -0700655static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
656 float dsdx, dtdx, tx, dtdy, dsdy, ty;
657 status_t status = parcel.readFloat(&dsdx);
658 status |= parcel.readFloat(&dtdx);
659 status |= parcel.readFloat(&tx);
660 status |= parcel.readFloat(&dtdy);
661 status |= parcel.readFloat(&dsdy);
662 status |= parcel.readFloat(&ty);
663
664 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
665 return status;
666}
667
668static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
669 status_t status = parcel.writeFloat(transform.dsdx());
670 status |= parcel.writeFloat(transform.dtdx());
671 status |= parcel.writeFloat(transform.tx());
672 status |= parcel.writeFloat(transform.dtdy());
673 status |= parcel.writeFloat(transform.dsdy());
674 status |= parcel.writeFloat(transform.ty());
675 return status;
676}
677
Jeff Brown5912f952013-07-01 19:10:31 -0700678status_t MotionEvent::readFromParcel(Parcel* parcel) {
679 size_t pointerCount = parcel->readInt32();
680 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800681 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
682 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700683 return BAD_VALUE;
684 }
685
Garfield Tan4cc839f2020-01-24 11:26:14 -0800686 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700687 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600688 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800689 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600690 std::vector<uint8_t> hmac;
691 status_t result = parcel->readByteVector(&hmac);
692 if (result != OK || hmac.size() != 32) {
693 return BAD_VALUE;
694 }
695 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700696 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100697 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700698 mFlags = parcel->readInt32();
699 mEdgeFlags = parcel->readInt32();
700 mMetaState = parcel->readInt32();
701 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800702 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700703
704 result = android::readFromParcel(mTransform, *parcel);
705 if (result != OK) {
706 return result;
707 }
Jeff Brown5912f952013-07-01 19:10:31 -0700708 mXPrecision = parcel->readFloat();
709 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700710 mRawXCursorPosition = parcel->readFloat();
711 mRawYCursorPosition = parcel->readFloat();
Evan Rosky09576692021-07-01 12:22:09 -0700712 mDisplayOrientation = parcel->readUint32();
Evan Rosky84f07f02021-04-16 10:42:42 -0700713 mDisplayWidth = parcel->readInt32();
714 mDisplayHeight = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700715 mDownTime = parcel->readInt64();
716
717 mPointerProperties.clear();
718 mPointerProperties.setCapacity(pointerCount);
719 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500720 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700721 mSamplePointerCoords.clear();
722 mSamplePointerCoords.setCapacity(sampleCount * pointerCount);
723
724 for (size_t i = 0; i < pointerCount; i++) {
725 mPointerProperties.push();
726 PointerProperties& properties = mPointerProperties.editTop();
727 properties.id = parcel->readInt32();
728 properties.toolType = parcel->readInt32();
729 }
730
Dan Austinc94fc452015-09-22 14:22:41 -0700731 while (sampleCount > 0) {
732 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500733 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700734 for (size_t i = 0; i < pointerCount; i++) {
735 mSamplePointerCoords.push();
736 status_t status = mSamplePointerCoords.editTop().readFromParcel(parcel);
737 if (status) {
738 return status;
739 }
740 }
741 }
742 return OK;
743}
744
745status_t MotionEvent::writeToParcel(Parcel* parcel) const {
746 size_t pointerCount = mPointerProperties.size();
747 size_t sampleCount = mSampleEventTimes.size();
748
749 parcel->writeInt32(pointerCount);
750 parcel->writeInt32(sampleCount);
751
Garfield Tan4cc839f2020-01-24 11:26:14 -0800752 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700753 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600754 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800755 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600756 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
757 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700758 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100759 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700760 parcel->writeInt32(mFlags);
761 parcel->writeInt32(mEdgeFlags);
762 parcel->writeInt32(mMetaState);
763 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800764 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700765
766 status_t result = android::writeToParcel(mTransform, *parcel);
767 if (result != OK) {
768 return result;
769 }
Jeff Brown5912f952013-07-01 19:10:31 -0700770 parcel->writeFloat(mXPrecision);
771 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700772 parcel->writeFloat(mRawXCursorPosition);
773 parcel->writeFloat(mRawYCursorPosition);
Evan Rosky09576692021-07-01 12:22:09 -0700774 parcel->writeUint32(mDisplayOrientation);
Evan Rosky84f07f02021-04-16 10:42:42 -0700775 parcel->writeInt32(mDisplayWidth);
776 parcel->writeInt32(mDisplayHeight);
Jeff Brown5912f952013-07-01 19:10:31 -0700777 parcel->writeInt64(mDownTime);
778
779 for (size_t i = 0; i < pointerCount; i++) {
780 const PointerProperties& properties = mPointerProperties.itemAt(i);
781 parcel->writeInt32(properties.id);
782 parcel->writeInt32(properties.toolType);
783 }
784
785 const PointerCoords* pc = mSamplePointerCoords.array();
786 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500787 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700788 for (size_t i = 0; i < pointerCount; i++) {
789 status_t status = (pc++)->writeToParcel(parcel);
790 if (status) {
791 return status;
792 }
793 }
794 }
795 return OK;
796}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800797#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700798
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600799bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Jeff Brown5912f952013-07-01 19:10:31 -0700800 if (source & AINPUT_SOURCE_CLASS_POINTER) {
801 // Specifically excludes HOVER_MOVE and SCROLL.
802 switch (action & AMOTION_EVENT_ACTION_MASK) {
803 case AMOTION_EVENT_ACTION_DOWN:
804 case AMOTION_EVENT_ACTION_MOVE:
805 case AMOTION_EVENT_ACTION_UP:
806 case AMOTION_EVENT_ACTION_POINTER_DOWN:
807 case AMOTION_EVENT_ACTION_POINTER_UP:
808 case AMOTION_EVENT_ACTION_CANCEL:
809 case AMOTION_EVENT_ACTION_OUTSIDE:
810 return true;
811 }
812 }
813 return false;
814}
815
Michael Wright872db4f2014-04-22 15:03:51 -0700816const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700817 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700818}
819
820int32_t MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700821 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700822}
823
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500824std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700825 // Convert MotionEvent action to string
826 switch (action & AMOTION_EVENT_ACTION_MASK) {
827 case AMOTION_EVENT_ACTION_DOWN:
828 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700829 case AMOTION_EVENT_ACTION_UP:
830 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500831 case AMOTION_EVENT_ACTION_MOVE:
832 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700833 case AMOTION_EVENT_ACTION_CANCEL:
834 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500835 case AMOTION_EVENT_ACTION_OUTSIDE:
836 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700837 case AMOTION_EVENT_ACTION_POINTER_DOWN:
838 return "POINTER_DOWN";
839 case AMOTION_EVENT_ACTION_POINTER_UP:
840 return "POINTER_UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500841 case AMOTION_EVENT_ACTION_HOVER_MOVE:
842 return "HOVER_MOVE";
843 case AMOTION_EVENT_ACTION_SCROLL:
844 return "SCROLL";
845 case AMOTION_EVENT_ACTION_HOVER_ENTER:
846 return "HOVER_ENTER";
847 case AMOTION_EVENT_ACTION_HOVER_EXIT:
848 return "HOVER_EXIT";
849 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
850 return "BUTTON_PRESS";
851 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
852 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700853 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500854 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700855}
856
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800857// --- FocusEvent ---
858
Garfield Tan4cc839f2020-01-24 11:26:14 -0800859void FocusEvent::initialize(int32_t id, bool hasFocus, bool inTouchMode) {
860 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600861 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800862 mHasFocus = hasFocus;
863 mInTouchMode = inTouchMode;
864}
865
866void FocusEvent::initialize(const FocusEvent& from) {
867 InputEvent::initialize(from);
868 mHasFocus = from.mHasFocus;
869 mInTouchMode = from.mInTouchMode;
870}
Jeff Brown5912f952013-07-01 19:10:31 -0700871
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800872// --- CaptureEvent ---
873
874void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) {
875 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
876 ADISPLAY_ID_NONE, INVALID_HMAC);
877 mPointerCaptureEnabled = pointerCaptureEnabled;
878}
879
880void CaptureEvent::initialize(const CaptureEvent& from) {
881 InputEvent::initialize(from);
882 mPointerCaptureEnabled = from.mPointerCaptureEnabled;
883}
884
arthurhung7632c332020-12-30 16:58:01 +0800885// --- DragEvent ---
886
887void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) {
888 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
889 ADISPLAY_ID_NONE, INVALID_HMAC);
890 mIsExiting = isExiting;
891 mX = x;
892 mY = y;
893}
894
895void DragEvent::initialize(const DragEvent& from) {
896 InputEvent::initialize(from);
897 mIsExiting = from.mIsExiting;
898 mX = from.mX;
899 mY = from.mY;
900}
901
Jeff Brown5912f952013-07-01 19:10:31 -0700902// --- PooledInputEventFactory ---
903
904PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
905 mMaxPoolSize(maxPoolSize) {
906}
907
908PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -0700909}
910
911KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800912 if (mKeyEventPool.empty()) {
913 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700914 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800915 KeyEvent* event = mKeyEventPool.front().release();
916 mKeyEventPool.pop();
917 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700918}
919
920MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800921 if (mMotionEventPool.empty()) {
922 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700923 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800924 MotionEvent* event = mMotionEventPool.front().release();
925 mMotionEventPool.pop();
926 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700927}
928
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800929FocusEvent* PooledInputEventFactory::createFocusEvent() {
930 if (mFocusEventPool.empty()) {
931 return new FocusEvent();
932 }
933 FocusEvent* event = mFocusEventPool.front().release();
934 mFocusEventPool.pop();
935 return event;
936}
937
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800938CaptureEvent* PooledInputEventFactory::createCaptureEvent() {
939 if (mCaptureEventPool.empty()) {
940 return new CaptureEvent();
941 }
942 CaptureEvent* event = mCaptureEventPool.front().release();
943 mCaptureEventPool.pop();
944 return event;
945}
946
arthurhung7632c332020-12-30 16:58:01 +0800947DragEvent* PooledInputEventFactory::createDragEvent() {
948 if (mDragEventPool.empty()) {
949 return new DragEvent();
950 }
951 DragEvent* event = mDragEventPool.front().release();
952 mDragEventPool.pop();
953 return event;
954}
955
Jeff Brown5912f952013-07-01 19:10:31 -0700956void PooledInputEventFactory::recycle(InputEvent* event) {
957 switch (event->getType()) {
958 case AINPUT_EVENT_TYPE_KEY:
959 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800960 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700961 return;
962 }
963 break;
964 case AINPUT_EVENT_TYPE_MOTION:
965 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800966 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700967 return;
968 }
969 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800970 case AINPUT_EVENT_TYPE_FOCUS:
971 if (mFocusEventPool.size() < mMaxPoolSize) {
972 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
973 return;
974 }
975 break;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800976 case AINPUT_EVENT_TYPE_CAPTURE:
977 if (mCaptureEventPool.size() < mMaxPoolSize) {
978 mCaptureEventPool.push(
979 std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event)));
980 return;
981 }
982 break;
arthurhung7632c332020-12-30 16:58:01 +0800983 case AINPUT_EVENT_TYPE_DRAG:
984 if (mDragEventPool.size() < mMaxPoolSize) {
985 mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event)));
986 return;
987 }
988 break;
Jeff Brown5912f952013-07-01 19:10:31 -0700989 }
990 delete event;
991}
992
993} // namespace android