blob: 24a77209c458585849e67c39532923bfb3413369 [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>
Garfield Tan84b087e2020-01-23 10:49:05 -080023#include <string.h>
Jeff Brown5912f952013-07-01 19:10:31 -070024
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050025#include <android-base/stringprintf.h>
chaviw98318de2021-05-19 16:45:23 -050026#include <gui/constants.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.
Prabir Pradhand2b02672021-10-19 11:24:45 -070059 // The return value of atan2f is in range [-pi, pi] which conforms to the orientation API.
60 return atan2f(transformedPoint.x, -transformedPoint.y);
Prabir Pradhan6b384612021-05-14 16:56:25 -070061}
62
Prabir Pradhanb5cb9572021-09-24 06:35:16 -070063vec2 transformWithoutTranslation(const ui::Transform& transform, const vec2& xy) {
64 const vec2 transformedXy = transform.transform(xy);
Prabir Pradhan9f388812021-05-13 16:54:53 -070065 const vec2 transformedOrigin = transform.transform(0, 0);
66 return transformedXy - transformedOrigin;
67}
68
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070069bool isFromSource(uint32_t source, uint32_t test) {
70 return (source & test) == test;
71}
72
73bool shouldDisregardTransformation(uint32_t source) {
74 // Do not apply any transformations to axes from joysticks or touchpads.
75 return isFromSource(source, AINPUT_SOURCE_CLASS_JOYSTICK) ||
76 isFromSource(source, AINPUT_SOURCE_CLASS_POSITION);
77}
78
79bool shouldDisregardOffset(uint32_t source) {
Prabir Pradhan9f388812021-05-13 16:54:53 -070080 // Pointer events are the only type of events that refer to absolute coordinates on the display,
81 // so we should apply the entire window transform. For other types of events, we should make
82 // sure to not apply the window translation/offset.
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070083 return !isFromSource(source, AINPUT_SOURCE_CLASS_POINTER);
Prabir Pradhan9f388812021-05-13 16:54:53 -070084}
85
Prabir Pradhan6b384612021-05-14 16:56:25 -070086} // namespace
87
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -080088const char* motionClassificationToString(MotionClassification classification) {
89 switch (classification) {
90 case MotionClassification::NONE:
91 return "NONE";
92 case MotionClassification::AMBIGUOUS_GESTURE:
93 return "AMBIGUOUS_GESTURE";
94 case MotionClassification::DEEP_PRESS:
95 return "DEEP_PRESS";
96 }
97}
98
Garfield Tan84b087e2020-01-23 10:49:05 -080099// --- IdGenerator ---
100IdGenerator::IdGenerator(Source source) : mSource(source) {}
101
102int32_t IdGenerator::nextId() const {
103 constexpr uint32_t SEQUENCE_NUMBER_MASK = ~SOURCE_MASK;
104 int32_t id = 0;
105
106// Avoid building against syscall getrandom(2) on host, which will fail build on Mac. Host doesn't
107// use sequence number so just always return mSource.
108#ifdef __ANDROID__
109 constexpr size_t BUF_LEN = sizeof(id);
110 size_t totalBytes = 0;
111 while (totalBytes < BUF_LEN) {
112 ssize_t bytes = TEMP_FAILURE_RETRY(getrandom(&id, BUF_LEN, GRND_NONBLOCK));
113 if (CC_UNLIKELY(bytes < 0)) {
114 ALOGW("Failed to fill in random number for sequence number: %s.", strerror(errno));
115 id = 0;
116 break;
117 }
118 totalBytes += bytes;
119 }
120#endif // __ANDROID__
121
122 return (id & SEQUENCE_NUMBER_MASK) | static_cast<int32_t>(mSource);
123}
124
Jeff Brown5912f952013-07-01 19:10:31 -0700125// --- InputEvent ---
126
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800127const char* inputEventTypeToString(int32_t type) {
128 switch (type) {
129 case AINPUT_EVENT_TYPE_KEY: {
130 return "KEY";
131 }
132 case AINPUT_EVENT_TYPE_MOTION: {
133 return "MOTION";
134 }
135 case AINPUT_EVENT_TYPE_FOCUS: {
136 return "FOCUS";
137 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800138 case AINPUT_EVENT_TYPE_CAPTURE: {
139 return "CAPTURE";
140 }
arthurhung7632c332020-12-30 16:58:01 +0800141 case AINPUT_EVENT_TYPE_DRAG: {
142 return "DRAG";
143 }
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700144 case AINPUT_EVENT_TYPE_TOUCH_MODE: {
145 return "TOUCH_MODE";
146 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800147 }
148 return "UNKNOWN";
149}
150
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800151VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
152 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
153 event.getSource(), event.getDisplayId()},
154 event.getAction(),
155 event.getDownTime(),
156 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
157 event.getKeyCode(),
158 event.getScanCode(),
159 event.getMetaState(),
160 event.getRepeatCount()};
161}
162
163VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
164 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
165 event.getSource(), event.getDisplayId()},
166 event.getRawX(0),
167 event.getRawY(0),
168 event.getActionMasked(),
169 event.getDownTime(),
170 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
171 event.getMetaState(),
172 event.getButtonState()};
173}
174
Garfield Tan4cc839f2020-01-24 11:26:14 -0800175void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600176 std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800177 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700178 mDeviceId = deviceId;
179 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100180 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600181 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700182}
183
184void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800185 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700186 mDeviceId = from.mDeviceId;
187 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100188 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600189 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700190}
191
Garfield Tan4cc839f2020-01-24 11:26:14 -0800192int32_t InputEvent::nextId() {
193 static IdGenerator idGen(IdGenerator::Source::OTHER);
194 return idGen.nextId();
195}
196
Jeff Brown5912f952013-07-01 19:10:31 -0700197// --- KeyEvent ---
198
Michael Wright872db4f2014-04-22 15:03:51 -0700199const char* KeyEvent::getLabel(int32_t keyCode) {
Chris Ye4958d062020-08-20 13:21:10 -0700200 return InputEventLookup::getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700201}
202
Michael Wright872db4f2014-04-22 15:03:51 -0700203int32_t KeyEvent::getKeyCodeFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700204 return InputEventLookup::getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700205}
206
Garfield Tan4cc839f2020-01-24 11:26:14 -0800207void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600208 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
209 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
210 nsecs_t downTime, nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800211 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700212 mAction = action;
213 mFlags = flags;
214 mKeyCode = keyCode;
215 mScanCode = scanCode;
216 mMetaState = metaState;
217 mRepeatCount = repeatCount;
218 mDownTime = downTime;
219 mEventTime = eventTime;
220}
221
222void KeyEvent::initialize(const KeyEvent& from) {
223 InputEvent::initialize(from);
224 mAction = from.mAction;
225 mFlags = from.mFlags;
226 mKeyCode = from.mKeyCode;
227 mScanCode = from.mScanCode;
228 mMetaState = from.mMetaState;
229 mRepeatCount = from.mRepeatCount;
230 mDownTime = from.mDownTime;
231 mEventTime = from.mEventTime;
232}
233
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700234const char* KeyEvent::actionToString(int32_t action) {
235 // Convert KeyEvent action to string
236 switch (action) {
237 case AKEY_EVENT_ACTION_DOWN:
238 return "DOWN";
239 case AKEY_EVENT_ACTION_UP:
240 return "UP";
241 case AKEY_EVENT_ACTION_MULTIPLE:
242 return "MULTIPLE";
243 }
244 return "UNKNOWN";
245}
Jeff Brown5912f952013-07-01 19:10:31 -0700246
247// --- PointerCoords ---
248
249float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700250 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700251 return 0;
252 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700253 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700254}
255
256status_t PointerCoords::setAxisValue(int32_t axis, float value) {
257 if (axis < 0 || axis > 63) {
258 return NAME_NOT_FOUND;
259 }
260
Michael Wright38dcdff2014-03-19 12:06:10 -0700261 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
262 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700263 if (value == 0) {
264 return OK; // axes with value 0 do not need to be stored
265 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700266
267 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700268 if (count >= MAX_AXES) {
269 tooManyAxes(axis);
270 return NO_MEMORY;
271 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700272 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700273 for (uint32_t i = count; i > index; i--) {
274 values[i] = values[i - 1];
275 }
276 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700277
Jeff Brown5912f952013-07-01 19:10:31 -0700278 values[index] = value;
279 return OK;
280}
281
282static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
283 float value = c.getAxisValue(axis);
284 if (value != 0) {
285 c.setAxisValue(axis, value * scaleFactor);
286 }
287}
288
Robert Carre07e1032018-11-26 12:55:53 -0800289void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700290 // No need to scale pressure or size since they are normalized.
291 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800292
293 // If there is a global scale factor, it is included in the windowX/YScale
294 // so we don't need to apply it twice to the X/Y axes.
295 // However we don't want to apply any windowXYScale not included in the global scale
296 // to the TOUCH_MAJOR/MINOR coordinates.
297 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
298 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
299 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
300 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
301 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
302 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
Prabir Pradhanc6523582021-05-14 18:02:55 -0700303 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_X, windowXScale);
304 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_Y, windowYScale);
Robert Carre07e1032018-11-26 12:55:53 -0800305}
306
Jeff Brownf086ddb2014-02-11 14:28:48 -0800307void PointerCoords::applyOffset(float xOffset, float yOffset) {
308 setAxisValue(AMOTION_EVENT_AXIS_X, getX() + xOffset);
309 setAxisValue(AMOTION_EVENT_AXIS_Y, getY() + yOffset);
310}
311
Brett Chabotfaa986c2020-11-04 17:39:36 -0800312#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700313status_t PointerCoords::readFromParcel(Parcel* parcel) {
314 bits = parcel->readInt64();
315
Michael Wright38dcdff2014-03-19 12:06:10 -0700316 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700317 if (count > MAX_AXES) {
318 return BAD_VALUE;
319 }
320
321 for (uint32_t i = 0; i < count; i++) {
322 values[i] = parcel->readFloat();
323 }
324 return OK;
325}
326
327status_t PointerCoords::writeToParcel(Parcel* parcel) const {
328 parcel->writeInt64(bits);
329
Michael Wright38dcdff2014-03-19 12:06:10 -0700330 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700331 for (uint32_t i = 0; i < count; i++) {
332 parcel->writeFloat(values[i]);
333 }
334 return OK;
335}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800336#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700337
338void PointerCoords::tooManyAxes(int axis) {
339 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
340 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
341}
342
343bool PointerCoords::operator==(const PointerCoords& other) const {
344 if (bits != other.bits) {
345 return false;
346 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700347 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700348 for (uint32_t i = 0; i < count; i++) {
349 if (values[i] != other.values[i]) {
350 return false;
351 }
352 }
353 return true;
354}
355
356void PointerCoords::copyFrom(const PointerCoords& other) {
357 bits = other.bits;
Michael Wright38dcdff2014-03-19 12:06:10 -0700358 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700359 for (uint32_t i = 0; i < count; i++) {
360 values[i] = other.values[i];
361 }
362}
363
chaviwc01e1372020-07-01 12:37:31 -0700364void PointerCoords::transform(const ui::Transform& transform) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700365 const vec2 xy = transform.transform(getXYValue());
366 setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
367 setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
368
Prabir Pradhanc6523582021-05-14 18:02:55 -0700369 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_X) ||
370 BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_Y)) {
371 const ui::Transform rotation(transform.getOrientation());
372 const vec2 relativeXy = rotation.transform(getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
373 getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
374 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
375 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
376 }
377
Prabir Pradhan6b384612021-05-14 16:56:25 -0700378 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) {
379 const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
380 setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val));
381 }
chaviwc01e1372020-07-01 12:37:31 -0700382}
Jeff Brown5912f952013-07-01 19:10:31 -0700383
384// --- PointerProperties ---
385
386bool PointerProperties::operator==(const PointerProperties& other) const {
387 return id == other.id
388 && toolType == other.toolType;
389}
390
391void PointerProperties::copyFrom(const PointerProperties& other) {
392 id = other.id;
393 toolType = other.toolType;
394}
395
396
397// --- MotionEvent ---
398
Garfield Tan4cc839f2020-01-24 11:26:14 -0800399void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600400 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
401 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700402 int32_t buttonState, MotionClassification classification,
403 const ui::Transform& transform, float xPrecision, float yPrecision,
Evan Rosky84f07f02021-04-16 10:42:42 -0700404 float rawXCursorPosition, float rawYCursorPosition,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700405 const ui::Transform& rawTransform, nsecs_t downTime, nsecs_t eventTime,
Evan Rosky09576692021-07-01 12:22:09 -0700406 size_t pointerCount, const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700407 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800408 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700409 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100410 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700411 mFlags = flags;
412 mEdgeFlags = edgeFlags;
413 mMetaState = metaState;
414 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800415 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700416 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700417 mXPrecision = xPrecision;
418 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700419 mRawXCursorPosition = rawXCursorPosition;
420 mRawYCursorPosition = rawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700421 mRawTransform = rawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700422 mDownTime = downTime;
423 mPointerProperties.clear();
424 mPointerProperties.appendArray(pointerProperties, pointerCount);
425 mSampleEventTimes.clear();
426 mSamplePointerCoords.clear();
427 addSample(eventTime, pointerCoords);
428}
429
430void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800431 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
432 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700433 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100434 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700435 mFlags = other->mFlags;
436 mEdgeFlags = other->mEdgeFlags;
437 mMetaState = other->mMetaState;
438 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800439 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700440 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700441 mXPrecision = other->mXPrecision;
442 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700443 mRawXCursorPosition = other->mRawXCursorPosition;
444 mRawYCursorPosition = other->mRawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700445 mRawTransform = other->mRawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700446 mDownTime = other->mDownTime;
447 mPointerProperties = other->mPointerProperties;
448
449 if (keepHistory) {
450 mSampleEventTimes = other->mSampleEventTimes;
451 mSamplePointerCoords = other->mSamplePointerCoords;
452 } else {
453 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500454 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700455 mSamplePointerCoords.clear();
456 size_t pointerCount = other->getPointerCount();
457 size_t historySize = other->getHistorySize();
458 mSamplePointerCoords.appendArray(other->mSamplePointerCoords.array()
459 + (historySize * pointerCount), pointerCount);
460 }
461}
462
463void MotionEvent::addSample(
464 int64_t eventTime,
465 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500466 mSampleEventTimes.push_back(eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -0700467 mSamplePointerCoords.appendArray(pointerCoords, getPointerCount());
468}
469
Garfield Tan00f511d2019-06-12 16:55:40 -0700470float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700471 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
472 return vals.x;
Garfield Tan00f511d2019-06-12 16:55:40 -0700473}
474
475float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700476 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
477 return vals.y;
Garfield Tan00f511d2019-06-12 16:55:40 -0700478}
479
Garfield Tan937bb832019-07-25 17:48:31 -0700480void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700481 ui::Transform inverse = mTransform.inverse();
482 vec2 vals = inverse.transform(x, y);
483 mRawXCursorPosition = vals.x;
484 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700485}
486
Jeff Brown5912f952013-07-01 19:10:31 -0700487const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
488 return &mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
489}
490
491float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
Evan Rosky84f07f02021-04-16 10:42:42 -0700492 return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700493}
494
495float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700496 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700497}
498
499const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
500 size_t pointerIndex, size_t historicalIndex) const {
501 return &mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
502}
503
504float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan6b384612021-05-14 16:56:25 -0700505 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700506 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
507 return calculateTransformedAxisValue(axis, mSource, mRawTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700508}
509
510float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan9f388812021-05-13 16:54:53 -0700511 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700512 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
513 return calculateTransformedAxisValue(axis, mSource, mTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700514}
515
516ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
517 size_t pointerCount = mPointerProperties.size();
518 for (size_t i = 0; i < pointerCount; i++) {
519 if (mPointerProperties.itemAt(i).id == pointerId) {
520 return i;
521 }
522 }
523 return -1;
524}
525
526void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700527 float currXOffset = mTransform.tx();
528 float currYOffset = mTransform.ty();
529 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700530}
531
Robert Carre07e1032018-11-26 12:55:53 -0800532void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700533 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700534 mRawTransform.set(mRawTransform.tx() * globalScaleFactor,
535 mRawTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800536 mXPrecision *= globalScaleFactor;
537 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700538
539 size_t numSamples = mSamplePointerCoords.size();
540 for (size_t i = 0; i < numSamples; i++) {
chaviw9eaa22c2020-07-01 16:21:27 -0700541 mSamplePointerCoords.editItemAt(i).scale(globalScaleFactor, globalScaleFactor,
542 globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700543 }
544}
545
chaviw9eaa22c2020-07-01 16:21:27 -0700546void MotionEvent::transform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700547 // We want to preserve the raw axes values stored in the PointerCoords, so we just update the
548 // transform using the values passed in.
chaviw9eaa22c2020-07-01 16:21:27 -0700549 ui::Transform newTransform;
550 newTransform.set(matrix);
551 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700552}
553
Evan Roskyd4d4d802021-05-03 20:12:21 -0700554void MotionEvent::applyTransform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700555 ui::Transform transform;
556 transform.set(matrix);
Evan Roskyd4d4d802021-05-03 20:12:21 -0700557
558 // Apply the transformation to all samples.
Prabir Pradhan6b384612021-05-14 16:56:25 -0700559 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
560 [&transform](PointerCoords& c) { c.transform(transform); });
Prabir Pradhan4b19bd02021-06-01 17:34:59 -0700561
562 if (mRawXCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION &&
563 mRawYCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) {
564 const vec2 cursor = transform.transform(mRawXCursorPosition, mRawYCursorPosition);
565 mRawXCursorPosition = cursor.x;
566 mRawYCursorPosition = cursor.y;
567 }
Evan Roskyd4d4d802021-05-03 20:12:21 -0700568}
569
Brett Chabotfaa986c2020-11-04 17:39:36 -0800570#ifdef __linux__
chaviw9eaa22c2020-07-01 16:21:27 -0700571static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
572 float dsdx, dtdx, tx, dtdy, dsdy, ty;
573 status_t status = parcel.readFloat(&dsdx);
574 status |= parcel.readFloat(&dtdx);
575 status |= parcel.readFloat(&tx);
576 status |= parcel.readFloat(&dtdy);
577 status |= parcel.readFloat(&dsdy);
578 status |= parcel.readFloat(&ty);
579
580 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
581 return status;
582}
583
584static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
585 status_t status = parcel.writeFloat(transform.dsdx());
586 status |= parcel.writeFloat(transform.dtdx());
587 status |= parcel.writeFloat(transform.tx());
588 status |= parcel.writeFloat(transform.dtdy());
589 status |= parcel.writeFloat(transform.dsdy());
590 status |= parcel.writeFloat(transform.ty());
591 return status;
592}
593
Jeff Brown5912f952013-07-01 19:10:31 -0700594status_t MotionEvent::readFromParcel(Parcel* parcel) {
595 size_t pointerCount = parcel->readInt32();
596 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800597 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
598 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700599 return BAD_VALUE;
600 }
601
Garfield Tan4cc839f2020-01-24 11:26:14 -0800602 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700603 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600604 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800605 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600606 std::vector<uint8_t> hmac;
607 status_t result = parcel->readByteVector(&hmac);
608 if (result != OK || hmac.size() != 32) {
609 return BAD_VALUE;
610 }
611 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700612 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100613 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700614 mFlags = parcel->readInt32();
615 mEdgeFlags = parcel->readInt32();
616 mMetaState = parcel->readInt32();
617 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800618 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700619
620 result = android::readFromParcel(mTransform, *parcel);
621 if (result != OK) {
622 return result;
623 }
Jeff Brown5912f952013-07-01 19:10:31 -0700624 mXPrecision = parcel->readFloat();
625 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700626 mRawXCursorPosition = parcel->readFloat();
627 mRawYCursorPosition = parcel->readFloat();
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700628
629 result = android::readFromParcel(mRawTransform, *parcel);
630 if (result != OK) {
631 return result;
632 }
Jeff Brown5912f952013-07-01 19:10:31 -0700633 mDownTime = parcel->readInt64();
634
635 mPointerProperties.clear();
636 mPointerProperties.setCapacity(pointerCount);
637 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500638 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700639 mSamplePointerCoords.clear();
640 mSamplePointerCoords.setCapacity(sampleCount * pointerCount);
641
642 for (size_t i = 0; i < pointerCount; i++) {
643 mPointerProperties.push();
644 PointerProperties& properties = mPointerProperties.editTop();
645 properties.id = parcel->readInt32();
646 properties.toolType = parcel->readInt32();
647 }
648
Dan Austinc94fc452015-09-22 14:22:41 -0700649 while (sampleCount > 0) {
650 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500651 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700652 for (size_t i = 0; i < pointerCount; i++) {
653 mSamplePointerCoords.push();
654 status_t status = mSamplePointerCoords.editTop().readFromParcel(parcel);
655 if (status) {
656 return status;
657 }
658 }
659 }
660 return OK;
661}
662
663status_t MotionEvent::writeToParcel(Parcel* parcel) const {
664 size_t pointerCount = mPointerProperties.size();
665 size_t sampleCount = mSampleEventTimes.size();
666
667 parcel->writeInt32(pointerCount);
668 parcel->writeInt32(sampleCount);
669
Garfield Tan4cc839f2020-01-24 11:26:14 -0800670 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700671 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600672 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800673 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600674 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
675 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700676 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100677 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700678 parcel->writeInt32(mFlags);
679 parcel->writeInt32(mEdgeFlags);
680 parcel->writeInt32(mMetaState);
681 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800682 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700683
684 status_t result = android::writeToParcel(mTransform, *parcel);
685 if (result != OK) {
686 return result;
687 }
Jeff Brown5912f952013-07-01 19:10:31 -0700688 parcel->writeFloat(mXPrecision);
689 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700690 parcel->writeFloat(mRawXCursorPosition);
691 parcel->writeFloat(mRawYCursorPosition);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700692
693 result = android::writeToParcel(mRawTransform, *parcel);
694 if (result != OK) {
695 return result;
696 }
Jeff Brown5912f952013-07-01 19:10:31 -0700697 parcel->writeInt64(mDownTime);
698
699 for (size_t i = 0; i < pointerCount; i++) {
700 const PointerProperties& properties = mPointerProperties.itemAt(i);
701 parcel->writeInt32(properties.id);
702 parcel->writeInt32(properties.toolType);
703 }
704
705 const PointerCoords* pc = mSamplePointerCoords.array();
706 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500707 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700708 for (size_t i = 0; i < pointerCount; i++) {
709 status_t status = (pc++)->writeToParcel(parcel);
710 if (status) {
711 return status;
712 }
713 }
714 }
715 return OK;
716}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800717#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700718
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600719bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700720 if (isFromSource(source, AINPUT_SOURCE_CLASS_POINTER)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700721 // Specifically excludes HOVER_MOVE and SCROLL.
722 switch (action & AMOTION_EVENT_ACTION_MASK) {
723 case AMOTION_EVENT_ACTION_DOWN:
724 case AMOTION_EVENT_ACTION_MOVE:
725 case AMOTION_EVENT_ACTION_UP:
726 case AMOTION_EVENT_ACTION_POINTER_DOWN:
727 case AMOTION_EVENT_ACTION_POINTER_UP:
728 case AMOTION_EVENT_ACTION_CANCEL:
729 case AMOTION_EVENT_ACTION_OUTSIDE:
730 return true;
731 }
732 }
733 return false;
734}
735
Michael Wright872db4f2014-04-22 15:03:51 -0700736const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700737 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700738}
739
740int32_t MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700741 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700742}
743
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500744std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700745 // Convert MotionEvent action to string
746 switch (action & AMOTION_EVENT_ACTION_MASK) {
747 case AMOTION_EVENT_ACTION_DOWN:
748 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700749 case AMOTION_EVENT_ACTION_UP:
750 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500751 case AMOTION_EVENT_ACTION_MOVE:
752 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700753 case AMOTION_EVENT_ACTION_CANCEL:
754 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500755 case AMOTION_EVENT_ACTION_OUTSIDE:
756 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700757 case AMOTION_EVENT_ACTION_POINTER_DOWN:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000758 return StringPrintf("POINTER_DOWN(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700759 case AMOTION_EVENT_ACTION_POINTER_UP:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000760 return StringPrintf("POINTER_UP(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500761 case AMOTION_EVENT_ACTION_HOVER_MOVE:
762 return "HOVER_MOVE";
763 case AMOTION_EVENT_ACTION_SCROLL:
764 return "SCROLL";
765 case AMOTION_EVENT_ACTION_HOVER_ENTER:
766 return "HOVER_ENTER";
767 case AMOTION_EVENT_ACTION_HOVER_EXIT:
768 return "HOVER_EXIT";
769 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
770 return "BUTTON_PRESS";
771 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
772 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700773 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500774 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700775}
776
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700777// Apply the given transformation to the point without checking whether the entire transform
778// should be disregarded altogether for the provided source.
779static inline vec2 calculateTransformedXYUnchecked(uint32_t source, const ui::Transform& transform,
780 const vec2& xy) {
781 return shouldDisregardOffset(source) ? transformWithoutTranslation(transform, xy)
782 : transform.transform(xy);
783}
784
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700785vec2 MotionEvent::calculateTransformedXY(uint32_t source, const ui::Transform& transform,
786 const vec2& xy) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700787 if (shouldDisregardTransformation(source)) {
788 return xy;
789 }
790 return calculateTransformedXYUnchecked(source, transform, xy);
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700791}
792
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700793float MotionEvent::calculateTransformedAxisValue(int32_t axis, uint32_t source,
794 const ui::Transform& transform,
795 const PointerCoords& coords) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700796 if (shouldDisregardTransformation(source)) {
797 return coords.getAxisValue(axis);
798 }
799
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700800 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700801 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700802 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
803 return xy[axis];
804 }
805
806 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
807 const vec2 relativeXy =
808 transformWithoutTranslation(transform,
809 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
810 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
811 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
812 }
813
814 if (axis == AMOTION_EVENT_AXIS_ORIENTATION) {
815 return transformAngle(transform, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION));
816 }
817
818 return coords.getAxisValue(axis);
819}
820
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800821// --- FocusEvent ---
822
Garfield Tan4cc839f2020-01-24 11:26:14 -0800823void FocusEvent::initialize(int32_t id, bool hasFocus, bool inTouchMode) {
824 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600825 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800826 mHasFocus = hasFocus;
827 mInTouchMode = inTouchMode;
828}
829
830void FocusEvent::initialize(const FocusEvent& from) {
831 InputEvent::initialize(from);
832 mHasFocus = from.mHasFocus;
833 mInTouchMode = from.mInTouchMode;
834}
Jeff Brown5912f952013-07-01 19:10:31 -0700835
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800836// --- CaptureEvent ---
837
838void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) {
839 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
840 ADISPLAY_ID_NONE, INVALID_HMAC);
841 mPointerCaptureEnabled = pointerCaptureEnabled;
842}
843
844void CaptureEvent::initialize(const CaptureEvent& from) {
845 InputEvent::initialize(from);
846 mPointerCaptureEnabled = from.mPointerCaptureEnabled;
847}
848
arthurhung7632c332020-12-30 16:58:01 +0800849// --- DragEvent ---
850
851void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) {
852 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
853 ADISPLAY_ID_NONE, INVALID_HMAC);
854 mIsExiting = isExiting;
855 mX = x;
856 mY = y;
857}
858
859void DragEvent::initialize(const DragEvent& from) {
860 InputEvent::initialize(from);
861 mIsExiting = from.mIsExiting;
862 mX = from.mX;
863 mY = from.mY;
864}
865
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700866// --- TouchModeEvent ---
867
868void TouchModeEvent::initialize(int32_t id, bool isInTouchMode) {
869 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
870 ADISPLAY_ID_NONE, INVALID_HMAC);
871 mIsInTouchMode = isInTouchMode;
872}
873
874void TouchModeEvent::initialize(const TouchModeEvent& from) {
875 InputEvent::initialize(from);
876 mIsInTouchMode = from.mIsInTouchMode;
877}
878
Jeff Brown5912f952013-07-01 19:10:31 -0700879// --- PooledInputEventFactory ---
880
881PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
882 mMaxPoolSize(maxPoolSize) {
883}
884
885PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -0700886}
887
888KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800889 if (mKeyEventPool.empty()) {
890 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700891 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800892 KeyEvent* event = mKeyEventPool.front().release();
893 mKeyEventPool.pop();
894 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700895}
896
897MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800898 if (mMotionEventPool.empty()) {
899 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700900 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800901 MotionEvent* event = mMotionEventPool.front().release();
902 mMotionEventPool.pop();
903 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700904}
905
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800906FocusEvent* PooledInputEventFactory::createFocusEvent() {
907 if (mFocusEventPool.empty()) {
908 return new FocusEvent();
909 }
910 FocusEvent* event = mFocusEventPool.front().release();
911 mFocusEventPool.pop();
912 return event;
913}
914
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800915CaptureEvent* PooledInputEventFactory::createCaptureEvent() {
916 if (mCaptureEventPool.empty()) {
917 return new CaptureEvent();
918 }
919 CaptureEvent* event = mCaptureEventPool.front().release();
920 mCaptureEventPool.pop();
921 return event;
922}
923
arthurhung7632c332020-12-30 16:58:01 +0800924DragEvent* PooledInputEventFactory::createDragEvent() {
925 if (mDragEventPool.empty()) {
926 return new DragEvent();
927 }
928 DragEvent* event = mDragEventPool.front().release();
929 mDragEventPool.pop();
930 return event;
931}
932
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700933TouchModeEvent* PooledInputEventFactory::createTouchModeEvent() {
934 if (mTouchModeEventPool.empty()) {
935 return new TouchModeEvent();
936 }
937 TouchModeEvent* event = mTouchModeEventPool.front().release();
938 mTouchModeEventPool.pop();
939 return event;
940}
941
Jeff Brown5912f952013-07-01 19:10:31 -0700942void PooledInputEventFactory::recycle(InputEvent* event) {
943 switch (event->getType()) {
944 case AINPUT_EVENT_TYPE_KEY:
945 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800946 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700947 return;
948 }
949 break;
950 case AINPUT_EVENT_TYPE_MOTION:
951 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800952 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700953 return;
954 }
955 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800956 case AINPUT_EVENT_TYPE_FOCUS:
957 if (mFocusEventPool.size() < mMaxPoolSize) {
958 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
959 return;
960 }
961 break;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800962 case AINPUT_EVENT_TYPE_CAPTURE:
963 if (mCaptureEventPool.size() < mMaxPoolSize) {
964 mCaptureEventPool.push(
965 std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event)));
966 return;
967 }
968 break;
arthurhung7632c332020-12-30 16:58:01 +0800969 case AINPUT_EVENT_TYPE_DRAG:
970 if (mDragEventPool.size() < mMaxPoolSize) {
971 mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event)));
972 return;
973 }
974 break;
Antonio Kantekeb4a30c2021-09-28 17:49:49 -0700975 case AINPUT_EVENT_TYPE_TOUCH_MODE:
976 if (mTouchModeEventPool.size() < mMaxPoolSize) {
977 mTouchModeEventPool.push(
978 std::unique_ptr<TouchModeEvent>(static_cast<TouchModeEvent*>(event)));
979 return;
980 }
981 break;
Jeff Brown5912f952013-07-01 19:10:31 -0700982 }
983 delete event;
984}
985
986} // namespace android