blob: 9390467f55242f5cba318ee52af7119d8ef204f2 [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 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800166 }
167 return "UNKNOWN";
168}
169
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800170VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
171 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
172 event.getSource(), event.getDisplayId()},
173 event.getAction(),
174 event.getDownTime(),
175 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
176 event.getKeyCode(),
177 event.getScanCode(),
178 event.getMetaState(),
179 event.getRepeatCount()};
180}
181
182VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
183 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
184 event.getSource(), event.getDisplayId()},
185 event.getRawX(0),
186 event.getRawY(0),
187 event.getActionMasked(),
188 event.getDownTime(),
189 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
190 event.getMetaState(),
191 event.getButtonState()};
192}
193
Garfield Tan4cc839f2020-01-24 11:26:14 -0800194void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600195 std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800196 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700197 mDeviceId = deviceId;
198 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100199 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600200 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700201}
202
203void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800204 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700205 mDeviceId = from.mDeviceId;
206 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100207 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600208 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700209}
210
Garfield Tan4cc839f2020-01-24 11:26:14 -0800211int32_t InputEvent::nextId() {
212 static IdGenerator idGen(IdGenerator::Source::OTHER);
213 return idGen.nextId();
214}
215
Jeff Brown5912f952013-07-01 19:10:31 -0700216// --- KeyEvent ---
217
Michael Wright872db4f2014-04-22 15:03:51 -0700218const char* KeyEvent::getLabel(int32_t keyCode) {
Chris Ye4958d062020-08-20 13:21:10 -0700219 return InputEventLookup::getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700220}
221
Michael Wright872db4f2014-04-22 15:03:51 -0700222int32_t KeyEvent::getKeyCodeFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700223 return InputEventLookup::getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700224}
225
Garfield Tan4cc839f2020-01-24 11:26:14 -0800226void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600227 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
228 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
229 nsecs_t downTime, nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800230 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700231 mAction = action;
232 mFlags = flags;
233 mKeyCode = keyCode;
234 mScanCode = scanCode;
235 mMetaState = metaState;
236 mRepeatCount = repeatCount;
237 mDownTime = downTime;
238 mEventTime = eventTime;
239}
240
241void KeyEvent::initialize(const KeyEvent& from) {
242 InputEvent::initialize(from);
243 mAction = from.mAction;
244 mFlags = from.mFlags;
245 mKeyCode = from.mKeyCode;
246 mScanCode = from.mScanCode;
247 mMetaState = from.mMetaState;
248 mRepeatCount = from.mRepeatCount;
249 mDownTime = from.mDownTime;
250 mEventTime = from.mEventTime;
251}
252
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700253const char* KeyEvent::actionToString(int32_t action) {
254 // Convert KeyEvent action to string
255 switch (action) {
256 case AKEY_EVENT_ACTION_DOWN:
257 return "DOWN";
258 case AKEY_EVENT_ACTION_UP:
259 return "UP";
260 case AKEY_EVENT_ACTION_MULTIPLE:
261 return "MULTIPLE";
262 }
263 return "UNKNOWN";
264}
Jeff Brown5912f952013-07-01 19:10:31 -0700265
266// --- PointerCoords ---
267
268float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700269 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700270 return 0;
271 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700272 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700273}
274
275status_t PointerCoords::setAxisValue(int32_t axis, float value) {
276 if (axis < 0 || axis > 63) {
277 return NAME_NOT_FOUND;
278 }
279
Michael Wright38dcdff2014-03-19 12:06:10 -0700280 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
281 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700282 if (value == 0) {
283 return OK; // axes with value 0 do not need to be stored
284 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700285
286 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700287 if (count >= MAX_AXES) {
288 tooManyAxes(axis);
289 return NO_MEMORY;
290 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700291 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700292 for (uint32_t i = count; i > index; i--) {
293 values[i] = values[i - 1];
294 }
295 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700296
Jeff Brown5912f952013-07-01 19:10:31 -0700297 values[index] = value;
298 return OK;
299}
300
301static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
302 float value = c.getAxisValue(axis);
303 if (value != 0) {
304 c.setAxisValue(axis, value * scaleFactor);
305 }
306}
307
Robert Carre07e1032018-11-26 12:55:53 -0800308void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700309 // No need to scale pressure or size since they are normalized.
310 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800311
312 // If there is a global scale factor, it is included in the windowX/YScale
313 // so we don't need to apply it twice to the X/Y axes.
314 // However we don't want to apply any windowXYScale not included in the global scale
315 // to the TOUCH_MAJOR/MINOR coordinates.
316 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
317 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
318 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
319 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
320 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
321 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
Prabir Pradhanc6523582021-05-14 18:02:55 -0700322 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_X, windowXScale);
323 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_Y, windowYScale);
Robert Carre07e1032018-11-26 12:55:53 -0800324}
325
Jeff Brownf086ddb2014-02-11 14:28:48 -0800326void PointerCoords::applyOffset(float xOffset, float yOffset) {
327 setAxisValue(AMOTION_EVENT_AXIS_X, getX() + xOffset);
328 setAxisValue(AMOTION_EVENT_AXIS_Y, getY() + yOffset);
329}
330
Brett Chabotfaa986c2020-11-04 17:39:36 -0800331#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700332status_t PointerCoords::readFromParcel(Parcel* parcel) {
333 bits = parcel->readInt64();
334
Michael Wright38dcdff2014-03-19 12:06:10 -0700335 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700336 if (count > MAX_AXES) {
337 return BAD_VALUE;
338 }
339
340 for (uint32_t i = 0; i < count; i++) {
341 values[i] = parcel->readFloat();
342 }
343 return OK;
344}
345
346status_t PointerCoords::writeToParcel(Parcel* parcel) const {
347 parcel->writeInt64(bits);
348
Michael Wright38dcdff2014-03-19 12:06:10 -0700349 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700350 for (uint32_t i = 0; i < count; i++) {
351 parcel->writeFloat(values[i]);
352 }
353 return OK;
354}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800355#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700356
357void PointerCoords::tooManyAxes(int axis) {
358 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
359 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
360}
361
362bool PointerCoords::operator==(const PointerCoords& other) const {
363 if (bits != other.bits) {
364 return false;
365 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700366 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700367 for (uint32_t i = 0; i < count; i++) {
368 if (values[i] != other.values[i]) {
369 return false;
370 }
371 }
372 return true;
373}
374
375void PointerCoords::copyFrom(const PointerCoords& other) {
376 bits = other.bits;
Michael Wright38dcdff2014-03-19 12:06:10 -0700377 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700378 for (uint32_t i = 0; i < count; i++) {
379 values[i] = other.values[i];
380 }
381}
382
chaviwc01e1372020-07-01 12:37:31 -0700383void PointerCoords::transform(const ui::Transform& transform) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700384 const vec2 xy = transform.transform(getXYValue());
385 setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
386 setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
387
Prabir Pradhanc6523582021-05-14 18:02:55 -0700388 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_X) ||
389 BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_Y)) {
390 const ui::Transform rotation(transform.getOrientation());
391 const vec2 relativeXy = rotation.transform(getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
392 getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
393 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
394 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
395 }
396
Prabir Pradhan6b384612021-05-14 16:56:25 -0700397 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) {
398 const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
399 setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val));
400 }
chaviwc01e1372020-07-01 12:37:31 -0700401}
Jeff Brown5912f952013-07-01 19:10:31 -0700402
403// --- PointerProperties ---
404
405bool PointerProperties::operator==(const PointerProperties& other) const {
406 return id == other.id
407 && toolType == other.toolType;
408}
409
410void PointerProperties::copyFrom(const PointerProperties& other) {
411 id = other.id;
412 toolType = other.toolType;
413}
414
415
416// --- MotionEvent ---
417
Garfield Tan4cc839f2020-01-24 11:26:14 -0800418void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600419 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
420 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700421 int32_t buttonState, MotionClassification classification,
422 const ui::Transform& transform, float xPrecision, float yPrecision,
Evan Rosky84f07f02021-04-16 10:42:42 -0700423 float rawXCursorPosition, float rawYCursorPosition,
424 int32_t displayWidth, int32_t displayHeight, nsecs_t downTime,
chaviw9eaa22c2020-07-01 16:21:27 -0700425 nsecs_t eventTime, size_t pointerCount,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600426 const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700427 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800428 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700429 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100430 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700431 mFlags = flags;
432 mEdgeFlags = edgeFlags;
433 mMetaState = metaState;
434 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800435 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700436 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700437 mXPrecision = xPrecision;
438 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700439 mRawXCursorPosition = rawXCursorPosition;
440 mRawYCursorPosition = rawYCursorPosition;
Evan Rosky84f07f02021-04-16 10:42:42 -0700441 mDisplayWidth = displayWidth;
442 mDisplayHeight = displayHeight;
Jeff Brown5912f952013-07-01 19:10:31 -0700443 mDownTime = downTime;
444 mPointerProperties.clear();
445 mPointerProperties.appendArray(pointerProperties, pointerCount);
446 mSampleEventTimes.clear();
447 mSamplePointerCoords.clear();
448 addSample(eventTime, pointerCoords);
449}
450
451void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800452 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
453 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700454 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100455 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700456 mFlags = other->mFlags;
457 mEdgeFlags = other->mEdgeFlags;
458 mMetaState = other->mMetaState;
459 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800460 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700461 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700462 mXPrecision = other->mXPrecision;
463 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700464 mRawXCursorPosition = other->mRawXCursorPosition;
465 mRawYCursorPosition = other->mRawYCursorPosition;
Evan Rosky84f07f02021-04-16 10:42:42 -0700466 mDisplayWidth = other->mDisplayWidth;
467 mDisplayHeight = other->mDisplayHeight;
Jeff Brown5912f952013-07-01 19:10:31 -0700468 mDownTime = other->mDownTime;
469 mPointerProperties = other->mPointerProperties;
470
471 if (keepHistory) {
472 mSampleEventTimes = other->mSampleEventTimes;
473 mSamplePointerCoords = other->mSamplePointerCoords;
474 } else {
475 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500476 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700477 mSamplePointerCoords.clear();
478 size_t pointerCount = other->getPointerCount();
479 size_t historySize = other->getHistorySize();
480 mSamplePointerCoords.appendArray(other->mSamplePointerCoords.array()
481 + (historySize * pointerCount), pointerCount);
482 }
483}
484
485void MotionEvent::addSample(
486 int64_t eventTime,
487 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500488 mSampleEventTimes.push_back(eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -0700489 mSamplePointerCoords.appendArray(pointerCoords, getPointerCount());
490}
491
Garfield Tan00f511d2019-06-12 16:55:40 -0700492float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700493 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
494 return vals.x;
Garfield Tan00f511d2019-06-12 16:55:40 -0700495}
496
497float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700498 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
499 return vals.y;
Garfield Tan00f511d2019-06-12 16:55:40 -0700500}
501
Garfield Tan937bb832019-07-25 17:48:31 -0700502void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700503 ui::Transform inverse = mTransform.inverse();
504 vec2 vals = inverse.transform(x, y);
505 mRawXCursorPosition = vals.x;
506 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700507}
508
Jeff Brown5912f952013-07-01 19:10:31 -0700509const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
510 return &mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
511}
512
513float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
Evan Rosky84f07f02021-04-16 10:42:42 -0700514 return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700515}
516
517float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700518 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700519}
520
521const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
522 size_t pointerIndex, size_t historicalIndex) const {
523 return &mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
524}
525
526float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan6b384612021-05-14 16:56:25 -0700527 size_t historicalIndex) const {
528 const PointerCoords* coords = getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
529
530 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
531 // For compatibility, convert raw coordinates into "oriented screen space". Once app
532 // developers are educated about getRaw, we can consider removing this.
Prabir Pradhan9f388812021-05-13 16:54:53 -0700533 const vec2 xy = shouldDisregardWindowTranslation(mSource)
534 ? rotatePoint(mTransform, coords->getX(), coords->getY())
535 : rotatePoint(mTransform, coords->getX(), coords->getY(), mDisplayWidth,
536 mDisplayHeight);
Prabir Pradhan6b384612021-05-14 16:56:25 -0700537 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
538 return xy[axis];
Evan Rosky84f07f02021-04-16 10:42:42 -0700539 }
540
Prabir Pradhanc6523582021-05-14 18:02:55 -0700541 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
542 // For compatibility, since we convert raw coordinates into "oriented screen space", we
543 // need to convert the relative axes into the same orientation for consistency.
544 const vec2 relativeXy =
545 rotatePoint(mTransform, coords->getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
546 coords->getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
547 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
548 }
549
Prabir Pradhan6b384612021-05-14 16:56:25 -0700550 return coords->getAxisValue(axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700551}
552
553float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan9f388812021-05-13 16:54:53 -0700554 size_t historicalIndex) const {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700555 const PointerCoords* coords = getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
556
557 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
Prabir Pradhan9f388812021-05-13 16:54:53 -0700558 const vec2 xy = shouldDisregardWindowTranslation(mSource)
559 ? applyTransformWithoutTranslation(mTransform, coords->getX(), coords->getY())
560 : mTransform.transform(coords->getXYValue());
Prabir Pradhan6b384612021-05-14 16:56:25 -0700561 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
562 return xy[axis];
chaviw9eaa22c2020-07-01 16:21:27 -0700563 }
564
Prabir Pradhanc6523582021-05-14 18:02:55 -0700565 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
566 const vec2 relativeXy =
567 applyTransformWithoutTranslation(mTransform,
568 coords->getAxisValue(
569 AMOTION_EVENT_AXIS_RELATIVE_X),
570 coords->getAxisValue(
571 AMOTION_EVENT_AXIS_RELATIVE_Y));
572 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
573 }
574
Prabir Pradhan6b384612021-05-14 16:56:25 -0700575 return coords->getAxisValue(axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700576}
577
578ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
579 size_t pointerCount = mPointerProperties.size();
580 for (size_t i = 0; i < pointerCount; i++) {
581 if (mPointerProperties.itemAt(i).id == pointerId) {
582 return i;
583 }
584 }
585 return -1;
586}
587
588void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700589 float currXOffset = mTransform.tx();
590 float currYOffset = mTransform.ty();
591 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700592}
593
Robert Carre07e1032018-11-26 12:55:53 -0800594void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700595 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800596 mXPrecision *= globalScaleFactor;
597 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700598
599 size_t numSamples = mSamplePointerCoords.size();
600 for (size_t i = 0; i < numSamples; i++) {
chaviw9eaa22c2020-07-01 16:21:27 -0700601 mSamplePointerCoords.editItemAt(i).scale(globalScaleFactor, globalScaleFactor,
602 globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700603 }
604}
605
chaviw9eaa22c2020-07-01 16:21:27 -0700606void MotionEvent::transform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700607 // We want to preserve the raw axes values stored in the PointerCoords, so we just update the
608 // transform using the values passed in.
chaviw9eaa22c2020-07-01 16:21:27 -0700609 ui::Transform newTransform;
610 newTransform.set(matrix);
611 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700612
Prabir Pradhan6b384612021-05-14 16:56:25 -0700613 // We need to update the AXIS_ORIENTATION value here to maintain the old behavior where the
614 // orientation angle is not affected by the initial transformation set in the MotionEvent.
615 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
616 [&newTransform](PointerCoords& c) {
617 float orientation = c.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
618 c.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
619 transformAngle(newTransform, orientation));
620 });
Jeff Brown5912f952013-07-01 19:10:31 -0700621}
622
Evan Roskyd4d4d802021-05-03 20:12:21 -0700623void MotionEvent::applyTransform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700624 ui::Transform transform;
625 transform.set(matrix);
Evan Roskyd4d4d802021-05-03 20:12:21 -0700626
627 // Apply the transformation to all samples.
Prabir Pradhan6b384612021-05-14 16:56:25 -0700628 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
629 [&transform](PointerCoords& c) { c.transform(transform); });
Prabir Pradhan4b19bd02021-06-01 17:34:59 -0700630
631 if (mRawXCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION &&
632 mRawYCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) {
633 const vec2 cursor = transform.transform(mRawXCursorPosition, mRawYCursorPosition);
634 mRawXCursorPosition = cursor.x;
635 mRawYCursorPosition = cursor.y;
636 }
Evan Roskyd4d4d802021-05-03 20:12:21 -0700637}
638
Brett Chabotfaa986c2020-11-04 17:39:36 -0800639#ifdef __linux__
chaviw9eaa22c2020-07-01 16:21:27 -0700640static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
641 float dsdx, dtdx, tx, dtdy, dsdy, ty;
642 status_t status = parcel.readFloat(&dsdx);
643 status |= parcel.readFloat(&dtdx);
644 status |= parcel.readFloat(&tx);
645 status |= parcel.readFloat(&dtdy);
646 status |= parcel.readFloat(&dsdy);
647 status |= parcel.readFloat(&ty);
648
649 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
650 return status;
651}
652
653static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
654 status_t status = parcel.writeFloat(transform.dsdx());
655 status |= parcel.writeFloat(transform.dtdx());
656 status |= parcel.writeFloat(transform.tx());
657 status |= parcel.writeFloat(transform.dtdy());
658 status |= parcel.writeFloat(transform.dsdy());
659 status |= parcel.writeFloat(transform.ty());
660 return status;
661}
662
Jeff Brown5912f952013-07-01 19:10:31 -0700663status_t MotionEvent::readFromParcel(Parcel* parcel) {
664 size_t pointerCount = parcel->readInt32();
665 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800666 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
667 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700668 return BAD_VALUE;
669 }
670
Garfield Tan4cc839f2020-01-24 11:26:14 -0800671 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700672 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600673 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800674 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600675 std::vector<uint8_t> hmac;
676 status_t result = parcel->readByteVector(&hmac);
677 if (result != OK || hmac.size() != 32) {
678 return BAD_VALUE;
679 }
680 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700681 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100682 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700683 mFlags = parcel->readInt32();
684 mEdgeFlags = parcel->readInt32();
685 mMetaState = parcel->readInt32();
686 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800687 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700688
689 result = android::readFromParcel(mTransform, *parcel);
690 if (result != OK) {
691 return result;
692 }
Jeff Brown5912f952013-07-01 19:10:31 -0700693 mXPrecision = parcel->readFloat();
694 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700695 mRawXCursorPosition = parcel->readFloat();
696 mRawYCursorPosition = parcel->readFloat();
Evan Rosky84f07f02021-04-16 10:42:42 -0700697 mDisplayWidth = parcel->readInt32();
698 mDisplayHeight = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700699 mDownTime = parcel->readInt64();
700
701 mPointerProperties.clear();
702 mPointerProperties.setCapacity(pointerCount);
703 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500704 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700705 mSamplePointerCoords.clear();
706 mSamplePointerCoords.setCapacity(sampleCount * pointerCount);
707
708 for (size_t i = 0; i < pointerCount; i++) {
709 mPointerProperties.push();
710 PointerProperties& properties = mPointerProperties.editTop();
711 properties.id = parcel->readInt32();
712 properties.toolType = parcel->readInt32();
713 }
714
Dan Austinc94fc452015-09-22 14:22:41 -0700715 while (sampleCount > 0) {
716 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500717 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700718 for (size_t i = 0; i < pointerCount; i++) {
719 mSamplePointerCoords.push();
720 status_t status = mSamplePointerCoords.editTop().readFromParcel(parcel);
721 if (status) {
722 return status;
723 }
724 }
725 }
726 return OK;
727}
728
729status_t MotionEvent::writeToParcel(Parcel* parcel) const {
730 size_t pointerCount = mPointerProperties.size();
731 size_t sampleCount = mSampleEventTimes.size();
732
733 parcel->writeInt32(pointerCount);
734 parcel->writeInt32(sampleCount);
735
Garfield Tan4cc839f2020-01-24 11:26:14 -0800736 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700737 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600738 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800739 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600740 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
741 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700742 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100743 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700744 parcel->writeInt32(mFlags);
745 parcel->writeInt32(mEdgeFlags);
746 parcel->writeInt32(mMetaState);
747 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800748 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700749
750 status_t result = android::writeToParcel(mTransform, *parcel);
751 if (result != OK) {
752 return result;
753 }
Jeff Brown5912f952013-07-01 19:10:31 -0700754 parcel->writeFloat(mXPrecision);
755 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700756 parcel->writeFloat(mRawXCursorPosition);
757 parcel->writeFloat(mRawYCursorPosition);
Evan Rosky84f07f02021-04-16 10:42:42 -0700758 parcel->writeInt32(mDisplayWidth);
759 parcel->writeInt32(mDisplayHeight);
Jeff Brown5912f952013-07-01 19:10:31 -0700760 parcel->writeInt64(mDownTime);
761
762 for (size_t i = 0; i < pointerCount; i++) {
763 const PointerProperties& properties = mPointerProperties.itemAt(i);
764 parcel->writeInt32(properties.id);
765 parcel->writeInt32(properties.toolType);
766 }
767
768 const PointerCoords* pc = mSamplePointerCoords.array();
769 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500770 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700771 for (size_t i = 0; i < pointerCount; i++) {
772 status_t status = (pc++)->writeToParcel(parcel);
773 if (status) {
774 return status;
775 }
776 }
777 }
778 return OK;
779}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800780#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700781
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600782bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Jeff Brown5912f952013-07-01 19:10:31 -0700783 if (source & AINPUT_SOURCE_CLASS_POINTER) {
784 // Specifically excludes HOVER_MOVE and SCROLL.
785 switch (action & AMOTION_EVENT_ACTION_MASK) {
786 case AMOTION_EVENT_ACTION_DOWN:
787 case AMOTION_EVENT_ACTION_MOVE:
788 case AMOTION_EVENT_ACTION_UP:
789 case AMOTION_EVENT_ACTION_POINTER_DOWN:
790 case AMOTION_EVENT_ACTION_POINTER_UP:
791 case AMOTION_EVENT_ACTION_CANCEL:
792 case AMOTION_EVENT_ACTION_OUTSIDE:
793 return true;
794 }
795 }
796 return false;
797}
798
Michael Wright872db4f2014-04-22 15:03:51 -0700799const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700800 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700801}
802
803int32_t MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700804 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700805}
806
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500807std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700808 // Convert MotionEvent action to string
809 switch (action & AMOTION_EVENT_ACTION_MASK) {
810 case AMOTION_EVENT_ACTION_DOWN:
811 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700812 case AMOTION_EVENT_ACTION_UP:
813 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500814 case AMOTION_EVENT_ACTION_MOVE:
815 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700816 case AMOTION_EVENT_ACTION_CANCEL:
817 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500818 case AMOTION_EVENT_ACTION_OUTSIDE:
819 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700820 case AMOTION_EVENT_ACTION_POINTER_DOWN:
821 return "POINTER_DOWN";
822 case AMOTION_EVENT_ACTION_POINTER_UP:
823 return "POINTER_UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500824 case AMOTION_EVENT_ACTION_HOVER_MOVE:
825 return "HOVER_MOVE";
826 case AMOTION_EVENT_ACTION_SCROLL:
827 return "SCROLL";
828 case AMOTION_EVENT_ACTION_HOVER_ENTER:
829 return "HOVER_ENTER";
830 case AMOTION_EVENT_ACTION_HOVER_EXIT:
831 return "HOVER_EXIT";
832 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
833 return "BUTTON_PRESS";
834 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
835 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700836 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500837 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700838}
839
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800840// --- FocusEvent ---
841
Garfield Tan4cc839f2020-01-24 11:26:14 -0800842void FocusEvent::initialize(int32_t id, bool hasFocus, bool inTouchMode) {
843 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600844 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800845 mHasFocus = hasFocus;
846 mInTouchMode = inTouchMode;
847}
848
849void FocusEvent::initialize(const FocusEvent& from) {
850 InputEvent::initialize(from);
851 mHasFocus = from.mHasFocus;
852 mInTouchMode = from.mInTouchMode;
853}
Jeff Brown5912f952013-07-01 19:10:31 -0700854
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800855// --- CaptureEvent ---
856
857void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) {
858 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
859 ADISPLAY_ID_NONE, INVALID_HMAC);
860 mPointerCaptureEnabled = pointerCaptureEnabled;
861}
862
863void CaptureEvent::initialize(const CaptureEvent& from) {
864 InputEvent::initialize(from);
865 mPointerCaptureEnabled = from.mPointerCaptureEnabled;
866}
867
arthurhung7632c332020-12-30 16:58:01 +0800868// --- DragEvent ---
869
870void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) {
871 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
872 ADISPLAY_ID_NONE, INVALID_HMAC);
873 mIsExiting = isExiting;
874 mX = x;
875 mY = y;
876}
877
878void DragEvent::initialize(const DragEvent& from) {
879 InputEvent::initialize(from);
880 mIsExiting = from.mIsExiting;
881 mX = from.mX;
882 mY = from.mY;
883}
884
Jeff Brown5912f952013-07-01 19:10:31 -0700885// --- PooledInputEventFactory ---
886
887PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
888 mMaxPoolSize(maxPoolSize) {
889}
890
891PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -0700892}
893
894KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800895 if (mKeyEventPool.empty()) {
896 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700897 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800898 KeyEvent* event = mKeyEventPool.front().release();
899 mKeyEventPool.pop();
900 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700901}
902
903MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800904 if (mMotionEventPool.empty()) {
905 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700906 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800907 MotionEvent* event = mMotionEventPool.front().release();
908 mMotionEventPool.pop();
909 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700910}
911
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800912FocusEvent* PooledInputEventFactory::createFocusEvent() {
913 if (mFocusEventPool.empty()) {
914 return new FocusEvent();
915 }
916 FocusEvent* event = mFocusEventPool.front().release();
917 mFocusEventPool.pop();
918 return event;
919}
920
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800921CaptureEvent* PooledInputEventFactory::createCaptureEvent() {
922 if (mCaptureEventPool.empty()) {
923 return new CaptureEvent();
924 }
925 CaptureEvent* event = mCaptureEventPool.front().release();
926 mCaptureEventPool.pop();
927 return event;
928}
929
arthurhung7632c332020-12-30 16:58:01 +0800930DragEvent* PooledInputEventFactory::createDragEvent() {
931 if (mDragEventPool.empty()) {
932 return new DragEvent();
933 }
934 DragEvent* event = mDragEventPool.front().release();
935 mDragEventPool.pop();
936 return event;
937}
938
Jeff Brown5912f952013-07-01 19:10:31 -0700939void PooledInputEventFactory::recycle(InputEvent* event) {
940 switch (event->getType()) {
941 case AINPUT_EVENT_TYPE_KEY:
942 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800943 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700944 return;
945 }
946 break;
947 case AINPUT_EVENT_TYPE_MOTION:
948 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800949 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700950 return;
951 }
952 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800953 case AINPUT_EVENT_TYPE_FOCUS:
954 if (mFocusEventPool.size() < mMaxPoolSize) {
955 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
956 return;
957 }
958 break;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800959 case AINPUT_EVENT_TYPE_CAPTURE:
960 if (mCaptureEventPool.size() < mMaxPoolSize) {
961 mCaptureEventPool.push(
962 std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event)));
963 return;
964 }
965 break;
arthurhung7632c332020-12-30 16:58:01 +0800966 case AINPUT_EVENT_TYPE_DRAG:
967 if (mDragEventPool.size() < mMaxPoolSize) {
968 mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event)));
969 return;
970 }
971 break;
Jeff Brown5912f952013-07-01 19:10:31 -0700972 }
973 delete event;
974}
975
976} // namespace android