blob: cf5a7e7b05c8fbc23d9c9339f75c7d182db269f2 [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 Vishniakou31977182022-09-30 08:51:23 -070025#include <android-base/file.h>
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +000026#include <android-base/logging.h>
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050027#include <android-base/stringprintf.h>
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +000028#include <cutils/compiler.h>
chaviw98318de2021-05-19 16:45:23 -050029#include <gui/constants.h>
Prabir Pradhan092f3a92021-11-25 10:53:27 -080030#include <input/DisplayViewport.h>
Jeff Brown5912f952013-07-01 19:10:31 -070031#include <input/Input.h>
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080032#include <input/InputDevice.h>
Michael Wright872db4f2014-04-22 15:03:51 -070033#include <input/InputEventLabels.h>
Jeff Brown5912f952013-07-01 19:10:31 -070034
Brett Chabotfaa986c2020-11-04 17:39:36 -080035#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -070036#include <binder/Parcel.h>
Brett Chabotfaa986c2020-11-04 17:39:36 -080037#endif
Siarhei Vishniakou63740b92022-10-20 10:28:08 -070038#if defined(__ANDROID__)
39#include <sys/random.h>
40#endif
Jeff Brown5912f952013-07-01 19:10:31 -070041
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050042using android::base::StringPrintf;
43
Jeff Brown5912f952013-07-01 19:10:31 -070044namespace android {
45
Prabir Pradhan6b384612021-05-14 16:56:25 -070046namespace {
47
48float transformAngle(const ui::Transform& transform, float angleRadians) {
49 // Construct and transform a vector oriented at the specified clockwise angle from vertical.
50 // Coordinate system: down is increasing Y, right is increasing X.
51 float x = sinf(angleRadians);
52 float y = -cosf(angleRadians);
53 vec2 transformedPoint = transform.transform(x, y);
54
55 // Determine how the origin is transformed by the matrix so that we
56 // can transform orientation vectors.
57 const vec2 origin = transform.transform(0, 0);
58
59 transformedPoint.x -= origin.x;
60 transformedPoint.y -= origin.y;
61
62 // Derive the transformed vector's clockwise angle from vertical.
Prabir Pradhand2b02672021-10-19 11:24:45 -070063 // The return value of atan2f is in range [-pi, pi] which conforms to the orientation API.
64 return atan2f(transformedPoint.x, -transformedPoint.y);
Prabir Pradhan6b384612021-05-14 16:56:25 -070065}
66
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070067bool shouldDisregardTransformation(uint32_t source) {
Prabir Pradhan258e2b92022-06-24 18:37:04 +000068 // Do not apply any transformations to axes from joysticks, touchpads, or relative mice.
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070069 return isFromSource(source, AINPUT_SOURCE_CLASS_JOYSTICK) ||
Prabir Pradhan258e2b92022-06-24 18:37:04 +000070 isFromSource(source, AINPUT_SOURCE_CLASS_POSITION) ||
71 isFromSource(source, AINPUT_SOURCE_MOUSE_RELATIVE);
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070072}
73
74bool shouldDisregardOffset(uint32_t source) {
Prabir Pradhan9f388812021-05-13 16:54:53 -070075 // Pointer events are the only type of events that refer to absolute coordinates on the display,
76 // so we should apply the entire window transform. For other types of events, we should make
77 // sure to not apply the window translation/offset.
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070078 return !isFromSource(source, AINPUT_SOURCE_CLASS_POINTER);
Prabir Pradhan9f388812021-05-13 16:54:53 -070079}
80
Prabir Pradhan6b384612021-05-14 16:56:25 -070081} // namespace
82
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -080083const char* motionClassificationToString(MotionClassification classification) {
84 switch (classification) {
85 case MotionClassification::NONE:
86 return "NONE";
87 case MotionClassification::AMBIGUOUS_GESTURE:
88 return "AMBIGUOUS_GESTURE";
89 case MotionClassification::DEEP_PRESS:
90 return "DEEP_PRESS";
Harry Cutts2800fb02022-09-15 13:49:23 +000091 case MotionClassification::TWO_FINGER_SWIPE:
92 return "TWO_FINGER_SWIPE";
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -080093 }
94}
95
Siarhei Vishniakoud5fe5182022-07-20 23:28:40 +000096const char* motionToolTypeToString(int32_t toolType) {
97 switch (toolType) {
98 case AMOTION_EVENT_TOOL_TYPE_UNKNOWN:
99 return "UNKNOWN";
100 case AMOTION_EVENT_TOOL_TYPE_FINGER:
101 return "FINGER";
102 case AMOTION_EVENT_TOOL_TYPE_STYLUS:
103 return "STYLUS";
104 case AMOTION_EVENT_TOOL_TYPE_MOUSE:
105 return "MOUSE";
106 case AMOTION_EVENT_TOOL_TYPE_ERASER:
107 return "ERASER";
108 case AMOTION_EVENT_TOOL_TYPE_PALM:
109 return "PALM";
110 default:
111 return "INVALID";
112 }
113}
114
Garfield Tan84b087e2020-01-23 10:49:05 -0800115// --- IdGenerator ---
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700116#if defined(__ANDROID__)
117[[maybe_unused]]
118#endif
119static status_t
120getRandomBytes(uint8_t* data, size_t size) {
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700121 int ret = TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY | O_CLOEXEC | O_NOFOLLOW));
122 if (ret == -1) {
123 return -errno;
124 }
125
126 base::unique_fd fd(ret);
127 if (!base::ReadFully(fd, data, size)) {
128 return -errno;
129 }
130 return OK;
131}
132
Garfield Tan84b087e2020-01-23 10:49:05 -0800133IdGenerator::IdGenerator(Source source) : mSource(source) {}
134
135int32_t IdGenerator::nextId() const {
136 constexpr uint32_t SEQUENCE_NUMBER_MASK = ~SOURCE_MASK;
137 int32_t id = 0;
138
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700139#if defined(__ANDROID__)
140 // On device, prefer 'getrandom' to '/dev/urandom' because it's faster.
141 constexpr size_t BUF_LEN = sizeof(id);
142 size_t totalBytes = 0;
143 while (totalBytes < BUF_LEN) {
144 ssize_t bytes = TEMP_FAILURE_RETRY(getrandom(&id, BUF_LEN, GRND_NONBLOCK));
145 if (CC_UNLIKELY(bytes < 0)) {
146 ALOGW("Failed to fill in random number for sequence number: %s.", strerror(errno));
147 id = 0;
148 break;
149 }
150 totalBytes += bytes;
151 }
152#else
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700153#if defined(__linux__)
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700154 // On host, <sys/random.h> / GRND_NONBLOCK is not available
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700155 while (true) {
156 status_t result = getRandomBytes(reinterpret_cast<uint8_t*>(&id), sizeof(id));
157 if (result == OK) {
Garfield Tan84b087e2020-01-23 10:49:05 -0800158 break;
159 }
Garfield Tan84b087e2020-01-23 10:49:05 -0800160 }
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700161#endif // __linux__
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700162#endif // __ANDROID__
Garfield Tan84b087e2020-01-23 10:49:05 -0800163 return (id & SEQUENCE_NUMBER_MASK) | static_cast<int32_t>(mSource);
164}
165
Jeff Brown5912f952013-07-01 19:10:31 -0700166// --- InputEvent ---
167
Prabir Pradhande69f8a2021-11-18 16:40:34 +0000168vec2 transformWithoutTranslation(const ui::Transform& transform, const vec2& xy) {
169 const vec2 transformedXy = transform.transform(xy);
170 const vec2 transformedOrigin = transform.transform(0, 0);
171 return transformedXy - transformedOrigin;
172}
173
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800174const char* inputEventTypeToString(int32_t type) {
175 switch (type) {
176 case AINPUT_EVENT_TYPE_KEY: {
177 return "KEY";
178 }
179 case AINPUT_EVENT_TYPE_MOTION: {
180 return "MOTION";
181 }
182 case AINPUT_EVENT_TYPE_FOCUS: {
183 return "FOCUS";
184 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800185 case AINPUT_EVENT_TYPE_CAPTURE: {
186 return "CAPTURE";
187 }
arthurhung7632c332020-12-30 16:58:01 +0800188 case AINPUT_EVENT_TYPE_DRAG: {
189 return "DRAG";
190 }
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700191 case AINPUT_EVENT_TYPE_TOUCH_MODE: {
192 return "TOUCH_MODE";
193 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800194 }
195 return "UNKNOWN";
196}
197
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800198std::string inputEventSourceToString(int32_t source) {
199 if (source == AINPUT_SOURCE_UNKNOWN) {
200 return "UNKNOWN";
201 }
202 if (source == static_cast<int32_t>(AINPUT_SOURCE_ANY)) {
203 return "ANY";
204 }
205 static const std::map<int32_t, const char*> SOURCES{
206 {AINPUT_SOURCE_KEYBOARD, "KEYBOARD"},
207 {AINPUT_SOURCE_DPAD, "DPAD"},
208 {AINPUT_SOURCE_GAMEPAD, "GAMEPAD"},
209 {AINPUT_SOURCE_TOUCHSCREEN, "TOUCHSCREEN"},
210 {AINPUT_SOURCE_MOUSE, "MOUSE"},
211 {AINPUT_SOURCE_STYLUS, "STYLUS"},
212 {AINPUT_SOURCE_BLUETOOTH_STYLUS, "BLUETOOTH_STYLUS"},
213 {AINPUT_SOURCE_TRACKBALL, "TRACKBALL"},
214 {AINPUT_SOURCE_MOUSE_RELATIVE, "MOUSE_RELATIVE"},
215 {AINPUT_SOURCE_TOUCHPAD, "TOUCHPAD"},
216 {AINPUT_SOURCE_TOUCH_NAVIGATION, "TOUCH_NAVIGATION"},
217 {AINPUT_SOURCE_JOYSTICK, "JOYSTICK"},
218 {AINPUT_SOURCE_HDMI, "HDMI"},
219 {AINPUT_SOURCE_SENSOR, "SENSOR"},
220 {AINPUT_SOURCE_ROTARY_ENCODER, "ROTARY_ENCODER"},
221 };
222 std::string result;
223 for (const auto& [source_entry, str] : SOURCES) {
224 if ((source & source_entry) == source_entry) {
225 if (!result.empty()) {
226 result += " | ";
227 }
228 result += str;
229 }
230 }
231 if (result.empty()) {
232 result = StringPrintf("0x%08x", source);
233 }
234 return result;
235}
236
237bool isFromSource(uint32_t source, uint32_t test) {
238 return (source & test) == test;
239}
240
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800241VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
242 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
243 event.getSource(), event.getDisplayId()},
244 event.getAction(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800245 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800246 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800247 event.getKeyCode(),
248 event.getScanCode(),
249 event.getMetaState(),
250 event.getRepeatCount()};
251}
252
253VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
254 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
255 event.getSource(), event.getDisplayId()},
256 event.getRawX(0),
257 event.getRawY(0),
258 event.getActionMasked(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800259 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800260 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800261 event.getMetaState(),
262 event.getButtonState()};
263}
264
Garfield Tan4cc839f2020-01-24 11:26:14 -0800265void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600266 std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800267 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700268 mDeviceId = deviceId;
269 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100270 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600271 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700272}
273
274void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800275 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700276 mDeviceId = from.mDeviceId;
277 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100278 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600279 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700280}
281
Garfield Tan4cc839f2020-01-24 11:26:14 -0800282int32_t InputEvent::nextId() {
283 static IdGenerator idGen(IdGenerator::Source::OTHER);
284 return idGen.nextId();
285}
286
Jeff Brown5912f952013-07-01 19:10:31 -0700287// --- KeyEvent ---
288
Michael Wright872db4f2014-04-22 15:03:51 -0700289const char* KeyEvent::getLabel(int32_t keyCode) {
Chris Ye4958d062020-08-20 13:21:10 -0700290 return InputEventLookup::getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700291}
292
Michael Wright872db4f2014-04-22 15:03:51 -0700293int32_t KeyEvent::getKeyCodeFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700294 return InputEventLookup::getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700295}
296
Garfield Tan4cc839f2020-01-24 11:26:14 -0800297void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600298 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
299 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
300 nsecs_t downTime, nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800301 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700302 mAction = action;
303 mFlags = flags;
304 mKeyCode = keyCode;
305 mScanCode = scanCode;
306 mMetaState = metaState;
307 mRepeatCount = repeatCount;
308 mDownTime = downTime;
309 mEventTime = eventTime;
310}
311
312void KeyEvent::initialize(const KeyEvent& from) {
313 InputEvent::initialize(from);
314 mAction = from.mAction;
315 mFlags = from.mFlags;
316 mKeyCode = from.mKeyCode;
317 mScanCode = from.mScanCode;
318 mMetaState = from.mMetaState;
319 mRepeatCount = from.mRepeatCount;
320 mDownTime = from.mDownTime;
321 mEventTime = from.mEventTime;
322}
323
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700324const char* KeyEvent::actionToString(int32_t action) {
325 // Convert KeyEvent action to string
326 switch (action) {
327 case AKEY_EVENT_ACTION_DOWN:
328 return "DOWN";
329 case AKEY_EVENT_ACTION_UP:
330 return "UP";
331 case AKEY_EVENT_ACTION_MULTIPLE:
332 return "MULTIPLE";
333 }
334 return "UNKNOWN";
335}
Jeff Brown5912f952013-07-01 19:10:31 -0700336
337// --- PointerCoords ---
338
339float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700340 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700341 return 0;
342 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700343 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700344}
345
346status_t PointerCoords::setAxisValue(int32_t axis, float value) {
347 if (axis < 0 || axis > 63) {
348 return NAME_NOT_FOUND;
349 }
350
Michael Wright38dcdff2014-03-19 12:06:10 -0700351 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
352 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700353 if (value == 0) {
354 return OK; // axes with value 0 do not need to be stored
355 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700356
357 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700358 if (count >= MAX_AXES) {
359 tooManyAxes(axis);
360 return NO_MEMORY;
361 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700362 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700363 for (uint32_t i = count; i > index; i--) {
364 values[i] = values[i - 1];
365 }
366 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700367
Jeff Brown5912f952013-07-01 19:10:31 -0700368 values[index] = value;
369 return OK;
370}
371
372static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
373 float value = c.getAxisValue(axis);
374 if (value != 0) {
375 c.setAxisValue(axis, value * scaleFactor);
376 }
377}
378
Robert Carre07e1032018-11-26 12:55:53 -0800379void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700380 // No need to scale pressure or size since they are normalized.
381 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800382
383 // If there is a global scale factor, it is included in the windowX/YScale
384 // so we don't need to apply it twice to the X/Y axes.
385 // However we don't want to apply any windowXYScale not included in the global scale
386 // to the TOUCH_MAJOR/MINOR coordinates.
387 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
388 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
389 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
390 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
391 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
392 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
Prabir Pradhanc6523582021-05-14 18:02:55 -0700393 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_X, windowXScale);
394 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_Y, windowYScale);
Robert Carre07e1032018-11-26 12:55:53 -0800395}
396
Brett Chabotfaa986c2020-11-04 17:39:36 -0800397#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700398status_t PointerCoords::readFromParcel(Parcel* parcel) {
399 bits = parcel->readInt64();
400
Michael Wright38dcdff2014-03-19 12:06:10 -0700401 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700402 if (count > MAX_AXES) {
403 return BAD_VALUE;
404 }
405
406 for (uint32_t i = 0; i < count; i++) {
407 values[i] = parcel->readFloat();
408 }
409 return OK;
410}
411
412status_t PointerCoords::writeToParcel(Parcel* parcel) const {
413 parcel->writeInt64(bits);
414
Michael Wright38dcdff2014-03-19 12:06:10 -0700415 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700416 for (uint32_t i = 0; i < count; i++) {
417 parcel->writeFloat(values[i]);
418 }
419 return OK;
420}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800421#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700422
423void PointerCoords::tooManyAxes(int axis) {
424 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
425 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
426}
427
428bool PointerCoords::operator==(const PointerCoords& other) const {
429 if (bits != other.bits) {
430 return false;
431 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700432 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700433 for (uint32_t i = 0; i < count; i++) {
434 if (values[i] != other.values[i]) {
435 return false;
436 }
437 }
438 return true;
439}
440
chaviwc01e1372020-07-01 12:37:31 -0700441void PointerCoords::transform(const ui::Transform& transform) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700442 const vec2 xy = transform.transform(getXYValue());
443 setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
444 setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
445
Prabir Pradhanc6523582021-05-14 18:02:55 -0700446 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_X) ||
447 BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_Y)) {
448 const ui::Transform rotation(transform.getOrientation());
449 const vec2 relativeXy = rotation.transform(getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
450 getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
451 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
452 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
453 }
454
Prabir Pradhan6b384612021-05-14 16:56:25 -0700455 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) {
456 const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
457 setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val));
458 }
chaviwc01e1372020-07-01 12:37:31 -0700459}
Jeff Brown5912f952013-07-01 19:10:31 -0700460
461// --- PointerProperties ---
462
463bool PointerProperties::operator==(const PointerProperties& other) const {
464 return id == other.id
465 && toolType == other.toolType;
466}
467
468void PointerProperties::copyFrom(const PointerProperties& other) {
469 id = other.id;
470 toolType = other.toolType;
471}
472
473
474// --- MotionEvent ---
475
Garfield Tan4cc839f2020-01-24 11:26:14 -0800476void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600477 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
478 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700479 int32_t buttonState, MotionClassification classification,
480 const ui::Transform& transform, float xPrecision, float yPrecision,
Evan Rosky84f07f02021-04-16 10:42:42 -0700481 float rawXCursorPosition, float rawYCursorPosition,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700482 const ui::Transform& rawTransform, nsecs_t downTime, nsecs_t eventTime,
Evan Rosky09576692021-07-01 12:22:09 -0700483 size_t pointerCount, const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700484 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800485 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700486 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100487 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700488 mFlags = flags;
489 mEdgeFlags = edgeFlags;
490 mMetaState = metaState;
491 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800492 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700493 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700494 mXPrecision = xPrecision;
495 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700496 mRawXCursorPosition = rawXCursorPosition;
497 mRawYCursorPosition = rawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700498 mRawTransform = rawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700499 mDownTime = downTime;
500 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800501 mPointerProperties.insert(mPointerProperties.end(), &pointerProperties[0],
502 &pointerProperties[pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700503 mSampleEventTimes.clear();
504 mSamplePointerCoords.clear();
505 addSample(eventTime, pointerCoords);
506}
507
508void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800509 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
510 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700511 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100512 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700513 mFlags = other->mFlags;
514 mEdgeFlags = other->mEdgeFlags;
515 mMetaState = other->mMetaState;
516 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800517 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700518 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700519 mXPrecision = other->mXPrecision;
520 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700521 mRawXCursorPosition = other->mRawXCursorPosition;
522 mRawYCursorPosition = other->mRawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700523 mRawTransform = other->mRawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700524 mDownTime = other->mDownTime;
525 mPointerProperties = other->mPointerProperties;
526
527 if (keepHistory) {
528 mSampleEventTimes = other->mSampleEventTimes;
529 mSamplePointerCoords = other->mSamplePointerCoords;
530 } else {
531 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500532 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700533 mSamplePointerCoords.clear();
534 size_t pointerCount = other->getPointerCount();
535 size_t historySize = other->getHistorySize();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800536 mSamplePointerCoords
537 .insert(mSamplePointerCoords.end(),
538 &other->mSamplePointerCoords[historySize * pointerCount],
539 &other->mSamplePointerCoords[historySize * pointerCount + pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700540 }
541}
542
543void MotionEvent::addSample(
544 int64_t eventTime,
545 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500546 mSampleEventTimes.push_back(eventTime);
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800547 mSamplePointerCoords.insert(mSamplePointerCoords.end(), &pointerCoords[0],
548 &pointerCoords[getPointerCount()]);
Jeff Brown5912f952013-07-01 19:10:31 -0700549}
550
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800551int MotionEvent::getSurfaceRotation() const {
552 // The surface rotation is the rotation from the window's coordinate space to that of the
553 // display. Since the event's transform takes display space coordinates to window space, the
554 // returned surface rotation is the inverse of the rotation for the surface.
555 switch (mTransform.getOrientation()) {
556 case ui::Transform::ROT_0:
557 return DISPLAY_ORIENTATION_0;
558 case ui::Transform::ROT_90:
559 return DISPLAY_ORIENTATION_270;
560 case ui::Transform::ROT_180:
561 return DISPLAY_ORIENTATION_180;
562 case ui::Transform::ROT_270:
563 return DISPLAY_ORIENTATION_90;
564 default:
565 return -1;
566 }
567}
568
Garfield Tan00f511d2019-06-12 16:55:40 -0700569float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700570 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
571 return vals.x;
Garfield Tan00f511d2019-06-12 16:55:40 -0700572}
573
574float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700575 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
576 return vals.y;
Garfield Tan00f511d2019-06-12 16:55:40 -0700577}
578
Garfield Tan937bb832019-07-25 17:48:31 -0700579void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700580 ui::Transform inverse = mTransform.inverse();
581 vec2 vals = inverse.transform(x, y);
582 mRawXCursorPosition = vals.x;
583 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700584}
585
Jeff Brown5912f952013-07-01 19:10:31 -0700586const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000587 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
588 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
589 }
590 const size_t position = getHistorySize() * getPointerCount() + pointerIndex;
591 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
592 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
593 }
594 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700595}
596
597float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
Evan Rosky84f07f02021-04-16 10:42:42 -0700598 return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700599}
600
601float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700602 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700603}
604
605const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
606 size_t pointerIndex, size_t historicalIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000607 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
608 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
609 }
610 if (CC_UNLIKELY(historicalIndex < 0 || historicalIndex > getHistorySize())) {
611 LOG(FATAL) << __func__ << ": Invalid historical index " << historicalIndex << " for "
612 << *this;
613 }
614 const size_t position = historicalIndex * getPointerCount() + pointerIndex;
615 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
616 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
617 }
618 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700619}
620
621float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan6b384612021-05-14 16:56:25 -0700622 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700623 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
624 return calculateTransformedAxisValue(axis, mSource, mRawTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700625}
626
627float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan9f388812021-05-13 16:54:53 -0700628 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700629 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
630 return calculateTransformedAxisValue(axis, mSource, mTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700631}
632
633ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
634 size_t pointerCount = mPointerProperties.size();
635 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800636 if (mPointerProperties[i].id == pointerId) {
Jeff Brown5912f952013-07-01 19:10:31 -0700637 return i;
638 }
639 }
640 return -1;
641}
642
643void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700644 float currXOffset = mTransform.tx();
645 float currYOffset = mTransform.ty();
646 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700647}
648
Robert Carre07e1032018-11-26 12:55:53 -0800649void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700650 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700651 mRawTransform.set(mRawTransform.tx() * globalScaleFactor,
652 mRawTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800653 mXPrecision *= globalScaleFactor;
654 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700655
656 size_t numSamples = mSamplePointerCoords.size();
657 for (size_t i = 0; i < numSamples; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800658 mSamplePointerCoords[i].scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700659 }
660}
661
chaviw9eaa22c2020-07-01 16:21:27 -0700662void MotionEvent::transform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700663 // We want to preserve the raw axes values stored in the PointerCoords, so we just update the
664 // transform using the values passed in.
chaviw9eaa22c2020-07-01 16:21:27 -0700665 ui::Transform newTransform;
666 newTransform.set(matrix);
667 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700668}
669
Evan Roskyd4d4d802021-05-03 20:12:21 -0700670void MotionEvent::applyTransform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700671 ui::Transform transform;
672 transform.set(matrix);
Evan Roskyd4d4d802021-05-03 20:12:21 -0700673
674 // Apply the transformation to all samples.
Prabir Pradhan6b384612021-05-14 16:56:25 -0700675 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
676 [&transform](PointerCoords& c) { c.transform(transform); });
Prabir Pradhan4b19bd02021-06-01 17:34:59 -0700677
678 if (mRawXCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION &&
679 mRawYCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) {
680 const vec2 cursor = transform.transform(mRawXCursorPosition, mRawYCursorPosition);
681 mRawXCursorPosition = cursor.x;
682 mRawYCursorPosition = cursor.y;
683 }
Evan Roskyd4d4d802021-05-03 20:12:21 -0700684}
685
Brett Chabotfaa986c2020-11-04 17:39:36 -0800686#ifdef __linux__
chaviw9eaa22c2020-07-01 16:21:27 -0700687static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
688 float dsdx, dtdx, tx, dtdy, dsdy, ty;
689 status_t status = parcel.readFloat(&dsdx);
690 status |= parcel.readFloat(&dtdx);
691 status |= parcel.readFloat(&tx);
692 status |= parcel.readFloat(&dtdy);
693 status |= parcel.readFloat(&dsdy);
694 status |= parcel.readFloat(&ty);
695
696 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
697 return status;
698}
699
700static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
701 status_t status = parcel.writeFloat(transform.dsdx());
702 status |= parcel.writeFloat(transform.dtdx());
703 status |= parcel.writeFloat(transform.tx());
704 status |= parcel.writeFloat(transform.dtdy());
705 status |= parcel.writeFloat(transform.dsdy());
706 status |= parcel.writeFloat(transform.ty());
707 return status;
708}
709
Jeff Brown5912f952013-07-01 19:10:31 -0700710status_t MotionEvent::readFromParcel(Parcel* parcel) {
711 size_t pointerCount = parcel->readInt32();
712 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800713 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
714 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700715 return BAD_VALUE;
716 }
717
Garfield Tan4cc839f2020-01-24 11:26:14 -0800718 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700719 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600720 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800721 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600722 std::vector<uint8_t> hmac;
723 status_t result = parcel->readByteVector(&hmac);
724 if (result != OK || hmac.size() != 32) {
725 return BAD_VALUE;
726 }
727 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700728 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100729 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700730 mFlags = parcel->readInt32();
731 mEdgeFlags = parcel->readInt32();
732 mMetaState = parcel->readInt32();
733 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800734 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700735
736 result = android::readFromParcel(mTransform, *parcel);
737 if (result != OK) {
738 return result;
739 }
Jeff Brown5912f952013-07-01 19:10:31 -0700740 mXPrecision = parcel->readFloat();
741 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700742 mRawXCursorPosition = parcel->readFloat();
743 mRawYCursorPosition = parcel->readFloat();
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700744
745 result = android::readFromParcel(mRawTransform, *parcel);
746 if (result != OK) {
747 return result;
748 }
Jeff Brown5912f952013-07-01 19:10:31 -0700749 mDownTime = parcel->readInt64();
750
751 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800752 mPointerProperties.reserve(pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700753 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500754 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700755 mSamplePointerCoords.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800756 mSamplePointerCoords.reserve(sampleCount * pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700757
758 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800759 mPointerProperties.push_back({});
760 PointerProperties& properties = mPointerProperties.back();
Jeff Brown5912f952013-07-01 19:10:31 -0700761 properties.id = parcel->readInt32();
762 properties.toolType = parcel->readInt32();
763 }
764
Dan Austinc94fc452015-09-22 14:22:41 -0700765 while (sampleCount > 0) {
766 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500767 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700768 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800769 mSamplePointerCoords.push_back({});
770 status_t status = mSamplePointerCoords.back().readFromParcel(parcel);
Jeff Brown5912f952013-07-01 19:10:31 -0700771 if (status) {
772 return status;
773 }
774 }
775 }
776 return OK;
777}
778
779status_t MotionEvent::writeToParcel(Parcel* parcel) const {
780 size_t pointerCount = mPointerProperties.size();
781 size_t sampleCount = mSampleEventTimes.size();
782
783 parcel->writeInt32(pointerCount);
784 parcel->writeInt32(sampleCount);
785
Garfield Tan4cc839f2020-01-24 11:26:14 -0800786 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700787 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600788 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800789 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600790 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
791 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700792 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100793 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700794 parcel->writeInt32(mFlags);
795 parcel->writeInt32(mEdgeFlags);
796 parcel->writeInt32(mMetaState);
797 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800798 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700799
800 status_t result = android::writeToParcel(mTransform, *parcel);
801 if (result != OK) {
802 return result;
803 }
Jeff Brown5912f952013-07-01 19:10:31 -0700804 parcel->writeFloat(mXPrecision);
805 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700806 parcel->writeFloat(mRawXCursorPosition);
807 parcel->writeFloat(mRawYCursorPosition);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700808
809 result = android::writeToParcel(mRawTransform, *parcel);
810 if (result != OK) {
811 return result;
812 }
Jeff Brown5912f952013-07-01 19:10:31 -0700813 parcel->writeInt64(mDownTime);
814
815 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800816 const PointerProperties& properties = mPointerProperties[i];
Jeff Brown5912f952013-07-01 19:10:31 -0700817 parcel->writeInt32(properties.id);
818 parcel->writeInt32(properties.toolType);
819 }
820
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800821 const PointerCoords* pc = mSamplePointerCoords.data();
Jeff Brown5912f952013-07-01 19:10:31 -0700822 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500823 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700824 for (size_t i = 0; i < pointerCount; i++) {
825 status_t status = (pc++)->writeToParcel(parcel);
826 if (status) {
827 return status;
828 }
829 }
830 }
831 return OK;
832}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800833#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700834
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600835bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700836 if (isFromSource(source, AINPUT_SOURCE_CLASS_POINTER)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700837 // Specifically excludes HOVER_MOVE and SCROLL.
838 switch (action & AMOTION_EVENT_ACTION_MASK) {
839 case AMOTION_EVENT_ACTION_DOWN:
840 case AMOTION_EVENT_ACTION_MOVE:
841 case AMOTION_EVENT_ACTION_UP:
842 case AMOTION_EVENT_ACTION_POINTER_DOWN:
843 case AMOTION_EVENT_ACTION_POINTER_UP:
844 case AMOTION_EVENT_ACTION_CANCEL:
845 case AMOTION_EVENT_ACTION_OUTSIDE:
846 return true;
847 }
848 }
849 return false;
850}
851
Michael Wright872db4f2014-04-22 15:03:51 -0700852const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700853 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700854}
855
856int32_t MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700857 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700858}
859
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500860std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700861 // Convert MotionEvent action to string
862 switch (action & AMOTION_EVENT_ACTION_MASK) {
863 case AMOTION_EVENT_ACTION_DOWN:
864 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700865 case AMOTION_EVENT_ACTION_UP:
866 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500867 case AMOTION_EVENT_ACTION_MOVE:
868 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700869 case AMOTION_EVENT_ACTION_CANCEL:
870 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500871 case AMOTION_EVENT_ACTION_OUTSIDE:
872 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700873 case AMOTION_EVENT_ACTION_POINTER_DOWN:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000874 return StringPrintf("POINTER_DOWN(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700875 case AMOTION_EVENT_ACTION_POINTER_UP:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000876 return StringPrintf("POINTER_UP(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500877 case AMOTION_EVENT_ACTION_HOVER_MOVE:
878 return "HOVER_MOVE";
879 case AMOTION_EVENT_ACTION_SCROLL:
880 return "SCROLL";
881 case AMOTION_EVENT_ACTION_HOVER_ENTER:
882 return "HOVER_ENTER";
883 case AMOTION_EVENT_ACTION_HOVER_EXIT:
884 return "HOVER_EXIT";
885 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
886 return "BUTTON_PRESS";
887 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
888 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700889 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500890 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700891}
892
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700893// Apply the given transformation to the point without checking whether the entire transform
894// should be disregarded altogether for the provided source.
895static inline vec2 calculateTransformedXYUnchecked(uint32_t source, const ui::Transform& transform,
896 const vec2& xy) {
897 return shouldDisregardOffset(source) ? transformWithoutTranslation(transform, xy)
898 : transform.transform(xy);
899}
900
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700901vec2 MotionEvent::calculateTransformedXY(uint32_t source, const ui::Transform& transform,
902 const vec2& xy) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700903 if (shouldDisregardTransformation(source)) {
904 return xy;
905 }
906 return calculateTransformedXYUnchecked(source, transform, xy);
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700907}
908
Prabir Pradhan8e6ce222022-02-24 09:08:54 -0800909// Keep in sync with calculateTransformedCoords.
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700910float MotionEvent::calculateTransformedAxisValue(int32_t axis, uint32_t source,
911 const ui::Transform& transform,
912 const PointerCoords& coords) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700913 if (shouldDisregardTransformation(source)) {
914 return coords.getAxisValue(axis);
915 }
916
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700917 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700918 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700919 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
920 return xy[axis];
921 }
922
923 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
924 const vec2 relativeXy =
925 transformWithoutTranslation(transform,
926 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
927 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
928 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
929 }
930
931 if (axis == AMOTION_EVENT_AXIS_ORIENTATION) {
932 return transformAngle(transform, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION));
933 }
934
935 return coords.getAxisValue(axis);
936}
937
Prabir Pradhan8e6ce222022-02-24 09:08:54 -0800938// Keep in sync with calculateTransformedAxisValue. This is an optimization of
939// calculateTransformedAxisValue for all PointerCoords axes.
940PointerCoords MotionEvent::calculateTransformedCoords(uint32_t source,
941 const ui::Transform& transform,
942 const PointerCoords& coords) {
943 if (shouldDisregardTransformation(source)) {
944 return coords;
945 }
946 PointerCoords out = coords;
947
948 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
949 out.setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
950 out.setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
951
952 const vec2 relativeXy =
953 transformWithoutTranslation(transform,
954 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
955 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
956 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
957 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
958
959 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
960 transformAngle(transform,
961 coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)));
962
963 return out;
964}
965
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000966std::ostream& operator<<(std::ostream& out, const MotionEvent& event) {
967 out << "MotionEvent { action=" << MotionEvent::actionToString(event.getAction());
968 if (event.getActionButton() != 0) {
969 out << ", actionButton=" << std::to_string(event.getActionButton());
970 }
971 const size_t pointerCount = event.getPointerCount();
hupeng3aa5a51a2022-09-02 16:00:18 +0800972 LOG_ALWAYS_FATAL_IF(pointerCount > MAX_POINTERS, "Too many pointers : pointerCount = %zu",
973 pointerCount);
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000974 for (size_t i = 0; i < pointerCount; i++) {
975 out << ", id[" << i << "]=" << event.getPointerId(i);
976 float x = event.getX(i);
977 float y = event.getY(i);
978 if (x != 0 || y != 0) {
979 out << ", x[" << i << "]=" << x;
980 out << ", y[" << i << "]=" << y;
981 }
982 int toolType = event.getToolType(i);
983 if (toolType != AMOTION_EVENT_TOOL_TYPE_FINGER) {
984 out << ", toolType[" << i << "]=" << toolType;
985 }
986 }
987 if (event.getButtonState() != 0) {
988 out << ", buttonState=" << event.getButtonState();
989 }
990 if (event.getClassification() != MotionClassification::NONE) {
991 out << ", classification=" << motionClassificationToString(event.getClassification());
992 }
993 if (event.getMetaState() != 0) {
994 out << ", metaState=" << event.getMetaState();
995 }
996 if (event.getEdgeFlags() != 0) {
997 out << ", edgeFlags=" << event.getEdgeFlags();
998 }
999 if (pointerCount != 1) {
1000 out << ", pointerCount=" << pointerCount;
1001 }
1002 if (event.getHistorySize() != 0) {
1003 out << ", historySize=" << event.getHistorySize();
1004 }
1005 out << ", eventTime=" << event.getEventTime();
1006 out << ", downTime=" << event.getDownTime();
1007 out << ", deviceId=" << event.getDeviceId();
1008 out << ", source=" << inputEventSourceToString(event.getSource());
1009 out << ", displayId=" << event.getDisplayId();
1010 out << ", eventId=" << event.getId();
1011 out << "}";
1012 return out;
1013}
1014
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001015// --- FocusEvent ---
1016
Antonio Kantek3cfec7b2021-11-05 18:26:17 -07001017void FocusEvent::initialize(int32_t id, bool hasFocus) {
Garfield Tan4cc839f2020-01-24 11:26:14 -08001018 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -06001019 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001020 mHasFocus = hasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001021}
1022
1023void FocusEvent::initialize(const FocusEvent& from) {
1024 InputEvent::initialize(from);
1025 mHasFocus = from.mHasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001026}
Jeff Brown5912f952013-07-01 19:10:31 -07001027
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001028// --- CaptureEvent ---
1029
1030void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) {
1031 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1032 ADISPLAY_ID_NONE, INVALID_HMAC);
1033 mPointerCaptureEnabled = pointerCaptureEnabled;
1034}
1035
1036void CaptureEvent::initialize(const CaptureEvent& from) {
1037 InputEvent::initialize(from);
1038 mPointerCaptureEnabled = from.mPointerCaptureEnabled;
1039}
1040
arthurhung7632c332020-12-30 16:58:01 +08001041// --- DragEvent ---
1042
1043void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) {
1044 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1045 ADISPLAY_ID_NONE, INVALID_HMAC);
1046 mIsExiting = isExiting;
1047 mX = x;
1048 mY = y;
1049}
1050
1051void DragEvent::initialize(const DragEvent& from) {
1052 InputEvent::initialize(from);
1053 mIsExiting = from.mIsExiting;
1054 mX = from.mX;
1055 mY = from.mY;
1056}
1057
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001058// --- TouchModeEvent ---
1059
1060void TouchModeEvent::initialize(int32_t id, bool isInTouchMode) {
1061 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1062 ADISPLAY_ID_NONE, INVALID_HMAC);
1063 mIsInTouchMode = isInTouchMode;
1064}
1065
1066void TouchModeEvent::initialize(const TouchModeEvent& from) {
1067 InputEvent::initialize(from);
1068 mIsInTouchMode = from.mIsInTouchMode;
1069}
1070
Jeff Brown5912f952013-07-01 19:10:31 -07001071// --- PooledInputEventFactory ---
1072
1073PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
1074 mMaxPoolSize(maxPoolSize) {
1075}
1076
1077PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -07001078}
1079
1080KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001081 if (mKeyEventPool.empty()) {
1082 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001083 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001084 KeyEvent* event = mKeyEventPool.front().release();
1085 mKeyEventPool.pop();
1086 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001087}
1088
1089MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001090 if (mMotionEventPool.empty()) {
1091 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001092 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001093 MotionEvent* event = mMotionEventPool.front().release();
1094 mMotionEventPool.pop();
1095 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001096}
1097
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001098FocusEvent* PooledInputEventFactory::createFocusEvent() {
1099 if (mFocusEventPool.empty()) {
1100 return new FocusEvent();
1101 }
1102 FocusEvent* event = mFocusEventPool.front().release();
1103 mFocusEventPool.pop();
1104 return event;
1105}
1106
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001107CaptureEvent* PooledInputEventFactory::createCaptureEvent() {
1108 if (mCaptureEventPool.empty()) {
1109 return new CaptureEvent();
1110 }
1111 CaptureEvent* event = mCaptureEventPool.front().release();
1112 mCaptureEventPool.pop();
1113 return event;
1114}
1115
arthurhung7632c332020-12-30 16:58:01 +08001116DragEvent* PooledInputEventFactory::createDragEvent() {
1117 if (mDragEventPool.empty()) {
1118 return new DragEvent();
1119 }
1120 DragEvent* event = mDragEventPool.front().release();
1121 mDragEventPool.pop();
1122 return event;
1123}
1124
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001125TouchModeEvent* PooledInputEventFactory::createTouchModeEvent() {
1126 if (mTouchModeEventPool.empty()) {
1127 return new TouchModeEvent();
1128 }
1129 TouchModeEvent* event = mTouchModeEventPool.front().release();
1130 mTouchModeEventPool.pop();
1131 return event;
1132}
1133
Jeff Brown5912f952013-07-01 19:10:31 -07001134void PooledInputEventFactory::recycle(InputEvent* event) {
1135 switch (event->getType()) {
1136 case AINPUT_EVENT_TYPE_KEY:
1137 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001138 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -07001139 return;
1140 }
1141 break;
1142 case AINPUT_EVENT_TYPE_MOTION:
1143 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001144 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -07001145 return;
1146 }
1147 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001148 case AINPUT_EVENT_TYPE_FOCUS:
1149 if (mFocusEventPool.size() < mMaxPoolSize) {
1150 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
1151 return;
1152 }
1153 break;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001154 case AINPUT_EVENT_TYPE_CAPTURE:
1155 if (mCaptureEventPool.size() < mMaxPoolSize) {
1156 mCaptureEventPool.push(
1157 std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event)));
1158 return;
1159 }
1160 break;
arthurhung7632c332020-12-30 16:58:01 +08001161 case AINPUT_EVENT_TYPE_DRAG:
1162 if (mDragEventPool.size() < mMaxPoolSize) {
1163 mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event)));
1164 return;
1165 }
1166 break;
Antonio Kantekeb4a30c2021-09-28 17:49:49 -07001167 case AINPUT_EVENT_TYPE_TOUCH_MODE:
1168 if (mTouchModeEventPool.size() < mMaxPoolSize) {
1169 mTouchModeEventPool.push(
1170 std::unique_ptr<TouchModeEvent>(static_cast<TouchModeEvent*>(event)));
1171 return;
1172 }
1173 break;
Jeff Brown5912f952013-07-01 19:10:31 -07001174 }
1175 delete event;
1176}
1177
1178} // namespace android