blob: abd031ae2642179fc67086f3146bc498fdf94bf7 [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
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600113void InputEvent::initialize(int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600114 std::array<uint8_t, 32> hmac) {
Jeff Brown5912f952013-07-01 19:10:31 -0700115 mDeviceId = deviceId;
116 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100117 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600118 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700119}
120
121void InputEvent::initialize(const InputEvent& from) {
122 mDeviceId = from.mDeviceId;
123 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100124 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600125 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700126}
127
128// --- KeyEvent ---
129
Michael Wright872db4f2014-04-22 15:03:51 -0700130const char* KeyEvent::getLabel(int32_t keyCode) {
131 return getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700132}
133
Michael Wright872db4f2014-04-22 15:03:51 -0700134int32_t KeyEvent::getKeyCodeFromLabel(const char* label) {
135 return getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700136}
137
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600138void KeyEvent::initialize(int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600139 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
140 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
141 nsecs_t downTime, nsecs_t eventTime) {
142 InputEvent::initialize(deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700143 mAction = action;
144 mFlags = flags;
145 mKeyCode = keyCode;
146 mScanCode = scanCode;
147 mMetaState = metaState;
148 mRepeatCount = repeatCount;
149 mDownTime = downTime;
150 mEventTime = eventTime;
151}
152
153void KeyEvent::initialize(const KeyEvent& from) {
154 InputEvent::initialize(from);
155 mAction = from.mAction;
156 mFlags = from.mFlags;
157 mKeyCode = from.mKeyCode;
158 mScanCode = from.mScanCode;
159 mMetaState = from.mMetaState;
160 mRepeatCount = from.mRepeatCount;
161 mDownTime = from.mDownTime;
162 mEventTime = from.mEventTime;
163}
164
165
166// --- PointerCoords ---
167
168float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700169 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700170 return 0;
171 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700172 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700173}
174
175status_t PointerCoords::setAxisValue(int32_t axis, float value) {
176 if (axis < 0 || axis > 63) {
177 return NAME_NOT_FOUND;
178 }
179
Michael Wright38dcdff2014-03-19 12:06:10 -0700180 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
181 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700182 if (value == 0) {
183 return OK; // axes with value 0 do not need to be stored
184 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700185
186 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700187 if (count >= MAX_AXES) {
188 tooManyAxes(axis);
189 return NO_MEMORY;
190 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700191 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700192 for (uint32_t i = count; i > index; i--) {
193 values[i] = values[i - 1];
194 }
195 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700196
Jeff Brown5912f952013-07-01 19:10:31 -0700197 values[index] = value;
198 return OK;
199}
200
201static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
202 float value = c.getAxisValue(axis);
203 if (value != 0) {
204 c.setAxisValue(axis, value * scaleFactor);
205 }
206}
207
Robert Carre07e1032018-11-26 12:55:53 -0800208void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700209 // No need to scale pressure or size since they are normalized.
210 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800211
212 // If there is a global scale factor, it is included in the windowX/YScale
213 // so we don't need to apply it twice to the X/Y axes.
214 // However we don't want to apply any windowXYScale not included in the global scale
215 // to the TOUCH_MAJOR/MINOR coordinates.
216 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
217 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
218 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
219 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
220 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
221 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
222}
223
224void PointerCoords::scale(float globalScaleFactor) {
225 scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700226}
227
Jeff Brownf086ddb2014-02-11 14:28:48 -0800228void PointerCoords::applyOffset(float xOffset, float yOffset) {
229 setAxisValue(AMOTION_EVENT_AXIS_X, getX() + xOffset);
230 setAxisValue(AMOTION_EVENT_AXIS_Y, getY() + yOffset);
231}
232
Elliott Hughes6071da72015-08-12 15:27:47 -0700233#ifdef __ANDROID__
Jeff Brown5912f952013-07-01 19:10:31 -0700234status_t PointerCoords::readFromParcel(Parcel* parcel) {
235 bits = parcel->readInt64();
236
Michael Wright38dcdff2014-03-19 12:06:10 -0700237 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700238 if (count > MAX_AXES) {
239 return BAD_VALUE;
240 }
241
242 for (uint32_t i = 0; i < count; i++) {
243 values[i] = parcel->readFloat();
244 }
245 return OK;
246}
247
248status_t PointerCoords::writeToParcel(Parcel* parcel) const {
249 parcel->writeInt64(bits);
250
Michael Wright38dcdff2014-03-19 12:06:10 -0700251 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700252 for (uint32_t i = 0; i < count; i++) {
253 parcel->writeFloat(values[i]);
254 }
255 return OK;
256}
257#endif
258
259void PointerCoords::tooManyAxes(int axis) {
260 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
261 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
262}
263
264bool PointerCoords::operator==(const PointerCoords& other) const {
265 if (bits != other.bits) {
266 return false;
267 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700268 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700269 for (uint32_t i = 0; i < count; i++) {
270 if (values[i] != other.values[i]) {
271 return false;
272 }
273 }
274 return true;
275}
276
277void PointerCoords::copyFrom(const PointerCoords& other) {
278 bits = other.bits;
Michael Wright38dcdff2014-03-19 12:06:10 -0700279 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700280 for (uint32_t i = 0; i < count; i++) {
281 values[i] = other.values[i];
282 }
283}
284
285
286// --- PointerProperties ---
287
288bool PointerProperties::operator==(const PointerProperties& other) const {
289 return id == other.id
290 && toolType == other.toolType;
291}
292
293void PointerProperties::copyFrom(const PointerProperties& other) {
294 id = other.id;
295 toolType = other.toolType;
296}
297
298
299// --- MotionEvent ---
300
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600301void MotionEvent::initialize(int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600302 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
303 int32_t flags, int32_t edgeFlags, int32_t metaState,
304 int32_t buttonState, MotionClassification classification, float xScale,
305 float yScale, float xOffset, float yOffset, float xPrecision,
306 float yPrecision, float rawXCursorPosition, float rawYCursorPosition,
307 nsecs_t downTime, nsecs_t eventTime, size_t pointerCount,
308 const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700309 const PointerCoords* pointerCoords) {
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600310 InputEvent::initialize(deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700311 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100312 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700313 mFlags = flags;
314 mEdgeFlags = edgeFlags;
315 mMetaState = metaState;
316 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800317 mClassification = classification;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600318 mXScale = xScale;
319 mYScale = yScale;
Jeff Brown5912f952013-07-01 19:10:31 -0700320 mXOffset = xOffset;
321 mYOffset = yOffset;
322 mXPrecision = xPrecision;
323 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700324 mRawXCursorPosition = rawXCursorPosition;
325 mRawYCursorPosition = rawYCursorPosition;
Jeff Brown5912f952013-07-01 19:10:31 -0700326 mDownTime = downTime;
327 mPointerProperties.clear();
328 mPointerProperties.appendArray(pointerProperties, pointerCount);
329 mSampleEventTimes.clear();
330 mSamplePointerCoords.clear();
331 addSample(eventTime, pointerCoords);
332}
333
334void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600335 InputEvent::initialize(other->mDeviceId, other->mSource, other->mDisplayId, other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700336 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100337 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700338 mFlags = other->mFlags;
339 mEdgeFlags = other->mEdgeFlags;
340 mMetaState = other->mMetaState;
341 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800342 mClassification = other->mClassification;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600343 mXScale = other->mXScale;
344 mYScale = other->mYScale;
Jeff Brown5912f952013-07-01 19:10:31 -0700345 mXOffset = other->mXOffset;
346 mYOffset = other->mYOffset;
347 mXPrecision = other->mXPrecision;
348 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700349 mRawXCursorPosition = other->mRawXCursorPosition;
350 mRawYCursorPosition = other->mRawYCursorPosition;
Jeff Brown5912f952013-07-01 19:10:31 -0700351 mDownTime = other->mDownTime;
352 mPointerProperties = other->mPointerProperties;
353
354 if (keepHistory) {
355 mSampleEventTimes = other->mSampleEventTimes;
356 mSamplePointerCoords = other->mSamplePointerCoords;
357 } else {
358 mSampleEventTimes.clear();
359 mSampleEventTimes.push(other->getEventTime());
360 mSamplePointerCoords.clear();
361 size_t pointerCount = other->getPointerCount();
362 size_t historySize = other->getHistorySize();
363 mSamplePointerCoords.appendArray(other->mSamplePointerCoords.array()
364 + (historySize * pointerCount), pointerCount);
365 }
366}
367
368void MotionEvent::addSample(
369 int64_t eventTime,
370 const PointerCoords* pointerCoords) {
371 mSampleEventTimes.push(eventTime);
372 mSamplePointerCoords.appendArray(pointerCoords, getPointerCount());
373}
374
Garfield Tan00f511d2019-06-12 16:55:40 -0700375float MotionEvent::getXCursorPosition() const {
376 const float rawX = getRawXCursorPosition();
chaviw82357092020-01-28 13:13:06 -0800377 return rawX * mXScale + mXOffset;
Garfield Tan00f511d2019-06-12 16:55:40 -0700378}
379
380float MotionEvent::getYCursorPosition() const {
381 const float rawY = getRawYCursorPosition();
chaviw82357092020-01-28 13:13:06 -0800382 return rawY * mYScale + mYOffset;
Garfield Tan00f511d2019-06-12 16:55:40 -0700383}
384
Garfield Tan937bb832019-07-25 17:48:31 -0700385void MotionEvent::setCursorPosition(float x, float y) {
chaviw82357092020-01-28 13:13:06 -0800386 mRawXCursorPosition = (x - mXOffset) / mXScale;
387 mRawYCursorPosition = (y - mYOffset) / mYScale;
Garfield Tan937bb832019-07-25 17:48:31 -0700388}
389
Jeff Brown5912f952013-07-01 19:10:31 -0700390const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
391 return &mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
392}
393
394float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
395 return getRawPointerCoords(pointerIndex)->getAxisValue(axis);
396}
397
398float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
399 float value = getRawPointerCoords(pointerIndex)->getAxisValue(axis);
400 switch (axis) {
401 case AMOTION_EVENT_AXIS_X:
chaviw82357092020-01-28 13:13:06 -0800402 return value * mXScale + mXOffset;
Jeff Brown5912f952013-07-01 19:10:31 -0700403 case AMOTION_EVENT_AXIS_Y:
chaviw82357092020-01-28 13:13:06 -0800404 return value * mYScale + mYOffset;
Jeff Brown5912f952013-07-01 19:10:31 -0700405 }
406 return value;
407}
408
409const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
410 size_t pointerIndex, size_t historicalIndex) const {
411 return &mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
412}
413
414float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
415 size_t historicalIndex) const {
416 return getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis);
417}
418
419float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
420 size_t historicalIndex) const {
421 float value = getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis);
422 switch (axis) {
423 case AMOTION_EVENT_AXIS_X:
chaviw82357092020-01-28 13:13:06 -0800424 return value * mXScale + mXOffset;
Jeff Brown5912f952013-07-01 19:10:31 -0700425 case AMOTION_EVENT_AXIS_Y:
chaviw82357092020-01-28 13:13:06 -0800426 return value * mYScale + mYOffset;
Jeff Brown5912f952013-07-01 19:10:31 -0700427 }
428 return value;
429}
430
431ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
432 size_t pointerCount = mPointerProperties.size();
433 for (size_t i = 0; i < pointerCount; i++) {
434 if (mPointerProperties.itemAt(i).id == pointerId) {
435 return i;
436 }
437 }
438 return -1;
439}
440
441void MotionEvent::offsetLocation(float xOffset, float yOffset) {
442 mXOffset += xOffset;
443 mYOffset += yOffset;
444}
445
Robert Carre07e1032018-11-26 12:55:53 -0800446void MotionEvent::scale(float globalScaleFactor) {
447 mXOffset *= globalScaleFactor;
448 mYOffset *= globalScaleFactor;
449 mXPrecision *= globalScaleFactor;
450 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700451
452 size_t numSamples = mSamplePointerCoords.size();
453 for (size_t i = 0; i < numSamples; i++) {
Robert Carre07e1032018-11-26 12:55:53 -0800454 mSamplePointerCoords.editItemAt(i).scale(globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700455 }
456}
457
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700458static void transformPoint(const float matrix[9], float x, float y, float *outX, float *outY) {
459 // Apply perspective transform like Skia.
460 float newX = matrix[0] * x + matrix[1] * y + matrix[2];
461 float newY = matrix[3] * x + matrix[4] * y + matrix[5];
462 float newZ = matrix[6] * x + matrix[7] * y + matrix[8];
463 if (newZ) {
464 newZ = 1.0f / newZ;
465 }
466 *outX = newX * newZ;
467 *outY = newY * newZ;
468}
469
470static float transformAngle(const float matrix[9], float angleRadians,
471 float originX, float originY) {
Jeff Brown5912f952013-07-01 19:10:31 -0700472 // Construct and transform a vector oriented at the specified clockwise angle from vertical.
473 // Coordinate system: down is increasing Y, right is increasing X.
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700474 float x = sinf(angleRadians);
475 float y = -cosf(angleRadians);
476 transformPoint(matrix, x, y, &x, &y);
477 x -= originX;
478 y -= originY;
Jeff Brown5912f952013-07-01 19:10:31 -0700479
480 // Derive the transformed vector's clockwise angle from vertical.
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700481 float result = atan2f(x, -y);
Jeff Brown5912f952013-07-01 19:10:31 -0700482 if (result < - M_PI_2) {
483 result += M_PI;
484 } else if (result > M_PI_2) {
485 result -= M_PI;
486 }
487 return result;
488}
489
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700490void MotionEvent::transform(const float matrix[9]) {
Jeff Brown5912f952013-07-01 19:10:31 -0700491 // The tricky part of this implementation is to preserve the value of
492 // rawX and rawY. So we apply the transformation to the first point
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700493 // then derive an appropriate new X/Y offset that will preserve rawX
494 // and rawY for that point.
495 float oldXOffset = mXOffset;
496 float oldYOffset = mYOffset;
497 float newX, newY;
chaviw82357092020-01-28 13:13:06 -0800498 float scaledRawX = getRawX(0) * mXScale;
499 float scaledRawY = getRawY(0) * mYScale;
500 transformPoint(matrix, scaledRawX + oldXOffset, scaledRawY + oldYOffset, &newX, &newY);
501 mXOffset = newX - scaledRawX;
502 mYOffset = newY - scaledRawY;
Jeff Brown5912f952013-07-01 19:10:31 -0700503
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700504 // Determine how the origin is transformed by the matrix so that we
505 // can transform orientation vectors.
506 float originX, originY;
507 transformPoint(matrix, 0, 0, &originX, &originY);
Jeff Brown5912f952013-07-01 19:10:31 -0700508
Garfield Tan00f511d2019-06-12 16:55:40 -0700509 // Apply the transformation to cursor position.
Garfield Tan937bb832019-07-25 17:48:31 -0700510 if (isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition)) {
chaviw82357092020-01-28 13:13:06 -0800511 float x = mRawXCursorPosition * mXScale + oldXOffset;
512 float y = mRawYCursorPosition * mYScale + oldYOffset;
Garfield Tan00f511d2019-06-12 16:55:40 -0700513 transformPoint(matrix, x, y, &x, &y);
chaviw82357092020-01-28 13:13:06 -0800514 mRawXCursorPosition = (x - mXOffset) / mXScale;
515 mRawYCursorPosition = (y - mYOffset) / mYScale;
Garfield Tan00f511d2019-06-12 16:55:40 -0700516 }
517
Jeff Brown5912f952013-07-01 19:10:31 -0700518 // Apply the transformation to all samples.
519 size_t numSamples = mSamplePointerCoords.size();
520 for (size_t i = 0; i < numSamples; i++) {
521 PointerCoords& c = mSamplePointerCoords.editItemAt(i);
chaviw82357092020-01-28 13:13:06 -0800522 float x = c.getAxisValue(AMOTION_EVENT_AXIS_X) * mXScale + oldXOffset;
523 float y = c.getAxisValue(AMOTION_EVENT_AXIS_Y) * mYScale + oldYOffset;
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700524 transformPoint(matrix, x, y, &x, &y);
chaviw82357092020-01-28 13:13:06 -0800525 c.setAxisValue(AMOTION_EVENT_AXIS_X, (x - mXOffset) / mXScale);
526 c.setAxisValue(AMOTION_EVENT_AXIS_Y, (y - mYOffset) / mYScale);
Jeff Brown5912f952013-07-01 19:10:31 -0700527
528 float orientation = c.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700529 c.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
530 transformAngle(matrix, orientation, originX, originY));
Jeff Brown5912f952013-07-01 19:10:31 -0700531 }
532}
533
Elliott Hughes6071da72015-08-12 15:27:47 -0700534#ifdef __ANDROID__
Jeff Brown5912f952013-07-01 19:10:31 -0700535status_t MotionEvent::readFromParcel(Parcel* parcel) {
536 size_t pointerCount = parcel->readInt32();
537 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800538 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
539 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700540 return BAD_VALUE;
541 }
542
543 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600544 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800545 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600546 std::vector<uint8_t> hmac;
547 status_t result = parcel->readByteVector(&hmac);
548 if (result != OK || hmac.size() != 32) {
549 return BAD_VALUE;
550 }
551 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700552 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100553 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700554 mFlags = parcel->readInt32();
555 mEdgeFlags = parcel->readInt32();
556 mMetaState = parcel->readInt32();
557 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800558 mClassification = static_cast<MotionClassification>(parcel->readByte());
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600559 mXScale = parcel->readFloat();
560 mYScale = parcel->readFloat();
Jeff Brown5912f952013-07-01 19:10:31 -0700561 mXOffset = parcel->readFloat();
562 mYOffset = parcel->readFloat();
563 mXPrecision = parcel->readFloat();
564 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700565 mRawXCursorPosition = parcel->readFloat();
566 mRawYCursorPosition = parcel->readFloat();
Jeff Brown5912f952013-07-01 19:10:31 -0700567 mDownTime = parcel->readInt64();
568
569 mPointerProperties.clear();
570 mPointerProperties.setCapacity(pointerCount);
571 mSampleEventTimes.clear();
572 mSampleEventTimes.setCapacity(sampleCount);
573 mSamplePointerCoords.clear();
574 mSamplePointerCoords.setCapacity(sampleCount * pointerCount);
575
576 for (size_t i = 0; i < pointerCount; i++) {
577 mPointerProperties.push();
578 PointerProperties& properties = mPointerProperties.editTop();
579 properties.id = parcel->readInt32();
580 properties.toolType = parcel->readInt32();
581 }
582
Dan Austinc94fc452015-09-22 14:22:41 -0700583 while (sampleCount > 0) {
584 sampleCount--;
Jeff Brown5912f952013-07-01 19:10:31 -0700585 mSampleEventTimes.push(parcel->readInt64());
586 for (size_t i = 0; i < pointerCount; i++) {
587 mSamplePointerCoords.push();
588 status_t status = mSamplePointerCoords.editTop().readFromParcel(parcel);
589 if (status) {
590 return status;
591 }
592 }
593 }
594 return OK;
595}
596
597status_t MotionEvent::writeToParcel(Parcel* parcel) const {
598 size_t pointerCount = mPointerProperties.size();
599 size_t sampleCount = mSampleEventTimes.size();
600
601 parcel->writeInt32(pointerCount);
602 parcel->writeInt32(sampleCount);
603
604 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600605 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800606 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600607 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
608 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700609 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100610 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700611 parcel->writeInt32(mFlags);
612 parcel->writeInt32(mEdgeFlags);
613 parcel->writeInt32(mMetaState);
614 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800615 parcel->writeByte(static_cast<int8_t>(mClassification));
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600616 parcel->writeFloat(mXScale);
617 parcel->writeFloat(mYScale);
Jeff Brown5912f952013-07-01 19:10:31 -0700618 parcel->writeFloat(mXOffset);
619 parcel->writeFloat(mYOffset);
620 parcel->writeFloat(mXPrecision);
621 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700622 parcel->writeFloat(mRawXCursorPosition);
623 parcel->writeFloat(mRawYCursorPosition);
Jeff Brown5912f952013-07-01 19:10:31 -0700624 parcel->writeInt64(mDownTime);
625
626 for (size_t i = 0; i < pointerCount; i++) {
627 const PointerProperties& properties = mPointerProperties.itemAt(i);
628 parcel->writeInt32(properties.id);
629 parcel->writeInt32(properties.toolType);
630 }
631
632 const PointerCoords* pc = mSamplePointerCoords.array();
633 for (size_t h = 0; h < sampleCount; h++) {
634 parcel->writeInt64(mSampleEventTimes.itemAt(h));
635 for (size_t i = 0; i < pointerCount; i++) {
636 status_t status = (pc++)->writeToParcel(parcel);
637 if (status) {
638 return status;
639 }
640 }
641 }
642 return OK;
643}
644#endif
645
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600646bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Jeff Brown5912f952013-07-01 19:10:31 -0700647 if (source & AINPUT_SOURCE_CLASS_POINTER) {
648 // Specifically excludes HOVER_MOVE and SCROLL.
649 switch (action & AMOTION_EVENT_ACTION_MASK) {
650 case AMOTION_EVENT_ACTION_DOWN:
651 case AMOTION_EVENT_ACTION_MOVE:
652 case AMOTION_EVENT_ACTION_UP:
653 case AMOTION_EVENT_ACTION_POINTER_DOWN:
654 case AMOTION_EVENT_ACTION_POINTER_UP:
655 case AMOTION_EVENT_ACTION_CANCEL:
656 case AMOTION_EVENT_ACTION_OUTSIDE:
657 return true;
658 }
659 }
660 return false;
661}
662
Michael Wright872db4f2014-04-22 15:03:51 -0700663const char* MotionEvent::getLabel(int32_t axis) {
664 return getAxisLabel(axis);
665}
666
667int32_t MotionEvent::getAxisFromLabel(const char* label) {
668 return getAxisByLabel(label);
669}
670
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800671// --- FocusEvent ---
672
673void FocusEvent::initialize(bool hasFocus, bool inTouchMode) {
674 InputEvent::initialize(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600675 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800676 mHasFocus = hasFocus;
677 mInTouchMode = inTouchMode;
678}
679
680void FocusEvent::initialize(const FocusEvent& from) {
681 InputEvent::initialize(from);
682 mHasFocus = from.mHasFocus;
683 mInTouchMode = from.mInTouchMode;
684}
Jeff Brown5912f952013-07-01 19:10:31 -0700685
686// --- PooledInputEventFactory ---
687
688PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
689 mMaxPoolSize(maxPoolSize) {
690}
691
692PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -0700693}
694
695KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800696 if (mKeyEventPool.empty()) {
697 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700698 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800699 KeyEvent* event = mKeyEventPool.front().release();
700 mKeyEventPool.pop();
701 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700702}
703
704MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800705 if (mMotionEventPool.empty()) {
706 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700707 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800708 MotionEvent* event = mMotionEventPool.front().release();
709 mMotionEventPool.pop();
710 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700711}
712
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800713FocusEvent* PooledInputEventFactory::createFocusEvent() {
714 if (mFocusEventPool.empty()) {
715 return new FocusEvent();
716 }
717 FocusEvent* event = mFocusEventPool.front().release();
718 mFocusEventPool.pop();
719 return event;
720}
721
Jeff Brown5912f952013-07-01 19:10:31 -0700722void PooledInputEventFactory::recycle(InputEvent* event) {
723 switch (event->getType()) {
724 case AINPUT_EVENT_TYPE_KEY:
725 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800726 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700727 return;
728 }
729 break;
730 case AINPUT_EVENT_TYPE_MOTION:
731 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800732 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700733 return;
734 }
735 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800736 case AINPUT_EVENT_TYPE_FOCUS:
737 if (mFocusEventPool.size() < mMaxPoolSize) {
738 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
739 return;
740 }
741 break;
Jeff Brown5912f952013-07-01 19:10:31 -0700742 }
743 delete event;
744}
745
746} // namespace android