blob: 692c65dc46fc81cdf43d3a0547a32eb065300aa0 [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
Elliott Hughes6071da72015-08-12 15:27:47 -070029#ifdef __ANDROID__
Jeff Brown5912f952013-07-01 19:10:31 -070030#include <binder/Parcel.h>
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
Elliott Hughes6071da72015-08-12 15:27:47 -0700253#ifdef __ANDROID__
Jeff Brown5912f952013-07-01 19:10:31 -0700254status_t PointerCoords::readFromParcel(Parcel* parcel) {
255 bits = parcel->readInt64();
256
Michael Wright38dcdff2014-03-19 12:06:10 -0700257 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700258 if (count > MAX_AXES) {
259 return BAD_VALUE;
260 }
261
262 for (uint32_t i = 0; i < count; i++) {
263 values[i] = parcel->readFloat();
264 }
265 return OK;
266}
267
268status_t PointerCoords::writeToParcel(Parcel* parcel) const {
269 parcel->writeInt64(bits);
270
Michael Wright38dcdff2014-03-19 12:06:10 -0700271 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700272 for (uint32_t i = 0; i < count; i++) {
273 parcel->writeFloat(values[i]);
274 }
275 return OK;
276}
277#endif
278
279void PointerCoords::tooManyAxes(int axis) {
280 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
281 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
282}
283
284bool PointerCoords::operator==(const PointerCoords& other) const {
285 if (bits != other.bits) {
286 return false;
287 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700288 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700289 for (uint32_t i = 0; i < count; i++) {
290 if (values[i] != other.values[i]) {
291 return false;
292 }
293 }
294 return true;
295}
296
297void PointerCoords::copyFrom(const PointerCoords& other) {
298 bits = other.bits;
Michael Wright38dcdff2014-03-19 12:06:10 -0700299 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700300 for (uint32_t i = 0; i < count; i++) {
301 values[i] = other.values[i];
302 }
303}
304
chaviwc01e1372020-07-01 12:37:31 -0700305void PointerCoords::transform(const ui::Transform& transform) {
306 vec2 newCoords = transform.transform(getX(), getY());
307 setAxisValue(AMOTION_EVENT_AXIS_X, newCoords.x);
308 setAxisValue(AMOTION_EVENT_AXIS_Y, newCoords.y);
309}
Jeff Brown5912f952013-07-01 19:10:31 -0700310
311// --- PointerProperties ---
312
313bool PointerProperties::operator==(const PointerProperties& other) const {
314 return id == other.id
315 && toolType == other.toolType;
316}
317
318void PointerProperties::copyFrom(const PointerProperties& other) {
319 id = other.id;
320 toolType = other.toolType;
321}
322
323
324// --- MotionEvent ---
325
Garfield Tan4cc839f2020-01-24 11:26:14 -0800326void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600327 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
328 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700329 int32_t buttonState, MotionClassification classification,
330 const ui::Transform& transform, float xPrecision, float yPrecision,
331 float rawXCursorPosition, float rawYCursorPosition, nsecs_t downTime,
332 nsecs_t eventTime, size_t pointerCount,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600333 const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700334 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800335 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700336 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100337 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700338 mFlags = flags;
339 mEdgeFlags = edgeFlags;
340 mMetaState = metaState;
341 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800342 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700343 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700344 mXPrecision = xPrecision;
345 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700346 mRawXCursorPosition = rawXCursorPosition;
347 mRawYCursorPosition = rawYCursorPosition;
Jeff Brown5912f952013-07-01 19:10:31 -0700348 mDownTime = downTime;
349 mPointerProperties.clear();
350 mPointerProperties.appendArray(pointerProperties, pointerCount);
351 mSampleEventTimes.clear();
352 mSamplePointerCoords.clear();
353 addSample(eventTime, pointerCoords);
354}
355
356void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800357 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
358 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700359 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100360 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700361 mFlags = other->mFlags;
362 mEdgeFlags = other->mEdgeFlags;
363 mMetaState = other->mMetaState;
364 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800365 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700366 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700367 mXPrecision = other->mXPrecision;
368 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700369 mRawXCursorPosition = other->mRawXCursorPosition;
370 mRawYCursorPosition = other->mRawYCursorPosition;
Jeff Brown5912f952013-07-01 19:10:31 -0700371 mDownTime = other->mDownTime;
372 mPointerProperties = other->mPointerProperties;
373
374 if (keepHistory) {
375 mSampleEventTimes = other->mSampleEventTimes;
376 mSamplePointerCoords = other->mSamplePointerCoords;
377 } else {
378 mSampleEventTimes.clear();
379 mSampleEventTimes.push(other->getEventTime());
380 mSamplePointerCoords.clear();
381 size_t pointerCount = other->getPointerCount();
382 size_t historySize = other->getHistorySize();
383 mSamplePointerCoords.appendArray(other->mSamplePointerCoords.array()
384 + (historySize * pointerCount), pointerCount);
385 }
386}
387
388void MotionEvent::addSample(
389 int64_t eventTime,
390 const PointerCoords* pointerCoords) {
391 mSampleEventTimes.push(eventTime);
392 mSamplePointerCoords.appendArray(pointerCoords, getPointerCount());
393}
394
Garfield Tan00f511d2019-06-12 16:55:40 -0700395float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700396 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
397 return vals.x;
Garfield Tan00f511d2019-06-12 16:55:40 -0700398}
399
400float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700401 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
402 return vals.y;
Garfield Tan00f511d2019-06-12 16:55:40 -0700403}
404
Garfield Tan937bb832019-07-25 17:48:31 -0700405void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700406 ui::Transform inverse = mTransform.inverse();
407 vec2 vals = inverse.transform(x, y);
408 mRawXCursorPosition = vals.x;
409 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700410}
411
Jeff Brown5912f952013-07-01 19:10:31 -0700412const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
413 return &mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
414}
415
416float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
417 return getRawPointerCoords(pointerIndex)->getAxisValue(axis);
418}
419
420float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700421 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700422}
423
424const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
425 size_t pointerIndex, size_t historicalIndex) const {
426 return &mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
427}
428
429float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
430 size_t historicalIndex) const {
431 return getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis);
432}
433
434float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
435 size_t historicalIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700436 if (axis != AMOTION_EVENT_AXIS_X && axis != AMOTION_EVENT_AXIS_Y) {
437 return getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis);
438 }
439
440 float rawX = getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getX();
441 float rawY = getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getY();
442 vec2 vals = mTransform.transform(rawX, rawY);
443
Jeff Brown5912f952013-07-01 19:10:31 -0700444 switch (axis) {
445 case AMOTION_EVENT_AXIS_X:
chaviw9eaa22c2020-07-01 16:21:27 -0700446 return vals.x;
Jeff Brown5912f952013-07-01 19:10:31 -0700447 case AMOTION_EVENT_AXIS_Y:
chaviw9eaa22c2020-07-01 16:21:27 -0700448 return vals.y;
Jeff Brown5912f952013-07-01 19:10:31 -0700449 }
chaviw9eaa22c2020-07-01 16:21:27 -0700450
451 // This should never happen
452 return 0;
Jeff Brown5912f952013-07-01 19:10:31 -0700453}
454
455ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
456 size_t pointerCount = mPointerProperties.size();
457 for (size_t i = 0; i < pointerCount; i++) {
458 if (mPointerProperties.itemAt(i).id == pointerId) {
459 return i;
460 }
461 }
462 return -1;
463}
464
465void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700466 float currXOffset = mTransform.tx();
467 float currYOffset = mTransform.ty();
468 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700469}
470
Robert Carre07e1032018-11-26 12:55:53 -0800471void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700472 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800473 mXPrecision *= globalScaleFactor;
474 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700475
476 size_t numSamples = mSamplePointerCoords.size();
477 for (size_t i = 0; i < numSamples; i++) {
chaviw9eaa22c2020-07-01 16:21:27 -0700478 mSamplePointerCoords.editItemAt(i).scale(globalScaleFactor, globalScaleFactor,
479 globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700480 }
481}
482
chaviw9eaa22c2020-07-01 16:21:27 -0700483static vec2 transformPoint(const std::array<float, 9>& matrix, float x, float y) {
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700484 // Apply perspective transform like Skia.
485 float newX = matrix[0] * x + matrix[1] * y + matrix[2];
486 float newY = matrix[3] * x + matrix[4] * y + matrix[5];
487 float newZ = matrix[6] * x + matrix[7] * y + matrix[8];
488 if (newZ) {
489 newZ = 1.0f / newZ;
490 }
chaviw9eaa22c2020-07-01 16:21:27 -0700491 vec2 transformedPoint;
492 transformedPoint.x = newX * newZ;
493 transformedPoint.y = newY * newZ;
494 return transformedPoint;
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700495}
496
chaviw9eaa22c2020-07-01 16:21:27 -0700497static float transformAngle(const std::array<float, 9>& matrix, float angleRadians, float originX,
498 float originY) {
Jeff Brown5912f952013-07-01 19:10:31 -0700499 // Construct and transform a vector oriented at the specified clockwise angle from vertical.
500 // Coordinate system: down is increasing Y, right is increasing X.
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700501 float x = sinf(angleRadians);
502 float y = -cosf(angleRadians);
chaviw9eaa22c2020-07-01 16:21:27 -0700503 vec2 transformedPoint = transformPoint(matrix, x, y);
504
505 transformedPoint.x -= originX;
506 transformedPoint.y -= originY;
Jeff Brown5912f952013-07-01 19:10:31 -0700507
508 // Derive the transformed vector's clockwise angle from vertical.
chaviw9eaa22c2020-07-01 16:21:27 -0700509 float result = atan2f(transformedPoint.x, -transformedPoint.y);
Jeff Brown5912f952013-07-01 19:10:31 -0700510 if (result < - M_PI_2) {
511 result += M_PI;
512 } else if (result > M_PI_2) {
513 result -= M_PI;
514 }
515 return result;
516}
517
chaviw9eaa22c2020-07-01 16:21:27 -0700518void MotionEvent::transform(const std::array<float, 9>& matrix) {
519 // We want to preserve the rawX and rawY so we just update the transform
520 // using the values of the transform passed in
521 ui::Transform newTransform;
522 newTransform.set(matrix);
523 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700524
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700525 // Determine how the origin is transformed by the matrix so that we
526 // can transform orientation vectors.
chaviw9eaa22c2020-07-01 16:21:27 -0700527 vec2 origin = transformPoint(matrix, 0, 0);
Garfield Tan00f511d2019-06-12 16:55:40 -0700528
Jeff Brown5912f952013-07-01 19:10:31 -0700529 // Apply the transformation to all samples.
530 size_t numSamples = mSamplePointerCoords.size();
531 for (size_t i = 0; i < numSamples; i++) {
532 PointerCoords& c = mSamplePointerCoords.editItemAt(i);
Jeff Brown5912f952013-07-01 19:10:31 -0700533 float orientation = c.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700534 c.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
chaviw9eaa22c2020-07-01 16:21:27 -0700535 transformAngle(matrix, orientation, origin.x, origin.y));
Jeff Brown5912f952013-07-01 19:10:31 -0700536 }
537}
538
Elliott Hughes6071da72015-08-12 15:27:47 -0700539#ifdef __ANDROID__
chaviw9eaa22c2020-07-01 16:21:27 -0700540static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
541 float dsdx, dtdx, tx, dtdy, dsdy, ty;
542 status_t status = parcel.readFloat(&dsdx);
543 status |= parcel.readFloat(&dtdx);
544 status |= parcel.readFloat(&tx);
545 status |= parcel.readFloat(&dtdy);
546 status |= parcel.readFloat(&dsdy);
547 status |= parcel.readFloat(&ty);
548
549 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
550 return status;
551}
552
553static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
554 status_t status = parcel.writeFloat(transform.dsdx());
555 status |= parcel.writeFloat(transform.dtdx());
556 status |= parcel.writeFloat(transform.tx());
557 status |= parcel.writeFloat(transform.dtdy());
558 status |= parcel.writeFloat(transform.dsdy());
559 status |= parcel.writeFloat(transform.ty());
560 return status;
561}
562
Jeff Brown5912f952013-07-01 19:10:31 -0700563status_t MotionEvent::readFromParcel(Parcel* parcel) {
564 size_t pointerCount = parcel->readInt32();
565 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800566 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
567 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700568 return BAD_VALUE;
569 }
570
Garfield Tan4cc839f2020-01-24 11:26:14 -0800571 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700572 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600573 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800574 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600575 std::vector<uint8_t> hmac;
576 status_t result = parcel->readByteVector(&hmac);
577 if (result != OK || hmac.size() != 32) {
578 return BAD_VALUE;
579 }
580 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700581 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100582 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700583 mFlags = parcel->readInt32();
584 mEdgeFlags = parcel->readInt32();
585 mMetaState = parcel->readInt32();
586 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800587 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700588
589 result = android::readFromParcel(mTransform, *parcel);
590 if (result != OK) {
591 return result;
592 }
Jeff Brown5912f952013-07-01 19:10:31 -0700593 mXPrecision = parcel->readFloat();
594 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700595 mRawXCursorPosition = parcel->readFloat();
596 mRawYCursorPosition = parcel->readFloat();
Jeff Brown5912f952013-07-01 19:10:31 -0700597 mDownTime = parcel->readInt64();
598
599 mPointerProperties.clear();
600 mPointerProperties.setCapacity(pointerCount);
601 mSampleEventTimes.clear();
602 mSampleEventTimes.setCapacity(sampleCount);
603 mSamplePointerCoords.clear();
604 mSamplePointerCoords.setCapacity(sampleCount * pointerCount);
605
606 for (size_t i = 0; i < pointerCount; i++) {
607 mPointerProperties.push();
608 PointerProperties& properties = mPointerProperties.editTop();
609 properties.id = parcel->readInt32();
610 properties.toolType = parcel->readInt32();
611 }
612
Dan Austinc94fc452015-09-22 14:22:41 -0700613 while (sampleCount > 0) {
614 sampleCount--;
Jeff Brown5912f952013-07-01 19:10:31 -0700615 mSampleEventTimes.push(parcel->readInt64());
616 for (size_t i = 0; i < pointerCount; i++) {
617 mSamplePointerCoords.push();
618 status_t status = mSamplePointerCoords.editTop().readFromParcel(parcel);
619 if (status) {
620 return status;
621 }
622 }
623 }
624 return OK;
625}
626
627status_t MotionEvent::writeToParcel(Parcel* parcel) const {
628 size_t pointerCount = mPointerProperties.size();
629 size_t sampleCount = mSampleEventTimes.size();
630
631 parcel->writeInt32(pointerCount);
632 parcel->writeInt32(sampleCount);
633
Garfield Tan4cc839f2020-01-24 11:26:14 -0800634 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700635 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600636 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800637 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600638 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
639 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700640 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100641 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700642 parcel->writeInt32(mFlags);
643 parcel->writeInt32(mEdgeFlags);
644 parcel->writeInt32(mMetaState);
645 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800646 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700647
648 status_t result = android::writeToParcel(mTransform, *parcel);
649 if (result != OK) {
650 return result;
651 }
Jeff Brown5912f952013-07-01 19:10:31 -0700652 parcel->writeFloat(mXPrecision);
653 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700654 parcel->writeFloat(mRawXCursorPosition);
655 parcel->writeFloat(mRawYCursorPosition);
Jeff Brown5912f952013-07-01 19:10:31 -0700656 parcel->writeInt64(mDownTime);
657
658 for (size_t i = 0; i < pointerCount; i++) {
659 const PointerProperties& properties = mPointerProperties.itemAt(i);
660 parcel->writeInt32(properties.id);
661 parcel->writeInt32(properties.toolType);
662 }
663
664 const PointerCoords* pc = mSamplePointerCoords.array();
665 for (size_t h = 0; h < sampleCount; h++) {
666 parcel->writeInt64(mSampleEventTimes.itemAt(h));
667 for (size_t i = 0; i < pointerCount; i++) {
668 status_t status = (pc++)->writeToParcel(parcel);
669 if (status) {
670 return status;
671 }
672 }
673 }
674 return OK;
675}
676#endif
677
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600678bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Jeff Brown5912f952013-07-01 19:10:31 -0700679 if (source & AINPUT_SOURCE_CLASS_POINTER) {
680 // Specifically excludes HOVER_MOVE and SCROLL.
681 switch (action & AMOTION_EVENT_ACTION_MASK) {
682 case AMOTION_EVENT_ACTION_DOWN:
683 case AMOTION_EVENT_ACTION_MOVE:
684 case AMOTION_EVENT_ACTION_UP:
685 case AMOTION_EVENT_ACTION_POINTER_DOWN:
686 case AMOTION_EVENT_ACTION_POINTER_UP:
687 case AMOTION_EVENT_ACTION_CANCEL:
688 case AMOTION_EVENT_ACTION_OUTSIDE:
689 return true;
690 }
691 }
692 return false;
693}
694
Michael Wright872db4f2014-04-22 15:03:51 -0700695const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700696 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700697}
698
699int32_t MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700700 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700701}
702
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700703const char* MotionEvent::actionToString(int32_t action) {
704 // Convert MotionEvent action to string
705 switch (action & AMOTION_EVENT_ACTION_MASK) {
706 case AMOTION_EVENT_ACTION_DOWN:
707 return "DOWN";
708 case AMOTION_EVENT_ACTION_MOVE:
709 return "MOVE";
710 case AMOTION_EVENT_ACTION_UP:
711 return "UP";
712 case AMOTION_EVENT_ACTION_CANCEL:
713 return "CANCEL";
714 case AMOTION_EVENT_ACTION_POINTER_DOWN:
715 return "POINTER_DOWN";
716 case AMOTION_EVENT_ACTION_POINTER_UP:
717 return "POINTER_UP";
718 }
719 return "UNKNOWN";
720}
721
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800722// --- FocusEvent ---
723
Garfield Tan4cc839f2020-01-24 11:26:14 -0800724void FocusEvent::initialize(int32_t id, bool hasFocus, bool inTouchMode) {
725 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600726 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800727 mHasFocus = hasFocus;
728 mInTouchMode = inTouchMode;
729}
730
731void FocusEvent::initialize(const FocusEvent& from) {
732 InputEvent::initialize(from);
733 mHasFocus = from.mHasFocus;
734 mInTouchMode = from.mInTouchMode;
735}
Jeff Brown5912f952013-07-01 19:10:31 -0700736
737// --- PooledInputEventFactory ---
738
739PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
740 mMaxPoolSize(maxPoolSize) {
741}
742
743PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -0700744}
745
746KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800747 if (mKeyEventPool.empty()) {
748 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700749 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800750 KeyEvent* event = mKeyEventPool.front().release();
751 mKeyEventPool.pop();
752 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700753}
754
755MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800756 if (mMotionEventPool.empty()) {
757 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700758 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800759 MotionEvent* event = mMotionEventPool.front().release();
760 mMotionEventPool.pop();
761 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700762}
763
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800764FocusEvent* PooledInputEventFactory::createFocusEvent() {
765 if (mFocusEventPool.empty()) {
766 return new FocusEvent();
767 }
768 FocusEvent* event = mFocusEventPool.front().release();
769 mFocusEventPool.pop();
770 return event;
771}
772
Jeff Brown5912f952013-07-01 19:10:31 -0700773void PooledInputEventFactory::recycle(InputEvent* event) {
774 switch (event->getType()) {
775 case AINPUT_EVENT_TYPE_KEY:
776 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800777 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700778 return;
779 }
780 break;
781 case AINPUT_EVENT_TYPE_MOTION:
782 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800783 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700784 return;
785 }
786 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800787 case AINPUT_EVENT_TYPE_FOCUS:
788 if (mFocusEventPool.size() < mMaxPoolSize) {
789 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
790 return;
791 }
792 break;
Jeff Brown5912f952013-07-01 19:10:31 -0700793 }
794 delete event;
795}
796
797} // namespace android