blob: 2ac079ec5235b262871aec9ad952177b3a6bf030 [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,
328 int32_t buttonState, MotionClassification classification, float xScale,
329 float yScale, float xOffset, float yOffset, float xPrecision,
330 float yPrecision, float rawXCursorPosition, float rawYCursorPosition,
331 nsecs_t downTime, nsecs_t eventTime, size_t pointerCount,
332 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;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600342 mXScale = xScale;
343 mYScale = yScale;
Jeff Brown5912f952013-07-01 19:10:31 -0700344 mXOffset = xOffset;
345 mYOffset = yOffset;
346 mXPrecision = xPrecision;
347 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700348 mRawXCursorPosition = rawXCursorPosition;
349 mRawYCursorPosition = rawYCursorPosition;
Jeff Brown5912f952013-07-01 19:10:31 -0700350 mDownTime = downTime;
351 mPointerProperties.clear();
352 mPointerProperties.appendArray(pointerProperties, pointerCount);
353 mSampleEventTimes.clear();
354 mSamplePointerCoords.clear();
355 addSample(eventTime, pointerCoords);
356}
357
358void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800359 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
360 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700361 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100362 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700363 mFlags = other->mFlags;
364 mEdgeFlags = other->mEdgeFlags;
365 mMetaState = other->mMetaState;
366 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800367 mClassification = other->mClassification;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600368 mXScale = other->mXScale;
369 mYScale = other->mYScale;
Jeff Brown5912f952013-07-01 19:10:31 -0700370 mXOffset = other->mXOffset;
371 mYOffset = other->mYOffset;
372 mXPrecision = other->mXPrecision;
373 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700374 mRawXCursorPosition = other->mRawXCursorPosition;
375 mRawYCursorPosition = other->mRawYCursorPosition;
Jeff Brown5912f952013-07-01 19:10:31 -0700376 mDownTime = other->mDownTime;
377 mPointerProperties = other->mPointerProperties;
378
379 if (keepHistory) {
380 mSampleEventTimes = other->mSampleEventTimes;
381 mSamplePointerCoords = other->mSamplePointerCoords;
382 } else {
383 mSampleEventTimes.clear();
384 mSampleEventTimes.push(other->getEventTime());
385 mSamplePointerCoords.clear();
386 size_t pointerCount = other->getPointerCount();
387 size_t historySize = other->getHistorySize();
388 mSamplePointerCoords.appendArray(other->mSamplePointerCoords.array()
389 + (historySize * pointerCount), pointerCount);
390 }
391}
392
393void MotionEvent::addSample(
394 int64_t eventTime,
395 const PointerCoords* pointerCoords) {
396 mSampleEventTimes.push(eventTime);
397 mSamplePointerCoords.appendArray(pointerCoords, getPointerCount());
398}
399
Garfield Tan00f511d2019-06-12 16:55:40 -0700400float MotionEvent::getXCursorPosition() const {
401 const float rawX = getRawXCursorPosition();
chaviw82357092020-01-28 13:13:06 -0800402 return rawX * mXScale + mXOffset;
Garfield Tan00f511d2019-06-12 16:55:40 -0700403}
404
405float MotionEvent::getYCursorPosition() const {
406 const float rawY = getRawYCursorPosition();
chaviw82357092020-01-28 13:13:06 -0800407 return rawY * mYScale + mYOffset;
Garfield Tan00f511d2019-06-12 16:55:40 -0700408}
409
Garfield Tan937bb832019-07-25 17:48:31 -0700410void MotionEvent::setCursorPosition(float x, float y) {
chaviw82357092020-01-28 13:13:06 -0800411 mRawXCursorPosition = (x - mXOffset) / mXScale;
412 mRawYCursorPosition = (y - mYOffset) / mYScale;
Garfield Tan937bb832019-07-25 17:48:31 -0700413}
414
Jeff Brown5912f952013-07-01 19:10:31 -0700415const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
416 return &mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
417}
418
419float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
420 return getRawPointerCoords(pointerIndex)->getAxisValue(axis);
421}
422
423float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
424 float value = getRawPointerCoords(pointerIndex)->getAxisValue(axis);
425 switch (axis) {
426 case AMOTION_EVENT_AXIS_X:
chaviw82357092020-01-28 13:13:06 -0800427 return value * mXScale + mXOffset;
Jeff Brown5912f952013-07-01 19:10:31 -0700428 case AMOTION_EVENT_AXIS_Y:
chaviw82357092020-01-28 13:13:06 -0800429 return value * mYScale + mYOffset;
Jeff Brown5912f952013-07-01 19:10:31 -0700430 }
431 return value;
432}
433
434const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
435 size_t pointerIndex, size_t historicalIndex) const {
436 return &mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
437}
438
439float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
440 size_t historicalIndex) const {
441 return getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis);
442}
443
444float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
445 size_t historicalIndex) const {
446 float value = getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis);
447 switch (axis) {
448 case AMOTION_EVENT_AXIS_X:
chaviw82357092020-01-28 13:13:06 -0800449 return value * mXScale + mXOffset;
Jeff Brown5912f952013-07-01 19:10:31 -0700450 case AMOTION_EVENT_AXIS_Y:
chaviw82357092020-01-28 13:13:06 -0800451 return value * mYScale + mYOffset;
Jeff Brown5912f952013-07-01 19:10:31 -0700452 }
453 return value;
454}
455
456ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
457 size_t pointerCount = mPointerProperties.size();
458 for (size_t i = 0; i < pointerCount; i++) {
459 if (mPointerProperties.itemAt(i).id == pointerId) {
460 return i;
461 }
462 }
463 return -1;
464}
465
466void MotionEvent::offsetLocation(float xOffset, float yOffset) {
467 mXOffset += xOffset;
468 mYOffset += yOffset;
469}
470
Robert Carre07e1032018-11-26 12:55:53 -0800471void MotionEvent::scale(float globalScaleFactor) {
472 mXOffset *= globalScaleFactor;
473 mYOffset *= globalScaleFactor;
474 mXPrecision *= globalScaleFactor;
475 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700476
477 size_t numSamples = mSamplePointerCoords.size();
478 for (size_t i = 0; i < numSamples; i++) {
Robert Carre07e1032018-11-26 12:55:53 -0800479 mSamplePointerCoords.editItemAt(i).scale(globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700480 }
481}
482
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700483static void transformPoint(const float matrix[9], float x, float y, float *outX, float *outY) {
484 // 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 }
491 *outX = newX * newZ;
492 *outY = newY * newZ;
493}
494
495static float transformAngle(const float matrix[9], float angleRadians,
496 float originX, 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);
501 transformPoint(matrix, x, y, &x, &y);
502 x -= originX;
503 y -= originY;
Jeff Brown5912f952013-07-01 19:10:31 -0700504
505 // Derive the transformed vector's clockwise angle from vertical.
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700506 float result = atan2f(x, -y);
Jeff Brown5912f952013-07-01 19:10:31 -0700507 if (result < - M_PI_2) {
508 result += M_PI;
509 } else if (result > M_PI_2) {
510 result -= M_PI;
511 }
512 return result;
513}
514
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700515void MotionEvent::transform(const float matrix[9]) {
Jeff Brown5912f952013-07-01 19:10:31 -0700516 // The tricky part of this implementation is to preserve the value of
517 // rawX and rawY. So we apply the transformation to the first point
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700518 // then derive an appropriate new X/Y offset that will preserve rawX
519 // and rawY for that point.
520 float oldXOffset = mXOffset;
521 float oldYOffset = mYOffset;
522 float newX, newY;
chaviw82357092020-01-28 13:13:06 -0800523 float scaledRawX = getRawX(0) * mXScale;
524 float scaledRawY = getRawY(0) * mYScale;
525 transformPoint(matrix, scaledRawX + oldXOffset, scaledRawY + oldYOffset, &newX, &newY);
526 mXOffset = newX - scaledRawX;
527 mYOffset = newY - scaledRawY;
Jeff Brown5912f952013-07-01 19:10:31 -0700528
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700529 // Determine how the origin is transformed by the matrix so that we
530 // can transform orientation vectors.
531 float originX, originY;
532 transformPoint(matrix, 0, 0, &originX, &originY);
Jeff Brown5912f952013-07-01 19:10:31 -0700533
Garfield Tan00f511d2019-06-12 16:55:40 -0700534 // Apply the transformation to cursor position.
Garfield Tan937bb832019-07-25 17:48:31 -0700535 if (isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition)) {
chaviw82357092020-01-28 13:13:06 -0800536 float x = mRawXCursorPosition * mXScale + oldXOffset;
537 float y = mRawYCursorPosition * mYScale + oldYOffset;
Garfield Tan00f511d2019-06-12 16:55:40 -0700538 transformPoint(matrix, x, y, &x, &y);
chaviw82357092020-01-28 13:13:06 -0800539 mRawXCursorPosition = (x - mXOffset) / mXScale;
540 mRawYCursorPosition = (y - mYOffset) / mYScale;
Garfield Tan00f511d2019-06-12 16:55:40 -0700541 }
542
Jeff Brown5912f952013-07-01 19:10:31 -0700543 // Apply the transformation to all samples.
544 size_t numSamples = mSamplePointerCoords.size();
545 for (size_t i = 0; i < numSamples; i++) {
546 PointerCoords& c = mSamplePointerCoords.editItemAt(i);
chaviw82357092020-01-28 13:13:06 -0800547 float x = c.getAxisValue(AMOTION_EVENT_AXIS_X) * mXScale + oldXOffset;
548 float y = c.getAxisValue(AMOTION_EVENT_AXIS_Y) * mYScale + oldYOffset;
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700549 transformPoint(matrix, x, y, &x, &y);
chaviw82357092020-01-28 13:13:06 -0800550 c.setAxisValue(AMOTION_EVENT_AXIS_X, (x - mXOffset) / mXScale);
551 c.setAxisValue(AMOTION_EVENT_AXIS_Y, (y - mYOffset) / mYScale);
Jeff Brown5912f952013-07-01 19:10:31 -0700552
553 float orientation = c.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700554 c.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
555 transformAngle(matrix, orientation, originX, originY));
Jeff Brown5912f952013-07-01 19:10:31 -0700556 }
557}
558
Elliott Hughes6071da72015-08-12 15:27:47 -0700559#ifdef __ANDROID__
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());
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600585 mXScale = parcel->readFloat();
586 mYScale = parcel->readFloat();
Jeff Brown5912f952013-07-01 19:10:31 -0700587 mXOffset = parcel->readFloat();
588 mYOffset = parcel->readFloat();
589 mXPrecision = parcel->readFloat();
590 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700591 mRawXCursorPosition = parcel->readFloat();
592 mRawYCursorPosition = parcel->readFloat();
Jeff Brown5912f952013-07-01 19:10:31 -0700593 mDownTime = parcel->readInt64();
594
595 mPointerProperties.clear();
596 mPointerProperties.setCapacity(pointerCount);
597 mSampleEventTimes.clear();
598 mSampleEventTimes.setCapacity(sampleCount);
599 mSamplePointerCoords.clear();
600 mSamplePointerCoords.setCapacity(sampleCount * pointerCount);
601
602 for (size_t i = 0; i < pointerCount; i++) {
603 mPointerProperties.push();
604 PointerProperties& properties = mPointerProperties.editTop();
605 properties.id = parcel->readInt32();
606 properties.toolType = parcel->readInt32();
607 }
608
Dan Austinc94fc452015-09-22 14:22:41 -0700609 while (sampleCount > 0) {
610 sampleCount--;
Jeff Brown5912f952013-07-01 19:10:31 -0700611 mSampleEventTimes.push(parcel->readInt64());
612 for (size_t i = 0; i < pointerCount; i++) {
613 mSamplePointerCoords.push();
614 status_t status = mSamplePointerCoords.editTop().readFromParcel(parcel);
615 if (status) {
616 return status;
617 }
618 }
619 }
620 return OK;
621}
622
623status_t MotionEvent::writeToParcel(Parcel* parcel) const {
624 size_t pointerCount = mPointerProperties.size();
625 size_t sampleCount = mSampleEventTimes.size();
626
627 parcel->writeInt32(pointerCount);
628 parcel->writeInt32(sampleCount);
629
Garfield Tan4cc839f2020-01-24 11:26:14 -0800630 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700631 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600632 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800633 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600634 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
635 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700636 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100637 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700638 parcel->writeInt32(mFlags);
639 parcel->writeInt32(mEdgeFlags);
640 parcel->writeInt32(mMetaState);
641 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800642 parcel->writeByte(static_cast<int8_t>(mClassification));
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600643 parcel->writeFloat(mXScale);
644 parcel->writeFloat(mYScale);
Jeff Brown5912f952013-07-01 19:10:31 -0700645 parcel->writeFloat(mXOffset);
646 parcel->writeFloat(mYOffset);
647 parcel->writeFloat(mXPrecision);
648 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700649 parcel->writeFloat(mRawXCursorPosition);
650 parcel->writeFloat(mRawYCursorPosition);
Jeff Brown5912f952013-07-01 19:10:31 -0700651 parcel->writeInt64(mDownTime);
652
653 for (size_t i = 0; i < pointerCount; i++) {
654 const PointerProperties& properties = mPointerProperties.itemAt(i);
655 parcel->writeInt32(properties.id);
656 parcel->writeInt32(properties.toolType);
657 }
658
659 const PointerCoords* pc = mSamplePointerCoords.array();
660 for (size_t h = 0; h < sampleCount; h++) {
661 parcel->writeInt64(mSampleEventTimes.itemAt(h));
662 for (size_t i = 0; i < pointerCount; i++) {
663 status_t status = (pc++)->writeToParcel(parcel);
664 if (status) {
665 return status;
666 }
667 }
668 }
669 return OK;
670}
671#endif
672
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600673bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Jeff Brown5912f952013-07-01 19:10:31 -0700674 if (source & AINPUT_SOURCE_CLASS_POINTER) {
675 // Specifically excludes HOVER_MOVE and SCROLL.
676 switch (action & AMOTION_EVENT_ACTION_MASK) {
677 case AMOTION_EVENT_ACTION_DOWN:
678 case AMOTION_EVENT_ACTION_MOVE:
679 case AMOTION_EVENT_ACTION_UP:
680 case AMOTION_EVENT_ACTION_POINTER_DOWN:
681 case AMOTION_EVENT_ACTION_POINTER_UP:
682 case AMOTION_EVENT_ACTION_CANCEL:
683 case AMOTION_EVENT_ACTION_OUTSIDE:
684 return true;
685 }
686 }
687 return false;
688}
689
Michael Wright872db4f2014-04-22 15:03:51 -0700690const char* MotionEvent::getLabel(int32_t axis) {
691 return getAxisLabel(axis);
692}
693
694int32_t MotionEvent::getAxisFromLabel(const char* label) {
695 return getAxisByLabel(label);
696}
697
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700698const char* MotionEvent::actionToString(int32_t action) {
699 // Convert MotionEvent action to string
700 switch (action & AMOTION_EVENT_ACTION_MASK) {
701 case AMOTION_EVENT_ACTION_DOWN:
702 return "DOWN";
703 case AMOTION_EVENT_ACTION_MOVE:
704 return "MOVE";
705 case AMOTION_EVENT_ACTION_UP:
706 return "UP";
707 case AMOTION_EVENT_ACTION_CANCEL:
708 return "CANCEL";
709 case AMOTION_EVENT_ACTION_POINTER_DOWN:
710 return "POINTER_DOWN";
711 case AMOTION_EVENT_ACTION_POINTER_UP:
712 return "POINTER_UP";
713 }
714 return "UNKNOWN";
715}
716
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800717// --- FocusEvent ---
718
Garfield Tan4cc839f2020-01-24 11:26:14 -0800719void FocusEvent::initialize(int32_t id, bool hasFocus, bool inTouchMode) {
720 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600721 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800722 mHasFocus = hasFocus;
723 mInTouchMode = inTouchMode;
724}
725
726void FocusEvent::initialize(const FocusEvent& from) {
727 InputEvent::initialize(from);
728 mHasFocus = from.mHasFocus;
729 mInTouchMode = from.mInTouchMode;
730}
Jeff Brown5912f952013-07-01 19:10:31 -0700731
732// --- PooledInputEventFactory ---
733
734PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
735 mMaxPoolSize(maxPoolSize) {
736}
737
738PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -0700739}
740
741KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800742 if (mKeyEventPool.empty()) {
743 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700744 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800745 KeyEvent* event = mKeyEventPool.front().release();
746 mKeyEventPool.pop();
747 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700748}
749
750MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800751 if (mMotionEventPool.empty()) {
752 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700753 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800754 MotionEvent* event = mMotionEventPool.front().release();
755 mMotionEventPool.pop();
756 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700757}
758
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800759FocusEvent* PooledInputEventFactory::createFocusEvent() {
760 if (mFocusEventPool.empty()) {
761 return new FocusEvent();
762 }
763 FocusEvent* event = mFocusEventPool.front().release();
764 mFocusEventPool.pop();
765 return event;
766}
767
Jeff Brown5912f952013-07-01 19:10:31 -0700768void PooledInputEventFactory::recycle(InputEvent* event) {
769 switch (event->getType()) {
770 case AINPUT_EVENT_TYPE_KEY:
771 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800772 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700773 return;
774 }
775 break;
776 case AINPUT_EVENT_TYPE_MOTION:
777 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800778 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700779 return;
780 }
781 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800782 case AINPUT_EVENT_TYPE_FOCUS:
783 if (mFocusEventPool.size() < mMaxPoolSize) {
784 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
785 return;
786 }
787 break;
Jeff Brown5912f952013-07-01 19:10:31 -0700788 }
789 delete event;
790}
791
792} // namespace android