blob: 82a814f293bd4f979426d3f3b001aba0f13d8fb3 [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
Evan Rosky09576692021-07-01 12:22:09 -070069// Rotates the given point to the specified orientation. If the display width and height are
Prabir Pradhan6b384612021-05-14 16:56:25 -070070// 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.
Evan Rosky09576692021-07-01 12:22:09 -070072vec2 rotatePoint(uint32_t orientation, float x, float y, int32_t displayWidth = 0,
Prabir Pradhan6b384612021-05-14 16:56:25 -070073 int32_t displayHeight = 0) {
Prabir Pradhan6b384612021-05-14 16:56:25 -070074 if (orientation == ui::Transform::ROT_0) {
75 return {x, y};
76 }
77
78 vec2 xy(x, y);
79 if (orientation == ui::Transform::ROT_90) {
80 xy.x = displayHeight - y;
81 xy.y = x;
82 } else if (orientation == ui::Transform::ROT_180) {
83 xy.x = displayWidth - x;
84 xy.y = displayHeight - y;
85 } else if (orientation == ui::Transform::ROT_270) {
86 xy.x = y;
87 xy.y = displayWidth - x;
88 }
89 return xy;
90}
91
Prabir Pradhan9f388812021-05-13 16:54:53 -070092vec2 applyTransformWithoutTranslation(const ui::Transform& transform, float x, float y) {
93 const vec2 transformedXy = transform.transform(x, y);
94 const vec2 transformedOrigin = transform.transform(0, 0);
95 return transformedXy - transformedOrigin;
96}
97
98bool shouldDisregardWindowTranslation(uint32_t source) {
99 // Pointer events are the only type of events that refer to absolute coordinates on the display,
100 // so we should apply the entire window transform. For other types of events, we should make
101 // sure to not apply the window translation/offset.
102 return (source & AINPUT_SOURCE_CLASS_POINTER) == 0;
103}
104
Prabir Pradhan6b384612021-05-14 16:56:25 -0700105} // namespace
106
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800107const char* motionClassificationToString(MotionClassification classification) {
108 switch (classification) {
109 case MotionClassification::NONE:
110 return "NONE";
111 case MotionClassification::AMBIGUOUS_GESTURE:
112 return "AMBIGUOUS_GESTURE";
113 case MotionClassification::DEEP_PRESS:
114 return "DEEP_PRESS";
115 }
116}
117
Garfield Tan84b087e2020-01-23 10:49:05 -0800118// --- IdGenerator ---
119IdGenerator::IdGenerator(Source source) : mSource(source) {}
120
121int32_t IdGenerator::nextId() const {
122 constexpr uint32_t SEQUENCE_NUMBER_MASK = ~SOURCE_MASK;
123 int32_t id = 0;
124
125// Avoid building against syscall getrandom(2) on host, which will fail build on Mac. Host doesn't
126// use sequence number so just always return mSource.
127#ifdef __ANDROID__
128 constexpr size_t BUF_LEN = sizeof(id);
129 size_t totalBytes = 0;
130 while (totalBytes < BUF_LEN) {
131 ssize_t bytes = TEMP_FAILURE_RETRY(getrandom(&id, BUF_LEN, GRND_NONBLOCK));
132 if (CC_UNLIKELY(bytes < 0)) {
133 ALOGW("Failed to fill in random number for sequence number: %s.", strerror(errno));
134 id = 0;
135 break;
136 }
137 totalBytes += bytes;
138 }
139#endif // __ANDROID__
140
141 return (id & SEQUENCE_NUMBER_MASK) | static_cast<int32_t>(mSource);
142}
143
Jeff Brown5912f952013-07-01 19:10:31 -0700144// --- InputEvent ---
145
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800146const char* inputEventTypeToString(int32_t type) {
147 switch (type) {
148 case AINPUT_EVENT_TYPE_KEY: {
149 return "KEY";
150 }
151 case AINPUT_EVENT_TYPE_MOTION: {
152 return "MOTION";
153 }
154 case AINPUT_EVENT_TYPE_FOCUS: {
155 return "FOCUS";
156 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800157 case AINPUT_EVENT_TYPE_CAPTURE: {
158 return "CAPTURE";
159 }
arthurhung7632c332020-12-30 16:58:01 +0800160 case AINPUT_EVENT_TYPE_DRAG: {
161 return "DRAG";
162 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800163 }
164 return "UNKNOWN";
165}
166
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800167VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
168 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
169 event.getSource(), event.getDisplayId()},
170 event.getAction(),
171 event.getDownTime(),
172 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
173 event.getKeyCode(),
174 event.getScanCode(),
175 event.getMetaState(),
176 event.getRepeatCount()};
177}
178
179VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
180 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
181 event.getSource(), event.getDisplayId()},
182 event.getRawX(0),
183 event.getRawY(0),
184 event.getActionMasked(),
185 event.getDownTime(),
186 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
187 event.getMetaState(),
188 event.getButtonState()};
189}
190
Garfield Tan4cc839f2020-01-24 11:26:14 -0800191void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600192 std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800193 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700194 mDeviceId = deviceId;
195 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100196 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600197 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700198}
199
200void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800201 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700202 mDeviceId = from.mDeviceId;
203 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100204 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600205 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700206}
207
Garfield Tan4cc839f2020-01-24 11:26:14 -0800208int32_t InputEvent::nextId() {
209 static IdGenerator idGen(IdGenerator::Source::OTHER);
210 return idGen.nextId();
211}
212
Jeff Brown5912f952013-07-01 19:10:31 -0700213// --- KeyEvent ---
214
Michael Wright872db4f2014-04-22 15:03:51 -0700215const char* KeyEvent::getLabel(int32_t keyCode) {
Chris Ye4958d062020-08-20 13:21:10 -0700216 return InputEventLookup::getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700217}
218
Michael Wright872db4f2014-04-22 15:03:51 -0700219int32_t KeyEvent::getKeyCodeFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700220 return InputEventLookup::getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700221}
222
Garfield Tan4cc839f2020-01-24 11:26:14 -0800223void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600224 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
225 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
226 nsecs_t downTime, nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800227 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700228 mAction = action;
229 mFlags = flags;
230 mKeyCode = keyCode;
231 mScanCode = scanCode;
232 mMetaState = metaState;
233 mRepeatCount = repeatCount;
234 mDownTime = downTime;
235 mEventTime = eventTime;
236}
237
238void KeyEvent::initialize(const KeyEvent& from) {
239 InputEvent::initialize(from);
240 mAction = from.mAction;
241 mFlags = from.mFlags;
242 mKeyCode = from.mKeyCode;
243 mScanCode = from.mScanCode;
244 mMetaState = from.mMetaState;
245 mRepeatCount = from.mRepeatCount;
246 mDownTime = from.mDownTime;
247 mEventTime = from.mEventTime;
248}
249
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700250const char* KeyEvent::actionToString(int32_t action) {
251 // Convert KeyEvent action to string
252 switch (action) {
253 case AKEY_EVENT_ACTION_DOWN:
254 return "DOWN";
255 case AKEY_EVENT_ACTION_UP:
256 return "UP";
257 case AKEY_EVENT_ACTION_MULTIPLE:
258 return "MULTIPLE";
259 }
260 return "UNKNOWN";
261}
Jeff Brown5912f952013-07-01 19:10:31 -0700262
263// --- PointerCoords ---
264
265float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700266 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700267 return 0;
268 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700269 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700270}
271
272status_t PointerCoords::setAxisValue(int32_t axis, float value) {
273 if (axis < 0 || axis > 63) {
274 return NAME_NOT_FOUND;
275 }
276
Michael Wright38dcdff2014-03-19 12:06:10 -0700277 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
278 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700279 if (value == 0) {
280 return OK; // axes with value 0 do not need to be stored
281 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700282
283 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700284 if (count >= MAX_AXES) {
285 tooManyAxes(axis);
286 return NO_MEMORY;
287 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700288 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700289 for (uint32_t i = count; i > index; i--) {
290 values[i] = values[i - 1];
291 }
292 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700293
Jeff Brown5912f952013-07-01 19:10:31 -0700294 values[index] = value;
295 return OK;
296}
297
298static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
299 float value = c.getAxisValue(axis);
300 if (value != 0) {
301 c.setAxisValue(axis, value * scaleFactor);
302 }
303}
304
Robert Carre07e1032018-11-26 12:55:53 -0800305void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700306 // No need to scale pressure or size since they are normalized.
307 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800308
309 // If there is a global scale factor, it is included in the windowX/YScale
310 // so we don't need to apply it twice to the X/Y axes.
311 // However we don't want to apply any windowXYScale not included in the global scale
312 // to the TOUCH_MAJOR/MINOR coordinates.
313 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
314 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
315 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
316 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
317 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
318 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
Prabir Pradhanc6523582021-05-14 18:02:55 -0700319 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_X, windowXScale);
320 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_Y, windowYScale);
Robert Carre07e1032018-11-26 12:55:53 -0800321}
322
Jeff Brownf086ddb2014-02-11 14:28:48 -0800323void PointerCoords::applyOffset(float xOffset, float yOffset) {
324 setAxisValue(AMOTION_EVENT_AXIS_X, getX() + xOffset);
325 setAxisValue(AMOTION_EVENT_AXIS_Y, getY() + yOffset);
326}
327
Brett Chabotfaa986c2020-11-04 17:39:36 -0800328#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700329status_t PointerCoords::readFromParcel(Parcel* parcel) {
330 bits = parcel->readInt64();
331
Michael Wright38dcdff2014-03-19 12:06:10 -0700332 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700333 if (count > MAX_AXES) {
334 return BAD_VALUE;
335 }
336
337 for (uint32_t i = 0; i < count; i++) {
338 values[i] = parcel->readFloat();
339 }
340 return OK;
341}
342
343status_t PointerCoords::writeToParcel(Parcel* parcel) const {
344 parcel->writeInt64(bits);
345
Michael Wright38dcdff2014-03-19 12:06:10 -0700346 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700347 for (uint32_t i = 0; i < count; i++) {
348 parcel->writeFloat(values[i]);
349 }
350 return OK;
351}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800352#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700353
354void PointerCoords::tooManyAxes(int axis) {
355 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
356 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
357}
358
359bool PointerCoords::operator==(const PointerCoords& other) const {
360 if (bits != other.bits) {
361 return false;
362 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700363 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700364 for (uint32_t i = 0; i < count; i++) {
365 if (values[i] != other.values[i]) {
366 return false;
367 }
368 }
369 return true;
370}
371
372void PointerCoords::copyFrom(const PointerCoords& other) {
373 bits = other.bits;
Michael Wright38dcdff2014-03-19 12:06:10 -0700374 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700375 for (uint32_t i = 0; i < count; i++) {
376 values[i] = other.values[i];
377 }
378}
379
chaviwc01e1372020-07-01 12:37:31 -0700380void PointerCoords::transform(const ui::Transform& transform) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700381 const vec2 xy = transform.transform(getXYValue());
382 setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
383 setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
384
Prabir Pradhanc6523582021-05-14 18:02:55 -0700385 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_X) ||
386 BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_Y)) {
387 const ui::Transform rotation(transform.getOrientation());
388 const vec2 relativeXy = rotation.transform(getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
389 getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
390 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
391 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
392 }
393
Prabir Pradhan6b384612021-05-14 16:56:25 -0700394 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) {
395 const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
396 setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val));
397 }
chaviwc01e1372020-07-01 12:37:31 -0700398}
Jeff Brown5912f952013-07-01 19:10:31 -0700399
400// --- PointerProperties ---
401
402bool PointerProperties::operator==(const PointerProperties& other) const {
403 return id == other.id
404 && toolType == other.toolType;
405}
406
407void PointerProperties::copyFrom(const PointerProperties& other) {
408 id = other.id;
409 toolType = other.toolType;
410}
411
412
413// --- MotionEvent ---
414
Garfield Tan4cc839f2020-01-24 11:26:14 -0800415void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600416 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
417 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700418 int32_t buttonState, MotionClassification classification,
419 const ui::Transform& transform, float xPrecision, float yPrecision,
Evan Rosky84f07f02021-04-16 10:42:42 -0700420 float rawXCursorPosition, float rawYCursorPosition,
Evan Rosky09576692021-07-01 12:22:09 -0700421 uint32_t displayOrientation, int32_t displayWidth,
422 int32_t displayHeight, nsecs_t downTime, nsecs_t eventTime,
423 size_t pointerCount, const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700424 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800425 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700426 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100427 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700428 mFlags = flags;
429 mEdgeFlags = edgeFlags;
430 mMetaState = metaState;
431 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800432 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700433 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700434 mXPrecision = xPrecision;
435 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700436 mRawXCursorPosition = rawXCursorPosition;
437 mRawYCursorPosition = rawYCursorPosition;
Evan Rosky09576692021-07-01 12:22:09 -0700438 mDisplayOrientation = displayOrientation;
Evan Rosky84f07f02021-04-16 10:42:42 -0700439 mDisplayWidth = displayWidth;
440 mDisplayHeight = displayHeight;
Jeff Brown5912f952013-07-01 19:10:31 -0700441 mDownTime = downTime;
442 mPointerProperties.clear();
443 mPointerProperties.appendArray(pointerProperties, pointerCount);
444 mSampleEventTimes.clear();
445 mSamplePointerCoords.clear();
446 addSample(eventTime, pointerCoords);
447}
448
449void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800450 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
451 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700452 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100453 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700454 mFlags = other->mFlags;
455 mEdgeFlags = other->mEdgeFlags;
456 mMetaState = other->mMetaState;
457 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800458 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700459 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700460 mXPrecision = other->mXPrecision;
461 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700462 mRawXCursorPosition = other->mRawXCursorPosition;
463 mRawYCursorPosition = other->mRawYCursorPosition;
Evan Rosky09576692021-07-01 12:22:09 -0700464 mDisplayOrientation = other->mDisplayOrientation;
Evan Rosky84f07f02021-04-16 10:42:42 -0700465 mDisplayWidth = other->mDisplayWidth;
466 mDisplayHeight = other->mDisplayHeight;
Jeff Brown5912f952013-07-01 19:10:31 -0700467 mDownTime = other->mDownTime;
468 mPointerProperties = other->mPointerProperties;
469
470 if (keepHistory) {
471 mSampleEventTimes = other->mSampleEventTimes;
472 mSamplePointerCoords = other->mSamplePointerCoords;
473 } else {
474 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500475 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700476 mSamplePointerCoords.clear();
477 size_t pointerCount = other->getPointerCount();
478 size_t historySize = other->getHistorySize();
479 mSamplePointerCoords.appendArray(other->mSamplePointerCoords.array()
480 + (historySize * pointerCount), pointerCount);
481 }
482}
483
484void MotionEvent::addSample(
485 int64_t eventTime,
486 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500487 mSampleEventTimes.push_back(eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -0700488 mSamplePointerCoords.appendArray(pointerCoords, getPointerCount());
489}
490
Garfield Tan00f511d2019-06-12 16:55:40 -0700491float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700492 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
493 return vals.x;
Garfield Tan00f511d2019-06-12 16:55:40 -0700494}
495
496float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700497 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
498 return vals.y;
Garfield Tan00f511d2019-06-12 16:55:40 -0700499}
500
Garfield Tan937bb832019-07-25 17:48:31 -0700501void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700502 ui::Transform inverse = mTransform.inverse();
503 vec2 vals = inverse.transform(x, y);
504 mRawXCursorPosition = vals.x;
505 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700506}
507
Jeff Brown5912f952013-07-01 19:10:31 -0700508const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
509 return &mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
510}
511
512float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
Evan Rosky84f07f02021-04-16 10:42:42 -0700513 return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700514}
515
516float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700517 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700518}
519
520const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
521 size_t pointerIndex, size_t historicalIndex) const {
522 return &mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
523}
524
525float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan6b384612021-05-14 16:56:25 -0700526 size_t historicalIndex) const {
527 const PointerCoords* coords = getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
528
529 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
530 // For compatibility, convert raw coordinates into "oriented screen space". Once app
531 // developers are educated about getRaw, we can consider removing this.
Prabir Pradhan9f388812021-05-13 16:54:53 -0700532 const vec2 xy = shouldDisregardWindowTranslation(mSource)
Evan Rosky09576692021-07-01 12:22:09 -0700533 ? rotatePoint(mDisplayOrientation, coords->getX(), coords->getY())
534 : rotatePoint(mDisplayOrientation, coords->getX(), coords->getY(), mDisplayWidth,
Prabir Pradhan9f388812021-05-13 16:54:53 -0700535 mDisplayHeight);
Prabir Pradhan6b384612021-05-14 16:56:25 -0700536 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
537 return xy[axis];
Evan Rosky84f07f02021-04-16 10:42:42 -0700538 }
539
Prabir Pradhanc6523582021-05-14 18:02:55 -0700540 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
541 // For compatibility, since we convert raw coordinates into "oriented screen space", we
542 // need to convert the relative axes into the same orientation for consistency.
Evan Rosky09576692021-07-01 12:22:09 -0700543 const vec2 relativeXy = rotatePoint(mDisplayOrientation,
544 coords->getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
545 coords->getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
Prabir Pradhanc6523582021-05-14 18:02:55 -0700546 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
547 }
548
Prabir Pradhan6b384612021-05-14 16:56:25 -0700549 return coords->getAxisValue(axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700550}
551
552float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan9f388812021-05-13 16:54:53 -0700553 size_t historicalIndex) const {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700554 const PointerCoords* coords = getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
555
556 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
Prabir Pradhan9f388812021-05-13 16:54:53 -0700557 const vec2 xy = shouldDisregardWindowTranslation(mSource)
558 ? applyTransformWithoutTranslation(mTransform, coords->getX(), coords->getY())
559 : mTransform.transform(coords->getXYValue());
Prabir Pradhan6b384612021-05-14 16:56:25 -0700560 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
561 return xy[axis];
chaviw9eaa22c2020-07-01 16:21:27 -0700562 }
563
Prabir Pradhanc6523582021-05-14 18:02:55 -0700564 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
565 const vec2 relativeXy =
566 applyTransformWithoutTranslation(mTransform,
567 coords->getAxisValue(
568 AMOTION_EVENT_AXIS_RELATIVE_X),
569 coords->getAxisValue(
570 AMOTION_EVENT_AXIS_RELATIVE_Y));
571 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
572 }
573
Prabir Pradhan6b384612021-05-14 16:56:25 -0700574 return coords->getAxisValue(axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700575}
576
577ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
578 size_t pointerCount = mPointerProperties.size();
579 for (size_t i = 0; i < pointerCount; i++) {
580 if (mPointerProperties.itemAt(i).id == pointerId) {
581 return i;
582 }
583 }
584 return -1;
585}
586
587void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700588 float currXOffset = mTransform.tx();
589 float currYOffset = mTransform.ty();
590 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700591}
592
Robert Carre07e1032018-11-26 12:55:53 -0800593void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700594 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800595 mXPrecision *= globalScaleFactor;
596 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700597
598 size_t numSamples = mSamplePointerCoords.size();
599 for (size_t i = 0; i < numSamples; i++) {
chaviw9eaa22c2020-07-01 16:21:27 -0700600 mSamplePointerCoords.editItemAt(i).scale(globalScaleFactor, globalScaleFactor,
601 globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700602 }
603}
604
chaviw9eaa22c2020-07-01 16:21:27 -0700605void MotionEvent::transform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700606 // We want to preserve the raw axes values stored in the PointerCoords, so we just update the
607 // transform using the values passed in.
chaviw9eaa22c2020-07-01 16:21:27 -0700608 ui::Transform newTransform;
609 newTransform.set(matrix);
610 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700611
Prabir Pradhan6b384612021-05-14 16:56:25 -0700612 // We need to update the AXIS_ORIENTATION value here to maintain the old behavior where the
613 // orientation angle is not affected by the initial transformation set in the MotionEvent.
614 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
615 [&newTransform](PointerCoords& c) {
616 float orientation = c.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
617 c.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
618 transformAngle(newTransform, orientation));
619 });
Jeff Brown5912f952013-07-01 19:10:31 -0700620}
621
Evan Roskyd4d4d802021-05-03 20:12:21 -0700622void MotionEvent::applyTransform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700623 ui::Transform transform;
624 transform.set(matrix);
Evan Roskyd4d4d802021-05-03 20:12:21 -0700625
626 // Apply the transformation to all samples.
Prabir Pradhan6b384612021-05-14 16:56:25 -0700627 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
628 [&transform](PointerCoords& c) { c.transform(transform); });
Prabir Pradhan4b19bd02021-06-01 17:34:59 -0700629
630 if (mRawXCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION &&
631 mRawYCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) {
632 const vec2 cursor = transform.transform(mRawXCursorPosition, mRawYCursorPosition);
633 mRawXCursorPosition = cursor.x;
634 mRawYCursorPosition = cursor.y;
635 }
Evan Roskyd4d4d802021-05-03 20:12:21 -0700636}
637
Brett Chabotfaa986c2020-11-04 17:39:36 -0800638#ifdef __linux__
chaviw9eaa22c2020-07-01 16:21:27 -0700639static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
640 float dsdx, dtdx, tx, dtdy, dsdy, ty;
641 status_t status = parcel.readFloat(&dsdx);
642 status |= parcel.readFloat(&dtdx);
643 status |= parcel.readFloat(&tx);
644 status |= parcel.readFloat(&dtdy);
645 status |= parcel.readFloat(&dsdy);
646 status |= parcel.readFloat(&ty);
647
648 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
649 return status;
650}
651
652static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
653 status_t status = parcel.writeFloat(transform.dsdx());
654 status |= parcel.writeFloat(transform.dtdx());
655 status |= parcel.writeFloat(transform.tx());
656 status |= parcel.writeFloat(transform.dtdy());
657 status |= parcel.writeFloat(transform.dsdy());
658 status |= parcel.writeFloat(transform.ty());
659 return status;
660}
661
Jeff Brown5912f952013-07-01 19:10:31 -0700662status_t MotionEvent::readFromParcel(Parcel* parcel) {
663 size_t pointerCount = parcel->readInt32();
664 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800665 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
666 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700667 return BAD_VALUE;
668 }
669
Garfield Tan4cc839f2020-01-24 11:26:14 -0800670 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700671 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600672 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800673 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600674 std::vector<uint8_t> hmac;
675 status_t result = parcel->readByteVector(&hmac);
676 if (result != OK || hmac.size() != 32) {
677 return BAD_VALUE;
678 }
679 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700680 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100681 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700682 mFlags = parcel->readInt32();
683 mEdgeFlags = parcel->readInt32();
684 mMetaState = parcel->readInt32();
685 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800686 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700687
688 result = android::readFromParcel(mTransform, *parcel);
689 if (result != OK) {
690 return result;
691 }
Jeff Brown5912f952013-07-01 19:10:31 -0700692 mXPrecision = parcel->readFloat();
693 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700694 mRawXCursorPosition = parcel->readFloat();
695 mRawYCursorPosition = parcel->readFloat();
Evan Rosky09576692021-07-01 12:22:09 -0700696 mDisplayOrientation = parcel->readUint32();
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 Rosky09576692021-07-01 12:22:09 -0700758 parcel->writeUint32(mDisplayOrientation);
Evan Rosky84f07f02021-04-16 10:42:42 -0700759 parcel->writeInt32(mDisplayWidth);
760 parcel->writeInt32(mDisplayHeight);
Jeff Brown5912f952013-07-01 19:10:31 -0700761 parcel->writeInt64(mDownTime);
762
763 for (size_t i = 0; i < pointerCount; i++) {
764 const PointerProperties& properties = mPointerProperties.itemAt(i);
765 parcel->writeInt32(properties.id);
766 parcel->writeInt32(properties.toolType);
767 }
768
769 const PointerCoords* pc = mSamplePointerCoords.array();
770 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500771 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700772 for (size_t i = 0; i < pointerCount; i++) {
773 status_t status = (pc++)->writeToParcel(parcel);
774 if (status) {
775 return status;
776 }
777 }
778 }
779 return OK;
780}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800781#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700782
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600783bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Jeff Brown5912f952013-07-01 19:10:31 -0700784 if (source & AINPUT_SOURCE_CLASS_POINTER) {
785 // Specifically excludes HOVER_MOVE and SCROLL.
786 switch (action & AMOTION_EVENT_ACTION_MASK) {
787 case AMOTION_EVENT_ACTION_DOWN:
788 case AMOTION_EVENT_ACTION_MOVE:
789 case AMOTION_EVENT_ACTION_UP:
790 case AMOTION_EVENT_ACTION_POINTER_DOWN:
791 case AMOTION_EVENT_ACTION_POINTER_UP:
792 case AMOTION_EVENT_ACTION_CANCEL:
793 case AMOTION_EVENT_ACTION_OUTSIDE:
794 return true;
795 }
796 }
797 return false;
798}
799
Michael Wright872db4f2014-04-22 15:03:51 -0700800const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700801 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700802}
803
804int32_t MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700805 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700806}
807
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500808std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700809 // Convert MotionEvent action to string
810 switch (action & AMOTION_EVENT_ACTION_MASK) {
811 case AMOTION_EVENT_ACTION_DOWN:
812 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700813 case AMOTION_EVENT_ACTION_UP:
814 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500815 case AMOTION_EVENT_ACTION_MOVE:
816 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700817 case AMOTION_EVENT_ACTION_CANCEL:
818 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500819 case AMOTION_EVENT_ACTION_OUTSIDE:
820 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700821 case AMOTION_EVENT_ACTION_POINTER_DOWN:
822 return "POINTER_DOWN";
823 case AMOTION_EVENT_ACTION_POINTER_UP:
824 return "POINTER_UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500825 case AMOTION_EVENT_ACTION_HOVER_MOVE:
826 return "HOVER_MOVE";
827 case AMOTION_EVENT_ACTION_SCROLL:
828 return "SCROLL";
829 case AMOTION_EVENT_ACTION_HOVER_ENTER:
830 return "HOVER_ENTER";
831 case AMOTION_EVENT_ACTION_HOVER_EXIT:
832 return "HOVER_EXIT";
833 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
834 return "BUTTON_PRESS";
835 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
836 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700837 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500838 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700839}
840
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800841// --- FocusEvent ---
842
Garfield Tan4cc839f2020-01-24 11:26:14 -0800843void FocusEvent::initialize(int32_t id, bool hasFocus, bool inTouchMode) {
844 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600845 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800846 mHasFocus = hasFocus;
847 mInTouchMode = inTouchMode;
848}
849
850void FocusEvent::initialize(const FocusEvent& from) {
851 InputEvent::initialize(from);
852 mHasFocus = from.mHasFocus;
853 mInTouchMode = from.mInTouchMode;
854}
Jeff Brown5912f952013-07-01 19:10:31 -0700855
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800856// --- CaptureEvent ---
857
858void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) {
859 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
860 ADISPLAY_ID_NONE, INVALID_HMAC);
861 mPointerCaptureEnabled = pointerCaptureEnabled;
862}
863
864void CaptureEvent::initialize(const CaptureEvent& from) {
865 InputEvent::initialize(from);
866 mPointerCaptureEnabled = from.mPointerCaptureEnabled;
867}
868
arthurhung7632c332020-12-30 16:58:01 +0800869// --- DragEvent ---
870
871void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) {
872 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
873 ADISPLAY_ID_NONE, INVALID_HMAC);
874 mIsExiting = isExiting;
875 mX = x;
876 mY = y;
877}
878
879void DragEvent::initialize(const DragEvent& from) {
880 InputEvent::initialize(from);
881 mIsExiting = from.mIsExiting;
882 mX = from.mX;
883 mY = from.mY;
884}
885
Jeff Brown5912f952013-07-01 19:10:31 -0700886// --- PooledInputEventFactory ---
887
888PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
889 mMaxPoolSize(maxPoolSize) {
890}
891
892PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -0700893}
894
895KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800896 if (mKeyEventPool.empty()) {
897 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700898 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800899 KeyEvent* event = mKeyEventPool.front().release();
900 mKeyEventPool.pop();
901 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700902}
903
904MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800905 if (mMotionEventPool.empty()) {
906 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700907 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800908 MotionEvent* event = mMotionEventPool.front().release();
909 mMotionEventPool.pop();
910 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700911}
912
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800913FocusEvent* PooledInputEventFactory::createFocusEvent() {
914 if (mFocusEventPool.empty()) {
915 return new FocusEvent();
916 }
917 FocusEvent* event = mFocusEventPool.front().release();
918 mFocusEventPool.pop();
919 return event;
920}
921
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800922CaptureEvent* PooledInputEventFactory::createCaptureEvent() {
923 if (mCaptureEventPool.empty()) {
924 return new CaptureEvent();
925 }
926 CaptureEvent* event = mCaptureEventPool.front().release();
927 mCaptureEventPool.pop();
928 return event;
929}
930
arthurhung7632c332020-12-30 16:58:01 +0800931DragEvent* PooledInputEventFactory::createDragEvent() {
932 if (mDragEventPool.empty()) {
933 return new DragEvent();
934 }
935 DragEvent* event = mDragEventPool.front().release();
936 mDragEventPool.pop();
937 return event;
938}
939
Jeff Brown5912f952013-07-01 19:10:31 -0700940void PooledInputEventFactory::recycle(InputEvent* event) {
941 switch (event->getType()) {
942 case AINPUT_EVENT_TYPE_KEY:
943 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800944 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700945 return;
946 }
947 break;
948 case AINPUT_EVENT_TYPE_MOTION:
949 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800950 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700951 return;
952 }
953 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800954 case AINPUT_EVENT_TYPE_FOCUS:
955 if (mFocusEventPool.size() < mMaxPoolSize) {
956 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
957 return;
958 }
959 break;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800960 case AINPUT_EVENT_TYPE_CAPTURE:
961 if (mCaptureEventPool.size() < mMaxPoolSize) {
962 mCaptureEventPool.push(
963 std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event)));
964 return;
965 }
966 break;
arthurhung7632c332020-12-30 16:58:01 +0800967 case AINPUT_EVENT_TYPE_DRAG:
968 if (mDragEventPool.size() < mMaxPoolSize) {
969 mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event)));
970 return;
971 }
972 break;
Jeff Brown5912f952013-07-01 19:10:31 -0700973 }
974 delete event;
975}
976
977} // namespace android