blob: 037849eac4fc9aec69e0ed454743c453a360debf [file] [log] [blame]
Jeff Brown5912f952013-07-01 19:10:31 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Input"
18//#define LOG_NDEBUG 0
19
chaviw09c8d2d2020-08-24 15:48:26 -070020#include <attestation/HmacKeyManager.h>
Garfield Tan84b087e2020-01-23 10:49:05 -080021#include <cutils/compiler.h>
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050022#include <inttypes.h>
Jeff Brown5912f952013-07-01 19:10:31 -070023#include <limits.h>
Garfield Tan84b087e2020-01-23 10:49:05 -080024#include <string.h>
Jeff Brown5912f952013-07-01 19:10:31 -070025
Prabir Pradhan7e1443f2021-07-23 21:01:55 +000026#include <android-base/properties.h>
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050027#include <android-base/stringprintf.h>
chaviw98318de2021-05-19 16:45:23 -050028#include <gui/constants.h>
Jeff Brown5912f952013-07-01 19:10:31 -070029#include <input/Input.h>
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080030#include <input/InputDevice.h>
Michael Wright872db4f2014-04-22 15:03:51 -070031#include <input/InputEventLabels.h>
Jeff Brown5912f952013-07-01 19:10:31 -070032
Brett Chabotfaa986c2020-11-04 17:39:36 -080033#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -070034#include <binder/Parcel.h>
Brett Chabotfaa986c2020-11-04 17:39:36 -080035#endif
Brett Chabot58208522020-09-09 13:55:24 -070036#ifdef __ANDROID__
Garfield Tan84b087e2020-01-23 10:49:05 -080037#include <sys/random.h>
Jeff Brown5912f952013-07-01 19:10:31 -070038#endif
39
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050040using android::base::StringPrintf;
41
Jeff Brown5912f952013-07-01 19:10:31 -070042namespace android {
43
Prabir Pradhan6b384612021-05-14 16:56:25 -070044namespace {
45
Prabir Pradhan7e1443f2021-07-23 21:01:55 +000046// When per-window-input-rotation is enabled, InputFlinger works in the un-rotated display
47// coordinates and SurfaceFlinger includes the display rotation in the input window transforms.
48bool isPerWindowInputRotationEnabled() {
49 static const bool PER_WINDOW_INPUT_ROTATION =
Vadim Tryshev7719c7d2021-08-27 17:28:43 +000050 base::GetBoolProperty("persist.debug.per_window_input_rotation", false);
Prabir Pradhan7e1443f2021-07-23 21:01:55 +000051
52 return PER_WINDOW_INPUT_ROTATION;
53}
54
Prabir Pradhan6b384612021-05-14 16:56:25 -070055float transformAngle(const ui::Transform& transform, float angleRadians) {
56 // Construct and transform a vector oriented at the specified clockwise angle from vertical.
57 // Coordinate system: down is increasing Y, right is increasing X.
58 float x = sinf(angleRadians);
59 float y = -cosf(angleRadians);
60 vec2 transformedPoint = transform.transform(x, y);
61
62 // Determine how the origin is transformed by the matrix so that we
63 // can transform orientation vectors.
64 const vec2 origin = transform.transform(0, 0);
65
66 transformedPoint.x -= origin.x;
67 transformedPoint.y -= origin.y;
68
69 // Derive the transformed vector's clockwise angle from vertical.
70 float result = atan2f(transformedPoint.x, -transformedPoint.y);
71 if (result < -M_PI_2) {
72 result += M_PI;
73 } else if (result > M_PI_2) {
74 result -= M_PI;
75 }
76 return result;
77}
78
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 }
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700173 case AINPUT_EVENT_TYPE_TOUCH_MODE: {
174 return "TOUCH_MODE";
175 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800176 }
177 return "UNKNOWN";
178}
179
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800180VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
181 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
182 event.getSource(), event.getDisplayId()},
183 event.getAction(),
184 event.getDownTime(),
185 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
186 event.getKeyCode(),
187 event.getScanCode(),
188 event.getMetaState(),
189 event.getRepeatCount()};
190}
191
192VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
193 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
194 event.getSource(), event.getDisplayId()},
195 event.getRawX(0),
196 event.getRawY(0),
197 event.getActionMasked(),
198 event.getDownTime(),
199 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
200 event.getMetaState(),
201 event.getButtonState()};
202}
203
Garfield Tan4cc839f2020-01-24 11:26:14 -0800204void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600205 std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800206 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700207 mDeviceId = deviceId;
208 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100209 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600210 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700211}
212
213void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800214 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700215 mDeviceId = from.mDeviceId;
216 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100217 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600218 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700219}
220
Garfield Tan4cc839f2020-01-24 11:26:14 -0800221int32_t InputEvent::nextId() {
222 static IdGenerator idGen(IdGenerator::Source::OTHER);
223 return idGen.nextId();
224}
225
Jeff Brown5912f952013-07-01 19:10:31 -0700226// --- KeyEvent ---
227
Michael Wright872db4f2014-04-22 15:03:51 -0700228const char* KeyEvent::getLabel(int32_t keyCode) {
Chris Ye4958d062020-08-20 13:21:10 -0700229 return InputEventLookup::getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700230}
231
Michael Wright872db4f2014-04-22 15:03:51 -0700232int32_t KeyEvent::getKeyCodeFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700233 return InputEventLookup::getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700234}
235
Garfield Tan4cc839f2020-01-24 11:26:14 -0800236void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600237 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
238 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
239 nsecs_t downTime, nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800240 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700241 mAction = action;
242 mFlags = flags;
243 mKeyCode = keyCode;
244 mScanCode = scanCode;
245 mMetaState = metaState;
246 mRepeatCount = repeatCount;
247 mDownTime = downTime;
248 mEventTime = eventTime;
249}
250
251void KeyEvent::initialize(const KeyEvent& from) {
252 InputEvent::initialize(from);
253 mAction = from.mAction;
254 mFlags = from.mFlags;
255 mKeyCode = from.mKeyCode;
256 mScanCode = from.mScanCode;
257 mMetaState = from.mMetaState;
258 mRepeatCount = from.mRepeatCount;
259 mDownTime = from.mDownTime;
260 mEventTime = from.mEventTime;
261}
262
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700263const char* KeyEvent::actionToString(int32_t action) {
264 // Convert KeyEvent action to string
265 switch (action) {
266 case AKEY_EVENT_ACTION_DOWN:
267 return "DOWN";
268 case AKEY_EVENT_ACTION_UP:
269 return "UP";
270 case AKEY_EVENT_ACTION_MULTIPLE:
271 return "MULTIPLE";
272 }
273 return "UNKNOWN";
274}
Jeff Brown5912f952013-07-01 19:10:31 -0700275
276// --- PointerCoords ---
277
278float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700279 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700280 return 0;
281 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700282 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700283}
284
285status_t PointerCoords::setAxisValue(int32_t axis, float value) {
286 if (axis < 0 || axis > 63) {
287 return NAME_NOT_FOUND;
288 }
289
Michael Wright38dcdff2014-03-19 12:06:10 -0700290 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
291 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700292 if (value == 0) {
293 return OK; // axes with value 0 do not need to be stored
294 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700295
296 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700297 if (count >= MAX_AXES) {
298 tooManyAxes(axis);
299 return NO_MEMORY;
300 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700301 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700302 for (uint32_t i = count; i > index; i--) {
303 values[i] = values[i - 1];
304 }
305 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700306
Jeff Brown5912f952013-07-01 19:10:31 -0700307 values[index] = value;
308 return OK;
309}
310
311static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
312 float value = c.getAxisValue(axis);
313 if (value != 0) {
314 c.setAxisValue(axis, value * scaleFactor);
315 }
316}
317
Robert Carre07e1032018-11-26 12:55:53 -0800318void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700319 // No need to scale pressure or size since they are normalized.
320 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800321
322 // If there is a global scale factor, it is included in the windowX/YScale
323 // so we don't need to apply it twice to the X/Y axes.
324 // However we don't want to apply any windowXYScale not included in the global scale
325 // to the TOUCH_MAJOR/MINOR coordinates.
326 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
327 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
328 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
329 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
330 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
331 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
Prabir Pradhanc6523582021-05-14 18:02:55 -0700332 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_X, windowXScale);
333 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_Y, windowYScale);
Robert Carre07e1032018-11-26 12:55:53 -0800334}
335
Jeff Brownf086ddb2014-02-11 14:28:48 -0800336void PointerCoords::applyOffset(float xOffset, float yOffset) {
337 setAxisValue(AMOTION_EVENT_AXIS_X, getX() + xOffset);
338 setAxisValue(AMOTION_EVENT_AXIS_Y, getY() + yOffset);
339}
340
Brett Chabotfaa986c2020-11-04 17:39:36 -0800341#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700342status_t PointerCoords::readFromParcel(Parcel* parcel) {
343 bits = parcel->readInt64();
344
Michael Wright38dcdff2014-03-19 12:06:10 -0700345 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700346 if (count > MAX_AXES) {
347 return BAD_VALUE;
348 }
349
350 for (uint32_t i = 0; i < count; i++) {
351 values[i] = parcel->readFloat();
352 }
353 return OK;
354}
355
356status_t PointerCoords::writeToParcel(Parcel* parcel) const {
357 parcel->writeInt64(bits);
358
Michael Wright38dcdff2014-03-19 12:06:10 -0700359 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700360 for (uint32_t i = 0; i < count; i++) {
361 parcel->writeFloat(values[i]);
362 }
363 return OK;
364}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800365#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700366
367void PointerCoords::tooManyAxes(int axis) {
368 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
369 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
370}
371
372bool PointerCoords::operator==(const PointerCoords& other) const {
373 if (bits != other.bits) {
374 return false;
375 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700376 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700377 for (uint32_t i = 0; i < count; i++) {
378 if (values[i] != other.values[i]) {
379 return false;
380 }
381 }
382 return true;
383}
384
385void PointerCoords::copyFrom(const PointerCoords& other) {
386 bits = other.bits;
Michael Wright38dcdff2014-03-19 12:06:10 -0700387 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700388 for (uint32_t i = 0; i < count; i++) {
389 values[i] = other.values[i];
390 }
391}
392
chaviwc01e1372020-07-01 12:37:31 -0700393void PointerCoords::transform(const ui::Transform& transform) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700394 const vec2 xy = transform.transform(getXYValue());
395 setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
396 setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
397
Prabir Pradhanc6523582021-05-14 18:02:55 -0700398 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_X) ||
399 BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_Y)) {
400 const ui::Transform rotation(transform.getOrientation());
401 const vec2 relativeXy = rotation.transform(getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
402 getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
403 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
404 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
405 }
406
Prabir Pradhan6b384612021-05-14 16:56:25 -0700407 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) {
408 const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
409 setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val));
410 }
chaviwc01e1372020-07-01 12:37:31 -0700411}
Jeff Brown5912f952013-07-01 19:10:31 -0700412
413// --- PointerProperties ---
414
415bool PointerProperties::operator==(const PointerProperties& other) const {
416 return id == other.id
417 && toolType == other.toolType;
418}
419
420void PointerProperties::copyFrom(const PointerProperties& other) {
421 id = other.id;
422 toolType = other.toolType;
423}
424
425
426// --- MotionEvent ---
427
Garfield Tan4cc839f2020-01-24 11:26:14 -0800428void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600429 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
430 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700431 int32_t buttonState, MotionClassification classification,
432 const ui::Transform& transform, float xPrecision, float yPrecision,
Evan Rosky84f07f02021-04-16 10:42:42 -0700433 float rawXCursorPosition, float rawYCursorPosition,
Evan Rosky09576692021-07-01 12:22:09 -0700434 uint32_t displayOrientation, int32_t displayWidth,
435 int32_t displayHeight, nsecs_t downTime, nsecs_t eventTime,
436 size_t pointerCount, const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700437 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800438 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700439 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100440 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700441 mFlags = flags;
442 mEdgeFlags = edgeFlags;
443 mMetaState = metaState;
444 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800445 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700446 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700447 mXPrecision = xPrecision;
448 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700449 mRawXCursorPosition = rawXCursorPosition;
450 mRawYCursorPosition = rawYCursorPosition;
Evan Rosky09576692021-07-01 12:22:09 -0700451 mDisplayOrientation = displayOrientation;
Evan Rosky84f07f02021-04-16 10:42:42 -0700452 mDisplayWidth = displayWidth;
453 mDisplayHeight = displayHeight;
Jeff Brown5912f952013-07-01 19:10:31 -0700454 mDownTime = downTime;
455 mPointerProperties.clear();
456 mPointerProperties.appendArray(pointerProperties, pointerCount);
457 mSampleEventTimes.clear();
458 mSamplePointerCoords.clear();
459 addSample(eventTime, pointerCoords);
460}
461
462void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800463 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
464 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700465 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100466 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700467 mFlags = other->mFlags;
468 mEdgeFlags = other->mEdgeFlags;
469 mMetaState = other->mMetaState;
470 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800471 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700472 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700473 mXPrecision = other->mXPrecision;
474 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700475 mRawXCursorPosition = other->mRawXCursorPosition;
476 mRawYCursorPosition = other->mRawYCursorPosition;
Evan Rosky09576692021-07-01 12:22:09 -0700477 mDisplayOrientation = other->mDisplayOrientation;
Evan Rosky84f07f02021-04-16 10:42:42 -0700478 mDisplayWidth = other->mDisplayWidth;
479 mDisplayHeight = other->mDisplayHeight;
Jeff Brown5912f952013-07-01 19:10:31 -0700480 mDownTime = other->mDownTime;
481 mPointerProperties = other->mPointerProperties;
482
483 if (keepHistory) {
484 mSampleEventTimes = other->mSampleEventTimes;
485 mSamplePointerCoords = other->mSamplePointerCoords;
486 } else {
487 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500488 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700489 mSamplePointerCoords.clear();
490 size_t pointerCount = other->getPointerCount();
491 size_t historySize = other->getHistorySize();
492 mSamplePointerCoords.appendArray(other->mSamplePointerCoords.array()
493 + (historySize * pointerCount), pointerCount);
494 }
495}
496
497void MotionEvent::addSample(
498 int64_t eventTime,
499 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500500 mSampleEventTimes.push_back(eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -0700501 mSamplePointerCoords.appendArray(pointerCoords, getPointerCount());
502}
503
Garfield Tan00f511d2019-06-12 16:55:40 -0700504float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700505 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
506 return vals.x;
Garfield Tan00f511d2019-06-12 16:55:40 -0700507}
508
509float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700510 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
511 return vals.y;
Garfield Tan00f511d2019-06-12 16:55:40 -0700512}
513
Garfield Tan937bb832019-07-25 17:48:31 -0700514void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700515 ui::Transform inverse = mTransform.inverse();
516 vec2 vals = inverse.transform(x, y);
517 mRawXCursorPosition = vals.x;
518 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700519}
520
Jeff Brown5912f952013-07-01 19:10:31 -0700521const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
522 return &mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
523}
524
525float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
Evan Rosky84f07f02021-04-16 10:42:42 -0700526 return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700527}
528
529float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700530 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700531}
532
533const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
534 size_t pointerIndex, size_t historicalIndex) const {
535 return &mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
536}
537
538float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan6b384612021-05-14 16:56:25 -0700539 size_t historicalIndex) const {
540 const PointerCoords* coords = getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
541
Prabir Pradhan7e1443f2021-07-23 21:01:55 +0000542 if (!isPerWindowInputRotationEnabled()) return coords->getAxisValue(axis);
543
Prabir Pradhan6b384612021-05-14 16:56:25 -0700544 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
545 // For compatibility, convert raw coordinates into "oriented screen space". Once app
546 // developers are educated about getRaw, we can consider removing this.
Prabir Pradhan9f388812021-05-13 16:54:53 -0700547 const vec2 xy = shouldDisregardWindowTranslation(mSource)
Evan Rosky09576692021-07-01 12:22:09 -0700548 ? rotatePoint(mDisplayOrientation, coords->getX(), coords->getY())
549 : rotatePoint(mDisplayOrientation, coords->getX(), coords->getY(), mDisplayWidth,
Prabir Pradhan9f388812021-05-13 16:54:53 -0700550 mDisplayHeight);
Prabir Pradhan6b384612021-05-14 16:56:25 -0700551 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
552 return xy[axis];
Evan Rosky84f07f02021-04-16 10:42:42 -0700553 }
554
Prabir Pradhanc6523582021-05-14 18:02:55 -0700555 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
556 // For compatibility, since we convert raw coordinates into "oriented screen space", we
557 // need to convert the relative axes into the same orientation for consistency.
Evan Rosky09576692021-07-01 12:22:09 -0700558 const vec2 relativeXy = rotatePoint(mDisplayOrientation,
559 coords->getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
560 coords->getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
Prabir Pradhanc6523582021-05-14 18:02:55 -0700561 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
562 }
563
Prabir Pradhan6b384612021-05-14 16:56:25 -0700564 return coords->getAxisValue(axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700565}
566
567float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan9f388812021-05-13 16:54:53 -0700568 size_t historicalIndex) const {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700569 const PointerCoords* coords = getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
570
571 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
Prabir Pradhan9f388812021-05-13 16:54:53 -0700572 const vec2 xy = shouldDisregardWindowTranslation(mSource)
573 ? applyTransformWithoutTranslation(mTransform, coords->getX(), coords->getY())
574 : mTransform.transform(coords->getXYValue());
Prabir Pradhan6b384612021-05-14 16:56:25 -0700575 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
576 return xy[axis];
chaviw9eaa22c2020-07-01 16:21:27 -0700577 }
578
Prabir Pradhanc6523582021-05-14 18:02:55 -0700579 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
580 const vec2 relativeXy =
581 applyTransformWithoutTranslation(mTransform,
582 coords->getAxisValue(
583 AMOTION_EVENT_AXIS_RELATIVE_X),
584 coords->getAxisValue(
585 AMOTION_EVENT_AXIS_RELATIVE_Y));
586 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
587 }
588
Prabir Pradhan6b384612021-05-14 16:56:25 -0700589 return coords->getAxisValue(axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700590}
591
592ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
593 size_t pointerCount = mPointerProperties.size();
594 for (size_t i = 0; i < pointerCount; i++) {
595 if (mPointerProperties.itemAt(i).id == pointerId) {
596 return i;
597 }
598 }
599 return -1;
600}
601
602void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700603 float currXOffset = mTransform.tx();
604 float currYOffset = mTransform.ty();
605 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700606}
607
Robert Carre07e1032018-11-26 12:55:53 -0800608void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700609 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800610 mXPrecision *= globalScaleFactor;
611 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700612
613 size_t numSamples = mSamplePointerCoords.size();
614 for (size_t i = 0; i < numSamples; i++) {
chaviw9eaa22c2020-07-01 16:21:27 -0700615 mSamplePointerCoords.editItemAt(i).scale(globalScaleFactor, globalScaleFactor,
616 globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700617 }
618}
619
chaviw9eaa22c2020-07-01 16:21:27 -0700620void MotionEvent::transform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700621 // We want to preserve the raw axes values stored in the PointerCoords, so we just update the
622 // transform using the values passed in.
chaviw9eaa22c2020-07-01 16:21:27 -0700623 ui::Transform newTransform;
624 newTransform.set(matrix);
625 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700626
Prabir Pradhan6b384612021-05-14 16:56:25 -0700627 // We need to update the AXIS_ORIENTATION value here to maintain the old behavior where the
628 // orientation angle is not affected by the initial transformation set in the MotionEvent.
629 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
630 [&newTransform](PointerCoords& c) {
631 float orientation = c.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
632 c.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
633 transformAngle(newTransform, orientation));
634 });
Jeff Brown5912f952013-07-01 19:10:31 -0700635}
636
Evan Roskyd4d4d802021-05-03 20:12:21 -0700637void MotionEvent::applyTransform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700638 ui::Transform transform;
639 transform.set(matrix);
Evan Roskyd4d4d802021-05-03 20:12:21 -0700640
641 // Apply the transformation to all samples.
Prabir Pradhan6b384612021-05-14 16:56:25 -0700642 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
643 [&transform](PointerCoords& c) { c.transform(transform); });
Prabir Pradhan4b19bd02021-06-01 17:34:59 -0700644
645 if (mRawXCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION &&
646 mRawYCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) {
647 const vec2 cursor = transform.transform(mRawXCursorPosition, mRawYCursorPosition);
648 mRawXCursorPosition = cursor.x;
649 mRawYCursorPosition = cursor.y;
650 }
Evan Roskyd4d4d802021-05-03 20:12:21 -0700651}
652
Brett Chabotfaa986c2020-11-04 17:39:36 -0800653#ifdef __linux__
chaviw9eaa22c2020-07-01 16:21:27 -0700654static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
655 float dsdx, dtdx, tx, dtdy, dsdy, ty;
656 status_t status = parcel.readFloat(&dsdx);
657 status |= parcel.readFloat(&dtdx);
658 status |= parcel.readFloat(&tx);
659 status |= parcel.readFloat(&dtdy);
660 status |= parcel.readFloat(&dsdy);
661 status |= parcel.readFloat(&ty);
662
663 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
664 return status;
665}
666
667static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
668 status_t status = parcel.writeFloat(transform.dsdx());
669 status |= parcel.writeFloat(transform.dtdx());
670 status |= parcel.writeFloat(transform.tx());
671 status |= parcel.writeFloat(transform.dtdy());
672 status |= parcel.writeFloat(transform.dsdy());
673 status |= parcel.writeFloat(transform.ty());
674 return status;
675}
676
Jeff Brown5912f952013-07-01 19:10:31 -0700677status_t MotionEvent::readFromParcel(Parcel* parcel) {
678 size_t pointerCount = parcel->readInt32();
679 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800680 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
681 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700682 return BAD_VALUE;
683 }
684
Garfield Tan4cc839f2020-01-24 11:26:14 -0800685 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700686 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600687 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800688 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600689 std::vector<uint8_t> hmac;
690 status_t result = parcel->readByteVector(&hmac);
691 if (result != OK || hmac.size() != 32) {
692 return BAD_VALUE;
693 }
694 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700695 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100696 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700697 mFlags = parcel->readInt32();
698 mEdgeFlags = parcel->readInt32();
699 mMetaState = parcel->readInt32();
700 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800701 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700702
703 result = android::readFromParcel(mTransform, *parcel);
704 if (result != OK) {
705 return result;
706 }
Jeff Brown5912f952013-07-01 19:10:31 -0700707 mXPrecision = parcel->readFloat();
708 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700709 mRawXCursorPosition = parcel->readFloat();
710 mRawYCursorPosition = parcel->readFloat();
Evan Rosky09576692021-07-01 12:22:09 -0700711 mDisplayOrientation = parcel->readUint32();
Evan Rosky84f07f02021-04-16 10:42:42 -0700712 mDisplayWidth = parcel->readInt32();
713 mDisplayHeight = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700714 mDownTime = parcel->readInt64();
715
716 mPointerProperties.clear();
717 mPointerProperties.setCapacity(pointerCount);
718 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500719 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700720 mSamplePointerCoords.clear();
721 mSamplePointerCoords.setCapacity(sampleCount * pointerCount);
722
723 for (size_t i = 0; i < pointerCount; i++) {
724 mPointerProperties.push();
725 PointerProperties& properties = mPointerProperties.editTop();
726 properties.id = parcel->readInt32();
727 properties.toolType = parcel->readInt32();
728 }
729
Dan Austinc94fc452015-09-22 14:22:41 -0700730 while (sampleCount > 0) {
731 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500732 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700733 for (size_t i = 0; i < pointerCount; i++) {
734 mSamplePointerCoords.push();
735 status_t status = mSamplePointerCoords.editTop().readFromParcel(parcel);
736 if (status) {
737 return status;
738 }
739 }
740 }
741 return OK;
742}
743
744status_t MotionEvent::writeToParcel(Parcel* parcel) const {
745 size_t pointerCount = mPointerProperties.size();
746 size_t sampleCount = mSampleEventTimes.size();
747
748 parcel->writeInt32(pointerCount);
749 parcel->writeInt32(sampleCount);
750
Garfield Tan4cc839f2020-01-24 11:26:14 -0800751 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700752 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600753 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800754 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600755 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
756 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700757 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100758 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700759 parcel->writeInt32(mFlags);
760 parcel->writeInt32(mEdgeFlags);
761 parcel->writeInt32(mMetaState);
762 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800763 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700764
765 status_t result = android::writeToParcel(mTransform, *parcel);
766 if (result != OK) {
767 return result;
768 }
Jeff Brown5912f952013-07-01 19:10:31 -0700769 parcel->writeFloat(mXPrecision);
770 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700771 parcel->writeFloat(mRawXCursorPosition);
772 parcel->writeFloat(mRawYCursorPosition);
Evan Rosky09576692021-07-01 12:22:09 -0700773 parcel->writeUint32(mDisplayOrientation);
Evan Rosky84f07f02021-04-16 10:42:42 -0700774 parcel->writeInt32(mDisplayWidth);
775 parcel->writeInt32(mDisplayHeight);
Jeff Brown5912f952013-07-01 19:10:31 -0700776 parcel->writeInt64(mDownTime);
777
778 for (size_t i = 0; i < pointerCount; i++) {
779 const PointerProperties& properties = mPointerProperties.itemAt(i);
780 parcel->writeInt32(properties.id);
781 parcel->writeInt32(properties.toolType);
782 }
783
784 const PointerCoords* pc = mSamplePointerCoords.array();
785 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500786 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700787 for (size_t i = 0; i < pointerCount; i++) {
788 status_t status = (pc++)->writeToParcel(parcel);
789 if (status) {
790 return status;
791 }
792 }
793 }
794 return OK;
795}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800796#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700797
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600798bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Jeff Brown5912f952013-07-01 19:10:31 -0700799 if (source & AINPUT_SOURCE_CLASS_POINTER) {
800 // Specifically excludes HOVER_MOVE and SCROLL.
801 switch (action & AMOTION_EVENT_ACTION_MASK) {
802 case AMOTION_EVENT_ACTION_DOWN:
803 case AMOTION_EVENT_ACTION_MOVE:
804 case AMOTION_EVENT_ACTION_UP:
805 case AMOTION_EVENT_ACTION_POINTER_DOWN:
806 case AMOTION_EVENT_ACTION_POINTER_UP:
807 case AMOTION_EVENT_ACTION_CANCEL:
808 case AMOTION_EVENT_ACTION_OUTSIDE:
809 return true;
810 }
811 }
812 return false;
813}
814
Michael Wright872db4f2014-04-22 15:03:51 -0700815const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700816 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700817}
818
819int32_t MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700820 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700821}
822
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500823std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700824 // Convert MotionEvent action to string
825 switch (action & AMOTION_EVENT_ACTION_MASK) {
826 case AMOTION_EVENT_ACTION_DOWN:
827 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700828 case AMOTION_EVENT_ACTION_UP:
829 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500830 case AMOTION_EVENT_ACTION_MOVE:
831 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700832 case AMOTION_EVENT_ACTION_CANCEL:
833 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500834 case AMOTION_EVENT_ACTION_OUTSIDE:
835 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700836 case AMOTION_EVENT_ACTION_POINTER_DOWN:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000837 return StringPrintf("POINTER_DOWN(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700838 case AMOTION_EVENT_ACTION_POINTER_UP:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000839 return StringPrintf("POINTER_UP(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500840 case AMOTION_EVENT_ACTION_HOVER_MOVE:
841 return "HOVER_MOVE";
842 case AMOTION_EVENT_ACTION_SCROLL:
843 return "SCROLL";
844 case AMOTION_EVENT_ACTION_HOVER_ENTER:
845 return "HOVER_ENTER";
846 case AMOTION_EVENT_ACTION_HOVER_EXIT:
847 return "HOVER_EXIT";
848 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
849 return "BUTTON_PRESS";
850 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
851 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700852 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500853 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700854}
855
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800856// --- FocusEvent ---
857
Garfield Tan4cc839f2020-01-24 11:26:14 -0800858void FocusEvent::initialize(int32_t id, bool hasFocus, bool inTouchMode) {
859 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600860 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800861 mHasFocus = hasFocus;
862 mInTouchMode = inTouchMode;
863}
864
865void FocusEvent::initialize(const FocusEvent& from) {
866 InputEvent::initialize(from);
867 mHasFocus = from.mHasFocus;
868 mInTouchMode = from.mInTouchMode;
869}
Jeff Brown5912f952013-07-01 19:10:31 -0700870
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800871// --- CaptureEvent ---
872
873void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) {
874 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
875 ADISPLAY_ID_NONE, INVALID_HMAC);
876 mPointerCaptureEnabled = pointerCaptureEnabled;
877}
878
879void CaptureEvent::initialize(const CaptureEvent& from) {
880 InputEvent::initialize(from);
881 mPointerCaptureEnabled = from.mPointerCaptureEnabled;
882}
883
arthurhung7632c332020-12-30 16:58:01 +0800884// --- DragEvent ---
885
886void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) {
887 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
888 ADISPLAY_ID_NONE, INVALID_HMAC);
889 mIsExiting = isExiting;
890 mX = x;
891 mY = y;
892}
893
894void DragEvent::initialize(const DragEvent& from) {
895 InputEvent::initialize(from);
896 mIsExiting = from.mIsExiting;
897 mX = from.mX;
898 mY = from.mY;
899}
900
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700901// --- TouchModeEvent ---
902
903void TouchModeEvent::initialize(int32_t id, bool isInTouchMode) {
904 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
905 ADISPLAY_ID_NONE, INVALID_HMAC);
906 mIsInTouchMode = isInTouchMode;
907}
908
909void TouchModeEvent::initialize(const TouchModeEvent& from) {
910 InputEvent::initialize(from);
911 mIsInTouchMode = from.mIsInTouchMode;
912}
913
Jeff Brown5912f952013-07-01 19:10:31 -0700914// --- PooledInputEventFactory ---
915
916PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
917 mMaxPoolSize(maxPoolSize) {
918}
919
920PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -0700921}
922
923KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800924 if (mKeyEventPool.empty()) {
925 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700926 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800927 KeyEvent* event = mKeyEventPool.front().release();
928 mKeyEventPool.pop();
929 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700930}
931
932MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800933 if (mMotionEventPool.empty()) {
934 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700935 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800936 MotionEvent* event = mMotionEventPool.front().release();
937 mMotionEventPool.pop();
938 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700939}
940
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800941FocusEvent* PooledInputEventFactory::createFocusEvent() {
942 if (mFocusEventPool.empty()) {
943 return new FocusEvent();
944 }
945 FocusEvent* event = mFocusEventPool.front().release();
946 mFocusEventPool.pop();
947 return event;
948}
949
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800950CaptureEvent* PooledInputEventFactory::createCaptureEvent() {
951 if (mCaptureEventPool.empty()) {
952 return new CaptureEvent();
953 }
954 CaptureEvent* event = mCaptureEventPool.front().release();
955 mCaptureEventPool.pop();
956 return event;
957}
958
arthurhung7632c332020-12-30 16:58:01 +0800959DragEvent* PooledInputEventFactory::createDragEvent() {
960 if (mDragEventPool.empty()) {
961 return new DragEvent();
962 }
963 DragEvent* event = mDragEventPool.front().release();
964 mDragEventPool.pop();
965 return event;
966}
967
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700968TouchModeEvent* PooledInputEventFactory::createTouchModeEvent() {
969 if (mTouchModeEventPool.empty()) {
970 return new TouchModeEvent();
971 }
972 TouchModeEvent* event = mTouchModeEventPool.front().release();
973 mTouchModeEventPool.pop();
974 return event;
975}
976
Jeff Brown5912f952013-07-01 19:10:31 -0700977void PooledInputEventFactory::recycle(InputEvent* event) {
978 switch (event->getType()) {
979 case AINPUT_EVENT_TYPE_KEY:
980 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800981 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700982 return;
983 }
984 break;
985 case AINPUT_EVENT_TYPE_MOTION:
986 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800987 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700988 return;
989 }
990 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800991 case AINPUT_EVENT_TYPE_FOCUS:
992 if (mFocusEventPool.size() < mMaxPoolSize) {
993 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
994 return;
995 }
996 break;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800997 case AINPUT_EVENT_TYPE_CAPTURE:
998 if (mCaptureEventPool.size() < mMaxPoolSize) {
999 mCaptureEventPool.push(
1000 std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event)));
1001 return;
1002 }
1003 break;
arthurhung7632c332020-12-30 16:58:01 +08001004 case AINPUT_EVENT_TYPE_DRAG:
1005 if (mDragEventPool.size() < mMaxPoolSize) {
1006 mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event)));
1007 return;
1008 }
1009 break;
Jeff Brown5912f952013-07-01 19:10:31 -07001010 }
1011 delete event;
1012}
1013
1014} // namespace android