blob: 2a73dc0149d9bd08b22b76d554d06ecfed462f34 [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
Jeff Brown5912f952013-07-01 19:10:31 -070020#include <limits.h>
21
22#include <input/Input.h>
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080023#include <input/InputDevice.h>
Michael Wright872db4f2014-04-22 15:03:51 -070024#include <input/InputEventLabels.h>
Jeff Brown5912f952013-07-01 19:10:31 -070025
Elliott Hughes6071da72015-08-12 15:27:47 -070026#ifdef __ANDROID__
Jeff Brown5912f952013-07-01 19:10:31 -070027#include <binder/Parcel.h>
Jeff Brown5912f952013-07-01 19:10:31 -070028#endif
29
30namespace android {
31
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -080032const char* motionClassificationToString(MotionClassification classification) {
33 switch (classification) {
34 case MotionClassification::NONE:
35 return "NONE";
36 case MotionClassification::AMBIGUOUS_GESTURE:
37 return "AMBIGUOUS_GESTURE";
38 case MotionClassification::DEEP_PRESS:
39 return "DEEP_PRESS";
40 }
41}
42
Jeff Brown5912f952013-07-01 19:10:31 -070043// --- InputEvent ---
44
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080045const char* inputEventTypeToString(int32_t type) {
46 switch (type) {
47 case AINPUT_EVENT_TYPE_KEY: {
48 return "KEY";
49 }
50 case AINPUT_EVENT_TYPE_MOTION: {
51 return "MOTION";
52 }
53 case AINPUT_EVENT_TYPE_FOCUS: {
54 return "FOCUS";
55 }
56 }
57 return "UNKNOWN";
58}
59
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -080060VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
61 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
62 event.getSource(), event.getDisplayId()},
63 event.getAction(),
64 event.getDownTime(),
65 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
66 event.getKeyCode(),
67 event.getScanCode(),
68 event.getMetaState(),
69 event.getRepeatCount()};
70}
71
72VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
73 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
74 event.getSource(), event.getDisplayId()},
75 event.getRawX(0),
76 event.getRawY(0),
77 event.getActionMasked(),
78 event.getDownTime(),
79 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
80 event.getMetaState(),
81 event.getButtonState()};
82}
83
Siarhei Vishniakou3826d472020-01-27 10:44:40 -060084void InputEvent::initialize(int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -060085 std::array<uint8_t, 32> hmac) {
Jeff Brown5912f952013-07-01 19:10:31 -070086 mDeviceId = deviceId;
87 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010088 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -060089 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -070090}
91
92void InputEvent::initialize(const InputEvent& from) {
93 mDeviceId = from.mDeviceId;
94 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010095 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -060096 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -070097}
98
99// --- KeyEvent ---
100
Michael Wright872db4f2014-04-22 15:03:51 -0700101const char* KeyEvent::getLabel(int32_t keyCode) {
102 return getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700103}
104
Michael Wright872db4f2014-04-22 15:03:51 -0700105int32_t KeyEvent::getKeyCodeFromLabel(const char* label) {
106 return getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700107}
108
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600109void KeyEvent::initialize(int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600110 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
111 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
112 nsecs_t downTime, nsecs_t eventTime) {
113 InputEvent::initialize(deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700114 mAction = action;
115 mFlags = flags;
116 mKeyCode = keyCode;
117 mScanCode = scanCode;
118 mMetaState = metaState;
119 mRepeatCount = repeatCount;
120 mDownTime = downTime;
121 mEventTime = eventTime;
122}
123
124void KeyEvent::initialize(const KeyEvent& from) {
125 InputEvent::initialize(from);
126 mAction = from.mAction;
127 mFlags = from.mFlags;
128 mKeyCode = from.mKeyCode;
129 mScanCode = from.mScanCode;
130 mMetaState = from.mMetaState;
131 mRepeatCount = from.mRepeatCount;
132 mDownTime = from.mDownTime;
133 mEventTime = from.mEventTime;
134}
135
136
137// --- PointerCoords ---
138
139float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700140 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700141 return 0;
142 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700143 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700144}
145
146status_t PointerCoords::setAxisValue(int32_t axis, float value) {
147 if (axis < 0 || axis > 63) {
148 return NAME_NOT_FOUND;
149 }
150
Michael Wright38dcdff2014-03-19 12:06:10 -0700151 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
152 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700153 if (value == 0) {
154 return OK; // axes with value 0 do not need to be stored
155 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700156
157 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700158 if (count >= MAX_AXES) {
159 tooManyAxes(axis);
160 return NO_MEMORY;
161 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700162 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700163 for (uint32_t i = count; i > index; i--) {
164 values[i] = values[i - 1];
165 }
166 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700167
Jeff Brown5912f952013-07-01 19:10:31 -0700168 values[index] = value;
169 return OK;
170}
171
172static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
173 float value = c.getAxisValue(axis);
174 if (value != 0) {
175 c.setAxisValue(axis, value * scaleFactor);
176 }
177}
178
Robert Carre07e1032018-11-26 12:55:53 -0800179void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700180 // No need to scale pressure or size since they are normalized.
181 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800182
183 // If there is a global scale factor, it is included in the windowX/YScale
184 // so we don't need to apply it twice to the X/Y axes.
185 // However we don't want to apply any windowXYScale not included in the global scale
186 // to the TOUCH_MAJOR/MINOR coordinates.
187 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
188 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
189 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
190 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
191 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
192 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
193}
194
195void PointerCoords::scale(float globalScaleFactor) {
196 scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700197}
198
Jeff Brownf086ddb2014-02-11 14:28:48 -0800199void PointerCoords::applyOffset(float xOffset, float yOffset) {
200 setAxisValue(AMOTION_EVENT_AXIS_X, getX() + xOffset);
201 setAxisValue(AMOTION_EVENT_AXIS_Y, getY() + yOffset);
202}
203
Elliott Hughes6071da72015-08-12 15:27:47 -0700204#ifdef __ANDROID__
Jeff Brown5912f952013-07-01 19:10:31 -0700205status_t PointerCoords::readFromParcel(Parcel* parcel) {
206 bits = parcel->readInt64();
207
Michael Wright38dcdff2014-03-19 12:06:10 -0700208 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700209 if (count > MAX_AXES) {
210 return BAD_VALUE;
211 }
212
213 for (uint32_t i = 0; i < count; i++) {
214 values[i] = parcel->readFloat();
215 }
216 return OK;
217}
218
219status_t PointerCoords::writeToParcel(Parcel* parcel) const {
220 parcel->writeInt64(bits);
221
Michael Wright38dcdff2014-03-19 12:06:10 -0700222 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700223 for (uint32_t i = 0; i < count; i++) {
224 parcel->writeFloat(values[i]);
225 }
226 return OK;
227}
228#endif
229
230void PointerCoords::tooManyAxes(int axis) {
231 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
232 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
233}
234
235bool PointerCoords::operator==(const PointerCoords& other) const {
236 if (bits != other.bits) {
237 return false;
238 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700239 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700240 for (uint32_t i = 0; i < count; i++) {
241 if (values[i] != other.values[i]) {
242 return false;
243 }
244 }
245 return true;
246}
247
248void PointerCoords::copyFrom(const PointerCoords& other) {
249 bits = other.bits;
Michael Wright38dcdff2014-03-19 12:06:10 -0700250 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700251 for (uint32_t i = 0; i < count; i++) {
252 values[i] = other.values[i];
253 }
254}
255
256
257// --- PointerProperties ---
258
259bool PointerProperties::operator==(const PointerProperties& other) const {
260 return id == other.id
261 && toolType == other.toolType;
262}
263
264void PointerProperties::copyFrom(const PointerProperties& other) {
265 id = other.id;
266 toolType = other.toolType;
267}
268
269
270// --- MotionEvent ---
271
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600272void MotionEvent::initialize(int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600273 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
274 int32_t flags, int32_t edgeFlags, int32_t metaState,
275 int32_t buttonState, MotionClassification classification, float xScale,
276 float yScale, float xOffset, float yOffset, float xPrecision,
277 float yPrecision, float rawXCursorPosition, float rawYCursorPosition,
278 nsecs_t downTime, nsecs_t eventTime, size_t pointerCount,
279 const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700280 const PointerCoords* pointerCoords) {
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600281 InputEvent::initialize(deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700282 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100283 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700284 mFlags = flags;
285 mEdgeFlags = edgeFlags;
286 mMetaState = metaState;
287 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800288 mClassification = classification;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600289 mXScale = xScale;
290 mYScale = yScale;
Jeff Brown5912f952013-07-01 19:10:31 -0700291 mXOffset = xOffset;
292 mYOffset = yOffset;
293 mXPrecision = xPrecision;
294 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700295 mRawXCursorPosition = rawXCursorPosition;
296 mRawYCursorPosition = rawYCursorPosition;
Jeff Brown5912f952013-07-01 19:10:31 -0700297 mDownTime = downTime;
298 mPointerProperties.clear();
299 mPointerProperties.appendArray(pointerProperties, pointerCount);
300 mSampleEventTimes.clear();
301 mSamplePointerCoords.clear();
302 addSample(eventTime, pointerCoords);
303}
304
305void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600306 InputEvent::initialize(other->mDeviceId, other->mSource, other->mDisplayId, other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700307 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100308 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700309 mFlags = other->mFlags;
310 mEdgeFlags = other->mEdgeFlags;
311 mMetaState = other->mMetaState;
312 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800313 mClassification = other->mClassification;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600314 mXScale = other->mXScale;
315 mYScale = other->mYScale;
Jeff Brown5912f952013-07-01 19:10:31 -0700316 mXOffset = other->mXOffset;
317 mYOffset = other->mYOffset;
318 mXPrecision = other->mXPrecision;
319 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700320 mRawXCursorPosition = other->mRawXCursorPosition;
321 mRawYCursorPosition = other->mRawYCursorPosition;
Jeff Brown5912f952013-07-01 19:10:31 -0700322 mDownTime = other->mDownTime;
323 mPointerProperties = other->mPointerProperties;
324
325 if (keepHistory) {
326 mSampleEventTimes = other->mSampleEventTimes;
327 mSamplePointerCoords = other->mSamplePointerCoords;
328 } else {
329 mSampleEventTimes.clear();
330 mSampleEventTimes.push(other->getEventTime());
331 mSamplePointerCoords.clear();
332 size_t pointerCount = other->getPointerCount();
333 size_t historySize = other->getHistorySize();
334 mSamplePointerCoords.appendArray(other->mSamplePointerCoords.array()
335 + (historySize * pointerCount), pointerCount);
336 }
337}
338
339void MotionEvent::addSample(
340 int64_t eventTime,
341 const PointerCoords* pointerCoords) {
342 mSampleEventTimes.push(eventTime);
343 mSamplePointerCoords.appendArray(pointerCoords, getPointerCount());
344}
345
Garfield Tan00f511d2019-06-12 16:55:40 -0700346float MotionEvent::getXCursorPosition() const {
347 const float rawX = getRawXCursorPosition();
chaviw82357092020-01-28 13:13:06 -0800348 return rawX * mXScale + mXOffset;
Garfield Tan00f511d2019-06-12 16:55:40 -0700349}
350
351float MotionEvent::getYCursorPosition() const {
352 const float rawY = getRawYCursorPosition();
chaviw82357092020-01-28 13:13:06 -0800353 return rawY * mYScale + mYOffset;
Garfield Tan00f511d2019-06-12 16:55:40 -0700354}
355
Garfield Tan937bb832019-07-25 17:48:31 -0700356void MotionEvent::setCursorPosition(float x, float y) {
chaviw82357092020-01-28 13:13:06 -0800357 mRawXCursorPosition = (x - mXOffset) / mXScale;
358 mRawYCursorPosition = (y - mYOffset) / mYScale;
Garfield Tan937bb832019-07-25 17:48:31 -0700359}
360
Jeff Brown5912f952013-07-01 19:10:31 -0700361const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
362 return &mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
363}
364
365float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
366 return getRawPointerCoords(pointerIndex)->getAxisValue(axis);
367}
368
369float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
370 float value = getRawPointerCoords(pointerIndex)->getAxisValue(axis);
371 switch (axis) {
372 case AMOTION_EVENT_AXIS_X:
chaviw82357092020-01-28 13:13:06 -0800373 return value * mXScale + mXOffset;
Jeff Brown5912f952013-07-01 19:10:31 -0700374 case AMOTION_EVENT_AXIS_Y:
chaviw82357092020-01-28 13:13:06 -0800375 return value * mYScale + mYOffset;
Jeff Brown5912f952013-07-01 19:10:31 -0700376 }
377 return value;
378}
379
380const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
381 size_t pointerIndex, size_t historicalIndex) const {
382 return &mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
383}
384
385float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
386 size_t historicalIndex) const {
387 return getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis);
388}
389
390float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
391 size_t historicalIndex) const {
392 float value = getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis);
393 switch (axis) {
394 case AMOTION_EVENT_AXIS_X:
chaviw82357092020-01-28 13:13:06 -0800395 return value * mXScale + mXOffset;
Jeff Brown5912f952013-07-01 19:10:31 -0700396 case AMOTION_EVENT_AXIS_Y:
chaviw82357092020-01-28 13:13:06 -0800397 return value * mYScale + mYOffset;
Jeff Brown5912f952013-07-01 19:10:31 -0700398 }
399 return value;
400}
401
402ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
403 size_t pointerCount = mPointerProperties.size();
404 for (size_t i = 0; i < pointerCount; i++) {
405 if (mPointerProperties.itemAt(i).id == pointerId) {
406 return i;
407 }
408 }
409 return -1;
410}
411
412void MotionEvent::offsetLocation(float xOffset, float yOffset) {
413 mXOffset += xOffset;
414 mYOffset += yOffset;
415}
416
Robert Carre07e1032018-11-26 12:55:53 -0800417void MotionEvent::scale(float globalScaleFactor) {
418 mXOffset *= globalScaleFactor;
419 mYOffset *= globalScaleFactor;
420 mXPrecision *= globalScaleFactor;
421 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700422
423 size_t numSamples = mSamplePointerCoords.size();
424 for (size_t i = 0; i < numSamples; i++) {
Robert Carre07e1032018-11-26 12:55:53 -0800425 mSamplePointerCoords.editItemAt(i).scale(globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700426 }
427}
428
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700429static void transformPoint(const float matrix[9], float x, float y, float *outX, float *outY) {
430 // Apply perspective transform like Skia.
431 float newX = matrix[0] * x + matrix[1] * y + matrix[2];
432 float newY = matrix[3] * x + matrix[4] * y + matrix[5];
433 float newZ = matrix[6] * x + matrix[7] * y + matrix[8];
434 if (newZ) {
435 newZ = 1.0f / newZ;
436 }
437 *outX = newX * newZ;
438 *outY = newY * newZ;
439}
440
441static float transformAngle(const float matrix[9], float angleRadians,
442 float originX, float originY) {
Jeff Brown5912f952013-07-01 19:10:31 -0700443 // Construct and transform a vector oriented at the specified clockwise angle from vertical.
444 // Coordinate system: down is increasing Y, right is increasing X.
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700445 float x = sinf(angleRadians);
446 float y = -cosf(angleRadians);
447 transformPoint(matrix, x, y, &x, &y);
448 x -= originX;
449 y -= originY;
Jeff Brown5912f952013-07-01 19:10:31 -0700450
451 // Derive the transformed vector's clockwise angle from vertical.
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700452 float result = atan2f(x, -y);
Jeff Brown5912f952013-07-01 19:10:31 -0700453 if (result < - M_PI_2) {
454 result += M_PI;
455 } else if (result > M_PI_2) {
456 result -= M_PI;
457 }
458 return result;
459}
460
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700461void MotionEvent::transform(const float matrix[9]) {
Jeff Brown5912f952013-07-01 19:10:31 -0700462 // The tricky part of this implementation is to preserve the value of
463 // rawX and rawY. So we apply the transformation to the first point
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700464 // then derive an appropriate new X/Y offset that will preserve rawX
465 // and rawY for that point.
466 float oldXOffset = mXOffset;
467 float oldYOffset = mYOffset;
468 float newX, newY;
chaviw82357092020-01-28 13:13:06 -0800469 float scaledRawX = getRawX(0) * mXScale;
470 float scaledRawY = getRawY(0) * mYScale;
471 transformPoint(matrix, scaledRawX + oldXOffset, scaledRawY + oldYOffset, &newX, &newY);
472 mXOffset = newX - scaledRawX;
473 mYOffset = newY - scaledRawY;
Jeff Brown5912f952013-07-01 19:10:31 -0700474
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700475 // Determine how the origin is transformed by the matrix so that we
476 // can transform orientation vectors.
477 float originX, originY;
478 transformPoint(matrix, 0, 0, &originX, &originY);
Jeff Brown5912f952013-07-01 19:10:31 -0700479
Garfield Tan00f511d2019-06-12 16:55:40 -0700480 // Apply the transformation to cursor position.
Garfield Tan937bb832019-07-25 17:48:31 -0700481 if (isValidCursorPosition(mRawXCursorPosition, mRawYCursorPosition)) {
chaviw82357092020-01-28 13:13:06 -0800482 float x = mRawXCursorPosition * mXScale + oldXOffset;
483 float y = mRawYCursorPosition * mYScale + oldYOffset;
Garfield Tan00f511d2019-06-12 16:55:40 -0700484 transformPoint(matrix, x, y, &x, &y);
chaviw82357092020-01-28 13:13:06 -0800485 mRawXCursorPosition = (x - mXOffset) / mXScale;
486 mRawYCursorPosition = (y - mYOffset) / mYScale;
Garfield Tan00f511d2019-06-12 16:55:40 -0700487 }
488
Jeff Brown5912f952013-07-01 19:10:31 -0700489 // Apply the transformation to all samples.
490 size_t numSamples = mSamplePointerCoords.size();
491 for (size_t i = 0; i < numSamples; i++) {
492 PointerCoords& c = mSamplePointerCoords.editItemAt(i);
chaviw82357092020-01-28 13:13:06 -0800493 float x = c.getAxisValue(AMOTION_EVENT_AXIS_X) * mXScale + oldXOffset;
494 float y = c.getAxisValue(AMOTION_EVENT_AXIS_Y) * mYScale + oldYOffset;
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700495 transformPoint(matrix, x, y, &x, &y);
chaviw82357092020-01-28 13:13:06 -0800496 c.setAxisValue(AMOTION_EVENT_AXIS_X, (x - mXOffset) / mXScale);
497 c.setAxisValue(AMOTION_EVENT_AXIS_Y, (y - mYOffset) / mYScale);
Jeff Brown5912f952013-07-01 19:10:31 -0700498
499 float orientation = c.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
Jeff Brown5a2f68e2013-07-15 17:28:19 -0700500 c.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
501 transformAngle(matrix, orientation, originX, originY));
Jeff Brown5912f952013-07-01 19:10:31 -0700502 }
503}
504
Elliott Hughes6071da72015-08-12 15:27:47 -0700505#ifdef __ANDROID__
Jeff Brown5912f952013-07-01 19:10:31 -0700506status_t MotionEvent::readFromParcel(Parcel* parcel) {
507 size_t pointerCount = parcel->readInt32();
508 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800509 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
510 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700511 return BAD_VALUE;
512 }
513
514 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600515 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800516 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600517 std::vector<uint8_t> hmac;
518 status_t result = parcel->readByteVector(&hmac);
519 if (result != OK || hmac.size() != 32) {
520 return BAD_VALUE;
521 }
522 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700523 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100524 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700525 mFlags = parcel->readInt32();
526 mEdgeFlags = parcel->readInt32();
527 mMetaState = parcel->readInt32();
528 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800529 mClassification = static_cast<MotionClassification>(parcel->readByte());
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600530 mXScale = parcel->readFloat();
531 mYScale = parcel->readFloat();
Jeff Brown5912f952013-07-01 19:10:31 -0700532 mXOffset = parcel->readFloat();
533 mYOffset = parcel->readFloat();
534 mXPrecision = parcel->readFloat();
535 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700536 mRawXCursorPosition = parcel->readFloat();
537 mRawYCursorPosition = parcel->readFloat();
Jeff Brown5912f952013-07-01 19:10:31 -0700538 mDownTime = parcel->readInt64();
539
540 mPointerProperties.clear();
541 mPointerProperties.setCapacity(pointerCount);
542 mSampleEventTimes.clear();
543 mSampleEventTimes.setCapacity(sampleCount);
544 mSamplePointerCoords.clear();
545 mSamplePointerCoords.setCapacity(sampleCount * pointerCount);
546
547 for (size_t i = 0; i < pointerCount; i++) {
548 mPointerProperties.push();
549 PointerProperties& properties = mPointerProperties.editTop();
550 properties.id = parcel->readInt32();
551 properties.toolType = parcel->readInt32();
552 }
553
Dan Austinc94fc452015-09-22 14:22:41 -0700554 while (sampleCount > 0) {
555 sampleCount--;
Jeff Brown5912f952013-07-01 19:10:31 -0700556 mSampleEventTimes.push(parcel->readInt64());
557 for (size_t i = 0; i < pointerCount; i++) {
558 mSamplePointerCoords.push();
559 status_t status = mSamplePointerCoords.editTop().readFromParcel(parcel);
560 if (status) {
561 return status;
562 }
563 }
564 }
565 return OK;
566}
567
568status_t MotionEvent::writeToParcel(Parcel* parcel) const {
569 size_t pointerCount = mPointerProperties.size();
570 size_t sampleCount = mSampleEventTimes.size();
571
572 parcel->writeInt32(pointerCount);
573 parcel->writeInt32(sampleCount);
574
575 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600576 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800577 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600578 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
579 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700580 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100581 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700582 parcel->writeInt32(mFlags);
583 parcel->writeInt32(mEdgeFlags);
584 parcel->writeInt32(mMetaState);
585 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800586 parcel->writeByte(static_cast<int8_t>(mClassification));
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600587 parcel->writeFloat(mXScale);
588 parcel->writeFloat(mYScale);
Jeff Brown5912f952013-07-01 19:10:31 -0700589 parcel->writeFloat(mXOffset);
590 parcel->writeFloat(mYOffset);
591 parcel->writeFloat(mXPrecision);
592 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700593 parcel->writeFloat(mRawXCursorPosition);
594 parcel->writeFloat(mRawYCursorPosition);
Jeff Brown5912f952013-07-01 19:10:31 -0700595 parcel->writeInt64(mDownTime);
596
597 for (size_t i = 0; i < pointerCount; i++) {
598 const PointerProperties& properties = mPointerProperties.itemAt(i);
599 parcel->writeInt32(properties.id);
600 parcel->writeInt32(properties.toolType);
601 }
602
603 const PointerCoords* pc = mSamplePointerCoords.array();
604 for (size_t h = 0; h < sampleCount; h++) {
605 parcel->writeInt64(mSampleEventTimes.itemAt(h));
606 for (size_t i = 0; i < pointerCount; i++) {
607 status_t status = (pc++)->writeToParcel(parcel);
608 if (status) {
609 return status;
610 }
611 }
612 }
613 return OK;
614}
615#endif
616
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600617bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Jeff Brown5912f952013-07-01 19:10:31 -0700618 if (source & AINPUT_SOURCE_CLASS_POINTER) {
619 // Specifically excludes HOVER_MOVE and SCROLL.
620 switch (action & AMOTION_EVENT_ACTION_MASK) {
621 case AMOTION_EVENT_ACTION_DOWN:
622 case AMOTION_EVENT_ACTION_MOVE:
623 case AMOTION_EVENT_ACTION_UP:
624 case AMOTION_EVENT_ACTION_POINTER_DOWN:
625 case AMOTION_EVENT_ACTION_POINTER_UP:
626 case AMOTION_EVENT_ACTION_CANCEL:
627 case AMOTION_EVENT_ACTION_OUTSIDE:
628 return true;
629 }
630 }
631 return false;
632}
633
Michael Wright872db4f2014-04-22 15:03:51 -0700634const char* MotionEvent::getLabel(int32_t axis) {
635 return getAxisLabel(axis);
636}
637
638int32_t MotionEvent::getAxisFromLabel(const char* label) {
639 return getAxisByLabel(label);
640}
641
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800642// --- FocusEvent ---
643
644void FocusEvent::initialize(bool hasFocus, bool inTouchMode) {
645 InputEvent::initialize(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600646 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800647 mHasFocus = hasFocus;
648 mInTouchMode = inTouchMode;
649}
650
651void FocusEvent::initialize(const FocusEvent& from) {
652 InputEvent::initialize(from);
653 mHasFocus = from.mHasFocus;
654 mInTouchMode = from.mInTouchMode;
655}
Jeff Brown5912f952013-07-01 19:10:31 -0700656
657// --- PooledInputEventFactory ---
658
659PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
660 mMaxPoolSize(maxPoolSize) {
661}
662
663PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -0700664}
665
666KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800667 if (mKeyEventPool.empty()) {
668 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700669 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800670 KeyEvent* event = mKeyEventPool.front().release();
671 mKeyEventPool.pop();
672 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700673}
674
675MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800676 if (mMotionEventPool.empty()) {
677 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700678 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800679 MotionEvent* event = mMotionEventPool.front().release();
680 mMotionEventPool.pop();
681 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700682}
683
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800684FocusEvent* PooledInputEventFactory::createFocusEvent() {
685 if (mFocusEventPool.empty()) {
686 return new FocusEvent();
687 }
688 FocusEvent* event = mFocusEventPool.front().release();
689 mFocusEventPool.pop();
690 return event;
691}
692
Jeff Brown5912f952013-07-01 19:10:31 -0700693void PooledInputEventFactory::recycle(InputEvent* event) {
694 switch (event->getType()) {
695 case AINPUT_EVENT_TYPE_KEY:
696 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800697 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700698 return;
699 }
700 break;
701 case AINPUT_EVENT_TYPE_MOTION:
702 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800703 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700704 return;
705 }
706 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800707 case AINPUT_EVENT_TYPE_FOCUS:
708 if (mFocusEventPool.size() < mMaxPoolSize) {
709 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
710 return;
711 }
712 break;
Jeff Brown5912f952013-07-01 19:10:31 -0700713 }
714 delete event;
715}
716
717} // namespace android