blob: c2437673dfa53e40438c59a15db3975c7d634857 [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
172
173// --- PointerCoords ---
174
175float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700176 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700177 return 0;
178 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700179 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700180}
181
182status_t PointerCoords::setAxisValue(int32_t axis, float value) {
183 if (axis < 0 || axis > 63) {
184 return NAME_NOT_FOUND;
185 }
186
Michael Wright38dcdff2014-03-19 12:06:10 -0700187 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
188 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700189 if (value == 0) {
190 return OK; // axes with value 0 do not need to be stored
191 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700192
193 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700194 if (count >= MAX_AXES) {
195 tooManyAxes(axis);
196 return NO_MEMORY;
197 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700198 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700199 for (uint32_t i = count; i > index; i--) {
200 values[i] = values[i - 1];
201 }
202 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700203
Jeff Brown5912f952013-07-01 19:10:31 -0700204 values[index] = value;
205 return OK;
206}
207
208static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
209 float value = c.getAxisValue(axis);
210 if (value != 0) {
211 c.setAxisValue(axis, value * scaleFactor);
212 }
213}
214
Robert Carre07e1032018-11-26 12:55:53 -0800215void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700216 // No need to scale pressure or size since they are normalized.
217 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800218
219 // If there is a global scale factor, it is included in the windowX/YScale
220 // so we don't need to apply it twice to the X/Y axes.
221 // However we don't want to apply any windowXYScale not included in the global scale
222 // to the TOUCH_MAJOR/MINOR coordinates.
223 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
224 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
225 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
226 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
227 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
228 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
229}
230
231void PointerCoords::scale(float globalScaleFactor) {
232 scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700233}
234
Jeff Brownf086ddb2014-02-11 14:28:48 -0800235void PointerCoords::applyOffset(float xOffset, float yOffset) {
236 setAxisValue(AMOTION_EVENT_AXIS_X, getX() + xOffset);
237 setAxisValue(AMOTION_EVENT_AXIS_Y, getY() + yOffset);
238}
239
Elliott Hughes6071da72015-08-12 15:27:47 -0700240#ifdef __ANDROID__
Jeff Brown5912f952013-07-01 19:10:31 -0700241status_t PointerCoords::readFromParcel(Parcel* parcel) {
242 bits = parcel->readInt64();
243
Michael Wright38dcdff2014-03-19 12:06:10 -0700244 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700245 if (count > MAX_AXES) {
246 return BAD_VALUE;
247 }
248
249 for (uint32_t i = 0; i < count; i++) {
250 values[i] = parcel->readFloat();
251 }
252 return OK;
253}
254
255status_t PointerCoords::writeToParcel(Parcel* parcel) const {
256 parcel->writeInt64(bits);
257
Michael Wright38dcdff2014-03-19 12:06:10 -0700258 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700259 for (uint32_t i = 0; i < count; i++) {
260 parcel->writeFloat(values[i]);
261 }
262 return OK;
263}
264#endif
265
266void PointerCoords::tooManyAxes(int axis) {
267 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
268 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
269}
270
271bool PointerCoords::operator==(const PointerCoords& other) const {
272 if (bits != other.bits) {
273 return false;
274 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700275 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700276 for (uint32_t i = 0; i < count; i++) {
277 if (values[i] != other.values[i]) {
278 return false;
279 }
280 }
281 return true;
282}
283
284void PointerCoords::copyFrom(const PointerCoords& other) {
285 bits = other.bits;
Michael Wright38dcdff2014-03-19 12:06:10 -0700286 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700287 for (uint32_t i = 0; i < count; i++) {
288 values[i] = other.values[i];
289 }
290}
291
292
293// --- PointerProperties ---
294
295bool PointerProperties::operator==(const PointerProperties& other) const {
296 return id == other.id
297 && toolType == other.toolType;
298}
299
300void PointerProperties::copyFrom(const PointerProperties& other) {
301 id = other.id;
302 toolType = other.toolType;
303}
304
305
306// --- MotionEvent ---
307
Garfield Tan4cc839f2020-01-24 11:26:14 -0800308void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600309 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
310 int32_t flags, int32_t edgeFlags, int32_t metaState,
311 int32_t buttonState, MotionClassification classification, float xScale,
312 float yScale, float xOffset, float yOffset, float xPrecision,
313 float yPrecision, float rawXCursorPosition, float rawYCursorPosition,
314 nsecs_t downTime, nsecs_t eventTime, size_t pointerCount,
315 const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700316 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800317 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700318 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100319 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700320 mFlags = flags;
321 mEdgeFlags = edgeFlags;
322 mMetaState = metaState;
323 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800324 mClassification = classification;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600325 mXScale = xScale;
326 mYScale = yScale;
Jeff Brown5912f952013-07-01 19:10:31 -0700327 mXOffset = xOffset;
328 mYOffset = yOffset;
329 mXPrecision = xPrecision;
330 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700331 mRawXCursorPosition = rawXCursorPosition;
332 mRawYCursorPosition = rawYCursorPosition;
Jeff Brown5912f952013-07-01 19:10:31 -0700333 mDownTime = downTime;
334 mPointerProperties.clear();
335 mPointerProperties.appendArray(pointerProperties, pointerCount);
336 mSampleEventTimes.clear();
337 mSamplePointerCoords.clear();
338 addSample(eventTime, pointerCoords);
339}
340
341void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800342 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
343 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700344 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100345 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700346 mFlags = other->mFlags;
347 mEdgeFlags = other->mEdgeFlags;
348 mMetaState = other->mMetaState;
349 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800350 mClassification = other->mClassification;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600351 mXScale = other->mXScale;
352 mYScale = other->mYScale;
Jeff Brown5912f952013-07-01 19:10:31 -0700353 mXOffset = other->mXOffset;
354 mYOffset = other->mYOffset;
355 mXPrecision = other->mXPrecision;
356 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700357 mRawXCursorPosition = other->mRawXCursorPosition;
358 mRawYCursorPosition = other->mRawYCursorPosition;
Jeff Brown5912f952013-07-01 19:10:31 -0700359 mDownTime = other->mDownTime;
360 mPointerProperties = other->mPointerProperties;
361
362 if (keepHistory) {
363 mSampleEventTimes = other->mSampleEventTimes;
364 mSamplePointerCoords = other->mSamplePointerCoords;
365 } else {
366 mSampleEventTimes.clear();
367 mSampleEventTimes.push(other->getEventTime());
368 mSamplePointerCoords.clear();
369 size_t pointerCount = other->getPointerCount();
370 size_t historySize = other->getHistorySize();
371 mSamplePointerCoords.appendArray(other->mSamplePointerCoords.array()
372 + (historySize * pointerCount), pointerCount);
373 }
374}
375
376void MotionEvent::addSample(
377 int64_t eventTime,
378 const PointerCoords* pointerCoords) {
379 mSampleEventTimes.push(eventTime);
380 mSamplePointerCoords.appendArray(pointerCoords, getPointerCount());
381}
382
Garfield Tan00f511d2019-06-12 16:55:40 -0700383float MotionEvent::getXCursorPosition() const {
384 const float rawX = getRawXCursorPosition();
chaviw82357092020-01-28 13:13:06 -0800385 return rawX * mXScale + mXOffset;
Garfield Tan00f511d2019-06-12 16:55:40 -0700386}
387
388float MotionEvent::getYCursorPosition() const {
389 const float rawY = getRawYCursorPosition();
chaviw82357092020-01-28 13:13:06 -0800390 return rawY * mYScale + mYOffset;
Garfield Tan00f511d2019-06-12 16:55:40 -0700391}
392
Garfield Tan937bb832019-07-25 17:48:31 -0700393void MotionEvent::setCursorPosition(float x, float y) {
chaviw82357092020-01-28 13:13:06 -0800394 mRawXCursorPosition = (x - mXOffset) / mXScale;
395 mRawYCursorPosition = (y - mYOffset) / mYScale;
Garfield Tan937bb832019-07-25 17:48:31 -0700396}
397
Jeff Brown5912f952013-07-01 19:10:31 -0700398const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
399 return &mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
400}
401
402float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
403 return getRawPointerCoords(pointerIndex)->getAxisValue(axis);
404}
405
406float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
407 float value = getRawPointerCoords(pointerIndex)->getAxisValue(axis);
408 switch (axis) {
409 case AMOTION_EVENT_AXIS_X:
chaviw82357092020-01-28 13:13:06 -0800410 return value * mXScale + mXOffset;
Jeff Brown5912f952013-07-01 19:10:31 -0700411 case AMOTION_EVENT_AXIS_Y:
chaviw82357092020-01-28 13:13:06 -0800412 return value * mYScale + mYOffset;
Jeff Brown5912f952013-07-01 19:10:31 -0700413 }
414 return value;
415}
416
417const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
418 size_t pointerIndex, size_t historicalIndex) const {
419 return &mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
420}
421
422float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
423 size_t historicalIndex) const {
424 return getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis);
425}
426
427float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
428 size_t historicalIndex) const {
429 float value = getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis);
430 switch (axis) {
431 case AMOTION_EVENT_AXIS_X:
chaviw82357092020-01-28 13:13:06 -0800432 return value * mXScale + mXOffset;
Jeff Brown5912f952013-07-01 19:10:31 -0700433 case AMOTION_EVENT_AXIS_Y:
chaviw82357092020-01-28 13:13:06 -0800434 return value * mYScale + mYOffset;
Jeff Brown5912f952013-07-01 19:10:31 -0700435 }
436 return value;
437}
438
439ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
440 size_t pointerCount = mPointerProperties.size();
441 for (size_t i = 0; i < pointerCount; i++) {
442 if (mPointerProperties.itemAt(i).id == pointerId) {
443 return i;
444 }
445 }
446 return -1;
447}
448
449void MotionEvent::offsetLocation(float xOffset, float yOffset) {
450 mXOffset += xOffset;
451 mYOffset += yOffset;
452}
453
Robert Carre07e1032018-11-26 12:55:53 -0800454void MotionEvent::scale(float globalScaleFactor) {
455 mXOffset *= globalScaleFactor;
456 mYOffset *= globalScaleFactor;
457 mXPrecision *= globalScaleFactor;
458 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700459
460 size_t numSamples = mSamplePointerCoords.size();
461 for (size_t i = 0; i < numSamples; i++) {
Robert Carre07e1032018-11-26 12:55:53 -0800462 mSamplePointerCoords.editItemAt(i).scale(globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700463 }
464}
465
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700466static void transformPoint(const float matrix[9], float x, float y, float *outX, float *outY) {
467 // Apply perspective transform like Skia.
468 float newX = matrix[0] * x + matrix[1] * y + matrix[2];
469 float newY = matrix[3] * x + matrix[4] * y + matrix[5];
470 float newZ = matrix[6] * x + matrix[7] * y + matrix[8];
471 if (newZ) {
472 newZ = 1.0f / newZ;
473 }
474 *outX = newX * newZ;
475 *outY = newY * newZ;
476}
477
478static float transformAngle(const float matrix[9], float angleRadians,
479 float originX, float originY) {
Jeff Brown5912f952013-07-01 19:10:31 -0700480 // Construct and transform a vector oriented at the specified clockwise angle from vertical.
481 // Coordinate system: down is increasing Y, right is increasing X.
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700482 float x = sinf(angleRadians);
483 float y = -cosf(angleRadians);
484 transformPoint(matrix, x, y, &x, &y);
485 x -= originX;
486 y -= originY;
Jeff Brown5912f952013-07-01 19:10:31 -0700487
488 // Derive the transformed vector's clockwise angle from vertical.
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700489 float result = atan2f(x, -y);
Jeff Brown5912f952013-07-01 19:10:31 -0700490 if (result < - M_PI_2) {
491 result += M_PI;
492 } else if (result > M_PI_2) {
493 result -= M_PI;
494 }
495 return result;
496}
497
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700498void MotionEvent::transform(const float matrix[9]) {
Jeff Brown5912f952013-07-01 19:10:31 -0700499 // The tricky part of this implementation is to preserve the value of
500 // rawX and rawY. So we apply the transformation to the first point
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700501 // then derive an appropriate new X/Y offset that will preserve rawX
502 // and rawY for that point.
503 float oldXOffset = mXOffset;
504 float oldYOffset = mYOffset;
505 float newX, newY;
chaviw82357092020-01-28 13:13:06 -0800506 float scaledRawX = getRawX(0) * mXScale;
507 float scaledRawY = getRawY(0) * mYScale;
508 transformPoint(matrix, scaledRawX + oldXOffset, scaledRawY + oldYOffset, &newX, &newY);
509 mXOffset = newX - scaledRawX;
510 mYOffset = newY - scaledRawY;
Jeff Brown5912f952013-07-01 19:10:31 -0700511
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700512 // Determine how the origin is transformed by the matrix so that we
513 // can transform orientation vectors.
514 float originX, originY;
515 transformPoint(matrix, 0, 0, &originX, &originY);
Jeff Brown5912f952013-07-01 19:10:31 -0700516
Garfield Tan00f511d2019-06-12 16:55:40 -0700517 // Apply the transformation to cursor position.
Garfield Tan937bb832019-07-25 17:48:31 -0700518 if (isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition)) {
chaviw82357092020-01-28 13:13:06 -0800519 float x = mRawXCursorPosition * mXScale + oldXOffset;
520 float y = mRawYCursorPosition * mYScale + oldYOffset;
Garfield Tan00f511d2019-06-12 16:55:40 -0700521 transformPoint(matrix, x, y, &x, &y);
chaviw82357092020-01-28 13:13:06 -0800522 mRawXCursorPosition = (x - mXOffset) / mXScale;
523 mRawYCursorPosition = (y - mYOffset) / mYScale;
Garfield Tan00f511d2019-06-12 16:55:40 -0700524 }
525
Jeff Brown5912f952013-07-01 19:10:31 -0700526 // Apply the transformation to all samples.
527 size_t numSamples = mSamplePointerCoords.size();
528 for (size_t i = 0; i < numSamples; i++) {
529 PointerCoords& c = mSamplePointerCoords.editItemAt(i);
chaviw82357092020-01-28 13:13:06 -0800530 float x = c.getAxisValue(AMOTION_EVENT_AXIS_X) * mXScale + oldXOffset;
531 float y = c.getAxisValue(AMOTION_EVENT_AXIS_Y) * mYScale + oldYOffset;
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700532 transformPoint(matrix, x, y, &x, &y);
chaviw82357092020-01-28 13:13:06 -0800533 c.setAxisValue(AMOTION_EVENT_AXIS_X, (x - mXOffset) / mXScale);
534 c.setAxisValue(AMOTION_EVENT_AXIS_Y, (y - mYOffset) / mYScale);
Jeff Brown5912f952013-07-01 19:10:31 -0700535
536 float orientation = c.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700537 c.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
538 transformAngle(matrix, orientation, originX, originY));
Jeff Brown5912f952013-07-01 19:10:31 -0700539 }
540}
541
Elliott Hughes6071da72015-08-12 15:27:47 -0700542#ifdef __ANDROID__
Jeff Brown5912f952013-07-01 19:10:31 -0700543status_t MotionEvent::readFromParcel(Parcel* parcel) {
544 size_t pointerCount = parcel->readInt32();
545 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800546 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
547 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700548 return BAD_VALUE;
549 }
550
Garfield Tan4cc839f2020-01-24 11:26:14 -0800551 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700552 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600553 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800554 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600555 std::vector<uint8_t> hmac;
556 status_t result = parcel->readByteVector(&hmac);
557 if (result != OK || hmac.size() != 32) {
558 return BAD_VALUE;
559 }
560 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700561 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100562 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700563 mFlags = parcel->readInt32();
564 mEdgeFlags = parcel->readInt32();
565 mMetaState = parcel->readInt32();
566 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800567 mClassification = static_cast<MotionClassification>(parcel->readByte());
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600568 mXScale = parcel->readFloat();
569 mYScale = parcel->readFloat();
Jeff Brown5912f952013-07-01 19:10:31 -0700570 mXOffset = parcel->readFloat();
571 mYOffset = parcel->readFloat();
572 mXPrecision = parcel->readFloat();
573 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700574 mRawXCursorPosition = parcel->readFloat();
575 mRawYCursorPosition = parcel->readFloat();
Jeff Brown5912f952013-07-01 19:10:31 -0700576 mDownTime = parcel->readInt64();
577
578 mPointerProperties.clear();
579 mPointerProperties.setCapacity(pointerCount);
580 mSampleEventTimes.clear();
581 mSampleEventTimes.setCapacity(sampleCount);
582 mSamplePointerCoords.clear();
583 mSamplePointerCoords.setCapacity(sampleCount * pointerCount);
584
585 for (size_t i = 0; i < pointerCount; i++) {
586 mPointerProperties.push();
587 PointerProperties& properties = mPointerProperties.editTop();
588 properties.id = parcel->readInt32();
589 properties.toolType = parcel->readInt32();
590 }
591
Dan Austinc94fc452015-09-22 14:22:41 -0700592 while (sampleCount > 0) {
593 sampleCount--;
Jeff Brown5912f952013-07-01 19:10:31 -0700594 mSampleEventTimes.push(parcel->readInt64());
595 for (size_t i = 0; i < pointerCount; i++) {
596 mSamplePointerCoords.push();
597 status_t status = mSamplePointerCoords.editTop().readFromParcel(parcel);
598 if (status) {
599 return status;
600 }
601 }
602 }
603 return OK;
604}
605
606status_t MotionEvent::writeToParcel(Parcel* parcel) const {
607 size_t pointerCount = mPointerProperties.size();
608 size_t sampleCount = mSampleEventTimes.size();
609
610 parcel->writeInt32(pointerCount);
611 parcel->writeInt32(sampleCount);
612
Garfield Tan4cc839f2020-01-24 11:26:14 -0800613 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700614 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600615 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800616 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600617 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
618 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700619 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100620 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700621 parcel->writeInt32(mFlags);
622 parcel->writeInt32(mEdgeFlags);
623 parcel->writeInt32(mMetaState);
624 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800625 parcel->writeByte(static_cast<int8_t>(mClassification));
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600626 parcel->writeFloat(mXScale);
627 parcel->writeFloat(mYScale);
Jeff Brown5912f952013-07-01 19:10:31 -0700628 parcel->writeFloat(mXOffset);
629 parcel->writeFloat(mYOffset);
630 parcel->writeFloat(mXPrecision);
631 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700632 parcel->writeFloat(mRawXCursorPosition);
633 parcel->writeFloat(mRawYCursorPosition);
Jeff Brown5912f952013-07-01 19:10:31 -0700634 parcel->writeInt64(mDownTime);
635
636 for (size_t i = 0; i < pointerCount; i++) {
637 const PointerProperties& properties = mPointerProperties.itemAt(i);
638 parcel->writeInt32(properties.id);
639 parcel->writeInt32(properties.toolType);
640 }
641
642 const PointerCoords* pc = mSamplePointerCoords.array();
643 for (size_t h = 0; h < sampleCount; h++) {
644 parcel->writeInt64(mSampleEventTimes.itemAt(h));
645 for (size_t i = 0; i < pointerCount; i++) {
646 status_t status = (pc++)->writeToParcel(parcel);
647 if (status) {
648 return status;
649 }
650 }
651 }
652 return OK;
653}
654#endif
655
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600656bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Jeff Brown5912f952013-07-01 19:10:31 -0700657 if (source & AINPUT_SOURCE_CLASS_POINTER) {
658 // Specifically excludes HOVER_MOVE and SCROLL.
659 switch (action & AMOTION_EVENT_ACTION_MASK) {
660 case AMOTION_EVENT_ACTION_DOWN:
661 case AMOTION_EVENT_ACTION_MOVE:
662 case AMOTION_EVENT_ACTION_UP:
663 case AMOTION_EVENT_ACTION_POINTER_DOWN:
664 case AMOTION_EVENT_ACTION_POINTER_UP:
665 case AMOTION_EVENT_ACTION_CANCEL:
666 case AMOTION_EVENT_ACTION_OUTSIDE:
667 return true;
668 }
669 }
670 return false;
671}
672
Michael Wright872db4f2014-04-22 15:03:51 -0700673const char* MotionEvent::getLabel(int32_t axis) {
674 return getAxisLabel(axis);
675}
676
677int32_t MotionEvent::getAxisFromLabel(const char* label) {
678 return getAxisByLabel(label);
679}
680
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800681// --- FocusEvent ---
682
Garfield Tan4cc839f2020-01-24 11:26:14 -0800683void FocusEvent::initialize(int32_t id, bool hasFocus, bool inTouchMode) {
684 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600685 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800686 mHasFocus = hasFocus;
687 mInTouchMode = inTouchMode;
688}
689
690void FocusEvent::initialize(const FocusEvent& from) {
691 InputEvent::initialize(from);
692 mHasFocus = from.mHasFocus;
693 mInTouchMode = from.mInTouchMode;
694}
Jeff Brown5912f952013-07-01 19:10:31 -0700695
696// --- PooledInputEventFactory ---
697
698PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
699 mMaxPoolSize(maxPoolSize) {
700}
701
702PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -0700703}
704
705KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800706 if (mKeyEventPool.empty()) {
707 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700708 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800709 KeyEvent* event = mKeyEventPool.front().release();
710 mKeyEventPool.pop();
711 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700712}
713
714MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800715 if (mMotionEventPool.empty()) {
716 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700717 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800718 MotionEvent* event = mMotionEventPool.front().release();
719 mMotionEventPool.pop();
720 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700721}
722
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800723FocusEvent* PooledInputEventFactory::createFocusEvent() {
724 if (mFocusEventPool.empty()) {
725 return new FocusEvent();
726 }
727 FocusEvent* event = mFocusEventPool.front().release();
728 mFocusEventPool.pop();
729 return event;
730}
731
Jeff Brown5912f952013-07-01 19:10:31 -0700732void PooledInputEventFactory::recycle(InputEvent* event) {
733 switch (event->getType()) {
734 case AINPUT_EVENT_TYPE_KEY:
735 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800736 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700737 return;
738 }
739 break;
740 case AINPUT_EVENT_TYPE_MOTION:
741 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800742 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700743 return;
744 }
745 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800746 case AINPUT_EVENT_TYPE_FOCUS:
747 if (mFocusEventPool.size() < mMaxPoolSize) {
748 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
749 return;
750 }
751 break;
Jeff Brown5912f952013-07-01 19:10:31 -0700752 }
753 delete event;
754}
755
756} // namespace android