blob: 1178d02510554459f56b8d07c938cac57ad166b9 [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 Pradhan6b384612021-05-14 16:56:25 -070099} // namespace
100
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800101const char* motionClassificationToString(MotionClassification classification) {
102 switch (classification) {
103 case MotionClassification::NONE:
104 return "NONE";
105 case MotionClassification::AMBIGUOUS_GESTURE:
106 return "AMBIGUOUS_GESTURE";
107 case MotionClassification::DEEP_PRESS:
108 return "DEEP_PRESS";
Harry Cutts2800fb02022-09-15 13:49:23 +0000109 case MotionClassification::TWO_FINGER_SWIPE:
110 return "TWO_FINGER_SWIPE";
Harry Cuttsc5748d12022-12-02 17:30:18 +0000111 case MotionClassification::MULTI_FINGER_SWIPE:
112 return "MULTI_FINGER_SWIPE";
Harry Cuttsb1e83552022-12-20 11:02:26 +0000113 case MotionClassification::PINCH:
114 return "PINCH";
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800115 }
116}
117
Garfield Tan84b087e2020-01-23 10:49:05 -0800118// --- IdGenerator ---
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700119#if defined(__ANDROID__)
120[[maybe_unused]]
121#endif
122static status_t
123getRandomBytes(uint8_t* data, size_t size) {
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700124 int ret = TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY | O_CLOEXEC | O_NOFOLLOW));
125 if (ret == -1) {
126 return -errno;
127 }
128
129 base::unique_fd fd(ret);
130 if (!base::ReadFully(fd, data, size)) {
131 return -errno;
132 }
133 return OK;
134}
135
Garfield Tan84b087e2020-01-23 10:49:05 -0800136IdGenerator::IdGenerator(Source source) : mSource(source) {}
137
138int32_t IdGenerator::nextId() const {
139 constexpr uint32_t SEQUENCE_NUMBER_MASK = ~SOURCE_MASK;
140 int32_t id = 0;
141
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700142#if defined(__ANDROID__)
143 // On device, prefer 'getrandom' to '/dev/urandom' because it's faster.
144 constexpr size_t BUF_LEN = sizeof(id);
145 size_t totalBytes = 0;
146 while (totalBytes < BUF_LEN) {
147 ssize_t bytes = TEMP_FAILURE_RETRY(getrandom(&id, BUF_LEN, GRND_NONBLOCK));
148 if (CC_UNLIKELY(bytes < 0)) {
149 ALOGW("Failed to fill in random number for sequence number: %s.", strerror(errno));
150 id = 0;
151 break;
152 }
153 totalBytes += bytes;
154 }
155#else
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700156#if defined(__linux__)
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700157 // On host, <sys/random.h> / GRND_NONBLOCK is not available
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700158 while (true) {
159 status_t result = getRandomBytes(reinterpret_cast<uint8_t*>(&id), sizeof(id));
160 if (result == OK) {
Garfield Tan84b087e2020-01-23 10:49:05 -0800161 break;
162 }
Garfield Tan84b087e2020-01-23 10:49:05 -0800163 }
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700164#endif // __linux__
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700165#endif // __ANDROID__
Garfield Tan84b087e2020-01-23 10:49:05 -0800166 return (id & SEQUENCE_NUMBER_MASK) | static_cast<int32_t>(mSource);
167}
168
Jeff Brown5912f952013-07-01 19:10:31 -0700169// --- InputEvent ---
170
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000171// Due to precision limitations when working with floating points, transforming - namely
172// scaling - floating points can lead to minute errors. We round transformed values to approximately
173// three decimal places so that values like 0.99997 show up as 1.0.
174inline float roundTransformedCoords(float val) {
175 // Use a power to two to approximate three decimal places to potentially reduce some cycles.
176 // This should be at least as precise as MotionEvent::ROUNDING_PRECISION.
177 return std::round(val * 1024.f) / 1024.f;
178}
179
180inline vec2 roundTransformedCoords(vec2 p) {
181 return {roundTransformedCoords(p.x), roundTransformedCoords(p.y)};
182}
183
Prabir Pradhande69f8a2021-11-18 16:40:34 +0000184vec2 transformWithoutTranslation(const ui::Transform& transform, const vec2& xy) {
185 const vec2 transformedXy = transform.transform(xy);
186 const vec2 transformedOrigin = transform.transform(0, 0);
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000187 return roundTransformedCoords(transformedXy - transformedOrigin);
Prabir Pradhande69f8a2021-11-18 16:40:34 +0000188}
189
Prabir Pradhane2e10b42022-11-17 20:59:36 +0000190float transformAngle(const ui::Transform& transform, float angleRadians) {
191 // Construct and transform a vector oriented at the specified clockwise angle from vertical.
192 // Coordinate system: down is increasing Y, right is increasing X.
193 float x = sinf(angleRadians);
194 float y = -cosf(angleRadians);
195 vec2 transformedPoint = transform.transform(x, y);
196
197 // Determine how the origin is transformed by the matrix so that we
198 // can transform orientation vectors.
199 const vec2 origin = transform.transform(0, 0);
200
201 transformedPoint.x -= origin.x;
202 transformedPoint.y -= origin.y;
203
204 // Derive the transformed vector's clockwise angle from vertical.
205 // The return value of atan2f is in range [-pi, pi] which conforms to the orientation API.
206 return atan2f(transformedPoint.x, -transformedPoint.y);
207}
208
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800209std::string inputEventSourceToString(int32_t source) {
210 if (source == AINPUT_SOURCE_UNKNOWN) {
211 return "UNKNOWN";
212 }
213 if (source == static_cast<int32_t>(AINPUT_SOURCE_ANY)) {
214 return "ANY";
215 }
216 static const std::map<int32_t, const char*> SOURCES{
217 {AINPUT_SOURCE_KEYBOARD, "KEYBOARD"},
218 {AINPUT_SOURCE_DPAD, "DPAD"},
219 {AINPUT_SOURCE_GAMEPAD, "GAMEPAD"},
220 {AINPUT_SOURCE_TOUCHSCREEN, "TOUCHSCREEN"},
221 {AINPUT_SOURCE_MOUSE, "MOUSE"},
222 {AINPUT_SOURCE_STYLUS, "STYLUS"},
223 {AINPUT_SOURCE_BLUETOOTH_STYLUS, "BLUETOOTH_STYLUS"},
224 {AINPUT_SOURCE_TRACKBALL, "TRACKBALL"},
225 {AINPUT_SOURCE_MOUSE_RELATIVE, "MOUSE_RELATIVE"},
226 {AINPUT_SOURCE_TOUCHPAD, "TOUCHPAD"},
227 {AINPUT_SOURCE_TOUCH_NAVIGATION, "TOUCH_NAVIGATION"},
228 {AINPUT_SOURCE_JOYSTICK, "JOYSTICK"},
229 {AINPUT_SOURCE_HDMI, "HDMI"},
230 {AINPUT_SOURCE_SENSOR, "SENSOR"},
231 {AINPUT_SOURCE_ROTARY_ENCODER, "ROTARY_ENCODER"},
232 };
233 std::string result;
234 for (const auto& [source_entry, str] : SOURCES) {
235 if ((source & source_entry) == source_entry) {
236 if (!result.empty()) {
237 result += " | ";
238 }
239 result += str;
240 }
241 }
242 if (result.empty()) {
243 result = StringPrintf("0x%08x", source);
244 }
245 return result;
246}
247
248bool isFromSource(uint32_t source, uint32_t test) {
249 return (source & test) == test;
250}
251
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700252bool isStylusToolType(ToolType toolType) {
253 return toolType == ToolType::STYLUS || toolType == ToolType::ERASER;
Prabir Pradhane5626962022-10-27 20:30:53 +0000254}
255
Siarhei Vishniakoudcc6e6e2023-10-18 09:20:07 -0700256bool isStylusEvent(uint32_t source, const std::vector<PointerProperties>& properties) {
257 if (!isFromSource(source, AINPUT_SOURCE_STYLUS)) {
258 return false;
259 }
260 // Need at least one stylus pointer for this event to be considered a stylus event
261 for (const PointerProperties& pointerProperties : properties) {
262 if (isStylusToolType(pointerProperties.toolType)) {
263 return true;
264 }
265 }
266 return false;
267}
268
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800269VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
270 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
271 event.getSource(), event.getDisplayId()},
272 event.getAction(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800273 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800274 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800275 event.getKeyCode(),
276 event.getScanCode(),
277 event.getMetaState(),
278 event.getRepeatCount()};
279}
280
281VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
282 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
283 event.getSource(), event.getDisplayId()},
284 event.getRawX(0),
285 event.getRawY(0),
286 event.getActionMasked(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800287 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800288 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800289 event.getMetaState(),
290 event.getButtonState()};
291}
292
Linnan Li13bf76a2024-05-05 19:18:02 +0800293void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source,
294 ui::LogicalDisplayId displayId, std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800295 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700296 mDeviceId = deviceId;
297 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100298 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600299 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700300}
301
302void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800303 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700304 mDeviceId = from.mDeviceId;
305 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100306 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600307 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700308}
309
Garfield Tan4cc839f2020-01-24 11:26:14 -0800310int32_t InputEvent::nextId() {
311 static IdGenerator idGen(IdGenerator::Source::OTHER);
312 return idGen.nextId();
313}
314
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700315std::ostream& operator<<(std::ostream& out, const InputEvent& event) {
316 switch (event.getType()) {
317 case InputEventType::KEY: {
318 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event);
319 out << keyEvent;
320 return out;
321 }
322 case InputEventType::MOTION: {
323 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event);
324 out << motionEvent;
325 return out;
326 }
327 case InputEventType::FOCUS: {
328 out << "FocusEvent";
329 return out;
330 }
331 case InputEventType::CAPTURE: {
332 out << "CaptureEvent";
333 return out;
334 }
335 case InputEventType::DRAG: {
336 out << "DragEvent";
337 return out;
338 }
339 case InputEventType::TOUCH_MODE: {
340 out << "TouchModeEvent";
341 return out;
342 }
343 }
344}
345
Jeff Brown5912f952013-07-01 19:10:31 -0700346// --- KeyEvent ---
347
Michael Wright872db4f2014-04-22 15:03:51 -0700348const char* KeyEvent::getLabel(int32_t keyCode) {
Chris Ye4958d062020-08-20 13:21:10 -0700349 return InputEventLookup::getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700350}
351
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800352std::optional<int> KeyEvent::getKeyCodeFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700353 return InputEventLookup::getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700354}
355
Linnan Li13bf76a2024-05-05 19:18:02 +0800356void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source,
357 ui::LogicalDisplayId displayId, std::array<uint8_t, 32> hmac,
358 int32_t action, int32_t flags, int32_t keyCode, int32_t scanCode,
359 int32_t metaState, int32_t repeatCount, nsecs_t downTime,
360 nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800361 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700362 mAction = action;
363 mFlags = flags;
364 mKeyCode = keyCode;
365 mScanCode = scanCode;
366 mMetaState = metaState;
367 mRepeatCount = repeatCount;
368 mDownTime = downTime;
369 mEventTime = eventTime;
370}
371
372void KeyEvent::initialize(const KeyEvent& from) {
373 InputEvent::initialize(from);
374 mAction = from.mAction;
375 mFlags = from.mFlags;
376 mKeyCode = from.mKeyCode;
377 mScanCode = from.mScanCode;
378 mMetaState = from.mMetaState;
379 mRepeatCount = from.mRepeatCount;
380 mDownTime = from.mDownTime;
381 mEventTime = from.mEventTime;
382}
383
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700384const char* KeyEvent::actionToString(int32_t action) {
385 // Convert KeyEvent action to string
386 switch (action) {
387 case AKEY_EVENT_ACTION_DOWN:
388 return "DOWN";
389 case AKEY_EVENT_ACTION_UP:
390 return "UP";
391 case AKEY_EVENT_ACTION_MULTIPLE:
392 return "MULTIPLE";
393 }
394 return "UNKNOWN";
395}
Jeff Brown5912f952013-07-01 19:10:31 -0700396
Siarhei Vishniakoud010b012023-01-18 15:00:53 -0800397std::ostream& operator<<(std::ostream& out, const KeyEvent& event) {
398 out << "KeyEvent { action=" << KeyEvent::actionToString(event.getAction());
399
400 out << ", keycode=" << event.getKeyCode() << "(" << KeyEvent::getLabel(event.getKeyCode())
401 << ")";
402
403 if (event.getMetaState() != 0) {
404 out << ", metaState=" << event.getMetaState();
405 }
406
407 out << ", eventTime=" << event.getEventTime();
408 out << ", downTime=" << event.getDownTime();
409 out << ", flags=" << std::hex << event.getFlags() << std::dec;
410 out << ", repeatCount=" << event.getRepeatCount();
411 out << ", deviceId=" << event.getDeviceId();
412 out << ", source=" << inputEventSourceToString(event.getSource());
413 out << ", displayId=" << event.getDisplayId();
Prabir Pradhanf5abab62024-02-01 20:51:32 +0000414 out << ", eventId=0x" << std::hex << event.getId() << std::dec;
Siarhei Vishniakoud010b012023-01-18 15:00:53 -0800415 out << "}";
416 return out;
417}
418
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -0800419std::ostream& operator<<(std::ostream& out, const PointerProperties& properties) {
420 out << "Pointer(id=" << properties.id << ", " << ftl::enum_string(properties.toolType) << ")";
421 return out;
422}
423
Jeff Brown5912f952013-07-01 19:10:31 -0700424// --- PointerCoords ---
425
426float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700427 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700428 return 0;
429 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700430 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700431}
432
433status_t PointerCoords::setAxisValue(int32_t axis, float value) {
434 if (axis < 0 || axis > 63) {
435 return NAME_NOT_FOUND;
436 }
437
Michael Wright38dcdff2014-03-19 12:06:10 -0700438 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
439 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700440 if (value == 0) {
441 return OK; // axes with value 0 do not need to be stored
442 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700443
444 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700445 if (count >= MAX_AXES) {
446 tooManyAxes(axis);
447 return NO_MEMORY;
448 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700449 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700450 for (uint32_t i = count; i > index; i--) {
451 values[i] = values[i - 1];
452 }
453 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700454
Jeff Brown5912f952013-07-01 19:10:31 -0700455 values[index] = value;
456 return OK;
457}
458
459static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
460 float value = c.getAxisValue(axis);
461 if (value != 0) {
462 c.setAxisValue(axis, value * scaleFactor);
463 }
464}
465
Robert Carre07e1032018-11-26 12:55:53 -0800466void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700467 // No need to scale pressure or size since they are normalized.
468 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800469
470 // If there is a global scale factor, it is included in the windowX/YScale
471 // so we don't need to apply it twice to the X/Y axes.
472 // However we don't want to apply any windowXYScale not included in the global scale
473 // to the TOUCH_MAJOR/MINOR coordinates.
474 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
475 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
476 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
477 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
478 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
479 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
Prabir Pradhanc6523582021-05-14 18:02:55 -0700480 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_X, windowXScale);
481 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_Y, windowYScale);
Robert Carre07e1032018-11-26 12:55:53 -0800482}
483
Jeff Brown5912f952013-07-01 19:10:31 -0700484status_t PointerCoords::readFromParcel(Parcel* parcel) {
485 bits = parcel->readInt64();
486
Michael Wright38dcdff2014-03-19 12:06:10 -0700487 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700488 if (count > MAX_AXES) {
489 return BAD_VALUE;
490 }
491
492 for (uint32_t i = 0; i < count; i++) {
493 values[i] = parcel->readFloat();
494 }
Philip Quinnafb31282022-12-20 18:17:55 -0800495
496 isResampled = parcel->readBool();
Jeff Brown5912f952013-07-01 19:10:31 -0700497 return OK;
498}
499
500status_t PointerCoords::writeToParcel(Parcel* parcel) const {
501 parcel->writeInt64(bits);
502
Michael Wright38dcdff2014-03-19 12:06:10 -0700503 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700504 for (uint32_t i = 0; i < count; i++) {
505 parcel->writeFloat(values[i]);
506 }
Philip Quinnafb31282022-12-20 18:17:55 -0800507
508 parcel->writeBool(isResampled);
Jeff Brown5912f952013-07-01 19:10:31 -0700509 return OK;
510}
Jeff Brown5912f952013-07-01 19:10:31 -0700511
512void PointerCoords::tooManyAxes(int axis) {
513 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
514 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
515}
516
517bool PointerCoords::operator==(const PointerCoords& other) const {
518 if (bits != other.bits) {
519 return false;
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 if (values[i] != other.values[i]) {
524 return false;
525 }
526 }
Philip Quinnafb31282022-12-20 18:17:55 -0800527 if (isResampled != other.isResampled) {
528 return false;
529 }
Jeff Brown5912f952013-07-01 19:10:31 -0700530 return true;
531}
532
chaviwc01e1372020-07-01 12:37:31 -0700533void PointerCoords::transform(const ui::Transform& transform) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700534 const vec2 xy = transform.transform(getXYValue());
535 setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
536 setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
537
Prabir Pradhanc6523582021-05-14 18:02:55 -0700538 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_X) ||
539 BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_Y)) {
540 const ui::Transform rotation(transform.getOrientation());
541 const vec2 relativeXy = rotation.transform(getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
542 getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
543 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
544 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
545 }
546
Prabir Pradhan6b384612021-05-14 16:56:25 -0700547 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) {
548 const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
549 setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val));
550 }
chaviwc01e1372020-07-01 12:37:31 -0700551}
Jeff Brown5912f952013-07-01 19:10:31 -0700552
Jeff Brown5912f952013-07-01 19:10:31 -0700553// --- MotionEvent ---
554
Linnan Li13bf76a2024-05-05 19:18:02 +0800555void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source,
556 ui::LogicalDisplayId displayId, std::array<uint8_t, 32> hmac,
557 int32_t action, int32_t actionButton, int32_t flags, int32_t edgeFlags,
558 int32_t metaState, int32_t buttonState,
559 MotionClassification classification, const ui::Transform& transform,
560 float xPrecision, float yPrecision, float rawXCursorPosition,
561 float rawYCursorPosition, const ui::Transform& rawTransform,
562 nsecs_t downTime, nsecs_t eventTime, size_t pointerCount,
563 const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700564 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800565 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700566 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100567 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700568 mFlags = flags;
569 mEdgeFlags = edgeFlags;
570 mMetaState = metaState;
571 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800572 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700573 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700574 mXPrecision = xPrecision;
575 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700576 mRawXCursorPosition = rawXCursorPosition;
577 mRawYCursorPosition = rawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700578 mRawTransform = rawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700579 mDownTime = downTime;
580 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800581 mPointerProperties.insert(mPointerProperties.end(), &pointerProperties[0],
582 &pointerProperties[pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700583 mSampleEventTimes.clear();
584 mSamplePointerCoords.clear();
585 addSample(eventTime, pointerCoords);
586}
587
588void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800589 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
590 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700591 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100592 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700593 mFlags = other->mFlags;
594 mEdgeFlags = other->mEdgeFlags;
595 mMetaState = other->mMetaState;
596 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800597 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700598 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700599 mXPrecision = other->mXPrecision;
600 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700601 mRawXCursorPosition = other->mRawXCursorPosition;
602 mRawYCursorPosition = other->mRawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700603 mRawTransform = other->mRawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700604 mDownTime = other->mDownTime;
605 mPointerProperties = other->mPointerProperties;
606
607 if (keepHistory) {
608 mSampleEventTimes = other->mSampleEventTimes;
609 mSamplePointerCoords = other->mSamplePointerCoords;
610 } else {
611 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500612 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700613 mSamplePointerCoords.clear();
614 size_t pointerCount = other->getPointerCount();
615 size_t historySize = other->getHistorySize();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800616 mSamplePointerCoords
617 .insert(mSamplePointerCoords.end(),
618 &other->mSamplePointerCoords[historySize * pointerCount],
619 &other->mSamplePointerCoords[historySize * pointerCount + pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700620 }
621}
622
Prabir Pradhanbf9b0a82024-02-29 02:23:50 +0000623void MotionEvent::splitFrom(const android::MotionEvent& other,
624 std::bitset<MAX_POINTER_ID + 1> splitPointerIds, int32_t newEventId) {
625 // TODO(b/327503168): The down time should be a parameter to the split function, because only
626 // the caller can know when the first event went down on the target.
627 const nsecs_t splitDownTime = other.mDownTime;
628
629 auto [action, pointerProperties, pointerCoords] =
630 split(other.getAction(), other.getFlags(), other.getHistorySize(),
631 other.mPointerProperties, other.mSamplePointerCoords, splitPointerIds);
632
633 // Initialize the event with zero pointers, and manually set the split pointers.
634 initialize(newEventId, other.mDeviceId, other.mSource, other.mDisplayId, /*hmac=*/{}, action,
635 other.mActionButton, other.mFlags, other.mEdgeFlags, other.mMetaState,
636 other.mButtonState, other.mClassification, other.mTransform, other.mXPrecision,
637 other.mYPrecision, other.mRawXCursorPosition, other.mRawYCursorPosition,
638 other.mRawTransform, splitDownTime, other.getEventTime(), /*pointerCount=*/0,
639 pointerProperties.data(), pointerCoords.data());
640 mPointerProperties = std::move(pointerProperties);
641 mSamplePointerCoords = std::move(pointerCoords);
642 mSampleEventTimes = other.mSampleEventTimes;
643}
644
Jeff Brown5912f952013-07-01 19:10:31 -0700645void MotionEvent::addSample(
646 int64_t eventTime,
647 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500648 mSampleEventTimes.push_back(eventTime);
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800649 mSamplePointerCoords.insert(mSamplePointerCoords.end(), &pointerCoords[0],
650 &pointerCoords[getPointerCount()]);
Jeff Brown5912f952013-07-01 19:10:31 -0700651}
652
Michael Wright635422b2022-12-02 00:43:56 +0000653std::optional<ui::Rotation> MotionEvent::getSurfaceRotation() const {
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800654 // The surface rotation is the rotation from the window's coordinate space to that of the
655 // display. Since the event's transform takes display space coordinates to window space, the
656 // returned surface rotation is the inverse of the rotation for the surface.
657 switch (mTransform.getOrientation()) {
658 case ui::Transform::ROT_0:
Michael Wright635422b2022-12-02 00:43:56 +0000659 return ui::ROTATION_0;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800660 case ui::Transform::ROT_90:
Michael Wright635422b2022-12-02 00:43:56 +0000661 return ui::ROTATION_270;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800662 case ui::Transform::ROT_180:
Michael Wright635422b2022-12-02 00:43:56 +0000663 return ui::ROTATION_180;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800664 case ui::Transform::ROT_270:
Michael Wright635422b2022-12-02 00:43:56 +0000665 return ui::ROTATION_90;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800666 default:
Michael Wright635422b2022-12-02 00:43:56 +0000667 return std::nullopt;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800668 }
669}
670
Garfield Tan00f511d2019-06-12 16:55:40 -0700671float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700672 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000673 return roundTransformedCoords(vals.x);
Garfield Tan00f511d2019-06-12 16:55:40 -0700674}
675
676float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700677 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000678 return roundTransformedCoords(vals.y);
Garfield Tan00f511d2019-06-12 16:55:40 -0700679}
680
Garfield Tan937bb832019-07-25 17:48:31 -0700681void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700682 ui::Transform inverse = mTransform.inverse();
683 vec2 vals = inverse.transform(x, y);
684 mRawXCursorPosition = vals.x;
685 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700686}
687
Jeff Brown5912f952013-07-01 19:10:31 -0700688const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000689 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
690 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
691 }
692 const size_t position = getHistorySize() * getPointerCount() + pointerIndex;
693 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
694 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
695 }
696 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700697}
698
699float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
Evan Rosky84f07f02021-04-16 10:42:42 -0700700 return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700701}
702
703float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700704 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700705}
706
707const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
708 size_t pointerIndex, size_t historicalIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000709 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
710 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
711 }
712 if (CC_UNLIKELY(historicalIndex < 0 || historicalIndex > getHistorySize())) {
713 LOG(FATAL) << __func__ << ": Invalid historical index " << historicalIndex << " for "
714 << *this;
715 }
716 const size_t position = historicalIndex * getPointerCount() + pointerIndex;
717 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
718 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
719 }
720 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700721}
722
723float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan6b384612021-05-14 16:56:25 -0700724 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700725 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
726 return calculateTransformedAxisValue(axis, mSource, mRawTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700727}
728
729float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan9f388812021-05-13 16:54:53 -0700730 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700731 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
732 return calculateTransformedAxisValue(axis, mSource, mTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700733}
734
735ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
736 size_t pointerCount = mPointerProperties.size();
737 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800738 if (mPointerProperties[i].id == pointerId) {
Jeff Brown5912f952013-07-01 19:10:31 -0700739 return i;
740 }
741 }
742 return -1;
743}
744
745void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700746 float currXOffset = mTransform.tx();
747 float currYOffset = mTransform.ty();
748 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700749}
750
Prabir Pradhanadd8a4a2024-03-05 22:18:09 +0000751float MotionEvent::getRawXOffset() const {
752 // This is equivalent to the x-coordinate of the point that the origin of the raw coordinate
753 // space maps to.
754 return (mTransform * mRawTransform.inverse()).tx();
755}
756
757float MotionEvent::getRawYOffset() const {
758 // This is equivalent to the y-coordinate of the point that the origin of the raw coordinate
759 // space maps to.
760 return (mTransform * mRawTransform.inverse()).ty();
761}
762
Robert Carre07e1032018-11-26 12:55:53 -0800763void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700764 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700765 mRawTransform.set(mRawTransform.tx() * globalScaleFactor,
766 mRawTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800767 mXPrecision *= globalScaleFactor;
768 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700769
770 size_t numSamples = mSamplePointerCoords.size();
771 for (size_t i = 0; i < numSamples; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800772 mSamplePointerCoords[i].scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700773 }
774}
775
chaviw9eaa22c2020-07-01 16:21:27 -0700776void MotionEvent::transform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700777 // We want to preserve the raw axes values stored in the PointerCoords, so we just update the
778 // transform using the values passed in.
chaviw9eaa22c2020-07-01 16:21:27 -0700779 ui::Transform newTransform;
780 newTransform.set(matrix);
781 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700782}
783
Evan Roskyd4d4d802021-05-03 20:12:21 -0700784void MotionEvent::applyTransform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700785 ui::Transform transform;
786 transform.set(matrix);
Evan Roskyd4d4d802021-05-03 20:12:21 -0700787
788 // Apply the transformation to all samples.
Prabir Pradhan6b384612021-05-14 16:56:25 -0700789 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
790 [&transform](PointerCoords& c) { c.transform(transform); });
Prabir Pradhan4b19bd02021-06-01 17:34:59 -0700791
792 if (mRawXCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION &&
793 mRawYCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) {
794 const vec2 cursor = transform.transform(mRawXCursorPosition, mRawYCursorPosition);
795 mRawXCursorPosition = cursor.x;
796 mRawYCursorPosition = cursor.y;
797 }
Evan Roskyd4d4d802021-05-03 20:12:21 -0700798}
799
chaviw9eaa22c2020-07-01 16:21:27 -0700800static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
801 float dsdx, dtdx, tx, dtdy, dsdy, ty;
802 status_t status = parcel.readFloat(&dsdx);
803 status |= parcel.readFloat(&dtdx);
804 status |= parcel.readFloat(&tx);
805 status |= parcel.readFloat(&dtdy);
806 status |= parcel.readFloat(&dsdy);
807 status |= parcel.readFloat(&ty);
808
809 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
810 return status;
811}
812
813static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
814 status_t status = parcel.writeFloat(transform.dsdx());
815 status |= parcel.writeFloat(transform.dtdx());
816 status |= parcel.writeFloat(transform.tx());
817 status |= parcel.writeFloat(transform.dtdy());
818 status |= parcel.writeFloat(transform.dsdy());
819 status |= parcel.writeFloat(transform.ty());
820 return status;
821}
822
Jeff Brown5912f952013-07-01 19:10:31 -0700823status_t MotionEvent::readFromParcel(Parcel* parcel) {
824 size_t pointerCount = parcel->readInt32();
825 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800826 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
827 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700828 return BAD_VALUE;
829 }
830
Garfield Tan4cc839f2020-01-24 11:26:14 -0800831 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700832 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600833 mSource = parcel->readUint32();
Linnan Li13bf76a2024-05-05 19:18:02 +0800834 mDisplayId = ui::LogicalDisplayId{parcel->readInt32()};
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600835 std::vector<uint8_t> hmac;
836 status_t result = parcel->readByteVector(&hmac);
837 if (result != OK || hmac.size() != 32) {
838 return BAD_VALUE;
839 }
840 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700841 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100842 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700843 mFlags = parcel->readInt32();
844 mEdgeFlags = parcel->readInt32();
845 mMetaState = parcel->readInt32();
846 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800847 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700848
849 result = android::readFromParcel(mTransform, *parcel);
850 if (result != OK) {
851 return result;
852 }
Jeff Brown5912f952013-07-01 19:10:31 -0700853 mXPrecision = parcel->readFloat();
854 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700855 mRawXCursorPosition = parcel->readFloat();
856 mRawYCursorPosition = parcel->readFloat();
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700857
858 result = android::readFromParcel(mRawTransform, *parcel);
859 if (result != OK) {
860 return result;
861 }
Jeff Brown5912f952013-07-01 19:10:31 -0700862 mDownTime = parcel->readInt64();
863
864 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800865 mPointerProperties.reserve(pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700866 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500867 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700868 mSamplePointerCoords.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800869 mSamplePointerCoords.reserve(sampleCount * pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700870
871 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800872 mPointerProperties.push_back({});
873 PointerProperties& properties = mPointerProperties.back();
Jeff Brown5912f952013-07-01 19:10:31 -0700874 properties.id = parcel->readInt32();
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700875 properties.toolType = static_cast<ToolType>(parcel->readInt32());
Jeff Brown5912f952013-07-01 19:10:31 -0700876 }
877
Dan Austinc94fc452015-09-22 14:22:41 -0700878 while (sampleCount > 0) {
879 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500880 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700881 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800882 mSamplePointerCoords.push_back({});
883 status_t status = mSamplePointerCoords.back().readFromParcel(parcel);
Jeff Brown5912f952013-07-01 19:10:31 -0700884 if (status) {
885 return status;
886 }
887 }
888 }
889 return OK;
890}
891
892status_t MotionEvent::writeToParcel(Parcel* parcel) const {
893 size_t pointerCount = mPointerProperties.size();
894 size_t sampleCount = mSampleEventTimes.size();
895
896 parcel->writeInt32(pointerCount);
897 parcel->writeInt32(sampleCount);
898
Garfield Tan4cc839f2020-01-24 11:26:14 -0800899 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700900 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600901 parcel->writeUint32(mSource);
Linnan Li13bf76a2024-05-05 19:18:02 +0800902 parcel->writeInt32(mDisplayId.val());
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600903 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
904 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700905 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100906 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700907 parcel->writeInt32(mFlags);
908 parcel->writeInt32(mEdgeFlags);
909 parcel->writeInt32(mMetaState);
910 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800911 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700912
913 status_t result = android::writeToParcel(mTransform, *parcel);
914 if (result != OK) {
915 return result;
916 }
Jeff Brown5912f952013-07-01 19:10:31 -0700917 parcel->writeFloat(mXPrecision);
918 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700919 parcel->writeFloat(mRawXCursorPosition);
920 parcel->writeFloat(mRawYCursorPosition);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700921
922 result = android::writeToParcel(mRawTransform, *parcel);
923 if (result != OK) {
924 return result;
925 }
Jeff Brown5912f952013-07-01 19:10:31 -0700926 parcel->writeInt64(mDownTime);
927
928 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800929 const PointerProperties& properties = mPointerProperties[i];
Jeff Brown5912f952013-07-01 19:10:31 -0700930 parcel->writeInt32(properties.id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700931 parcel->writeInt32(static_cast<int32_t>(properties.toolType));
Jeff Brown5912f952013-07-01 19:10:31 -0700932 }
933
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800934 const PointerCoords* pc = mSamplePointerCoords.data();
Jeff Brown5912f952013-07-01 19:10:31 -0700935 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500936 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700937 for (size_t i = 0; i < pointerCount; i++) {
938 status_t status = (pc++)->writeToParcel(parcel);
939 if (status) {
940 return status;
941 }
942 }
943 }
944 return OK;
945}
Jeff Brown5912f952013-07-01 19:10:31 -0700946
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600947bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700948 if (isFromSource(source, AINPUT_SOURCE_CLASS_POINTER)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700949 // Specifically excludes HOVER_MOVE and SCROLL.
950 switch (action & AMOTION_EVENT_ACTION_MASK) {
951 case AMOTION_EVENT_ACTION_DOWN:
952 case AMOTION_EVENT_ACTION_MOVE:
953 case AMOTION_EVENT_ACTION_UP:
954 case AMOTION_EVENT_ACTION_POINTER_DOWN:
955 case AMOTION_EVENT_ACTION_POINTER_UP:
956 case AMOTION_EVENT_ACTION_CANCEL:
957 case AMOTION_EVENT_ACTION_OUTSIDE:
958 return true;
959 }
960 }
961 return false;
962}
963
Michael Wright872db4f2014-04-22 15:03:51 -0700964const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700965 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700966}
967
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800968std::optional<int> MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700969 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700970}
971
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500972std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700973 // Convert MotionEvent action to string
974 switch (action & AMOTION_EVENT_ACTION_MASK) {
975 case AMOTION_EVENT_ACTION_DOWN:
976 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700977 case AMOTION_EVENT_ACTION_UP:
978 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500979 case AMOTION_EVENT_ACTION_MOVE:
980 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700981 case AMOTION_EVENT_ACTION_CANCEL:
982 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500983 case AMOTION_EVENT_ACTION_OUTSIDE:
984 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700985 case AMOTION_EVENT_ACTION_POINTER_DOWN:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000986 return StringPrintf("POINTER_DOWN(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700987 case AMOTION_EVENT_ACTION_POINTER_UP:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000988 return StringPrintf("POINTER_UP(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500989 case AMOTION_EVENT_ACTION_HOVER_MOVE:
990 return "HOVER_MOVE";
991 case AMOTION_EVENT_ACTION_SCROLL:
992 return "SCROLL";
993 case AMOTION_EVENT_ACTION_HOVER_ENTER:
994 return "HOVER_ENTER";
995 case AMOTION_EVENT_ACTION_HOVER_EXIT:
996 return "HOVER_EXIT";
997 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
998 return "BUTTON_PRESS";
999 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
1000 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001001 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -05001002 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001003}
1004
Prabir Pradhanbf9b0a82024-02-29 02:23:50 +00001005std::tuple<int32_t, std::vector<PointerProperties>, std::vector<PointerCoords>> MotionEvent::split(
1006 int32_t action, int32_t flags, int32_t historySize,
1007 const std::vector<PointerProperties>& pointerProperties,
1008 const std::vector<PointerCoords>& pointerCoords,
1009 std::bitset<MAX_POINTER_ID + 1> splitPointerIds) {
1010 LOG_ALWAYS_FATAL_IF(!splitPointerIds.any());
1011 const auto pointerCount = pointerProperties.size();
1012 LOG_ALWAYS_FATAL_IF(pointerCoords.size() != (pointerCount * (historySize + 1)));
1013 const auto splitCount = splitPointerIds.count();
1014
1015 std::vector<PointerProperties> splitPointerProperties;
1016 std::vector<PointerCoords> splitPointerCoords;
1017
1018 for (uint32_t i = 0; i < pointerCount; i++) {
1019 if (splitPointerIds.test(pointerProperties[i].id)) {
1020 splitPointerProperties.emplace_back(pointerProperties[i]);
1021 }
1022 }
1023 for (uint32_t i = 0; i < pointerCoords.size(); i++) {
1024 if (splitPointerIds.test(pointerProperties[i % pointerCount].id)) {
1025 splitPointerCoords.emplace_back(pointerCoords[i]);
1026 }
1027 }
1028 LOG_ALWAYS_FATAL_IF(splitPointerCoords.size() !=
1029 (splitPointerProperties.size() * (historySize + 1)));
1030
1031 if (CC_UNLIKELY(splitPointerProperties.size() != splitCount)) {
Prabir Pradhan1a41fe02024-03-11 18:38:40 +00001032 // TODO(b/329107108): Promote this to a fatal check once bugs in the caller are resolved.
1033 LOG(ERROR) << "Cannot split MotionEvent: Requested splitting " << splitCount
Prabir Pradhanbf9b0a82024-02-29 02:23:50 +00001034 << " pointers from the original event, but the original event only contained "
1035 << splitPointerProperties.size() << " of those pointers.";
1036 }
1037
1038 // TODO(b/327503168): Verify the splitDownTime here once it is used correctly.
1039
1040 const auto splitAction = resolveActionForSplitMotionEvent(action, flags, pointerProperties,
1041 splitPointerProperties);
1042 return {splitAction, splitPointerProperties, splitPointerCoords};
1043}
1044
Prabir Pradhan7e1ee562021-10-26 10:19:49 -07001045// Apply the given transformation to the point without checking whether the entire transform
1046// should be disregarded altogether for the provided source.
1047static inline vec2 calculateTransformedXYUnchecked(uint32_t source, const ui::Transform& transform,
1048 const vec2& xy) {
1049 return shouldDisregardOffset(source) ? transformWithoutTranslation(transform, xy)
Prabir Pradhan00e029d2023-03-09 20:11:09 +00001050 : roundTransformedCoords(transform.transform(xy));
Prabir Pradhan7e1ee562021-10-26 10:19:49 -07001051}
1052
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07001053vec2 MotionEvent::calculateTransformedXY(uint32_t source, const ui::Transform& transform,
1054 const vec2& xy) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -07001055 if (shouldDisregardTransformation(source)) {
1056 return xy;
1057 }
1058 return calculateTransformedXYUnchecked(source, transform, xy);
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07001059}
1060
Prabir Pradhan8e6ce222022-02-24 09:08:54 -08001061// Keep in sync with calculateTransformedCoords.
Prabir Pradhan9eb02c02021-10-19 14:02:20 -07001062float MotionEvent::calculateTransformedAxisValue(int32_t axis, uint32_t source,
1063 const ui::Transform& transform,
1064 const PointerCoords& coords) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -07001065 if (shouldDisregardTransformation(source)) {
1066 return coords.getAxisValue(axis);
1067 }
1068
Prabir Pradhan9eb02c02021-10-19 14:02:20 -07001069 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -07001070 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
Prabir Pradhan9eb02c02021-10-19 14:02:20 -07001071 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
1072 return xy[axis];
1073 }
1074
1075 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
1076 const vec2 relativeXy =
1077 transformWithoutTranslation(transform,
1078 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
1079 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
1080 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
1081 }
1082
1083 if (axis == AMOTION_EVENT_AXIS_ORIENTATION) {
1084 return transformAngle(transform, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION));
1085 }
1086
1087 return coords.getAxisValue(axis);
1088}
1089
Prabir Pradhan8e6ce222022-02-24 09:08:54 -08001090// Keep in sync with calculateTransformedAxisValue. This is an optimization of
1091// calculateTransformedAxisValue for all PointerCoords axes.
1092PointerCoords MotionEvent::calculateTransformedCoords(uint32_t source,
1093 const ui::Transform& transform,
1094 const PointerCoords& coords) {
1095 if (shouldDisregardTransformation(source)) {
1096 return coords;
1097 }
1098 PointerCoords out = coords;
1099
1100 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
1101 out.setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
1102 out.setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
1103
1104 const vec2 relativeXy =
1105 transformWithoutTranslation(transform,
1106 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
1107 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
1108 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
1109 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
1110
1111 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
1112 transformAngle(transform,
1113 coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)));
1114
1115 return out;
1116}
1117
Prabir Pradhan65a071a2024-01-05 20:52:09 +00001118bool MotionEvent::operator==(const android::MotionEvent& o) const {
1119 // We use NaN values to represent invalid cursor positions. Since NaN values are not equal
1120 // to themselves according to IEEE 754, we cannot use the default equality operator to compare
1121 // MotionEvents. Therefore we define a custom equality operator with special handling for NaNs.
1122 // clang-format off
1123 return InputEvent::operator==(static_cast<const InputEvent&>(o)) &&
1124 mAction == o.mAction &&
1125 mActionButton == o.mActionButton &&
1126 mFlags == o.mFlags &&
1127 mEdgeFlags == o.mEdgeFlags &&
1128 mMetaState == o.mMetaState &&
1129 mButtonState == o.mButtonState &&
1130 mClassification == o.mClassification &&
1131 mTransform == o.mTransform &&
1132 mXPrecision == o.mXPrecision &&
1133 mYPrecision == o.mYPrecision &&
1134 ((std::isnan(mRawXCursorPosition) && std::isnan(o.mRawXCursorPosition)) ||
1135 mRawXCursorPosition == o.mRawXCursorPosition) &&
1136 ((std::isnan(mRawYCursorPosition) && std::isnan(o.mRawYCursorPosition)) ||
1137 mRawYCursorPosition == o.mRawYCursorPosition) &&
1138 mRawTransform == o.mRawTransform && mDownTime == o.mDownTime &&
1139 mPointerProperties == o.mPointerProperties &&
1140 mSampleEventTimes == o.mSampleEventTimes &&
1141 mSamplePointerCoords == o.mSamplePointerCoords;
1142 // clang-format on
1143}
1144
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +00001145std::ostream& operator<<(std::ostream& out, const MotionEvent& event) {
1146 out << "MotionEvent { action=" << MotionEvent::actionToString(event.getAction());
1147 if (event.getActionButton() != 0) {
1148 out << ", actionButton=" << std::to_string(event.getActionButton());
1149 }
1150 const size_t pointerCount = event.getPointerCount();
hupeng3aa5a51a2022-09-02 16:00:18 +08001151 LOG_ALWAYS_FATAL_IF(pointerCount > MAX_POINTERS, "Too many pointers : pointerCount = %zu",
1152 pointerCount);
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +00001153 for (size_t i = 0; i < pointerCount; i++) {
1154 out << ", id[" << i << "]=" << event.getPointerId(i);
1155 float x = event.getX(i);
1156 float y = event.getY(i);
1157 if (x != 0 || y != 0) {
1158 out << ", x[" << i << "]=" << x;
1159 out << ", y[" << i << "]=" << y;
1160 }
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001161 ToolType toolType = event.getToolType(i);
1162 if (toolType != ToolType::FINGER) {
1163 out << ", toolType[" << i << "]=" << ftl::enum_string(toolType);
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +00001164 }
1165 }
1166 if (event.getButtonState() != 0) {
1167 out << ", buttonState=" << event.getButtonState();
1168 }
1169 if (event.getClassification() != MotionClassification::NONE) {
1170 out << ", classification=" << motionClassificationToString(event.getClassification());
1171 }
1172 if (event.getMetaState() != 0) {
1173 out << ", metaState=" << event.getMetaState();
1174 }
Prabir Pradhan65455c72024-02-13 21:46:41 +00001175 if (event.getFlags() != 0) {
1176 out << ", flags=0x" << std::hex << event.getFlags() << std::dec;
1177 }
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +00001178 if (event.getEdgeFlags() != 0) {
1179 out << ", edgeFlags=" << event.getEdgeFlags();
1180 }
1181 if (pointerCount != 1) {
1182 out << ", pointerCount=" << pointerCount;
1183 }
1184 if (event.getHistorySize() != 0) {
1185 out << ", historySize=" << event.getHistorySize();
1186 }
1187 out << ", eventTime=" << event.getEventTime();
1188 out << ", downTime=" << event.getDownTime();
1189 out << ", deviceId=" << event.getDeviceId();
1190 out << ", source=" << inputEventSourceToString(event.getSource());
1191 out << ", displayId=" << event.getDisplayId();
Prabir Pradhanf5abab62024-02-01 20:51:32 +00001192 out << ", eventId=0x" << std::hex << event.getId() << std::dec;
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +00001193 out << "}";
1194 return out;
1195}
1196
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001197// --- FocusEvent ---
1198
Antonio Kantek3cfec7b2021-11-05 18:26:17 -07001199void FocusEvent::initialize(int32_t id, bool hasFocus) {
Garfield Tan4cc839f2020-01-24 11:26:14 -08001200 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Linnan Li13bf76a2024-05-05 19:18:02 +08001201 ui::ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001202 mHasFocus = hasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001203}
1204
1205void FocusEvent::initialize(const FocusEvent& from) {
1206 InputEvent::initialize(from);
1207 mHasFocus = from.mHasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001208}
Jeff Brown5912f952013-07-01 19:10:31 -07001209
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001210// --- CaptureEvent ---
1211
1212void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) {
1213 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Linnan Li13bf76a2024-05-05 19:18:02 +08001214 ui::ADISPLAY_ID_NONE, INVALID_HMAC);
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001215 mPointerCaptureEnabled = pointerCaptureEnabled;
1216}
1217
1218void CaptureEvent::initialize(const CaptureEvent& from) {
1219 InputEvent::initialize(from);
1220 mPointerCaptureEnabled = from.mPointerCaptureEnabled;
1221}
1222
arthurhung7632c332020-12-30 16:58:01 +08001223// --- DragEvent ---
1224
1225void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) {
1226 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Linnan Li13bf76a2024-05-05 19:18:02 +08001227 ui::ADISPLAY_ID_NONE, INVALID_HMAC);
arthurhung7632c332020-12-30 16:58:01 +08001228 mIsExiting = isExiting;
1229 mX = x;
1230 mY = y;
1231}
1232
1233void DragEvent::initialize(const DragEvent& from) {
1234 InputEvent::initialize(from);
1235 mIsExiting = from.mIsExiting;
1236 mX = from.mX;
1237 mY = from.mY;
1238}
1239
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001240// --- TouchModeEvent ---
1241
1242void TouchModeEvent::initialize(int32_t id, bool isInTouchMode) {
1243 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Linnan Li13bf76a2024-05-05 19:18:02 +08001244 ui::ADISPLAY_ID_NONE, INVALID_HMAC);
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001245 mIsInTouchMode = isInTouchMode;
1246}
1247
1248void TouchModeEvent::initialize(const TouchModeEvent& from) {
1249 InputEvent::initialize(from);
1250 mIsInTouchMode = from.mIsInTouchMode;
1251}
1252
Jeff Brown5912f952013-07-01 19:10:31 -07001253// --- PooledInputEventFactory ---
1254
1255PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
1256 mMaxPoolSize(maxPoolSize) {
1257}
1258
1259PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -07001260}
1261
1262KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001263 if (mKeyEventPool.empty()) {
1264 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001265 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001266 KeyEvent* event = mKeyEventPool.front().release();
1267 mKeyEventPool.pop();
1268 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001269}
1270
1271MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001272 if (mMotionEventPool.empty()) {
1273 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001274 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001275 MotionEvent* event = mMotionEventPool.front().release();
1276 mMotionEventPool.pop();
1277 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001278}
1279
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001280FocusEvent* PooledInputEventFactory::createFocusEvent() {
1281 if (mFocusEventPool.empty()) {
1282 return new FocusEvent();
1283 }
1284 FocusEvent* event = mFocusEventPool.front().release();
1285 mFocusEventPool.pop();
1286 return event;
1287}
1288
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001289CaptureEvent* PooledInputEventFactory::createCaptureEvent() {
1290 if (mCaptureEventPool.empty()) {
1291 return new CaptureEvent();
1292 }
1293 CaptureEvent* event = mCaptureEventPool.front().release();
1294 mCaptureEventPool.pop();
1295 return event;
1296}
1297
arthurhung7632c332020-12-30 16:58:01 +08001298DragEvent* PooledInputEventFactory::createDragEvent() {
1299 if (mDragEventPool.empty()) {
1300 return new DragEvent();
1301 }
1302 DragEvent* event = mDragEventPool.front().release();
1303 mDragEventPool.pop();
1304 return event;
1305}
1306
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001307TouchModeEvent* PooledInputEventFactory::createTouchModeEvent() {
1308 if (mTouchModeEventPool.empty()) {
1309 return new TouchModeEvent();
1310 }
1311 TouchModeEvent* event = mTouchModeEventPool.front().release();
1312 mTouchModeEventPool.pop();
1313 return event;
1314}
1315
Jeff Brown5912f952013-07-01 19:10:31 -07001316void PooledInputEventFactory::recycle(InputEvent* event) {
1317 switch (event->getType()) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001318 case InputEventType::KEY: {
1319 if (mKeyEventPool.size() < mMaxPoolSize) {
1320 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
1321 return;
1322 }
1323 break;
Jeff Brown5912f952013-07-01 19:10:31 -07001324 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001325 case InputEventType::MOTION: {
1326 if (mMotionEventPool.size() < mMaxPoolSize) {
1327 mMotionEventPool.push(
1328 std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
1329 return;
1330 }
1331 break;
Jeff Brown5912f952013-07-01 19:10:31 -07001332 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001333 case InputEventType::FOCUS: {
1334 if (mFocusEventPool.size() < mMaxPoolSize) {
1335 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
1336 return;
1337 }
1338 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001339 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001340 case InputEventType::CAPTURE: {
1341 if (mCaptureEventPool.size() < mMaxPoolSize) {
1342 mCaptureEventPool.push(
1343 std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event)));
1344 return;
1345 }
1346 break;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001347 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001348 case InputEventType::DRAG: {
1349 if (mDragEventPool.size() < mMaxPoolSize) {
1350 mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event)));
1351 return;
1352 }
1353 break;
arthurhung7632c332020-12-30 16:58:01 +08001354 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001355 case InputEventType::TOUCH_MODE: {
1356 if (mTouchModeEventPool.size() < mMaxPoolSize) {
1357 mTouchModeEventPool.push(
1358 std::unique_ptr<TouchModeEvent>(static_cast<TouchModeEvent*>(event)));
1359 return;
1360 }
1361 break;
Antonio Kantekeb4a30c2021-09-28 17:49:49 -07001362 }
Jeff Brown5912f952013-07-01 19:10:31 -07001363 }
1364 delete event;
1365}
1366
1367} // namespace android