blob: 0a00d685564f40ed28a0d518d9b11027169f4fe5 [file] [log] [blame]
Jeff Brown5912f952013-07-01 19:10:31 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Input"
18//#define LOG_NDEBUG 0
19
chaviw09c8d2d2020-08-24 15:48:26 -070020#include <attestation/HmacKeyManager.h>
Garfield Tan84b087e2020-01-23 10:49:05 -080021#include <cutils/compiler.h>
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050022#include <inttypes.h>
Jeff Brown5912f952013-07-01 19:10:31 -070023#include <limits.h>
Garfield Tan84b087e2020-01-23 10:49:05 -080024#include <string.h>
Jeff Brown5912f952013-07-01 19:10:31 -070025
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050026#include <android-base/stringprintf.h>
Jeff Brown5912f952013-07-01 19:10:31 -070027#include <input/Input.h>
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080028#include <input/InputDevice.h>
Michael Wright872db4f2014-04-22 15:03:51 -070029#include <input/InputEventLabels.h>
Jeff Brown5912f952013-07-01 19:10:31 -070030
Brett Chabotfaa986c2020-11-04 17:39:36 -080031#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -070032#include <binder/Parcel.h>
Brett Chabotfaa986c2020-11-04 17:39:36 -080033#endif
Brett Chabot58208522020-09-09 13:55:24 -070034#ifdef __ANDROID__
Garfield Tan84b087e2020-01-23 10:49:05 -080035#include <sys/random.h>
Jeff Brown5912f952013-07-01 19:10:31 -070036#endif
37
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050038using android::base::StringPrintf;
39
Jeff Brown5912f952013-07-01 19:10:31 -070040namespace android {
41
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -080042const char* motionClassificationToString(MotionClassification classification) {
43 switch (classification) {
44 case MotionClassification::NONE:
45 return "NONE";
46 case MotionClassification::AMBIGUOUS_GESTURE:
47 return "AMBIGUOUS_GESTURE";
48 case MotionClassification::DEEP_PRESS:
49 return "DEEP_PRESS";
50 }
51}
52
Garfield Tan84b087e2020-01-23 10:49:05 -080053// --- IdGenerator ---
54IdGenerator::IdGenerator(Source source) : mSource(source) {}
55
56int32_t IdGenerator::nextId() const {
57 constexpr uint32_t SEQUENCE_NUMBER_MASK = ~SOURCE_MASK;
58 int32_t id = 0;
59
60// Avoid building against syscall getrandom(2) on host, which will fail build on Mac. Host doesn't
61// use sequence number so just always return mSource.
62#ifdef __ANDROID__
63 constexpr size_t BUF_LEN = sizeof(id);
64 size_t totalBytes = 0;
65 while (totalBytes < BUF_LEN) {
66 ssize_t bytes = TEMP_FAILURE_RETRY(getrandom(&id, BUF_LEN, GRND_NONBLOCK));
67 if (CC_UNLIKELY(bytes < 0)) {
68 ALOGW("Failed to fill in random number for sequence number: %s.", strerror(errno));
69 id = 0;
70 break;
71 }
72 totalBytes += bytes;
73 }
74#endif // __ANDROID__
75
76 return (id & SEQUENCE_NUMBER_MASK) | static_cast<int32_t>(mSource);
77}
78
Jeff Brown5912f952013-07-01 19:10:31 -070079// --- InputEvent ---
80
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080081const char* inputEventTypeToString(int32_t type) {
82 switch (type) {
83 case AINPUT_EVENT_TYPE_KEY: {
84 return "KEY";
85 }
86 case AINPUT_EVENT_TYPE_MOTION: {
87 return "MOTION";
88 }
89 case AINPUT_EVENT_TYPE_FOCUS: {
90 return "FOCUS";
91 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -080092 case AINPUT_EVENT_TYPE_CAPTURE: {
93 return "CAPTURE";
94 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080095 }
96 return "UNKNOWN";
97}
98
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -080099VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
100 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
101 event.getSource(), event.getDisplayId()},
102 event.getAction(),
103 event.getDownTime(),
104 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
105 event.getKeyCode(),
106 event.getScanCode(),
107 event.getMetaState(),
108 event.getRepeatCount()};
109}
110
111VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
112 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
113 event.getSource(), event.getDisplayId()},
114 event.getRawX(0),
115 event.getRawY(0),
116 event.getActionMasked(),
117 event.getDownTime(),
118 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
119 event.getMetaState(),
120 event.getButtonState()};
121}
122
Garfield Tan4cc839f2020-01-24 11:26:14 -0800123void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600124 std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800125 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700126 mDeviceId = deviceId;
127 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100128 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600129 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700130}
131
132void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800133 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700134 mDeviceId = from.mDeviceId;
135 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100136 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600137 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700138}
139
Garfield Tan4cc839f2020-01-24 11:26:14 -0800140int32_t InputEvent::nextId() {
141 static IdGenerator idGen(IdGenerator::Source::OTHER);
142 return idGen.nextId();
143}
144
Jeff Brown5912f952013-07-01 19:10:31 -0700145// --- KeyEvent ---
146
Michael Wright872db4f2014-04-22 15:03:51 -0700147const char* KeyEvent::getLabel(int32_t keyCode) {
Chris Ye4958d062020-08-20 13:21:10 -0700148 return InputEventLookup::getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700149}
150
Michael Wright872db4f2014-04-22 15:03:51 -0700151int32_t KeyEvent::getKeyCodeFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700152 return InputEventLookup::getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700153}
154
Garfield Tan4cc839f2020-01-24 11:26:14 -0800155void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600156 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
157 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
158 nsecs_t downTime, nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800159 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700160 mAction = action;
161 mFlags = flags;
162 mKeyCode = keyCode;
163 mScanCode = scanCode;
164 mMetaState = metaState;
165 mRepeatCount = repeatCount;
166 mDownTime = downTime;
167 mEventTime = eventTime;
168}
169
170void KeyEvent::initialize(const KeyEvent& from) {
171 InputEvent::initialize(from);
172 mAction = from.mAction;
173 mFlags = from.mFlags;
174 mKeyCode = from.mKeyCode;
175 mScanCode = from.mScanCode;
176 mMetaState = from.mMetaState;
177 mRepeatCount = from.mRepeatCount;
178 mDownTime = from.mDownTime;
179 mEventTime = from.mEventTime;
180}
181
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700182const char* KeyEvent::actionToString(int32_t action) {
183 // Convert KeyEvent action to string
184 switch (action) {
185 case AKEY_EVENT_ACTION_DOWN:
186 return "DOWN";
187 case AKEY_EVENT_ACTION_UP:
188 return "UP";
189 case AKEY_EVENT_ACTION_MULTIPLE:
190 return "MULTIPLE";
191 }
192 return "UNKNOWN";
193}
Jeff Brown5912f952013-07-01 19:10:31 -0700194
195// --- PointerCoords ---
196
197float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700198 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700199 return 0;
200 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700201 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700202}
203
204status_t PointerCoords::setAxisValue(int32_t axis, float value) {
205 if (axis < 0 || axis > 63) {
206 return NAME_NOT_FOUND;
207 }
208
Michael Wright38dcdff2014-03-19 12:06:10 -0700209 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
210 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700211 if (value == 0) {
212 return OK; // axes with value 0 do not need to be stored
213 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700214
215 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700216 if (count >= MAX_AXES) {
217 tooManyAxes(axis);
218 return NO_MEMORY;
219 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700220 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700221 for (uint32_t i = count; i > index; i--) {
222 values[i] = values[i - 1];
223 }
224 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700225
Jeff Brown5912f952013-07-01 19:10:31 -0700226 values[index] = value;
227 return OK;
228}
229
230static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
231 float value = c.getAxisValue(axis);
232 if (value != 0) {
233 c.setAxisValue(axis, value * scaleFactor);
234 }
235}
236
Robert Carre07e1032018-11-26 12:55:53 -0800237void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700238 // No need to scale pressure or size since they are normalized.
239 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800240
241 // If there is a global scale factor, it is included in the windowX/YScale
242 // so we don't need to apply it twice to the X/Y axes.
243 // However we don't want to apply any windowXYScale not included in the global scale
244 // to the TOUCH_MAJOR/MINOR coordinates.
245 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
246 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
247 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
248 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
249 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
250 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
251}
252
253void PointerCoords::scale(float globalScaleFactor) {
254 scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700255}
256
Jeff Brownf086ddb2014-02-11 14:28:48 -0800257void PointerCoords::applyOffset(float xOffset, float yOffset) {
258 setAxisValue(AMOTION_EVENT_AXIS_X, getX() + xOffset);
259 setAxisValue(AMOTION_EVENT_AXIS_Y, getY() + yOffset);
260}
261
Brett Chabotfaa986c2020-11-04 17:39:36 -0800262#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700263status_t PointerCoords::readFromParcel(Parcel* parcel) {
264 bits = parcel->readInt64();
265
Michael Wright38dcdff2014-03-19 12:06:10 -0700266 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700267 if (count > MAX_AXES) {
268 return BAD_VALUE;
269 }
270
271 for (uint32_t i = 0; i < count; i++) {
272 values[i] = parcel->readFloat();
273 }
274 return OK;
275}
276
277status_t PointerCoords::writeToParcel(Parcel* parcel) const {
278 parcel->writeInt64(bits);
279
Michael Wright38dcdff2014-03-19 12:06:10 -0700280 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700281 for (uint32_t i = 0; i < count; i++) {
282 parcel->writeFloat(values[i]);
283 }
284 return OK;
285}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800286#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700287
288void PointerCoords::tooManyAxes(int axis) {
289 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
290 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
291}
292
293bool PointerCoords::operator==(const PointerCoords& other) const {
294 if (bits != other.bits) {
295 return false;
296 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700297 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700298 for (uint32_t i = 0; i < count; i++) {
299 if (values[i] != other.values[i]) {
300 return false;
301 }
302 }
303 return true;
304}
305
306void PointerCoords::copyFrom(const PointerCoords& other) {
307 bits = other.bits;
Michael Wright38dcdff2014-03-19 12:06:10 -0700308 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700309 for (uint32_t i = 0; i < count; i++) {
310 values[i] = other.values[i];
311 }
312}
313
chaviwc01e1372020-07-01 12:37:31 -0700314void PointerCoords::transform(const ui::Transform& transform) {
315 vec2 newCoords = transform.transform(getX(), getY());
316 setAxisValue(AMOTION_EVENT_AXIS_X, newCoords.x);
317 setAxisValue(AMOTION_EVENT_AXIS_Y, newCoords.y);
318}
Jeff Brown5912f952013-07-01 19:10:31 -0700319
320// --- PointerProperties ---
321
322bool PointerProperties::operator==(const PointerProperties& other) const {
323 return id == other.id
324 && toolType == other.toolType;
325}
326
327void PointerProperties::copyFrom(const PointerProperties& other) {
328 id = other.id;
329 toolType = other.toolType;
330}
331
332
333// --- MotionEvent ---
334
Garfield Tan4cc839f2020-01-24 11:26:14 -0800335void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600336 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
337 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700338 int32_t buttonState, MotionClassification classification,
339 const ui::Transform& transform, float xPrecision, float yPrecision,
340 float rawXCursorPosition, float rawYCursorPosition, nsecs_t downTime,
341 nsecs_t eventTime, size_t pointerCount,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600342 const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700343 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800344 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700345 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100346 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700347 mFlags = flags;
348 mEdgeFlags = edgeFlags;
349 mMetaState = metaState;
350 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800351 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700352 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700353 mXPrecision = xPrecision;
354 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700355 mRawXCursorPosition = rawXCursorPosition;
356 mRawYCursorPosition = rawYCursorPosition;
Jeff Brown5912f952013-07-01 19:10:31 -0700357 mDownTime = downTime;
358 mPointerProperties.clear();
359 mPointerProperties.appendArray(pointerProperties, pointerCount);
360 mSampleEventTimes.clear();
361 mSamplePointerCoords.clear();
362 addSample(eventTime, pointerCoords);
363}
364
365void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800366 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
367 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700368 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100369 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700370 mFlags = other->mFlags;
371 mEdgeFlags = other->mEdgeFlags;
372 mMetaState = other->mMetaState;
373 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800374 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700375 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700376 mXPrecision = other->mXPrecision;
377 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700378 mRawXCursorPosition = other->mRawXCursorPosition;
379 mRawYCursorPosition = other->mRawYCursorPosition;
Jeff Brown5912f952013-07-01 19:10:31 -0700380 mDownTime = other->mDownTime;
381 mPointerProperties = other->mPointerProperties;
382
383 if (keepHistory) {
384 mSampleEventTimes = other->mSampleEventTimes;
385 mSamplePointerCoords = other->mSamplePointerCoords;
386 } else {
387 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500388 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700389 mSamplePointerCoords.clear();
390 size_t pointerCount = other->getPointerCount();
391 size_t historySize = other->getHistorySize();
392 mSamplePointerCoords.appendArray(other->mSamplePointerCoords.array()
393 + (historySize * pointerCount), pointerCount);
394 }
395}
396
397void MotionEvent::addSample(
398 int64_t eventTime,
399 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500400 mSampleEventTimes.push_back(eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -0700401 mSamplePointerCoords.appendArray(pointerCoords, getPointerCount());
402}
403
Garfield Tan00f511d2019-06-12 16:55:40 -0700404float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700405 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
406 return vals.x;
Garfield Tan00f511d2019-06-12 16:55:40 -0700407}
408
409float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700410 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
411 return vals.y;
Garfield Tan00f511d2019-06-12 16:55:40 -0700412}
413
Garfield Tan937bb832019-07-25 17:48:31 -0700414void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700415 ui::Transform inverse = mTransform.inverse();
416 vec2 vals = inverse.transform(x, y);
417 mRawXCursorPosition = vals.x;
418 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700419}
420
Jeff Brown5912f952013-07-01 19:10:31 -0700421const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
422 return &mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
423}
424
425float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
426 return getRawPointerCoords(pointerIndex)->getAxisValue(axis);
427}
428
429float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700430 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700431}
432
433const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
434 size_t pointerIndex, size_t historicalIndex) const {
435 return &mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
436}
437
438float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
439 size_t historicalIndex) const {
440 return getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis);
441}
442
443float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
444 size_t historicalIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700445 if (axis != AMOTION_EVENT_AXIS_X && axis != AMOTION_EVENT_AXIS_Y) {
446 return getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis);
447 }
448
449 float rawX = getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getX();
450 float rawY = getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getY();
451 vec2 vals = mTransform.transform(rawX, rawY);
452
Jeff Brown5912f952013-07-01 19:10:31 -0700453 switch (axis) {
454 case AMOTION_EVENT_AXIS_X:
chaviw9eaa22c2020-07-01 16:21:27 -0700455 return vals.x;
Jeff Brown5912f952013-07-01 19:10:31 -0700456 case AMOTION_EVENT_AXIS_Y:
chaviw9eaa22c2020-07-01 16:21:27 -0700457 return vals.y;
Jeff Brown5912f952013-07-01 19:10:31 -0700458 }
chaviw9eaa22c2020-07-01 16:21:27 -0700459
460 // This should never happen
461 return 0;
Jeff Brown5912f952013-07-01 19:10:31 -0700462}
463
464ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
465 size_t pointerCount = mPointerProperties.size();
466 for (size_t i = 0; i < pointerCount; i++) {
467 if (mPointerProperties.itemAt(i).id == pointerId) {
468 return i;
469 }
470 }
471 return -1;
472}
473
474void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700475 float currXOffset = mTransform.tx();
476 float currYOffset = mTransform.ty();
477 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700478}
479
Robert Carre07e1032018-11-26 12:55:53 -0800480void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700481 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800482 mXPrecision *= globalScaleFactor;
483 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700484
485 size_t numSamples = mSamplePointerCoords.size();
486 for (size_t i = 0; i < numSamples; i++) {
chaviw9eaa22c2020-07-01 16:21:27 -0700487 mSamplePointerCoords.editItemAt(i).scale(globalScaleFactor, globalScaleFactor,
488 globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700489 }
490}
491
chaviw9eaa22c2020-07-01 16:21:27 -0700492static vec2 transformPoint(const std::array<float, 9>& matrix, float x, float y) {
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700493 // Apply perspective transform like Skia.
494 float newX = matrix[0] * x + matrix[1] * y + matrix[2];
495 float newY = matrix[3] * x + matrix[4] * y + matrix[5];
496 float newZ = matrix[6] * x + matrix[7] * y + matrix[8];
497 if (newZ) {
498 newZ = 1.0f / newZ;
499 }
chaviw9eaa22c2020-07-01 16:21:27 -0700500 vec2 transformedPoint;
501 transformedPoint.x = newX * newZ;
502 transformedPoint.y = newY * newZ;
503 return transformedPoint;
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700504}
505
chaviw9eaa22c2020-07-01 16:21:27 -0700506static float transformAngle(const std::array<float, 9>& matrix, float angleRadians, float originX,
507 float originY) {
Jeff Brown5912f952013-07-01 19:10:31 -0700508 // Construct and transform a vector oriented at the specified clockwise angle from vertical.
509 // Coordinate system: down is increasing Y, right is increasing X.
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700510 float x = sinf(angleRadians);
511 float y = -cosf(angleRadians);
chaviw9eaa22c2020-07-01 16:21:27 -0700512 vec2 transformedPoint = transformPoint(matrix, x, y);
513
514 transformedPoint.x -= originX;
515 transformedPoint.y -= originY;
Jeff Brown5912f952013-07-01 19:10:31 -0700516
517 // Derive the transformed vector's clockwise angle from vertical.
chaviw9eaa22c2020-07-01 16:21:27 -0700518 float result = atan2f(transformedPoint.x, -transformedPoint.y);
Jeff Brown5912f952013-07-01 19:10:31 -0700519 if (result < - M_PI_2) {
520 result += M_PI;
521 } else if (result > M_PI_2) {
522 result -= M_PI;
523 }
524 return result;
525}
526
chaviw9eaa22c2020-07-01 16:21:27 -0700527void MotionEvent::transform(const std::array<float, 9>& matrix) {
528 // We want to preserve the rawX and rawY so we just update the transform
529 // using the values of the transform passed in
530 ui::Transform newTransform;
531 newTransform.set(matrix);
532 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700533
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700534 // Determine how the origin is transformed by the matrix so that we
535 // can transform orientation vectors.
chaviw9eaa22c2020-07-01 16:21:27 -0700536 vec2 origin = transformPoint(matrix, 0, 0);
Garfield Tan00f511d2019-06-12 16:55:40 -0700537
Jeff Brown5912f952013-07-01 19:10:31 -0700538 // Apply the transformation to all samples.
539 size_t numSamples = mSamplePointerCoords.size();
540 for (size_t i = 0; i < numSamples; i++) {
541 PointerCoords& c = mSamplePointerCoords.editItemAt(i);
Jeff Brown5912f952013-07-01 19:10:31 -0700542 float orientation = c.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700543 c.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
chaviw9eaa22c2020-07-01 16:21:27 -0700544 transformAngle(matrix, orientation, origin.x, origin.y));
Jeff Brown5912f952013-07-01 19:10:31 -0700545 }
546}
547
Brett Chabotfaa986c2020-11-04 17:39:36 -0800548#ifdef __linux__
chaviw9eaa22c2020-07-01 16:21:27 -0700549static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
550 float dsdx, dtdx, tx, dtdy, dsdy, ty;
551 status_t status = parcel.readFloat(&dsdx);
552 status |= parcel.readFloat(&dtdx);
553 status |= parcel.readFloat(&tx);
554 status |= parcel.readFloat(&dtdy);
555 status |= parcel.readFloat(&dsdy);
556 status |= parcel.readFloat(&ty);
557
558 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
559 return status;
560}
561
562static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
563 status_t status = parcel.writeFloat(transform.dsdx());
564 status |= parcel.writeFloat(transform.dtdx());
565 status |= parcel.writeFloat(transform.tx());
566 status |= parcel.writeFloat(transform.dtdy());
567 status |= parcel.writeFloat(transform.dsdy());
568 status |= parcel.writeFloat(transform.ty());
569 return status;
570}
571
Jeff Brown5912f952013-07-01 19:10:31 -0700572status_t MotionEvent::readFromParcel(Parcel* parcel) {
573 size_t pointerCount = parcel->readInt32();
574 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800575 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
576 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700577 return BAD_VALUE;
578 }
579
Garfield Tan4cc839f2020-01-24 11:26:14 -0800580 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700581 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600582 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800583 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600584 std::vector<uint8_t> hmac;
585 status_t result = parcel->readByteVector(&hmac);
586 if (result != OK || hmac.size() != 32) {
587 return BAD_VALUE;
588 }
589 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700590 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100591 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700592 mFlags = parcel->readInt32();
593 mEdgeFlags = parcel->readInt32();
594 mMetaState = parcel->readInt32();
595 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800596 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700597
598 result = android::readFromParcel(mTransform, *parcel);
599 if (result != OK) {
600 return result;
601 }
Jeff Brown5912f952013-07-01 19:10:31 -0700602 mXPrecision = parcel->readFloat();
603 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700604 mRawXCursorPosition = parcel->readFloat();
605 mRawYCursorPosition = parcel->readFloat();
Jeff Brown5912f952013-07-01 19:10:31 -0700606 mDownTime = parcel->readInt64();
607
608 mPointerProperties.clear();
609 mPointerProperties.setCapacity(pointerCount);
610 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500611 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700612 mSamplePointerCoords.clear();
613 mSamplePointerCoords.setCapacity(sampleCount * pointerCount);
614
615 for (size_t i = 0; i < pointerCount; i++) {
616 mPointerProperties.push();
617 PointerProperties& properties = mPointerProperties.editTop();
618 properties.id = parcel->readInt32();
619 properties.toolType = parcel->readInt32();
620 }
621
Dan Austinc94fc452015-09-22 14:22:41 -0700622 while (sampleCount > 0) {
623 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500624 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700625 for (size_t i = 0; i < pointerCount; i++) {
626 mSamplePointerCoords.push();
627 status_t status = mSamplePointerCoords.editTop().readFromParcel(parcel);
628 if (status) {
629 return status;
630 }
631 }
632 }
633 return OK;
634}
635
636status_t MotionEvent::writeToParcel(Parcel* parcel) const {
637 size_t pointerCount = mPointerProperties.size();
638 size_t sampleCount = mSampleEventTimes.size();
639
640 parcel->writeInt32(pointerCount);
641 parcel->writeInt32(sampleCount);
642
Garfield Tan4cc839f2020-01-24 11:26:14 -0800643 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700644 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600645 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800646 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600647 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
648 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700649 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100650 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700651 parcel->writeInt32(mFlags);
652 parcel->writeInt32(mEdgeFlags);
653 parcel->writeInt32(mMetaState);
654 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800655 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700656
657 status_t result = android::writeToParcel(mTransform, *parcel);
658 if (result != OK) {
659 return result;
660 }
Jeff Brown5912f952013-07-01 19:10:31 -0700661 parcel->writeFloat(mXPrecision);
662 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700663 parcel->writeFloat(mRawXCursorPosition);
664 parcel->writeFloat(mRawYCursorPosition);
Jeff Brown5912f952013-07-01 19:10:31 -0700665 parcel->writeInt64(mDownTime);
666
667 for (size_t i = 0; i < pointerCount; i++) {
668 const PointerProperties& properties = mPointerProperties.itemAt(i);
669 parcel->writeInt32(properties.id);
670 parcel->writeInt32(properties.toolType);
671 }
672
673 const PointerCoords* pc = mSamplePointerCoords.array();
674 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500675 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700676 for (size_t i = 0; i < pointerCount; i++) {
677 status_t status = (pc++)->writeToParcel(parcel);
678 if (status) {
679 return status;
680 }
681 }
682 }
683 return OK;
684}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800685#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700686
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600687bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Jeff Brown5912f952013-07-01 19:10:31 -0700688 if (source & AINPUT_SOURCE_CLASS_POINTER) {
689 // Specifically excludes HOVER_MOVE and SCROLL.
690 switch (action & AMOTION_EVENT_ACTION_MASK) {
691 case AMOTION_EVENT_ACTION_DOWN:
692 case AMOTION_EVENT_ACTION_MOVE:
693 case AMOTION_EVENT_ACTION_UP:
694 case AMOTION_EVENT_ACTION_POINTER_DOWN:
695 case AMOTION_EVENT_ACTION_POINTER_UP:
696 case AMOTION_EVENT_ACTION_CANCEL:
697 case AMOTION_EVENT_ACTION_OUTSIDE:
698 return true;
699 }
700 }
701 return false;
702}
703
Michael Wright872db4f2014-04-22 15:03:51 -0700704const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700705 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700706}
707
708int32_t MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700709 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700710}
711
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500712std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700713 // Convert MotionEvent action to string
714 switch (action & AMOTION_EVENT_ACTION_MASK) {
715 case AMOTION_EVENT_ACTION_DOWN:
716 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700717 case AMOTION_EVENT_ACTION_UP:
718 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500719 case AMOTION_EVENT_ACTION_MOVE:
720 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700721 case AMOTION_EVENT_ACTION_CANCEL:
722 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500723 case AMOTION_EVENT_ACTION_OUTSIDE:
724 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700725 case AMOTION_EVENT_ACTION_POINTER_DOWN:
726 return "POINTER_DOWN";
727 case AMOTION_EVENT_ACTION_POINTER_UP:
728 return "POINTER_UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500729 case AMOTION_EVENT_ACTION_HOVER_MOVE:
730 return "HOVER_MOVE";
731 case AMOTION_EVENT_ACTION_SCROLL:
732 return "SCROLL";
733 case AMOTION_EVENT_ACTION_HOVER_ENTER:
734 return "HOVER_ENTER";
735 case AMOTION_EVENT_ACTION_HOVER_EXIT:
736 return "HOVER_EXIT";
737 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
738 return "BUTTON_PRESS";
739 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
740 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700741 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500742 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700743}
744
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800745// --- FocusEvent ---
746
Garfield Tan4cc839f2020-01-24 11:26:14 -0800747void FocusEvent::initialize(int32_t id, bool hasFocus, bool inTouchMode) {
748 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600749 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800750 mHasFocus = hasFocus;
751 mInTouchMode = inTouchMode;
752}
753
754void FocusEvent::initialize(const FocusEvent& from) {
755 InputEvent::initialize(from);
756 mHasFocus = from.mHasFocus;
757 mInTouchMode = from.mInTouchMode;
758}
Jeff Brown5912f952013-07-01 19:10:31 -0700759
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800760// --- CaptureEvent ---
761
762void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) {
763 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
764 ADISPLAY_ID_NONE, INVALID_HMAC);
765 mPointerCaptureEnabled = pointerCaptureEnabled;
766}
767
768void CaptureEvent::initialize(const CaptureEvent& from) {
769 InputEvent::initialize(from);
770 mPointerCaptureEnabled = from.mPointerCaptureEnabled;
771}
772
Jeff Brown5912f952013-07-01 19:10:31 -0700773// --- PooledInputEventFactory ---
774
775PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
776 mMaxPoolSize(maxPoolSize) {
777}
778
779PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -0700780}
781
782KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800783 if (mKeyEventPool.empty()) {
784 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700785 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800786 KeyEvent* event = mKeyEventPool.front().release();
787 mKeyEventPool.pop();
788 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700789}
790
791MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800792 if (mMotionEventPool.empty()) {
793 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700794 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800795 MotionEvent* event = mMotionEventPool.front().release();
796 mMotionEventPool.pop();
797 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700798}
799
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800800FocusEvent* PooledInputEventFactory::createFocusEvent() {
801 if (mFocusEventPool.empty()) {
802 return new FocusEvent();
803 }
804 FocusEvent* event = mFocusEventPool.front().release();
805 mFocusEventPool.pop();
806 return event;
807}
808
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800809CaptureEvent* PooledInputEventFactory::createCaptureEvent() {
810 if (mCaptureEventPool.empty()) {
811 return new CaptureEvent();
812 }
813 CaptureEvent* event = mCaptureEventPool.front().release();
814 mCaptureEventPool.pop();
815 return event;
816}
817
Jeff Brown5912f952013-07-01 19:10:31 -0700818void PooledInputEventFactory::recycle(InputEvent* event) {
819 switch (event->getType()) {
820 case AINPUT_EVENT_TYPE_KEY:
821 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800822 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700823 return;
824 }
825 break;
826 case AINPUT_EVENT_TYPE_MOTION:
827 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800828 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700829 return;
830 }
831 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800832 case AINPUT_EVENT_TYPE_FOCUS:
833 if (mFocusEventPool.size() < mMaxPoolSize) {
834 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
835 return;
836 }
837 break;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800838 case AINPUT_EVENT_TYPE_CAPTURE:
839 if (mCaptureEventPool.size() < mMaxPoolSize) {
840 mCaptureEventPool.push(
841 std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event)));
842 return;
843 }
844 break;
Jeff Brown5912f952013-07-01 19:10:31 -0700845 }
846 delete event;
847}
848
849} // namespace android