blob: b09814797fc904d4855e5ba1e444ef825c594d39 [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>
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
Jeff Brown5912f952013-07-01 19:10:31 -070035#include <binder/Parcel.h>
Siarhei Vishniakou63740b92022-10-20 10:28:08 -070036#if defined(__ANDROID__)
37#include <sys/random.h>
38#endif
Jeff Brown5912f952013-07-01 19:10:31 -070039
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050040using android::base::StringPrintf;
41
Jeff Brown5912f952013-07-01 19:10:31 -070042namespace android {
43
Prabir Pradhan6b384612021-05-14 16:56:25 -070044namespace {
45
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070046bool shouldDisregardTransformation(uint32_t source) {
Prabir Pradhan258e2b92022-06-24 18:37:04 +000047 // Do not apply any transformations to axes from joysticks, touchpads, or relative mice.
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070048 return isFromSource(source, AINPUT_SOURCE_CLASS_JOYSTICK) ||
Prabir Pradhan258e2b92022-06-24 18:37:04 +000049 isFromSource(source, AINPUT_SOURCE_CLASS_POSITION) ||
50 isFromSource(source, AINPUT_SOURCE_MOUSE_RELATIVE);
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070051}
52
53bool shouldDisregardOffset(uint32_t source) {
Prabir Pradhan9f388812021-05-13 16:54:53 -070054 // Pointer events are the only type of events that refer to absolute coordinates on the display,
55 // so we should apply the entire window transform. For other types of events, we should make
56 // sure to not apply the window translation/offset.
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070057 return !isFromSource(source, AINPUT_SOURCE_CLASS_POINTER);
Prabir Pradhan9f388812021-05-13 16:54:53 -070058}
59
Prabir Pradhanbf9b0a82024-02-29 02:23:50 +000060int32_t resolveActionForSplitMotionEvent(
61 int32_t action, int32_t flags, const std::vector<PointerProperties>& pointerProperties,
62 const std::vector<PointerProperties>& splitPointerProperties) {
63 LOG_ALWAYS_FATAL_IF(splitPointerProperties.empty());
64 const auto maskedAction = MotionEvent::getActionMasked(action);
65 if (maskedAction != AMOTION_EVENT_ACTION_POINTER_DOWN &&
66 maskedAction != AMOTION_EVENT_ACTION_POINTER_UP) {
67 // The action is unaffected by splitting this motion event.
68 return action;
69 }
70 const auto actionIndex = MotionEvent::getActionIndex(action);
71 if (CC_UNLIKELY(actionIndex >= pointerProperties.size())) {
72 LOG(FATAL) << "Action index is out of bounds, index: " << actionIndex;
73 }
74
75 const auto affectedPointerId = pointerProperties[actionIndex].id;
76 std::optional<uint32_t> splitActionIndex;
77 for (uint32_t i = 0; i < splitPointerProperties.size(); i++) {
78 if (affectedPointerId == splitPointerProperties[i].id) {
79 splitActionIndex = i;
80 break;
81 }
82 }
83 if (!splitActionIndex.has_value()) {
84 // The affected pointer is not part of the split motion event.
85 return AMOTION_EVENT_ACTION_MOVE;
86 }
87
88 if (splitPointerProperties.size() > 1) {
89 return maskedAction | (*splitActionIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
90 }
91
92 if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) {
93 return ((flags & AMOTION_EVENT_FLAG_CANCELED) != 0) ? AMOTION_EVENT_ACTION_CANCEL
94 : AMOTION_EVENT_ACTION_UP;
95 }
96 return AMOTION_EVENT_ACTION_DOWN;
97}
98
Prabir Pradhan9a53b552024-06-04 02:59:40 +000099float transformOrientation(const ui::Transform& transform, const PointerCoords& coords,
100 int32_t motionEventFlags) {
101 if ((motionEventFlags & AMOTION_EVENT_PRIVATE_FLAG_SUPPORTS_ORIENTATION) == 0) {
102 return 0;
103 }
104
105 const bool isDirectionalAngle =
106 (motionEventFlags & AMOTION_EVENT_PRIVATE_FLAG_SUPPORTS_DIRECTIONAL_ORIENTATION) != 0;
107
108 return transformAngle(transform, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
109 isDirectionalAngle);
110}
111
Prabir Pradhan6b384612021-05-14 16:56:25 -0700112} // namespace
113
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800114const char* motionClassificationToString(MotionClassification classification) {
115 switch (classification) {
116 case MotionClassification::NONE:
117 return "NONE";
118 case MotionClassification::AMBIGUOUS_GESTURE:
119 return "AMBIGUOUS_GESTURE";
120 case MotionClassification::DEEP_PRESS:
121 return "DEEP_PRESS";
Harry Cutts2800fb02022-09-15 13:49:23 +0000122 case MotionClassification::TWO_FINGER_SWIPE:
123 return "TWO_FINGER_SWIPE";
Harry Cuttsc5748d12022-12-02 17:30:18 +0000124 case MotionClassification::MULTI_FINGER_SWIPE:
125 return "MULTI_FINGER_SWIPE";
Harry Cuttsb1e83552022-12-20 11:02:26 +0000126 case MotionClassification::PINCH:
127 return "PINCH";
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800128 }
129}
130
Garfield Tan84b087e2020-01-23 10:49:05 -0800131// --- IdGenerator ---
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700132#if defined(__ANDROID__)
133[[maybe_unused]]
134#endif
135static status_t
136getRandomBytes(uint8_t* data, size_t size) {
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700137 int ret = TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY | O_CLOEXEC | O_NOFOLLOW));
138 if (ret == -1) {
139 return -errno;
140 }
141
142 base::unique_fd fd(ret);
143 if (!base::ReadFully(fd, data, size)) {
144 return -errno;
145 }
146 return OK;
147}
148
Garfield Tan84b087e2020-01-23 10:49:05 -0800149IdGenerator::IdGenerator(Source source) : mSource(source) {}
150
151int32_t IdGenerator::nextId() const {
152 constexpr uint32_t SEQUENCE_NUMBER_MASK = ~SOURCE_MASK;
153 int32_t id = 0;
154
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700155#if defined(__ANDROID__)
156 // On device, prefer 'getrandom' to '/dev/urandom' because it's faster.
157 constexpr size_t BUF_LEN = sizeof(id);
158 size_t totalBytes = 0;
159 while (totalBytes < BUF_LEN) {
160 ssize_t bytes = TEMP_FAILURE_RETRY(getrandom(&id, BUF_LEN, GRND_NONBLOCK));
161 if (CC_UNLIKELY(bytes < 0)) {
162 ALOGW("Failed to fill in random number for sequence number: %s.", strerror(errno));
163 id = 0;
164 break;
165 }
166 totalBytes += bytes;
167 }
168#else
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700169#if defined(__linux__)
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700170 // On host, <sys/random.h> / GRND_NONBLOCK is not available
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700171 while (true) {
172 status_t result = getRandomBytes(reinterpret_cast<uint8_t*>(&id), sizeof(id));
173 if (result == OK) {
Garfield Tan84b087e2020-01-23 10:49:05 -0800174 break;
175 }
Garfield Tan84b087e2020-01-23 10:49:05 -0800176 }
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700177#endif // __linux__
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700178#endif // __ANDROID__
Garfield Tan84b087e2020-01-23 10:49:05 -0800179 return (id & SEQUENCE_NUMBER_MASK) | static_cast<int32_t>(mSource);
180}
181
Jeff Brown5912f952013-07-01 19:10:31 -0700182// --- InputEvent ---
183
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000184// Due to precision limitations when working with floating points, transforming - namely
185// scaling - floating points can lead to minute errors. We round transformed values to approximately
186// three decimal places so that values like 0.99997 show up as 1.0.
187inline float roundTransformedCoords(float val) {
188 // Use a power to two to approximate three decimal places to potentially reduce some cycles.
189 // This should be at least as precise as MotionEvent::ROUNDING_PRECISION.
190 return std::round(val * 1024.f) / 1024.f;
191}
192
193inline vec2 roundTransformedCoords(vec2 p) {
194 return {roundTransformedCoords(p.x), roundTransformedCoords(p.y)};
195}
196
Prabir Pradhande69f8a2021-11-18 16:40:34 +0000197vec2 transformWithoutTranslation(const ui::Transform& transform, const vec2& xy) {
198 const vec2 transformedXy = transform.transform(xy);
199 const vec2 transformedOrigin = transform.transform(0, 0);
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000200 return roundTransformedCoords(transformedXy - transformedOrigin);
Prabir Pradhande69f8a2021-11-18 16:40:34 +0000201}
202
Prabir Pradhan9a53b552024-06-04 02:59:40 +0000203float transformAngle(const ui::Transform& transform, float angleRadians, bool isDirectional) {
Prabir Pradhane2e10b42022-11-17 20:59:36 +0000204 // Construct and transform a vector oriented at the specified clockwise angle from vertical.
205 // Coordinate system: down is increasing Y, right is increasing X.
206 float x = sinf(angleRadians);
207 float y = -cosf(angleRadians);
208 vec2 transformedPoint = transform.transform(x, y);
209
210 // Determine how the origin is transformed by the matrix so that we
211 // can transform orientation vectors.
212 const vec2 origin = transform.transform(0, 0);
213
214 transformedPoint.x -= origin.x;
215 transformedPoint.y -= origin.y;
216
Prabir Pradhan9a53b552024-06-04 02:59:40 +0000217 if (!isDirectional && transformedPoint.y > 0) {
218 // Limit the range of atan2f to [-pi/2, pi/2] by reversing the direction of the vector.
219 transformedPoint *= -1;
220 }
221
Prabir Pradhane2e10b42022-11-17 20:59:36 +0000222 // Derive the transformed vector's clockwise angle from vertical.
223 // The return value of atan2f is in range [-pi, pi] which conforms to the orientation API.
224 return atan2f(transformedPoint.x, -transformedPoint.y);
225}
226
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800227std::string inputEventSourceToString(int32_t source) {
228 if (source == AINPUT_SOURCE_UNKNOWN) {
229 return "UNKNOWN";
230 }
231 if (source == static_cast<int32_t>(AINPUT_SOURCE_ANY)) {
232 return "ANY";
233 }
234 static const std::map<int32_t, const char*> SOURCES{
235 {AINPUT_SOURCE_KEYBOARD, "KEYBOARD"},
236 {AINPUT_SOURCE_DPAD, "DPAD"},
237 {AINPUT_SOURCE_GAMEPAD, "GAMEPAD"},
238 {AINPUT_SOURCE_TOUCHSCREEN, "TOUCHSCREEN"},
239 {AINPUT_SOURCE_MOUSE, "MOUSE"},
240 {AINPUT_SOURCE_STYLUS, "STYLUS"},
241 {AINPUT_SOURCE_BLUETOOTH_STYLUS, "BLUETOOTH_STYLUS"},
242 {AINPUT_SOURCE_TRACKBALL, "TRACKBALL"},
243 {AINPUT_SOURCE_MOUSE_RELATIVE, "MOUSE_RELATIVE"},
244 {AINPUT_SOURCE_TOUCHPAD, "TOUCHPAD"},
245 {AINPUT_SOURCE_TOUCH_NAVIGATION, "TOUCH_NAVIGATION"},
246 {AINPUT_SOURCE_JOYSTICK, "JOYSTICK"},
247 {AINPUT_SOURCE_HDMI, "HDMI"},
248 {AINPUT_SOURCE_SENSOR, "SENSOR"},
249 {AINPUT_SOURCE_ROTARY_ENCODER, "ROTARY_ENCODER"},
250 };
251 std::string result;
252 for (const auto& [source_entry, str] : SOURCES) {
253 if ((source & source_entry) == source_entry) {
254 if (!result.empty()) {
255 result += " | ";
256 }
257 result += str;
258 }
259 }
260 if (result.empty()) {
261 result = StringPrintf("0x%08x", source);
262 }
263 return result;
264}
265
266bool isFromSource(uint32_t source, uint32_t test) {
267 return (source & test) == test;
268}
269
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700270bool isStylusToolType(ToolType toolType) {
271 return toolType == ToolType::STYLUS || toolType == ToolType::ERASER;
Prabir Pradhane5626962022-10-27 20:30:53 +0000272}
273
Siarhei Vishniakoudcc6e6e2023-10-18 09:20:07 -0700274bool isStylusEvent(uint32_t source, const std::vector<PointerProperties>& properties) {
275 if (!isFromSource(source, AINPUT_SOURCE_STYLUS)) {
276 return false;
277 }
278 // Need at least one stylus pointer for this event to be considered a stylus event
279 for (const PointerProperties& pointerProperties : properties) {
280 if (isStylusToolType(pointerProperties.toolType)) {
281 return true;
282 }
283 }
284 return false;
285}
286
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800287VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
288 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
289 event.getSource(), event.getDisplayId()},
290 event.getAction(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800291 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800292 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800293 event.getKeyCode(),
294 event.getScanCode(),
295 event.getMetaState(),
296 event.getRepeatCount()};
297}
298
299VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
300 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
301 event.getSource(), event.getDisplayId()},
302 event.getRawX(0),
303 event.getRawY(0),
304 event.getActionMasked(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800305 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800306 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800307 event.getMetaState(),
308 event.getButtonState()};
309}
310
Linnan Li13bf76a2024-05-05 19:18:02 +0800311void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source,
312 ui::LogicalDisplayId displayId, std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800313 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700314 mDeviceId = deviceId;
315 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100316 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600317 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700318}
319
320void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800321 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700322 mDeviceId = from.mDeviceId;
323 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100324 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600325 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700326}
327
Garfield Tan4cc839f2020-01-24 11:26:14 -0800328int32_t InputEvent::nextId() {
329 static IdGenerator idGen(IdGenerator::Source::OTHER);
330 return idGen.nextId();
331}
332
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700333std::ostream& operator<<(std::ostream& out, const InputEvent& event) {
334 switch (event.getType()) {
335 case InputEventType::KEY: {
336 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event);
337 out << keyEvent;
338 return out;
339 }
340 case InputEventType::MOTION: {
341 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event);
342 out << motionEvent;
343 return out;
344 }
345 case InputEventType::FOCUS: {
346 out << "FocusEvent";
347 return out;
348 }
349 case InputEventType::CAPTURE: {
350 out << "CaptureEvent";
351 return out;
352 }
353 case InputEventType::DRAG: {
354 out << "DragEvent";
355 return out;
356 }
357 case InputEventType::TOUCH_MODE: {
358 out << "TouchModeEvent";
359 return out;
360 }
361 }
362}
363
Jeff Brown5912f952013-07-01 19:10:31 -0700364// --- KeyEvent ---
365
Michael Wright872db4f2014-04-22 15:03:51 -0700366const char* KeyEvent::getLabel(int32_t keyCode) {
Chris Ye4958d062020-08-20 13:21:10 -0700367 return InputEventLookup::getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700368}
369
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800370std::optional<int> KeyEvent::getKeyCodeFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700371 return InputEventLookup::getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700372}
373
Linnan Li13bf76a2024-05-05 19:18:02 +0800374void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source,
375 ui::LogicalDisplayId displayId, std::array<uint8_t, 32> hmac,
376 int32_t action, int32_t flags, int32_t keyCode, int32_t scanCode,
377 int32_t metaState, int32_t repeatCount, nsecs_t downTime,
378 nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800379 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700380 mAction = action;
381 mFlags = flags;
382 mKeyCode = keyCode;
383 mScanCode = scanCode;
384 mMetaState = metaState;
385 mRepeatCount = repeatCount;
386 mDownTime = downTime;
387 mEventTime = eventTime;
388}
389
390void KeyEvent::initialize(const KeyEvent& from) {
391 InputEvent::initialize(from);
392 mAction = from.mAction;
393 mFlags = from.mFlags;
394 mKeyCode = from.mKeyCode;
395 mScanCode = from.mScanCode;
396 mMetaState = from.mMetaState;
397 mRepeatCount = from.mRepeatCount;
398 mDownTime = from.mDownTime;
399 mEventTime = from.mEventTime;
400}
401
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700402const char* KeyEvent::actionToString(int32_t action) {
403 // Convert KeyEvent action to string
404 switch (action) {
405 case AKEY_EVENT_ACTION_DOWN:
406 return "DOWN";
407 case AKEY_EVENT_ACTION_UP:
408 return "UP";
409 case AKEY_EVENT_ACTION_MULTIPLE:
410 return "MULTIPLE";
411 }
412 return "UNKNOWN";
413}
Jeff Brown5912f952013-07-01 19:10:31 -0700414
Siarhei Vishniakoud010b012023-01-18 15:00:53 -0800415std::ostream& operator<<(std::ostream& out, const KeyEvent& event) {
416 out << "KeyEvent { action=" << KeyEvent::actionToString(event.getAction());
417
418 out << ", keycode=" << event.getKeyCode() << "(" << KeyEvent::getLabel(event.getKeyCode())
419 << ")";
420
421 if (event.getMetaState() != 0) {
422 out << ", metaState=" << event.getMetaState();
423 }
424
425 out << ", eventTime=" << event.getEventTime();
426 out << ", downTime=" << event.getDownTime();
427 out << ", flags=" << std::hex << event.getFlags() << std::dec;
428 out << ", repeatCount=" << event.getRepeatCount();
429 out << ", deviceId=" << event.getDeviceId();
430 out << ", source=" << inputEventSourceToString(event.getSource());
431 out << ", displayId=" << event.getDisplayId();
Prabir Pradhanf5abab62024-02-01 20:51:32 +0000432 out << ", eventId=0x" << std::hex << event.getId() << std::dec;
Siarhei Vishniakoud010b012023-01-18 15:00:53 -0800433 out << "}";
434 return out;
435}
436
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -0800437std::ostream& operator<<(std::ostream& out, const PointerProperties& properties) {
438 out << "Pointer(id=" << properties.id << ", " << ftl::enum_string(properties.toolType) << ")";
439 return out;
440}
441
Jeff Brown5912f952013-07-01 19:10:31 -0700442// --- PointerCoords ---
443
444float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700445 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700446 return 0;
447 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700448 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700449}
450
451status_t PointerCoords::setAxisValue(int32_t axis, float value) {
452 if (axis < 0 || axis > 63) {
453 return NAME_NOT_FOUND;
454 }
455
Michael Wright38dcdff2014-03-19 12:06:10 -0700456 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
457 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700458 if (value == 0) {
459 return OK; // axes with value 0 do not need to be stored
460 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700461
462 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700463 if (count >= MAX_AXES) {
464 tooManyAxes(axis);
465 return NO_MEMORY;
466 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700467 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700468 for (uint32_t i = count; i > index; i--) {
469 values[i] = values[i - 1];
470 }
471 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700472
Jeff Brown5912f952013-07-01 19:10:31 -0700473 values[index] = value;
474 return OK;
475}
476
477static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
478 float value = c.getAxisValue(axis);
479 if (value != 0) {
480 c.setAxisValue(axis, value * scaleFactor);
481 }
482}
483
Robert Carre07e1032018-11-26 12:55:53 -0800484void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700485 // No need to scale pressure or size since they are normalized.
486 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800487
488 // If there is a global scale factor, it is included in the windowX/YScale
489 // so we don't need to apply it twice to the X/Y axes.
490 // However we don't want to apply any windowXYScale not included in the global scale
491 // to the TOUCH_MAJOR/MINOR coordinates.
492 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
493 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
494 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
495 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
496 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
497 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
Prabir Pradhanc6523582021-05-14 18:02:55 -0700498 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_X, windowXScale);
499 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_Y, windowYScale);
Robert Carre07e1032018-11-26 12:55:53 -0800500}
501
Jeff Brown5912f952013-07-01 19:10:31 -0700502status_t PointerCoords::readFromParcel(Parcel* parcel) {
503 bits = parcel->readInt64();
504
Michael Wright38dcdff2014-03-19 12:06:10 -0700505 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700506 if (count > MAX_AXES) {
507 return BAD_VALUE;
508 }
509
510 for (uint32_t i = 0; i < count; i++) {
511 values[i] = parcel->readFloat();
512 }
Philip Quinnafb31282022-12-20 18:17:55 -0800513
514 isResampled = parcel->readBool();
Jeff Brown5912f952013-07-01 19:10:31 -0700515 return OK;
516}
517
518status_t PointerCoords::writeToParcel(Parcel* parcel) const {
519 parcel->writeInt64(bits);
520
Michael Wright38dcdff2014-03-19 12:06:10 -0700521 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700522 for (uint32_t i = 0; i < count; i++) {
523 parcel->writeFloat(values[i]);
524 }
Philip Quinnafb31282022-12-20 18:17:55 -0800525
526 parcel->writeBool(isResampled);
Jeff Brown5912f952013-07-01 19:10:31 -0700527 return OK;
528}
Jeff Brown5912f952013-07-01 19:10:31 -0700529
530void PointerCoords::tooManyAxes(int axis) {
531 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
532 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
533}
534
535bool PointerCoords::operator==(const PointerCoords& other) const {
536 if (bits != other.bits) {
537 return false;
538 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700539 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700540 for (uint32_t i = 0; i < count; i++) {
541 if (values[i] != other.values[i]) {
542 return false;
543 }
544 }
Philip Quinnafb31282022-12-20 18:17:55 -0800545 if (isResampled != other.isResampled) {
546 return false;
547 }
Jeff Brown5912f952013-07-01 19:10:31 -0700548 return true;
549}
550
Jeff Brown5912f952013-07-01 19:10:31 -0700551// --- MotionEvent ---
552
Linnan Li13bf76a2024-05-05 19:18:02 +0800553void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source,
554 ui::LogicalDisplayId displayId, std::array<uint8_t, 32> hmac,
555 int32_t action, int32_t actionButton, int32_t flags, int32_t edgeFlags,
556 int32_t metaState, int32_t buttonState,
557 MotionClassification classification, const ui::Transform& transform,
558 float xPrecision, float yPrecision, float rawXCursorPosition,
559 float rawYCursorPosition, const ui::Transform& rawTransform,
560 nsecs_t downTime, nsecs_t eventTime, size_t pointerCount,
561 const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700562 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800563 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700564 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100565 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700566 mFlags = flags;
567 mEdgeFlags = edgeFlags;
568 mMetaState = metaState;
569 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800570 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700571 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700572 mXPrecision = xPrecision;
573 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700574 mRawXCursorPosition = rawXCursorPosition;
575 mRawYCursorPosition = rawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700576 mRawTransform = rawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700577 mDownTime = downTime;
578 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800579 mPointerProperties.insert(mPointerProperties.end(), &pointerProperties[0],
580 &pointerProperties[pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700581 mSampleEventTimes.clear();
582 mSamplePointerCoords.clear();
583 addSample(eventTime, pointerCoords);
584}
585
586void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800587 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
588 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700589 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100590 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700591 mFlags = other->mFlags;
592 mEdgeFlags = other->mEdgeFlags;
593 mMetaState = other->mMetaState;
594 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800595 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700596 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700597 mXPrecision = other->mXPrecision;
598 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700599 mRawXCursorPosition = other->mRawXCursorPosition;
600 mRawYCursorPosition = other->mRawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700601 mRawTransform = other->mRawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700602 mDownTime = other->mDownTime;
603 mPointerProperties = other->mPointerProperties;
604
605 if (keepHistory) {
606 mSampleEventTimes = other->mSampleEventTimes;
607 mSamplePointerCoords = other->mSamplePointerCoords;
608 } else {
609 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500610 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700611 mSamplePointerCoords.clear();
612 size_t pointerCount = other->getPointerCount();
613 size_t historySize = other->getHistorySize();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800614 mSamplePointerCoords
615 .insert(mSamplePointerCoords.end(),
616 &other->mSamplePointerCoords[historySize * pointerCount],
617 &other->mSamplePointerCoords[historySize * pointerCount + pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700618 }
619}
620
Prabir Pradhanbf9b0a82024-02-29 02:23:50 +0000621void MotionEvent::splitFrom(const android::MotionEvent& other,
622 std::bitset<MAX_POINTER_ID + 1> splitPointerIds, int32_t newEventId) {
623 // TODO(b/327503168): The down time should be a parameter to the split function, because only
624 // the caller can know when the first event went down on the target.
625 const nsecs_t splitDownTime = other.mDownTime;
626
627 auto [action, pointerProperties, pointerCoords] =
628 split(other.getAction(), other.getFlags(), other.getHistorySize(),
629 other.mPointerProperties, other.mSamplePointerCoords, splitPointerIds);
630
631 // Initialize the event with zero pointers, and manually set the split pointers.
632 initialize(newEventId, other.mDeviceId, other.mSource, other.mDisplayId, /*hmac=*/{}, action,
633 other.mActionButton, other.mFlags, other.mEdgeFlags, other.mMetaState,
634 other.mButtonState, other.mClassification, other.mTransform, other.mXPrecision,
635 other.mYPrecision, other.mRawXCursorPosition, other.mRawYCursorPosition,
636 other.mRawTransform, splitDownTime, other.getEventTime(), /*pointerCount=*/0,
637 pointerProperties.data(), pointerCoords.data());
638 mPointerProperties = std::move(pointerProperties);
639 mSamplePointerCoords = std::move(pointerCoords);
640 mSampleEventTimes = other.mSampleEventTimes;
641}
642
Jeff Brown5912f952013-07-01 19:10:31 -0700643void MotionEvent::addSample(
644 int64_t eventTime,
645 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500646 mSampleEventTimes.push_back(eventTime);
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800647 mSamplePointerCoords.insert(mSamplePointerCoords.end(), &pointerCoords[0],
648 &pointerCoords[getPointerCount()]);
Jeff Brown5912f952013-07-01 19:10:31 -0700649}
650
Michael Wright635422b2022-12-02 00:43:56 +0000651std::optional<ui::Rotation> MotionEvent::getSurfaceRotation() const {
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800652 // The surface rotation is the rotation from the window's coordinate space to that of the
653 // display. Since the event's transform takes display space coordinates to window space, the
654 // returned surface rotation is the inverse of the rotation for the surface.
655 switch (mTransform.getOrientation()) {
656 case ui::Transform::ROT_0:
Michael Wright635422b2022-12-02 00:43:56 +0000657 return ui::ROTATION_0;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800658 case ui::Transform::ROT_90:
Michael Wright635422b2022-12-02 00:43:56 +0000659 return ui::ROTATION_270;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800660 case ui::Transform::ROT_180:
Michael Wright635422b2022-12-02 00:43:56 +0000661 return ui::ROTATION_180;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800662 case ui::Transform::ROT_270:
Michael Wright635422b2022-12-02 00:43:56 +0000663 return ui::ROTATION_90;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800664 default:
Michael Wright635422b2022-12-02 00:43:56 +0000665 return std::nullopt;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800666 }
667}
668
Garfield Tan00f511d2019-06-12 16:55:40 -0700669float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700670 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000671 return roundTransformedCoords(vals.x);
Garfield Tan00f511d2019-06-12 16:55:40 -0700672}
673
674float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700675 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000676 return roundTransformedCoords(vals.y);
Garfield Tan00f511d2019-06-12 16:55:40 -0700677}
678
Garfield Tan937bb832019-07-25 17:48:31 -0700679void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700680 ui::Transform inverse = mTransform.inverse();
681 vec2 vals = inverse.transform(x, y);
682 mRawXCursorPosition = vals.x;
683 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700684}
685
Jeff Brown5912f952013-07-01 19:10:31 -0700686const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000687 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
688 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
689 }
690 const size_t position = getHistorySize() * getPointerCount() + pointerIndex;
691 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
692 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
693 }
694 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700695}
696
697float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
Evan Rosky84f07f02021-04-16 10:42:42 -0700698 return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700699}
700
701float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700702 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700703}
704
705const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
706 size_t pointerIndex, size_t historicalIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000707 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
708 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
709 }
710 if (CC_UNLIKELY(historicalIndex < 0 || historicalIndex > getHistorySize())) {
711 LOG(FATAL) << __func__ << ": Invalid historical index " << historicalIndex << " for "
712 << *this;
713 }
714 const size_t position = historicalIndex * getPointerCount() + pointerIndex;
715 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
716 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
717 }
718 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700719}
720
721float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan6b384612021-05-14 16:56:25 -0700722 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700723 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
Prabir Pradhan9a53b552024-06-04 02:59:40 +0000724 return calculateTransformedAxisValue(axis, mSource, mFlags, mRawTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700725}
726
727float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan9f388812021-05-13 16:54:53 -0700728 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700729 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
Prabir Pradhan9a53b552024-06-04 02:59:40 +0000730 return calculateTransformedAxisValue(axis, mSource, mFlags, mTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700731}
732
733ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
734 size_t pointerCount = mPointerProperties.size();
735 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800736 if (mPointerProperties[i].id == pointerId) {
Jeff Brown5912f952013-07-01 19:10:31 -0700737 return i;
738 }
739 }
740 return -1;
741}
742
743void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700744 float currXOffset = mTransform.tx();
745 float currYOffset = mTransform.ty();
746 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700747}
748
Prabir Pradhanadd8a4a2024-03-05 22:18:09 +0000749float MotionEvent::getRawXOffset() const {
750 // This is equivalent to the x-coordinate of the point that the origin of the raw coordinate
751 // space maps to.
752 return (mTransform * mRawTransform.inverse()).tx();
753}
754
755float MotionEvent::getRawYOffset() const {
756 // This is equivalent to the y-coordinate of the point that the origin of the raw coordinate
757 // space maps to.
758 return (mTransform * mRawTransform.inverse()).ty();
759}
760
Robert Carre07e1032018-11-26 12:55:53 -0800761void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700762 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700763 mRawTransform.set(mRawTransform.tx() * globalScaleFactor,
764 mRawTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800765 mXPrecision *= globalScaleFactor;
766 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700767
768 size_t numSamples = mSamplePointerCoords.size();
769 for (size_t i = 0; i < numSamples; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800770 mSamplePointerCoords[i].scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700771 }
772}
773
chaviw9eaa22c2020-07-01 16:21:27 -0700774void MotionEvent::transform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700775 // We want to preserve the raw axes values stored in the PointerCoords, so we just update the
776 // transform using the values passed in.
chaviw9eaa22c2020-07-01 16:21:27 -0700777 ui::Transform newTransform;
778 newTransform.set(matrix);
779 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700780}
781
Evan Roskyd4d4d802021-05-03 20:12:21 -0700782void MotionEvent::applyTransform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700783 ui::Transform transform;
784 transform.set(matrix);
Evan Roskyd4d4d802021-05-03 20:12:21 -0700785
786 // Apply the transformation to all samples.
Prabir Pradhan4b8d36c2024-06-07 15:10:46 +0000787 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(), [&](PointerCoords& c) {
788 calculateTransformedCoordsInPlace(c, mSource, mFlags, transform);
789 });
Prabir Pradhan4b19bd02021-06-01 17:34:59 -0700790
791 if (mRawXCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION &&
792 mRawYCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) {
793 const vec2 cursor = transform.transform(mRawXCursorPosition, mRawYCursorPosition);
794 mRawXCursorPosition = cursor.x;
795 mRawYCursorPosition = cursor.y;
796 }
Evan Roskyd4d4d802021-05-03 20:12:21 -0700797}
798
chaviw9eaa22c2020-07-01 16:21:27 -0700799static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
800 float dsdx, dtdx, tx, dtdy, dsdy, ty;
801 status_t status = parcel.readFloat(&dsdx);
802 status |= parcel.readFloat(&dtdx);
803 status |= parcel.readFloat(&tx);
804 status |= parcel.readFloat(&dtdy);
805 status |= parcel.readFloat(&dsdy);
806 status |= parcel.readFloat(&ty);
807
808 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
809 return status;
810}
811
812static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
813 status_t status = parcel.writeFloat(transform.dsdx());
814 status |= parcel.writeFloat(transform.dtdx());
815 status |= parcel.writeFloat(transform.tx());
816 status |= parcel.writeFloat(transform.dtdy());
817 status |= parcel.writeFloat(transform.dsdy());
818 status |= parcel.writeFloat(transform.ty());
819 return status;
820}
821
Jeff Brown5912f952013-07-01 19:10:31 -0700822status_t MotionEvent::readFromParcel(Parcel* parcel) {
823 size_t pointerCount = parcel->readInt32();
824 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800825 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
826 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700827 return BAD_VALUE;
828 }
829
Garfield Tan4cc839f2020-01-24 11:26:14 -0800830 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700831 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600832 mSource = parcel->readUint32();
Linnan Li13bf76a2024-05-05 19:18:02 +0800833 mDisplayId = ui::LogicalDisplayId{parcel->readInt32()};
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600834 std::vector<uint8_t> hmac;
835 status_t result = parcel->readByteVector(&hmac);
836 if (result != OK || hmac.size() != 32) {
837 return BAD_VALUE;
838 }
839 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700840 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100841 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700842 mFlags = parcel->readInt32();
843 mEdgeFlags = parcel->readInt32();
844 mMetaState = parcel->readInt32();
845 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800846 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700847
848 result = android::readFromParcel(mTransform, *parcel);
849 if (result != OK) {
850 return result;
851 }
Jeff Brown5912f952013-07-01 19:10:31 -0700852 mXPrecision = parcel->readFloat();
853 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700854 mRawXCursorPosition = parcel->readFloat();
855 mRawYCursorPosition = parcel->readFloat();
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700856
857 result = android::readFromParcel(mRawTransform, *parcel);
858 if (result != OK) {
859 return result;
860 }
Jeff Brown5912f952013-07-01 19:10:31 -0700861 mDownTime = parcel->readInt64();
862
863 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800864 mPointerProperties.reserve(pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700865 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500866 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700867 mSamplePointerCoords.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800868 mSamplePointerCoords.reserve(sampleCount * pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700869
870 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800871 mPointerProperties.push_back({});
872 PointerProperties& properties = mPointerProperties.back();
Jeff Brown5912f952013-07-01 19:10:31 -0700873 properties.id = parcel->readInt32();
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700874 properties.toolType = static_cast<ToolType>(parcel->readInt32());
Jeff Brown5912f952013-07-01 19:10:31 -0700875 }
876
Dan Austinc94fc452015-09-22 14:22:41 -0700877 while (sampleCount > 0) {
878 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500879 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700880 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800881 mSamplePointerCoords.push_back({});
882 status_t status = mSamplePointerCoords.back().readFromParcel(parcel);
Jeff Brown5912f952013-07-01 19:10:31 -0700883 if (status) {
884 return status;
885 }
886 }
887 }
888 return OK;
889}
890
891status_t MotionEvent::writeToParcel(Parcel* parcel) const {
892 size_t pointerCount = mPointerProperties.size();
893 size_t sampleCount = mSampleEventTimes.size();
894
895 parcel->writeInt32(pointerCount);
896 parcel->writeInt32(sampleCount);
897
Garfield Tan4cc839f2020-01-24 11:26:14 -0800898 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700899 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600900 parcel->writeUint32(mSource);
Linnan Li13bf76a2024-05-05 19:18:02 +0800901 parcel->writeInt32(mDisplayId.val());
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600902 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
903 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700904 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100905 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700906 parcel->writeInt32(mFlags);
907 parcel->writeInt32(mEdgeFlags);
908 parcel->writeInt32(mMetaState);
909 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800910 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700911
912 status_t result = android::writeToParcel(mTransform, *parcel);
913 if (result != OK) {
914 return result;
915 }
Jeff Brown5912f952013-07-01 19:10:31 -0700916 parcel->writeFloat(mXPrecision);
917 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700918 parcel->writeFloat(mRawXCursorPosition);
919 parcel->writeFloat(mRawYCursorPosition);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700920
921 result = android::writeToParcel(mRawTransform, *parcel);
922 if (result != OK) {
923 return result;
924 }
Jeff Brown5912f952013-07-01 19:10:31 -0700925 parcel->writeInt64(mDownTime);
926
927 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800928 const PointerProperties& properties = mPointerProperties[i];
Jeff Brown5912f952013-07-01 19:10:31 -0700929 parcel->writeInt32(properties.id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700930 parcel->writeInt32(static_cast<int32_t>(properties.toolType));
Jeff Brown5912f952013-07-01 19:10:31 -0700931 }
932
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800933 const PointerCoords* pc = mSamplePointerCoords.data();
Jeff Brown5912f952013-07-01 19:10:31 -0700934 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500935 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700936 for (size_t i = 0; i < pointerCount; i++) {
937 status_t status = (pc++)->writeToParcel(parcel);
938 if (status) {
939 return status;
940 }
941 }
942 }
943 return OK;
944}
Jeff Brown5912f952013-07-01 19:10:31 -0700945
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600946bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700947 if (isFromSource(source, AINPUT_SOURCE_CLASS_POINTER)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700948 // Specifically excludes HOVER_MOVE and SCROLL.
949 switch (action & AMOTION_EVENT_ACTION_MASK) {
950 case AMOTION_EVENT_ACTION_DOWN:
951 case AMOTION_EVENT_ACTION_MOVE:
952 case AMOTION_EVENT_ACTION_UP:
953 case AMOTION_EVENT_ACTION_POINTER_DOWN:
954 case AMOTION_EVENT_ACTION_POINTER_UP:
955 case AMOTION_EVENT_ACTION_CANCEL:
956 case AMOTION_EVENT_ACTION_OUTSIDE:
957 return true;
958 }
959 }
960 return false;
961}
962
Michael Wright872db4f2014-04-22 15:03:51 -0700963const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700964 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700965}
966
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800967std::optional<int> MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700968 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700969}
970
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500971std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700972 // Convert MotionEvent action to string
973 switch (action & AMOTION_EVENT_ACTION_MASK) {
974 case AMOTION_EVENT_ACTION_DOWN:
975 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700976 case AMOTION_EVENT_ACTION_UP:
977 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500978 case AMOTION_EVENT_ACTION_MOVE:
979 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700980 case AMOTION_EVENT_ACTION_CANCEL:
981 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500982 case AMOTION_EVENT_ACTION_OUTSIDE:
983 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700984 case AMOTION_EVENT_ACTION_POINTER_DOWN:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000985 return StringPrintf("POINTER_DOWN(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700986 case AMOTION_EVENT_ACTION_POINTER_UP:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000987 return StringPrintf("POINTER_UP(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500988 case AMOTION_EVENT_ACTION_HOVER_MOVE:
989 return "HOVER_MOVE";
990 case AMOTION_EVENT_ACTION_SCROLL:
991 return "SCROLL";
992 case AMOTION_EVENT_ACTION_HOVER_ENTER:
993 return "HOVER_ENTER";
994 case AMOTION_EVENT_ACTION_HOVER_EXIT:
995 return "HOVER_EXIT";
996 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
997 return "BUTTON_PRESS";
998 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
999 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001000 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -05001001 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001002}
1003
Prabir Pradhanbf9b0a82024-02-29 02:23:50 +00001004std::tuple<int32_t, std::vector<PointerProperties>, std::vector<PointerCoords>> MotionEvent::split(
1005 int32_t action, int32_t flags, int32_t historySize,
1006 const std::vector<PointerProperties>& pointerProperties,
1007 const std::vector<PointerCoords>& pointerCoords,
1008 std::bitset<MAX_POINTER_ID + 1> splitPointerIds) {
1009 LOG_ALWAYS_FATAL_IF(!splitPointerIds.any());
1010 const auto pointerCount = pointerProperties.size();
1011 LOG_ALWAYS_FATAL_IF(pointerCoords.size() != (pointerCount * (historySize + 1)));
1012 const auto splitCount = splitPointerIds.count();
1013
1014 std::vector<PointerProperties> splitPointerProperties;
1015 std::vector<PointerCoords> splitPointerCoords;
1016
1017 for (uint32_t i = 0; i < pointerCount; i++) {
1018 if (splitPointerIds.test(pointerProperties[i].id)) {
1019 splitPointerProperties.emplace_back(pointerProperties[i]);
1020 }
1021 }
1022 for (uint32_t i = 0; i < pointerCoords.size(); i++) {
1023 if (splitPointerIds.test(pointerProperties[i % pointerCount].id)) {
1024 splitPointerCoords.emplace_back(pointerCoords[i]);
1025 }
1026 }
1027 LOG_ALWAYS_FATAL_IF(splitPointerCoords.size() !=
1028 (splitPointerProperties.size() * (historySize + 1)));
1029
1030 if (CC_UNLIKELY(splitPointerProperties.size() != splitCount)) {
Prabir Pradhan1a41fe02024-03-11 18:38:40 +00001031 // TODO(b/329107108): Promote this to a fatal check once bugs in the caller are resolved.
1032 LOG(ERROR) << "Cannot split MotionEvent: Requested splitting " << splitCount
Prabir Pradhanbf9b0a82024-02-29 02:23:50 +00001033 << " pointers from the original event, but the original event only contained "
1034 << splitPointerProperties.size() << " of those pointers.";
1035 }
1036
1037 // TODO(b/327503168): Verify the splitDownTime here once it is used correctly.
1038
1039 const auto splitAction = resolveActionForSplitMotionEvent(action, flags, pointerProperties,
1040 splitPointerProperties);
1041 return {splitAction, splitPointerProperties, splitPointerCoords};
1042}
1043
Prabir Pradhan7e1ee562021-10-26 10:19:49 -07001044// Apply the given transformation to the point without checking whether the entire transform
1045// should be disregarded altogether for the provided source.
1046static inline vec2 calculateTransformedXYUnchecked(uint32_t source, const ui::Transform& transform,
1047 const vec2& xy) {
1048 return shouldDisregardOffset(source) ? transformWithoutTranslation(transform, xy)
Prabir Pradhan00e029d2023-03-09 20:11:09 +00001049 : roundTransformedCoords(transform.transform(xy));
Prabir Pradhan7e1ee562021-10-26 10:19:49 -07001050}
1051
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07001052vec2 MotionEvent::calculateTransformedXY(uint32_t source, const ui::Transform& transform,
1053 const vec2& xy) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -07001054 if (shouldDisregardTransformation(source)) {
1055 return xy;
1056 }
1057 return calculateTransformedXYUnchecked(source, transform, xy);
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07001058}
1059
Prabir Pradhan8e6ce222022-02-24 09:08:54 -08001060// Keep in sync with calculateTransformedCoords.
Prabir Pradhan9a53b552024-06-04 02:59:40 +00001061float MotionEvent::calculateTransformedAxisValue(int32_t axis, uint32_t source, int32_t flags,
Prabir Pradhan9eb02c02021-10-19 14:02:20 -07001062 const ui::Transform& transform,
1063 const PointerCoords& coords) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -07001064 if (shouldDisregardTransformation(source)) {
1065 return coords.getAxisValue(axis);
1066 }
1067
Prabir Pradhan9eb02c02021-10-19 14:02:20 -07001068 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -07001069 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
Prabir Pradhan9eb02c02021-10-19 14:02:20 -07001070 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
1071 return xy[axis];
1072 }
1073
1074 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
1075 const vec2 relativeXy =
1076 transformWithoutTranslation(transform,
1077 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
1078 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
1079 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
1080 }
1081
1082 if (axis == AMOTION_EVENT_AXIS_ORIENTATION) {
Prabir Pradhan9a53b552024-06-04 02:59:40 +00001083 return transformOrientation(transform, coords, flags);
Prabir Pradhan9eb02c02021-10-19 14:02:20 -07001084 }
1085
1086 return coords.getAxisValue(axis);
1087}
1088
Prabir Pradhan8e6ce222022-02-24 09:08:54 -08001089// Keep in sync with calculateTransformedAxisValue. This is an optimization of
1090// calculateTransformedAxisValue for all PointerCoords axes.
Prabir Pradhan4b8d36c2024-06-07 15:10:46 +00001091void MotionEvent::calculateTransformedCoordsInPlace(PointerCoords& coords, uint32_t source,
1092 int32_t flags, const ui::Transform& transform) {
Prabir Pradhan8e6ce222022-02-24 09:08:54 -08001093 if (shouldDisregardTransformation(source)) {
Prabir Pradhan4b8d36c2024-06-07 15:10:46 +00001094 return;
Prabir Pradhan8e6ce222022-02-24 09:08:54 -08001095 }
Prabir Pradhan8e6ce222022-02-24 09:08:54 -08001096
1097 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
Prabir Pradhan4b8d36c2024-06-07 15:10:46 +00001098 coords.setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
1099 coords.setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
Prabir Pradhan8e6ce222022-02-24 09:08:54 -08001100
1101 const vec2 relativeXy =
1102 transformWithoutTranslation(transform,
1103 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
1104 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
Prabir Pradhan4b8d36c2024-06-07 15:10:46 +00001105 coords.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
1106 coords.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
Prabir Pradhan8e6ce222022-02-24 09:08:54 -08001107
Prabir Pradhan4b8d36c2024-06-07 15:10:46 +00001108 coords.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
1109 transformOrientation(transform, coords, flags));
1110}
Prabir Pradhan8e6ce222022-02-24 09:08:54 -08001111
Prabir Pradhan4b8d36c2024-06-07 15:10:46 +00001112PointerCoords MotionEvent::calculateTransformedCoords(uint32_t source, int32_t flags,
1113 const ui::Transform& transform,
1114 const PointerCoords& coords) {
1115 PointerCoords out = coords;
1116 calculateTransformedCoordsInPlace(out, source, flags, transform);
Prabir Pradhan8e6ce222022-02-24 09:08:54 -08001117 return out;
1118}
1119
Prabir Pradhan65a071a2024-01-05 20:52:09 +00001120bool MotionEvent::operator==(const android::MotionEvent& o) const {
1121 // We use NaN values to represent invalid cursor positions. Since NaN values are not equal
1122 // to themselves according to IEEE 754, we cannot use the default equality operator to compare
1123 // MotionEvents. Therefore we define a custom equality operator with special handling for NaNs.
1124 // clang-format off
1125 return InputEvent::operator==(static_cast<const InputEvent&>(o)) &&
1126 mAction == o.mAction &&
1127 mActionButton == o.mActionButton &&
1128 mFlags == o.mFlags &&
1129 mEdgeFlags == o.mEdgeFlags &&
1130 mMetaState == o.mMetaState &&
1131 mButtonState == o.mButtonState &&
1132 mClassification == o.mClassification &&
1133 mTransform == o.mTransform &&
1134 mXPrecision == o.mXPrecision &&
1135 mYPrecision == o.mYPrecision &&
1136 ((std::isnan(mRawXCursorPosition) && std::isnan(o.mRawXCursorPosition)) ||
1137 mRawXCursorPosition == o.mRawXCursorPosition) &&
1138 ((std::isnan(mRawYCursorPosition) && std::isnan(o.mRawYCursorPosition)) ||
1139 mRawYCursorPosition == o.mRawYCursorPosition) &&
1140 mRawTransform == o.mRawTransform && mDownTime == o.mDownTime &&
1141 mPointerProperties == o.mPointerProperties &&
1142 mSampleEventTimes == o.mSampleEventTimes &&
1143 mSamplePointerCoords == o.mSamplePointerCoords;
1144 // clang-format on
1145}
1146
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +00001147std::ostream& operator<<(std::ostream& out, const MotionEvent& event) {
1148 out << "MotionEvent { action=" << MotionEvent::actionToString(event.getAction());
1149 if (event.getActionButton() != 0) {
1150 out << ", actionButton=" << std::to_string(event.getActionButton());
1151 }
1152 const size_t pointerCount = event.getPointerCount();
hupeng3aa5a51a2022-09-02 16:00:18 +08001153 LOG_ALWAYS_FATAL_IF(pointerCount > MAX_POINTERS, "Too many pointers : pointerCount = %zu",
1154 pointerCount);
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +00001155 for (size_t i = 0; i < pointerCount; i++) {
1156 out << ", id[" << i << "]=" << event.getPointerId(i);
1157 float x = event.getX(i);
1158 float y = event.getY(i);
1159 if (x != 0 || y != 0) {
1160 out << ", x[" << i << "]=" << x;
1161 out << ", y[" << i << "]=" << y;
1162 }
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001163 ToolType toolType = event.getToolType(i);
1164 if (toolType != ToolType::FINGER) {
1165 out << ", toolType[" << i << "]=" << ftl::enum_string(toolType);
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +00001166 }
1167 }
1168 if (event.getButtonState() != 0) {
1169 out << ", buttonState=" << event.getButtonState();
1170 }
1171 if (event.getClassification() != MotionClassification::NONE) {
1172 out << ", classification=" << motionClassificationToString(event.getClassification());
1173 }
1174 if (event.getMetaState() != 0) {
1175 out << ", metaState=" << event.getMetaState();
1176 }
Prabir Pradhan65455c72024-02-13 21:46:41 +00001177 if (event.getFlags() != 0) {
1178 out << ", flags=0x" << std::hex << event.getFlags() << std::dec;
1179 }
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +00001180 if (event.getEdgeFlags() != 0) {
1181 out << ", edgeFlags=" << event.getEdgeFlags();
1182 }
1183 if (pointerCount != 1) {
1184 out << ", pointerCount=" << pointerCount;
1185 }
1186 if (event.getHistorySize() != 0) {
1187 out << ", historySize=" << event.getHistorySize();
1188 }
1189 out << ", eventTime=" << event.getEventTime();
1190 out << ", downTime=" << event.getDownTime();
1191 out << ", deviceId=" << event.getDeviceId();
1192 out << ", source=" << inputEventSourceToString(event.getSource());
1193 out << ", displayId=" << event.getDisplayId();
Prabir Pradhanf5abab62024-02-01 20:51:32 +00001194 out << ", eventId=0x" << std::hex << event.getId() << std::dec;
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +00001195 out << "}";
1196 return out;
1197}
1198
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001199// --- FocusEvent ---
1200
Antonio Kantek3cfec7b2021-11-05 18:26:17 -07001201void FocusEvent::initialize(int32_t id, bool hasFocus) {
Garfield Tan4cc839f2020-01-24 11:26:14 -08001202 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001203 ui::LogicalDisplayId::INVALID, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001204 mHasFocus = hasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001205}
1206
1207void FocusEvent::initialize(const FocusEvent& from) {
1208 InputEvent::initialize(from);
1209 mHasFocus = from.mHasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001210}
Jeff Brown5912f952013-07-01 19:10:31 -07001211
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001212// --- CaptureEvent ---
1213
1214void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) {
1215 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001216 ui::LogicalDisplayId::INVALID, INVALID_HMAC);
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001217 mPointerCaptureEnabled = pointerCaptureEnabled;
1218}
1219
1220void CaptureEvent::initialize(const CaptureEvent& from) {
1221 InputEvent::initialize(from);
1222 mPointerCaptureEnabled = from.mPointerCaptureEnabled;
1223}
1224
arthurhung7632c332020-12-30 16:58:01 +08001225// --- DragEvent ---
1226
1227void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) {
1228 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001229 ui::LogicalDisplayId::INVALID, INVALID_HMAC);
arthurhung7632c332020-12-30 16:58:01 +08001230 mIsExiting = isExiting;
1231 mX = x;
1232 mY = y;
1233}
1234
1235void DragEvent::initialize(const DragEvent& from) {
1236 InputEvent::initialize(from);
1237 mIsExiting = from.mIsExiting;
1238 mX = from.mX;
1239 mY = from.mY;
1240}
1241
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001242// --- TouchModeEvent ---
1243
1244void TouchModeEvent::initialize(int32_t id, bool isInTouchMode) {
1245 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001246 ui::LogicalDisplayId::INVALID, INVALID_HMAC);
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001247 mIsInTouchMode = isInTouchMode;
1248}
1249
1250void TouchModeEvent::initialize(const TouchModeEvent& from) {
1251 InputEvent::initialize(from);
1252 mIsInTouchMode = from.mIsInTouchMode;
1253}
1254
Jeff Brown5912f952013-07-01 19:10:31 -07001255// --- PooledInputEventFactory ---
1256
1257PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
1258 mMaxPoolSize(maxPoolSize) {
1259}
1260
1261PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -07001262}
1263
1264KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001265 if (mKeyEventPool.empty()) {
1266 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001267 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001268 KeyEvent* event = mKeyEventPool.front().release();
1269 mKeyEventPool.pop();
1270 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001271}
1272
1273MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001274 if (mMotionEventPool.empty()) {
1275 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001276 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001277 MotionEvent* event = mMotionEventPool.front().release();
1278 mMotionEventPool.pop();
1279 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001280}
1281
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001282FocusEvent* PooledInputEventFactory::createFocusEvent() {
1283 if (mFocusEventPool.empty()) {
1284 return new FocusEvent();
1285 }
1286 FocusEvent* event = mFocusEventPool.front().release();
1287 mFocusEventPool.pop();
1288 return event;
1289}
1290
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001291CaptureEvent* PooledInputEventFactory::createCaptureEvent() {
1292 if (mCaptureEventPool.empty()) {
1293 return new CaptureEvent();
1294 }
1295 CaptureEvent* event = mCaptureEventPool.front().release();
1296 mCaptureEventPool.pop();
1297 return event;
1298}
1299
arthurhung7632c332020-12-30 16:58:01 +08001300DragEvent* PooledInputEventFactory::createDragEvent() {
1301 if (mDragEventPool.empty()) {
1302 return new DragEvent();
1303 }
1304 DragEvent* event = mDragEventPool.front().release();
1305 mDragEventPool.pop();
1306 return event;
1307}
1308
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001309TouchModeEvent* PooledInputEventFactory::createTouchModeEvent() {
1310 if (mTouchModeEventPool.empty()) {
1311 return new TouchModeEvent();
1312 }
1313 TouchModeEvent* event = mTouchModeEventPool.front().release();
1314 mTouchModeEventPool.pop();
1315 return event;
1316}
1317
Jeff Brown5912f952013-07-01 19:10:31 -07001318void PooledInputEventFactory::recycle(InputEvent* event) {
1319 switch (event->getType()) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001320 case InputEventType::KEY: {
1321 if (mKeyEventPool.size() < mMaxPoolSize) {
1322 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
1323 return;
1324 }
1325 break;
Jeff Brown5912f952013-07-01 19:10:31 -07001326 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001327 case InputEventType::MOTION: {
1328 if (mMotionEventPool.size() < mMaxPoolSize) {
1329 mMotionEventPool.push(
1330 std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
1331 return;
1332 }
1333 break;
Jeff Brown5912f952013-07-01 19:10:31 -07001334 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001335 case InputEventType::FOCUS: {
1336 if (mFocusEventPool.size() < mMaxPoolSize) {
1337 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
1338 return;
1339 }
1340 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001341 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001342 case InputEventType::CAPTURE: {
1343 if (mCaptureEventPool.size() < mMaxPoolSize) {
1344 mCaptureEventPool.push(
1345 std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event)));
1346 return;
1347 }
1348 break;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001349 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001350 case InputEventType::DRAG: {
1351 if (mDragEventPool.size() < mMaxPoolSize) {
1352 mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event)));
1353 return;
1354 }
1355 break;
arthurhung7632c332020-12-30 16:58:01 +08001356 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001357 case InputEventType::TOUCH_MODE: {
1358 if (mTouchModeEventPool.size() < mMaxPoolSize) {
1359 mTouchModeEventPool.push(
1360 std::unique_ptr<TouchModeEvent>(static_cast<TouchModeEvent*>(event)));
1361 return;
1362 }
1363 break;
Antonio Kantekeb4a30c2021-09-28 17:49:49 -07001364 }
Jeff Brown5912f952013-07-01 19:10:31 -07001365 }
1366 delete event;
1367}
1368
1369} // namespace android