blob: 0ea38893383cbab4089aaafdb81c29803739bb65 [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 }
92 }
93 return "UNKNOWN";
94}
95
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -080096VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
97 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
98 event.getSource(), event.getDisplayId()},
99 event.getAction(),
100 event.getDownTime(),
101 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
102 event.getKeyCode(),
103 event.getScanCode(),
104 event.getMetaState(),
105 event.getRepeatCount()};
106}
107
108VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
109 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
110 event.getSource(), event.getDisplayId()},
111 event.getRawX(0),
112 event.getRawY(0),
113 event.getActionMasked(),
114 event.getDownTime(),
115 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
116 event.getMetaState(),
117 event.getButtonState()};
118}
119
Garfield Tan4cc839f2020-01-24 11:26:14 -0800120void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600121 std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800122 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700123 mDeviceId = deviceId;
124 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100125 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600126 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700127}
128
129void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800130 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700131 mDeviceId = from.mDeviceId;
132 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100133 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600134 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700135}
136
Garfield Tan4cc839f2020-01-24 11:26:14 -0800137int32_t InputEvent::nextId() {
138 static IdGenerator idGen(IdGenerator::Source::OTHER);
139 return idGen.nextId();
140}
141
Jeff Brown5912f952013-07-01 19:10:31 -0700142// --- KeyEvent ---
143
Michael Wright872db4f2014-04-22 15:03:51 -0700144const char* KeyEvent::getLabel(int32_t keyCode) {
Chris Ye4958d062020-08-20 13:21:10 -0700145 return InputEventLookup::getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700146}
147
Michael Wright872db4f2014-04-22 15:03:51 -0700148int32_t KeyEvent::getKeyCodeFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700149 return InputEventLookup::getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700150}
151
Garfield Tan4cc839f2020-01-24 11:26:14 -0800152void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600153 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
154 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
155 nsecs_t downTime, nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800156 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700157 mAction = action;
158 mFlags = flags;
159 mKeyCode = keyCode;
160 mScanCode = scanCode;
161 mMetaState = metaState;
162 mRepeatCount = repeatCount;
163 mDownTime = downTime;
164 mEventTime = eventTime;
165}
166
167void KeyEvent::initialize(const KeyEvent& from) {
168 InputEvent::initialize(from);
169 mAction = from.mAction;
170 mFlags = from.mFlags;
171 mKeyCode = from.mKeyCode;
172 mScanCode = from.mScanCode;
173 mMetaState = from.mMetaState;
174 mRepeatCount = from.mRepeatCount;
175 mDownTime = from.mDownTime;
176 mEventTime = from.mEventTime;
177}
178
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700179const char* KeyEvent::actionToString(int32_t action) {
180 // Convert KeyEvent action to string
181 switch (action) {
182 case AKEY_EVENT_ACTION_DOWN:
183 return "DOWN";
184 case AKEY_EVENT_ACTION_UP:
185 return "UP";
186 case AKEY_EVENT_ACTION_MULTIPLE:
187 return "MULTIPLE";
188 }
189 return "UNKNOWN";
190}
Jeff Brown5912f952013-07-01 19:10:31 -0700191
192// --- PointerCoords ---
193
194float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700195 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700196 return 0;
197 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700198 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700199}
200
201status_t PointerCoords::setAxisValue(int32_t axis, float value) {
202 if (axis < 0 || axis > 63) {
203 return NAME_NOT_FOUND;
204 }
205
Michael Wright38dcdff2014-03-19 12:06:10 -0700206 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
207 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700208 if (value == 0) {
209 return OK; // axes with value 0 do not need to be stored
210 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700211
212 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700213 if (count >= MAX_AXES) {
214 tooManyAxes(axis);
215 return NO_MEMORY;
216 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700217 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700218 for (uint32_t i = count; i > index; i--) {
219 values[i] = values[i - 1];
220 }
221 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700222
Jeff Brown5912f952013-07-01 19:10:31 -0700223 values[index] = value;
224 return OK;
225}
226
227static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
228 float value = c.getAxisValue(axis);
229 if (value != 0) {
230 c.setAxisValue(axis, value * scaleFactor);
231 }
232}
233
Robert Carre07e1032018-11-26 12:55:53 -0800234void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700235 // No need to scale pressure or size since they are normalized.
236 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800237
238 // If there is a global scale factor, it is included in the windowX/YScale
239 // so we don't need to apply it twice to the X/Y axes.
240 // However we don't want to apply any windowXYScale not included in the global scale
241 // to the TOUCH_MAJOR/MINOR coordinates.
242 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
243 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
244 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
245 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
246 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
247 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
248}
249
250void PointerCoords::scale(float globalScaleFactor) {
251 scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700252}
253
Jeff Brownf086ddb2014-02-11 14:28:48 -0800254void PointerCoords::applyOffset(float xOffset, float yOffset) {
255 setAxisValue(AMOTION_EVENT_AXIS_X, getX() + xOffset);
256 setAxisValue(AMOTION_EVENT_AXIS_Y, getY() + yOffset);
257}
258
Brett Chabotfaa986c2020-11-04 17:39:36 -0800259#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700260status_t PointerCoords::readFromParcel(Parcel* parcel) {
261 bits = parcel->readInt64();
262
Michael Wright38dcdff2014-03-19 12:06:10 -0700263 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700264 if (count > MAX_AXES) {
265 return BAD_VALUE;
266 }
267
268 for (uint32_t i = 0; i < count; i++) {
269 values[i] = parcel->readFloat();
270 }
271 return OK;
272}
273
274status_t PointerCoords::writeToParcel(Parcel* parcel) const {
275 parcel->writeInt64(bits);
276
Michael Wright38dcdff2014-03-19 12:06:10 -0700277 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700278 for (uint32_t i = 0; i < count; i++) {
279 parcel->writeFloat(values[i]);
280 }
281 return OK;
282}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800283#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700284
285void PointerCoords::tooManyAxes(int axis) {
286 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
287 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
288}
289
290bool PointerCoords::operator==(const PointerCoords& other) const {
291 if (bits != other.bits) {
292 return false;
293 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700294 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700295 for (uint32_t i = 0; i < count; i++) {
296 if (values[i] != other.values[i]) {
297 return false;
298 }
299 }
300 return true;
301}
302
303void PointerCoords::copyFrom(const PointerCoords& other) {
304 bits = other.bits;
Michael Wright38dcdff2014-03-19 12:06:10 -0700305 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700306 for (uint32_t i = 0; i < count; i++) {
307 values[i] = other.values[i];
308 }
309}
310
chaviwc01e1372020-07-01 12:37:31 -0700311void PointerCoords::transform(const ui::Transform& transform) {
312 vec2 newCoords = transform.transform(getX(), getY());
313 setAxisValue(AMOTION_EVENT_AXIS_X, newCoords.x);
314 setAxisValue(AMOTION_EVENT_AXIS_Y, newCoords.y);
315}
Jeff Brown5912f952013-07-01 19:10:31 -0700316
317// --- PointerProperties ---
318
319bool PointerProperties::operator==(const PointerProperties& other) const {
320 return id == other.id
321 && toolType == other.toolType;
322}
323
324void PointerProperties::copyFrom(const PointerProperties& other) {
325 id = other.id;
326 toolType = other.toolType;
327}
328
329
330// --- MotionEvent ---
331
Garfield Tan4cc839f2020-01-24 11:26:14 -0800332void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600333 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
334 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700335 int32_t buttonState, MotionClassification classification,
336 const ui::Transform& transform, float xPrecision, float yPrecision,
337 float rawXCursorPosition, float rawYCursorPosition, nsecs_t downTime,
338 nsecs_t eventTime, size_t pointerCount,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600339 const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700340 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800341 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700342 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100343 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700344 mFlags = flags;
345 mEdgeFlags = edgeFlags;
346 mMetaState = metaState;
347 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800348 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700349 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700350 mXPrecision = xPrecision;
351 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700352 mRawXCursorPosition = rawXCursorPosition;
353 mRawYCursorPosition = rawYCursorPosition;
Jeff Brown5912f952013-07-01 19:10:31 -0700354 mDownTime = downTime;
355 mPointerProperties.clear();
356 mPointerProperties.appendArray(pointerProperties, pointerCount);
357 mSampleEventTimes.clear();
358 mSamplePointerCoords.clear();
359 addSample(eventTime, pointerCoords);
360}
361
362void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800363 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
364 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700365 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100366 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700367 mFlags = other->mFlags;
368 mEdgeFlags = other->mEdgeFlags;
369 mMetaState = other->mMetaState;
370 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800371 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700372 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700373 mXPrecision = other->mXPrecision;
374 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700375 mRawXCursorPosition = other->mRawXCursorPosition;
376 mRawYCursorPosition = other->mRawYCursorPosition;
Jeff Brown5912f952013-07-01 19:10:31 -0700377 mDownTime = other->mDownTime;
378 mPointerProperties = other->mPointerProperties;
379
380 if (keepHistory) {
381 mSampleEventTimes = other->mSampleEventTimes;
382 mSamplePointerCoords = other->mSamplePointerCoords;
383 } else {
384 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500385 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700386 mSamplePointerCoords.clear();
387 size_t pointerCount = other->getPointerCount();
388 size_t historySize = other->getHistorySize();
389 mSamplePointerCoords.appendArray(other->mSamplePointerCoords.array()
390 + (historySize * pointerCount), pointerCount);
391 }
392}
393
394void MotionEvent::addSample(
395 int64_t eventTime,
396 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500397 mSampleEventTimes.push_back(eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -0700398 mSamplePointerCoords.appendArray(pointerCoords, getPointerCount());
399}
400
Garfield Tan00f511d2019-06-12 16:55:40 -0700401float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700402 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
403 return vals.x;
Garfield Tan00f511d2019-06-12 16:55:40 -0700404}
405
406float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700407 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
408 return vals.y;
Garfield Tan00f511d2019-06-12 16:55:40 -0700409}
410
Garfield Tan937bb832019-07-25 17:48:31 -0700411void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700412 ui::Transform inverse = mTransform.inverse();
413 vec2 vals = inverse.transform(x, y);
414 mRawXCursorPosition = vals.x;
415 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700416}
417
Jeff Brown5912f952013-07-01 19:10:31 -0700418const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
419 return &mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
420}
421
422float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
423 return getRawPointerCoords(pointerIndex)->getAxisValue(axis);
424}
425
426float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700427 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700428}
429
430const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
431 size_t pointerIndex, size_t historicalIndex) const {
432 return &mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
433}
434
435float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
436 size_t historicalIndex) const {
437 return getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis);
438}
439
440float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
441 size_t historicalIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700442 if (axis != AMOTION_EVENT_AXIS_X && axis != AMOTION_EVENT_AXIS_Y) {
443 return getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis);
444 }
445
446 float rawX = getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getX();
447 float rawY = getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getY();
448 vec2 vals = mTransform.transform(rawX, rawY);
449
Jeff Brown5912f952013-07-01 19:10:31 -0700450 switch (axis) {
451 case AMOTION_EVENT_AXIS_X:
chaviw9eaa22c2020-07-01 16:21:27 -0700452 return vals.x;
Jeff Brown5912f952013-07-01 19:10:31 -0700453 case AMOTION_EVENT_AXIS_Y:
chaviw9eaa22c2020-07-01 16:21:27 -0700454 return vals.y;
Jeff Brown5912f952013-07-01 19:10:31 -0700455 }
chaviw9eaa22c2020-07-01 16:21:27 -0700456
457 // This should never happen
458 return 0;
Jeff Brown5912f952013-07-01 19:10:31 -0700459}
460
461ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
462 size_t pointerCount = mPointerProperties.size();
463 for (size_t i = 0; i < pointerCount; i++) {
464 if (mPointerProperties.itemAt(i).id == pointerId) {
465 return i;
466 }
467 }
468 return -1;
469}
470
471void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700472 float currXOffset = mTransform.tx();
473 float currYOffset = mTransform.ty();
474 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700475}
476
Robert Carre07e1032018-11-26 12:55:53 -0800477void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700478 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800479 mXPrecision *= globalScaleFactor;
480 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700481
482 size_t numSamples = mSamplePointerCoords.size();
483 for (size_t i = 0; i < numSamples; i++) {
chaviw9eaa22c2020-07-01 16:21:27 -0700484 mSamplePointerCoords.editItemAt(i).scale(globalScaleFactor, globalScaleFactor,
485 globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700486 }
487}
488
chaviw9eaa22c2020-07-01 16:21:27 -0700489static vec2 transformPoint(const std::array<float, 9>& matrix, float x, float y) {
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700490 // Apply perspective transform like Skia.
491 float newX = matrix[0] * x + matrix[1] * y + matrix[2];
492 float newY = matrix[3] * x + matrix[4] * y + matrix[5];
493 float newZ = matrix[6] * x + matrix[7] * y + matrix[8];
494 if (newZ) {
495 newZ = 1.0f / newZ;
496 }
chaviw9eaa22c2020-07-01 16:21:27 -0700497 vec2 transformedPoint;
498 transformedPoint.x = newX * newZ;
499 transformedPoint.y = newY * newZ;
500 return transformedPoint;
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700501}
502
chaviw9eaa22c2020-07-01 16:21:27 -0700503static float transformAngle(const std::array<float, 9>& matrix, float angleRadians, float originX,
504 float originY) {
Jeff Brown5912f952013-07-01 19:10:31 -0700505 // Construct and transform a vector oriented at the specified clockwise angle from vertical.
506 // Coordinate system: down is increasing Y, right is increasing X.
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700507 float x = sinf(angleRadians);
508 float y = -cosf(angleRadians);
chaviw9eaa22c2020-07-01 16:21:27 -0700509 vec2 transformedPoint = transformPoint(matrix, x, y);
510
511 transformedPoint.x -= originX;
512 transformedPoint.y -= originY;
Jeff Brown5912f952013-07-01 19:10:31 -0700513
514 // Derive the transformed vector's clockwise angle from vertical.
chaviw9eaa22c2020-07-01 16:21:27 -0700515 float result = atan2f(transformedPoint.x, -transformedPoint.y);
Jeff Brown5912f952013-07-01 19:10:31 -0700516 if (result < - M_PI_2) {
517 result += M_PI;
518 } else if (result > M_PI_2) {
519 result -= M_PI;
520 }
521 return result;
522}
523
chaviw9eaa22c2020-07-01 16:21:27 -0700524void MotionEvent::transform(const std::array<float, 9>& matrix) {
525 // We want to preserve the rawX and rawY so we just update the transform
526 // using the values of the transform passed in
527 ui::Transform newTransform;
528 newTransform.set(matrix);
529 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700530
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700531 // Determine how the origin is transformed by the matrix so that we
532 // can transform orientation vectors.
chaviw9eaa22c2020-07-01 16:21:27 -0700533 vec2 origin = transformPoint(matrix, 0, 0);
Garfield Tan00f511d2019-06-12 16:55:40 -0700534
Jeff Brown5912f952013-07-01 19:10:31 -0700535 // Apply the transformation to all samples.
536 size_t numSamples = mSamplePointerCoords.size();
537 for (size_t i = 0; i < numSamples; i++) {
538 PointerCoords& c = mSamplePointerCoords.editItemAt(i);
Jeff Brown5912f952013-07-01 19:10:31 -0700539 float orientation = c.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700540 c.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
chaviw9eaa22c2020-07-01 16:21:27 -0700541 transformAngle(matrix, orientation, origin.x, origin.y));
Jeff Brown5912f952013-07-01 19:10:31 -0700542 }
543}
544
Brett Chabotfaa986c2020-11-04 17:39:36 -0800545#ifdef __linux__
chaviw9eaa22c2020-07-01 16:21:27 -0700546static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
547 float dsdx, dtdx, tx, dtdy, dsdy, ty;
548 status_t status = parcel.readFloat(&dsdx);
549 status |= parcel.readFloat(&dtdx);
550 status |= parcel.readFloat(&tx);
551 status |= parcel.readFloat(&dtdy);
552 status |= parcel.readFloat(&dsdy);
553 status |= parcel.readFloat(&ty);
554
555 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
556 return status;
557}
558
559static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
560 status_t status = parcel.writeFloat(transform.dsdx());
561 status |= parcel.writeFloat(transform.dtdx());
562 status |= parcel.writeFloat(transform.tx());
563 status |= parcel.writeFloat(transform.dtdy());
564 status |= parcel.writeFloat(transform.dsdy());
565 status |= parcel.writeFloat(transform.ty());
566 return status;
567}
568
Jeff Brown5912f952013-07-01 19:10:31 -0700569status_t MotionEvent::readFromParcel(Parcel* parcel) {
570 size_t pointerCount = parcel->readInt32();
571 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800572 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
573 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700574 return BAD_VALUE;
575 }
576
Garfield Tan4cc839f2020-01-24 11:26:14 -0800577 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700578 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600579 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800580 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600581 std::vector<uint8_t> hmac;
582 status_t result = parcel->readByteVector(&hmac);
583 if (result != OK || hmac.size() != 32) {
584 return BAD_VALUE;
585 }
586 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700587 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100588 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700589 mFlags = parcel->readInt32();
590 mEdgeFlags = parcel->readInt32();
591 mMetaState = parcel->readInt32();
592 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800593 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700594
595 result = android::readFromParcel(mTransform, *parcel);
596 if (result != OK) {
597 return result;
598 }
Jeff Brown5912f952013-07-01 19:10:31 -0700599 mXPrecision = parcel->readFloat();
600 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700601 mRawXCursorPosition = parcel->readFloat();
602 mRawYCursorPosition = parcel->readFloat();
Jeff Brown5912f952013-07-01 19:10:31 -0700603 mDownTime = parcel->readInt64();
604
605 mPointerProperties.clear();
606 mPointerProperties.setCapacity(pointerCount);
607 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500608 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700609 mSamplePointerCoords.clear();
610 mSamplePointerCoords.setCapacity(sampleCount * pointerCount);
611
612 for (size_t i = 0; i < pointerCount; i++) {
613 mPointerProperties.push();
614 PointerProperties& properties = mPointerProperties.editTop();
615 properties.id = parcel->readInt32();
616 properties.toolType = parcel->readInt32();
617 }
618
Dan Austinc94fc452015-09-22 14:22:41 -0700619 while (sampleCount > 0) {
620 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500621 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700622 for (size_t i = 0; i < pointerCount; i++) {
623 mSamplePointerCoords.push();
624 status_t status = mSamplePointerCoords.editTop().readFromParcel(parcel);
625 if (status) {
626 return status;
627 }
628 }
629 }
630 return OK;
631}
632
633status_t MotionEvent::writeToParcel(Parcel* parcel) const {
634 size_t pointerCount = mPointerProperties.size();
635 size_t sampleCount = mSampleEventTimes.size();
636
637 parcel->writeInt32(pointerCount);
638 parcel->writeInt32(sampleCount);
639
Garfield Tan4cc839f2020-01-24 11:26:14 -0800640 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700641 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600642 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800643 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600644 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
645 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700646 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100647 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700648 parcel->writeInt32(mFlags);
649 parcel->writeInt32(mEdgeFlags);
650 parcel->writeInt32(mMetaState);
651 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800652 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700653
654 status_t result = android::writeToParcel(mTransform, *parcel);
655 if (result != OK) {
656 return result;
657 }
Jeff Brown5912f952013-07-01 19:10:31 -0700658 parcel->writeFloat(mXPrecision);
659 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700660 parcel->writeFloat(mRawXCursorPosition);
661 parcel->writeFloat(mRawYCursorPosition);
Jeff Brown5912f952013-07-01 19:10:31 -0700662 parcel->writeInt64(mDownTime);
663
664 for (size_t i = 0; i < pointerCount; i++) {
665 const PointerProperties& properties = mPointerProperties.itemAt(i);
666 parcel->writeInt32(properties.id);
667 parcel->writeInt32(properties.toolType);
668 }
669
670 const PointerCoords* pc = mSamplePointerCoords.array();
671 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500672 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700673 for (size_t i = 0; i < pointerCount; i++) {
674 status_t status = (pc++)->writeToParcel(parcel);
675 if (status) {
676 return status;
677 }
678 }
679 }
680 return OK;
681}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800682#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700683
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600684bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Jeff Brown5912f952013-07-01 19:10:31 -0700685 if (source & AINPUT_SOURCE_CLASS_POINTER) {
686 // Specifically excludes HOVER_MOVE and SCROLL.
687 switch (action & AMOTION_EVENT_ACTION_MASK) {
688 case AMOTION_EVENT_ACTION_DOWN:
689 case AMOTION_EVENT_ACTION_MOVE:
690 case AMOTION_EVENT_ACTION_UP:
691 case AMOTION_EVENT_ACTION_POINTER_DOWN:
692 case AMOTION_EVENT_ACTION_POINTER_UP:
693 case AMOTION_EVENT_ACTION_CANCEL:
694 case AMOTION_EVENT_ACTION_OUTSIDE:
695 return true;
696 }
697 }
698 return false;
699}
700
Michael Wright872db4f2014-04-22 15:03:51 -0700701const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700702 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700703}
704
705int32_t MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700706 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700707}
708
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500709std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700710 // Convert MotionEvent action to string
711 switch (action & AMOTION_EVENT_ACTION_MASK) {
712 case AMOTION_EVENT_ACTION_DOWN:
713 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700714 case AMOTION_EVENT_ACTION_UP:
715 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500716 case AMOTION_EVENT_ACTION_MOVE:
717 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700718 case AMOTION_EVENT_ACTION_CANCEL:
719 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500720 case AMOTION_EVENT_ACTION_OUTSIDE:
721 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700722 case AMOTION_EVENT_ACTION_POINTER_DOWN:
723 return "POINTER_DOWN";
724 case AMOTION_EVENT_ACTION_POINTER_UP:
725 return "POINTER_UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500726 case AMOTION_EVENT_ACTION_HOVER_MOVE:
727 return "HOVER_MOVE";
728 case AMOTION_EVENT_ACTION_SCROLL:
729 return "SCROLL";
730 case AMOTION_EVENT_ACTION_HOVER_ENTER:
731 return "HOVER_ENTER";
732 case AMOTION_EVENT_ACTION_HOVER_EXIT:
733 return "HOVER_EXIT";
734 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
735 return "BUTTON_PRESS";
736 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
737 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700738 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500739 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700740}
741
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800742// --- FocusEvent ---
743
Garfield Tan4cc839f2020-01-24 11:26:14 -0800744void FocusEvent::initialize(int32_t id, bool hasFocus, bool inTouchMode) {
745 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600746 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800747 mHasFocus = hasFocus;
748 mInTouchMode = inTouchMode;
749}
750
751void FocusEvent::initialize(const FocusEvent& from) {
752 InputEvent::initialize(from);
753 mHasFocus = from.mHasFocus;
754 mInTouchMode = from.mInTouchMode;
755}
Jeff Brown5912f952013-07-01 19:10:31 -0700756
757// --- PooledInputEventFactory ---
758
759PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
760 mMaxPoolSize(maxPoolSize) {
761}
762
763PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -0700764}
765
766KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800767 if (mKeyEventPool.empty()) {
768 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700769 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800770 KeyEvent* event = mKeyEventPool.front().release();
771 mKeyEventPool.pop();
772 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700773}
774
775MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800776 if (mMotionEventPool.empty()) {
777 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700778 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800779 MotionEvent* event = mMotionEventPool.front().release();
780 mMotionEventPool.pop();
781 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700782}
783
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800784FocusEvent* PooledInputEventFactory::createFocusEvent() {
785 if (mFocusEventPool.empty()) {
786 return new FocusEvent();
787 }
788 FocusEvent* event = mFocusEventPool.front().release();
789 mFocusEventPool.pop();
790 return event;
791}
792
Jeff Brown5912f952013-07-01 19:10:31 -0700793void PooledInputEventFactory::recycle(InputEvent* event) {
794 switch (event->getType()) {
795 case AINPUT_EVENT_TYPE_KEY:
796 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800797 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700798 return;
799 }
800 break;
801 case AINPUT_EVENT_TYPE_MOTION:
802 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800803 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700804 return;
805 }
806 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800807 case AINPUT_EVENT_TYPE_FOCUS:
808 if (mFocusEventPool.size() < mMaxPoolSize) {
809 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
810 return;
811 }
812 break;
Jeff Brown5912f952013-07-01 19:10:31 -0700813 }
814 delete event;
815}
816
817} // namespace android