blob: c6604cbe3dd0f1226ba59a20ce2ee4c4368a22c5 [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
Elliott Hughes6071da72015-08-12 15:27:47 -070031#ifdef __ANDROID__
Jeff Brown5912f952013-07-01 19:10:31 -070032#include <binder/Parcel.h>
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
Elliott Hughes6071da72015-08-12 15:27:47 -0700257#ifdef __ANDROID__
Jeff Brown5912f952013-07-01 19:10:31 -0700258status_t PointerCoords::readFromParcel(Parcel* parcel) {
259 bits = parcel->readInt64();
260
Michael Wright38dcdff2014-03-19 12:06:10 -0700261 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700262 if (count > MAX_AXES) {
263 return BAD_VALUE;
264 }
265
266 for (uint32_t i = 0; i < count; i++) {
267 values[i] = parcel->readFloat();
268 }
269 return OK;
270}
271
272status_t PointerCoords::writeToParcel(Parcel* parcel) const {
273 parcel->writeInt64(bits);
274
Michael Wright38dcdff2014-03-19 12:06:10 -0700275 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700276 for (uint32_t i = 0; i < count; i++) {
277 parcel->writeFloat(values[i]);
278 }
279 return OK;
280}
281#endif
282
283void PointerCoords::tooManyAxes(int axis) {
284 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
285 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
286}
287
288bool PointerCoords::operator==(const PointerCoords& other) const {
289 if (bits != other.bits) {
290 return false;
291 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700292 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700293 for (uint32_t i = 0; i < count; i++) {
294 if (values[i] != other.values[i]) {
295 return false;
296 }
297 }
298 return true;
299}
300
301void PointerCoords::copyFrom(const PointerCoords& other) {
302 bits = other.bits;
Michael Wright38dcdff2014-03-19 12:06:10 -0700303 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700304 for (uint32_t i = 0; i < count; i++) {
305 values[i] = other.values[i];
306 }
307}
308
chaviwc01e1372020-07-01 12:37:31 -0700309void PointerCoords::transform(const ui::Transform& transform) {
310 vec2 newCoords = transform.transform(getX(), getY());
311 setAxisValue(AMOTION_EVENT_AXIS_X, newCoords.x);
312 setAxisValue(AMOTION_EVENT_AXIS_Y, newCoords.y);
313}
Jeff Brown5912f952013-07-01 19:10:31 -0700314
315// --- PointerProperties ---
316
317bool PointerProperties::operator==(const PointerProperties& other) const {
318 return id == other.id
319 && toolType == other.toolType;
320}
321
322void PointerProperties::copyFrom(const PointerProperties& other) {
323 id = other.id;
324 toolType = other.toolType;
325}
326
327
328// --- MotionEvent ---
329
Garfield Tan4cc839f2020-01-24 11:26:14 -0800330void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600331 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
332 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700333 int32_t buttonState, MotionClassification classification,
334 const ui::Transform& transform, float xPrecision, float yPrecision,
335 float rawXCursorPosition, float rawYCursorPosition, nsecs_t downTime,
336 nsecs_t eventTime, size_t pointerCount,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600337 const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700338 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800339 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700340 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100341 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700342 mFlags = flags;
343 mEdgeFlags = edgeFlags;
344 mMetaState = metaState;
345 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800346 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700347 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700348 mXPrecision = xPrecision;
349 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700350 mRawXCursorPosition = rawXCursorPosition;
351 mRawYCursorPosition = rawYCursorPosition;
Jeff Brown5912f952013-07-01 19:10:31 -0700352 mDownTime = downTime;
353 mPointerProperties.clear();
354 mPointerProperties.appendArray(pointerProperties, pointerCount);
355 mSampleEventTimes.clear();
356 mSamplePointerCoords.clear();
357 addSample(eventTime, pointerCoords);
358}
359
360void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800361 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
362 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700363 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100364 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700365 mFlags = other->mFlags;
366 mEdgeFlags = other->mEdgeFlags;
367 mMetaState = other->mMetaState;
368 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800369 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700370 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700371 mXPrecision = other->mXPrecision;
372 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700373 mRawXCursorPosition = other->mRawXCursorPosition;
374 mRawYCursorPosition = other->mRawYCursorPosition;
Jeff Brown5912f952013-07-01 19:10:31 -0700375 mDownTime = other->mDownTime;
376 mPointerProperties = other->mPointerProperties;
377
378 if (keepHistory) {
379 mSampleEventTimes = other->mSampleEventTimes;
380 mSamplePointerCoords = other->mSamplePointerCoords;
381 } else {
382 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500383 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700384 mSamplePointerCoords.clear();
385 size_t pointerCount = other->getPointerCount();
386 size_t historySize = other->getHistorySize();
387 mSamplePointerCoords.appendArray(other->mSamplePointerCoords.array()
388 + (historySize * pointerCount), pointerCount);
389 }
390}
391
392void MotionEvent::addSample(
393 int64_t eventTime,
394 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500395 mSampleEventTimes.push_back(eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -0700396 mSamplePointerCoords.appendArray(pointerCoords, getPointerCount());
397}
398
Garfield Tan00f511d2019-06-12 16:55:40 -0700399float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700400 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
401 return vals.x;
Garfield Tan00f511d2019-06-12 16:55:40 -0700402}
403
404float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700405 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
406 return vals.y;
Garfield Tan00f511d2019-06-12 16:55:40 -0700407}
408
Garfield Tan937bb832019-07-25 17:48:31 -0700409void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700410 ui::Transform inverse = mTransform.inverse();
411 vec2 vals = inverse.transform(x, y);
412 mRawXCursorPosition = vals.x;
413 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700414}
415
Jeff Brown5912f952013-07-01 19:10:31 -0700416const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
417 return &mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
418}
419
420float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
421 return getRawPointerCoords(pointerIndex)->getAxisValue(axis);
422}
423
424float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700425 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700426}
427
428const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
429 size_t pointerIndex, size_t historicalIndex) const {
430 return &mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
431}
432
433float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
434 size_t historicalIndex) const {
435 return getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis);
436}
437
438float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
439 size_t historicalIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700440 if (axis != AMOTION_EVENT_AXIS_X && axis != AMOTION_EVENT_AXIS_Y) {
441 return getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis);
442 }
443
444 float rawX = getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getX();
445 float rawY = getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getY();
446 vec2 vals = mTransform.transform(rawX, rawY);
447
Jeff Brown5912f952013-07-01 19:10:31 -0700448 switch (axis) {
449 case AMOTION_EVENT_AXIS_X:
chaviw9eaa22c2020-07-01 16:21:27 -0700450 return vals.x;
Jeff Brown5912f952013-07-01 19:10:31 -0700451 case AMOTION_EVENT_AXIS_Y:
chaviw9eaa22c2020-07-01 16:21:27 -0700452 return vals.y;
Jeff Brown5912f952013-07-01 19:10:31 -0700453 }
chaviw9eaa22c2020-07-01 16:21:27 -0700454
455 // This should never happen
456 return 0;
Jeff Brown5912f952013-07-01 19:10:31 -0700457}
458
459ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
460 size_t pointerCount = mPointerProperties.size();
461 for (size_t i = 0; i < pointerCount; i++) {
462 if (mPointerProperties.itemAt(i).id == pointerId) {
463 return i;
464 }
465 }
466 return -1;
467}
468
469void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700470 float currXOffset = mTransform.tx();
471 float currYOffset = mTransform.ty();
472 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700473}
474
Robert Carre07e1032018-11-26 12:55:53 -0800475void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700476 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800477 mXPrecision *= globalScaleFactor;
478 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700479
480 size_t numSamples = mSamplePointerCoords.size();
481 for (size_t i = 0; i < numSamples; i++) {
chaviw9eaa22c2020-07-01 16:21:27 -0700482 mSamplePointerCoords.editItemAt(i).scale(globalScaleFactor, globalScaleFactor,
483 globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700484 }
485}
486
chaviw9eaa22c2020-07-01 16:21:27 -0700487static vec2 transformPoint(const std::array<float, 9>& matrix, float x, float y) {
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700488 // Apply perspective transform like Skia.
489 float newX = matrix[0] * x + matrix[1] * y + matrix[2];
490 float newY = matrix[3] * x + matrix[4] * y + matrix[5];
491 float newZ = matrix[6] * x + matrix[7] * y + matrix[8];
492 if (newZ) {
493 newZ = 1.0f / newZ;
494 }
chaviw9eaa22c2020-07-01 16:21:27 -0700495 vec2 transformedPoint;
496 transformedPoint.x = newX * newZ;
497 transformedPoint.y = newY * newZ;
498 return transformedPoint;
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700499}
500
chaviw9eaa22c2020-07-01 16:21:27 -0700501static float transformAngle(const std::array<float, 9>& matrix, float angleRadians, float originX,
502 float originY) {
Jeff Brown5912f952013-07-01 19:10:31 -0700503 // Construct and transform a vector oriented at the specified clockwise angle from vertical.
504 // Coordinate system: down is increasing Y, right is increasing X.
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700505 float x = sinf(angleRadians);
506 float y = -cosf(angleRadians);
chaviw9eaa22c2020-07-01 16:21:27 -0700507 vec2 transformedPoint = transformPoint(matrix, x, y);
508
509 transformedPoint.x -= originX;
510 transformedPoint.y -= originY;
Jeff Brown5912f952013-07-01 19:10:31 -0700511
512 // Derive the transformed vector's clockwise angle from vertical.
chaviw9eaa22c2020-07-01 16:21:27 -0700513 float result = atan2f(transformedPoint.x, -transformedPoint.y);
Jeff Brown5912f952013-07-01 19:10:31 -0700514 if (result < - M_PI_2) {
515 result += M_PI;
516 } else if (result > M_PI_2) {
517 result -= M_PI;
518 }
519 return result;
520}
521
chaviw9eaa22c2020-07-01 16:21:27 -0700522void MotionEvent::transform(const std::array<float, 9>& matrix) {
523 // We want to preserve the rawX and rawY so we just update the transform
524 // using the values of the transform passed in
525 ui::Transform newTransform;
526 newTransform.set(matrix);
527 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700528
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700529 // Determine how the origin is transformed by the matrix so that we
530 // can transform orientation vectors.
chaviw9eaa22c2020-07-01 16:21:27 -0700531 vec2 origin = transformPoint(matrix, 0, 0);
Garfield Tan00f511d2019-06-12 16:55:40 -0700532
Jeff Brown5912f952013-07-01 19:10:31 -0700533 // Apply the transformation to all samples.
534 size_t numSamples = mSamplePointerCoords.size();
535 for (size_t i = 0; i < numSamples; i++) {
536 PointerCoords& c = mSamplePointerCoords.editItemAt(i);
Jeff Brown5912f952013-07-01 19:10:31 -0700537 float orientation = c.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700538 c.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
chaviw9eaa22c2020-07-01 16:21:27 -0700539 transformAngle(matrix, orientation, origin.x, origin.y));
Jeff Brown5912f952013-07-01 19:10:31 -0700540 }
541}
542
Elliott Hughes6071da72015-08-12 15:27:47 -0700543#ifdef __ANDROID__
chaviw9eaa22c2020-07-01 16:21:27 -0700544static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
545 float dsdx, dtdx, tx, dtdy, dsdy, ty;
546 status_t status = parcel.readFloat(&dsdx);
547 status |= parcel.readFloat(&dtdx);
548 status |= parcel.readFloat(&tx);
549 status |= parcel.readFloat(&dtdy);
550 status |= parcel.readFloat(&dsdy);
551 status |= parcel.readFloat(&ty);
552
553 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
554 return status;
555}
556
557static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
558 status_t status = parcel.writeFloat(transform.dsdx());
559 status |= parcel.writeFloat(transform.dtdx());
560 status |= parcel.writeFloat(transform.tx());
561 status |= parcel.writeFloat(transform.dtdy());
562 status |= parcel.writeFloat(transform.dsdy());
563 status |= parcel.writeFloat(transform.ty());
564 return status;
565}
566
Jeff Brown5912f952013-07-01 19:10:31 -0700567status_t MotionEvent::readFromParcel(Parcel* parcel) {
568 size_t pointerCount = parcel->readInt32();
569 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800570 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
571 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700572 return BAD_VALUE;
573 }
574
Garfield Tan4cc839f2020-01-24 11:26:14 -0800575 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700576 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600577 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800578 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600579 std::vector<uint8_t> hmac;
580 status_t result = parcel->readByteVector(&hmac);
581 if (result != OK || hmac.size() != 32) {
582 return BAD_VALUE;
583 }
584 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700585 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100586 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700587 mFlags = parcel->readInt32();
588 mEdgeFlags = parcel->readInt32();
589 mMetaState = parcel->readInt32();
590 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800591 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700592
593 result = android::readFromParcel(mTransform, *parcel);
594 if (result != OK) {
595 return result;
596 }
Jeff Brown5912f952013-07-01 19:10:31 -0700597 mXPrecision = parcel->readFloat();
598 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700599 mRawXCursorPosition = parcel->readFloat();
600 mRawYCursorPosition = parcel->readFloat();
Jeff Brown5912f952013-07-01 19:10:31 -0700601 mDownTime = parcel->readInt64();
602
603 mPointerProperties.clear();
604 mPointerProperties.setCapacity(pointerCount);
605 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500606 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700607 mSamplePointerCoords.clear();
608 mSamplePointerCoords.setCapacity(sampleCount * pointerCount);
609
610 for (size_t i = 0; i < pointerCount; i++) {
611 mPointerProperties.push();
612 PointerProperties& properties = mPointerProperties.editTop();
613 properties.id = parcel->readInt32();
614 properties.toolType = parcel->readInt32();
615 }
616
Dan Austinc94fc452015-09-22 14:22:41 -0700617 while (sampleCount > 0) {
618 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500619 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700620 for (size_t i = 0; i < pointerCount; i++) {
621 mSamplePointerCoords.push();
622 status_t status = mSamplePointerCoords.editTop().readFromParcel(parcel);
623 if (status) {
624 return status;
625 }
626 }
627 }
628 return OK;
629}
630
631status_t MotionEvent::writeToParcel(Parcel* parcel) const {
632 size_t pointerCount = mPointerProperties.size();
633 size_t sampleCount = mSampleEventTimes.size();
634
635 parcel->writeInt32(pointerCount);
636 parcel->writeInt32(sampleCount);
637
Garfield Tan4cc839f2020-01-24 11:26:14 -0800638 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700639 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600640 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800641 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600642 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
643 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700644 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100645 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700646 parcel->writeInt32(mFlags);
647 parcel->writeInt32(mEdgeFlags);
648 parcel->writeInt32(mMetaState);
649 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800650 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700651
652 status_t result = android::writeToParcel(mTransform, *parcel);
653 if (result != OK) {
654 return result;
655 }
Jeff Brown5912f952013-07-01 19:10:31 -0700656 parcel->writeFloat(mXPrecision);
657 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700658 parcel->writeFloat(mRawXCursorPosition);
659 parcel->writeFloat(mRawYCursorPosition);
Jeff Brown5912f952013-07-01 19:10:31 -0700660 parcel->writeInt64(mDownTime);
661
662 for (size_t i = 0; i < pointerCount; i++) {
663 const PointerProperties& properties = mPointerProperties.itemAt(i);
664 parcel->writeInt32(properties.id);
665 parcel->writeInt32(properties.toolType);
666 }
667
668 const PointerCoords* pc = mSamplePointerCoords.array();
669 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500670 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700671 for (size_t i = 0; i < pointerCount; i++) {
672 status_t status = (pc++)->writeToParcel(parcel);
673 if (status) {
674 return status;
675 }
676 }
677 }
678 return OK;
679}
680#endif
681
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600682bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Jeff Brown5912f952013-07-01 19:10:31 -0700683 if (source & AINPUT_SOURCE_CLASS_POINTER) {
684 // Specifically excludes HOVER_MOVE and SCROLL.
685 switch (action & AMOTION_EVENT_ACTION_MASK) {
686 case AMOTION_EVENT_ACTION_DOWN:
687 case AMOTION_EVENT_ACTION_MOVE:
688 case AMOTION_EVENT_ACTION_UP:
689 case AMOTION_EVENT_ACTION_POINTER_DOWN:
690 case AMOTION_EVENT_ACTION_POINTER_UP:
691 case AMOTION_EVENT_ACTION_CANCEL:
692 case AMOTION_EVENT_ACTION_OUTSIDE:
693 return true;
694 }
695 }
696 return false;
697}
698
Michael Wright872db4f2014-04-22 15:03:51 -0700699const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700700 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700701}
702
703int32_t MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700704 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700705}
706
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500707std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700708 // Convert MotionEvent action to string
709 switch (action & AMOTION_EVENT_ACTION_MASK) {
710 case AMOTION_EVENT_ACTION_DOWN:
711 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700712 case AMOTION_EVENT_ACTION_UP:
713 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500714 case AMOTION_EVENT_ACTION_MOVE:
715 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700716 case AMOTION_EVENT_ACTION_CANCEL:
717 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500718 case AMOTION_EVENT_ACTION_OUTSIDE:
719 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700720 case AMOTION_EVENT_ACTION_POINTER_DOWN:
721 return "POINTER_DOWN";
722 case AMOTION_EVENT_ACTION_POINTER_UP:
723 return "POINTER_UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500724 case AMOTION_EVENT_ACTION_HOVER_MOVE:
725 return "HOVER_MOVE";
726 case AMOTION_EVENT_ACTION_SCROLL:
727 return "SCROLL";
728 case AMOTION_EVENT_ACTION_HOVER_ENTER:
729 return "HOVER_ENTER";
730 case AMOTION_EVENT_ACTION_HOVER_EXIT:
731 return "HOVER_EXIT";
732 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
733 return "BUTTON_PRESS";
734 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
735 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700736 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500737 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700738}
739
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800740// --- FocusEvent ---
741
Garfield Tan4cc839f2020-01-24 11:26:14 -0800742void FocusEvent::initialize(int32_t id, bool hasFocus, bool inTouchMode) {
743 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600744 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800745 mHasFocus = hasFocus;
746 mInTouchMode = inTouchMode;
747}
748
749void FocusEvent::initialize(const FocusEvent& from) {
750 InputEvent::initialize(from);
751 mHasFocus = from.mHasFocus;
752 mInTouchMode = from.mInTouchMode;
753}
Jeff Brown5912f952013-07-01 19:10:31 -0700754
755// --- PooledInputEventFactory ---
756
757PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
758 mMaxPoolSize(maxPoolSize) {
759}
760
761PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -0700762}
763
764KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800765 if (mKeyEventPool.empty()) {
766 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700767 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800768 KeyEvent* event = mKeyEventPool.front().release();
769 mKeyEventPool.pop();
770 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700771}
772
773MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800774 if (mMotionEventPool.empty()) {
775 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700776 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800777 MotionEvent* event = mMotionEventPool.front().release();
778 mMotionEventPool.pop();
779 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700780}
781
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800782FocusEvent* PooledInputEventFactory::createFocusEvent() {
783 if (mFocusEventPool.empty()) {
784 return new FocusEvent();
785 }
786 FocusEvent* event = mFocusEventPool.front().release();
787 mFocusEventPool.pop();
788 return event;
789}
790
Jeff Brown5912f952013-07-01 19:10:31 -0700791void PooledInputEventFactory::recycle(InputEvent* event) {
792 switch (event->getType()) {
793 case AINPUT_EVENT_TYPE_KEY:
794 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800795 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700796 return;
797 }
798 break;
799 case AINPUT_EVENT_TYPE_MOTION:
800 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800801 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700802 return;
803 }
804 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800805 case AINPUT_EVENT_TYPE_FOCUS:
806 if (mFocusEventPool.size() < mMaxPoolSize) {
807 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
808 return;
809 }
810 break;
Jeff Brown5912f952013-07-01 19:10:31 -0700811 }
812 delete event;
813}
814
815} // namespace android