blob: f1ccce1b9f668ba66dbbc7b310feceee060f4a7d [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>
Jeff Brown5912f952013-07-01 19:10:31 -070027#include <input/Input.h>
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080028#include <input/InputDevice.h>
Michael Wright872db4f2014-04-22 15:03:51 -070029#include <input/InputEventLabels.h>
Jeff Brown5912f952013-07-01 19:10:31 -070030
Brett Chabotfaa986c2020-11-04 17:39:36 -080031#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -070032#include <binder/Parcel.h>
Brett Chabotfaa986c2020-11-04 17:39:36 -080033#endif
Brett Chabot58208522020-09-09 13:55:24 -070034#ifdef __ANDROID__
Garfield Tan84b087e2020-01-23 10:49:05 -080035#include <sys/random.h>
Jeff Brown5912f952013-07-01 19:10:31 -070036#endif
37
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050038using android::base::StringPrintf;
39
Jeff Brown5912f952013-07-01 19:10:31 -070040namespace android {
41
Prabir Pradhan6b384612021-05-14 16:56:25 -070042namespace {
43
44float transformAngle(const ui::Transform& transform, float angleRadians) {
45 // Construct and transform a vector oriented at the specified clockwise angle from vertical.
46 // Coordinate system: down is increasing Y, right is increasing X.
47 float x = sinf(angleRadians);
48 float y = -cosf(angleRadians);
49 vec2 transformedPoint = transform.transform(x, y);
50
51 // Determine how the origin is transformed by the matrix so that we
52 // can transform orientation vectors.
53 const vec2 origin = transform.transform(0, 0);
54
55 transformedPoint.x -= origin.x;
56 transformedPoint.y -= origin.y;
57
58 // Derive the transformed vector's clockwise angle from vertical.
59 float result = atan2f(transformedPoint.x, -transformedPoint.y);
60 if (result < -M_PI_2) {
61 result += M_PI;
62 } else if (result > M_PI_2) {
63 result -= M_PI;
64 }
65 return result;
66}
67
68// Rotates the given point to the transform's orientation. If the display width and height are
69// provided, the point is rotated in the screen space. Otherwise, the point is rotated about the
70// origin. This helper is used to avoid the extra overhead of creating new Transforms.
71vec2 rotatePoint(const ui::Transform& transform, float x, float y, int32_t displayWidth = 0,
72 int32_t displayHeight = 0) {
73 // 0x7 encapsulates all 3 rotations (see ui::Transform::RotationFlags)
74 static const int ALL_ROTATIONS_MASK = 0x7;
75 const uint32_t orientation = (transform.getOrientation() & ALL_ROTATIONS_MASK);
76 if (orientation == ui::Transform::ROT_0) {
77 return {x, y};
78 }
79
80 vec2 xy(x, y);
81 if (orientation == ui::Transform::ROT_90) {
82 xy.x = displayHeight - y;
83 xy.y = x;
84 } else if (orientation == ui::Transform::ROT_180) {
85 xy.x = displayWidth - x;
86 xy.y = displayHeight - y;
87 } else if (orientation == ui::Transform::ROT_270) {
88 xy.x = y;
89 xy.y = displayWidth - x;
90 }
91 return xy;
92}
93
Prabir Pradhan9f388812021-05-13 16:54:53 -070094vec2 applyTransformWithoutTranslation(const ui::Transform& transform, float x, float y) {
95 const vec2 transformedXy = transform.transform(x, y);
96 const vec2 transformedOrigin = transform.transform(0, 0);
97 return transformedXy - transformedOrigin;
98}
99
100bool shouldDisregardWindowTranslation(uint32_t source) {
101 // Pointer events are the only type of events that refer to absolute coordinates on the display,
102 // so we should apply the entire window transform. For other types of events, we should make
103 // sure to not apply the window translation/offset.
104 return (source & AINPUT_SOURCE_CLASS_POINTER) == 0;
105}
106
Prabir Pradhan6b384612021-05-14 16:56:25 -0700107} // namespace
108
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800109const char* motionClassificationToString(MotionClassification classification) {
110 switch (classification) {
111 case MotionClassification::NONE:
112 return "NONE";
113 case MotionClassification::AMBIGUOUS_GESTURE:
114 return "AMBIGUOUS_GESTURE";
115 case MotionClassification::DEEP_PRESS:
116 return "DEEP_PRESS";
117 }
118}
119
Garfield Tan84b087e2020-01-23 10:49:05 -0800120// --- IdGenerator ---
121IdGenerator::IdGenerator(Source source) : mSource(source) {}
122
123int32_t IdGenerator::nextId() const {
124 constexpr uint32_t SEQUENCE_NUMBER_MASK = ~SOURCE_MASK;
125 int32_t id = 0;
126
127// Avoid building against syscall getrandom(2) on host, which will fail build on Mac. Host doesn't
128// use sequence number so just always return mSource.
129#ifdef __ANDROID__
130 constexpr size_t BUF_LEN = sizeof(id);
131 size_t totalBytes = 0;
132 while (totalBytes < BUF_LEN) {
133 ssize_t bytes = TEMP_FAILURE_RETRY(getrandom(&id, BUF_LEN, GRND_NONBLOCK));
134 if (CC_UNLIKELY(bytes < 0)) {
135 ALOGW("Failed to fill in random number for sequence number: %s.", strerror(errno));
136 id = 0;
137 break;
138 }
139 totalBytes += bytes;
140 }
141#endif // __ANDROID__
142
143 return (id & SEQUENCE_NUMBER_MASK) | static_cast<int32_t>(mSource);
144}
145
Jeff Brown5912f952013-07-01 19:10:31 -0700146// --- InputEvent ---
147
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800148const char* inputEventTypeToString(int32_t type) {
149 switch (type) {
150 case AINPUT_EVENT_TYPE_KEY: {
151 return "KEY";
152 }
153 case AINPUT_EVENT_TYPE_MOTION: {
154 return "MOTION";
155 }
156 case AINPUT_EVENT_TYPE_FOCUS: {
157 return "FOCUS";
158 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800159 case AINPUT_EVENT_TYPE_CAPTURE: {
160 return "CAPTURE";
161 }
arthurhung7632c332020-12-30 16:58:01 +0800162 case AINPUT_EVENT_TYPE_DRAG: {
163 return "DRAG";
164 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800165 }
166 return "UNKNOWN";
167}
168
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800169VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
170 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
171 event.getSource(), event.getDisplayId()},
172 event.getAction(),
173 event.getDownTime(),
174 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
175 event.getKeyCode(),
176 event.getScanCode(),
177 event.getMetaState(),
178 event.getRepeatCount()};
179}
180
181VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
182 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
183 event.getSource(), event.getDisplayId()},
184 event.getRawX(0),
185 event.getRawY(0),
186 event.getActionMasked(),
187 event.getDownTime(),
188 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
189 event.getMetaState(),
190 event.getButtonState()};
191}
192
Garfield Tan4cc839f2020-01-24 11:26:14 -0800193void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600194 std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800195 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700196 mDeviceId = deviceId;
197 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100198 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600199 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700200}
201
202void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800203 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700204 mDeviceId = from.mDeviceId;
205 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100206 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600207 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700208}
209
Garfield Tan4cc839f2020-01-24 11:26:14 -0800210int32_t InputEvent::nextId() {
211 static IdGenerator idGen(IdGenerator::Source::OTHER);
212 return idGen.nextId();
213}
214
Jeff Brown5912f952013-07-01 19:10:31 -0700215// --- KeyEvent ---
216
Michael Wright872db4f2014-04-22 15:03:51 -0700217const char* KeyEvent::getLabel(int32_t keyCode) {
Chris Ye4958d062020-08-20 13:21:10 -0700218 return InputEventLookup::getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700219}
220
Michael Wright872db4f2014-04-22 15:03:51 -0700221int32_t KeyEvent::getKeyCodeFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700222 return InputEventLookup::getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700223}
224
Garfield Tan4cc839f2020-01-24 11:26:14 -0800225void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600226 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
227 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
228 nsecs_t downTime, nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800229 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700230 mAction = action;
231 mFlags = flags;
232 mKeyCode = keyCode;
233 mScanCode = scanCode;
234 mMetaState = metaState;
235 mRepeatCount = repeatCount;
236 mDownTime = downTime;
237 mEventTime = eventTime;
238}
239
240void KeyEvent::initialize(const KeyEvent& from) {
241 InputEvent::initialize(from);
242 mAction = from.mAction;
243 mFlags = from.mFlags;
244 mKeyCode = from.mKeyCode;
245 mScanCode = from.mScanCode;
246 mMetaState = from.mMetaState;
247 mRepeatCount = from.mRepeatCount;
248 mDownTime = from.mDownTime;
249 mEventTime = from.mEventTime;
250}
251
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700252const char* KeyEvent::actionToString(int32_t action) {
253 // Convert KeyEvent action to string
254 switch (action) {
255 case AKEY_EVENT_ACTION_DOWN:
256 return "DOWN";
257 case AKEY_EVENT_ACTION_UP:
258 return "UP";
259 case AKEY_EVENT_ACTION_MULTIPLE:
260 return "MULTIPLE";
261 }
262 return "UNKNOWN";
263}
Jeff Brown5912f952013-07-01 19:10:31 -0700264
265// --- PointerCoords ---
266
267float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700268 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700269 return 0;
270 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700271 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700272}
273
274status_t PointerCoords::setAxisValue(int32_t axis, float value) {
275 if (axis < 0 || axis > 63) {
276 return NAME_NOT_FOUND;
277 }
278
Michael Wright38dcdff2014-03-19 12:06:10 -0700279 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
280 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700281 if (value == 0) {
282 return OK; // axes with value 0 do not need to be stored
283 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700284
285 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700286 if (count >= MAX_AXES) {
287 tooManyAxes(axis);
288 return NO_MEMORY;
289 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700290 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700291 for (uint32_t i = count; i > index; i--) {
292 values[i] = values[i - 1];
293 }
294 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700295
Jeff Brown5912f952013-07-01 19:10:31 -0700296 values[index] = value;
297 return OK;
298}
299
300static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
301 float value = c.getAxisValue(axis);
302 if (value != 0) {
303 c.setAxisValue(axis, value * scaleFactor);
304 }
305}
306
Robert Carre07e1032018-11-26 12:55:53 -0800307void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700308 // No need to scale pressure or size since they are normalized.
309 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800310
311 // If there is a global scale factor, it is included in the windowX/YScale
312 // so we don't need to apply it twice to the X/Y axes.
313 // However we don't want to apply any windowXYScale not included in the global scale
314 // to the TOUCH_MAJOR/MINOR coordinates.
315 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
316 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
317 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
318 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
319 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
320 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
Prabir Pradhanc6523582021-05-14 18:02:55 -0700321 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_X, windowXScale);
322 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_Y, windowYScale);
Robert Carre07e1032018-11-26 12:55:53 -0800323}
324
325void PointerCoords::scale(float globalScaleFactor) {
326 scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700327}
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); });
Evan Roskyd4d4d802021-05-03 20:12:21 -0700633}
634
Brett Chabotfaa986c2020-11-04 17:39:36 -0800635#ifdef __linux__
chaviw9eaa22c2020-07-01 16:21:27 -0700636static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
637 float dsdx, dtdx, tx, dtdy, dsdy, ty;
638 status_t status = parcel.readFloat(&dsdx);
639 status |= parcel.readFloat(&dtdx);
640 status |= parcel.readFloat(&tx);
641 status |= parcel.readFloat(&dtdy);
642 status |= parcel.readFloat(&dsdy);
643 status |= parcel.readFloat(&ty);
644
645 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
646 return status;
647}
648
649static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
650 status_t status = parcel.writeFloat(transform.dsdx());
651 status |= parcel.writeFloat(transform.dtdx());
652 status |= parcel.writeFloat(transform.tx());
653 status |= parcel.writeFloat(transform.dtdy());
654 status |= parcel.writeFloat(transform.dsdy());
655 status |= parcel.writeFloat(transform.ty());
656 return status;
657}
658
Jeff Brown5912f952013-07-01 19:10:31 -0700659status_t MotionEvent::readFromParcel(Parcel* parcel) {
660 size_t pointerCount = parcel->readInt32();
661 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800662 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
663 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700664 return BAD_VALUE;
665 }
666
Garfield Tan4cc839f2020-01-24 11:26:14 -0800667 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700668 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600669 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800670 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600671 std::vector<uint8_t> hmac;
672 status_t result = parcel->readByteVector(&hmac);
673 if (result != OK || hmac.size() != 32) {
674 return BAD_VALUE;
675 }
676 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700677 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100678 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700679 mFlags = parcel->readInt32();
680 mEdgeFlags = parcel->readInt32();
681 mMetaState = parcel->readInt32();
682 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800683 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700684
685 result = android::readFromParcel(mTransform, *parcel);
686 if (result != OK) {
687 return result;
688 }
Jeff Brown5912f952013-07-01 19:10:31 -0700689 mXPrecision = parcel->readFloat();
690 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700691 mRawXCursorPosition = parcel->readFloat();
692 mRawYCursorPosition = parcel->readFloat();
Evan Rosky84f07f02021-04-16 10:42:42 -0700693 mDisplayWidth = parcel->readInt32();
694 mDisplayHeight = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700695 mDownTime = parcel->readInt64();
696
697 mPointerProperties.clear();
698 mPointerProperties.setCapacity(pointerCount);
699 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500700 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700701 mSamplePointerCoords.clear();
702 mSamplePointerCoords.setCapacity(sampleCount * pointerCount);
703
704 for (size_t i = 0; i < pointerCount; i++) {
705 mPointerProperties.push();
706 PointerProperties& properties = mPointerProperties.editTop();
707 properties.id = parcel->readInt32();
708 properties.toolType = parcel->readInt32();
709 }
710
Dan Austinc94fc452015-09-22 14:22:41 -0700711 while (sampleCount > 0) {
712 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500713 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700714 for (size_t i = 0; i < pointerCount; i++) {
715 mSamplePointerCoords.push();
716 status_t status = mSamplePointerCoords.editTop().readFromParcel(parcel);
717 if (status) {
718 return status;
719 }
720 }
721 }
722 return OK;
723}
724
725status_t MotionEvent::writeToParcel(Parcel* parcel) const {
726 size_t pointerCount = mPointerProperties.size();
727 size_t sampleCount = mSampleEventTimes.size();
728
729 parcel->writeInt32(pointerCount);
730 parcel->writeInt32(sampleCount);
731
Garfield Tan4cc839f2020-01-24 11:26:14 -0800732 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700733 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600734 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800735 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600736 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
737 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700738 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100739 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700740 parcel->writeInt32(mFlags);
741 parcel->writeInt32(mEdgeFlags);
742 parcel->writeInt32(mMetaState);
743 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800744 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700745
746 status_t result = android::writeToParcel(mTransform, *parcel);
747 if (result != OK) {
748 return result;
749 }
Jeff Brown5912f952013-07-01 19:10:31 -0700750 parcel->writeFloat(mXPrecision);
751 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700752 parcel->writeFloat(mRawXCursorPosition);
753 parcel->writeFloat(mRawYCursorPosition);
Evan Rosky84f07f02021-04-16 10:42:42 -0700754 parcel->writeInt32(mDisplayWidth);
755 parcel->writeInt32(mDisplayHeight);
Jeff Brown5912f952013-07-01 19:10:31 -0700756 parcel->writeInt64(mDownTime);
757
758 for (size_t i = 0; i < pointerCount; i++) {
759 const PointerProperties& properties = mPointerProperties.itemAt(i);
760 parcel->writeInt32(properties.id);
761 parcel->writeInt32(properties.toolType);
762 }
763
764 const PointerCoords* pc = mSamplePointerCoords.array();
765 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500766 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700767 for (size_t i = 0; i < pointerCount; i++) {
768 status_t status = (pc++)->writeToParcel(parcel);
769 if (status) {
770 return status;
771 }
772 }
773 }
774 return OK;
775}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800776#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700777
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600778bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Jeff Brown5912f952013-07-01 19:10:31 -0700779 if (source & AINPUT_SOURCE_CLASS_POINTER) {
780 // Specifically excludes HOVER_MOVE and SCROLL.
781 switch (action & AMOTION_EVENT_ACTION_MASK) {
782 case AMOTION_EVENT_ACTION_DOWN:
783 case AMOTION_EVENT_ACTION_MOVE:
784 case AMOTION_EVENT_ACTION_UP:
785 case AMOTION_EVENT_ACTION_POINTER_DOWN:
786 case AMOTION_EVENT_ACTION_POINTER_UP:
787 case AMOTION_EVENT_ACTION_CANCEL:
788 case AMOTION_EVENT_ACTION_OUTSIDE:
789 return true;
790 }
791 }
792 return false;
793}
794
Michael Wright872db4f2014-04-22 15:03:51 -0700795const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700796 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700797}
798
799int32_t MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700800 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700801}
802
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500803std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700804 // Convert MotionEvent action to string
805 switch (action & AMOTION_EVENT_ACTION_MASK) {
806 case AMOTION_EVENT_ACTION_DOWN:
807 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700808 case AMOTION_EVENT_ACTION_UP:
809 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500810 case AMOTION_EVENT_ACTION_MOVE:
811 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700812 case AMOTION_EVENT_ACTION_CANCEL:
813 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500814 case AMOTION_EVENT_ACTION_OUTSIDE:
815 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700816 case AMOTION_EVENT_ACTION_POINTER_DOWN:
817 return "POINTER_DOWN";
818 case AMOTION_EVENT_ACTION_POINTER_UP:
819 return "POINTER_UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500820 case AMOTION_EVENT_ACTION_HOVER_MOVE:
821 return "HOVER_MOVE";
822 case AMOTION_EVENT_ACTION_SCROLL:
823 return "SCROLL";
824 case AMOTION_EVENT_ACTION_HOVER_ENTER:
825 return "HOVER_ENTER";
826 case AMOTION_EVENT_ACTION_HOVER_EXIT:
827 return "HOVER_EXIT";
828 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
829 return "BUTTON_PRESS";
830 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
831 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700832 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500833 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700834}
835
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800836// --- FocusEvent ---
837
Garfield Tan4cc839f2020-01-24 11:26:14 -0800838void FocusEvent::initialize(int32_t id, bool hasFocus, bool inTouchMode) {
839 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600840 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800841 mHasFocus = hasFocus;
842 mInTouchMode = inTouchMode;
843}
844
845void FocusEvent::initialize(const FocusEvent& from) {
846 InputEvent::initialize(from);
847 mHasFocus = from.mHasFocus;
848 mInTouchMode = from.mInTouchMode;
849}
Jeff Brown5912f952013-07-01 19:10:31 -0700850
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800851// --- CaptureEvent ---
852
853void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) {
854 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
855 ADISPLAY_ID_NONE, INVALID_HMAC);
856 mPointerCaptureEnabled = pointerCaptureEnabled;
857}
858
859void CaptureEvent::initialize(const CaptureEvent& from) {
860 InputEvent::initialize(from);
861 mPointerCaptureEnabled = from.mPointerCaptureEnabled;
862}
863
arthurhung7632c332020-12-30 16:58:01 +0800864// --- DragEvent ---
865
866void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) {
867 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
868 ADISPLAY_ID_NONE, INVALID_HMAC);
869 mIsExiting = isExiting;
870 mX = x;
871 mY = y;
872}
873
874void DragEvent::initialize(const DragEvent& from) {
875 InputEvent::initialize(from);
876 mIsExiting = from.mIsExiting;
877 mX = from.mX;
878 mY = from.mY;
879}
880
Jeff Brown5912f952013-07-01 19:10:31 -0700881// --- PooledInputEventFactory ---
882
883PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
884 mMaxPoolSize(maxPoolSize) {
885}
886
887PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -0700888}
889
890KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800891 if (mKeyEventPool.empty()) {
892 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700893 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800894 KeyEvent* event = mKeyEventPool.front().release();
895 mKeyEventPool.pop();
896 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700897}
898
899MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800900 if (mMotionEventPool.empty()) {
901 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700902 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800903 MotionEvent* event = mMotionEventPool.front().release();
904 mMotionEventPool.pop();
905 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700906}
907
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800908FocusEvent* PooledInputEventFactory::createFocusEvent() {
909 if (mFocusEventPool.empty()) {
910 return new FocusEvent();
911 }
912 FocusEvent* event = mFocusEventPool.front().release();
913 mFocusEventPool.pop();
914 return event;
915}
916
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800917CaptureEvent* PooledInputEventFactory::createCaptureEvent() {
918 if (mCaptureEventPool.empty()) {
919 return new CaptureEvent();
920 }
921 CaptureEvent* event = mCaptureEventPool.front().release();
922 mCaptureEventPool.pop();
923 return event;
924}
925
arthurhung7632c332020-12-30 16:58:01 +0800926DragEvent* PooledInputEventFactory::createDragEvent() {
927 if (mDragEventPool.empty()) {
928 return new DragEvent();
929 }
930 DragEvent* event = mDragEventPool.front().release();
931 mDragEventPool.pop();
932 return event;
933}
934
Jeff Brown5912f952013-07-01 19:10:31 -0700935void PooledInputEventFactory::recycle(InputEvent* event) {
936 switch (event->getType()) {
937 case AINPUT_EVENT_TYPE_KEY:
938 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800939 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700940 return;
941 }
942 break;
943 case AINPUT_EVENT_TYPE_MOTION:
944 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800945 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700946 return;
947 }
948 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800949 case AINPUT_EVENT_TYPE_FOCUS:
950 if (mFocusEventPool.size() < mMaxPoolSize) {
951 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
952 return;
953 }
954 break;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800955 case AINPUT_EVENT_TYPE_CAPTURE:
956 if (mCaptureEventPool.size() < mMaxPoolSize) {
957 mCaptureEventPool.push(
958 std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event)));
959 return;
960 }
961 break;
arthurhung7632c332020-12-30 16:58:01 +0800962 case AINPUT_EVENT_TYPE_DRAG:
963 if (mDragEventPool.size() < mMaxPoolSize) {
964 mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event)));
965 return;
966 }
967 break;
Jeff Brown5912f952013-07-01 19:10:31 -0700968 }
969 delete event;
970}
971
972} // namespace android