blob: 30d82b6051836f89608787d46151e3fb9177b51f [file] [log] [blame]
Jeff Brown5912f952013-07-01 19:10:31 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Input"
18//#define LOG_NDEBUG 0
19
chaviw09c8d2d2020-08-24 15:48:26 -070020#include <attestation/HmacKeyManager.h>
Garfield Tan84b087e2020-01-23 10:49:05 -080021#include <cutils/compiler.h>
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050022#include <inttypes.h>
Jeff Brown5912f952013-07-01 19:10:31 -070023#include <limits.h>
Garfield Tan84b087e2020-01-23 10:49:05 -080024#include <string.h>
Jeff Brown5912f952013-07-01 19:10:31 -070025
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050026#include <android-base/stringprintf.h>
chaviw98318de2021-05-19 16:45:23 -050027#include <gui/constants.h>
Jeff Brown5912f952013-07-01 19:10:31 -070028#include <input/Input.h>
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080029#include <input/InputDevice.h>
Michael Wright872db4f2014-04-22 15:03:51 -070030#include <input/InputEventLabels.h>
Jeff Brown5912f952013-07-01 19:10:31 -070031
Brett Chabotfaa986c2020-11-04 17:39:36 -080032#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -070033#include <binder/Parcel.h>
Brett Chabotfaa986c2020-11-04 17:39:36 -080034#endif
Brett Chabot58208522020-09-09 13:55:24 -070035#ifdef __ANDROID__
Garfield Tan84b087e2020-01-23 10:49:05 -080036#include <sys/random.h>
Jeff Brown5912f952013-07-01 19:10:31 -070037#endif
38
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050039using android::base::StringPrintf;
40
Jeff Brown5912f952013-07-01 19:10:31 -070041namespace android {
42
Prabir Pradhan6b384612021-05-14 16:56:25 -070043namespace {
44
45float transformAngle(const ui::Transform& transform, float angleRadians) {
46 // Construct and transform a vector oriented at the specified clockwise angle from vertical.
47 // Coordinate system: down is increasing Y, right is increasing X.
48 float x = sinf(angleRadians);
49 float y = -cosf(angleRadians);
50 vec2 transformedPoint = transform.transform(x, y);
51
52 // Determine how the origin is transformed by the matrix so that we
53 // can transform orientation vectors.
54 const vec2 origin = transform.transform(0, 0);
55
56 transformedPoint.x -= origin.x;
57 transformedPoint.y -= origin.y;
58
59 // Derive the transformed vector's clockwise angle from vertical.
60 float result = atan2f(transformedPoint.x, -transformedPoint.y);
61 if (result < -M_PI_2) {
62 result += M_PI;
63 } else if (result > M_PI_2) {
64 result -= M_PI;
65 }
66 return result;
67}
68
69// Rotates the given point to the transform's orientation. If the display width and height are
70// provided, the point is rotated in the screen space. Otherwise, the point is rotated about the
71// origin. This helper is used to avoid the extra overhead of creating new Transforms.
72vec2 rotatePoint(const ui::Transform& transform, float x, float y, int32_t displayWidth = 0,
73 int32_t displayHeight = 0) {
74 // 0x7 encapsulates all 3 rotations (see ui::Transform::RotationFlags)
75 static const int ALL_ROTATIONS_MASK = 0x7;
76 const uint32_t orientation = (transform.getOrientation() & ALL_ROTATIONS_MASK);
77 if (orientation == ui::Transform::ROT_0) {
78 return {x, y};
79 }
80
81 vec2 xy(x, y);
82 if (orientation == ui::Transform::ROT_90) {
83 xy.x = displayHeight - y;
84 xy.y = x;
85 } else if (orientation == ui::Transform::ROT_180) {
86 xy.x = displayWidth - x;
87 xy.y = displayHeight - y;
88 } else if (orientation == ui::Transform::ROT_270) {
89 xy.x = y;
90 xy.y = displayWidth - x;
91 }
92 return xy;
93}
94
Prabir Pradhan9f388812021-05-13 16:54:53 -070095vec2 applyTransformWithoutTranslation(const ui::Transform& transform, float x, float y) {
96 const vec2 transformedXy = transform.transform(x, y);
97 const vec2 transformedOrigin = transform.transform(0, 0);
98 return transformedXy - transformedOrigin;
99}
100
101bool shouldDisregardWindowTranslation(uint32_t source) {
102 // Pointer events are the only type of events that refer to absolute coordinates on the display,
103 // so we should apply the entire window transform. For other types of events, we should make
104 // sure to not apply the window translation/offset.
105 return (source & AINPUT_SOURCE_CLASS_POINTER) == 0;
106}
107
Prabir Pradhan6b384612021-05-14 16:56:25 -0700108} // namespace
109
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800110const char* motionClassificationToString(MotionClassification classification) {
111 switch (classification) {
112 case MotionClassification::NONE:
113 return "NONE";
114 case MotionClassification::AMBIGUOUS_GESTURE:
115 return "AMBIGUOUS_GESTURE";
116 case MotionClassification::DEEP_PRESS:
117 return "DEEP_PRESS";
118 }
119}
120
Garfield Tan84b087e2020-01-23 10:49:05 -0800121// --- IdGenerator ---
122IdGenerator::IdGenerator(Source source) : mSource(source) {}
123
124int32_t IdGenerator::nextId() const {
125 constexpr uint32_t SEQUENCE_NUMBER_MASK = ~SOURCE_MASK;
126 int32_t id = 0;
127
128// Avoid building against syscall getrandom(2) on host, which will fail build on Mac. Host doesn't
129// use sequence number so just always return mSource.
130#ifdef __ANDROID__
131 constexpr size_t BUF_LEN = sizeof(id);
132 size_t totalBytes = 0;
133 while (totalBytes < BUF_LEN) {
134 ssize_t bytes = TEMP_FAILURE_RETRY(getrandom(&id, BUF_LEN, GRND_NONBLOCK));
135 if (CC_UNLIKELY(bytes < 0)) {
136 ALOGW("Failed to fill in random number for sequence number: %s.", strerror(errno));
137 id = 0;
138 break;
139 }
140 totalBytes += bytes;
141 }
142#endif // __ANDROID__
143
144 return (id & SEQUENCE_NUMBER_MASK) | static_cast<int32_t>(mSource);
145}
146
Jeff Brown5912f952013-07-01 19:10:31 -0700147// --- InputEvent ---
148
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800149const char* inputEventTypeToString(int32_t type) {
150 switch (type) {
151 case AINPUT_EVENT_TYPE_KEY: {
152 return "KEY";
153 }
154 case AINPUT_EVENT_TYPE_MOTION: {
155 return "MOTION";
156 }
157 case AINPUT_EVENT_TYPE_FOCUS: {
158 return "FOCUS";
159 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800160 case AINPUT_EVENT_TYPE_CAPTURE: {
161 return "CAPTURE";
162 }
arthurhung7632c332020-12-30 16:58:01 +0800163 case AINPUT_EVENT_TYPE_DRAG: {
164 return "DRAG";
165 }
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700166 case AINPUT_EVENT_TYPE_TOUCH_MODE: {
167 return "TOUCH_MODE";
168 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800169 }
170 return "UNKNOWN";
171}
172
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800173VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
174 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
175 event.getSource(), event.getDisplayId()},
176 event.getAction(),
177 event.getDownTime(),
178 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
179 event.getKeyCode(),
180 event.getScanCode(),
181 event.getMetaState(),
182 event.getRepeatCount()};
183}
184
185VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
186 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
187 event.getSource(), event.getDisplayId()},
188 event.getRawX(0),
189 event.getRawY(0),
190 event.getActionMasked(),
191 event.getDownTime(),
192 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
193 event.getMetaState(),
194 event.getButtonState()};
195}
196
Garfield Tan4cc839f2020-01-24 11:26:14 -0800197void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600198 std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800199 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700200 mDeviceId = deviceId;
201 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100202 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600203 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700204}
205
206void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800207 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700208 mDeviceId = from.mDeviceId;
209 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100210 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600211 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700212}
213
Garfield Tan4cc839f2020-01-24 11:26:14 -0800214int32_t InputEvent::nextId() {
215 static IdGenerator idGen(IdGenerator::Source::OTHER);
216 return idGen.nextId();
217}
218
Jeff Brown5912f952013-07-01 19:10:31 -0700219// --- KeyEvent ---
220
Michael Wright872db4f2014-04-22 15:03:51 -0700221const char* KeyEvent::getLabel(int32_t keyCode) {
Chris Ye4958d062020-08-20 13:21:10 -0700222 return InputEventLookup::getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700223}
224
Michael Wright872db4f2014-04-22 15:03:51 -0700225int32_t KeyEvent::getKeyCodeFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700226 return InputEventLookup::getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700227}
228
Garfield Tan4cc839f2020-01-24 11:26:14 -0800229void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600230 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
231 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
232 nsecs_t downTime, nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800233 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700234 mAction = action;
235 mFlags = flags;
236 mKeyCode = keyCode;
237 mScanCode = scanCode;
238 mMetaState = metaState;
239 mRepeatCount = repeatCount;
240 mDownTime = downTime;
241 mEventTime = eventTime;
242}
243
244void KeyEvent::initialize(const KeyEvent& from) {
245 InputEvent::initialize(from);
246 mAction = from.mAction;
247 mFlags = from.mFlags;
248 mKeyCode = from.mKeyCode;
249 mScanCode = from.mScanCode;
250 mMetaState = from.mMetaState;
251 mRepeatCount = from.mRepeatCount;
252 mDownTime = from.mDownTime;
253 mEventTime = from.mEventTime;
254}
255
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700256const char* KeyEvent::actionToString(int32_t action) {
257 // Convert KeyEvent action to string
258 switch (action) {
259 case AKEY_EVENT_ACTION_DOWN:
260 return "DOWN";
261 case AKEY_EVENT_ACTION_UP:
262 return "UP";
263 case AKEY_EVENT_ACTION_MULTIPLE:
264 return "MULTIPLE";
265 }
266 return "UNKNOWN";
267}
Jeff Brown5912f952013-07-01 19:10:31 -0700268
269// --- PointerCoords ---
270
271float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700272 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700273 return 0;
274 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700275 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700276}
277
278status_t PointerCoords::setAxisValue(int32_t axis, float value) {
279 if (axis < 0 || axis > 63) {
280 return NAME_NOT_FOUND;
281 }
282
Michael Wright38dcdff2014-03-19 12:06:10 -0700283 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
284 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700285 if (value == 0) {
286 return OK; // axes with value 0 do not need to be stored
287 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700288
289 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700290 if (count >= MAX_AXES) {
291 tooManyAxes(axis);
292 return NO_MEMORY;
293 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700294 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700295 for (uint32_t i = count; i > index; i--) {
296 values[i] = values[i - 1];
297 }
298 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700299
Jeff Brown5912f952013-07-01 19:10:31 -0700300 values[index] = value;
301 return OK;
302}
303
304static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
305 float value = c.getAxisValue(axis);
306 if (value != 0) {
307 c.setAxisValue(axis, value * scaleFactor);
308 }
309}
310
Robert Carre07e1032018-11-26 12:55:53 -0800311void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700312 // No need to scale pressure or size since they are normalized.
313 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800314
315 // If there is a global scale factor, it is included in the windowX/YScale
316 // so we don't need to apply it twice to the X/Y axes.
317 // However we don't want to apply any windowXYScale not included in the global scale
318 // to the TOUCH_MAJOR/MINOR coordinates.
319 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
320 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
321 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
322 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
323 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
324 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
Prabir Pradhanc6523582021-05-14 18:02:55 -0700325 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_X, windowXScale);
326 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_Y, windowYScale);
Robert Carre07e1032018-11-26 12:55:53 -0800327}
328
Jeff Brownf086ddb2014-02-11 14:28:48 -0800329void PointerCoords::applyOffset(float xOffset, float yOffset) {
330 setAxisValue(AMOTION_EVENT_AXIS_X, getX() + xOffset);
331 setAxisValue(AMOTION_EVENT_AXIS_Y, getY() + yOffset);
332}
333
Brett Chabotfaa986c2020-11-04 17:39:36 -0800334#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700335status_t PointerCoords::readFromParcel(Parcel* parcel) {
336 bits = parcel->readInt64();
337
Michael Wright38dcdff2014-03-19 12:06:10 -0700338 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700339 if (count > MAX_AXES) {
340 return BAD_VALUE;
341 }
342
343 for (uint32_t i = 0; i < count; i++) {
344 values[i] = parcel->readFloat();
345 }
346 return OK;
347}
348
349status_t PointerCoords::writeToParcel(Parcel* parcel) const {
350 parcel->writeInt64(bits);
351
Michael Wright38dcdff2014-03-19 12:06:10 -0700352 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700353 for (uint32_t i = 0; i < count; i++) {
354 parcel->writeFloat(values[i]);
355 }
356 return OK;
357}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800358#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700359
360void PointerCoords::tooManyAxes(int axis) {
361 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
362 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
363}
364
365bool PointerCoords::operator==(const PointerCoords& other) const {
366 if (bits != other.bits) {
367 return false;
368 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700369 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700370 for (uint32_t i = 0; i < count; i++) {
371 if (values[i] != other.values[i]) {
372 return false;
373 }
374 }
375 return true;
376}
377
378void PointerCoords::copyFrom(const PointerCoords& other) {
379 bits = other.bits;
Michael Wright38dcdff2014-03-19 12:06:10 -0700380 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700381 for (uint32_t i = 0; i < count; i++) {
382 values[i] = other.values[i];
383 }
384}
385
chaviwc01e1372020-07-01 12:37:31 -0700386void PointerCoords::transform(const ui::Transform& transform) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700387 const vec2 xy = transform.transform(getXYValue());
388 setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
389 setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
390
Prabir Pradhanc6523582021-05-14 18:02:55 -0700391 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_X) ||
392 BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_Y)) {
393 const ui::Transform rotation(transform.getOrientation());
394 const vec2 relativeXy = rotation.transform(getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
395 getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
396 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
397 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
398 }
399
Prabir Pradhan6b384612021-05-14 16:56:25 -0700400 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) {
401 const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
402 setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val));
403 }
chaviwc01e1372020-07-01 12:37:31 -0700404}
Jeff Brown5912f952013-07-01 19:10:31 -0700405
406// --- PointerProperties ---
407
408bool PointerProperties::operator==(const PointerProperties& other) const {
409 return id == other.id
410 && toolType == other.toolType;
411}
412
413void PointerProperties::copyFrom(const PointerProperties& other) {
414 id = other.id;
415 toolType = other.toolType;
416}
417
418
419// --- MotionEvent ---
420
Garfield Tan4cc839f2020-01-24 11:26:14 -0800421void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600422 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
423 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700424 int32_t buttonState, MotionClassification classification,
425 const ui::Transform& transform, float xPrecision, float yPrecision,
Evan Rosky84f07f02021-04-16 10:42:42 -0700426 float rawXCursorPosition, float rawYCursorPosition,
427 int32_t displayWidth, int32_t displayHeight, nsecs_t downTime,
chaviw9eaa22c2020-07-01 16:21:27 -0700428 nsecs_t eventTime, size_t pointerCount,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600429 const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700430 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800431 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700432 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100433 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700434 mFlags = flags;
435 mEdgeFlags = edgeFlags;
436 mMetaState = metaState;
437 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800438 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700439 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700440 mXPrecision = xPrecision;
441 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700442 mRawXCursorPosition = rawXCursorPosition;
443 mRawYCursorPosition = rawYCursorPosition;
Evan Rosky84f07f02021-04-16 10:42:42 -0700444 mDisplayWidth = displayWidth;
445 mDisplayHeight = displayHeight;
Jeff Brown5912f952013-07-01 19:10:31 -0700446 mDownTime = downTime;
447 mPointerProperties.clear();
448 mPointerProperties.appendArray(pointerProperties, pointerCount);
449 mSampleEventTimes.clear();
450 mSamplePointerCoords.clear();
451 addSample(eventTime, pointerCoords);
452}
453
454void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800455 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
456 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700457 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100458 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700459 mFlags = other->mFlags;
460 mEdgeFlags = other->mEdgeFlags;
461 mMetaState = other->mMetaState;
462 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800463 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700464 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700465 mXPrecision = other->mXPrecision;
466 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700467 mRawXCursorPosition = other->mRawXCursorPosition;
468 mRawYCursorPosition = other->mRawYCursorPosition;
Evan Rosky84f07f02021-04-16 10:42:42 -0700469 mDisplayWidth = other->mDisplayWidth;
470 mDisplayHeight = other->mDisplayHeight;
Jeff Brown5912f952013-07-01 19:10:31 -0700471 mDownTime = other->mDownTime;
472 mPointerProperties = other->mPointerProperties;
473
474 if (keepHistory) {
475 mSampleEventTimes = other->mSampleEventTimes;
476 mSamplePointerCoords = other->mSamplePointerCoords;
477 } else {
478 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500479 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700480 mSamplePointerCoords.clear();
481 size_t pointerCount = other->getPointerCount();
482 size_t historySize = other->getHistorySize();
483 mSamplePointerCoords.appendArray(other->mSamplePointerCoords.array()
484 + (historySize * pointerCount), pointerCount);
485 }
486}
487
488void MotionEvent::addSample(
489 int64_t eventTime,
490 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500491 mSampleEventTimes.push_back(eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -0700492 mSamplePointerCoords.appendArray(pointerCoords, getPointerCount());
493}
494
Garfield Tan00f511d2019-06-12 16:55:40 -0700495float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700496 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
497 return vals.x;
Garfield Tan00f511d2019-06-12 16:55:40 -0700498}
499
500float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700501 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
502 return vals.y;
Garfield Tan00f511d2019-06-12 16:55:40 -0700503}
504
Garfield Tan937bb832019-07-25 17:48:31 -0700505void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700506 ui::Transform inverse = mTransform.inverse();
507 vec2 vals = inverse.transform(x, y);
508 mRawXCursorPosition = vals.x;
509 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700510}
511
Jeff Brown5912f952013-07-01 19:10:31 -0700512const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
513 return &mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
514}
515
516float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
Evan Rosky84f07f02021-04-16 10:42:42 -0700517 return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700518}
519
520float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700521 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700522}
523
524const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
525 size_t pointerIndex, size_t historicalIndex) const {
526 return &mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
527}
528
529float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan6b384612021-05-14 16:56:25 -0700530 size_t historicalIndex) const {
531 const PointerCoords* coords = getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
532
533 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
534 // For compatibility, convert raw coordinates into "oriented screen space". Once app
535 // developers are educated about getRaw, we can consider removing this.
Prabir Pradhan9f388812021-05-13 16:54:53 -0700536 const vec2 xy = shouldDisregardWindowTranslation(mSource)
537 ? rotatePoint(mTransform, coords->getX(), coords->getY())
538 : rotatePoint(mTransform, coords->getX(), coords->getY(), mDisplayWidth,
539 mDisplayHeight);
Prabir Pradhan6b384612021-05-14 16:56:25 -0700540 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
541 return xy[axis];
Evan Rosky84f07f02021-04-16 10:42:42 -0700542 }
543
Prabir Pradhanc6523582021-05-14 18:02:55 -0700544 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
545 // For compatibility, since we convert raw coordinates into "oriented screen space", we
546 // need to convert the relative axes into the same orientation for consistency.
547 const vec2 relativeXy =
548 rotatePoint(mTransform, coords->getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
549 coords->getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
550 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
551 }
552
Prabir Pradhan6b384612021-05-14 16:56:25 -0700553 return coords->getAxisValue(axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700554}
555
556float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan9f388812021-05-13 16:54:53 -0700557 size_t historicalIndex) const {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700558 const PointerCoords* coords = getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
559
560 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
Prabir Pradhan9f388812021-05-13 16:54:53 -0700561 const vec2 xy = shouldDisregardWindowTranslation(mSource)
562 ? applyTransformWithoutTranslation(mTransform, coords->getX(), coords->getY())
563 : mTransform.transform(coords->getXYValue());
Prabir Pradhan6b384612021-05-14 16:56:25 -0700564 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
565 return xy[axis];
chaviw9eaa22c2020-07-01 16:21:27 -0700566 }
567
Prabir Pradhanc6523582021-05-14 18:02:55 -0700568 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
569 const vec2 relativeXy =
570 applyTransformWithoutTranslation(mTransform,
571 coords->getAxisValue(
572 AMOTION_EVENT_AXIS_RELATIVE_X),
573 coords->getAxisValue(
574 AMOTION_EVENT_AXIS_RELATIVE_Y));
575 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
576 }
577
Prabir Pradhan6b384612021-05-14 16:56:25 -0700578 return coords->getAxisValue(axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700579}
580
581ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
582 size_t pointerCount = mPointerProperties.size();
583 for (size_t i = 0; i < pointerCount; i++) {
584 if (mPointerProperties.itemAt(i).id == pointerId) {
585 return i;
586 }
587 }
588 return -1;
589}
590
591void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700592 float currXOffset = mTransform.tx();
593 float currYOffset = mTransform.ty();
594 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700595}
596
Robert Carre07e1032018-11-26 12:55:53 -0800597void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700598 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800599 mXPrecision *= globalScaleFactor;
600 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700601
602 size_t numSamples = mSamplePointerCoords.size();
603 for (size_t i = 0; i < numSamples; i++) {
chaviw9eaa22c2020-07-01 16:21:27 -0700604 mSamplePointerCoords.editItemAt(i).scale(globalScaleFactor, globalScaleFactor,
605 globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700606 }
607}
608
chaviw9eaa22c2020-07-01 16:21:27 -0700609void MotionEvent::transform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700610 // We want to preserve the raw axes values stored in the PointerCoords, so we just update the
611 // transform using the values passed in.
chaviw9eaa22c2020-07-01 16:21:27 -0700612 ui::Transform newTransform;
613 newTransform.set(matrix);
614 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700615
Prabir Pradhan6b384612021-05-14 16:56:25 -0700616 // We need to update the AXIS_ORIENTATION value here to maintain the old behavior where the
617 // orientation angle is not affected by the initial transformation set in the MotionEvent.
618 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
619 [&newTransform](PointerCoords& c) {
620 float orientation = c.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
621 c.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
622 transformAngle(newTransform, orientation));
623 });
Jeff Brown5912f952013-07-01 19:10:31 -0700624}
625
Evan Roskyd4d4d802021-05-03 20:12:21 -0700626void MotionEvent::applyTransform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700627 ui::Transform transform;
628 transform.set(matrix);
Evan Roskyd4d4d802021-05-03 20:12:21 -0700629
630 // Apply the transformation to all samples.
Prabir Pradhan6b384612021-05-14 16:56:25 -0700631 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
632 [&transform](PointerCoords& c) { c.transform(transform); });
Prabir Pradhan4b19bd02021-06-01 17:34:59 -0700633
634 if (mRawXCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION &&
635 mRawYCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) {
636 const vec2 cursor = transform.transform(mRawXCursorPosition, mRawYCursorPosition);
637 mRawXCursorPosition = cursor.x;
638 mRawYCursorPosition = cursor.y;
639 }
Evan Roskyd4d4d802021-05-03 20:12:21 -0700640}
641
Brett Chabotfaa986c2020-11-04 17:39:36 -0800642#ifdef __linux__
chaviw9eaa22c2020-07-01 16:21:27 -0700643static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
644 float dsdx, dtdx, tx, dtdy, dsdy, ty;
645 status_t status = parcel.readFloat(&dsdx);
646 status |= parcel.readFloat(&dtdx);
647 status |= parcel.readFloat(&tx);
648 status |= parcel.readFloat(&dtdy);
649 status |= parcel.readFloat(&dsdy);
650 status |= parcel.readFloat(&ty);
651
652 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
653 return status;
654}
655
656static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
657 status_t status = parcel.writeFloat(transform.dsdx());
658 status |= parcel.writeFloat(transform.dtdx());
659 status |= parcel.writeFloat(transform.tx());
660 status |= parcel.writeFloat(transform.dtdy());
661 status |= parcel.writeFloat(transform.dsdy());
662 status |= parcel.writeFloat(transform.ty());
663 return status;
664}
665
Jeff Brown5912f952013-07-01 19:10:31 -0700666status_t MotionEvent::readFromParcel(Parcel* parcel) {
667 size_t pointerCount = parcel->readInt32();
668 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800669 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
670 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700671 return BAD_VALUE;
672 }
673
Garfield Tan4cc839f2020-01-24 11:26:14 -0800674 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700675 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600676 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800677 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600678 std::vector<uint8_t> hmac;
679 status_t result = parcel->readByteVector(&hmac);
680 if (result != OK || hmac.size() != 32) {
681 return BAD_VALUE;
682 }
683 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700684 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100685 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700686 mFlags = parcel->readInt32();
687 mEdgeFlags = parcel->readInt32();
688 mMetaState = parcel->readInt32();
689 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800690 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700691
692 result = android::readFromParcel(mTransform, *parcel);
693 if (result != OK) {
694 return result;
695 }
Jeff Brown5912f952013-07-01 19:10:31 -0700696 mXPrecision = parcel->readFloat();
697 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700698 mRawXCursorPosition = parcel->readFloat();
699 mRawYCursorPosition = parcel->readFloat();
Evan Rosky84f07f02021-04-16 10:42:42 -0700700 mDisplayWidth = parcel->readInt32();
701 mDisplayHeight = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700702 mDownTime = parcel->readInt64();
703
704 mPointerProperties.clear();
705 mPointerProperties.setCapacity(pointerCount);
706 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500707 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700708 mSamplePointerCoords.clear();
709 mSamplePointerCoords.setCapacity(sampleCount * pointerCount);
710
711 for (size_t i = 0; i < pointerCount; i++) {
712 mPointerProperties.push();
713 PointerProperties& properties = mPointerProperties.editTop();
714 properties.id = parcel->readInt32();
715 properties.toolType = parcel->readInt32();
716 }
717
Dan Austinc94fc452015-09-22 14:22:41 -0700718 while (sampleCount > 0) {
719 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500720 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700721 for (size_t i = 0; i < pointerCount; i++) {
722 mSamplePointerCoords.push();
723 status_t status = mSamplePointerCoords.editTop().readFromParcel(parcel);
724 if (status) {
725 return status;
726 }
727 }
728 }
729 return OK;
730}
731
732status_t MotionEvent::writeToParcel(Parcel* parcel) const {
733 size_t pointerCount = mPointerProperties.size();
734 size_t sampleCount = mSampleEventTimes.size();
735
736 parcel->writeInt32(pointerCount);
737 parcel->writeInt32(sampleCount);
738
Garfield Tan4cc839f2020-01-24 11:26:14 -0800739 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700740 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600741 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800742 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600743 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
744 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700745 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100746 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700747 parcel->writeInt32(mFlags);
748 parcel->writeInt32(mEdgeFlags);
749 parcel->writeInt32(mMetaState);
750 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800751 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700752
753 status_t result = android::writeToParcel(mTransform, *parcel);
754 if (result != OK) {
755 return result;
756 }
Jeff Brown5912f952013-07-01 19:10:31 -0700757 parcel->writeFloat(mXPrecision);
758 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700759 parcel->writeFloat(mRawXCursorPosition);
760 parcel->writeFloat(mRawYCursorPosition);
Evan Rosky84f07f02021-04-16 10:42:42 -0700761 parcel->writeInt32(mDisplayWidth);
762 parcel->writeInt32(mDisplayHeight);
Jeff Brown5912f952013-07-01 19:10:31 -0700763 parcel->writeInt64(mDownTime);
764
765 for (size_t i = 0; i < pointerCount; i++) {
766 const PointerProperties& properties = mPointerProperties.itemAt(i);
767 parcel->writeInt32(properties.id);
768 parcel->writeInt32(properties.toolType);
769 }
770
771 const PointerCoords* pc = mSamplePointerCoords.array();
772 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500773 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700774 for (size_t i = 0; i < pointerCount; i++) {
775 status_t status = (pc++)->writeToParcel(parcel);
776 if (status) {
777 return status;
778 }
779 }
780 }
781 return OK;
782}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800783#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700784
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600785bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Jeff Brown5912f952013-07-01 19:10:31 -0700786 if (source & AINPUT_SOURCE_CLASS_POINTER) {
787 // Specifically excludes HOVER_MOVE and SCROLL.
788 switch (action & AMOTION_EVENT_ACTION_MASK) {
789 case AMOTION_EVENT_ACTION_DOWN:
790 case AMOTION_EVENT_ACTION_MOVE:
791 case AMOTION_EVENT_ACTION_UP:
792 case AMOTION_EVENT_ACTION_POINTER_DOWN:
793 case AMOTION_EVENT_ACTION_POINTER_UP:
794 case AMOTION_EVENT_ACTION_CANCEL:
795 case AMOTION_EVENT_ACTION_OUTSIDE:
796 return true;
797 }
798 }
799 return false;
800}
801
Michael Wright872db4f2014-04-22 15:03:51 -0700802const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700803 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700804}
805
806int32_t MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700807 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700808}
809
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500810std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700811 // Convert MotionEvent action to string
812 switch (action & AMOTION_EVENT_ACTION_MASK) {
813 case AMOTION_EVENT_ACTION_DOWN:
814 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700815 case AMOTION_EVENT_ACTION_UP:
816 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500817 case AMOTION_EVENT_ACTION_MOVE:
818 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700819 case AMOTION_EVENT_ACTION_CANCEL:
820 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500821 case AMOTION_EVENT_ACTION_OUTSIDE:
822 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700823 case AMOTION_EVENT_ACTION_POINTER_DOWN:
824 return "POINTER_DOWN";
825 case AMOTION_EVENT_ACTION_POINTER_UP:
826 return "POINTER_UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500827 case AMOTION_EVENT_ACTION_HOVER_MOVE:
828 return "HOVER_MOVE";
829 case AMOTION_EVENT_ACTION_SCROLL:
830 return "SCROLL";
831 case AMOTION_EVENT_ACTION_HOVER_ENTER:
832 return "HOVER_ENTER";
833 case AMOTION_EVENT_ACTION_HOVER_EXIT:
834 return "HOVER_EXIT";
835 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
836 return "BUTTON_PRESS";
837 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
838 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700839 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500840 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700841}
842
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800843// --- FocusEvent ---
844
Garfield Tan4cc839f2020-01-24 11:26:14 -0800845void FocusEvent::initialize(int32_t id, bool hasFocus, bool inTouchMode) {
846 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600847 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800848 mHasFocus = hasFocus;
849 mInTouchMode = inTouchMode;
850}
851
852void FocusEvent::initialize(const FocusEvent& from) {
853 InputEvent::initialize(from);
854 mHasFocus = from.mHasFocus;
855 mInTouchMode = from.mInTouchMode;
856}
Jeff Brown5912f952013-07-01 19:10:31 -0700857
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800858// --- CaptureEvent ---
859
860void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) {
861 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
862 ADISPLAY_ID_NONE, INVALID_HMAC);
863 mPointerCaptureEnabled = pointerCaptureEnabled;
864}
865
866void CaptureEvent::initialize(const CaptureEvent& from) {
867 InputEvent::initialize(from);
868 mPointerCaptureEnabled = from.mPointerCaptureEnabled;
869}
870
arthurhung7632c332020-12-30 16:58:01 +0800871// --- DragEvent ---
872
873void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) {
874 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
875 ADISPLAY_ID_NONE, INVALID_HMAC);
876 mIsExiting = isExiting;
877 mX = x;
878 mY = y;
879}
880
881void DragEvent::initialize(const DragEvent& from) {
882 InputEvent::initialize(from);
883 mIsExiting = from.mIsExiting;
884 mX = from.mX;
885 mY = from.mY;
886}
887
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700888// --- TouchModeEvent ---
889
890void TouchModeEvent::initialize(int32_t id, bool isInTouchMode) {
891 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
892 ADISPLAY_ID_NONE, INVALID_HMAC);
893 mIsInTouchMode = isInTouchMode;
894}
895
896void TouchModeEvent::initialize(const TouchModeEvent& from) {
897 InputEvent::initialize(from);
898 mIsInTouchMode = from.mIsInTouchMode;
899}
900
Jeff Brown5912f952013-07-01 19:10:31 -0700901// --- PooledInputEventFactory ---
902
903PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
904 mMaxPoolSize(maxPoolSize) {
905}
906
907PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -0700908}
909
910KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800911 if (mKeyEventPool.empty()) {
912 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700913 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800914 KeyEvent* event = mKeyEventPool.front().release();
915 mKeyEventPool.pop();
916 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700917}
918
919MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800920 if (mMotionEventPool.empty()) {
921 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700922 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800923 MotionEvent* event = mMotionEventPool.front().release();
924 mMotionEventPool.pop();
925 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700926}
927
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800928FocusEvent* PooledInputEventFactory::createFocusEvent() {
929 if (mFocusEventPool.empty()) {
930 return new FocusEvent();
931 }
932 FocusEvent* event = mFocusEventPool.front().release();
933 mFocusEventPool.pop();
934 return event;
935}
936
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800937CaptureEvent* PooledInputEventFactory::createCaptureEvent() {
938 if (mCaptureEventPool.empty()) {
939 return new CaptureEvent();
940 }
941 CaptureEvent* event = mCaptureEventPool.front().release();
942 mCaptureEventPool.pop();
943 return event;
944}
945
arthurhung7632c332020-12-30 16:58:01 +0800946DragEvent* PooledInputEventFactory::createDragEvent() {
947 if (mDragEventPool.empty()) {
948 return new DragEvent();
949 }
950 DragEvent* event = mDragEventPool.front().release();
951 mDragEventPool.pop();
952 return event;
953}
954
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700955TouchModeEvent* PooledInputEventFactory::createTouchModeEvent() {
956 if (mTouchModeEventPool.empty()) {
957 return new TouchModeEvent();
958 }
959 TouchModeEvent* event = mTouchModeEventPool.front().release();
960 mTouchModeEventPool.pop();
961 return event;
962}
963
Jeff Brown5912f952013-07-01 19:10:31 -0700964void PooledInputEventFactory::recycle(InputEvent* event) {
965 switch (event->getType()) {
966 case AINPUT_EVENT_TYPE_KEY:
967 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800968 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700969 return;
970 }
971 break;
972 case AINPUT_EVENT_TYPE_MOTION:
973 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800974 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700975 return;
976 }
977 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800978 case AINPUT_EVENT_TYPE_FOCUS:
979 if (mFocusEventPool.size() < mMaxPoolSize) {
980 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
981 return;
982 }
983 break;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800984 case AINPUT_EVENT_TYPE_CAPTURE:
985 if (mCaptureEventPool.size() < mMaxPoolSize) {
986 mCaptureEventPool.push(
987 std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event)));
988 return;
989 }
990 break;
arthurhung7632c332020-12-30 16:58:01 +0800991 case AINPUT_EVENT_TYPE_DRAG:
992 if (mDragEventPool.size() < mMaxPoolSize) {
993 mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event)));
994 return;
995 }
996 break;
Jeff Brown5912f952013-07-01 19:10:31 -0700997 }
998 delete event;
999}
1000
1001} // namespace android