blob: cdc779dfd96872b887df9dcf48456980acfcf566 [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>
Michael Wright635422b2022-12-02 00:43:56 +000024#include <optional>
Jeff Brown5912f952013-07-01 19:10:31 -070025
Siarhei Vishniakou31977182022-09-30 08:51:23 -070026#include <android-base/file.h>
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +000027#include <android-base/logging.h>
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050028#include <android-base/stringprintf.h>
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +000029#include <cutils/compiler.h>
chaviw98318de2021-05-19 16:45:23 -050030#include <gui/constants.h>
Prabir Pradhan092f3a92021-11-25 10:53:27 -080031#include <input/DisplayViewport.h>
Jeff Brown5912f952013-07-01 19:10:31 -070032#include <input/Input.h>
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080033#include <input/InputDevice.h>
Michael Wright872db4f2014-04-22 15:03:51 -070034#include <input/InputEventLabels.h>
Jeff Brown5912f952013-07-01 19:10:31 -070035
Brett Chabotfaa986c2020-11-04 17:39:36 -080036#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -070037#include <binder/Parcel.h>
Brett Chabotfaa986c2020-11-04 17:39:36 -080038#endif
Siarhei Vishniakou63740b92022-10-20 10:28:08 -070039#if defined(__ANDROID__)
40#include <sys/random.h>
41#endif
Jeff Brown5912f952013-07-01 19:10:31 -070042
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050043using android::base::StringPrintf;
44
Jeff Brown5912f952013-07-01 19:10:31 -070045namespace android {
46
Prabir Pradhan6b384612021-05-14 16:56:25 -070047namespace {
48
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070049bool shouldDisregardTransformation(uint32_t source) {
Prabir Pradhan258e2b92022-06-24 18:37:04 +000050 // Do not apply any transformations to axes from joysticks, touchpads, or relative mice.
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070051 return isFromSource(source, AINPUT_SOURCE_CLASS_JOYSTICK) ||
Prabir Pradhan258e2b92022-06-24 18:37:04 +000052 isFromSource(source, AINPUT_SOURCE_CLASS_POSITION) ||
53 isFromSource(source, AINPUT_SOURCE_MOUSE_RELATIVE);
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070054}
55
56bool shouldDisregardOffset(uint32_t source) {
Prabir Pradhan9f388812021-05-13 16:54:53 -070057 // Pointer events are the only type of events that refer to absolute coordinates on the display,
58 // so we should apply the entire window transform. For other types of events, we should make
59 // sure to not apply the window translation/offset.
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070060 return !isFromSource(source, AINPUT_SOURCE_CLASS_POINTER);
Prabir Pradhan9f388812021-05-13 16:54:53 -070061}
62
Prabir Pradhan6b384612021-05-14 16:56:25 -070063} // namespace
64
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -080065const char* motionClassificationToString(MotionClassification classification) {
66 switch (classification) {
67 case MotionClassification::NONE:
68 return "NONE";
69 case MotionClassification::AMBIGUOUS_GESTURE:
70 return "AMBIGUOUS_GESTURE";
71 case MotionClassification::DEEP_PRESS:
72 return "DEEP_PRESS";
Harry Cutts2800fb02022-09-15 13:49:23 +000073 case MotionClassification::TWO_FINGER_SWIPE:
74 return "TWO_FINGER_SWIPE";
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -080075 }
76}
77
Siarhei Vishniakoud5fe5182022-07-20 23:28:40 +000078const char* motionToolTypeToString(int32_t toolType) {
79 switch (toolType) {
80 case AMOTION_EVENT_TOOL_TYPE_UNKNOWN:
81 return "UNKNOWN";
82 case AMOTION_EVENT_TOOL_TYPE_FINGER:
83 return "FINGER";
84 case AMOTION_EVENT_TOOL_TYPE_STYLUS:
85 return "STYLUS";
86 case AMOTION_EVENT_TOOL_TYPE_MOUSE:
87 return "MOUSE";
88 case AMOTION_EVENT_TOOL_TYPE_ERASER:
89 return "ERASER";
90 case AMOTION_EVENT_TOOL_TYPE_PALM:
91 return "PALM";
92 default:
93 return "INVALID";
94 }
95}
96
Garfield Tan84b087e2020-01-23 10:49:05 -080097// --- IdGenerator ---
Siarhei Vishniakou63740b92022-10-20 10:28:08 -070098#if defined(__ANDROID__)
99[[maybe_unused]]
100#endif
101static status_t
102getRandomBytes(uint8_t* data, size_t size) {
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700103 int ret = TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY | O_CLOEXEC | O_NOFOLLOW));
104 if (ret == -1) {
105 return -errno;
106 }
107
108 base::unique_fd fd(ret);
109 if (!base::ReadFully(fd, data, size)) {
110 return -errno;
111 }
112 return OK;
113}
114
Garfield Tan84b087e2020-01-23 10:49:05 -0800115IdGenerator::IdGenerator(Source source) : mSource(source) {}
116
117int32_t IdGenerator::nextId() const {
118 constexpr uint32_t SEQUENCE_NUMBER_MASK = ~SOURCE_MASK;
119 int32_t id = 0;
120
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700121#if defined(__ANDROID__)
122 // On device, prefer 'getrandom' to '/dev/urandom' because it's faster.
123 constexpr size_t BUF_LEN = sizeof(id);
124 size_t totalBytes = 0;
125 while (totalBytes < BUF_LEN) {
126 ssize_t bytes = TEMP_FAILURE_RETRY(getrandom(&id, BUF_LEN, GRND_NONBLOCK));
127 if (CC_UNLIKELY(bytes < 0)) {
128 ALOGW("Failed to fill in random number for sequence number: %s.", strerror(errno));
129 id = 0;
130 break;
131 }
132 totalBytes += bytes;
133 }
134#else
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700135#if defined(__linux__)
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700136 // On host, <sys/random.h> / GRND_NONBLOCK is not available
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700137 while (true) {
138 status_t result = getRandomBytes(reinterpret_cast<uint8_t*>(&id), sizeof(id));
139 if (result == OK) {
Garfield Tan84b087e2020-01-23 10:49:05 -0800140 break;
141 }
Garfield Tan84b087e2020-01-23 10:49:05 -0800142 }
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700143#endif // __linux__
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700144#endif // __ANDROID__
Garfield Tan84b087e2020-01-23 10:49:05 -0800145 return (id & SEQUENCE_NUMBER_MASK) | static_cast<int32_t>(mSource);
146}
147
Jeff Brown5912f952013-07-01 19:10:31 -0700148// --- InputEvent ---
149
Prabir Pradhande69f8a2021-11-18 16:40:34 +0000150vec2 transformWithoutTranslation(const ui::Transform& transform, const vec2& xy) {
151 const vec2 transformedXy = transform.transform(xy);
152 const vec2 transformedOrigin = transform.transform(0, 0);
153 return transformedXy - transformedOrigin;
154}
155
Prabir Pradhane2e10b42022-11-17 20:59:36 +0000156float transformAngle(const ui::Transform& transform, float angleRadians) {
157 // Construct and transform a vector oriented at the specified clockwise angle from vertical.
158 // Coordinate system: down is increasing Y, right is increasing X.
159 float x = sinf(angleRadians);
160 float y = -cosf(angleRadians);
161 vec2 transformedPoint = transform.transform(x, y);
162
163 // Determine how the origin is transformed by the matrix so that we
164 // can transform orientation vectors.
165 const vec2 origin = transform.transform(0, 0);
166
167 transformedPoint.x -= origin.x;
168 transformedPoint.y -= origin.y;
169
170 // Derive the transformed vector's clockwise angle from vertical.
171 // The return value of atan2f is in range [-pi, pi] which conforms to the orientation API.
172 return atan2f(transformedPoint.x, -transformedPoint.y);
173}
174
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800175const char* inputEventTypeToString(int32_t type) {
176 switch (type) {
177 case AINPUT_EVENT_TYPE_KEY: {
178 return "KEY";
179 }
180 case AINPUT_EVENT_TYPE_MOTION: {
181 return "MOTION";
182 }
183 case AINPUT_EVENT_TYPE_FOCUS: {
184 return "FOCUS";
185 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800186 case AINPUT_EVENT_TYPE_CAPTURE: {
187 return "CAPTURE";
188 }
arthurhung7632c332020-12-30 16:58:01 +0800189 case AINPUT_EVENT_TYPE_DRAG: {
190 return "DRAG";
191 }
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700192 case AINPUT_EVENT_TYPE_TOUCH_MODE: {
193 return "TOUCH_MODE";
194 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800195 }
196 return "UNKNOWN";
197}
198
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800199std::string inputEventSourceToString(int32_t source) {
200 if (source == AINPUT_SOURCE_UNKNOWN) {
201 return "UNKNOWN";
202 }
203 if (source == static_cast<int32_t>(AINPUT_SOURCE_ANY)) {
204 return "ANY";
205 }
206 static const std::map<int32_t, const char*> SOURCES{
207 {AINPUT_SOURCE_KEYBOARD, "KEYBOARD"},
208 {AINPUT_SOURCE_DPAD, "DPAD"},
209 {AINPUT_SOURCE_GAMEPAD, "GAMEPAD"},
210 {AINPUT_SOURCE_TOUCHSCREEN, "TOUCHSCREEN"},
211 {AINPUT_SOURCE_MOUSE, "MOUSE"},
212 {AINPUT_SOURCE_STYLUS, "STYLUS"},
213 {AINPUT_SOURCE_BLUETOOTH_STYLUS, "BLUETOOTH_STYLUS"},
214 {AINPUT_SOURCE_TRACKBALL, "TRACKBALL"},
215 {AINPUT_SOURCE_MOUSE_RELATIVE, "MOUSE_RELATIVE"},
216 {AINPUT_SOURCE_TOUCHPAD, "TOUCHPAD"},
217 {AINPUT_SOURCE_TOUCH_NAVIGATION, "TOUCH_NAVIGATION"},
218 {AINPUT_SOURCE_JOYSTICK, "JOYSTICK"},
219 {AINPUT_SOURCE_HDMI, "HDMI"},
220 {AINPUT_SOURCE_SENSOR, "SENSOR"},
221 {AINPUT_SOURCE_ROTARY_ENCODER, "ROTARY_ENCODER"},
222 };
223 std::string result;
224 for (const auto& [source_entry, str] : SOURCES) {
225 if ((source & source_entry) == source_entry) {
226 if (!result.empty()) {
227 result += " | ";
228 }
229 result += str;
230 }
231 }
232 if (result.empty()) {
233 result = StringPrintf("0x%08x", source);
234 }
235 return result;
236}
237
238bool isFromSource(uint32_t source, uint32_t test) {
239 return (source & test) == test;
240}
241
Prabir Pradhane5626962022-10-27 20:30:53 +0000242bool isStylusToolType(uint32_t toolType) {
243 return toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS || toolType == AMOTION_EVENT_TOOL_TYPE_ERASER;
244}
245
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800246VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
247 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
248 event.getSource(), event.getDisplayId()},
249 event.getAction(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800250 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800251 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800252 event.getKeyCode(),
253 event.getScanCode(),
254 event.getMetaState(),
255 event.getRepeatCount()};
256}
257
258VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
259 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
260 event.getSource(), event.getDisplayId()},
261 event.getRawX(0),
262 event.getRawY(0),
263 event.getActionMasked(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800264 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800265 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800266 event.getMetaState(),
267 event.getButtonState()};
268}
269
Garfield Tan4cc839f2020-01-24 11:26:14 -0800270void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600271 std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800272 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700273 mDeviceId = deviceId;
274 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100275 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600276 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700277}
278
279void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800280 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700281 mDeviceId = from.mDeviceId;
282 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100283 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600284 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700285}
286
Garfield Tan4cc839f2020-01-24 11:26:14 -0800287int32_t InputEvent::nextId() {
288 static IdGenerator idGen(IdGenerator::Source::OTHER);
289 return idGen.nextId();
290}
291
Jeff Brown5912f952013-07-01 19:10:31 -0700292// --- KeyEvent ---
293
Michael Wright872db4f2014-04-22 15:03:51 -0700294const char* KeyEvent::getLabel(int32_t keyCode) {
Chris Ye4958d062020-08-20 13:21:10 -0700295 return InputEventLookup::getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700296}
297
Michael Wright872db4f2014-04-22 15:03:51 -0700298int32_t KeyEvent::getKeyCodeFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700299 return InputEventLookup::getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700300}
301
Garfield Tan4cc839f2020-01-24 11:26:14 -0800302void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600303 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
304 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
305 nsecs_t downTime, nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800306 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700307 mAction = action;
308 mFlags = flags;
309 mKeyCode = keyCode;
310 mScanCode = scanCode;
311 mMetaState = metaState;
312 mRepeatCount = repeatCount;
313 mDownTime = downTime;
314 mEventTime = eventTime;
315}
316
317void KeyEvent::initialize(const KeyEvent& from) {
318 InputEvent::initialize(from);
319 mAction = from.mAction;
320 mFlags = from.mFlags;
321 mKeyCode = from.mKeyCode;
322 mScanCode = from.mScanCode;
323 mMetaState = from.mMetaState;
324 mRepeatCount = from.mRepeatCount;
325 mDownTime = from.mDownTime;
326 mEventTime = from.mEventTime;
327}
328
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700329const char* KeyEvent::actionToString(int32_t action) {
330 // Convert KeyEvent action to string
331 switch (action) {
332 case AKEY_EVENT_ACTION_DOWN:
333 return "DOWN";
334 case AKEY_EVENT_ACTION_UP:
335 return "UP";
336 case AKEY_EVENT_ACTION_MULTIPLE:
337 return "MULTIPLE";
338 }
339 return "UNKNOWN";
340}
Jeff Brown5912f952013-07-01 19:10:31 -0700341
342// --- PointerCoords ---
343
344float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700345 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700346 return 0;
347 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700348 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700349}
350
351status_t PointerCoords::setAxisValue(int32_t axis, float value) {
352 if (axis < 0 || axis > 63) {
353 return NAME_NOT_FOUND;
354 }
355
Michael Wright38dcdff2014-03-19 12:06:10 -0700356 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
357 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700358 if (value == 0) {
359 return OK; // axes with value 0 do not need to be stored
360 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700361
362 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700363 if (count >= MAX_AXES) {
364 tooManyAxes(axis);
365 return NO_MEMORY;
366 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700367 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700368 for (uint32_t i = count; i > index; i--) {
369 values[i] = values[i - 1];
370 }
371 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700372
Jeff Brown5912f952013-07-01 19:10:31 -0700373 values[index] = value;
374 return OK;
375}
376
377static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
378 float value = c.getAxisValue(axis);
379 if (value != 0) {
380 c.setAxisValue(axis, value * scaleFactor);
381 }
382}
383
Robert Carre07e1032018-11-26 12:55:53 -0800384void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700385 // No need to scale pressure or size since they are normalized.
386 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800387
388 // If there is a global scale factor, it is included in the windowX/YScale
389 // so we don't need to apply it twice to the X/Y axes.
390 // However we don't want to apply any windowXYScale not included in the global scale
391 // to the TOUCH_MAJOR/MINOR coordinates.
392 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
393 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
394 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
395 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
396 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
397 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
Prabir Pradhanc6523582021-05-14 18:02:55 -0700398 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_X, windowXScale);
399 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_Y, windowYScale);
Robert Carre07e1032018-11-26 12:55:53 -0800400}
401
Brett Chabotfaa986c2020-11-04 17:39:36 -0800402#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700403status_t PointerCoords::readFromParcel(Parcel* parcel) {
404 bits = parcel->readInt64();
405
Michael Wright38dcdff2014-03-19 12:06:10 -0700406 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700407 if (count > MAX_AXES) {
408 return BAD_VALUE;
409 }
410
411 for (uint32_t i = 0; i < count; i++) {
412 values[i] = parcel->readFloat();
413 }
Philip Quinnafb31282022-12-20 18:17:55 -0800414
415 isResampled = parcel->readBool();
Jeff Brown5912f952013-07-01 19:10:31 -0700416 return OK;
417}
418
419status_t PointerCoords::writeToParcel(Parcel* parcel) const {
420 parcel->writeInt64(bits);
421
Michael Wright38dcdff2014-03-19 12:06:10 -0700422 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700423 for (uint32_t i = 0; i < count; i++) {
424 parcel->writeFloat(values[i]);
425 }
Philip Quinnafb31282022-12-20 18:17:55 -0800426
427 parcel->writeBool(isResampled);
Jeff Brown5912f952013-07-01 19:10:31 -0700428 return OK;
429}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800430#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700431
432void PointerCoords::tooManyAxes(int axis) {
433 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
434 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
435}
436
437bool PointerCoords::operator==(const PointerCoords& other) const {
438 if (bits != other.bits) {
439 return false;
440 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700441 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700442 for (uint32_t i = 0; i < count; i++) {
443 if (values[i] != other.values[i]) {
444 return false;
445 }
446 }
Philip Quinnafb31282022-12-20 18:17:55 -0800447 if (isResampled != other.isResampled) {
448 return false;
449 }
Jeff Brown5912f952013-07-01 19:10:31 -0700450 return true;
451}
452
chaviwc01e1372020-07-01 12:37:31 -0700453void PointerCoords::transform(const ui::Transform& transform) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700454 const vec2 xy = transform.transform(getXYValue());
455 setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
456 setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
457
Prabir Pradhanc6523582021-05-14 18:02:55 -0700458 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_X) ||
459 BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_Y)) {
460 const ui::Transform rotation(transform.getOrientation());
461 const vec2 relativeXy = rotation.transform(getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
462 getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
463 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
464 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
465 }
466
Prabir Pradhan6b384612021-05-14 16:56:25 -0700467 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) {
468 const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
469 setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val));
470 }
chaviwc01e1372020-07-01 12:37:31 -0700471}
Jeff Brown5912f952013-07-01 19:10:31 -0700472
473// --- PointerProperties ---
474
475bool PointerProperties::operator==(const PointerProperties& other) const {
476 return id == other.id
477 && toolType == other.toolType;
478}
479
480void PointerProperties::copyFrom(const PointerProperties& other) {
481 id = other.id;
482 toolType = other.toolType;
483}
484
485
486// --- MotionEvent ---
487
Garfield Tan4cc839f2020-01-24 11:26:14 -0800488void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600489 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
490 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700491 int32_t buttonState, MotionClassification classification,
492 const ui::Transform& transform, float xPrecision, float yPrecision,
Evan Rosky84f07f02021-04-16 10:42:42 -0700493 float rawXCursorPosition, float rawYCursorPosition,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700494 const ui::Transform& rawTransform, nsecs_t downTime, nsecs_t eventTime,
Evan Rosky09576692021-07-01 12:22:09 -0700495 size_t pointerCount, const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700496 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800497 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700498 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100499 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700500 mFlags = flags;
501 mEdgeFlags = edgeFlags;
502 mMetaState = metaState;
503 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800504 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700505 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700506 mXPrecision = xPrecision;
507 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700508 mRawXCursorPosition = rawXCursorPosition;
509 mRawYCursorPosition = rawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700510 mRawTransform = rawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700511 mDownTime = downTime;
512 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800513 mPointerProperties.insert(mPointerProperties.end(), &pointerProperties[0],
514 &pointerProperties[pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700515 mSampleEventTimes.clear();
516 mSamplePointerCoords.clear();
517 addSample(eventTime, pointerCoords);
518}
519
520void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800521 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
522 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700523 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100524 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700525 mFlags = other->mFlags;
526 mEdgeFlags = other->mEdgeFlags;
527 mMetaState = other->mMetaState;
528 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800529 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700530 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700531 mXPrecision = other->mXPrecision;
532 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700533 mRawXCursorPosition = other->mRawXCursorPosition;
534 mRawYCursorPosition = other->mRawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700535 mRawTransform = other->mRawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700536 mDownTime = other->mDownTime;
537 mPointerProperties = other->mPointerProperties;
538
539 if (keepHistory) {
540 mSampleEventTimes = other->mSampleEventTimes;
541 mSamplePointerCoords = other->mSamplePointerCoords;
542 } else {
543 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500544 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700545 mSamplePointerCoords.clear();
546 size_t pointerCount = other->getPointerCount();
547 size_t historySize = other->getHistorySize();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800548 mSamplePointerCoords
549 .insert(mSamplePointerCoords.end(),
550 &other->mSamplePointerCoords[historySize * pointerCount],
551 &other->mSamplePointerCoords[historySize * pointerCount + pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700552 }
553}
554
555void MotionEvent::addSample(
556 int64_t eventTime,
557 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500558 mSampleEventTimes.push_back(eventTime);
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800559 mSamplePointerCoords.insert(mSamplePointerCoords.end(), &pointerCoords[0],
560 &pointerCoords[getPointerCount()]);
Jeff Brown5912f952013-07-01 19:10:31 -0700561}
562
Michael Wright635422b2022-12-02 00:43:56 +0000563std::optional<ui::Rotation> MotionEvent::getSurfaceRotation() const {
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800564 // The surface rotation is the rotation from the window's coordinate space to that of the
565 // display. Since the event's transform takes display space coordinates to window space, the
566 // returned surface rotation is the inverse of the rotation for the surface.
567 switch (mTransform.getOrientation()) {
568 case ui::Transform::ROT_0:
Michael Wright635422b2022-12-02 00:43:56 +0000569 return ui::ROTATION_0;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800570 case ui::Transform::ROT_90:
Michael Wright635422b2022-12-02 00:43:56 +0000571 return ui::ROTATION_270;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800572 case ui::Transform::ROT_180:
Michael Wright635422b2022-12-02 00:43:56 +0000573 return ui::ROTATION_180;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800574 case ui::Transform::ROT_270:
Michael Wright635422b2022-12-02 00:43:56 +0000575 return ui::ROTATION_90;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800576 default:
Michael Wright635422b2022-12-02 00:43:56 +0000577 return std::nullopt;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800578 }
579}
580
Garfield Tan00f511d2019-06-12 16:55:40 -0700581float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700582 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
583 return vals.x;
Garfield Tan00f511d2019-06-12 16:55:40 -0700584}
585
586float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700587 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
588 return vals.y;
Garfield Tan00f511d2019-06-12 16:55:40 -0700589}
590
Garfield Tan937bb832019-07-25 17:48:31 -0700591void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700592 ui::Transform inverse = mTransform.inverse();
593 vec2 vals = inverse.transform(x, y);
594 mRawXCursorPosition = vals.x;
595 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700596}
597
Jeff Brown5912f952013-07-01 19:10:31 -0700598const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000599 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
600 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
601 }
602 const size_t position = getHistorySize() * getPointerCount() + pointerIndex;
603 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
604 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
605 }
606 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700607}
608
609float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
Evan Rosky84f07f02021-04-16 10:42:42 -0700610 return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700611}
612
613float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700614 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700615}
616
617const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
618 size_t pointerIndex, size_t historicalIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000619 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
620 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
621 }
622 if (CC_UNLIKELY(historicalIndex < 0 || historicalIndex > getHistorySize())) {
623 LOG(FATAL) << __func__ << ": Invalid historical index " << historicalIndex << " for "
624 << *this;
625 }
626 const size_t position = historicalIndex * getPointerCount() + pointerIndex;
627 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
628 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
629 }
630 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700631}
632
633float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan6b384612021-05-14 16:56:25 -0700634 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700635 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
636 return calculateTransformedAxisValue(axis, mSource, mRawTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700637}
638
639float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan9f388812021-05-13 16:54:53 -0700640 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700641 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
642 return calculateTransformedAxisValue(axis, mSource, mTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700643}
644
645ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
646 size_t pointerCount = mPointerProperties.size();
647 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800648 if (mPointerProperties[i].id == pointerId) {
Jeff Brown5912f952013-07-01 19:10:31 -0700649 return i;
650 }
651 }
652 return -1;
653}
654
655void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700656 float currXOffset = mTransform.tx();
657 float currYOffset = mTransform.ty();
658 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700659}
660
Robert Carre07e1032018-11-26 12:55:53 -0800661void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700662 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700663 mRawTransform.set(mRawTransform.tx() * globalScaleFactor,
664 mRawTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800665 mXPrecision *= globalScaleFactor;
666 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700667
668 size_t numSamples = mSamplePointerCoords.size();
669 for (size_t i = 0; i < numSamples; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800670 mSamplePointerCoords[i].scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700671 }
672}
673
chaviw9eaa22c2020-07-01 16:21:27 -0700674void MotionEvent::transform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700675 // We want to preserve the raw axes values stored in the PointerCoords, so we just update the
676 // transform using the values passed in.
chaviw9eaa22c2020-07-01 16:21:27 -0700677 ui::Transform newTransform;
678 newTransform.set(matrix);
679 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700680}
681
Evan Roskyd4d4d802021-05-03 20:12:21 -0700682void MotionEvent::applyTransform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700683 ui::Transform transform;
684 transform.set(matrix);
Evan Roskyd4d4d802021-05-03 20:12:21 -0700685
686 // Apply the transformation to all samples.
Prabir Pradhan6b384612021-05-14 16:56:25 -0700687 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
688 [&transform](PointerCoords& c) { c.transform(transform); });
Prabir Pradhan4b19bd02021-06-01 17:34:59 -0700689
690 if (mRawXCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION &&
691 mRawYCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) {
692 const vec2 cursor = transform.transform(mRawXCursorPosition, mRawYCursorPosition);
693 mRawXCursorPosition = cursor.x;
694 mRawYCursorPosition = cursor.y;
695 }
Evan Roskyd4d4d802021-05-03 20:12:21 -0700696}
697
Brett Chabotfaa986c2020-11-04 17:39:36 -0800698#ifdef __linux__
chaviw9eaa22c2020-07-01 16:21:27 -0700699static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
700 float dsdx, dtdx, tx, dtdy, dsdy, ty;
701 status_t status = parcel.readFloat(&dsdx);
702 status |= parcel.readFloat(&dtdx);
703 status |= parcel.readFloat(&tx);
704 status |= parcel.readFloat(&dtdy);
705 status |= parcel.readFloat(&dsdy);
706 status |= parcel.readFloat(&ty);
707
708 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
709 return status;
710}
711
712static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
713 status_t status = parcel.writeFloat(transform.dsdx());
714 status |= parcel.writeFloat(transform.dtdx());
715 status |= parcel.writeFloat(transform.tx());
716 status |= parcel.writeFloat(transform.dtdy());
717 status |= parcel.writeFloat(transform.dsdy());
718 status |= parcel.writeFloat(transform.ty());
719 return status;
720}
721
Jeff Brown5912f952013-07-01 19:10:31 -0700722status_t MotionEvent::readFromParcel(Parcel* parcel) {
723 size_t pointerCount = parcel->readInt32();
724 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800725 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
726 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700727 return BAD_VALUE;
728 }
729
Garfield Tan4cc839f2020-01-24 11:26:14 -0800730 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700731 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600732 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800733 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600734 std::vector<uint8_t> hmac;
735 status_t result = parcel->readByteVector(&hmac);
736 if (result != OK || hmac.size() != 32) {
737 return BAD_VALUE;
738 }
739 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700740 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100741 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700742 mFlags = parcel->readInt32();
743 mEdgeFlags = parcel->readInt32();
744 mMetaState = parcel->readInt32();
745 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800746 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700747
748 result = android::readFromParcel(mTransform, *parcel);
749 if (result != OK) {
750 return result;
751 }
Jeff Brown5912f952013-07-01 19:10:31 -0700752 mXPrecision = parcel->readFloat();
753 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700754 mRawXCursorPosition = parcel->readFloat();
755 mRawYCursorPosition = parcel->readFloat();
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700756
757 result = android::readFromParcel(mRawTransform, *parcel);
758 if (result != OK) {
759 return result;
760 }
Jeff Brown5912f952013-07-01 19:10:31 -0700761 mDownTime = parcel->readInt64();
762
763 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800764 mPointerProperties.reserve(pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700765 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500766 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700767 mSamplePointerCoords.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800768 mSamplePointerCoords.reserve(sampleCount * pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700769
770 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800771 mPointerProperties.push_back({});
772 PointerProperties& properties = mPointerProperties.back();
Jeff Brown5912f952013-07-01 19:10:31 -0700773 properties.id = parcel->readInt32();
774 properties.toolType = parcel->readInt32();
775 }
776
Dan Austinc94fc452015-09-22 14:22:41 -0700777 while (sampleCount > 0) {
778 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500779 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700780 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800781 mSamplePointerCoords.push_back({});
782 status_t status = mSamplePointerCoords.back().readFromParcel(parcel);
Jeff Brown5912f952013-07-01 19:10:31 -0700783 if (status) {
784 return status;
785 }
786 }
787 }
788 return OK;
789}
790
791status_t MotionEvent::writeToParcel(Parcel* parcel) const {
792 size_t pointerCount = mPointerProperties.size();
793 size_t sampleCount = mSampleEventTimes.size();
794
795 parcel->writeInt32(pointerCount);
796 parcel->writeInt32(sampleCount);
797
Garfield Tan4cc839f2020-01-24 11:26:14 -0800798 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700799 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600800 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800801 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600802 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
803 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700804 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100805 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700806 parcel->writeInt32(mFlags);
807 parcel->writeInt32(mEdgeFlags);
808 parcel->writeInt32(mMetaState);
809 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800810 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700811
812 status_t result = android::writeToParcel(mTransform, *parcel);
813 if (result != OK) {
814 return result;
815 }
Jeff Brown5912f952013-07-01 19:10:31 -0700816 parcel->writeFloat(mXPrecision);
817 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700818 parcel->writeFloat(mRawXCursorPosition);
819 parcel->writeFloat(mRawYCursorPosition);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700820
821 result = android::writeToParcel(mRawTransform, *parcel);
822 if (result != OK) {
823 return result;
824 }
Jeff Brown5912f952013-07-01 19:10:31 -0700825 parcel->writeInt64(mDownTime);
826
827 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800828 const PointerProperties& properties = mPointerProperties[i];
Jeff Brown5912f952013-07-01 19:10:31 -0700829 parcel->writeInt32(properties.id);
830 parcel->writeInt32(properties.toolType);
831 }
832
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800833 const PointerCoords* pc = mSamplePointerCoords.data();
Jeff Brown5912f952013-07-01 19:10:31 -0700834 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500835 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700836 for (size_t i = 0; i < pointerCount; i++) {
837 status_t status = (pc++)->writeToParcel(parcel);
838 if (status) {
839 return status;
840 }
841 }
842 }
843 return OK;
844}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800845#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700846
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600847bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700848 if (isFromSource(source, AINPUT_SOURCE_CLASS_POINTER)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700849 // Specifically excludes HOVER_MOVE and SCROLL.
850 switch (action & AMOTION_EVENT_ACTION_MASK) {
851 case AMOTION_EVENT_ACTION_DOWN:
852 case AMOTION_EVENT_ACTION_MOVE:
853 case AMOTION_EVENT_ACTION_UP:
854 case AMOTION_EVENT_ACTION_POINTER_DOWN:
855 case AMOTION_EVENT_ACTION_POINTER_UP:
856 case AMOTION_EVENT_ACTION_CANCEL:
857 case AMOTION_EVENT_ACTION_OUTSIDE:
858 return true;
859 }
860 }
861 return false;
862}
863
Michael Wright872db4f2014-04-22 15:03:51 -0700864const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700865 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700866}
867
868int32_t MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700869 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700870}
871
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500872std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700873 // Convert MotionEvent action to string
874 switch (action & AMOTION_EVENT_ACTION_MASK) {
875 case AMOTION_EVENT_ACTION_DOWN:
876 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700877 case AMOTION_EVENT_ACTION_UP:
878 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500879 case AMOTION_EVENT_ACTION_MOVE:
880 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700881 case AMOTION_EVENT_ACTION_CANCEL:
882 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500883 case AMOTION_EVENT_ACTION_OUTSIDE:
884 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700885 case AMOTION_EVENT_ACTION_POINTER_DOWN:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000886 return StringPrintf("POINTER_DOWN(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700887 case AMOTION_EVENT_ACTION_POINTER_UP:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000888 return StringPrintf("POINTER_UP(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500889 case AMOTION_EVENT_ACTION_HOVER_MOVE:
890 return "HOVER_MOVE";
891 case AMOTION_EVENT_ACTION_SCROLL:
892 return "SCROLL";
893 case AMOTION_EVENT_ACTION_HOVER_ENTER:
894 return "HOVER_ENTER";
895 case AMOTION_EVENT_ACTION_HOVER_EXIT:
896 return "HOVER_EXIT";
897 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
898 return "BUTTON_PRESS";
899 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
900 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700901 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500902 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700903}
904
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700905// Apply the given transformation to the point without checking whether the entire transform
906// should be disregarded altogether for the provided source.
907static inline vec2 calculateTransformedXYUnchecked(uint32_t source, const ui::Transform& transform,
908 const vec2& xy) {
909 return shouldDisregardOffset(source) ? transformWithoutTranslation(transform, xy)
910 : transform.transform(xy);
911}
912
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700913vec2 MotionEvent::calculateTransformedXY(uint32_t source, const ui::Transform& transform,
914 const vec2& xy) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700915 if (shouldDisregardTransformation(source)) {
916 return xy;
917 }
918 return calculateTransformedXYUnchecked(source, transform, xy);
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700919}
920
Prabir Pradhan8e6ce222022-02-24 09:08:54 -0800921// Keep in sync with calculateTransformedCoords.
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700922float MotionEvent::calculateTransformedAxisValue(int32_t axis, uint32_t source,
923 const ui::Transform& transform,
924 const PointerCoords& coords) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700925 if (shouldDisregardTransformation(source)) {
926 return coords.getAxisValue(axis);
927 }
928
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700929 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700930 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700931 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
932 return xy[axis];
933 }
934
935 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
936 const vec2 relativeXy =
937 transformWithoutTranslation(transform,
938 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
939 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
940 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
941 }
942
943 if (axis == AMOTION_EVENT_AXIS_ORIENTATION) {
944 return transformAngle(transform, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION));
945 }
946
947 return coords.getAxisValue(axis);
948}
949
Prabir Pradhan8e6ce222022-02-24 09:08:54 -0800950// Keep in sync with calculateTransformedAxisValue. This is an optimization of
951// calculateTransformedAxisValue for all PointerCoords axes.
952PointerCoords MotionEvent::calculateTransformedCoords(uint32_t source,
953 const ui::Transform& transform,
954 const PointerCoords& coords) {
955 if (shouldDisregardTransformation(source)) {
956 return coords;
957 }
958 PointerCoords out = coords;
959
960 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
961 out.setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
962 out.setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
963
964 const vec2 relativeXy =
965 transformWithoutTranslation(transform,
966 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
967 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
968 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
969 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
970
971 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
972 transformAngle(transform,
973 coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)));
974
975 return out;
976}
977
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000978std::ostream& operator<<(std::ostream& out, const MotionEvent& event) {
979 out << "MotionEvent { action=" << MotionEvent::actionToString(event.getAction());
980 if (event.getActionButton() != 0) {
981 out << ", actionButton=" << std::to_string(event.getActionButton());
982 }
983 const size_t pointerCount = event.getPointerCount();
hupeng3aa5a51a2022-09-02 16:00:18 +0800984 LOG_ALWAYS_FATAL_IF(pointerCount > MAX_POINTERS, "Too many pointers : pointerCount = %zu",
985 pointerCount);
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000986 for (size_t i = 0; i < pointerCount; i++) {
987 out << ", id[" << i << "]=" << event.getPointerId(i);
988 float x = event.getX(i);
989 float y = event.getY(i);
990 if (x != 0 || y != 0) {
991 out << ", x[" << i << "]=" << x;
992 out << ", y[" << i << "]=" << y;
993 }
994 int toolType = event.getToolType(i);
995 if (toolType != AMOTION_EVENT_TOOL_TYPE_FINGER) {
996 out << ", toolType[" << i << "]=" << toolType;
997 }
998 }
999 if (event.getButtonState() != 0) {
1000 out << ", buttonState=" << event.getButtonState();
1001 }
1002 if (event.getClassification() != MotionClassification::NONE) {
1003 out << ", classification=" << motionClassificationToString(event.getClassification());
1004 }
1005 if (event.getMetaState() != 0) {
1006 out << ", metaState=" << event.getMetaState();
1007 }
1008 if (event.getEdgeFlags() != 0) {
1009 out << ", edgeFlags=" << event.getEdgeFlags();
1010 }
1011 if (pointerCount != 1) {
1012 out << ", pointerCount=" << pointerCount;
1013 }
1014 if (event.getHistorySize() != 0) {
1015 out << ", historySize=" << event.getHistorySize();
1016 }
1017 out << ", eventTime=" << event.getEventTime();
1018 out << ", downTime=" << event.getDownTime();
1019 out << ", deviceId=" << event.getDeviceId();
1020 out << ", source=" << inputEventSourceToString(event.getSource());
1021 out << ", displayId=" << event.getDisplayId();
1022 out << ", eventId=" << event.getId();
1023 out << "}";
1024 return out;
1025}
1026
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001027// --- FocusEvent ---
1028
Antonio Kantek3cfec7b2021-11-05 18:26:17 -07001029void FocusEvent::initialize(int32_t id, bool hasFocus) {
Garfield Tan4cc839f2020-01-24 11:26:14 -08001030 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -06001031 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001032 mHasFocus = hasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001033}
1034
1035void FocusEvent::initialize(const FocusEvent& from) {
1036 InputEvent::initialize(from);
1037 mHasFocus = from.mHasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001038}
Jeff Brown5912f952013-07-01 19:10:31 -07001039
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001040// --- CaptureEvent ---
1041
1042void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) {
1043 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1044 ADISPLAY_ID_NONE, INVALID_HMAC);
1045 mPointerCaptureEnabled = pointerCaptureEnabled;
1046}
1047
1048void CaptureEvent::initialize(const CaptureEvent& from) {
1049 InputEvent::initialize(from);
1050 mPointerCaptureEnabled = from.mPointerCaptureEnabled;
1051}
1052
arthurhung7632c332020-12-30 16:58:01 +08001053// --- DragEvent ---
1054
1055void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) {
1056 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1057 ADISPLAY_ID_NONE, INVALID_HMAC);
1058 mIsExiting = isExiting;
1059 mX = x;
1060 mY = y;
1061}
1062
1063void DragEvent::initialize(const DragEvent& from) {
1064 InputEvent::initialize(from);
1065 mIsExiting = from.mIsExiting;
1066 mX = from.mX;
1067 mY = from.mY;
1068}
1069
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001070// --- TouchModeEvent ---
1071
1072void TouchModeEvent::initialize(int32_t id, bool isInTouchMode) {
1073 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1074 ADISPLAY_ID_NONE, INVALID_HMAC);
1075 mIsInTouchMode = isInTouchMode;
1076}
1077
1078void TouchModeEvent::initialize(const TouchModeEvent& from) {
1079 InputEvent::initialize(from);
1080 mIsInTouchMode = from.mIsInTouchMode;
1081}
1082
Jeff Brown5912f952013-07-01 19:10:31 -07001083// --- PooledInputEventFactory ---
1084
1085PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
1086 mMaxPoolSize(maxPoolSize) {
1087}
1088
1089PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -07001090}
1091
1092KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001093 if (mKeyEventPool.empty()) {
1094 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001095 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001096 KeyEvent* event = mKeyEventPool.front().release();
1097 mKeyEventPool.pop();
1098 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001099}
1100
1101MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001102 if (mMotionEventPool.empty()) {
1103 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001104 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001105 MotionEvent* event = mMotionEventPool.front().release();
1106 mMotionEventPool.pop();
1107 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001108}
1109
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001110FocusEvent* PooledInputEventFactory::createFocusEvent() {
1111 if (mFocusEventPool.empty()) {
1112 return new FocusEvent();
1113 }
1114 FocusEvent* event = mFocusEventPool.front().release();
1115 mFocusEventPool.pop();
1116 return event;
1117}
1118
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001119CaptureEvent* PooledInputEventFactory::createCaptureEvent() {
1120 if (mCaptureEventPool.empty()) {
1121 return new CaptureEvent();
1122 }
1123 CaptureEvent* event = mCaptureEventPool.front().release();
1124 mCaptureEventPool.pop();
1125 return event;
1126}
1127
arthurhung7632c332020-12-30 16:58:01 +08001128DragEvent* PooledInputEventFactory::createDragEvent() {
1129 if (mDragEventPool.empty()) {
1130 return new DragEvent();
1131 }
1132 DragEvent* event = mDragEventPool.front().release();
1133 mDragEventPool.pop();
1134 return event;
1135}
1136
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001137TouchModeEvent* PooledInputEventFactory::createTouchModeEvent() {
1138 if (mTouchModeEventPool.empty()) {
1139 return new TouchModeEvent();
1140 }
1141 TouchModeEvent* event = mTouchModeEventPool.front().release();
1142 mTouchModeEventPool.pop();
1143 return event;
1144}
1145
Jeff Brown5912f952013-07-01 19:10:31 -07001146void PooledInputEventFactory::recycle(InputEvent* event) {
1147 switch (event->getType()) {
1148 case AINPUT_EVENT_TYPE_KEY:
1149 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001150 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -07001151 return;
1152 }
1153 break;
1154 case AINPUT_EVENT_TYPE_MOTION:
1155 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001156 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -07001157 return;
1158 }
1159 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001160 case AINPUT_EVENT_TYPE_FOCUS:
1161 if (mFocusEventPool.size() < mMaxPoolSize) {
1162 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
1163 return;
1164 }
1165 break;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001166 case AINPUT_EVENT_TYPE_CAPTURE:
1167 if (mCaptureEventPool.size() < mMaxPoolSize) {
1168 mCaptureEventPool.push(
1169 std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event)));
1170 return;
1171 }
1172 break;
arthurhung7632c332020-12-30 16:58:01 +08001173 case AINPUT_EVENT_TYPE_DRAG:
1174 if (mDragEventPool.size() < mMaxPoolSize) {
1175 mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event)));
1176 return;
1177 }
1178 break;
Antonio Kantekeb4a30c2021-09-28 17:49:49 -07001179 case AINPUT_EVENT_TYPE_TOUCH_MODE:
1180 if (mTouchModeEventPool.size() < mMaxPoolSize) {
1181 mTouchModeEventPool.push(
1182 std::unique_ptr<TouchModeEvent>(static_cast<TouchModeEvent*>(event)));
1183 return;
1184 }
1185 break;
Jeff Brown5912f952013-07-01 19:10:31 -07001186 }
1187 delete event;
1188}
1189
1190} // namespace android