blob: f3b8832a8f6c24935bc23d33a845aef9a4947400 [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>
Jeff Brown5912f952013-07-01 19:10:31 -070022#include <limits.h>
Garfield Tan84b087e2020-01-23 10:49:05 -080023#include <string.h>
Jeff Brown5912f952013-07-01 19:10:31 -070024
25#include <input/Input.h>
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080026#include <input/InputDevice.h>
Michael Wright872db4f2014-04-22 15:03:51 -070027#include <input/InputEventLabels.h>
Jeff Brown5912f952013-07-01 19:10:31 -070028
Jeff Brown5912f952013-07-01 19:10:31 -070029#include <binder/Parcel.h>
Brett Chabot58208522020-09-09 13:55:24 -070030#ifdef __ANDROID__
Garfield Tan84b087e2020-01-23 10:49:05 -080031#include <sys/random.h>
Jeff Brown5912f952013-07-01 19:10:31 -070032#endif
33
34namespace android {
35
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -080036const char* motionClassificationToString(MotionClassification classification) {
37 switch (classification) {
38 case MotionClassification::NONE:
39 return "NONE";
40 case MotionClassification::AMBIGUOUS_GESTURE:
41 return "AMBIGUOUS_GESTURE";
42 case MotionClassification::DEEP_PRESS:
43 return "DEEP_PRESS";
44 }
45}
46
Garfield Tan84b087e2020-01-23 10:49:05 -080047// --- IdGenerator ---
48IdGenerator::IdGenerator(Source source) : mSource(source) {}
49
50int32_t IdGenerator::nextId() const {
51 constexpr uint32_t SEQUENCE_NUMBER_MASK = ~SOURCE_MASK;
52 int32_t id = 0;
53
54// Avoid building against syscall getrandom(2) on host, which will fail build on Mac. Host doesn't
55// use sequence number so just always return mSource.
56#ifdef __ANDROID__
57 constexpr size_t BUF_LEN = sizeof(id);
58 size_t totalBytes = 0;
59 while (totalBytes < BUF_LEN) {
60 ssize_t bytes = TEMP_FAILURE_RETRY(getrandom(&id, BUF_LEN, GRND_NONBLOCK));
61 if (CC_UNLIKELY(bytes < 0)) {
62 ALOGW("Failed to fill in random number for sequence number: %s.", strerror(errno));
63 id = 0;
64 break;
65 }
66 totalBytes += bytes;
67 }
68#endif // __ANDROID__
69
70 return (id & SEQUENCE_NUMBER_MASK) | static_cast<int32_t>(mSource);
71}
72
Jeff Brown5912f952013-07-01 19:10:31 -070073// --- InputEvent ---
74
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080075const char* inputEventTypeToString(int32_t type) {
76 switch (type) {
77 case AINPUT_EVENT_TYPE_KEY: {
78 return "KEY";
79 }
80 case AINPUT_EVENT_TYPE_MOTION: {
81 return "MOTION";
82 }
83 case AINPUT_EVENT_TYPE_FOCUS: {
84 return "FOCUS";
85 }
86 }
87 return "UNKNOWN";
88}
89
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -080090VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
91 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
92 event.getSource(), event.getDisplayId()},
93 event.getAction(),
94 event.getDownTime(),
95 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
96 event.getKeyCode(),
97 event.getScanCode(),
98 event.getMetaState(),
99 event.getRepeatCount()};
100}
101
102VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
103 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
104 event.getSource(), event.getDisplayId()},
105 event.getRawX(0),
106 event.getRawY(0),
107 event.getActionMasked(),
108 event.getDownTime(),
109 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
110 event.getMetaState(),
111 event.getButtonState()};
112}
113
Garfield Tan4cc839f2020-01-24 11:26:14 -0800114void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600115 std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800116 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700117 mDeviceId = deviceId;
118 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100119 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600120 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700121}
122
123void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800124 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700125 mDeviceId = from.mDeviceId;
126 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100127 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600128 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700129}
130
Garfield Tan4cc839f2020-01-24 11:26:14 -0800131int32_t InputEvent::nextId() {
132 static IdGenerator idGen(IdGenerator::Source::OTHER);
133 return idGen.nextId();
134}
135
Jeff Brown5912f952013-07-01 19:10:31 -0700136// --- KeyEvent ---
137
Michael Wright872db4f2014-04-22 15:03:51 -0700138const char* KeyEvent::getLabel(int32_t keyCode) {
Chris Ye4958d062020-08-20 13:21:10 -0700139 return InputEventLookup::getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700140}
141
Michael Wright872db4f2014-04-22 15:03:51 -0700142int32_t KeyEvent::getKeyCodeFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700143 return InputEventLookup::getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700144}
145
Garfield Tan4cc839f2020-01-24 11:26:14 -0800146void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600147 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
148 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
149 nsecs_t downTime, nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800150 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700151 mAction = action;
152 mFlags = flags;
153 mKeyCode = keyCode;
154 mScanCode = scanCode;
155 mMetaState = metaState;
156 mRepeatCount = repeatCount;
157 mDownTime = downTime;
158 mEventTime = eventTime;
159}
160
161void KeyEvent::initialize(const KeyEvent& from) {
162 InputEvent::initialize(from);
163 mAction = from.mAction;
164 mFlags = from.mFlags;
165 mKeyCode = from.mKeyCode;
166 mScanCode = from.mScanCode;
167 mMetaState = from.mMetaState;
168 mRepeatCount = from.mRepeatCount;
169 mDownTime = from.mDownTime;
170 mEventTime = from.mEventTime;
171}
172
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700173const char* KeyEvent::actionToString(int32_t action) {
174 // Convert KeyEvent action to string
175 switch (action) {
176 case AKEY_EVENT_ACTION_DOWN:
177 return "DOWN";
178 case AKEY_EVENT_ACTION_UP:
179 return "UP";
180 case AKEY_EVENT_ACTION_MULTIPLE:
181 return "MULTIPLE";
182 }
183 return "UNKNOWN";
184}
Jeff Brown5912f952013-07-01 19:10:31 -0700185
186// --- PointerCoords ---
187
188float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700189 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700190 return 0;
191 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700192 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700193}
194
195status_t PointerCoords::setAxisValue(int32_t axis, float value) {
196 if (axis < 0 || axis > 63) {
197 return NAME_NOT_FOUND;
198 }
199
Michael Wright38dcdff2014-03-19 12:06:10 -0700200 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
201 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700202 if (value == 0) {
203 return OK; // axes with value 0 do not need to be stored
204 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700205
206 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700207 if (count >= MAX_AXES) {
208 tooManyAxes(axis);
209 return NO_MEMORY;
210 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700211 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700212 for (uint32_t i = count; i > index; i--) {
213 values[i] = values[i - 1];
214 }
215 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700216
Jeff Brown5912f952013-07-01 19:10:31 -0700217 values[index] = value;
218 return OK;
219}
220
221static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
222 float value = c.getAxisValue(axis);
223 if (value != 0) {
224 c.setAxisValue(axis, value * scaleFactor);
225 }
226}
227
Robert Carre07e1032018-11-26 12:55:53 -0800228void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700229 // No need to scale pressure or size since they are normalized.
230 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800231
232 // If there is a global scale factor, it is included in the windowX/YScale
233 // so we don't need to apply it twice to the X/Y axes.
234 // However we don't want to apply any windowXYScale not included in the global scale
235 // to the TOUCH_MAJOR/MINOR coordinates.
236 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
237 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
238 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
239 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
240 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
241 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
242}
243
244void PointerCoords::scale(float globalScaleFactor) {
245 scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700246}
247
Jeff Brownf086ddb2014-02-11 14:28:48 -0800248void PointerCoords::applyOffset(float xOffset, float yOffset) {
249 setAxisValue(AMOTION_EVENT_AXIS_X, getX() + xOffset);
250 setAxisValue(AMOTION_EVENT_AXIS_Y, getY() + yOffset);
251}
252
Jeff Brown5912f952013-07-01 19:10:31 -0700253status_t PointerCoords::readFromParcel(Parcel* parcel) {
254 bits = parcel->readInt64();
255
Michael Wright38dcdff2014-03-19 12:06:10 -0700256 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700257 if (count > MAX_AXES) {
258 return BAD_VALUE;
259 }
260
261 for (uint32_t i = 0; i < count; i++) {
262 values[i] = parcel->readFloat();
263 }
264 return OK;
265}
266
267status_t PointerCoords::writeToParcel(Parcel* parcel) const {
268 parcel->writeInt64(bits);
269
Michael Wright38dcdff2014-03-19 12:06:10 -0700270 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700271 for (uint32_t i = 0; i < count; i++) {
272 parcel->writeFloat(values[i]);
273 }
274 return OK;
275}
Jeff Brown5912f952013-07-01 19:10:31 -0700276
277void PointerCoords::tooManyAxes(int axis) {
278 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
279 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
280}
281
282bool PointerCoords::operator==(const PointerCoords& other) const {
283 if (bits != other.bits) {
284 return false;
285 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700286 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700287 for (uint32_t i = 0; i < count; i++) {
288 if (values[i] != other.values[i]) {
289 return false;
290 }
291 }
292 return true;
293}
294
295void PointerCoords::copyFrom(const PointerCoords& other) {
296 bits = other.bits;
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 values[i] = other.values[i];
300 }
301}
302
chaviwc01e1372020-07-01 12:37:31 -0700303void PointerCoords::transform(const ui::Transform& transform) {
304 vec2 newCoords = transform.transform(getX(), getY());
305 setAxisValue(AMOTION_EVENT_AXIS_X, newCoords.x);
306 setAxisValue(AMOTION_EVENT_AXIS_Y, newCoords.y);
307}
Jeff Brown5912f952013-07-01 19:10:31 -0700308
309// --- PointerProperties ---
310
311bool PointerProperties::operator==(const PointerProperties& other) const {
312 return id == other.id
313 && toolType == other.toolType;
314}
315
316void PointerProperties::copyFrom(const PointerProperties& other) {
317 id = other.id;
318 toolType = other.toolType;
319}
320
321
322// --- MotionEvent ---
323
Garfield Tan4cc839f2020-01-24 11:26:14 -0800324void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600325 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
326 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700327 int32_t buttonState, MotionClassification classification,
328 const ui::Transform& transform, float xPrecision, float yPrecision,
329 float rawXCursorPosition, float rawYCursorPosition, nsecs_t downTime,
330 nsecs_t eventTime, size_t pointerCount,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600331 const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700332 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800333 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700334 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100335 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700336 mFlags = flags;
337 mEdgeFlags = edgeFlags;
338 mMetaState = metaState;
339 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800340 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700341 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700342 mXPrecision = xPrecision;
343 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700344 mRawXCursorPosition = rawXCursorPosition;
345 mRawYCursorPosition = rawYCursorPosition;
Jeff Brown5912f952013-07-01 19:10:31 -0700346 mDownTime = downTime;
347 mPointerProperties.clear();
348 mPointerProperties.appendArray(pointerProperties, pointerCount);
349 mSampleEventTimes.clear();
350 mSamplePointerCoords.clear();
351 addSample(eventTime, pointerCoords);
352}
353
354void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800355 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
356 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700357 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100358 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700359 mFlags = other->mFlags;
360 mEdgeFlags = other->mEdgeFlags;
361 mMetaState = other->mMetaState;
362 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800363 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700364 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700365 mXPrecision = other->mXPrecision;
366 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700367 mRawXCursorPosition = other->mRawXCursorPosition;
368 mRawYCursorPosition = other->mRawYCursorPosition;
Jeff Brown5912f952013-07-01 19:10:31 -0700369 mDownTime = other->mDownTime;
370 mPointerProperties = other->mPointerProperties;
371
372 if (keepHistory) {
373 mSampleEventTimes = other->mSampleEventTimes;
374 mSamplePointerCoords = other->mSamplePointerCoords;
375 } else {
376 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500377 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700378 mSamplePointerCoords.clear();
379 size_t pointerCount = other->getPointerCount();
380 size_t historySize = other->getHistorySize();
381 mSamplePointerCoords.appendArray(other->mSamplePointerCoords.array()
382 + (historySize * pointerCount), pointerCount);
383 }
384}
385
386void MotionEvent::addSample(
387 int64_t eventTime,
388 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500389 mSampleEventTimes.push_back(eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -0700390 mSamplePointerCoords.appendArray(pointerCoords, getPointerCount());
391}
392
Garfield Tan00f511d2019-06-12 16:55:40 -0700393float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700394 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
395 return vals.x;
Garfield Tan00f511d2019-06-12 16:55:40 -0700396}
397
398float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700399 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
400 return vals.y;
Garfield Tan00f511d2019-06-12 16:55:40 -0700401}
402
Garfield Tan937bb832019-07-25 17:48:31 -0700403void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700404 ui::Transform inverse = mTransform.inverse();
405 vec2 vals = inverse.transform(x, y);
406 mRawXCursorPosition = vals.x;
407 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700408}
409
Jeff Brown5912f952013-07-01 19:10:31 -0700410const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
411 return &mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
412}
413
414float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
415 return getRawPointerCoords(pointerIndex)->getAxisValue(axis);
416}
417
418float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700419 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700420}
421
422const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
423 size_t pointerIndex, size_t historicalIndex) const {
424 return &mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
425}
426
427float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
428 size_t historicalIndex) const {
429 return getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis);
430}
431
432float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
433 size_t historicalIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700434 if (axis != AMOTION_EVENT_AXIS_X && axis != AMOTION_EVENT_AXIS_Y) {
435 return getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis);
436 }
437
438 float rawX = getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getX();
439 float rawY = getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getY();
440 vec2 vals = mTransform.transform(rawX, rawY);
441
Jeff Brown5912f952013-07-01 19:10:31 -0700442 switch (axis) {
443 case AMOTION_EVENT_AXIS_X:
chaviw9eaa22c2020-07-01 16:21:27 -0700444 return vals.x;
Jeff Brown5912f952013-07-01 19:10:31 -0700445 case AMOTION_EVENT_AXIS_Y:
chaviw9eaa22c2020-07-01 16:21:27 -0700446 return vals.y;
Jeff Brown5912f952013-07-01 19:10:31 -0700447 }
chaviw9eaa22c2020-07-01 16:21:27 -0700448
449 // This should never happen
450 return 0;
Jeff Brown5912f952013-07-01 19:10:31 -0700451}
452
453ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
454 size_t pointerCount = mPointerProperties.size();
455 for (size_t i = 0; i < pointerCount; i++) {
456 if (mPointerProperties.itemAt(i).id == pointerId) {
457 return i;
458 }
459 }
460 return -1;
461}
462
463void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700464 float currXOffset = mTransform.tx();
465 float currYOffset = mTransform.ty();
466 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700467}
468
Robert Carre07e1032018-11-26 12:55:53 -0800469void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700470 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800471 mXPrecision *= globalScaleFactor;
472 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700473
474 size_t numSamples = mSamplePointerCoords.size();
475 for (size_t i = 0; i < numSamples; i++) {
chaviw9eaa22c2020-07-01 16:21:27 -0700476 mSamplePointerCoords.editItemAt(i).scale(globalScaleFactor, globalScaleFactor,
477 globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700478 }
479}
480
chaviw9eaa22c2020-07-01 16:21:27 -0700481static vec2 transformPoint(const std::array<float, 9>& matrix, float x, float y) {
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700482 // Apply perspective transform like Skia.
483 float newX = matrix[0] * x + matrix[1] * y + matrix[2];
484 float newY = matrix[3] * x + matrix[4] * y + matrix[5];
485 float newZ = matrix[6] * x + matrix[7] * y + matrix[8];
486 if (newZ) {
487 newZ = 1.0f / newZ;
488 }
chaviw9eaa22c2020-07-01 16:21:27 -0700489 vec2 transformedPoint;
490 transformedPoint.x = newX * newZ;
491 transformedPoint.y = newY * newZ;
492 return transformedPoint;
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700493}
494
chaviw9eaa22c2020-07-01 16:21:27 -0700495static float transformAngle(const std::array<float, 9>& matrix, float angleRadians, float originX,
496 float originY) {
Jeff Brown5912f952013-07-01 19:10:31 -0700497 // Construct and transform a vector oriented at the specified clockwise angle from vertical.
498 // Coordinate system: down is increasing Y, right is increasing X.
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700499 float x = sinf(angleRadians);
500 float y = -cosf(angleRadians);
chaviw9eaa22c2020-07-01 16:21:27 -0700501 vec2 transformedPoint = transformPoint(matrix, x, y);
502
503 transformedPoint.x -= originX;
504 transformedPoint.y -= originY;
Jeff Brown5912f952013-07-01 19:10:31 -0700505
506 // Derive the transformed vector's clockwise angle from vertical.
chaviw9eaa22c2020-07-01 16:21:27 -0700507 float result = atan2f(transformedPoint.x, -transformedPoint.y);
Jeff Brown5912f952013-07-01 19:10:31 -0700508 if (result < - M_PI_2) {
509 result += M_PI;
510 } else if (result > M_PI_2) {
511 result -= M_PI;
512 }
513 return result;
514}
515
chaviw9eaa22c2020-07-01 16:21:27 -0700516void MotionEvent::transform(const std::array<float, 9>& matrix) {
517 // We want to preserve the rawX and rawY so we just update the transform
518 // using the values of the transform passed in
519 ui::Transform newTransform;
520 newTransform.set(matrix);
521 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700522
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700523 // Determine how the origin is transformed by the matrix so that we
524 // can transform orientation vectors.
chaviw9eaa22c2020-07-01 16:21:27 -0700525 vec2 origin = transformPoint(matrix, 0, 0);
Garfield Tan00f511d2019-06-12 16:55:40 -0700526
Jeff Brown5912f952013-07-01 19:10:31 -0700527 // Apply the transformation to all samples.
528 size_t numSamples = mSamplePointerCoords.size();
529 for (size_t i = 0; i < numSamples; i++) {
530 PointerCoords& c = mSamplePointerCoords.editItemAt(i);
Jeff Brown5912f952013-07-01 19:10:31 -0700531 float orientation = c.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700532 c.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
chaviw9eaa22c2020-07-01 16:21:27 -0700533 transformAngle(matrix, orientation, origin.x, origin.y));
Jeff Brown5912f952013-07-01 19:10:31 -0700534 }
535}
536
chaviw9eaa22c2020-07-01 16:21:27 -0700537static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
538 float dsdx, dtdx, tx, dtdy, dsdy, ty;
539 status_t status = parcel.readFloat(&dsdx);
540 status |= parcel.readFloat(&dtdx);
541 status |= parcel.readFloat(&tx);
542 status |= parcel.readFloat(&dtdy);
543 status |= parcel.readFloat(&dsdy);
544 status |= parcel.readFloat(&ty);
545
546 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
547 return status;
548}
549
550static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
551 status_t status = parcel.writeFloat(transform.dsdx());
552 status |= parcel.writeFloat(transform.dtdx());
553 status |= parcel.writeFloat(transform.tx());
554 status |= parcel.writeFloat(transform.dtdy());
555 status |= parcel.writeFloat(transform.dsdy());
556 status |= parcel.writeFloat(transform.ty());
557 return status;
558}
559
Jeff Brown5912f952013-07-01 19:10:31 -0700560status_t MotionEvent::readFromParcel(Parcel* parcel) {
561 size_t pointerCount = parcel->readInt32();
562 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800563 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
564 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700565 return BAD_VALUE;
566 }
567
Garfield Tan4cc839f2020-01-24 11:26:14 -0800568 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700569 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600570 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800571 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600572 std::vector<uint8_t> hmac;
573 status_t result = parcel->readByteVector(&hmac);
574 if (result != OK || hmac.size() != 32) {
575 return BAD_VALUE;
576 }
577 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700578 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100579 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700580 mFlags = parcel->readInt32();
581 mEdgeFlags = parcel->readInt32();
582 mMetaState = parcel->readInt32();
583 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800584 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700585
586 result = android::readFromParcel(mTransform, *parcel);
587 if (result != OK) {
588 return result;
589 }
Jeff Brown5912f952013-07-01 19:10:31 -0700590 mXPrecision = parcel->readFloat();
591 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700592 mRawXCursorPosition = parcel->readFloat();
593 mRawYCursorPosition = parcel->readFloat();
Jeff Brown5912f952013-07-01 19:10:31 -0700594 mDownTime = parcel->readInt64();
595
596 mPointerProperties.clear();
597 mPointerProperties.setCapacity(pointerCount);
598 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500599 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700600 mSamplePointerCoords.clear();
601 mSamplePointerCoords.setCapacity(sampleCount * pointerCount);
602
603 for (size_t i = 0; i < pointerCount; i++) {
604 mPointerProperties.push();
605 PointerProperties& properties = mPointerProperties.editTop();
606 properties.id = parcel->readInt32();
607 properties.toolType = parcel->readInt32();
608 }
609
Dan Austinc94fc452015-09-22 14:22:41 -0700610 while (sampleCount > 0) {
611 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500612 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700613 for (size_t i = 0; i < pointerCount; i++) {
614 mSamplePointerCoords.push();
615 status_t status = mSamplePointerCoords.editTop().readFromParcel(parcel);
616 if (status) {
617 return status;
618 }
619 }
620 }
621 return OK;
622}
623
624status_t MotionEvent::writeToParcel(Parcel* parcel) const {
625 size_t pointerCount = mPointerProperties.size();
626 size_t sampleCount = mSampleEventTimes.size();
627
628 parcel->writeInt32(pointerCount);
629 parcel->writeInt32(sampleCount);
630
Garfield Tan4cc839f2020-01-24 11:26:14 -0800631 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700632 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600633 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800634 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600635 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
636 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700637 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100638 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700639 parcel->writeInt32(mFlags);
640 parcel->writeInt32(mEdgeFlags);
641 parcel->writeInt32(mMetaState);
642 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800643 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700644
645 status_t result = android::writeToParcel(mTransform, *parcel);
646 if (result != OK) {
647 return result;
648 }
Jeff Brown5912f952013-07-01 19:10:31 -0700649 parcel->writeFloat(mXPrecision);
650 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700651 parcel->writeFloat(mRawXCursorPosition);
652 parcel->writeFloat(mRawYCursorPosition);
Jeff Brown5912f952013-07-01 19:10:31 -0700653 parcel->writeInt64(mDownTime);
654
655 for (size_t i = 0; i < pointerCount; i++) {
656 const PointerProperties& properties = mPointerProperties.itemAt(i);
657 parcel->writeInt32(properties.id);
658 parcel->writeInt32(properties.toolType);
659 }
660
661 const PointerCoords* pc = mSamplePointerCoords.array();
662 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500663 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700664 for (size_t i = 0; i < pointerCount; i++) {
665 status_t status = (pc++)->writeToParcel(parcel);
666 if (status) {
667 return status;
668 }
669 }
670 }
671 return OK;
672}
Jeff Brown5912f952013-07-01 19:10:31 -0700673
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600674bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Jeff Brown5912f952013-07-01 19:10:31 -0700675 if (source & AINPUT_SOURCE_CLASS_POINTER) {
676 // Specifically excludes HOVER_MOVE and SCROLL.
677 switch (action & AMOTION_EVENT_ACTION_MASK) {
678 case AMOTION_EVENT_ACTION_DOWN:
679 case AMOTION_EVENT_ACTION_MOVE:
680 case AMOTION_EVENT_ACTION_UP:
681 case AMOTION_EVENT_ACTION_POINTER_DOWN:
682 case AMOTION_EVENT_ACTION_POINTER_UP:
683 case AMOTION_EVENT_ACTION_CANCEL:
684 case AMOTION_EVENT_ACTION_OUTSIDE:
685 return true;
686 }
687 }
688 return false;
689}
690
Michael Wright872db4f2014-04-22 15:03:51 -0700691const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700692 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700693}
694
695int32_t MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700696 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700697}
698
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700699const char* MotionEvent::actionToString(int32_t action) {
700 // Convert MotionEvent action to string
701 switch (action & AMOTION_EVENT_ACTION_MASK) {
702 case AMOTION_EVENT_ACTION_DOWN:
703 return "DOWN";
704 case AMOTION_EVENT_ACTION_MOVE:
705 return "MOVE";
706 case AMOTION_EVENT_ACTION_UP:
707 return "UP";
708 case AMOTION_EVENT_ACTION_CANCEL:
709 return "CANCEL";
710 case AMOTION_EVENT_ACTION_POINTER_DOWN:
711 return "POINTER_DOWN";
712 case AMOTION_EVENT_ACTION_POINTER_UP:
713 return "POINTER_UP";
714 }
715 return "UNKNOWN";
716}
717
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800718// --- FocusEvent ---
719
Garfield Tan4cc839f2020-01-24 11:26:14 -0800720void FocusEvent::initialize(int32_t id, bool hasFocus, bool inTouchMode) {
721 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600722 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800723 mHasFocus = hasFocus;
724 mInTouchMode = inTouchMode;
725}
726
727void FocusEvent::initialize(const FocusEvent& from) {
728 InputEvent::initialize(from);
729 mHasFocus = from.mHasFocus;
730 mInTouchMode = from.mInTouchMode;
731}
Jeff Brown5912f952013-07-01 19:10:31 -0700732
733// --- PooledInputEventFactory ---
734
735PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
736 mMaxPoolSize(maxPoolSize) {
737}
738
739PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -0700740}
741
742KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800743 if (mKeyEventPool.empty()) {
744 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700745 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800746 KeyEvent* event = mKeyEventPool.front().release();
747 mKeyEventPool.pop();
748 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700749}
750
751MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800752 if (mMotionEventPool.empty()) {
753 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700754 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800755 MotionEvent* event = mMotionEventPool.front().release();
756 mMotionEventPool.pop();
757 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700758}
759
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800760FocusEvent* PooledInputEventFactory::createFocusEvent() {
761 if (mFocusEventPool.empty()) {
762 return new FocusEvent();
763 }
764 FocusEvent* event = mFocusEventPool.front().release();
765 mFocusEventPool.pop();
766 return event;
767}
768
Jeff Brown5912f952013-07-01 19:10:31 -0700769void PooledInputEventFactory::recycle(InputEvent* event) {
770 switch (event->getType()) {
771 case AINPUT_EVENT_TYPE_KEY:
772 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800773 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700774 return;
775 }
776 break;
777 case AINPUT_EVENT_TYPE_MOTION:
778 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800779 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700780 return;
781 }
782 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800783 case AINPUT_EVENT_TYPE_FOCUS:
784 if (mFocusEventPool.size() < mMaxPoolSize) {
785 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
786 return;
787 }
788 break;
Jeff Brown5912f952013-07-01 19:10:31 -0700789 }
790 delete event;
791}
792
793} // namespace android