blob: 3073d94dbebf373568b45c4e780b2e7fed352b78 [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>
Garfield Tan84b087e2020-01-23 10:49:05 -080023#include <string.h>
Jeff Brown5912f952013-07-01 19:10:31 -070024
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050025#include <android-base/stringprintf.h>
chaviw98318de2021-05-19 16:45:23 -050026#include <gui/constants.h>
Prabir Pradhan092f3a92021-11-25 10:53:27 -080027#include <input/DisplayViewport.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.
Prabir Pradhand2b02672021-10-19 11:24:45 -070060 // The return value of atan2f is in range [-pi, pi] which conforms to the orientation API.
61 return atan2f(transformedPoint.x, -transformedPoint.y);
Prabir Pradhan6b384612021-05-14 16:56:25 -070062}
63
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070064bool shouldDisregardTransformation(uint32_t source) {
65 // Do not apply any transformations to axes from joysticks or touchpads.
66 return isFromSource(source, AINPUT_SOURCE_CLASS_JOYSTICK) ||
67 isFromSource(source, AINPUT_SOURCE_CLASS_POSITION);
68}
69
70bool shouldDisregardOffset(uint32_t source) {
Prabir Pradhan9f388812021-05-13 16:54:53 -070071 // Pointer events are the only type of events that refer to absolute coordinates on the display,
72 // so we should apply the entire window transform. For other types of events, we should make
73 // sure to not apply the window translation/offset.
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070074 return !isFromSource(source, AINPUT_SOURCE_CLASS_POINTER);
Prabir Pradhan9f388812021-05-13 16:54:53 -070075}
76
Prabir Pradhan6b384612021-05-14 16:56:25 -070077} // namespace
78
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -080079const char* motionClassificationToString(MotionClassification classification) {
80 switch (classification) {
81 case MotionClassification::NONE:
82 return "NONE";
83 case MotionClassification::AMBIGUOUS_GESTURE:
84 return "AMBIGUOUS_GESTURE";
85 case MotionClassification::DEEP_PRESS:
86 return "DEEP_PRESS";
87 }
88}
89
Garfield Tan84b087e2020-01-23 10:49:05 -080090// --- IdGenerator ---
91IdGenerator::IdGenerator(Source source) : mSource(source) {}
92
93int32_t IdGenerator::nextId() const {
94 constexpr uint32_t SEQUENCE_NUMBER_MASK = ~SOURCE_MASK;
95 int32_t id = 0;
96
97// Avoid building against syscall getrandom(2) on host, which will fail build on Mac. Host doesn't
98// use sequence number so just always return mSource.
99#ifdef __ANDROID__
100 constexpr size_t BUF_LEN = sizeof(id);
101 size_t totalBytes = 0;
102 while (totalBytes < BUF_LEN) {
103 ssize_t bytes = TEMP_FAILURE_RETRY(getrandom(&id, BUF_LEN, GRND_NONBLOCK));
104 if (CC_UNLIKELY(bytes < 0)) {
105 ALOGW("Failed to fill in random number for sequence number: %s.", strerror(errno));
106 id = 0;
107 break;
108 }
109 totalBytes += bytes;
110 }
111#endif // __ANDROID__
112
113 return (id & SEQUENCE_NUMBER_MASK) | static_cast<int32_t>(mSource);
114}
115
Jeff Brown5912f952013-07-01 19:10:31 -0700116// --- InputEvent ---
117
Prabir Pradhande69f8a2021-11-18 16:40:34 +0000118vec2 transformWithoutTranslation(const ui::Transform& transform, const vec2& xy) {
119 const vec2 transformedXy = transform.transform(xy);
120 const vec2 transformedOrigin = transform.transform(0, 0);
121 return transformedXy - transformedOrigin;
122}
123
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800124const char* inputEventTypeToString(int32_t type) {
125 switch (type) {
126 case AINPUT_EVENT_TYPE_KEY: {
127 return "KEY";
128 }
129 case AINPUT_EVENT_TYPE_MOTION: {
130 return "MOTION";
131 }
132 case AINPUT_EVENT_TYPE_FOCUS: {
133 return "FOCUS";
134 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800135 case AINPUT_EVENT_TYPE_CAPTURE: {
136 return "CAPTURE";
137 }
arthurhung7632c332020-12-30 16:58:01 +0800138 case AINPUT_EVENT_TYPE_DRAG: {
139 return "DRAG";
140 }
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700141 case AINPUT_EVENT_TYPE_TOUCH_MODE: {
142 return "TOUCH_MODE";
143 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800144 }
145 return "UNKNOWN";
146}
147
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800148std::string inputEventSourceToString(int32_t source) {
149 if (source == AINPUT_SOURCE_UNKNOWN) {
150 return "UNKNOWN";
151 }
152 if (source == static_cast<int32_t>(AINPUT_SOURCE_ANY)) {
153 return "ANY";
154 }
155 static const std::map<int32_t, const char*> SOURCES{
156 {AINPUT_SOURCE_KEYBOARD, "KEYBOARD"},
157 {AINPUT_SOURCE_DPAD, "DPAD"},
158 {AINPUT_SOURCE_GAMEPAD, "GAMEPAD"},
159 {AINPUT_SOURCE_TOUCHSCREEN, "TOUCHSCREEN"},
160 {AINPUT_SOURCE_MOUSE, "MOUSE"},
161 {AINPUT_SOURCE_STYLUS, "STYLUS"},
162 {AINPUT_SOURCE_BLUETOOTH_STYLUS, "BLUETOOTH_STYLUS"},
163 {AINPUT_SOURCE_TRACKBALL, "TRACKBALL"},
164 {AINPUT_SOURCE_MOUSE_RELATIVE, "MOUSE_RELATIVE"},
165 {AINPUT_SOURCE_TOUCHPAD, "TOUCHPAD"},
166 {AINPUT_SOURCE_TOUCH_NAVIGATION, "TOUCH_NAVIGATION"},
167 {AINPUT_SOURCE_JOYSTICK, "JOYSTICK"},
168 {AINPUT_SOURCE_HDMI, "HDMI"},
169 {AINPUT_SOURCE_SENSOR, "SENSOR"},
170 {AINPUT_SOURCE_ROTARY_ENCODER, "ROTARY_ENCODER"},
171 };
172 std::string result;
173 for (const auto& [source_entry, str] : SOURCES) {
174 if ((source & source_entry) == source_entry) {
175 if (!result.empty()) {
176 result += " | ";
177 }
178 result += str;
179 }
180 }
181 if (result.empty()) {
182 result = StringPrintf("0x%08x", source);
183 }
184 return result;
185}
186
187bool isFromSource(uint32_t source, uint32_t test) {
188 return (source & test) == test;
189}
190
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800191VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
192 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
193 event.getSource(), event.getDisplayId()},
194 event.getAction(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800195 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800196 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800197 event.getKeyCode(),
198 event.getScanCode(),
199 event.getMetaState(),
200 event.getRepeatCount()};
201}
202
203VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
204 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
205 event.getSource(), event.getDisplayId()},
206 event.getRawX(0),
207 event.getRawY(0),
208 event.getActionMasked(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800209 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800210 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800211 event.getMetaState(),
212 event.getButtonState()};
213}
214
Garfield Tan4cc839f2020-01-24 11:26:14 -0800215void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600216 std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800217 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700218 mDeviceId = deviceId;
219 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100220 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600221 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700222}
223
224void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800225 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700226 mDeviceId = from.mDeviceId;
227 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100228 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600229 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700230}
231
Garfield Tan4cc839f2020-01-24 11:26:14 -0800232int32_t InputEvent::nextId() {
233 static IdGenerator idGen(IdGenerator::Source::OTHER);
234 return idGen.nextId();
235}
236
Jeff Brown5912f952013-07-01 19:10:31 -0700237// --- KeyEvent ---
238
Michael Wright872db4f2014-04-22 15:03:51 -0700239const char* KeyEvent::getLabel(int32_t keyCode) {
Chris Ye4958d062020-08-20 13:21:10 -0700240 return InputEventLookup::getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700241}
242
Michael Wright872db4f2014-04-22 15:03:51 -0700243int32_t KeyEvent::getKeyCodeFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700244 return InputEventLookup::getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700245}
246
Garfield Tan4cc839f2020-01-24 11:26:14 -0800247void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600248 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
249 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
250 nsecs_t downTime, nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800251 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700252 mAction = action;
253 mFlags = flags;
254 mKeyCode = keyCode;
255 mScanCode = scanCode;
256 mMetaState = metaState;
257 mRepeatCount = repeatCount;
258 mDownTime = downTime;
259 mEventTime = eventTime;
260}
261
262void KeyEvent::initialize(const KeyEvent& from) {
263 InputEvent::initialize(from);
264 mAction = from.mAction;
265 mFlags = from.mFlags;
266 mKeyCode = from.mKeyCode;
267 mScanCode = from.mScanCode;
268 mMetaState = from.mMetaState;
269 mRepeatCount = from.mRepeatCount;
270 mDownTime = from.mDownTime;
271 mEventTime = from.mEventTime;
272}
273
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700274const char* KeyEvent::actionToString(int32_t action) {
275 // Convert KeyEvent action to string
276 switch (action) {
277 case AKEY_EVENT_ACTION_DOWN:
278 return "DOWN";
279 case AKEY_EVENT_ACTION_UP:
280 return "UP";
281 case AKEY_EVENT_ACTION_MULTIPLE:
282 return "MULTIPLE";
283 }
284 return "UNKNOWN";
285}
Jeff Brown5912f952013-07-01 19:10:31 -0700286
287// --- PointerCoords ---
288
289float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700290 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700291 return 0;
292 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700293 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700294}
295
296status_t PointerCoords::setAxisValue(int32_t axis, float value) {
297 if (axis < 0 || axis > 63) {
298 return NAME_NOT_FOUND;
299 }
300
Michael Wright38dcdff2014-03-19 12:06:10 -0700301 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
302 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700303 if (value == 0) {
304 return OK; // axes with value 0 do not need to be stored
305 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700306
307 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700308 if (count >= MAX_AXES) {
309 tooManyAxes(axis);
310 return NO_MEMORY;
311 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700312 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700313 for (uint32_t i = count; i > index; i--) {
314 values[i] = values[i - 1];
315 }
316 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700317
Jeff Brown5912f952013-07-01 19:10:31 -0700318 values[index] = value;
319 return OK;
320}
321
322static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
323 float value = c.getAxisValue(axis);
324 if (value != 0) {
325 c.setAxisValue(axis, value * scaleFactor);
326 }
327}
328
Robert Carre07e1032018-11-26 12:55:53 -0800329void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700330 // No need to scale pressure or size since they are normalized.
331 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800332
333 // If there is a global scale factor, it is included in the windowX/YScale
334 // so we don't need to apply it twice to the X/Y axes.
335 // However we don't want to apply any windowXYScale not included in the global scale
336 // to the TOUCH_MAJOR/MINOR coordinates.
337 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
338 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
339 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
340 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
341 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
342 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
Prabir Pradhanc6523582021-05-14 18:02:55 -0700343 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_X, windowXScale);
344 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_Y, windowYScale);
Robert Carre07e1032018-11-26 12:55:53 -0800345}
346
Brett Chabotfaa986c2020-11-04 17:39:36 -0800347#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700348status_t PointerCoords::readFromParcel(Parcel* parcel) {
349 bits = parcel->readInt64();
350
Michael Wright38dcdff2014-03-19 12:06:10 -0700351 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700352 if (count > MAX_AXES) {
353 return BAD_VALUE;
354 }
355
356 for (uint32_t i = 0; i < count; i++) {
357 values[i] = parcel->readFloat();
358 }
359 return OK;
360}
361
362status_t PointerCoords::writeToParcel(Parcel* parcel) const {
363 parcel->writeInt64(bits);
364
Michael Wright38dcdff2014-03-19 12:06:10 -0700365 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700366 for (uint32_t i = 0; i < count; i++) {
367 parcel->writeFloat(values[i]);
368 }
369 return OK;
370}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800371#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700372
373void PointerCoords::tooManyAxes(int axis) {
374 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
375 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
376}
377
378bool PointerCoords::operator==(const PointerCoords& other) const {
379 if (bits != other.bits) {
380 return false;
381 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700382 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700383 for (uint32_t i = 0; i < count; i++) {
384 if (values[i] != other.values[i]) {
385 return false;
386 }
387 }
388 return true;
389}
390
391void PointerCoords::copyFrom(const PointerCoords& other) {
392 bits = other.bits;
Michael Wright38dcdff2014-03-19 12:06:10 -0700393 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700394 for (uint32_t i = 0; i < count; i++) {
395 values[i] = other.values[i];
396 }
397}
398
chaviwc01e1372020-07-01 12:37:31 -0700399void PointerCoords::transform(const ui::Transform& transform) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700400 const vec2 xy = transform.transform(getXYValue());
401 setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
402 setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
403
Prabir Pradhanc6523582021-05-14 18:02:55 -0700404 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_X) ||
405 BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_Y)) {
406 const ui::Transform rotation(transform.getOrientation());
407 const vec2 relativeXy = rotation.transform(getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
408 getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
409 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
410 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
411 }
412
Prabir Pradhan6b384612021-05-14 16:56:25 -0700413 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) {
414 const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
415 setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val));
416 }
chaviwc01e1372020-07-01 12:37:31 -0700417}
Jeff Brown5912f952013-07-01 19:10:31 -0700418
419// --- PointerProperties ---
420
421bool PointerProperties::operator==(const PointerProperties& other) const {
422 return id == other.id
423 && toolType == other.toolType;
424}
425
426void PointerProperties::copyFrom(const PointerProperties& other) {
427 id = other.id;
428 toolType = other.toolType;
429}
430
431
432// --- MotionEvent ---
433
Garfield Tan4cc839f2020-01-24 11:26:14 -0800434void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600435 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
436 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700437 int32_t buttonState, MotionClassification classification,
438 const ui::Transform& transform, float xPrecision, float yPrecision,
Evan Rosky84f07f02021-04-16 10:42:42 -0700439 float rawXCursorPosition, float rawYCursorPosition,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700440 const ui::Transform& rawTransform, nsecs_t downTime, nsecs_t eventTime,
Evan Rosky09576692021-07-01 12:22:09 -0700441 size_t pointerCount, const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700442 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800443 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700444 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100445 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700446 mFlags = flags;
447 mEdgeFlags = edgeFlags;
448 mMetaState = metaState;
449 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800450 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700451 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700452 mXPrecision = xPrecision;
453 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700454 mRawXCursorPosition = rawXCursorPosition;
455 mRawYCursorPosition = rawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700456 mRawTransform = rawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700457 mDownTime = downTime;
458 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800459 mPointerProperties.insert(mPointerProperties.end(), &pointerProperties[0],
460 &pointerProperties[pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700461 mSampleEventTimes.clear();
462 mSamplePointerCoords.clear();
463 addSample(eventTime, pointerCoords);
464}
465
466void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800467 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
468 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700469 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100470 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700471 mFlags = other->mFlags;
472 mEdgeFlags = other->mEdgeFlags;
473 mMetaState = other->mMetaState;
474 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800475 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700476 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700477 mXPrecision = other->mXPrecision;
478 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700479 mRawXCursorPosition = other->mRawXCursorPosition;
480 mRawYCursorPosition = other->mRawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700481 mRawTransform = other->mRawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700482 mDownTime = other->mDownTime;
483 mPointerProperties = other->mPointerProperties;
484
485 if (keepHistory) {
486 mSampleEventTimes = other->mSampleEventTimes;
487 mSamplePointerCoords = other->mSamplePointerCoords;
488 } else {
489 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500490 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700491 mSamplePointerCoords.clear();
492 size_t pointerCount = other->getPointerCount();
493 size_t historySize = other->getHistorySize();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800494 mSamplePointerCoords
495 .insert(mSamplePointerCoords.end(),
496 &other->mSamplePointerCoords[historySize * pointerCount],
497 &other->mSamplePointerCoords[historySize * pointerCount + pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700498 }
499}
500
501void MotionEvent::addSample(
502 int64_t eventTime,
503 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500504 mSampleEventTimes.push_back(eventTime);
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800505 mSamplePointerCoords.insert(mSamplePointerCoords.end(), &pointerCoords[0],
506 &pointerCoords[getPointerCount()]);
Jeff Brown5912f952013-07-01 19:10:31 -0700507}
508
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800509int MotionEvent::getSurfaceRotation() const {
510 // The surface rotation is the rotation from the window's coordinate space to that of the
511 // display. Since the event's transform takes display space coordinates to window space, the
512 // returned surface rotation is the inverse of the rotation for the surface.
513 switch (mTransform.getOrientation()) {
514 case ui::Transform::ROT_0:
515 return DISPLAY_ORIENTATION_0;
516 case ui::Transform::ROT_90:
517 return DISPLAY_ORIENTATION_270;
518 case ui::Transform::ROT_180:
519 return DISPLAY_ORIENTATION_180;
520 case ui::Transform::ROT_270:
521 return DISPLAY_ORIENTATION_90;
522 default:
523 return -1;
524 }
525}
526
Garfield Tan00f511d2019-06-12 16:55:40 -0700527float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700528 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
529 return vals.x;
Garfield Tan00f511d2019-06-12 16:55:40 -0700530}
531
532float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700533 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
534 return vals.y;
Garfield Tan00f511d2019-06-12 16:55:40 -0700535}
536
Garfield Tan937bb832019-07-25 17:48:31 -0700537void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700538 ui::Transform inverse = mTransform.inverse();
539 vec2 vals = inverse.transform(x, y);
540 mRawXCursorPosition = vals.x;
541 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700542}
543
Jeff Brown5912f952013-07-01 19:10:31 -0700544const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
545 return &mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex];
546}
547
548float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
Evan Rosky84f07f02021-04-16 10:42:42 -0700549 return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700550}
551
552float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700553 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700554}
555
556const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
557 size_t pointerIndex, size_t historicalIndex) const {
558 return &mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex];
559}
560
561float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan6b384612021-05-14 16:56:25 -0700562 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700563 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
564 return calculateTransformedAxisValue(axis, mSource, mRawTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700565}
566
567float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan9f388812021-05-13 16:54:53 -0700568 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700569 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
570 return calculateTransformedAxisValue(axis, mSource, mTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700571}
572
573ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
574 size_t pointerCount = mPointerProperties.size();
575 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800576 if (mPointerProperties[i].id == pointerId) {
Jeff Brown5912f952013-07-01 19:10:31 -0700577 return i;
578 }
579 }
580 return -1;
581}
582
583void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700584 float currXOffset = mTransform.tx();
585 float currYOffset = mTransform.ty();
586 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700587}
588
Robert Carre07e1032018-11-26 12:55:53 -0800589void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700590 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700591 mRawTransform.set(mRawTransform.tx() * globalScaleFactor,
592 mRawTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800593 mXPrecision *= globalScaleFactor;
594 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700595
596 size_t numSamples = mSamplePointerCoords.size();
597 for (size_t i = 0; i < numSamples; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800598 mSamplePointerCoords[i].scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700599 }
600}
601
chaviw9eaa22c2020-07-01 16:21:27 -0700602void MotionEvent::transform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700603 // We want to preserve the raw axes values stored in the PointerCoords, so we just update the
604 // transform using the values passed in.
chaviw9eaa22c2020-07-01 16:21:27 -0700605 ui::Transform newTransform;
606 newTransform.set(matrix);
607 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700608}
609
Evan Roskyd4d4d802021-05-03 20:12:21 -0700610void MotionEvent::applyTransform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700611 ui::Transform transform;
612 transform.set(matrix);
Evan Roskyd4d4d802021-05-03 20:12:21 -0700613
614 // Apply the transformation to all samples.
Prabir Pradhan6b384612021-05-14 16:56:25 -0700615 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
616 [&transform](PointerCoords& c) { c.transform(transform); });
Prabir Pradhan4b19bd02021-06-01 17:34:59 -0700617
618 if (mRawXCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION &&
619 mRawYCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) {
620 const vec2 cursor = transform.transform(mRawXCursorPosition, mRawYCursorPosition);
621 mRawXCursorPosition = cursor.x;
622 mRawYCursorPosition = cursor.y;
623 }
Evan Roskyd4d4d802021-05-03 20:12:21 -0700624}
625
Brett Chabotfaa986c2020-11-04 17:39:36 -0800626#ifdef __linux__
chaviw9eaa22c2020-07-01 16:21:27 -0700627static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
628 float dsdx, dtdx, tx, dtdy, dsdy, ty;
629 status_t status = parcel.readFloat(&dsdx);
630 status |= parcel.readFloat(&dtdx);
631 status |= parcel.readFloat(&tx);
632 status |= parcel.readFloat(&dtdy);
633 status |= parcel.readFloat(&dsdy);
634 status |= parcel.readFloat(&ty);
635
636 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
637 return status;
638}
639
640static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
641 status_t status = parcel.writeFloat(transform.dsdx());
642 status |= parcel.writeFloat(transform.dtdx());
643 status |= parcel.writeFloat(transform.tx());
644 status |= parcel.writeFloat(transform.dtdy());
645 status |= parcel.writeFloat(transform.dsdy());
646 status |= parcel.writeFloat(transform.ty());
647 return status;
648}
649
Jeff Brown5912f952013-07-01 19:10:31 -0700650status_t MotionEvent::readFromParcel(Parcel* parcel) {
651 size_t pointerCount = parcel->readInt32();
652 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800653 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
654 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700655 return BAD_VALUE;
656 }
657
Garfield Tan4cc839f2020-01-24 11:26:14 -0800658 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700659 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600660 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800661 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600662 std::vector<uint8_t> hmac;
663 status_t result = parcel->readByteVector(&hmac);
664 if (result != OK || hmac.size() != 32) {
665 return BAD_VALUE;
666 }
667 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700668 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100669 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700670 mFlags = parcel->readInt32();
671 mEdgeFlags = parcel->readInt32();
672 mMetaState = parcel->readInt32();
673 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800674 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700675
676 result = android::readFromParcel(mTransform, *parcel);
677 if (result != OK) {
678 return result;
679 }
Jeff Brown5912f952013-07-01 19:10:31 -0700680 mXPrecision = parcel->readFloat();
681 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700682 mRawXCursorPosition = parcel->readFloat();
683 mRawYCursorPosition = parcel->readFloat();
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700684
685 result = android::readFromParcel(mRawTransform, *parcel);
686 if (result != OK) {
687 return result;
688 }
Jeff Brown5912f952013-07-01 19:10:31 -0700689 mDownTime = parcel->readInt64();
690
691 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800692 mPointerProperties.reserve(pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700693 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500694 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700695 mSamplePointerCoords.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800696 mSamplePointerCoords.reserve(sampleCount * pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700697
698 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800699 mPointerProperties.push_back({});
700 PointerProperties& properties = mPointerProperties.back();
Jeff Brown5912f952013-07-01 19:10:31 -0700701 properties.id = parcel->readInt32();
702 properties.toolType = parcel->readInt32();
703 }
704
Dan Austinc94fc452015-09-22 14:22:41 -0700705 while (sampleCount > 0) {
706 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500707 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700708 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800709 mSamplePointerCoords.push_back({});
710 status_t status = mSamplePointerCoords.back().readFromParcel(parcel);
Jeff Brown5912f952013-07-01 19:10:31 -0700711 if (status) {
712 return status;
713 }
714 }
715 }
716 return OK;
717}
718
719status_t MotionEvent::writeToParcel(Parcel* parcel) const {
720 size_t pointerCount = mPointerProperties.size();
721 size_t sampleCount = mSampleEventTimes.size();
722
723 parcel->writeInt32(pointerCount);
724 parcel->writeInt32(sampleCount);
725
Garfield Tan4cc839f2020-01-24 11:26:14 -0800726 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700727 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600728 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800729 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600730 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
731 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700732 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100733 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700734 parcel->writeInt32(mFlags);
735 parcel->writeInt32(mEdgeFlags);
736 parcel->writeInt32(mMetaState);
737 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800738 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700739
740 status_t result = android::writeToParcel(mTransform, *parcel);
741 if (result != OK) {
742 return result;
743 }
Jeff Brown5912f952013-07-01 19:10:31 -0700744 parcel->writeFloat(mXPrecision);
745 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700746 parcel->writeFloat(mRawXCursorPosition);
747 parcel->writeFloat(mRawYCursorPosition);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700748
749 result = android::writeToParcel(mRawTransform, *parcel);
750 if (result != OK) {
751 return result;
752 }
Jeff Brown5912f952013-07-01 19:10:31 -0700753 parcel->writeInt64(mDownTime);
754
755 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800756 const PointerProperties& properties = mPointerProperties[i];
Jeff Brown5912f952013-07-01 19:10:31 -0700757 parcel->writeInt32(properties.id);
758 parcel->writeInt32(properties.toolType);
759 }
760
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800761 const PointerCoords* pc = mSamplePointerCoords.data();
Jeff Brown5912f952013-07-01 19:10:31 -0700762 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500763 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700764 for (size_t i = 0; i < pointerCount; i++) {
765 status_t status = (pc++)->writeToParcel(parcel);
766 if (status) {
767 return status;
768 }
769 }
770 }
771 return OK;
772}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800773#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700774
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600775bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700776 if (isFromSource(source, AINPUT_SOURCE_CLASS_POINTER)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700777 // Specifically excludes HOVER_MOVE and SCROLL.
778 switch (action & AMOTION_EVENT_ACTION_MASK) {
779 case AMOTION_EVENT_ACTION_DOWN:
780 case AMOTION_EVENT_ACTION_MOVE:
781 case AMOTION_EVENT_ACTION_UP:
782 case AMOTION_EVENT_ACTION_POINTER_DOWN:
783 case AMOTION_EVENT_ACTION_POINTER_UP:
784 case AMOTION_EVENT_ACTION_CANCEL:
785 case AMOTION_EVENT_ACTION_OUTSIDE:
786 return true;
787 }
788 }
789 return false;
790}
791
Michael Wright872db4f2014-04-22 15:03:51 -0700792const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700793 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700794}
795
796int32_t MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700797 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700798}
799
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500800std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700801 // Convert MotionEvent action to string
802 switch (action & AMOTION_EVENT_ACTION_MASK) {
803 case AMOTION_EVENT_ACTION_DOWN:
804 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700805 case AMOTION_EVENT_ACTION_UP:
806 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500807 case AMOTION_EVENT_ACTION_MOVE:
808 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700809 case AMOTION_EVENT_ACTION_CANCEL:
810 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500811 case AMOTION_EVENT_ACTION_OUTSIDE:
812 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700813 case AMOTION_EVENT_ACTION_POINTER_DOWN:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000814 return StringPrintf("POINTER_DOWN(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700815 case AMOTION_EVENT_ACTION_POINTER_UP:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000816 return StringPrintf("POINTER_UP(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500817 case AMOTION_EVENT_ACTION_HOVER_MOVE:
818 return "HOVER_MOVE";
819 case AMOTION_EVENT_ACTION_SCROLL:
820 return "SCROLL";
821 case AMOTION_EVENT_ACTION_HOVER_ENTER:
822 return "HOVER_ENTER";
823 case AMOTION_EVENT_ACTION_HOVER_EXIT:
824 return "HOVER_EXIT";
825 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
826 return "BUTTON_PRESS";
827 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
828 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700829 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500830 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700831}
832
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700833// Apply the given transformation to the point without checking whether the entire transform
834// should be disregarded altogether for the provided source.
835static inline vec2 calculateTransformedXYUnchecked(uint32_t source, const ui::Transform& transform,
836 const vec2& xy) {
837 return shouldDisregardOffset(source) ? transformWithoutTranslation(transform, xy)
838 : transform.transform(xy);
839}
840
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700841vec2 MotionEvent::calculateTransformedXY(uint32_t source, const ui::Transform& transform,
842 const vec2& xy) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700843 if (shouldDisregardTransformation(source)) {
844 return xy;
845 }
846 return calculateTransformedXYUnchecked(source, transform, xy);
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700847}
848
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700849float MotionEvent::calculateTransformedAxisValue(int32_t axis, uint32_t source,
850 const ui::Transform& transform,
851 const PointerCoords& coords) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700852 if (shouldDisregardTransformation(source)) {
853 return coords.getAxisValue(axis);
854 }
855
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700856 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700857 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700858 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
859 return xy[axis];
860 }
861
862 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
863 const vec2 relativeXy =
864 transformWithoutTranslation(transform,
865 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
866 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
867 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
868 }
869
870 if (axis == AMOTION_EVENT_AXIS_ORIENTATION) {
871 return transformAngle(transform, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION));
872 }
873
874 return coords.getAxisValue(axis);
875}
876
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800877// --- FocusEvent ---
878
Antonio Kantek3cfec7b2021-11-05 18:26:17 -0700879void FocusEvent::initialize(int32_t id, bool hasFocus) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800880 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600881 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800882 mHasFocus = hasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800883}
884
885void FocusEvent::initialize(const FocusEvent& from) {
886 InputEvent::initialize(from);
887 mHasFocus = from.mHasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800888}
Jeff Brown5912f952013-07-01 19:10:31 -0700889
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800890// --- CaptureEvent ---
891
892void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) {
893 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
894 ADISPLAY_ID_NONE, INVALID_HMAC);
895 mPointerCaptureEnabled = pointerCaptureEnabled;
896}
897
898void CaptureEvent::initialize(const CaptureEvent& from) {
899 InputEvent::initialize(from);
900 mPointerCaptureEnabled = from.mPointerCaptureEnabled;
901}
902
arthurhung7632c332020-12-30 16:58:01 +0800903// --- DragEvent ---
904
905void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) {
906 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
907 ADISPLAY_ID_NONE, INVALID_HMAC);
908 mIsExiting = isExiting;
909 mX = x;
910 mY = y;
911}
912
913void DragEvent::initialize(const DragEvent& from) {
914 InputEvent::initialize(from);
915 mIsExiting = from.mIsExiting;
916 mX = from.mX;
917 mY = from.mY;
918}
919
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700920// --- TouchModeEvent ---
921
922void TouchModeEvent::initialize(int32_t id, bool isInTouchMode) {
923 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
924 ADISPLAY_ID_NONE, INVALID_HMAC);
925 mIsInTouchMode = isInTouchMode;
926}
927
928void TouchModeEvent::initialize(const TouchModeEvent& from) {
929 InputEvent::initialize(from);
930 mIsInTouchMode = from.mIsInTouchMode;
931}
932
Jeff Brown5912f952013-07-01 19:10:31 -0700933// --- PooledInputEventFactory ---
934
935PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
936 mMaxPoolSize(maxPoolSize) {
937}
938
939PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -0700940}
941
942KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800943 if (mKeyEventPool.empty()) {
944 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700945 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800946 KeyEvent* event = mKeyEventPool.front().release();
947 mKeyEventPool.pop();
948 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700949}
950
951MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800952 if (mMotionEventPool.empty()) {
953 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -0700954 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -0800955 MotionEvent* event = mMotionEventPool.front().release();
956 mMotionEventPool.pop();
957 return event;
Jeff Brown5912f952013-07-01 19:10:31 -0700958}
959
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800960FocusEvent* PooledInputEventFactory::createFocusEvent() {
961 if (mFocusEventPool.empty()) {
962 return new FocusEvent();
963 }
964 FocusEvent* event = mFocusEventPool.front().release();
965 mFocusEventPool.pop();
966 return event;
967}
968
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800969CaptureEvent* PooledInputEventFactory::createCaptureEvent() {
970 if (mCaptureEventPool.empty()) {
971 return new CaptureEvent();
972 }
973 CaptureEvent* event = mCaptureEventPool.front().release();
974 mCaptureEventPool.pop();
975 return event;
976}
977
arthurhung7632c332020-12-30 16:58:01 +0800978DragEvent* PooledInputEventFactory::createDragEvent() {
979 if (mDragEventPool.empty()) {
980 return new DragEvent();
981 }
982 DragEvent* event = mDragEventPool.front().release();
983 mDragEventPool.pop();
984 return event;
985}
986
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700987TouchModeEvent* PooledInputEventFactory::createTouchModeEvent() {
988 if (mTouchModeEventPool.empty()) {
989 return new TouchModeEvent();
990 }
991 TouchModeEvent* event = mTouchModeEventPool.front().release();
992 mTouchModeEventPool.pop();
993 return event;
994}
995
Jeff Brown5912f952013-07-01 19:10:31 -0700996void PooledInputEventFactory::recycle(InputEvent* event) {
997 switch (event->getType()) {
998 case AINPUT_EVENT_TYPE_KEY:
999 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001000 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -07001001 return;
1002 }
1003 break;
1004 case AINPUT_EVENT_TYPE_MOTION:
1005 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001006 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -07001007 return;
1008 }
1009 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001010 case AINPUT_EVENT_TYPE_FOCUS:
1011 if (mFocusEventPool.size() < mMaxPoolSize) {
1012 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
1013 return;
1014 }
1015 break;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001016 case AINPUT_EVENT_TYPE_CAPTURE:
1017 if (mCaptureEventPool.size() < mMaxPoolSize) {
1018 mCaptureEventPool.push(
1019 std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event)));
1020 return;
1021 }
1022 break;
arthurhung7632c332020-12-30 16:58:01 +08001023 case AINPUT_EVENT_TYPE_DRAG:
1024 if (mDragEventPool.size() < mMaxPoolSize) {
1025 mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event)));
1026 return;
1027 }
1028 break;
Antonio Kantekeb4a30c2021-09-28 17:49:49 -07001029 case AINPUT_EVENT_TYPE_TOUCH_MODE:
1030 if (mTouchModeEventPool.size() < mMaxPoolSize) {
1031 mTouchModeEventPool.push(
1032 std::unique_ptr<TouchModeEvent>(static_cast<TouchModeEvent*>(event)));
1033 return;
1034 }
1035 break;
Jeff Brown5912f952013-07-01 19:10:31 -07001036 }
1037 delete event;
1038}
1039
1040} // namespace android