blob: ad7db75c1ad951492305fd9a00531b5cc01ed90d [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
Jeff Brown5912f952013-07-01 19:10:31 -070031#include <binder/Parcel.h>
Brett Chabot58208522020-09-09 13:55:24 -070032#ifdef __ANDROID__
Garfield Tan84b087e2020-01-23 10:49:05 -080033#include <sys/random.h>
Jeff Brown5912f952013-07-01 19:10:31 -070034#endif
35
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050036using android::base::StringPrintf;
37
Jeff Brown5912f952013-07-01 19:10:31 -070038namespace android {
39
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -080040const char* motionClassificationToString(MotionClassification classification) {
41 switch (classification) {
42 case MotionClassification::NONE:
43 return "NONE";
44 case MotionClassification::AMBIGUOUS_GESTURE:
45 return "AMBIGUOUS_GESTURE";
46 case MotionClassification::DEEP_PRESS:
47 return "DEEP_PRESS";
48 }
49}
50
Garfield Tan84b087e2020-01-23 10:49:05 -080051// --- IdGenerator ---
52IdGenerator::IdGenerator(Source source) : mSource(source) {}
53
54int32_t IdGenerator::nextId() const {
55 constexpr uint32_t SEQUENCE_NUMBER_MASK = ~SOURCE_MASK;
56 int32_t id = 0;
57
58// Avoid building against syscall getrandom(2) on host, which will fail build on Mac. Host doesn't
59// use sequence number so just always return mSource.
60#ifdef __ANDROID__
61 constexpr size_t BUF_LEN = sizeof(id);
62 size_t totalBytes = 0;
63 while (totalBytes < BUF_LEN) {
64 ssize_t bytes = TEMP_FAILURE_RETRY(getrandom(&id, BUF_LEN, GRND_NONBLOCK));
65 if (CC_UNLIKELY(bytes < 0)) {
66 ALOGW("Failed to fill in random number for sequence number: %s.", strerror(errno));
67 id = 0;
68 break;
69 }
70 totalBytes += bytes;
71 }
72#endif // __ANDROID__
73
74 return (id & SEQUENCE_NUMBER_MASK) | static_cast<int32_t>(mSource);
75}
76
Jeff Brown5912f952013-07-01 19:10:31 -070077// --- InputEvent ---
78
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080079const char* inputEventTypeToString(int32_t type) {
80 switch (type) {
81 case AINPUT_EVENT_TYPE_KEY: {
82 return "KEY";
83 }
84 case AINPUT_EVENT_TYPE_MOTION: {
85 return "MOTION";
86 }
87 case AINPUT_EVENT_TYPE_FOCUS: {
88 return "FOCUS";
89 }
90 }
91 return "UNKNOWN";
92}
93
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -080094VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
95 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
96 event.getSource(), event.getDisplayId()},
97 event.getAction(),
98 event.getDownTime(),
99 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
100 event.getKeyCode(),
101 event.getScanCode(),
102 event.getMetaState(),
103 event.getRepeatCount()};
104}
105
106VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
107 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
108 event.getSource(), event.getDisplayId()},
109 event.getRawX(0),
110 event.getRawY(0),
111 event.getActionMasked(),
112 event.getDownTime(),
113 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
114 event.getMetaState(),
115 event.getButtonState()};
116}
117
Garfield Tan4cc839f2020-01-24 11:26:14 -0800118void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600119 std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800120 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700121 mDeviceId = deviceId;
122 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100123 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600124 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700125}
126
127void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800128 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700129 mDeviceId = from.mDeviceId;
130 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100131 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600132 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700133}
134
Garfield Tan4cc839f2020-01-24 11:26:14 -0800135int32_t InputEvent::nextId() {
136 static IdGenerator idGen(IdGenerator::Source::OTHER);
137 return idGen.nextId();
138}
139
Jeff Brown5912f952013-07-01 19:10:31 -0700140// --- KeyEvent ---
141
Michael Wright872db4f2014-04-22 15:03:51 -0700142const char* KeyEvent::getLabel(int32_t keyCode) {
Chris Ye4958d062020-08-20 13:21:10 -0700143 return InputEventLookup::getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700144}
145
Michael Wright872db4f2014-04-22 15:03:51 -0700146int32_t KeyEvent::getKeyCodeFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700147 return InputEventLookup::getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700148}
149
Garfield Tan4cc839f2020-01-24 11:26:14 -0800150void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600151 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
152 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
153 nsecs_t downTime, nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800154 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700155 mAction = action;
156 mFlags = flags;
157 mKeyCode = keyCode;
158 mScanCode = scanCode;
159 mMetaState = metaState;
160 mRepeatCount = repeatCount;
161 mDownTime = downTime;
162 mEventTime = eventTime;
163}
164
165void KeyEvent::initialize(const KeyEvent& from) {
166 InputEvent::initialize(from);
167 mAction = from.mAction;
168 mFlags = from.mFlags;
169 mKeyCode = from.mKeyCode;
170 mScanCode = from.mScanCode;
171 mMetaState = from.mMetaState;
172 mRepeatCount = from.mRepeatCount;
173 mDownTime = from.mDownTime;
174 mEventTime = from.mEventTime;
175}
176
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700177const char* KeyEvent::actionToString(int32_t action) {
178 // Convert KeyEvent action to string
179 switch (action) {
180 case AKEY_EVENT_ACTION_DOWN:
181 return "DOWN";
182 case AKEY_EVENT_ACTION_UP:
183 return "UP";
184 case AKEY_EVENT_ACTION_MULTIPLE:
185 return "MULTIPLE";
186 }
187 return "UNKNOWN";
188}
Jeff Brown5912f952013-07-01 19:10:31 -0700189
190// --- PointerCoords ---
191
192float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700193 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700194 return 0;
195 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700196 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700197}
198
199status_t PointerCoords::setAxisValue(int32_t axis, float value) {
200 if (axis < 0 || axis > 63) {
201 return NAME_NOT_FOUND;
202 }
203
Michael Wright38dcdff2014-03-19 12:06:10 -0700204 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
205 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700206 if (value == 0) {
207 return OK; // axes with value 0 do not need to be stored
208 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700209
210 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700211 if (count >= MAX_AXES) {
212 tooManyAxes(axis);
213 return NO_MEMORY;
214 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700215 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700216 for (uint32_t i = count; i > index; i--) {
217 values[i] = values[i - 1];
218 }
219 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700220
Jeff Brown5912f952013-07-01 19:10:31 -0700221 values[index] = value;
222 return OK;
223}
224
225static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
226 float value = c.getAxisValue(axis);
227 if (value != 0) {
228 c.setAxisValue(axis, value * scaleFactor);
229 }
230}
231
Robert Carre07e1032018-11-26 12:55:53 -0800232void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700233 // No need to scale pressure or size since they are normalized.
234 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800235
236 // If there is a global scale factor, it is included in the windowX/YScale
237 // so we don't need to apply it twice to the X/Y axes.
238 // However we don't want to apply any windowXYScale not included in the global scale
239 // to the TOUCH_MAJOR/MINOR coordinates.
240 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
241 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
242 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
243 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
244 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
245 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
246}
247
248void PointerCoords::scale(float globalScaleFactor) {
249 scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700250}
251
Jeff Brownf086ddb2014-02-11 14:28:48 -0800252void PointerCoords::applyOffset(float xOffset, float yOffset) {
253 setAxisValue(AMOTION_EVENT_AXIS_X, getX() + xOffset);
254 setAxisValue(AMOTION_EVENT_AXIS_Y, getY() + yOffset);
255}
256
Jeff Brown5912f952013-07-01 19:10:31 -0700257status_t PointerCoords::readFromParcel(Parcel* parcel) {
258 bits = parcel->readInt64();
259
Michael Wright38dcdff2014-03-19 12:06:10 -0700260 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700261 if (count > MAX_AXES) {
262 return BAD_VALUE;
263 }
264
265 for (uint32_t i = 0; i < count; i++) {
266 values[i] = parcel->readFloat();
267 }
268 return OK;
269}
270
271status_t PointerCoords::writeToParcel(Parcel* parcel) const {
272 parcel->writeInt64(bits);
273
Michael Wright38dcdff2014-03-19 12:06:10 -0700274 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700275 for (uint32_t i = 0; i < count; i++) {
276 parcel->writeFloat(values[i]);
277 }
278 return OK;
279}
Jeff Brown5912f952013-07-01 19:10:31 -0700280
281void PointerCoords::tooManyAxes(int axis) {
282 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
283 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
284}
285
286bool PointerCoords::operator==(const PointerCoords& other) const {
287 if (bits != other.bits) {
288 return false;
289 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700290 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700291 for (uint32_t i = 0; i < count; i++) {
292 if (values[i] != other.values[i]) {
293 return false;
294 }
295 }
296 return true;
297}
298
299void PointerCoords::copyFrom(const PointerCoords& other) {
300 bits = other.bits;
Michael Wright38dcdff2014-03-19 12:06:10 -0700301 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700302 for (uint32_t i = 0; i < count; i++) {
303 values[i] = other.values[i];
304 }
305}
306
chaviwc01e1372020-07-01 12:37:31 -0700307void PointerCoords::transform(const ui::Transform& transform) {
308 vec2 newCoords = transform.transform(getX(), getY());
309 setAxisValue(AMOTION_EVENT_AXIS_X, newCoords.x);
310 setAxisValue(AMOTION_EVENT_AXIS_Y, newCoords.y);
311}
Jeff Brown5912f952013-07-01 19:10:31 -0700312
313// --- PointerProperties ---
314
315bool PointerProperties::operator==(const PointerProperties& other) const {
316 return id == other.id
317 && toolType == other.toolType;
318}
319
320void PointerProperties::copyFrom(const PointerProperties& other) {
321 id = other.id;
322 toolType = other.toolType;
323}
324
325
326// --- MotionEvent ---
327
Garfield Tan4cc839f2020-01-24 11:26:14 -0800328void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600329 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
330 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700331 int32_t buttonState, MotionClassification classification,
332 const ui::Transform& transform, float xPrecision, float yPrecision,
333 float rawXCursorPosition, float rawYCursorPosition, nsecs_t downTime,
334 nsecs_t eventTime, size_t pointerCount,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600335 const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700336 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800337 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700338 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100339 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700340 mFlags = flags;
341 mEdgeFlags = edgeFlags;
342 mMetaState = metaState;
343 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800344 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700345 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700346 mXPrecision = xPrecision;
347 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700348 mRawXCursorPosition = rawXCursorPosition;
349 mRawYCursorPosition = rawYCursorPosition;
Jeff Brown5912f952013-07-01 19:10:31 -0700350 mDownTime = downTime;
351 mPointerProperties.clear();
352 mPointerProperties.appendArray(pointerProperties, pointerCount);
353 mSampleEventTimes.clear();
354 mSamplePointerCoords.clear();
355 addSample(eventTime, pointerCoords);
356}
357
358void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800359 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
360 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700361 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100362 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700363 mFlags = other->mFlags;
364 mEdgeFlags = other->mEdgeFlags;
365 mMetaState = other->mMetaState;
366 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800367 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700368 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700369 mXPrecision = other->mXPrecision;
370 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700371 mRawXCursorPosition = other->mRawXCursorPosition;
372 mRawYCursorPosition = other->mRawYCursorPosition;
Jeff Brown5912f952013-07-01 19:10:31 -0700373 mDownTime = other->mDownTime;
374 mPointerProperties = other->mPointerProperties;
375
376 if (keepHistory) {
377 mSampleEventTimes = other->mSampleEventTimes;
378 mSamplePointerCoords = other->mSamplePointerCoords;
379 } else {
380 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500381 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700382 mSamplePointerCoords.clear();
383 size_t pointerCount = other->getPointerCount();
384 size_t historySize = other->getHistorySize();
385 mSamplePointerCoords.appendArray(other->mSamplePointerCoords.array()
386 + (historySize * pointerCount), pointerCount);
387 }
388}
389
390void MotionEvent::addSample(
391 int64_t eventTime,
392 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500393 mSampleEventTimes.push_back(eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -0700394 mSamplePointerCoords.appendArray(pointerCoords, getPointerCount());
395}
396
Garfield Tan00f511d2019-06-12 16:55:40 -0700397float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700398 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
399 return vals.x;
Garfield Tan00f511d2019-06-12 16:55:40 -0700400}
401
402float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700403 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
404 return vals.y;
Garfield Tan00f511d2019-06-12 16:55:40 -0700405}
406
Garfield Tan937bb832019-07-25 17:48:31 -0700407void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700408 ui::Transform inverse = mTransform.inverse();
409 vec2 vals = inverse.transform(x, y);
410 mRawXCursorPosition = vals.x;
411 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700412}
413
Jeff Brown5912f952013-07-01 19:10:31 -0700414const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
415 return &mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
416}
417
418float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
419 return getRawPointerCoords(pointerIndex)->getAxisValue(axis);
420}
421
422float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700423 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700424}
425
426const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
427 size_t pointerIndex, size_t historicalIndex) const {
428 return &mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
429}
430
431float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
432 size_t historicalIndex) const {
433 return getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis);
434}
435
436float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
437 size_t historicalIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700438 if (axis != AMOTION_EVENT_AXIS_X && axis != AMOTION_EVENT_AXIS_Y) {
439 return getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis);
440 }
441
442 float rawX = getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getX();
443 float rawY = getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getY();
444 vec2 vals = mTransform.transform(rawX, rawY);
445
Jeff Brown5912f952013-07-01 19:10:31 -0700446 switch (axis) {
447 case AMOTION_EVENT_AXIS_X:
chaviw9eaa22c2020-07-01 16:21:27 -0700448 return vals.x;
Jeff Brown5912f952013-07-01 19:10:31 -0700449 case AMOTION_EVENT_AXIS_Y:
chaviw9eaa22c2020-07-01 16:21:27 -0700450 return vals.y;
Jeff Brown5912f952013-07-01 19:10:31 -0700451 }
chaviw9eaa22c2020-07-01 16:21:27 -0700452
453 // This should never happen
454 return 0;
Jeff Brown5912f952013-07-01 19:10:31 -0700455}
456
457ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
458 size_t pointerCount = mPointerProperties.size();
459 for (size_t i = 0; i < pointerCount; i++) {
460 if (mPointerProperties.itemAt(i).id == pointerId) {
461 return i;
462 }
463 }
464 return -1;
465}
466
467void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700468 float currXOffset = mTransform.tx();
469 float currYOffset = mTransform.ty();
470 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700471}
472
Robert Carre07e1032018-11-26 12:55:53 -0800473void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700474 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800475 mXPrecision *= globalScaleFactor;
476 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700477
478 size_t numSamples = mSamplePointerCoords.size();
479 for (size_t i = 0; i < numSamples; i++) {
chaviw9eaa22c2020-07-01 16:21:27 -0700480 mSamplePointerCoords.editItemAt(i).scale(globalScaleFactor, globalScaleFactor,
481 globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700482 }
483}
484
chaviw9eaa22c2020-07-01 16:21:27 -0700485static vec2 transformPoint(const std::array<float, 9>& matrix, float x, float y) {
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700486 // Apply perspective transform like Skia.
487 float newX = matrix[0] * x + matrix[1] * y + matrix[2];
488 float newY = matrix[3] * x + matrix[4] * y + matrix[5];
489 float newZ = matrix[6] * x + matrix[7] * y + matrix[8];
490 if (newZ) {
491 newZ = 1.0f / newZ;
492 }
chaviw9eaa22c2020-07-01 16:21:27 -0700493 vec2 transformedPoint;
494 transformedPoint.x = newX * newZ;
495 transformedPoint.y = newY * newZ;
496 return transformedPoint;
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700497}
498
chaviw9eaa22c2020-07-01 16:21:27 -0700499static float transformAngle(const std::array<float, 9>& matrix, float angleRadians, float originX,
500 float originY) {
Jeff Brown5912f952013-07-01 19:10:31 -0700501 // Construct and transform a vector oriented at the specified clockwise angle from vertical.
502 // Coordinate system: down is increasing Y, right is increasing X.
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700503 float x = sinf(angleRadians);
504 float y = -cosf(angleRadians);
chaviw9eaa22c2020-07-01 16:21:27 -0700505 vec2 transformedPoint = transformPoint(matrix, x, y);
506
507 transformedPoint.x -= originX;
508 transformedPoint.y -= originY;
Jeff Brown5912f952013-07-01 19:10:31 -0700509
510 // Derive the transformed vector's clockwise angle from vertical.
chaviw9eaa22c2020-07-01 16:21:27 -0700511 float result = atan2f(transformedPoint.x, -transformedPoint.y);
Jeff Brown5912f952013-07-01 19:10:31 -0700512 if (result < - M_PI_2) {
513 result += M_PI;
514 } else if (result > M_PI_2) {
515 result -= M_PI;
516 }
517 return result;
518}
519
chaviw9eaa22c2020-07-01 16:21:27 -0700520void MotionEvent::transform(const std::array<float, 9>& matrix) {
521 // We want to preserve the rawX and rawY so we just update the transform
522 // using the values of the transform passed in
523 ui::Transform newTransform;
524 newTransform.set(matrix);
525 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700526
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700527 // Determine how the origin is transformed by the matrix so that we
528 // can transform orientation vectors.
chaviw9eaa22c2020-07-01 16:21:27 -0700529 vec2 origin = transformPoint(matrix, 0, 0);
Garfield Tan00f511d2019-06-12 16:55:40 -0700530
Jeff Brown5912f952013-07-01 19:10:31 -0700531 // Apply the transformation to all samples.
532 size_t numSamples = mSamplePointerCoords.size();
533 for (size_t i = 0; i < numSamples; i++) {
534 PointerCoords& c = mSamplePointerCoords.editItemAt(i);
Jeff Brown5912f952013-07-01 19:10:31 -0700535 float orientation = c.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700536 c.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
chaviw9eaa22c2020-07-01 16:21:27 -0700537 transformAngle(matrix, orientation, origin.x, origin.y));
Jeff Brown5912f952013-07-01 19:10:31 -0700538 }
539}
540
chaviw9eaa22c2020-07-01 16:21:27 -0700541static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
542 float dsdx, dtdx, tx, dtdy, dsdy, ty;
543 status_t status = parcel.readFloat(&dsdx);
544 status |= parcel.readFloat(&dtdx);
545 status |= parcel.readFloat(&tx);
546 status |= parcel.readFloat(&dtdy);
547 status |= parcel.readFloat(&dsdy);
548 status |= parcel.readFloat(&ty);
549
550 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
551 return status;
552}
553
554static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
555 status_t status = parcel.writeFloat(transform.dsdx());
556 status |= parcel.writeFloat(transform.dtdx());
557 status |= parcel.writeFloat(transform.tx());
558 status |= parcel.writeFloat(transform.dtdy());
559 status |= parcel.writeFloat(transform.dsdy());
560 status |= parcel.writeFloat(transform.ty());
561 return status;
562}
563
Jeff Brown5912f952013-07-01 19:10:31 -0700564status_t MotionEvent::readFromParcel(Parcel* parcel) {
565 size_t pointerCount = parcel->readInt32();
566 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800567 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
568 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700569 return BAD_VALUE;
570 }
571
Garfield Tan4cc839f2020-01-24 11:26:14 -0800572 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700573 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600574 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800575 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600576 std::vector<uint8_t> hmac;
577 status_t result = parcel->readByteVector(&hmac);
578 if (result != OK || hmac.size() != 32) {
579 return BAD_VALUE;
580 }
581 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700582 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100583 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700584 mFlags = parcel->readInt32();
585 mEdgeFlags = parcel->readInt32();
586 mMetaState = parcel->readInt32();
587 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800588 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700589
590 result = android::readFromParcel(mTransform, *parcel);
591 if (result != OK) {
592 return result;
593 }
Jeff Brown5912f952013-07-01 19:10:31 -0700594 mXPrecision = parcel->readFloat();
595 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700596 mRawXCursorPosition = parcel->readFloat();
597 mRawYCursorPosition = parcel->readFloat();
Jeff Brown5912f952013-07-01 19:10:31 -0700598 mDownTime = parcel->readInt64();
599
600 mPointerProperties.clear();
601 mPointerProperties.setCapacity(pointerCount);
602 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500603 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700604 mSamplePointerCoords.clear();
605 mSamplePointerCoords.setCapacity(sampleCount * pointerCount);
606
607 for (size_t i = 0; i < pointerCount; i++) {
608 mPointerProperties.push();
609 PointerProperties& properties = mPointerProperties.editTop();
610 properties.id = parcel->readInt32();
611 properties.toolType = parcel->readInt32();
612 }
613
Dan Austinc94fc452015-09-22 14:22:41 -0700614 while (sampleCount > 0) {
615 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500616 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700617 for (size_t i = 0; i < pointerCount; i++) {
618 mSamplePointerCoords.push();
619 status_t status = mSamplePointerCoords.editTop().readFromParcel(parcel);
620 if (status) {
621 return status;
622 }
623 }
624 }
625 return OK;
626}
627
628status_t MotionEvent::writeToParcel(Parcel* parcel) const {
629 size_t pointerCount = mPointerProperties.size();
630 size_t sampleCount = mSampleEventTimes.size();
631
632 parcel->writeInt32(pointerCount);
633 parcel->writeInt32(sampleCount);
634
Garfield Tan4cc839f2020-01-24 11:26:14 -0800635 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700636 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600637 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800638 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600639 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
640 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700641 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100642 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700643 parcel->writeInt32(mFlags);
644 parcel->writeInt32(mEdgeFlags);
645 parcel->writeInt32(mMetaState);
646 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800647 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700648
649 status_t result = android::writeToParcel(mTransform, *parcel);
650 if (result != OK) {
651 return result;
652 }
Jeff Brown5912f952013-07-01 19:10:31 -0700653 parcel->writeFloat(mXPrecision);
654 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700655 parcel->writeFloat(mRawXCursorPosition);
656 parcel->writeFloat(mRawYCursorPosition);
Jeff Brown5912f952013-07-01 19:10:31 -0700657 parcel->writeInt64(mDownTime);
658
659 for (size_t i = 0; i < pointerCount; i++) {
660 const PointerProperties& properties = mPointerProperties.itemAt(i);
661 parcel->writeInt32(properties.id);
662 parcel->writeInt32(properties.toolType);
663 }
664
665 const PointerCoords* pc = mSamplePointerCoords.array();
666 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500667 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700668 for (size_t i = 0; i < pointerCount; i++) {
669 status_t status = (pc++)->writeToParcel(parcel);
670 if (status) {
671 return status;
672 }
673 }
674 }
675 return OK;
676}
Jeff Brown5912f952013-07-01 19:10:31 -0700677
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600678bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Jeff Brown5912f952013-07-01 19:10:31 -0700679 if (source & AINPUT_SOURCE_CLASS_POINTER) {
680 // Specifically excludes HOVER_MOVE and SCROLL.
681 switch (action & AMOTION_EVENT_ACTION_MASK) {
682 case AMOTION_EVENT_ACTION_DOWN:
683 case AMOTION_EVENT_ACTION_MOVE:
684 case AMOTION_EVENT_ACTION_UP:
685 case AMOTION_EVENT_ACTION_POINTER_DOWN:
686 case AMOTION_EVENT_ACTION_POINTER_UP:
687 case AMOTION_EVENT_ACTION_CANCEL:
688 case AMOTION_EVENT_ACTION_OUTSIDE:
689 return true;
690 }
691 }
692 return false;
693}
694
Michael Wright872db4f2014-04-22 15:03:51 -0700695const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700696 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700697}
698
699int32_t MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700700 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700701}
702
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500703std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700704 // Convert MotionEvent action to string
705 switch (action & AMOTION_EVENT_ACTION_MASK) {
706 case AMOTION_EVENT_ACTION_DOWN:
707 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700708 case AMOTION_EVENT_ACTION_UP:
709 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500710 case AMOTION_EVENT_ACTION_MOVE:
711 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700712 case AMOTION_EVENT_ACTION_CANCEL:
713 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500714 case AMOTION_EVENT_ACTION_OUTSIDE:
715 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700716 case AMOTION_EVENT_ACTION_POINTER_DOWN:
717 return "POINTER_DOWN";
718 case AMOTION_EVENT_ACTION_POINTER_UP:
719 return "POINTER_UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500720 case AMOTION_EVENT_ACTION_HOVER_MOVE:
721 return "HOVER_MOVE";
722 case AMOTION_EVENT_ACTION_SCROLL:
723 return "SCROLL";
724 case AMOTION_EVENT_ACTION_HOVER_ENTER:
725 return "HOVER_ENTER";
726 case AMOTION_EVENT_ACTION_HOVER_EXIT:
727 return "HOVER_EXIT";
728 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
729 return "BUTTON_PRESS";
730 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
731 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700732 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500733 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700734}
735
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800736// --- FocusEvent ---
737
Garfield Tan4cc839f2020-01-24 11:26:14 -0800738void FocusEvent::initialize(int32_t id, bool hasFocus, bool inTouchMode) {
739 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600740 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800741 mHasFocus = hasFocus;
742 mInTouchMode = inTouchMode;
743}
744
745void FocusEvent::initialize(const FocusEvent& from) {
746 InputEvent::initialize(from);
747 mHasFocus = from.mHasFocus;
748 mInTouchMode = from.mInTouchMode;
749}
Jeff Brown5912f952013-07-01 19:10:31 -0700750
751// --- PooledInputEventFactory ---
752
753PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
754 mMaxPoolSize(maxPoolSize) {
755}
756
757PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -0700758}
759
760KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800761 if (mKeyEventPool.empty()) {
762 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700763 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800764 KeyEvent* event = mKeyEventPool.front().release();
765 mKeyEventPool.pop();
766 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700767}
768
769MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800770 if (mMotionEventPool.empty()) {
771 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700772 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800773 MotionEvent* event = mMotionEventPool.front().release();
774 mMotionEventPool.pop();
775 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700776}
777
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800778FocusEvent* PooledInputEventFactory::createFocusEvent() {
779 if (mFocusEventPool.empty()) {
780 return new FocusEvent();
781 }
782 FocusEvent* event = mFocusEventPool.front().release();
783 mFocusEventPool.pop();
784 return event;
785}
786
Jeff Brown5912f952013-07-01 19:10:31 -0700787void PooledInputEventFactory::recycle(InputEvent* event) {
788 switch (event->getType()) {
789 case AINPUT_EVENT_TYPE_KEY:
790 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800791 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700792 return;
793 }
794 break;
795 case AINPUT_EVENT_TYPE_MOTION:
796 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800797 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700798 return;
799 }
800 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800801 case AINPUT_EVENT_TYPE_FOCUS:
802 if (mFocusEventPool.size() < mMaxPoolSize) {
803 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
804 return;
805 }
806 break;
Jeff Brown5912f952013-07-01 19:10:31 -0700807 }
808 delete event;
809}
810
811} // namespace android