blob: 5f440b77e2d35b76ed322adfe23600e4ff101949 [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.
Prabir Pradhan4bb5ae92021-10-19 11:24:45 -070070 // The return value of atan2f is in range [-pi, pi] which conforms to the orientation API.
71 return atan2f(transformedPoint.x, -transformedPoint.y);
Prabir Pradhan6b384612021-05-14 16:56:25 -070072}
73
Evan Rosky09576692021-07-01 12:22:09 -070074// Rotates the given point to the specified orientation. If the display width and height are
Prabir Pradhan6b384612021-05-14 16:56:25 -070075// provided, the point is rotated in the screen space. Otherwise, the point is rotated about the
76// origin. This helper is used to avoid the extra overhead of creating new Transforms.
Evan Rosky09576692021-07-01 12:22:09 -070077vec2 rotatePoint(uint32_t orientation, float x, float y, int32_t displayWidth = 0,
Prabir Pradhan6b384612021-05-14 16:56:25 -070078 int32_t displayHeight = 0) {
Prabir Pradhan6b384612021-05-14 16:56:25 -070079 if (orientation == ui::Transform::ROT_0) {
80 return {x, y};
81 }
82
83 vec2 xy(x, y);
84 if (orientation == ui::Transform::ROT_90) {
85 xy.x = displayHeight - y;
86 xy.y = x;
87 } else if (orientation == ui::Transform::ROT_180) {
88 xy.x = displayWidth - x;
89 xy.y = displayHeight - y;
90 } else if (orientation == ui::Transform::ROT_270) {
91 xy.x = y;
92 xy.y = displayWidth - x;
93 }
94 return xy;
95}
96
Prabir Pradhan9f388812021-05-13 16:54:53 -070097vec2 applyTransformWithoutTranslation(const ui::Transform& transform, float x, float y) {
98 const vec2 transformedXy = transform.transform(x, y);
99 const vec2 transformedOrigin = transform.transform(0, 0);
100 return transformedXy - transformedOrigin;
101}
102
103bool shouldDisregardWindowTranslation(uint32_t source) {
104 // Pointer events are the only type of events that refer to absolute coordinates on the display,
105 // so we should apply the entire window transform. For other types of events, we should make
106 // sure to not apply the window translation/offset.
107 return (source & AINPUT_SOURCE_CLASS_POINTER) == 0;
108}
109
Prabir Pradhan6b384612021-05-14 16:56:25 -0700110} // namespace
111
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800112const char* motionClassificationToString(MotionClassification classification) {
113 switch (classification) {
114 case MotionClassification::NONE:
115 return "NONE";
116 case MotionClassification::AMBIGUOUS_GESTURE:
117 return "AMBIGUOUS_GESTURE";
118 case MotionClassification::DEEP_PRESS:
119 return "DEEP_PRESS";
120 }
121}
122
Garfield Tan84b087e2020-01-23 10:49:05 -0800123// --- IdGenerator ---
124IdGenerator::IdGenerator(Source source) : mSource(source) {}
125
126int32_t IdGenerator::nextId() const {
127 constexpr uint32_t SEQUENCE_NUMBER_MASK = ~SOURCE_MASK;
128 int32_t id = 0;
129
130// Avoid building against syscall getrandom(2) on host, which will fail build on Mac. Host doesn't
131// use sequence number so just always return mSource.
132#ifdef __ANDROID__
133 constexpr size_t BUF_LEN = sizeof(id);
134 size_t totalBytes = 0;
135 while (totalBytes < BUF_LEN) {
136 ssize_t bytes = TEMP_FAILURE_RETRY(getrandom(&id, BUF_LEN, GRND_NONBLOCK));
137 if (CC_UNLIKELY(bytes < 0)) {
138 ALOGW("Failed to fill in random number for sequence number: %s.", strerror(errno));
139 id = 0;
140 break;
141 }
142 totalBytes += bytes;
143 }
144#endif // __ANDROID__
145
146 return (id & SEQUENCE_NUMBER_MASK) | static_cast<int32_t>(mSource);
147}
148
Jeff Brown5912f952013-07-01 19:10:31 -0700149// --- InputEvent ---
150
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800151const char* inputEventTypeToString(int32_t type) {
152 switch (type) {
153 case AINPUT_EVENT_TYPE_KEY: {
154 return "KEY";
155 }
156 case AINPUT_EVENT_TYPE_MOTION: {
157 return "MOTION";
158 }
159 case AINPUT_EVENT_TYPE_FOCUS: {
160 return "FOCUS";
161 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800162 case AINPUT_EVENT_TYPE_CAPTURE: {
163 return "CAPTURE";
164 }
arthurhung7632c332020-12-30 16:58:01 +0800165 case AINPUT_EVENT_TYPE_DRAG: {
166 return "DRAG";
167 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800168 }
169 return "UNKNOWN";
170}
171
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800172VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
173 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
174 event.getSource(), event.getDisplayId()},
175 event.getAction(),
176 event.getDownTime(),
177 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
178 event.getKeyCode(),
179 event.getScanCode(),
180 event.getMetaState(),
181 event.getRepeatCount()};
182}
183
184VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
185 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
186 event.getSource(), event.getDisplayId()},
187 event.getRawX(0),
188 event.getRawY(0),
189 event.getActionMasked(),
190 event.getDownTime(),
191 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
192 event.getMetaState(),
193 event.getButtonState()};
194}
195
Garfield Tan4cc839f2020-01-24 11:26:14 -0800196void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600197 std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800198 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700199 mDeviceId = deviceId;
200 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100201 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600202 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700203}
204
205void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800206 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700207 mDeviceId = from.mDeviceId;
208 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100209 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600210 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700211}
212
Garfield Tan4cc839f2020-01-24 11:26:14 -0800213int32_t InputEvent::nextId() {
214 static IdGenerator idGen(IdGenerator::Source::OTHER);
215 return idGen.nextId();
216}
217
Jeff Brown5912f952013-07-01 19:10:31 -0700218// --- KeyEvent ---
219
Michael Wright872db4f2014-04-22 15:03:51 -0700220const char* KeyEvent::getLabel(int32_t keyCode) {
Chris Ye4958d062020-08-20 13:21:10 -0700221 return InputEventLookup::getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700222}
223
Michael Wright872db4f2014-04-22 15:03:51 -0700224int32_t KeyEvent::getKeyCodeFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700225 return InputEventLookup::getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700226}
227
Garfield Tan4cc839f2020-01-24 11:26:14 -0800228void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600229 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
230 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
231 nsecs_t downTime, nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800232 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700233 mAction = action;
234 mFlags = flags;
235 mKeyCode = keyCode;
236 mScanCode = scanCode;
237 mMetaState = metaState;
238 mRepeatCount = repeatCount;
239 mDownTime = downTime;
240 mEventTime = eventTime;
241}
242
243void KeyEvent::initialize(const KeyEvent& from) {
244 InputEvent::initialize(from);
245 mAction = from.mAction;
246 mFlags = from.mFlags;
247 mKeyCode = from.mKeyCode;
248 mScanCode = from.mScanCode;
249 mMetaState = from.mMetaState;
250 mRepeatCount = from.mRepeatCount;
251 mDownTime = from.mDownTime;
252 mEventTime = from.mEventTime;
253}
254
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700255const char* KeyEvent::actionToString(int32_t action) {
256 // Convert KeyEvent action to string
257 switch (action) {
258 case AKEY_EVENT_ACTION_DOWN:
259 return "DOWN";
260 case AKEY_EVENT_ACTION_UP:
261 return "UP";
262 case AKEY_EVENT_ACTION_MULTIPLE:
263 return "MULTIPLE";
264 }
265 return "UNKNOWN";
266}
Jeff Brown5912f952013-07-01 19:10:31 -0700267
268// --- PointerCoords ---
269
270float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700271 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700272 return 0;
273 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700274 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700275}
276
277status_t PointerCoords::setAxisValue(int32_t axis, float value) {
278 if (axis < 0 || axis > 63) {
279 return NAME_NOT_FOUND;
280 }
281
Michael Wright38dcdff2014-03-19 12:06:10 -0700282 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
283 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700284 if (value == 0) {
285 return OK; // axes with value 0 do not need to be stored
286 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700287
288 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700289 if (count >= MAX_AXES) {
290 tooManyAxes(axis);
291 return NO_MEMORY;
292 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700293 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700294 for (uint32_t i = count; i > index; i--) {
295 values[i] = values[i - 1];
296 }
297 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700298
Jeff Brown5912f952013-07-01 19:10:31 -0700299 values[index] = value;
300 return OK;
301}
302
303static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
304 float value = c.getAxisValue(axis);
305 if (value != 0) {
306 c.setAxisValue(axis, value * scaleFactor);
307 }
308}
309
Robert Carre07e1032018-11-26 12:55:53 -0800310void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700311 // No need to scale pressure or size since they are normalized.
312 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800313
314 // If there is a global scale factor, it is included in the windowX/YScale
315 // so we don't need to apply it twice to the X/Y axes.
316 // However we don't want to apply any windowXYScale not included in the global scale
317 // to the TOUCH_MAJOR/MINOR coordinates.
318 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
319 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
320 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
321 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
322 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
323 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
Prabir Pradhanc6523582021-05-14 18:02:55 -0700324 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_X, windowXScale);
325 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_Y, windowYScale);
Robert Carre07e1032018-11-26 12:55:53 -0800326}
327
328void PointerCoords::scale(float globalScaleFactor) {
329 scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700330}
331
Jeff Brownf086ddb2014-02-11 14:28:48 -0800332void PointerCoords::applyOffset(float xOffset, float yOffset) {
333 setAxisValue(AMOTION_EVENT_AXIS_X, getX() + xOffset);
334 setAxisValue(AMOTION_EVENT_AXIS_Y, getY() + yOffset);
335}
336
Brett Chabotfaa986c2020-11-04 17:39:36 -0800337#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700338status_t PointerCoords::readFromParcel(Parcel* parcel) {
339 bits = parcel->readInt64();
340
Michael Wright38dcdff2014-03-19 12:06:10 -0700341 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700342 if (count > MAX_AXES) {
343 return BAD_VALUE;
344 }
345
346 for (uint32_t i = 0; i < count; i++) {
347 values[i] = parcel->readFloat();
348 }
349 return OK;
350}
351
352status_t PointerCoords::writeToParcel(Parcel* parcel) const {
353 parcel->writeInt64(bits);
354
Michael Wright38dcdff2014-03-19 12:06:10 -0700355 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700356 for (uint32_t i = 0; i < count; i++) {
357 parcel->writeFloat(values[i]);
358 }
359 return OK;
360}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800361#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700362
363void PointerCoords::tooManyAxes(int axis) {
364 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
365 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
366}
367
368bool PointerCoords::operator==(const PointerCoords& other) const {
369 if (bits != other.bits) {
370 return false;
371 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700372 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700373 for (uint32_t i = 0; i < count; i++) {
374 if (values[i] != other.values[i]) {
375 return false;
376 }
377 }
378 return true;
379}
380
381void PointerCoords::copyFrom(const PointerCoords& other) {
382 bits = other.bits;
Michael Wright38dcdff2014-03-19 12:06:10 -0700383 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700384 for (uint32_t i = 0; i < count; i++) {
385 values[i] = other.values[i];
386 }
387}
388
chaviwc01e1372020-07-01 12:37:31 -0700389void PointerCoords::transform(const ui::Transform& transform) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700390 const vec2 xy = transform.transform(getXYValue());
391 setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
392 setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
393
Prabir Pradhanc6523582021-05-14 18:02:55 -0700394 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_X) ||
395 BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_Y)) {
396 const ui::Transform rotation(transform.getOrientation());
397 const vec2 relativeXy = rotation.transform(getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
398 getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
399 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
400 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
401 }
402
Prabir Pradhan6b384612021-05-14 16:56:25 -0700403 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) {
404 const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
405 setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val));
406 }
chaviwc01e1372020-07-01 12:37:31 -0700407}
Jeff Brown5912f952013-07-01 19:10:31 -0700408
409// --- PointerProperties ---
410
411bool PointerProperties::operator==(const PointerProperties& other) const {
412 return id == other.id
413 && toolType == other.toolType;
414}
415
416void PointerProperties::copyFrom(const PointerProperties& other) {
417 id = other.id;
418 toolType = other.toolType;
419}
420
421
422// --- MotionEvent ---
423
Garfield Tan4cc839f2020-01-24 11:26:14 -0800424void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600425 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
426 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700427 int32_t buttonState, MotionClassification classification,
428 const ui::Transform& transform, float xPrecision, float yPrecision,
Evan Rosky84f07f02021-04-16 10:42:42 -0700429 float rawXCursorPosition, float rawYCursorPosition,
Evan Rosky09576692021-07-01 12:22:09 -0700430 uint32_t displayOrientation, int32_t displayWidth,
431 int32_t displayHeight, nsecs_t downTime, nsecs_t eventTime,
432 size_t pointerCount, const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700433 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800434 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700435 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100436 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700437 mFlags = flags;
438 mEdgeFlags = edgeFlags;
439 mMetaState = metaState;
440 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800441 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700442 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700443 mXPrecision = xPrecision;
444 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700445 mRawXCursorPosition = rawXCursorPosition;
446 mRawYCursorPosition = rawYCursorPosition;
Evan Rosky09576692021-07-01 12:22:09 -0700447 mDisplayOrientation = displayOrientation;
Evan Rosky84f07f02021-04-16 10:42:42 -0700448 mDisplayWidth = displayWidth;
449 mDisplayHeight = displayHeight;
Jeff Brown5912f952013-07-01 19:10:31 -0700450 mDownTime = downTime;
451 mPointerProperties.clear();
452 mPointerProperties.appendArray(pointerProperties, pointerCount);
453 mSampleEventTimes.clear();
454 mSamplePointerCoords.clear();
455 addSample(eventTime, pointerCoords);
456}
457
458void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800459 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
460 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700461 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100462 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700463 mFlags = other->mFlags;
464 mEdgeFlags = other->mEdgeFlags;
465 mMetaState = other->mMetaState;
466 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800467 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700468 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700469 mXPrecision = other->mXPrecision;
470 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700471 mRawXCursorPosition = other->mRawXCursorPosition;
472 mRawYCursorPosition = other->mRawYCursorPosition;
Evan Rosky09576692021-07-01 12:22:09 -0700473 mDisplayOrientation = other->mDisplayOrientation;
Evan Rosky84f07f02021-04-16 10:42:42 -0700474 mDisplayWidth = other->mDisplayWidth;
475 mDisplayHeight = other->mDisplayHeight;
Jeff Brown5912f952013-07-01 19:10:31 -0700476 mDownTime = other->mDownTime;
477 mPointerProperties = other->mPointerProperties;
478
479 if (keepHistory) {
480 mSampleEventTimes = other->mSampleEventTimes;
481 mSamplePointerCoords = other->mSamplePointerCoords;
482 } else {
483 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500484 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700485 mSamplePointerCoords.clear();
486 size_t pointerCount = other->getPointerCount();
487 size_t historySize = other->getHistorySize();
488 mSamplePointerCoords.appendArray(other->mSamplePointerCoords.array()
489 + (historySize * pointerCount), pointerCount);
490 }
491}
492
493void MotionEvent::addSample(
494 int64_t eventTime,
495 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500496 mSampleEventTimes.push_back(eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -0700497 mSamplePointerCoords.appendArray(pointerCoords, getPointerCount());
498}
499
Garfield Tan00f511d2019-06-12 16:55:40 -0700500float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700501 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
502 return vals.x;
Garfield Tan00f511d2019-06-12 16:55:40 -0700503}
504
505float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700506 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
507 return vals.y;
Garfield Tan00f511d2019-06-12 16:55:40 -0700508}
509
Garfield Tan937bb832019-07-25 17:48:31 -0700510void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700511 ui::Transform inverse = mTransform.inverse();
512 vec2 vals = inverse.transform(x, y);
513 mRawXCursorPosition = vals.x;
514 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700515}
516
Jeff Brown5912f952013-07-01 19:10:31 -0700517const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
518 return &mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
519}
520
521float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
Evan Rosky84f07f02021-04-16 10:42:42 -0700522 return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700523}
524
525float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700526 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700527}
528
529const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
530 size_t pointerIndex, size_t historicalIndex) const {
531 return &mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
532}
533
534float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan6b384612021-05-14 16:56:25 -0700535 size_t historicalIndex) const {
536 const PointerCoords* coords = getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
537
Prabir Pradhan7e1443f2021-07-23 21:01:55 +0000538 if (!isPerWindowInputRotationEnabled()) return coords->getAxisValue(axis);
539
Prabir Pradhan6b384612021-05-14 16:56:25 -0700540 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
541 // For compatibility, convert raw coordinates into "oriented screen space". Once app
542 // developers are educated about getRaw, we can consider removing this.
Prabir Pradhan9f388812021-05-13 16:54:53 -0700543 const vec2 xy = shouldDisregardWindowTranslation(mSource)
Evan Rosky09576692021-07-01 12:22:09 -0700544 ? rotatePoint(mDisplayOrientation, coords->getX(), coords->getY())
545 : rotatePoint(mDisplayOrientation, coords->getX(), coords->getY(), mDisplayWidth,
Prabir Pradhan9f388812021-05-13 16:54:53 -0700546 mDisplayHeight);
Prabir Pradhan6b384612021-05-14 16:56:25 -0700547 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
548 return xy[axis];
Evan Rosky84f07f02021-04-16 10:42:42 -0700549 }
550
Prabir Pradhanc6523582021-05-14 18:02:55 -0700551 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
552 // For compatibility, since we convert raw coordinates into "oriented screen space", we
553 // need to convert the relative axes into the same orientation for consistency.
Evan Rosky09576692021-07-01 12:22:09 -0700554 const vec2 relativeXy = rotatePoint(mDisplayOrientation,
555 coords->getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
556 coords->getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
Prabir Pradhanc6523582021-05-14 18:02:55 -0700557 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
558 }
559
Prabir Pradhan6b384612021-05-14 16:56:25 -0700560 return coords->getAxisValue(axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700561}
562
563float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan9f388812021-05-13 16:54:53 -0700564 size_t historicalIndex) const {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700565 const PointerCoords* coords = getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
566
567 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
Prabir Pradhan9f388812021-05-13 16:54:53 -0700568 const vec2 xy = shouldDisregardWindowTranslation(mSource)
569 ? applyTransformWithoutTranslation(mTransform, coords->getX(), coords->getY())
570 : mTransform.transform(coords->getXYValue());
Prabir Pradhan6b384612021-05-14 16:56:25 -0700571 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
572 return xy[axis];
chaviw9eaa22c2020-07-01 16:21:27 -0700573 }
574
Prabir Pradhanc6523582021-05-14 18:02:55 -0700575 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
576 const vec2 relativeXy =
577 applyTransformWithoutTranslation(mTransform,
578 coords->getAxisValue(
579 AMOTION_EVENT_AXIS_RELATIVE_X),
580 coords->getAxisValue(
581 AMOTION_EVENT_AXIS_RELATIVE_Y));
582 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
583 }
584
Prabir Pradhan6b384612021-05-14 16:56:25 -0700585 return coords->getAxisValue(axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700586}
587
588ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
589 size_t pointerCount = mPointerProperties.size();
590 for (size_t i = 0; i < pointerCount; i++) {
591 if (mPointerProperties.itemAt(i).id == pointerId) {
592 return i;
593 }
594 }
595 return -1;
596}
597
598void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700599 float currXOffset = mTransform.tx();
600 float currYOffset = mTransform.ty();
601 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700602}
603
Robert Carre07e1032018-11-26 12:55:53 -0800604void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700605 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800606 mXPrecision *= globalScaleFactor;
607 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700608
609 size_t numSamples = mSamplePointerCoords.size();
610 for (size_t i = 0; i < numSamples; i++) {
chaviw9eaa22c2020-07-01 16:21:27 -0700611 mSamplePointerCoords.editItemAt(i).scale(globalScaleFactor, globalScaleFactor,
612 globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700613 }
614}
615
chaviw9eaa22c2020-07-01 16:21:27 -0700616void MotionEvent::transform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700617 // We want to preserve the raw axes values stored in the PointerCoords, so we just update the
618 // transform using the values passed in.
chaviw9eaa22c2020-07-01 16:21:27 -0700619 ui::Transform newTransform;
620 newTransform.set(matrix);
621 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700622
Prabir Pradhan6b384612021-05-14 16:56:25 -0700623 // We need to update the AXIS_ORIENTATION value here to maintain the old behavior where the
624 // orientation angle is not affected by the initial transformation set in the MotionEvent.
625 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
626 [&newTransform](PointerCoords& c) {
627 float orientation = c.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
628 c.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
629 transformAngle(newTransform, orientation));
630 });
Jeff Brown5912f952013-07-01 19:10:31 -0700631}
632
Evan Roskyd4d4d802021-05-03 20:12:21 -0700633void MotionEvent::applyTransform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700634 ui::Transform transform;
635 transform.set(matrix);
Evan Roskyd4d4d802021-05-03 20:12:21 -0700636
637 // Apply the transformation to all samples.
Prabir Pradhan6b384612021-05-14 16:56:25 -0700638 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
639 [&transform](PointerCoords& c) { c.transform(transform); });
Prabir Pradhan4b19bd02021-06-01 17:34:59 -0700640
641 if (mRawXCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION &&
642 mRawYCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) {
643 const vec2 cursor = transform.transform(mRawXCursorPosition, mRawYCursorPosition);
644 mRawXCursorPosition = cursor.x;
645 mRawYCursorPosition = cursor.y;
646 }
Evan Roskyd4d4d802021-05-03 20:12:21 -0700647}
648
Brett Chabotfaa986c2020-11-04 17:39:36 -0800649#ifdef __linux__
chaviw9eaa22c2020-07-01 16:21:27 -0700650static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
651 float dsdx, dtdx, tx, dtdy, dsdy, ty;
652 status_t status = parcel.readFloat(&dsdx);
653 status |= parcel.readFloat(&dtdx);
654 status |= parcel.readFloat(&tx);
655 status |= parcel.readFloat(&dtdy);
656 status |= parcel.readFloat(&dsdy);
657 status |= parcel.readFloat(&ty);
658
659 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
660 return status;
661}
662
663static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
664 status_t status = parcel.writeFloat(transform.dsdx());
665 status |= parcel.writeFloat(transform.dtdx());
666 status |= parcel.writeFloat(transform.tx());
667 status |= parcel.writeFloat(transform.dtdy());
668 status |= parcel.writeFloat(transform.dsdy());
669 status |= parcel.writeFloat(transform.ty());
670 return status;
671}
672
Jeff Brown5912f952013-07-01 19:10:31 -0700673status_t MotionEvent::readFromParcel(Parcel* parcel) {
674 size_t pointerCount = parcel->readInt32();
675 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800676 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
677 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700678 return BAD_VALUE;
679 }
680
Garfield Tan4cc839f2020-01-24 11:26:14 -0800681 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700682 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600683 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800684 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600685 std::vector<uint8_t> hmac;
686 status_t result = parcel->readByteVector(&hmac);
687 if (result != OK || hmac.size() != 32) {
688 return BAD_VALUE;
689 }
690 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700691 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100692 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700693 mFlags = parcel->readInt32();
694 mEdgeFlags = parcel->readInt32();
695 mMetaState = parcel->readInt32();
696 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800697 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700698
699 result = android::readFromParcel(mTransform, *parcel);
700 if (result != OK) {
701 return result;
702 }
Jeff Brown5912f952013-07-01 19:10:31 -0700703 mXPrecision = parcel->readFloat();
704 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700705 mRawXCursorPosition = parcel->readFloat();
706 mRawYCursorPosition = parcel->readFloat();
Evan Rosky09576692021-07-01 12:22:09 -0700707 mDisplayOrientation = parcel->readUint32();
Evan Rosky84f07f02021-04-16 10:42:42 -0700708 mDisplayWidth = parcel->readInt32();
709 mDisplayHeight = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700710 mDownTime = parcel->readInt64();
711
712 mPointerProperties.clear();
713 mPointerProperties.setCapacity(pointerCount);
714 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500715 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700716 mSamplePointerCoords.clear();
717 mSamplePointerCoords.setCapacity(sampleCount * pointerCount);
718
719 for (size_t i = 0; i < pointerCount; i++) {
720 mPointerProperties.push();
721 PointerProperties& properties = mPointerProperties.editTop();
722 properties.id = parcel->readInt32();
723 properties.toolType = parcel->readInt32();
724 }
725
Dan Austinc94fc452015-09-22 14:22:41 -0700726 while (sampleCount > 0) {
727 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500728 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700729 for (size_t i = 0; i < pointerCount; i++) {
730 mSamplePointerCoords.push();
731 status_t status = mSamplePointerCoords.editTop().readFromParcel(parcel);
732 if (status) {
733 return status;
734 }
735 }
736 }
737 return OK;
738}
739
740status_t MotionEvent::writeToParcel(Parcel* parcel) const {
741 size_t pointerCount = mPointerProperties.size();
742 size_t sampleCount = mSampleEventTimes.size();
743
744 parcel->writeInt32(pointerCount);
745 parcel->writeInt32(sampleCount);
746
Garfield Tan4cc839f2020-01-24 11:26:14 -0800747 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700748 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600749 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800750 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600751 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
752 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700753 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100754 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700755 parcel->writeInt32(mFlags);
756 parcel->writeInt32(mEdgeFlags);
757 parcel->writeInt32(mMetaState);
758 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800759 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700760
761 status_t result = android::writeToParcel(mTransform, *parcel);
762 if (result != OK) {
763 return result;
764 }
Jeff Brown5912f952013-07-01 19:10:31 -0700765 parcel->writeFloat(mXPrecision);
766 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700767 parcel->writeFloat(mRawXCursorPosition);
768 parcel->writeFloat(mRawYCursorPosition);
Evan Rosky09576692021-07-01 12:22:09 -0700769 parcel->writeUint32(mDisplayOrientation);
Evan Rosky84f07f02021-04-16 10:42:42 -0700770 parcel->writeInt32(mDisplayWidth);
771 parcel->writeInt32(mDisplayHeight);
Jeff Brown5912f952013-07-01 19:10:31 -0700772 parcel->writeInt64(mDownTime);
773
774 for (size_t i = 0; i < pointerCount; i++) {
775 const PointerProperties& properties = mPointerProperties.itemAt(i);
776 parcel->writeInt32(properties.id);
777 parcel->writeInt32(properties.toolType);
778 }
779
780 const PointerCoords* pc = mSamplePointerCoords.array();
781 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500782 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700783 for (size_t i = 0; i < pointerCount; i++) {
784 status_t status = (pc++)->writeToParcel(parcel);
785 if (status) {
786 return status;
787 }
788 }
789 }
790 return OK;
791}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800792#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700793
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600794bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Jeff Brown5912f952013-07-01 19:10:31 -0700795 if (source & AINPUT_SOURCE_CLASS_POINTER) {
796 // Specifically excludes HOVER_MOVE and SCROLL.
797 switch (action & AMOTION_EVENT_ACTION_MASK) {
798 case AMOTION_EVENT_ACTION_DOWN:
799 case AMOTION_EVENT_ACTION_MOVE:
800 case AMOTION_EVENT_ACTION_UP:
801 case AMOTION_EVENT_ACTION_POINTER_DOWN:
802 case AMOTION_EVENT_ACTION_POINTER_UP:
803 case AMOTION_EVENT_ACTION_CANCEL:
804 case AMOTION_EVENT_ACTION_OUTSIDE:
805 return true;
806 }
807 }
808 return false;
809}
810
Michael Wright872db4f2014-04-22 15:03:51 -0700811const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700812 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700813}
814
815int32_t MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700816 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700817}
818
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500819std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700820 // Convert MotionEvent action to string
821 switch (action & AMOTION_EVENT_ACTION_MASK) {
822 case AMOTION_EVENT_ACTION_DOWN:
823 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700824 case AMOTION_EVENT_ACTION_UP:
825 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500826 case AMOTION_EVENT_ACTION_MOVE:
827 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700828 case AMOTION_EVENT_ACTION_CANCEL:
829 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500830 case AMOTION_EVENT_ACTION_OUTSIDE:
831 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700832 case AMOTION_EVENT_ACTION_POINTER_DOWN:
833 return "POINTER_DOWN";
834 case AMOTION_EVENT_ACTION_POINTER_UP:
835 return "POINTER_UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500836 case AMOTION_EVENT_ACTION_HOVER_MOVE:
837 return "HOVER_MOVE";
838 case AMOTION_EVENT_ACTION_SCROLL:
839 return "SCROLL";
840 case AMOTION_EVENT_ACTION_HOVER_ENTER:
841 return "HOVER_ENTER";
842 case AMOTION_EVENT_ACTION_HOVER_EXIT:
843 return "HOVER_EXIT";
844 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
845 return "BUTTON_PRESS";
846 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
847 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700848 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500849 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700850}
851
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800852// --- FocusEvent ---
853
Garfield Tan4cc839f2020-01-24 11:26:14 -0800854void FocusEvent::initialize(int32_t id, bool hasFocus, bool inTouchMode) {
855 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600856 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800857 mHasFocus = hasFocus;
858 mInTouchMode = inTouchMode;
859}
860
861void FocusEvent::initialize(const FocusEvent& from) {
862 InputEvent::initialize(from);
863 mHasFocus = from.mHasFocus;
864 mInTouchMode = from.mInTouchMode;
865}
Jeff Brown5912f952013-07-01 19:10:31 -0700866
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800867// --- CaptureEvent ---
868
869void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) {
870 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
871 ADISPLAY_ID_NONE, INVALID_HMAC);
872 mPointerCaptureEnabled = pointerCaptureEnabled;
873}
874
875void CaptureEvent::initialize(const CaptureEvent& from) {
876 InputEvent::initialize(from);
877 mPointerCaptureEnabled = from.mPointerCaptureEnabled;
878}
879
arthurhung7632c332020-12-30 16:58:01 +0800880// --- DragEvent ---
881
882void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) {
883 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
884 ADISPLAY_ID_NONE, INVALID_HMAC);
885 mIsExiting = isExiting;
886 mX = x;
887 mY = y;
888}
889
890void DragEvent::initialize(const DragEvent& from) {
891 InputEvent::initialize(from);
892 mIsExiting = from.mIsExiting;
893 mX = from.mX;
894 mY = from.mY;
895}
896
Jeff Brown5912f952013-07-01 19:10:31 -0700897// --- PooledInputEventFactory ---
898
899PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
900 mMaxPoolSize(maxPoolSize) {
901}
902
903PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -0700904}
905
906KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800907 if (mKeyEventPool.empty()) {
908 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700909 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800910 KeyEvent* event = mKeyEventPool.front().release();
911 mKeyEventPool.pop();
912 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700913}
914
915MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800916 if (mMotionEventPool.empty()) {
917 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700918 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800919 MotionEvent* event = mMotionEventPool.front().release();
920 mMotionEventPool.pop();
921 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700922}
923
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800924FocusEvent* PooledInputEventFactory::createFocusEvent() {
925 if (mFocusEventPool.empty()) {
926 return new FocusEvent();
927 }
928 FocusEvent* event = mFocusEventPool.front().release();
929 mFocusEventPool.pop();
930 return event;
931}
932
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800933CaptureEvent* PooledInputEventFactory::createCaptureEvent() {
934 if (mCaptureEventPool.empty()) {
935 return new CaptureEvent();
936 }
937 CaptureEvent* event = mCaptureEventPool.front().release();
938 mCaptureEventPool.pop();
939 return event;
940}
941
arthurhung7632c332020-12-30 16:58:01 +0800942DragEvent* PooledInputEventFactory::createDragEvent() {
943 if (mDragEventPool.empty()) {
944 return new DragEvent();
945 }
946 DragEvent* event = mDragEventPool.front().release();
947 mDragEventPool.pop();
948 return event;
949}
950
Jeff Brown5912f952013-07-01 19:10:31 -0700951void PooledInputEventFactory::recycle(InputEvent* event) {
952 switch (event->getType()) {
953 case AINPUT_EVENT_TYPE_KEY:
954 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800955 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700956 return;
957 }
958 break;
959 case AINPUT_EVENT_TYPE_MOTION:
960 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800961 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700962 return;
963 }
964 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800965 case AINPUT_EVENT_TYPE_FOCUS:
966 if (mFocusEventPool.size() < mMaxPoolSize) {
967 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
968 return;
969 }
970 break;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800971 case AINPUT_EVENT_TYPE_CAPTURE:
972 if (mCaptureEventPool.size() < mMaxPoolSize) {
973 mCaptureEventPool.push(
974 std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event)));
975 return;
976 }
977 break;
arthurhung7632c332020-12-30 16:58:01 +0800978 case AINPUT_EVENT_TYPE_DRAG:
979 if (mDragEventPool.size() < mMaxPoolSize) {
980 mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event)));
981 return;
982 }
983 break;
Jeff Brown5912f952013-07-01 19:10:31 -0700984 }
985 delete event;
986}
987
988} // namespace android