blob: fc73de3ca5a04fc2cbde73842623fe63aef34faa [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
Garfield Tan84b087e2020-01-23 10:49:05 -080020#include <cutils/compiler.h>
Jeff Brown5912f952013-07-01 19:10:31 -070021#include <limits.h>
Garfield Tan84b087e2020-01-23 10:49:05 -080022#include <string.h>
Jeff Brown5912f952013-07-01 19:10:31 -070023
24#include <input/Input.h>
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080025#include <input/InputDevice.h>
Michael Wright872db4f2014-04-22 15:03:51 -070026#include <input/InputEventLabels.h>
Jeff Brown5912f952013-07-01 19:10:31 -070027
Elliott Hughes6071da72015-08-12 15:27:47 -070028#ifdef __ANDROID__
Jeff Brown5912f952013-07-01 19:10:31 -070029#include <binder/Parcel.h>
Garfield Tan84b087e2020-01-23 10:49:05 -080030#include <sys/random.h>
Jeff Brown5912f952013-07-01 19:10:31 -070031#endif
32
33namespace android {
34
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -080035const char* motionClassificationToString(MotionClassification classification) {
36 switch (classification) {
37 case MotionClassification::NONE:
38 return "NONE";
39 case MotionClassification::AMBIGUOUS_GESTURE:
40 return "AMBIGUOUS_GESTURE";
41 case MotionClassification::DEEP_PRESS:
42 return "DEEP_PRESS";
43 }
44}
45
Garfield Tan84b087e2020-01-23 10:49:05 -080046// --- IdGenerator ---
47IdGenerator::IdGenerator(Source source) : mSource(source) {}
48
49int32_t IdGenerator::nextId() const {
50 constexpr uint32_t SEQUENCE_NUMBER_MASK = ~SOURCE_MASK;
51 int32_t id = 0;
52
53// Avoid building against syscall getrandom(2) on host, which will fail build on Mac. Host doesn't
54// use sequence number so just always return mSource.
55#ifdef __ANDROID__
56 constexpr size_t BUF_LEN = sizeof(id);
57 size_t totalBytes = 0;
58 while (totalBytes < BUF_LEN) {
59 ssize_t bytes = TEMP_FAILURE_RETRY(getrandom(&id, BUF_LEN, GRND_NONBLOCK));
60 if (CC_UNLIKELY(bytes < 0)) {
61 ALOGW("Failed to fill in random number for sequence number: %s.", strerror(errno));
62 id = 0;
63 break;
64 }
65 totalBytes += bytes;
66 }
67#endif // __ANDROID__
68
69 return (id & SEQUENCE_NUMBER_MASK) | static_cast<int32_t>(mSource);
70}
71
Jeff Brown5912f952013-07-01 19:10:31 -070072// --- InputEvent ---
73
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080074const char* inputEventTypeToString(int32_t type) {
75 switch (type) {
76 case AINPUT_EVENT_TYPE_KEY: {
77 return "KEY";
78 }
79 case AINPUT_EVENT_TYPE_MOTION: {
80 return "MOTION";
81 }
82 case AINPUT_EVENT_TYPE_FOCUS: {
83 return "FOCUS";
84 }
85 }
86 return "UNKNOWN";
87}
88
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -080089VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
90 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
91 event.getSource(), event.getDisplayId()},
92 event.getAction(),
93 event.getDownTime(),
94 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
95 event.getKeyCode(),
96 event.getScanCode(),
97 event.getMetaState(),
98 event.getRepeatCount()};
99}
100
101VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
102 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
103 event.getSource(), event.getDisplayId()},
104 event.getRawX(0),
105 event.getRawY(0),
106 event.getActionMasked(),
107 event.getDownTime(),
108 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
109 event.getMetaState(),
110 event.getButtonState()};
111}
112
Garfield Tan4cc839f2020-01-24 11:26:14 -0800113void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600114 std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800115 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700116 mDeviceId = deviceId;
117 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100118 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600119 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700120}
121
122void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800123 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700124 mDeviceId = from.mDeviceId;
125 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100126 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600127 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700128}
129
Garfield Tan4cc839f2020-01-24 11:26:14 -0800130int32_t InputEvent::nextId() {
131 static IdGenerator idGen(IdGenerator::Source::OTHER);
132 return idGen.nextId();
133}
134
Jeff Brown5912f952013-07-01 19:10:31 -0700135// --- KeyEvent ---
136
Michael Wright872db4f2014-04-22 15:03:51 -0700137const char* KeyEvent::getLabel(int32_t keyCode) {
138 return getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700139}
140
Michael Wright872db4f2014-04-22 15:03:51 -0700141int32_t KeyEvent::getKeyCodeFromLabel(const char* label) {
142 return getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700143}
144
Garfield Tan4cc839f2020-01-24 11:26:14 -0800145void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600146 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
147 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
148 nsecs_t downTime, nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800149 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700150 mAction = action;
151 mFlags = flags;
152 mKeyCode = keyCode;
153 mScanCode = scanCode;
154 mMetaState = metaState;
155 mRepeatCount = repeatCount;
156 mDownTime = downTime;
157 mEventTime = eventTime;
158}
159
160void KeyEvent::initialize(const KeyEvent& from) {
161 InputEvent::initialize(from);
162 mAction = from.mAction;
163 mFlags = from.mFlags;
164 mKeyCode = from.mKeyCode;
165 mScanCode = from.mScanCode;
166 mMetaState = from.mMetaState;
167 mRepeatCount = from.mRepeatCount;
168 mDownTime = from.mDownTime;
169 mEventTime = from.mEventTime;
170}
171
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700172const char* KeyEvent::actionToString(int32_t action) {
173 // Convert KeyEvent action to string
174 switch (action) {
175 case AKEY_EVENT_ACTION_DOWN:
176 return "DOWN";
177 case AKEY_EVENT_ACTION_UP:
178 return "UP";
179 case AKEY_EVENT_ACTION_MULTIPLE:
180 return "MULTIPLE";
181 }
182 return "UNKNOWN";
183}
Jeff Brown5912f952013-07-01 19:10:31 -0700184
185// --- PointerCoords ---
186
187float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700188 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700189 return 0;
190 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700191 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700192}
193
194status_t PointerCoords::setAxisValue(int32_t axis, float value) {
195 if (axis < 0 || axis > 63) {
196 return NAME_NOT_FOUND;
197 }
198
Michael Wright38dcdff2014-03-19 12:06:10 -0700199 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
200 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700201 if (value == 0) {
202 return OK; // axes with value 0 do not need to be stored
203 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700204
205 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700206 if (count >= MAX_AXES) {
207 tooManyAxes(axis);
208 return NO_MEMORY;
209 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700210 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700211 for (uint32_t i = count; i > index; i--) {
212 values[i] = values[i - 1];
213 }
214 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700215
Jeff Brown5912f952013-07-01 19:10:31 -0700216 values[index] = value;
217 return OK;
218}
219
220static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
221 float value = c.getAxisValue(axis);
222 if (value != 0) {
223 c.setAxisValue(axis, value * scaleFactor);
224 }
225}
226
Robert Carre07e1032018-11-26 12:55:53 -0800227void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700228 // No need to scale pressure or size since they are normalized.
229 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800230
231 // If there is a global scale factor, it is included in the windowX/YScale
232 // so we don't need to apply it twice to the X/Y axes.
233 // However we don't want to apply any windowXYScale not included in the global scale
234 // to the TOUCH_MAJOR/MINOR coordinates.
235 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
236 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
237 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
238 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
239 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
240 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
241}
242
243void PointerCoords::scale(float globalScaleFactor) {
244 scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700245}
246
Jeff Brownf086ddb2014-02-11 14:28:48 -0800247void PointerCoords::applyOffset(float xOffset, float yOffset) {
248 setAxisValue(AMOTION_EVENT_AXIS_X, getX() + xOffset);
249 setAxisValue(AMOTION_EVENT_AXIS_Y, getY() + yOffset);
250}
251
Elliott Hughes6071da72015-08-12 15:27:47 -0700252#ifdef __ANDROID__
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}
276#endif
277
278void PointerCoords::tooManyAxes(int axis) {
279 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
280 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
281}
282
283bool PointerCoords::operator==(const PointerCoords& other) const {
284 if (bits != other.bits) {
285 return false;
286 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700287 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700288 for (uint32_t i = 0; i < count; i++) {
289 if (values[i] != other.values[i]) {
290 return false;
291 }
292 }
293 return true;
294}
295
296void PointerCoords::copyFrom(const PointerCoords& other) {
297 bits = other.bits;
Michael Wright38dcdff2014-03-19 12:06:10 -0700298 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700299 for (uint32_t i = 0; i < count; i++) {
300 values[i] = other.values[i];
301 }
302}
303
chaviwc01e1372020-07-01 12:37:31 -0700304void PointerCoords::transform(const ui::Transform& transform) {
305 vec2 newCoords = transform.transform(getX(), getY());
306 setAxisValue(AMOTION_EVENT_AXIS_X, newCoords.x);
307 setAxisValue(AMOTION_EVENT_AXIS_Y, newCoords.y);
308}
Jeff Brown5912f952013-07-01 19:10:31 -0700309
310// --- PointerProperties ---
311
312bool PointerProperties::operator==(const PointerProperties& other) const {
313 return id == other.id
314 && toolType == other.toolType;
315}
316
317void PointerProperties::copyFrom(const PointerProperties& other) {
318 id = other.id;
319 toolType = other.toolType;
320}
321
322
323// --- MotionEvent ---
324
Garfield Tan4cc839f2020-01-24 11:26:14 -0800325void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600326 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
327 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700328 int32_t buttonState, MotionClassification classification,
329 const ui::Transform& transform, float xPrecision, float yPrecision,
330 float rawXCursorPosition, float rawYCursorPosition, nsecs_t downTime,
331 nsecs_t eventTime, size_t pointerCount,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600332 const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700333 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800334 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700335 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100336 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700337 mFlags = flags;
338 mEdgeFlags = edgeFlags;
339 mMetaState = metaState;
340 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800341 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700342 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700343 mXPrecision = xPrecision;
344 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700345 mRawXCursorPosition = rawXCursorPosition;
346 mRawYCursorPosition = rawYCursorPosition;
Jeff Brown5912f952013-07-01 19:10:31 -0700347 mDownTime = downTime;
348 mPointerProperties.clear();
349 mPointerProperties.appendArray(pointerProperties, pointerCount);
350 mSampleEventTimes.clear();
351 mSamplePointerCoords.clear();
352 addSample(eventTime, pointerCoords);
353}
354
355void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800356 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
357 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700358 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100359 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700360 mFlags = other->mFlags;
361 mEdgeFlags = other->mEdgeFlags;
362 mMetaState = other->mMetaState;
363 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800364 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700365 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700366 mXPrecision = other->mXPrecision;
367 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700368 mRawXCursorPosition = other->mRawXCursorPosition;
369 mRawYCursorPosition = other->mRawYCursorPosition;
Jeff Brown5912f952013-07-01 19:10:31 -0700370 mDownTime = other->mDownTime;
371 mPointerProperties = other->mPointerProperties;
372
373 if (keepHistory) {
374 mSampleEventTimes = other->mSampleEventTimes;
375 mSamplePointerCoords = other->mSamplePointerCoords;
376 } else {
377 mSampleEventTimes.clear();
378 mSampleEventTimes.push(other->getEventTime());
379 mSamplePointerCoords.clear();
380 size_t pointerCount = other->getPointerCount();
381 size_t historySize = other->getHistorySize();
382 mSamplePointerCoords.appendArray(other->mSamplePointerCoords.array()
383 + (historySize * pointerCount), pointerCount);
384 }
385}
386
387void MotionEvent::addSample(
388 int64_t eventTime,
389 const PointerCoords* pointerCoords) {
390 mSampleEventTimes.push(eventTime);
391 mSamplePointerCoords.appendArray(pointerCoords, getPointerCount());
392}
393
Garfield Tan00f511d2019-06-12 16:55:40 -0700394float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700395 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
396 return vals.x;
Garfield Tan00f511d2019-06-12 16:55:40 -0700397}
398
399float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700400 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
401 return vals.y;
Garfield Tan00f511d2019-06-12 16:55:40 -0700402}
403
Garfield Tan937bb832019-07-25 17:48:31 -0700404void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700405 ui::Transform inverse = mTransform.inverse();
406 vec2 vals = inverse.transform(x, y);
407 mRawXCursorPosition = vals.x;
408 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700409}
410
Jeff Brown5912f952013-07-01 19:10:31 -0700411const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
412 return &mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
413}
414
415float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
416 return getRawPointerCoords(pointerIndex)->getAxisValue(axis);
417}
418
419float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700420 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700421}
422
423const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
424 size_t pointerIndex, size_t historicalIndex) const {
425 return &mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
426}
427
428float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
429 size_t historicalIndex) const {
430 return getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis);
431}
432
433float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
434 size_t historicalIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700435 if (axis != AMOTION_EVENT_AXIS_X && axis != AMOTION_EVENT_AXIS_Y) {
436 return getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis);
437 }
438
439 float rawX = getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getX();
440 float rawY = getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getY();
441 vec2 vals = mTransform.transform(rawX, rawY);
442
Jeff Brown5912f952013-07-01 19:10:31 -0700443 switch (axis) {
444 case AMOTION_EVENT_AXIS_X:
chaviw9eaa22c2020-07-01 16:21:27 -0700445 return vals.x;
Jeff Brown5912f952013-07-01 19:10:31 -0700446 case AMOTION_EVENT_AXIS_Y:
chaviw9eaa22c2020-07-01 16:21:27 -0700447 return vals.y;
Jeff Brown5912f952013-07-01 19:10:31 -0700448 }
chaviw9eaa22c2020-07-01 16:21:27 -0700449
450 // This should never happen
451 return 0;
Jeff Brown5912f952013-07-01 19:10:31 -0700452}
453
454ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
455 size_t pointerCount = mPointerProperties.size();
456 for (size_t i = 0; i < pointerCount; i++) {
457 if (mPointerProperties.itemAt(i).id == pointerId) {
458 return i;
459 }
460 }
461 return -1;
462}
463
464void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700465 float currXOffset = mTransform.tx();
466 float currYOffset = mTransform.ty();
467 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700468}
469
Robert Carre07e1032018-11-26 12:55:53 -0800470void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700471 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800472 mXPrecision *= globalScaleFactor;
473 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700474
475 size_t numSamples = mSamplePointerCoords.size();
476 for (size_t i = 0; i < numSamples; i++) {
chaviw9eaa22c2020-07-01 16:21:27 -0700477 mSamplePointerCoords.editItemAt(i).scale(globalScaleFactor, globalScaleFactor,
478 globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700479 }
480}
481
chaviw9eaa22c2020-07-01 16:21:27 -0700482static vec2 transformPoint(const std::array<float, 9>& matrix, float x, float y) {
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700483 // Apply perspective transform like Skia.
484 float newX = matrix[0] * x + matrix[1] * y + matrix[2];
485 float newY = matrix[3] * x + matrix[4] * y + matrix[5];
486 float newZ = matrix[6] * x + matrix[7] * y + matrix[8];
487 if (newZ) {
488 newZ = 1.0f / newZ;
489 }
chaviw9eaa22c2020-07-01 16:21:27 -0700490 vec2 transformedPoint;
491 transformedPoint.x = newX * newZ;
492 transformedPoint.y = newY * newZ;
493 return transformedPoint;
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700494}
495
chaviw9eaa22c2020-07-01 16:21:27 -0700496static float transformAngle(const std::array<float, 9>& matrix, float angleRadians, float originX,
497 float originY) {
Jeff Brown5912f952013-07-01 19:10:31 -0700498 // Construct and transform a vector oriented at the specified clockwise angle from vertical.
499 // Coordinate system: down is increasing Y, right is increasing X.
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700500 float x = sinf(angleRadians);
501 float y = -cosf(angleRadians);
chaviw9eaa22c2020-07-01 16:21:27 -0700502 vec2 transformedPoint = transformPoint(matrix, x, y);
503
504 transformedPoint.x -= originX;
505 transformedPoint.y -= originY;
Jeff Brown5912f952013-07-01 19:10:31 -0700506
507 // Derive the transformed vector's clockwise angle from vertical.
chaviw9eaa22c2020-07-01 16:21:27 -0700508 float result = atan2f(transformedPoint.x, -transformedPoint.y);
Jeff Brown5912f952013-07-01 19:10:31 -0700509 if (result < - M_PI_2) {
510 result += M_PI;
511 } else if (result > M_PI_2) {
512 result -= M_PI;
513 }
514 return result;
515}
516
chaviw9eaa22c2020-07-01 16:21:27 -0700517void MotionEvent::transform(const std::array<float, 9>& matrix) {
518 // We want to preserve the rawX and rawY so we just update the transform
519 // using the values of the transform passed in
520 ui::Transform newTransform;
521 newTransform.set(matrix);
522 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700523
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700524 // Determine how the origin is transformed by the matrix so that we
525 // can transform orientation vectors.
chaviw9eaa22c2020-07-01 16:21:27 -0700526 vec2 origin = transformPoint(matrix, 0, 0);
Garfield Tan00f511d2019-06-12 16:55:40 -0700527
Jeff Brown5912f952013-07-01 19:10:31 -0700528 // Apply the transformation to all samples.
529 size_t numSamples = mSamplePointerCoords.size();
530 for (size_t i = 0; i < numSamples; i++) {
531 PointerCoords& c = mSamplePointerCoords.editItemAt(i);
Jeff Brown5912f952013-07-01 19:10:31 -0700532 float orientation = c.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700533 c.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
chaviw9eaa22c2020-07-01 16:21:27 -0700534 transformAngle(matrix, orientation, origin.x, origin.y));
Jeff Brown5912f952013-07-01 19:10:31 -0700535 }
536}
537
Elliott Hughes6071da72015-08-12 15:27:47 -0700538#ifdef __ANDROID__
chaviw9eaa22c2020-07-01 16:21:27 -0700539static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
540 float dsdx, dtdx, tx, dtdy, dsdy, ty;
541 status_t status = parcel.readFloat(&dsdx);
542 status |= parcel.readFloat(&dtdx);
543 status |= parcel.readFloat(&tx);
544 status |= parcel.readFloat(&dtdy);
545 status |= parcel.readFloat(&dsdy);
546 status |= parcel.readFloat(&ty);
547
548 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
549 return status;
550}
551
552static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
553 status_t status = parcel.writeFloat(transform.dsdx());
554 status |= parcel.writeFloat(transform.dtdx());
555 status |= parcel.writeFloat(transform.tx());
556 status |= parcel.writeFloat(transform.dtdy());
557 status |= parcel.writeFloat(transform.dsdy());
558 status |= parcel.writeFloat(transform.ty());
559 return status;
560}
561
Jeff Brown5912f952013-07-01 19:10:31 -0700562status_t MotionEvent::readFromParcel(Parcel* parcel) {
563 size_t pointerCount = parcel->readInt32();
564 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800565 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
566 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700567 return BAD_VALUE;
568 }
569
Garfield Tan4cc839f2020-01-24 11:26:14 -0800570 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700571 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600572 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800573 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600574 std::vector<uint8_t> hmac;
575 status_t result = parcel->readByteVector(&hmac);
576 if (result != OK || hmac.size() != 32) {
577 return BAD_VALUE;
578 }
579 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700580 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100581 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700582 mFlags = parcel->readInt32();
583 mEdgeFlags = parcel->readInt32();
584 mMetaState = parcel->readInt32();
585 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800586 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700587
588 result = android::readFromParcel(mTransform, *parcel);
589 if (result != OK) {
590 return result;
591 }
Jeff Brown5912f952013-07-01 19:10:31 -0700592 mXPrecision = parcel->readFloat();
593 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700594 mRawXCursorPosition = parcel->readFloat();
595 mRawYCursorPosition = parcel->readFloat();
Jeff Brown5912f952013-07-01 19:10:31 -0700596 mDownTime = parcel->readInt64();
597
598 mPointerProperties.clear();
599 mPointerProperties.setCapacity(pointerCount);
600 mSampleEventTimes.clear();
601 mSampleEventTimes.setCapacity(sampleCount);
602 mSamplePointerCoords.clear();
603 mSamplePointerCoords.setCapacity(sampleCount * pointerCount);
604
605 for (size_t i = 0; i < pointerCount; i++) {
606 mPointerProperties.push();
607 PointerProperties& properties = mPointerProperties.editTop();
608 properties.id = parcel->readInt32();
609 properties.toolType = parcel->readInt32();
610 }
611
Dan Austinc94fc452015-09-22 14:22:41 -0700612 while (sampleCount > 0) {
613 sampleCount--;
Jeff Brown5912f952013-07-01 19:10:31 -0700614 mSampleEventTimes.push(parcel->readInt64());
615 for (size_t i = 0; i < pointerCount; i++) {
616 mSamplePointerCoords.push();
617 status_t status = mSamplePointerCoords.editTop().readFromParcel(parcel);
618 if (status) {
619 return status;
620 }
621 }
622 }
623 return OK;
624}
625
626status_t MotionEvent::writeToParcel(Parcel* parcel) const {
627 size_t pointerCount = mPointerProperties.size();
628 size_t sampleCount = mSampleEventTimes.size();
629
630 parcel->writeInt32(pointerCount);
631 parcel->writeInt32(sampleCount);
632
Garfield Tan4cc839f2020-01-24 11:26:14 -0800633 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700634 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600635 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800636 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600637 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
638 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700639 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100640 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700641 parcel->writeInt32(mFlags);
642 parcel->writeInt32(mEdgeFlags);
643 parcel->writeInt32(mMetaState);
644 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800645 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700646
647 status_t result = android::writeToParcel(mTransform, *parcel);
648 if (result != OK) {
649 return result;
650 }
Jeff Brown5912f952013-07-01 19:10:31 -0700651 parcel->writeFloat(mXPrecision);
652 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700653 parcel->writeFloat(mRawXCursorPosition);
654 parcel->writeFloat(mRawYCursorPosition);
Jeff Brown5912f952013-07-01 19:10:31 -0700655 parcel->writeInt64(mDownTime);
656
657 for (size_t i = 0; i < pointerCount; i++) {
658 const PointerProperties& properties = mPointerProperties.itemAt(i);
659 parcel->writeInt32(properties.id);
660 parcel->writeInt32(properties.toolType);
661 }
662
663 const PointerCoords* pc = mSamplePointerCoords.array();
664 for (size_t h = 0; h < sampleCount; h++) {
665 parcel->writeInt64(mSampleEventTimes.itemAt(h));
666 for (size_t i = 0; i < pointerCount; i++) {
667 status_t status = (pc++)->writeToParcel(parcel);
668 if (status) {
669 return status;
670 }
671 }
672 }
673 return OK;
674}
675#endif
676
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600677bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Jeff Brown5912f952013-07-01 19:10:31 -0700678 if (source & AINPUT_SOURCE_CLASS_POINTER) {
679 // Specifically excludes HOVER_MOVE and SCROLL.
680 switch (action & AMOTION_EVENT_ACTION_MASK) {
681 case AMOTION_EVENT_ACTION_DOWN:
682 case AMOTION_EVENT_ACTION_MOVE:
683 case AMOTION_EVENT_ACTION_UP:
684 case AMOTION_EVENT_ACTION_POINTER_DOWN:
685 case AMOTION_EVENT_ACTION_POINTER_UP:
686 case AMOTION_EVENT_ACTION_CANCEL:
687 case AMOTION_EVENT_ACTION_OUTSIDE:
688 return true;
689 }
690 }
691 return false;
692}
693
Michael Wright872db4f2014-04-22 15:03:51 -0700694const char* MotionEvent::getLabel(int32_t axis) {
695 return getAxisLabel(axis);
696}
697
698int32_t MotionEvent::getAxisFromLabel(const char* label) {
699 return getAxisByLabel(label);
700}
701
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700702const char* MotionEvent::actionToString(int32_t action) {
703 // Convert MotionEvent action to string
704 switch (action & AMOTION_EVENT_ACTION_MASK) {
705 case AMOTION_EVENT_ACTION_DOWN:
706 return "DOWN";
707 case AMOTION_EVENT_ACTION_MOVE:
708 return "MOVE";
709 case AMOTION_EVENT_ACTION_UP:
710 return "UP";
711 case AMOTION_EVENT_ACTION_CANCEL:
712 return "CANCEL";
713 case AMOTION_EVENT_ACTION_POINTER_DOWN:
714 return "POINTER_DOWN";
715 case AMOTION_EVENT_ACTION_POINTER_UP:
716 return "POINTER_UP";
717 }
718 return "UNKNOWN";
719}
720
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800721// --- FocusEvent ---
722
Garfield Tan4cc839f2020-01-24 11:26:14 -0800723void FocusEvent::initialize(int32_t id, bool hasFocus, bool inTouchMode) {
724 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600725 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800726 mHasFocus = hasFocus;
727 mInTouchMode = inTouchMode;
728}
729
730void FocusEvent::initialize(const FocusEvent& from) {
731 InputEvent::initialize(from);
732 mHasFocus = from.mHasFocus;
733 mInTouchMode = from.mInTouchMode;
734}
Jeff Brown5912f952013-07-01 19:10:31 -0700735
736// --- PooledInputEventFactory ---
737
738PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
739 mMaxPoolSize(maxPoolSize) {
740}
741
742PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -0700743}
744
745KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800746 if (mKeyEventPool.empty()) {
747 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700748 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800749 KeyEvent* event = mKeyEventPool.front().release();
750 mKeyEventPool.pop();
751 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700752}
753
754MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800755 if (mMotionEventPool.empty()) {
756 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700757 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800758 MotionEvent* event = mMotionEventPool.front().release();
759 mMotionEventPool.pop();
760 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700761}
762
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800763FocusEvent* PooledInputEventFactory::createFocusEvent() {
764 if (mFocusEventPool.empty()) {
765 return new FocusEvent();
766 }
767 FocusEvent* event = mFocusEventPool.front().release();
768 mFocusEventPool.pop();
769 return event;
770}
771
Jeff Brown5912f952013-07-01 19:10:31 -0700772void PooledInputEventFactory::recycle(InputEvent* event) {
773 switch (event->getType()) {
774 case AINPUT_EVENT_TYPE_KEY:
775 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800776 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700777 return;
778 }
779 break;
780 case AINPUT_EVENT_TYPE_MOTION:
781 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800782 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700783 return;
784 }
785 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800786 case AINPUT_EVENT_TYPE_FOCUS:
787 if (mFocusEventPool.size() < mMaxPoolSize) {
788 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
789 return;
790 }
791 break;
Jeff Brown5912f952013-07-01 19:10:31 -0700792 }
793 delete event;
794}
795
796} // namespace android