blob: 2860aa303fa5e00dc41a35f9d483576ed6267d03 [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
chaviw09c8d2d2020-08-24 15:48:26 -070020#include <attestation/HmacKeyManager.h>
Garfield Tan84b087e2020-01-23 10:49:05 -080021#include <cutils/compiler.h>
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050022#include <inttypes.h>
Jeff Brown5912f952013-07-01 19:10:31 -070023#include <limits.h>
Garfield Tan84b087e2020-01-23 10:49:05 -080024#include <string.h>
Jeff Brown5912f952013-07-01 19:10:31 -070025
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050026#include <android-base/stringprintf.h>
chaviw3277faf2021-05-19 16:45:23 -050027#include <gui/constants.h>
Jeff Brown5912f952013-07-01 19:10:31 -070028#include <input/Input.h>
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080029#include <input/InputDevice.h>
Michael Wright872db4f2014-04-22 15:03:51 -070030#include <input/InputEventLabels.h>
Jeff Brown5912f952013-07-01 19:10:31 -070031
Brett Chabotfaa986c2020-11-04 17:39:36 -080032#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -070033#include <binder/Parcel.h>
Brett Chabotfaa986c2020-11-04 17:39:36 -080034#endif
Brett Chabot58208522020-09-09 13:55:24 -070035#ifdef __ANDROID__
Garfield Tan84b087e2020-01-23 10:49:05 -080036#include <sys/random.h>
Jeff Brown5912f952013-07-01 19:10:31 -070037#endif
38
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050039using android::base::StringPrintf;
40
Jeff Brown5912f952013-07-01 19:10:31 -070041namespace android {
42
Prabir Pradhan6b384612021-05-14 16:56:25 -070043namespace {
44
45float transformAngle(const ui::Transform& transform, float angleRadians) {
46 // Construct and transform a vector oriented at the specified clockwise angle from vertical.
47 // Coordinate system: down is increasing Y, right is increasing X.
48 float x = sinf(angleRadians);
49 float y = -cosf(angleRadians);
50 vec2 transformedPoint = transform.transform(x, y);
51
52 // Determine how the origin is transformed by the matrix so that we
53 // can transform orientation vectors.
54 const vec2 origin = transform.transform(0, 0);
55
56 transformedPoint.x -= origin.x;
57 transformedPoint.y -= origin.y;
58
59 // Derive the transformed vector's clockwise angle from vertical.
60 float result = atan2f(transformedPoint.x, -transformedPoint.y);
61 if (result < -M_PI_2) {
62 result += M_PI;
63 } else if (result > M_PI_2) {
64 result -= M_PI;
65 }
66 return result;
67}
68
Evan Rosky09576692021-07-01 12:22:09 -070069// Rotates the given point to the specified orientation. If the display width and height are
Prabir Pradhan6b384612021-05-14 16:56:25 -070070// provided, the point is rotated in the screen space. Otherwise, the point is rotated about the
71// origin. This helper is used to avoid the extra overhead of creating new Transforms.
Evan Rosky09576692021-07-01 12:22:09 -070072vec2 rotatePoint(uint32_t orientation, float x, float y, int32_t displayWidth = 0,
Prabir Pradhan6b384612021-05-14 16:56:25 -070073 int32_t displayHeight = 0) {
Prabir Pradhan6b384612021-05-14 16:56:25 -070074 if (orientation == ui::Transform::ROT_0) {
75 return {x, y};
76 }
77
78 vec2 xy(x, y);
79 if (orientation == ui::Transform::ROT_90) {
80 xy.x = displayHeight - y;
81 xy.y = x;
82 } else if (orientation == ui::Transform::ROT_180) {
83 xy.x = displayWidth - x;
84 xy.y = displayHeight - y;
85 } else if (orientation == ui::Transform::ROT_270) {
86 xy.x = y;
87 xy.y = displayWidth - x;
88 }
89 return xy;
90}
91
Prabir Pradhan9f388812021-05-13 16:54:53 -070092vec2 applyTransformWithoutTranslation(const ui::Transform& transform, float x, float y) {
93 const vec2 transformedXy = transform.transform(x, y);
94 const vec2 transformedOrigin = transform.transform(0, 0);
95 return transformedXy - transformedOrigin;
96}
97
98bool shouldDisregardWindowTranslation(uint32_t source) {
99 // Pointer events are the only type of events that refer to absolute coordinates on the display,
100 // so we should apply the entire window transform. For other types of events, we should make
101 // sure to not apply the window translation/offset.
102 return (source & AINPUT_SOURCE_CLASS_POINTER) == 0;
103}
104
Prabir Pradhan6b384612021-05-14 16:56:25 -0700105} // namespace
106
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800107const char* motionClassificationToString(MotionClassification classification) {
108 switch (classification) {
109 case MotionClassification::NONE:
110 return "NONE";
111 case MotionClassification::AMBIGUOUS_GESTURE:
112 return "AMBIGUOUS_GESTURE";
113 case MotionClassification::DEEP_PRESS:
114 return "DEEP_PRESS";
115 }
116}
117
Garfield Tan84b087e2020-01-23 10:49:05 -0800118// --- IdGenerator ---
119IdGenerator::IdGenerator(Source source) : mSource(source) {}
120
121int32_t IdGenerator::nextId() const {
122 constexpr uint32_t SEQUENCE_NUMBER_MASK = ~SOURCE_MASK;
123 int32_t id = 0;
124
125// Avoid building against syscall getrandom(2) on host, which will fail build on Mac. Host doesn't
126// use sequence number so just always return mSource.
127#ifdef __ANDROID__
128 constexpr size_t BUF_LEN = sizeof(id);
129 size_t totalBytes = 0;
130 while (totalBytes < BUF_LEN) {
131 ssize_t bytes = TEMP_FAILURE_RETRY(getrandom(&id, BUF_LEN, GRND_NONBLOCK));
132 if (CC_UNLIKELY(bytes < 0)) {
133 ALOGW("Failed to fill in random number for sequence number: %s.", strerror(errno));
134 id = 0;
135 break;
136 }
137 totalBytes += bytes;
138 }
139#endif // __ANDROID__
140
141 return (id & SEQUENCE_NUMBER_MASK) | static_cast<int32_t>(mSource);
142}
143
Jeff Brown5912f952013-07-01 19:10:31 -0700144// --- InputEvent ---
145
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800146const char* inputEventTypeToString(int32_t type) {
147 switch (type) {
148 case AINPUT_EVENT_TYPE_KEY: {
149 return "KEY";
150 }
151 case AINPUT_EVENT_TYPE_MOTION: {
152 return "MOTION";
153 }
154 case AINPUT_EVENT_TYPE_FOCUS: {
155 return "FOCUS";
156 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800157 case AINPUT_EVENT_TYPE_CAPTURE: {
158 return "CAPTURE";
159 }
arthurhung7632c332020-12-30 16:58:01 +0800160 case AINPUT_EVENT_TYPE_DRAG: {
161 return "DRAG";
162 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800163 }
164 return "UNKNOWN";
165}
166
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800167VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
168 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
169 event.getSource(), event.getDisplayId()},
170 event.getAction(),
171 event.getDownTime(),
172 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
173 event.getKeyCode(),
174 event.getScanCode(),
175 event.getMetaState(),
176 event.getRepeatCount()};
177}
178
179VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
180 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
181 event.getSource(), event.getDisplayId()},
182 event.getRawX(0),
183 event.getRawY(0),
184 event.getActionMasked(),
185 event.getDownTime(),
186 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
187 event.getMetaState(),
188 event.getButtonState()};
189}
190
Garfield Tan4cc839f2020-01-24 11:26:14 -0800191void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600192 std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800193 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700194 mDeviceId = deviceId;
195 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100196 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600197 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700198}
199
200void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800201 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700202 mDeviceId = from.mDeviceId;
203 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100204 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600205 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700206}
207
Garfield Tan4cc839f2020-01-24 11:26:14 -0800208int32_t InputEvent::nextId() {
209 static IdGenerator idGen(IdGenerator::Source::OTHER);
210 return idGen.nextId();
211}
212
Jeff Brown5912f952013-07-01 19:10:31 -0700213// --- KeyEvent ---
214
Michael Wright872db4f2014-04-22 15:03:51 -0700215const char* KeyEvent::getLabel(int32_t keyCode) {
Chris Ye4958d062020-08-20 13:21:10 -0700216 return InputEventLookup::getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700217}
218
Michael Wright872db4f2014-04-22 15:03:51 -0700219int32_t KeyEvent::getKeyCodeFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700220 return InputEventLookup::getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700221}
222
Garfield Tan4cc839f2020-01-24 11:26:14 -0800223void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600224 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
225 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
226 nsecs_t downTime, nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800227 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700228 mAction = action;
229 mFlags = flags;
230 mKeyCode = keyCode;
231 mScanCode = scanCode;
232 mMetaState = metaState;
233 mRepeatCount = repeatCount;
234 mDownTime = downTime;
235 mEventTime = eventTime;
236}
237
238void KeyEvent::initialize(const KeyEvent& from) {
239 InputEvent::initialize(from);
240 mAction = from.mAction;
241 mFlags = from.mFlags;
242 mKeyCode = from.mKeyCode;
243 mScanCode = from.mScanCode;
244 mMetaState = from.mMetaState;
245 mRepeatCount = from.mRepeatCount;
246 mDownTime = from.mDownTime;
247 mEventTime = from.mEventTime;
248}
249
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700250const char* KeyEvent::actionToString(int32_t action) {
251 // Convert KeyEvent action to string
252 switch (action) {
253 case AKEY_EVENT_ACTION_DOWN:
254 return "DOWN";
255 case AKEY_EVENT_ACTION_UP:
256 return "UP";
257 case AKEY_EVENT_ACTION_MULTIPLE:
258 return "MULTIPLE";
259 }
260 return "UNKNOWN";
261}
Jeff Brown5912f952013-07-01 19:10:31 -0700262
263// --- PointerCoords ---
264
265float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700266 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700267 return 0;
268 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700269 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700270}
271
272status_t PointerCoords::setAxisValue(int32_t axis, float value) {
273 if (axis < 0 || axis > 63) {
274 return NAME_NOT_FOUND;
275 }
276
Michael Wright38dcdff2014-03-19 12:06:10 -0700277 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
278 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700279 if (value == 0) {
280 return OK; // axes with value 0 do not need to be stored
281 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700282
283 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700284 if (count >= MAX_AXES) {
285 tooManyAxes(axis);
286 return NO_MEMORY;
287 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700288 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700289 for (uint32_t i = count; i > index; i--) {
290 values[i] = values[i - 1];
291 }
292 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700293
Jeff Brown5912f952013-07-01 19:10:31 -0700294 values[index] = value;
295 return OK;
296}
297
298static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
299 float value = c.getAxisValue(axis);
300 if (value != 0) {
301 c.setAxisValue(axis, value * scaleFactor);
302 }
303}
304
Robert Carre07e1032018-11-26 12:55:53 -0800305void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700306 // No need to scale pressure or size since they are normalized.
307 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800308
309 // If there is a global scale factor, it is included in the windowX/YScale
310 // so we don't need to apply it twice to the X/Y axes.
311 // However we don't want to apply any windowXYScale not included in the global scale
312 // to the TOUCH_MAJOR/MINOR coordinates.
313 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
314 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
315 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
316 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
317 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
318 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
Prabir Pradhanc6523582021-05-14 18:02:55 -0700319 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_X, windowXScale);
320 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_Y, windowYScale);
Robert Carre07e1032018-11-26 12:55:53 -0800321}
322
323void PointerCoords::scale(float globalScaleFactor) {
324 scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700325}
326
Jeff Brownf086ddb2014-02-11 14:28:48 -0800327void PointerCoords::applyOffset(float xOffset, float yOffset) {
328 setAxisValue(AMOTION_EVENT_AXIS_X, getX() + xOffset);
329 setAxisValue(AMOTION_EVENT_AXIS_Y, getY() + yOffset);
330}
331
Brett Chabotfaa986c2020-11-04 17:39:36 -0800332#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700333status_t PointerCoords::readFromParcel(Parcel* parcel) {
334 bits = parcel->readInt64();
335
Michael Wright38dcdff2014-03-19 12:06:10 -0700336 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700337 if (count > MAX_AXES) {
338 return BAD_VALUE;
339 }
340
341 for (uint32_t i = 0; i < count; i++) {
342 values[i] = parcel->readFloat();
343 }
344 return OK;
345}
346
347status_t PointerCoords::writeToParcel(Parcel* parcel) const {
348 parcel->writeInt64(bits);
349
Michael Wright38dcdff2014-03-19 12:06:10 -0700350 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700351 for (uint32_t i = 0; i < count; i++) {
352 parcel->writeFloat(values[i]);
353 }
354 return OK;
355}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800356#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700357
358void PointerCoords::tooManyAxes(int axis) {
359 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
360 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
361}
362
363bool PointerCoords::operator==(const PointerCoords& other) const {
364 if (bits != other.bits) {
365 return false;
366 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700367 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700368 for (uint32_t i = 0; i < count; i++) {
369 if (values[i] != other.values[i]) {
370 return false;
371 }
372 }
373 return true;
374}
375
376void PointerCoords::copyFrom(const PointerCoords& other) {
377 bits = other.bits;
Michael Wright38dcdff2014-03-19 12:06:10 -0700378 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700379 for (uint32_t i = 0; i < count; i++) {
380 values[i] = other.values[i];
381 }
382}
383
chaviwc01e1372020-07-01 12:37:31 -0700384void PointerCoords::transform(const ui::Transform& transform) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700385 const vec2 xy = transform.transform(getXYValue());
386 setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
387 setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
388
Prabir Pradhanc6523582021-05-14 18:02:55 -0700389 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_X) ||
390 BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_Y)) {
391 const ui::Transform rotation(transform.getOrientation());
392 const vec2 relativeXy = rotation.transform(getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
393 getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
394 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
395 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
396 }
397
Prabir Pradhan6b384612021-05-14 16:56:25 -0700398 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) {
399 const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
400 setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val));
401 }
chaviwc01e1372020-07-01 12:37:31 -0700402}
Jeff Brown5912f952013-07-01 19:10:31 -0700403
404// --- PointerProperties ---
405
406bool PointerProperties::operator==(const PointerProperties& other) const {
407 return id == other.id
408 && toolType == other.toolType;
409}
410
411void PointerProperties::copyFrom(const PointerProperties& other) {
412 id = other.id;
413 toolType = other.toolType;
414}
415
416
417// --- MotionEvent ---
418
Garfield Tan4cc839f2020-01-24 11:26:14 -0800419void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600420 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
421 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700422 int32_t buttonState, MotionClassification classification,
423 const ui::Transform& transform, float xPrecision, float yPrecision,
Evan Rosky84f07f02021-04-16 10:42:42 -0700424 float rawXCursorPosition, float rawYCursorPosition,
Evan Rosky09576692021-07-01 12:22:09 -0700425 uint32_t displayOrientation, int32_t displayWidth,
426 int32_t displayHeight, nsecs_t downTime, nsecs_t eventTime,
427 size_t pointerCount, const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700428 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800429 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700430 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100431 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700432 mFlags = flags;
433 mEdgeFlags = edgeFlags;
434 mMetaState = metaState;
435 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800436 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700437 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700438 mXPrecision = xPrecision;
439 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700440 mRawXCursorPosition = rawXCursorPosition;
441 mRawYCursorPosition = rawYCursorPosition;
Evan Rosky09576692021-07-01 12:22:09 -0700442 mDisplayOrientation = displayOrientation;
Evan Rosky84f07f02021-04-16 10:42:42 -0700443 mDisplayWidth = displayWidth;
444 mDisplayHeight = displayHeight;
Jeff Brown5912f952013-07-01 19:10:31 -0700445 mDownTime = downTime;
446 mPointerProperties.clear();
447 mPointerProperties.appendArray(pointerProperties, pointerCount);
448 mSampleEventTimes.clear();
449 mSamplePointerCoords.clear();
450 addSample(eventTime, pointerCoords);
451}
452
453void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800454 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
455 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700456 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100457 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700458 mFlags = other->mFlags;
459 mEdgeFlags = other->mEdgeFlags;
460 mMetaState = other->mMetaState;
461 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800462 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700463 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700464 mXPrecision = other->mXPrecision;
465 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700466 mRawXCursorPosition = other->mRawXCursorPosition;
467 mRawYCursorPosition = other->mRawYCursorPosition;
Evan Rosky09576692021-07-01 12:22:09 -0700468 mDisplayOrientation = other->mDisplayOrientation;
Evan Rosky84f07f02021-04-16 10:42:42 -0700469 mDisplayWidth = other->mDisplayWidth;
470 mDisplayHeight = other->mDisplayHeight;
Jeff Brown5912f952013-07-01 19:10:31 -0700471 mDownTime = other->mDownTime;
472 mPointerProperties = other->mPointerProperties;
473
474 if (keepHistory) {
475 mSampleEventTimes = other->mSampleEventTimes;
476 mSamplePointerCoords = other->mSamplePointerCoords;
477 } else {
478 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500479 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700480 mSamplePointerCoords.clear();
481 size_t pointerCount = other->getPointerCount();
482 size_t historySize = other->getHistorySize();
483 mSamplePointerCoords.appendArray(other->mSamplePointerCoords.array()
484 + (historySize * pointerCount), pointerCount);
485 }
486}
487
488void MotionEvent::addSample(
489 int64_t eventTime,
490 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500491 mSampleEventTimes.push_back(eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -0700492 mSamplePointerCoords.appendArray(pointerCoords, getPointerCount());
493}
494
Garfield Tan00f511d2019-06-12 16:55:40 -0700495float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700496 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
497 return vals.x;
Garfield Tan00f511d2019-06-12 16:55:40 -0700498}
499
500float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700501 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
502 return vals.y;
Garfield Tan00f511d2019-06-12 16:55:40 -0700503}
504
Garfield Tan937bb832019-07-25 17:48:31 -0700505void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700506 ui::Transform inverse = mTransform.inverse();
507 vec2 vals = inverse.transform(x, y);
508 mRawXCursorPosition = vals.x;
509 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700510}
511
Jeff Brown5912f952013-07-01 19:10:31 -0700512const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
513 return &mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
514}
515
516float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
Evan Rosky84f07f02021-04-16 10:42:42 -0700517 return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700518}
519
520float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700521 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700522}
523
524const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
525 size_t pointerIndex, size_t historicalIndex) const {
526 return &mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
527}
528
529float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan6b384612021-05-14 16:56:25 -0700530 size_t historicalIndex) const {
531 const PointerCoords* coords = getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
532
533 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
534 // For compatibility, convert raw coordinates into "oriented screen space". Once app
535 // developers are educated about getRaw, we can consider removing this.
Prabir Pradhan9f388812021-05-13 16:54:53 -0700536 const vec2 xy = shouldDisregardWindowTranslation(mSource)
Evan Rosky09576692021-07-01 12:22:09 -0700537 ? rotatePoint(mDisplayOrientation, coords->getX(), coords->getY())
538 : rotatePoint(mDisplayOrientation, coords->getX(), coords->getY(), mDisplayWidth,
Prabir Pradhan9f388812021-05-13 16:54:53 -0700539 mDisplayHeight);
Prabir Pradhan6b384612021-05-14 16:56:25 -0700540 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
541 return xy[axis];
Evan Rosky84f07f02021-04-16 10:42:42 -0700542 }
543
Prabir Pradhanc6523582021-05-14 18:02:55 -0700544 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
545 // For compatibility, since we convert raw coordinates into "oriented screen space", we
546 // need to convert the relative axes into the same orientation for consistency.
Evan Rosky09576692021-07-01 12:22:09 -0700547 const vec2 relativeXy = rotatePoint(mDisplayOrientation,
548 coords->getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
549 coords->getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
Prabir Pradhanc6523582021-05-14 18:02:55 -0700550 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
551 }
552
Prabir Pradhan6b384612021-05-14 16:56:25 -0700553 return coords->getAxisValue(axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700554}
555
556float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan9f388812021-05-13 16:54:53 -0700557 size_t historicalIndex) const {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700558 const PointerCoords* coords = getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
559
560 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
Prabir Pradhan9f388812021-05-13 16:54:53 -0700561 const vec2 xy = shouldDisregardWindowTranslation(mSource)
562 ? applyTransformWithoutTranslation(mTransform, coords->getX(), coords->getY())
563 : mTransform.transform(coords->getXYValue());
Prabir Pradhan6b384612021-05-14 16:56:25 -0700564 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
565 return xy[axis];
chaviw9eaa22c2020-07-01 16:21:27 -0700566 }
567
Prabir Pradhanc6523582021-05-14 18:02:55 -0700568 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
569 const vec2 relativeXy =
570 applyTransformWithoutTranslation(mTransform,
571 coords->getAxisValue(
572 AMOTION_EVENT_AXIS_RELATIVE_X),
573 coords->getAxisValue(
574 AMOTION_EVENT_AXIS_RELATIVE_Y));
575 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
576 }
577
Prabir Pradhan6b384612021-05-14 16:56:25 -0700578 return coords->getAxisValue(axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700579}
580
581ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
582 size_t pointerCount = mPointerProperties.size();
583 for (size_t i = 0; i < pointerCount; i++) {
584 if (mPointerProperties.itemAt(i).id == pointerId) {
585 return i;
586 }
587 }
588 return -1;
589}
590
591void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700592 float currXOffset = mTransform.tx();
593 float currYOffset = mTransform.ty();
594 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700595}
596
Robert Carre07e1032018-11-26 12:55:53 -0800597void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700598 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800599 mXPrecision *= globalScaleFactor;
600 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700601
602 size_t numSamples = mSamplePointerCoords.size();
603 for (size_t i = 0; i < numSamples; i++) {
chaviw9eaa22c2020-07-01 16:21:27 -0700604 mSamplePointerCoords.editItemAt(i).scale(globalScaleFactor, globalScaleFactor,
605 globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700606 }
607}
608
chaviw9eaa22c2020-07-01 16:21:27 -0700609void MotionEvent::transform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700610 // We want to preserve the raw axes values stored in the PointerCoords, so we just update the
611 // transform using the values passed in.
chaviw9eaa22c2020-07-01 16:21:27 -0700612 ui::Transform newTransform;
613 newTransform.set(matrix);
614 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700615
Prabir Pradhan6b384612021-05-14 16:56:25 -0700616 // We need to update the AXIS_ORIENTATION value here to maintain the old behavior where the
617 // orientation angle is not affected by the initial transformation set in the MotionEvent.
618 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
619 [&newTransform](PointerCoords& c) {
620 float orientation = c.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
621 c.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
622 transformAngle(newTransform, orientation));
623 });
Jeff Brown5912f952013-07-01 19:10:31 -0700624}
625
Evan Roskyd4d4d802021-05-03 20:12:21 -0700626void MotionEvent::applyTransform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700627 ui::Transform transform;
628 transform.set(matrix);
Evan Roskyd4d4d802021-05-03 20:12:21 -0700629
630 // Apply the transformation to all samples.
Prabir Pradhan6b384612021-05-14 16:56:25 -0700631 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
632 [&transform](PointerCoords& c) { c.transform(transform); });
Prabir Pradhan4b19bd02021-06-01 17:34:59 -0700633
634 if (mRawXCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION &&
635 mRawYCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) {
636 const vec2 cursor = transform.transform(mRawXCursorPosition, mRawYCursorPosition);
637 mRawXCursorPosition = cursor.x;
638 mRawYCursorPosition = cursor.y;
639 }
Evan Roskyd4d4d802021-05-03 20:12:21 -0700640}
641
Brett Chabotfaa986c2020-11-04 17:39:36 -0800642#ifdef __linux__
chaviw9eaa22c2020-07-01 16:21:27 -0700643static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
644 float dsdx, dtdx, tx, dtdy, dsdy, ty;
645 status_t status = parcel.readFloat(&dsdx);
646 status |= parcel.readFloat(&dtdx);
647 status |= parcel.readFloat(&tx);
648 status |= parcel.readFloat(&dtdy);
649 status |= parcel.readFloat(&dsdy);
650 status |= parcel.readFloat(&ty);
651
652 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
653 return status;
654}
655
656static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
657 status_t status = parcel.writeFloat(transform.dsdx());
658 status |= parcel.writeFloat(transform.dtdx());
659 status |= parcel.writeFloat(transform.tx());
660 status |= parcel.writeFloat(transform.dtdy());
661 status |= parcel.writeFloat(transform.dsdy());
662 status |= parcel.writeFloat(transform.ty());
663 return status;
664}
665
Jeff Brown5912f952013-07-01 19:10:31 -0700666status_t MotionEvent::readFromParcel(Parcel* parcel) {
667 size_t pointerCount = parcel->readInt32();
668 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800669 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
670 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700671 return BAD_VALUE;
672 }
673
Garfield Tan4cc839f2020-01-24 11:26:14 -0800674 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700675 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600676 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800677 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600678 std::vector<uint8_t> hmac;
679 status_t result = parcel->readByteVector(&hmac);
680 if (result != OK || hmac.size() != 32) {
681 return BAD_VALUE;
682 }
683 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700684 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100685 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700686 mFlags = parcel->readInt32();
687 mEdgeFlags = parcel->readInt32();
688 mMetaState = parcel->readInt32();
689 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800690 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700691
692 result = android::readFromParcel(mTransform, *parcel);
693 if (result != OK) {
694 return result;
695 }
Jeff Brown5912f952013-07-01 19:10:31 -0700696 mXPrecision = parcel->readFloat();
697 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700698 mRawXCursorPosition = parcel->readFloat();
699 mRawYCursorPosition = parcel->readFloat();
Evan Rosky09576692021-07-01 12:22:09 -0700700 mDisplayOrientation = parcel->readUint32();
Evan Rosky84f07f02021-04-16 10:42:42 -0700701 mDisplayWidth = parcel->readInt32();
702 mDisplayHeight = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700703 mDownTime = parcel->readInt64();
704
705 mPointerProperties.clear();
706 mPointerProperties.setCapacity(pointerCount);
707 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500708 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700709 mSamplePointerCoords.clear();
710 mSamplePointerCoords.setCapacity(sampleCount * pointerCount);
711
712 for (size_t i = 0; i < pointerCount; i++) {
713 mPointerProperties.push();
714 PointerProperties& properties = mPointerProperties.editTop();
715 properties.id = parcel->readInt32();
716 properties.toolType = parcel->readInt32();
717 }
718
Dan Austinc94fc452015-09-22 14:22:41 -0700719 while (sampleCount > 0) {
720 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500721 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700722 for (size_t i = 0; i < pointerCount; i++) {
723 mSamplePointerCoords.push();
724 status_t status = mSamplePointerCoords.editTop().readFromParcel(parcel);
725 if (status) {
726 return status;
727 }
728 }
729 }
730 return OK;
731}
732
733status_t MotionEvent::writeToParcel(Parcel* parcel) const {
734 size_t pointerCount = mPointerProperties.size();
735 size_t sampleCount = mSampleEventTimes.size();
736
737 parcel->writeInt32(pointerCount);
738 parcel->writeInt32(sampleCount);
739
Garfield Tan4cc839f2020-01-24 11:26:14 -0800740 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700741 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600742 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800743 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600744 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
745 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700746 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100747 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700748 parcel->writeInt32(mFlags);
749 parcel->writeInt32(mEdgeFlags);
750 parcel->writeInt32(mMetaState);
751 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800752 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700753
754 status_t result = android::writeToParcel(mTransform, *parcel);
755 if (result != OK) {
756 return result;
757 }
Jeff Brown5912f952013-07-01 19:10:31 -0700758 parcel->writeFloat(mXPrecision);
759 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700760 parcel->writeFloat(mRawXCursorPosition);
761 parcel->writeFloat(mRawYCursorPosition);
Evan Rosky09576692021-07-01 12:22:09 -0700762 parcel->writeUint32(mDisplayOrientation);
Evan Rosky84f07f02021-04-16 10:42:42 -0700763 parcel->writeInt32(mDisplayWidth);
764 parcel->writeInt32(mDisplayHeight);
Jeff Brown5912f952013-07-01 19:10:31 -0700765 parcel->writeInt64(mDownTime);
766
767 for (size_t i = 0; i < pointerCount; i++) {
768 const PointerProperties& properties = mPointerProperties.itemAt(i);
769 parcel->writeInt32(properties.id);
770 parcel->writeInt32(properties.toolType);
771 }
772
773 const PointerCoords* pc = mSamplePointerCoords.array();
774 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500775 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700776 for (size_t i = 0; i < pointerCount; i++) {
777 status_t status = (pc++)->writeToParcel(parcel);
778 if (status) {
779 return status;
780 }
781 }
782 }
783 return OK;
784}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800785#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700786
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600787bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Jeff Brown5912f952013-07-01 19:10:31 -0700788 if (source & AINPUT_SOURCE_CLASS_POINTER) {
789 // Specifically excludes HOVER_MOVE and SCROLL.
790 switch (action & AMOTION_EVENT_ACTION_MASK) {
791 case AMOTION_EVENT_ACTION_DOWN:
792 case AMOTION_EVENT_ACTION_MOVE:
793 case AMOTION_EVENT_ACTION_UP:
794 case AMOTION_EVENT_ACTION_POINTER_DOWN:
795 case AMOTION_EVENT_ACTION_POINTER_UP:
796 case AMOTION_EVENT_ACTION_CANCEL:
797 case AMOTION_EVENT_ACTION_OUTSIDE:
798 return true;
799 }
800 }
801 return false;
802}
803
Michael Wright872db4f2014-04-22 15:03:51 -0700804const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700805 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700806}
807
808int32_t MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700809 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700810}
811
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500812std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700813 // Convert MotionEvent action to string
814 switch (action & AMOTION_EVENT_ACTION_MASK) {
815 case AMOTION_EVENT_ACTION_DOWN:
816 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700817 case AMOTION_EVENT_ACTION_UP:
818 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500819 case AMOTION_EVENT_ACTION_MOVE:
820 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700821 case AMOTION_EVENT_ACTION_CANCEL:
822 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500823 case AMOTION_EVENT_ACTION_OUTSIDE:
824 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700825 case AMOTION_EVENT_ACTION_POINTER_DOWN:
826 return "POINTER_DOWN";
827 case AMOTION_EVENT_ACTION_POINTER_UP:
828 return "POINTER_UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500829 case AMOTION_EVENT_ACTION_HOVER_MOVE:
830 return "HOVER_MOVE";
831 case AMOTION_EVENT_ACTION_SCROLL:
832 return "SCROLL";
833 case AMOTION_EVENT_ACTION_HOVER_ENTER:
834 return "HOVER_ENTER";
835 case AMOTION_EVENT_ACTION_HOVER_EXIT:
836 return "HOVER_EXIT";
837 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
838 return "BUTTON_PRESS";
839 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
840 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700841 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500842 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700843}
844
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800845// --- FocusEvent ---
846
Garfield Tan4cc839f2020-01-24 11:26:14 -0800847void FocusEvent::initialize(int32_t id, bool hasFocus, bool inTouchMode) {
848 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600849 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800850 mHasFocus = hasFocus;
851 mInTouchMode = inTouchMode;
852}
853
854void FocusEvent::initialize(const FocusEvent& from) {
855 InputEvent::initialize(from);
856 mHasFocus = from.mHasFocus;
857 mInTouchMode = from.mInTouchMode;
858}
Jeff Brown5912f952013-07-01 19:10:31 -0700859
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800860// --- CaptureEvent ---
861
862void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) {
863 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
864 ADISPLAY_ID_NONE, INVALID_HMAC);
865 mPointerCaptureEnabled = pointerCaptureEnabled;
866}
867
868void CaptureEvent::initialize(const CaptureEvent& from) {
869 InputEvent::initialize(from);
870 mPointerCaptureEnabled = from.mPointerCaptureEnabled;
871}
872
arthurhung7632c332020-12-30 16:58:01 +0800873// --- DragEvent ---
874
875void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) {
876 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
877 ADISPLAY_ID_NONE, INVALID_HMAC);
878 mIsExiting = isExiting;
879 mX = x;
880 mY = y;
881}
882
883void DragEvent::initialize(const DragEvent& from) {
884 InputEvent::initialize(from);
885 mIsExiting = from.mIsExiting;
886 mX = from.mX;
887 mY = from.mY;
888}
889
Jeff Brown5912f952013-07-01 19:10:31 -0700890// --- PooledInputEventFactory ---
891
892PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
893 mMaxPoolSize(maxPoolSize) {
894}
895
896PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -0700897}
898
899KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800900 if (mKeyEventPool.empty()) {
901 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700902 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800903 KeyEvent* event = mKeyEventPool.front().release();
904 mKeyEventPool.pop();
905 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700906}
907
908MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800909 if (mMotionEventPool.empty()) {
910 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700911 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800912 MotionEvent* event = mMotionEventPool.front().release();
913 mMotionEventPool.pop();
914 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700915}
916
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800917FocusEvent* PooledInputEventFactory::createFocusEvent() {
918 if (mFocusEventPool.empty()) {
919 return new FocusEvent();
920 }
921 FocusEvent* event = mFocusEventPool.front().release();
922 mFocusEventPool.pop();
923 return event;
924}
925
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800926CaptureEvent* PooledInputEventFactory::createCaptureEvent() {
927 if (mCaptureEventPool.empty()) {
928 return new CaptureEvent();
929 }
930 CaptureEvent* event = mCaptureEventPool.front().release();
931 mCaptureEventPool.pop();
932 return event;
933}
934
arthurhung7632c332020-12-30 16:58:01 +0800935DragEvent* PooledInputEventFactory::createDragEvent() {
936 if (mDragEventPool.empty()) {
937 return new DragEvent();
938 }
939 DragEvent* event = mDragEventPool.front().release();
940 mDragEventPool.pop();
941 return event;
942}
943
Jeff Brown5912f952013-07-01 19:10:31 -0700944void PooledInputEventFactory::recycle(InputEvent* event) {
945 switch (event->getType()) {
946 case AINPUT_EVENT_TYPE_KEY:
947 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800948 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700949 return;
950 }
951 break;
952 case AINPUT_EVENT_TYPE_MOTION:
953 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800954 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -0700955 return;
956 }
957 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800958 case AINPUT_EVENT_TYPE_FOCUS:
959 if (mFocusEventPool.size() < mMaxPoolSize) {
960 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
961 return;
962 }
963 break;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800964 case AINPUT_EVENT_TYPE_CAPTURE:
965 if (mCaptureEventPool.size() < mMaxPoolSize) {
966 mCaptureEventPool.push(
967 std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event)));
968 return;
969 }
970 break;
arthurhung7632c332020-12-30 16:58:01 +0800971 case AINPUT_EVENT_TYPE_DRAG:
972 if (mDragEventPool.size() < mMaxPoolSize) {
973 mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event)));
974 return;
975 }
976 break;
Jeff Brown5912f952013-07-01 19:10:31 -0700977 }
978 delete event;
979}
980
981} // namespace android