blob: d58fb42f05f7177187bb0a3bb7bd2879a3088e3e [file] [log] [blame]
Jeff Brown5912f952013-07-01 19:10:31 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Input"
18//#define LOG_NDEBUG 0
19
chaviw09c8d2d2020-08-24 15:48:26 -070020#include <attestation/HmacKeyManager.h>
Garfield Tan84b087e2020-01-23 10:49:05 -080021#include <cutils/compiler.h>
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050022#include <inttypes.h>
Garfield Tan84b087e2020-01-23 10:49:05 -080023#include <string.h>
Michael Wright635422b2022-12-02 00:43:56 +000024#include <optional>
Jeff Brown5912f952013-07-01 19:10:31 -070025
Siarhei Vishniakou31977182022-09-30 08:51:23 -070026#include <android-base/file.h>
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +000027#include <android-base/logging.h>
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050028#include <android-base/stringprintf.h>
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +000029#include <cutils/compiler.h>
chaviw98318de2021-05-19 16:45:23 -050030#include <gui/constants.h>
Prabir Pradhan092f3a92021-11-25 10:53:27 -080031#include <input/DisplayViewport.h>
Jeff Brown5912f952013-07-01 19:10:31 -070032#include <input/Input.h>
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080033#include <input/InputDevice.h>
Michael Wright872db4f2014-04-22 15:03:51 -070034#include <input/InputEventLabels.h>
Jeff Brown5912f952013-07-01 19:10:31 -070035
Brett Chabotfaa986c2020-11-04 17:39:36 -080036#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -070037#include <binder/Parcel.h>
Brett Chabotfaa986c2020-11-04 17:39:36 -080038#endif
Siarhei Vishniakou63740b92022-10-20 10:28:08 -070039#if defined(__ANDROID__)
40#include <sys/random.h>
41#endif
Jeff Brown5912f952013-07-01 19:10:31 -070042
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050043using android::base::StringPrintf;
44
Jeff Brown5912f952013-07-01 19:10:31 -070045namespace android {
46
Prabir Pradhan6b384612021-05-14 16:56:25 -070047namespace {
48
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070049bool shouldDisregardTransformation(uint32_t source) {
Prabir Pradhan258e2b92022-06-24 18:37:04 +000050 // Do not apply any transformations to axes from joysticks, touchpads, or relative mice.
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070051 return isFromSource(source, AINPUT_SOURCE_CLASS_JOYSTICK) ||
Prabir Pradhan258e2b92022-06-24 18:37:04 +000052 isFromSource(source, AINPUT_SOURCE_CLASS_POSITION) ||
53 isFromSource(source, AINPUT_SOURCE_MOUSE_RELATIVE);
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070054}
55
56bool shouldDisregardOffset(uint32_t source) {
Prabir Pradhan9f388812021-05-13 16:54:53 -070057 // Pointer events are the only type of events that refer to absolute coordinates on the display,
58 // so we should apply the entire window transform. For other types of events, we should make
59 // sure to not apply the window translation/offset.
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070060 return !isFromSource(source, AINPUT_SOURCE_CLASS_POINTER);
Prabir Pradhan9f388812021-05-13 16:54:53 -070061}
62
Prabir Pradhanbf9b0a82024-02-29 02:23:50 +000063int32_t resolveActionForSplitMotionEvent(
64 int32_t action, int32_t flags, const std::vector<PointerProperties>& pointerProperties,
65 const std::vector<PointerProperties>& splitPointerProperties) {
66 LOG_ALWAYS_FATAL_IF(splitPointerProperties.empty());
67 const auto maskedAction = MotionEvent::getActionMasked(action);
68 if (maskedAction != AMOTION_EVENT_ACTION_POINTER_DOWN &&
69 maskedAction != AMOTION_EVENT_ACTION_POINTER_UP) {
70 // The action is unaffected by splitting this motion event.
71 return action;
72 }
73 const auto actionIndex = MotionEvent::getActionIndex(action);
74 if (CC_UNLIKELY(actionIndex >= pointerProperties.size())) {
75 LOG(FATAL) << "Action index is out of bounds, index: " << actionIndex;
76 }
77
78 const auto affectedPointerId = pointerProperties[actionIndex].id;
79 std::optional<uint32_t> splitActionIndex;
80 for (uint32_t i = 0; i < splitPointerProperties.size(); i++) {
81 if (affectedPointerId == splitPointerProperties[i].id) {
82 splitActionIndex = i;
83 break;
84 }
85 }
86 if (!splitActionIndex.has_value()) {
87 // The affected pointer is not part of the split motion event.
88 return AMOTION_EVENT_ACTION_MOVE;
89 }
90
91 if (splitPointerProperties.size() > 1) {
92 return maskedAction | (*splitActionIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
93 }
94
95 if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) {
96 return ((flags & AMOTION_EVENT_FLAG_CANCELED) != 0) ? AMOTION_EVENT_ACTION_CANCEL
97 : AMOTION_EVENT_ACTION_UP;
98 }
99 return AMOTION_EVENT_ACTION_DOWN;
100}
101
Prabir Pradhan6b384612021-05-14 16:56:25 -0700102} // namespace
103
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800104const char* motionClassificationToString(MotionClassification classification) {
105 switch (classification) {
106 case MotionClassification::NONE:
107 return "NONE";
108 case MotionClassification::AMBIGUOUS_GESTURE:
109 return "AMBIGUOUS_GESTURE";
110 case MotionClassification::DEEP_PRESS:
111 return "DEEP_PRESS";
Harry Cutts2800fb02022-09-15 13:49:23 +0000112 case MotionClassification::TWO_FINGER_SWIPE:
113 return "TWO_FINGER_SWIPE";
Harry Cuttsc5748d12022-12-02 17:30:18 +0000114 case MotionClassification::MULTI_FINGER_SWIPE:
115 return "MULTI_FINGER_SWIPE";
Harry Cuttsb1e83552022-12-20 11:02:26 +0000116 case MotionClassification::PINCH:
117 return "PINCH";
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800118 }
119}
120
Garfield Tan84b087e2020-01-23 10:49:05 -0800121// --- IdGenerator ---
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700122#if defined(__ANDROID__)
123[[maybe_unused]]
124#endif
125static status_t
126getRandomBytes(uint8_t* data, size_t size) {
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700127 int ret = TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY | O_CLOEXEC | O_NOFOLLOW));
128 if (ret == -1) {
129 return -errno;
130 }
131
132 base::unique_fd fd(ret);
133 if (!base::ReadFully(fd, data, size)) {
134 return -errno;
135 }
136 return OK;
137}
138
Garfield Tan84b087e2020-01-23 10:49:05 -0800139IdGenerator::IdGenerator(Source source) : mSource(source) {}
140
141int32_t IdGenerator::nextId() const {
142 constexpr uint32_t SEQUENCE_NUMBER_MASK = ~SOURCE_MASK;
143 int32_t id = 0;
144
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700145#if defined(__ANDROID__)
146 // On device, prefer 'getrandom' to '/dev/urandom' because it's faster.
147 constexpr size_t BUF_LEN = sizeof(id);
148 size_t totalBytes = 0;
149 while (totalBytes < BUF_LEN) {
150 ssize_t bytes = TEMP_FAILURE_RETRY(getrandom(&id, BUF_LEN, GRND_NONBLOCK));
151 if (CC_UNLIKELY(bytes < 0)) {
152 ALOGW("Failed to fill in random number for sequence number: %s.", strerror(errno));
153 id = 0;
154 break;
155 }
156 totalBytes += bytes;
157 }
158#else
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700159#if defined(__linux__)
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700160 // On host, <sys/random.h> / GRND_NONBLOCK is not available
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700161 while (true) {
162 status_t result = getRandomBytes(reinterpret_cast<uint8_t*>(&id), sizeof(id));
163 if (result == OK) {
Garfield Tan84b087e2020-01-23 10:49:05 -0800164 break;
165 }
Garfield Tan84b087e2020-01-23 10:49:05 -0800166 }
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700167#endif // __linux__
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700168#endif // __ANDROID__
Garfield Tan84b087e2020-01-23 10:49:05 -0800169 return (id & SEQUENCE_NUMBER_MASK) | static_cast<int32_t>(mSource);
170}
171
Jeff Brown5912f952013-07-01 19:10:31 -0700172// --- InputEvent ---
173
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000174// Due to precision limitations when working with floating points, transforming - namely
175// scaling - floating points can lead to minute errors. We round transformed values to approximately
176// three decimal places so that values like 0.99997 show up as 1.0.
177inline float roundTransformedCoords(float val) {
178 // Use a power to two to approximate three decimal places to potentially reduce some cycles.
179 // This should be at least as precise as MotionEvent::ROUNDING_PRECISION.
180 return std::round(val * 1024.f) / 1024.f;
181}
182
183inline vec2 roundTransformedCoords(vec2 p) {
184 return {roundTransformedCoords(p.x), roundTransformedCoords(p.y)};
185}
186
Prabir Pradhande69f8a2021-11-18 16:40:34 +0000187vec2 transformWithoutTranslation(const ui::Transform& transform, const vec2& xy) {
188 const vec2 transformedXy = transform.transform(xy);
189 const vec2 transformedOrigin = transform.transform(0, 0);
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000190 return roundTransformedCoords(transformedXy - transformedOrigin);
Prabir Pradhande69f8a2021-11-18 16:40:34 +0000191}
192
Prabir Pradhane2e10b42022-11-17 20:59:36 +0000193float transformAngle(const ui::Transform& transform, float angleRadians) {
194 // Construct and transform a vector oriented at the specified clockwise angle from vertical.
195 // Coordinate system: down is increasing Y, right is increasing X.
196 float x = sinf(angleRadians);
197 float y = -cosf(angleRadians);
198 vec2 transformedPoint = transform.transform(x, y);
199
200 // Determine how the origin is transformed by the matrix so that we
201 // can transform orientation vectors.
202 const vec2 origin = transform.transform(0, 0);
203
204 transformedPoint.x -= origin.x;
205 transformedPoint.y -= origin.y;
206
207 // Derive the transformed vector's clockwise angle from vertical.
208 // The return value of atan2f is in range [-pi, pi] which conforms to the orientation API.
209 return atan2f(transformedPoint.x, -transformedPoint.y);
210}
211
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800212std::string inputEventSourceToString(int32_t source) {
213 if (source == AINPUT_SOURCE_UNKNOWN) {
214 return "UNKNOWN";
215 }
216 if (source == static_cast<int32_t>(AINPUT_SOURCE_ANY)) {
217 return "ANY";
218 }
219 static const std::map<int32_t, const char*> SOURCES{
220 {AINPUT_SOURCE_KEYBOARD, "KEYBOARD"},
221 {AINPUT_SOURCE_DPAD, "DPAD"},
222 {AINPUT_SOURCE_GAMEPAD, "GAMEPAD"},
223 {AINPUT_SOURCE_TOUCHSCREEN, "TOUCHSCREEN"},
224 {AINPUT_SOURCE_MOUSE, "MOUSE"},
225 {AINPUT_SOURCE_STYLUS, "STYLUS"},
226 {AINPUT_SOURCE_BLUETOOTH_STYLUS, "BLUETOOTH_STYLUS"},
227 {AINPUT_SOURCE_TRACKBALL, "TRACKBALL"},
228 {AINPUT_SOURCE_MOUSE_RELATIVE, "MOUSE_RELATIVE"},
229 {AINPUT_SOURCE_TOUCHPAD, "TOUCHPAD"},
230 {AINPUT_SOURCE_TOUCH_NAVIGATION, "TOUCH_NAVIGATION"},
231 {AINPUT_SOURCE_JOYSTICK, "JOYSTICK"},
232 {AINPUT_SOURCE_HDMI, "HDMI"},
233 {AINPUT_SOURCE_SENSOR, "SENSOR"},
234 {AINPUT_SOURCE_ROTARY_ENCODER, "ROTARY_ENCODER"},
235 };
236 std::string result;
237 for (const auto& [source_entry, str] : SOURCES) {
238 if ((source & source_entry) == source_entry) {
239 if (!result.empty()) {
240 result += " | ";
241 }
242 result += str;
243 }
244 }
245 if (result.empty()) {
246 result = StringPrintf("0x%08x", source);
247 }
248 return result;
249}
250
251bool isFromSource(uint32_t source, uint32_t test) {
252 return (source & test) == test;
253}
254
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700255bool isStylusToolType(ToolType toolType) {
256 return toolType == ToolType::STYLUS || toolType == ToolType::ERASER;
Prabir Pradhane5626962022-10-27 20:30:53 +0000257}
258
Siarhei Vishniakoudcc6e6e2023-10-18 09:20:07 -0700259bool isStylusEvent(uint32_t source, const std::vector<PointerProperties>& properties) {
260 if (!isFromSource(source, AINPUT_SOURCE_STYLUS)) {
261 return false;
262 }
263 // Need at least one stylus pointer for this event to be considered a stylus event
264 for (const PointerProperties& pointerProperties : properties) {
265 if (isStylusToolType(pointerProperties.toolType)) {
266 return true;
267 }
268 }
269 return false;
270}
271
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800272VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
273 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
274 event.getSource(), event.getDisplayId()},
275 event.getAction(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800276 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800277 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800278 event.getKeyCode(),
279 event.getScanCode(),
280 event.getMetaState(),
281 event.getRepeatCount()};
282}
283
284VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
285 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
286 event.getSource(), event.getDisplayId()},
287 event.getRawX(0),
288 event.getRawY(0),
289 event.getActionMasked(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800290 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800291 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800292 event.getMetaState(),
293 event.getButtonState()};
294}
295
Garfield Tan4cc839f2020-01-24 11:26:14 -0800296void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600297 std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800298 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700299 mDeviceId = deviceId;
300 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100301 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600302 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700303}
304
305void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800306 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700307 mDeviceId = from.mDeviceId;
308 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100309 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600310 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700311}
312
Garfield Tan4cc839f2020-01-24 11:26:14 -0800313int32_t InputEvent::nextId() {
314 static IdGenerator idGen(IdGenerator::Source::OTHER);
315 return idGen.nextId();
316}
317
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700318std::ostream& operator<<(std::ostream& out, const InputEvent& event) {
319 switch (event.getType()) {
320 case InputEventType::KEY: {
321 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event);
322 out << keyEvent;
323 return out;
324 }
325 case InputEventType::MOTION: {
326 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event);
327 out << motionEvent;
328 return out;
329 }
330 case InputEventType::FOCUS: {
331 out << "FocusEvent";
332 return out;
333 }
334 case InputEventType::CAPTURE: {
335 out << "CaptureEvent";
336 return out;
337 }
338 case InputEventType::DRAG: {
339 out << "DragEvent";
340 return out;
341 }
342 case InputEventType::TOUCH_MODE: {
343 out << "TouchModeEvent";
344 return out;
345 }
346 }
347}
348
Jeff Brown5912f952013-07-01 19:10:31 -0700349// --- KeyEvent ---
350
Michael Wright872db4f2014-04-22 15:03:51 -0700351const char* KeyEvent::getLabel(int32_t keyCode) {
Chris Ye4958d062020-08-20 13:21:10 -0700352 return InputEventLookup::getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700353}
354
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800355std::optional<int> KeyEvent::getKeyCodeFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700356 return InputEventLookup::getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700357}
358
Garfield Tan4cc839f2020-01-24 11:26:14 -0800359void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600360 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
361 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
362 nsecs_t downTime, nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800363 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700364 mAction = action;
365 mFlags = flags;
366 mKeyCode = keyCode;
367 mScanCode = scanCode;
368 mMetaState = metaState;
369 mRepeatCount = repeatCount;
370 mDownTime = downTime;
371 mEventTime = eventTime;
372}
373
374void KeyEvent::initialize(const KeyEvent& from) {
375 InputEvent::initialize(from);
376 mAction = from.mAction;
377 mFlags = from.mFlags;
378 mKeyCode = from.mKeyCode;
379 mScanCode = from.mScanCode;
380 mMetaState = from.mMetaState;
381 mRepeatCount = from.mRepeatCount;
382 mDownTime = from.mDownTime;
383 mEventTime = from.mEventTime;
384}
385
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700386const char* KeyEvent::actionToString(int32_t action) {
387 // Convert KeyEvent action to string
388 switch (action) {
389 case AKEY_EVENT_ACTION_DOWN:
390 return "DOWN";
391 case AKEY_EVENT_ACTION_UP:
392 return "UP";
393 case AKEY_EVENT_ACTION_MULTIPLE:
394 return "MULTIPLE";
395 }
396 return "UNKNOWN";
397}
Jeff Brown5912f952013-07-01 19:10:31 -0700398
Siarhei Vishniakoud010b012023-01-18 15:00:53 -0800399std::ostream& operator<<(std::ostream& out, const KeyEvent& event) {
400 out << "KeyEvent { action=" << KeyEvent::actionToString(event.getAction());
401
402 out << ", keycode=" << event.getKeyCode() << "(" << KeyEvent::getLabel(event.getKeyCode())
403 << ")";
404
405 if (event.getMetaState() != 0) {
406 out << ", metaState=" << event.getMetaState();
407 }
408
409 out << ", eventTime=" << event.getEventTime();
410 out << ", downTime=" << event.getDownTime();
411 out << ", flags=" << std::hex << event.getFlags() << std::dec;
412 out << ", repeatCount=" << event.getRepeatCount();
413 out << ", deviceId=" << event.getDeviceId();
414 out << ", source=" << inputEventSourceToString(event.getSource());
415 out << ", displayId=" << event.getDisplayId();
Prabir Pradhanf5abab62024-02-01 20:51:32 +0000416 out << ", eventId=0x" << std::hex << event.getId() << std::dec;
Siarhei Vishniakoud010b012023-01-18 15:00:53 -0800417 out << "}";
418 return out;
419}
420
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -0800421std::ostream& operator<<(std::ostream& out, const PointerProperties& properties) {
422 out << "Pointer(id=" << properties.id << ", " << ftl::enum_string(properties.toolType) << ")";
423 return out;
424}
425
Jeff Brown5912f952013-07-01 19:10:31 -0700426// --- PointerCoords ---
427
428float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700429 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700430 return 0;
431 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700432 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700433}
434
435status_t PointerCoords::setAxisValue(int32_t axis, float value) {
436 if (axis < 0 || axis > 63) {
437 return NAME_NOT_FOUND;
438 }
439
Michael Wright38dcdff2014-03-19 12:06:10 -0700440 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
441 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700442 if (value == 0) {
443 return OK; // axes with value 0 do not need to be stored
444 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700445
446 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700447 if (count >= MAX_AXES) {
448 tooManyAxes(axis);
449 return NO_MEMORY;
450 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700451 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700452 for (uint32_t i = count; i > index; i--) {
453 values[i] = values[i - 1];
454 }
455 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700456
Jeff Brown5912f952013-07-01 19:10:31 -0700457 values[index] = value;
458 return OK;
459}
460
461static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
462 float value = c.getAxisValue(axis);
463 if (value != 0) {
464 c.setAxisValue(axis, value * scaleFactor);
465 }
466}
467
Robert Carre07e1032018-11-26 12:55:53 -0800468void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700469 // No need to scale pressure or size since they are normalized.
470 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800471
472 // If there is a global scale factor, it is included in the windowX/YScale
473 // so we don't need to apply it twice to the X/Y axes.
474 // However we don't want to apply any windowXYScale not included in the global scale
475 // to the TOUCH_MAJOR/MINOR coordinates.
476 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
477 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
478 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
479 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
480 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
481 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
Prabir Pradhanc6523582021-05-14 18:02:55 -0700482 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_X, windowXScale);
483 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_Y, windowYScale);
Robert Carre07e1032018-11-26 12:55:53 -0800484}
485
Brett Chabotfaa986c2020-11-04 17:39:36 -0800486#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700487status_t PointerCoords::readFromParcel(Parcel* parcel) {
488 bits = parcel->readInt64();
489
Michael Wright38dcdff2014-03-19 12:06:10 -0700490 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700491 if (count > MAX_AXES) {
492 return BAD_VALUE;
493 }
494
495 for (uint32_t i = 0; i < count; i++) {
496 values[i] = parcel->readFloat();
497 }
Philip Quinnafb31282022-12-20 18:17:55 -0800498
499 isResampled = parcel->readBool();
Jeff Brown5912f952013-07-01 19:10:31 -0700500 return OK;
501}
502
503status_t PointerCoords::writeToParcel(Parcel* parcel) const {
504 parcel->writeInt64(bits);
505
Michael Wright38dcdff2014-03-19 12:06:10 -0700506 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700507 for (uint32_t i = 0; i < count; i++) {
508 parcel->writeFloat(values[i]);
509 }
Philip Quinnafb31282022-12-20 18:17:55 -0800510
511 parcel->writeBool(isResampled);
Jeff Brown5912f952013-07-01 19:10:31 -0700512 return OK;
513}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800514#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700515
516void PointerCoords::tooManyAxes(int axis) {
517 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
518 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
519}
520
521bool PointerCoords::operator==(const PointerCoords& other) const {
522 if (bits != other.bits) {
523 return false;
524 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700525 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700526 for (uint32_t i = 0; i < count; i++) {
527 if (values[i] != other.values[i]) {
528 return false;
529 }
530 }
Philip Quinnafb31282022-12-20 18:17:55 -0800531 if (isResampled != other.isResampled) {
532 return false;
533 }
Jeff Brown5912f952013-07-01 19:10:31 -0700534 return true;
535}
536
chaviwc01e1372020-07-01 12:37:31 -0700537void PointerCoords::transform(const ui::Transform& transform) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700538 const vec2 xy = transform.transform(getXYValue());
539 setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
540 setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
541
Prabir Pradhanc6523582021-05-14 18:02:55 -0700542 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_X) ||
543 BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_Y)) {
544 const ui::Transform rotation(transform.getOrientation());
545 const vec2 relativeXy = rotation.transform(getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
546 getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
547 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
548 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
549 }
550
Prabir Pradhan6b384612021-05-14 16:56:25 -0700551 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) {
552 const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
553 setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val));
554 }
chaviwc01e1372020-07-01 12:37:31 -0700555}
Jeff Brown5912f952013-07-01 19:10:31 -0700556
Jeff Brown5912f952013-07-01 19:10:31 -0700557// --- MotionEvent ---
558
Garfield Tan4cc839f2020-01-24 11:26:14 -0800559void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600560 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
561 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700562 int32_t buttonState, MotionClassification classification,
563 const ui::Transform& transform, float xPrecision, float yPrecision,
Evan Rosky84f07f02021-04-16 10:42:42 -0700564 float rawXCursorPosition, float rawYCursorPosition,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700565 const ui::Transform& rawTransform, nsecs_t downTime, nsecs_t eventTime,
Evan Rosky09576692021-07-01 12:22:09 -0700566 size_t pointerCount, const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700567 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800568 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700569 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100570 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700571 mFlags = flags;
572 mEdgeFlags = edgeFlags;
573 mMetaState = metaState;
574 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800575 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700576 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700577 mXPrecision = xPrecision;
578 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700579 mRawXCursorPosition = rawXCursorPosition;
580 mRawYCursorPosition = rawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700581 mRawTransform = rawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700582 mDownTime = downTime;
583 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800584 mPointerProperties.insert(mPointerProperties.end(), &pointerProperties[0],
585 &pointerProperties[pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700586 mSampleEventTimes.clear();
587 mSamplePointerCoords.clear();
588 addSample(eventTime, pointerCoords);
589}
590
591void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800592 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
593 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700594 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100595 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700596 mFlags = other->mFlags;
597 mEdgeFlags = other->mEdgeFlags;
598 mMetaState = other->mMetaState;
599 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800600 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700601 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700602 mXPrecision = other->mXPrecision;
603 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700604 mRawXCursorPosition = other->mRawXCursorPosition;
605 mRawYCursorPosition = other->mRawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700606 mRawTransform = other->mRawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700607 mDownTime = other->mDownTime;
608 mPointerProperties = other->mPointerProperties;
609
610 if (keepHistory) {
611 mSampleEventTimes = other->mSampleEventTimes;
612 mSamplePointerCoords = other->mSamplePointerCoords;
613 } else {
614 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500615 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700616 mSamplePointerCoords.clear();
617 size_t pointerCount = other->getPointerCount();
618 size_t historySize = other->getHistorySize();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800619 mSamplePointerCoords
620 .insert(mSamplePointerCoords.end(),
621 &other->mSamplePointerCoords[historySize * pointerCount],
622 &other->mSamplePointerCoords[historySize * pointerCount + pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700623 }
624}
625
Prabir Pradhanbf9b0a82024-02-29 02:23:50 +0000626void MotionEvent::splitFrom(const android::MotionEvent& other,
627 std::bitset<MAX_POINTER_ID + 1> splitPointerIds, int32_t newEventId) {
628 // TODO(b/327503168): The down time should be a parameter to the split function, because only
629 // the caller can know when the first event went down on the target.
630 const nsecs_t splitDownTime = other.mDownTime;
631
632 auto [action, pointerProperties, pointerCoords] =
633 split(other.getAction(), other.getFlags(), other.getHistorySize(),
634 other.mPointerProperties, other.mSamplePointerCoords, splitPointerIds);
635
636 // Initialize the event with zero pointers, and manually set the split pointers.
637 initialize(newEventId, other.mDeviceId, other.mSource, other.mDisplayId, /*hmac=*/{}, action,
638 other.mActionButton, other.mFlags, other.mEdgeFlags, other.mMetaState,
639 other.mButtonState, other.mClassification, other.mTransform, other.mXPrecision,
640 other.mYPrecision, other.mRawXCursorPosition, other.mRawYCursorPosition,
641 other.mRawTransform, splitDownTime, other.getEventTime(), /*pointerCount=*/0,
642 pointerProperties.data(), pointerCoords.data());
643 mPointerProperties = std::move(pointerProperties);
644 mSamplePointerCoords = std::move(pointerCoords);
645 mSampleEventTimes = other.mSampleEventTimes;
646}
647
Jeff Brown5912f952013-07-01 19:10:31 -0700648void MotionEvent::addSample(
649 int64_t eventTime,
650 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500651 mSampleEventTimes.push_back(eventTime);
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800652 mSamplePointerCoords.insert(mSamplePointerCoords.end(), &pointerCoords[0],
653 &pointerCoords[getPointerCount()]);
Jeff Brown5912f952013-07-01 19:10:31 -0700654}
655
Michael Wright635422b2022-12-02 00:43:56 +0000656std::optional<ui::Rotation> MotionEvent::getSurfaceRotation() const {
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800657 // The surface rotation is the rotation from the window's coordinate space to that of the
658 // display. Since the event's transform takes display space coordinates to window space, the
659 // returned surface rotation is the inverse of the rotation for the surface.
660 switch (mTransform.getOrientation()) {
661 case ui::Transform::ROT_0:
Michael Wright635422b2022-12-02 00:43:56 +0000662 return ui::ROTATION_0;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800663 case ui::Transform::ROT_90:
Michael Wright635422b2022-12-02 00:43:56 +0000664 return ui::ROTATION_270;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800665 case ui::Transform::ROT_180:
Michael Wright635422b2022-12-02 00:43:56 +0000666 return ui::ROTATION_180;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800667 case ui::Transform::ROT_270:
Michael Wright635422b2022-12-02 00:43:56 +0000668 return ui::ROTATION_90;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800669 default:
Michael Wright635422b2022-12-02 00:43:56 +0000670 return std::nullopt;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800671 }
672}
673
Garfield Tan00f511d2019-06-12 16:55:40 -0700674float MotionEvent::getXCursorPosition() 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.x);
Garfield Tan00f511d2019-06-12 16:55:40 -0700677}
678
679float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700680 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000681 return roundTransformedCoords(vals.y);
Garfield Tan00f511d2019-06-12 16:55:40 -0700682}
683
Garfield Tan937bb832019-07-25 17:48:31 -0700684void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700685 ui::Transform inverse = mTransform.inverse();
686 vec2 vals = inverse.transform(x, y);
687 mRawXCursorPosition = vals.x;
688 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700689}
690
Jeff Brown5912f952013-07-01 19:10:31 -0700691const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000692 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
693 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
694 }
695 const size_t position = getHistorySize() * getPointerCount() + pointerIndex;
696 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
697 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
698 }
699 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700700}
701
702float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
Evan Rosky84f07f02021-04-16 10:42:42 -0700703 return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700704}
705
706float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700707 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700708}
709
710const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
711 size_t pointerIndex, size_t historicalIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000712 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
713 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
714 }
715 if (CC_UNLIKELY(historicalIndex < 0 || historicalIndex > getHistorySize())) {
716 LOG(FATAL) << __func__ << ": Invalid historical index " << historicalIndex << " for "
717 << *this;
718 }
719 const size_t position = historicalIndex * getPointerCount() + pointerIndex;
720 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
721 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
722 }
723 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700724}
725
726float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan6b384612021-05-14 16:56:25 -0700727 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700728 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
729 return calculateTransformedAxisValue(axis, mSource, mRawTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700730}
731
732float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan9f388812021-05-13 16:54:53 -0700733 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700734 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
735 return calculateTransformedAxisValue(axis, mSource, mTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700736}
737
738ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
739 size_t pointerCount = mPointerProperties.size();
740 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800741 if (mPointerProperties[i].id == pointerId) {
Jeff Brown5912f952013-07-01 19:10:31 -0700742 return i;
743 }
744 }
745 return -1;
746}
747
748void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700749 float currXOffset = mTransform.tx();
750 float currYOffset = mTransform.ty();
751 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700752}
753
Robert Carre07e1032018-11-26 12:55:53 -0800754void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700755 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700756 mRawTransform.set(mRawTransform.tx() * globalScaleFactor,
757 mRawTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800758 mXPrecision *= globalScaleFactor;
759 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700760
761 size_t numSamples = mSamplePointerCoords.size();
762 for (size_t i = 0; i < numSamples; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800763 mSamplePointerCoords[i].scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700764 }
765}
766
chaviw9eaa22c2020-07-01 16:21:27 -0700767void MotionEvent::transform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700768 // We want to preserve the raw axes values stored in the PointerCoords, so we just update the
769 // transform using the values passed in.
chaviw9eaa22c2020-07-01 16:21:27 -0700770 ui::Transform newTransform;
771 newTransform.set(matrix);
772 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700773}
774
Evan Roskyd4d4d802021-05-03 20:12:21 -0700775void MotionEvent::applyTransform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700776 ui::Transform transform;
777 transform.set(matrix);
Evan Roskyd4d4d802021-05-03 20:12:21 -0700778
779 // Apply the transformation to all samples.
Prabir Pradhan6b384612021-05-14 16:56:25 -0700780 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
781 [&transform](PointerCoords& c) { c.transform(transform); });
Prabir Pradhan4b19bd02021-06-01 17:34:59 -0700782
783 if (mRawXCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION &&
784 mRawYCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) {
785 const vec2 cursor = transform.transform(mRawXCursorPosition, mRawYCursorPosition);
786 mRawXCursorPosition = cursor.x;
787 mRawYCursorPosition = cursor.y;
788 }
Evan Roskyd4d4d802021-05-03 20:12:21 -0700789}
790
Brett Chabotfaa986c2020-11-04 17:39:36 -0800791#ifdef __linux__
chaviw9eaa22c2020-07-01 16:21:27 -0700792static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
793 float dsdx, dtdx, tx, dtdy, dsdy, ty;
794 status_t status = parcel.readFloat(&dsdx);
795 status |= parcel.readFloat(&dtdx);
796 status |= parcel.readFloat(&tx);
797 status |= parcel.readFloat(&dtdy);
798 status |= parcel.readFloat(&dsdy);
799 status |= parcel.readFloat(&ty);
800
801 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
802 return status;
803}
804
805static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
806 status_t status = parcel.writeFloat(transform.dsdx());
807 status |= parcel.writeFloat(transform.dtdx());
808 status |= parcel.writeFloat(transform.tx());
809 status |= parcel.writeFloat(transform.dtdy());
810 status |= parcel.writeFloat(transform.dsdy());
811 status |= parcel.writeFloat(transform.ty());
812 return status;
813}
814
Jeff Brown5912f952013-07-01 19:10:31 -0700815status_t MotionEvent::readFromParcel(Parcel* parcel) {
816 size_t pointerCount = parcel->readInt32();
817 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800818 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
819 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700820 return BAD_VALUE;
821 }
822
Garfield Tan4cc839f2020-01-24 11:26:14 -0800823 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700824 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600825 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800826 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600827 std::vector<uint8_t> hmac;
828 status_t result = parcel->readByteVector(&hmac);
829 if (result != OK || hmac.size() != 32) {
830 return BAD_VALUE;
831 }
832 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700833 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100834 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700835 mFlags = parcel->readInt32();
836 mEdgeFlags = parcel->readInt32();
837 mMetaState = parcel->readInt32();
838 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800839 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700840
841 result = android::readFromParcel(mTransform, *parcel);
842 if (result != OK) {
843 return result;
844 }
Jeff Brown5912f952013-07-01 19:10:31 -0700845 mXPrecision = parcel->readFloat();
846 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700847 mRawXCursorPosition = parcel->readFloat();
848 mRawYCursorPosition = parcel->readFloat();
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700849
850 result = android::readFromParcel(mRawTransform, *parcel);
851 if (result != OK) {
852 return result;
853 }
Jeff Brown5912f952013-07-01 19:10:31 -0700854 mDownTime = parcel->readInt64();
855
856 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800857 mPointerProperties.reserve(pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700858 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500859 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700860 mSamplePointerCoords.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800861 mSamplePointerCoords.reserve(sampleCount * pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700862
863 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800864 mPointerProperties.push_back({});
865 PointerProperties& properties = mPointerProperties.back();
Jeff Brown5912f952013-07-01 19:10:31 -0700866 properties.id = parcel->readInt32();
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700867 properties.toolType = static_cast<ToolType>(parcel->readInt32());
Jeff Brown5912f952013-07-01 19:10:31 -0700868 }
869
Dan Austinc94fc452015-09-22 14:22:41 -0700870 while (sampleCount > 0) {
871 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500872 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700873 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800874 mSamplePointerCoords.push_back({});
875 status_t status = mSamplePointerCoords.back().readFromParcel(parcel);
Jeff Brown5912f952013-07-01 19:10:31 -0700876 if (status) {
877 return status;
878 }
879 }
880 }
881 return OK;
882}
883
884status_t MotionEvent::writeToParcel(Parcel* parcel) const {
885 size_t pointerCount = mPointerProperties.size();
886 size_t sampleCount = mSampleEventTimes.size();
887
888 parcel->writeInt32(pointerCount);
889 parcel->writeInt32(sampleCount);
890
Garfield Tan4cc839f2020-01-24 11:26:14 -0800891 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700892 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600893 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800894 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600895 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
896 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700897 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100898 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700899 parcel->writeInt32(mFlags);
900 parcel->writeInt32(mEdgeFlags);
901 parcel->writeInt32(mMetaState);
902 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800903 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700904
905 status_t result = android::writeToParcel(mTransform, *parcel);
906 if (result != OK) {
907 return result;
908 }
Jeff Brown5912f952013-07-01 19:10:31 -0700909 parcel->writeFloat(mXPrecision);
910 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700911 parcel->writeFloat(mRawXCursorPosition);
912 parcel->writeFloat(mRawYCursorPosition);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700913
914 result = android::writeToParcel(mRawTransform, *parcel);
915 if (result != OK) {
916 return result;
917 }
Jeff Brown5912f952013-07-01 19:10:31 -0700918 parcel->writeInt64(mDownTime);
919
920 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800921 const PointerProperties& properties = mPointerProperties[i];
Jeff Brown5912f952013-07-01 19:10:31 -0700922 parcel->writeInt32(properties.id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700923 parcel->writeInt32(static_cast<int32_t>(properties.toolType));
Jeff Brown5912f952013-07-01 19:10:31 -0700924 }
925
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800926 const PointerCoords* pc = mSamplePointerCoords.data();
Jeff Brown5912f952013-07-01 19:10:31 -0700927 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500928 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700929 for (size_t i = 0; i < pointerCount; i++) {
930 status_t status = (pc++)->writeToParcel(parcel);
931 if (status) {
932 return status;
933 }
934 }
935 }
936 return OK;
937}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800938#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700939
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600940bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700941 if (isFromSource(source, AINPUT_SOURCE_CLASS_POINTER)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700942 // Specifically excludes HOVER_MOVE and SCROLL.
943 switch (action & AMOTION_EVENT_ACTION_MASK) {
944 case AMOTION_EVENT_ACTION_DOWN:
945 case AMOTION_EVENT_ACTION_MOVE:
946 case AMOTION_EVENT_ACTION_UP:
947 case AMOTION_EVENT_ACTION_POINTER_DOWN:
948 case AMOTION_EVENT_ACTION_POINTER_UP:
949 case AMOTION_EVENT_ACTION_CANCEL:
950 case AMOTION_EVENT_ACTION_OUTSIDE:
951 return true;
952 }
953 }
954 return false;
955}
956
Michael Wright872db4f2014-04-22 15:03:51 -0700957const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700958 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700959}
960
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800961std::optional<int> MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700962 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700963}
964
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500965std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700966 // Convert MotionEvent action to string
967 switch (action & AMOTION_EVENT_ACTION_MASK) {
968 case AMOTION_EVENT_ACTION_DOWN:
969 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700970 case AMOTION_EVENT_ACTION_UP:
971 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500972 case AMOTION_EVENT_ACTION_MOVE:
973 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700974 case AMOTION_EVENT_ACTION_CANCEL:
975 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500976 case AMOTION_EVENT_ACTION_OUTSIDE:
977 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700978 case AMOTION_EVENT_ACTION_POINTER_DOWN:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000979 return StringPrintf("POINTER_DOWN(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700980 case AMOTION_EVENT_ACTION_POINTER_UP:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000981 return StringPrintf("POINTER_UP(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500982 case AMOTION_EVENT_ACTION_HOVER_MOVE:
983 return "HOVER_MOVE";
984 case AMOTION_EVENT_ACTION_SCROLL:
985 return "SCROLL";
986 case AMOTION_EVENT_ACTION_HOVER_ENTER:
987 return "HOVER_ENTER";
988 case AMOTION_EVENT_ACTION_HOVER_EXIT:
989 return "HOVER_EXIT";
990 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
991 return "BUTTON_PRESS";
992 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
993 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700994 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500995 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700996}
997
Prabir Pradhanbf9b0a82024-02-29 02:23:50 +0000998std::tuple<int32_t, std::vector<PointerProperties>, std::vector<PointerCoords>> MotionEvent::split(
999 int32_t action, int32_t flags, int32_t historySize,
1000 const std::vector<PointerProperties>& pointerProperties,
1001 const std::vector<PointerCoords>& pointerCoords,
1002 std::bitset<MAX_POINTER_ID + 1> splitPointerIds) {
1003 LOG_ALWAYS_FATAL_IF(!splitPointerIds.any());
1004 const auto pointerCount = pointerProperties.size();
1005 LOG_ALWAYS_FATAL_IF(pointerCoords.size() != (pointerCount * (historySize + 1)));
1006 const auto splitCount = splitPointerIds.count();
1007
1008 std::vector<PointerProperties> splitPointerProperties;
1009 std::vector<PointerCoords> splitPointerCoords;
1010
1011 for (uint32_t i = 0; i < pointerCount; i++) {
1012 if (splitPointerIds.test(pointerProperties[i].id)) {
1013 splitPointerProperties.emplace_back(pointerProperties[i]);
1014 }
1015 }
1016 for (uint32_t i = 0; i < pointerCoords.size(); i++) {
1017 if (splitPointerIds.test(pointerProperties[i % pointerCount].id)) {
1018 splitPointerCoords.emplace_back(pointerCoords[i]);
1019 }
1020 }
1021 LOG_ALWAYS_FATAL_IF(splitPointerCoords.size() !=
1022 (splitPointerProperties.size() * (historySize + 1)));
1023
1024 if (CC_UNLIKELY(splitPointerProperties.size() != splitCount)) {
1025 LOG(FATAL) << "Cannot split MotionEvent: Requested splitting " << splitCount
1026 << " pointers from the original event, but the original event only contained "
1027 << splitPointerProperties.size() << " of those pointers.";
1028 }
1029
1030 // TODO(b/327503168): Verify the splitDownTime here once it is used correctly.
1031
1032 const auto splitAction = resolveActionForSplitMotionEvent(action, flags, pointerProperties,
1033 splitPointerProperties);
1034 return {splitAction, splitPointerProperties, splitPointerCoords};
1035}
1036
Prabir Pradhan7e1ee562021-10-26 10:19:49 -07001037// Apply the given transformation to the point without checking whether the entire transform
1038// should be disregarded altogether for the provided source.
1039static inline vec2 calculateTransformedXYUnchecked(uint32_t source, const ui::Transform& transform,
1040 const vec2& xy) {
1041 return shouldDisregardOffset(source) ? transformWithoutTranslation(transform, xy)
Prabir Pradhan00e029d2023-03-09 20:11:09 +00001042 : roundTransformedCoords(transform.transform(xy));
Prabir Pradhan7e1ee562021-10-26 10:19:49 -07001043}
1044
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07001045vec2 MotionEvent::calculateTransformedXY(uint32_t source, const ui::Transform& transform,
1046 const vec2& xy) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -07001047 if (shouldDisregardTransformation(source)) {
1048 return xy;
1049 }
1050 return calculateTransformedXYUnchecked(source, transform, xy);
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07001051}
1052
Prabir Pradhan8e6ce222022-02-24 09:08:54 -08001053// Keep in sync with calculateTransformedCoords.
Prabir Pradhan9eb02c02021-10-19 14:02:20 -07001054float MotionEvent::calculateTransformedAxisValue(int32_t axis, uint32_t source,
1055 const ui::Transform& transform,
1056 const PointerCoords& coords) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -07001057 if (shouldDisregardTransformation(source)) {
1058 return coords.getAxisValue(axis);
1059 }
1060
Prabir Pradhan9eb02c02021-10-19 14:02:20 -07001061 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -07001062 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
Prabir Pradhan9eb02c02021-10-19 14:02:20 -07001063 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
1064 return xy[axis];
1065 }
1066
1067 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
1068 const vec2 relativeXy =
1069 transformWithoutTranslation(transform,
1070 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
1071 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
1072 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
1073 }
1074
1075 if (axis == AMOTION_EVENT_AXIS_ORIENTATION) {
1076 return transformAngle(transform, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION));
1077 }
1078
1079 return coords.getAxisValue(axis);
1080}
1081
Prabir Pradhan8e6ce222022-02-24 09:08:54 -08001082// Keep in sync with calculateTransformedAxisValue. This is an optimization of
1083// calculateTransformedAxisValue for all PointerCoords axes.
1084PointerCoords MotionEvent::calculateTransformedCoords(uint32_t source,
1085 const ui::Transform& transform,
1086 const PointerCoords& coords) {
1087 if (shouldDisregardTransformation(source)) {
1088 return coords;
1089 }
1090 PointerCoords out = coords;
1091
1092 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
1093 out.setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
1094 out.setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
1095
1096 const vec2 relativeXy =
1097 transformWithoutTranslation(transform,
1098 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
1099 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
1100 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
1101 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
1102
1103 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
1104 transformAngle(transform,
1105 coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)));
1106
1107 return out;
1108}
1109
Prabir Pradhan65a071a2024-01-05 20:52:09 +00001110bool MotionEvent::operator==(const android::MotionEvent& o) const {
1111 // We use NaN values to represent invalid cursor positions. Since NaN values are not equal
1112 // to themselves according to IEEE 754, we cannot use the default equality operator to compare
1113 // MotionEvents. Therefore we define a custom equality operator with special handling for NaNs.
1114 // clang-format off
1115 return InputEvent::operator==(static_cast<const InputEvent&>(o)) &&
1116 mAction == o.mAction &&
1117 mActionButton == o.mActionButton &&
1118 mFlags == o.mFlags &&
1119 mEdgeFlags == o.mEdgeFlags &&
1120 mMetaState == o.mMetaState &&
1121 mButtonState == o.mButtonState &&
1122 mClassification == o.mClassification &&
1123 mTransform == o.mTransform &&
1124 mXPrecision == o.mXPrecision &&
1125 mYPrecision == o.mYPrecision &&
1126 ((std::isnan(mRawXCursorPosition) && std::isnan(o.mRawXCursorPosition)) ||
1127 mRawXCursorPosition == o.mRawXCursorPosition) &&
1128 ((std::isnan(mRawYCursorPosition) && std::isnan(o.mRawYCursorPosition)) ||
1129 mRawYCursorPosition == o.mRawYCursorPosition) &&
1130 mRawTransform == o.mRawTransform && mDownTime == o.mDownTime &&
1131 mPointerProperties == o.mPointerProperties &&
1132 mSampleEventTimes == o.mSampleEventTimes &&
1133 mSamplePointerCoords == o.mSamplePointerCoords;
1134 // clang-format on
1135}
1136
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +00001137std::ostream& operator<<(std::ostream& out, const MotionEvent& event) {
1138 out << "MotionEvent { action=" << MotionEvent::actionToString(event.getAction());
1139 if (event.getActionButton() != 0) {
1140 out << ", actionButton=" << std::to_string(event.getActionButton());
1141 }
1142 const size_t pointerCount = event.getPointerCount();
hupeng3aa5a51a2022-09-02 16:00:18 +08001143 LOG_ALWAYS_FATAL_IF(pointerCount > MAX_POINTERS, "Too many pointers : pointerCount = %zu",
1144 pointerCount);
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +00001145 for (size_t i = 0; i < pointerCount; i++) {
1146 out << ", id[" << i << "]=" << event.getPointerId(i);
1147 float x = event.getX(i);
1148 float y = event.getY(i);
1149 if (x != 0 || y != 0) {
1150 out << ", x[" << i << "]=" << x;
1151 out << ", y[" << i << "]=" << y;
1152 }
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001153 ToolType toolType = event.getToolType(i);
1154 if (toolType != ToolType::FINGER) {
1155 out << ", toolType[" << i << "]=" << ftl::enum_string(toolType);
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +00001156 }
1157 }
1158 if (event.getButtonState() != 0) {
1159 out << ", buttonState=" << event.getButtonState();
1160 }
1161 if (event.getClassification() != MotionClassification::NONE) {
1162 out << ", classification=" << motionClassificationToString(event.getClassification());
1163 }
1164 if (event.getMetaState() != 0) {
1165 out << ", metaState=" << event.getMetaState();
1166 }
Prabir Pradhan65455c72024-02-13 21:46:41 +00001167 if (event.getFlags() != 0) {
1168 out << ", flags=0x" << std::hex << event.getFlags() << std::dec;
1169 }
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +00001170 if (event.getEdgeFlags() != 0) {
1171 out << ", edgeFlags=" << event.getEdgeFlags();
1172 }
1173 if (pointerCount != 1) {
1174 out << ", pointerCount=" << pointerCount;
1175 }
1176 if (event.getHistorySize() != 0) {
1177 out << ", historySize=" << event.getHistorySize();
1178 }
1179 out << ", eventTime=" << event.getEventTime();
1180 out << ", downTime=" << event.getDownTime();
1181 out << ", deviceId=" << event.getDeviceId();
1182 out << ", source=" << inputEventSourceToString(event.getSource());
1183 out << ", displayId=" << event.getDisplayId();
Prabir Pradhanf5abab62024-02-01 20:51:32 +00001184 out << ", eventId=0x" << std::hex << event.getId() << std::dec;
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +00001185 out << "}";
1186 return out;
1187}
1188
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001189// --- FocusEvent ---
1190
Antonio Kantek3cfec7b2021-11-05 18:26:17 -07001191void FocusEvent::initialize(int32_t id, bool hasFocus) {
Garfield Tan4cc839f2020-01-24 11:26:14 -08001192 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -06001193 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001194 mHasFocus = hasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001195}
1196
1197void FocusEvent::initialize(const FocusEvent& from) {
1198 InputEvent::initialize(from);
1199 mHasFocus = from.mHasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001200}
Jeff Brown5912f952013-07-01 19:10:31 -07001201
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001202// --- CaptureEvent ---
1203
1204void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) {
1205 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1206 ADISPLAY_ID_NONE, INVALID_HMAC);
1207 mPointerCaptureEnabled = pointerCaptureEnabled;
1208}
1209
1210void CaptureEvent::initialize(const CaptureEvent& from) {
1211 InputEvent::initialize(from);
1212 mPointerCaptureEnabled = from.mPointerCaptureEnabled;
1213}
1214
arthurhung7632c332020-12-30 16:58:01 +08001215// --- DragEvent ---
1216
1217void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) {
1218 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1219 ADISPLAY_ID_NONE, INVALID_HMAC);
1220 mIsExiting = isExiting;
1221 mX = x;
1222 mY = y;
1223}
1224
1225void DragEvent::initialize(const DragEvent& from) {
1226 InputEvent::initialize(from);
1227 mIsExiting = from.mIsExiting;
1228 mX = from.mX;
1229 mY = from.mY;
1230}
1231
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001232// --- TouchModeEvent ---
1233
1234void TouchModeEvent::initialize(int32_t id, bool isInTouchMode) {
1235 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1236 ADISPLAY_ID_NONE, INVALID_HMAC);
1237 mIsInTouchMode = isInTouchMode;
1238}
1239
1240void TouchModeEvent::initialize(const TouchModeEvent& from) {
1241 InputEvent::initialize(from);
1242 mIsInTouchMode = from.mIsInTouchMode;
1243}
1244
Jeff Brown5912f952013-07-01 19:10:31 -07001245// --- PooledInputEventFactory ---
1246
1247PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
1248 mMaxPoolSize(maxPoolSize) {
1249}
1250
1251PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -07001252}
1253
1254KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001255 if (mKeyEventPool.empty()) {
1256 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001257 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001258 KeyEvent* event = mKeyEventPool.front().release();
1259 mKeyEventPool.pop();
1260 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001261}
1262
1263MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001264 if (mMotionEventPool.empty()) {
1265 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001266 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001267 MotionEvent* event = mMotionEventPool.front().release();
1268 mMotionEventPool.pop();
1269 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001270}
1271
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001272FocusEvent* PooledInputEventFactory::createFocusEvent() {
1273 if (mFocusEventPool.empty()) {
1274 return new FocusEvent();
1275 }
1276 FocusEvent* event = mFocusEventPool.front().release();
1277 mFocusEventPool.pop();
1278 return event;
1279}
1280
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001281CaptureEvent* PooledInputEventFactory::createCaptureEvent() {
1282 if (mCaptureEventPool.empty()) {
1283 return new CaptureEvent();
1284 }
1285 CaptureEvent* event = mCaptureEventPool.front().release();
1286 mCaptureEventPool.pop();
1287 return event;
1288}
1289
arthurhung7632c332020-12-30 16:58:01 +08001290DragEvent* PooledInputEventFactory::createDragEvent() {
1291 if (mDragEventPool.empty()) {
1292 return new DragEvent();
1293 }
1294 DragEvent* event = mDragEventPool.front().release();
1295 mDragEventPool.pop();
1296 return event;
1297}
1298
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001299TouchModeEvent* PooledInputEventFactory::createTouchModeEvent() {
1300 if (mTouchModeEventPool.empty()) {
1301 return new TouchModeEvent();
1302 }
1303 TouchModeEvent* event = mTouchModeEventPool.front().release();
1304 mTouchModeEventPool.pop();
1305 return event;
1306}
1307
Jeff Brown5912f952013-07-01 19:10:31 -07001308void PooledInputEventFactory::recycle(InputEvent* event) {
1309 switch (event->getType()) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001310 case InputEventType::KEY: {
1311 if (mKeyEventPool.size() < mMaxPoolSize) {
1312 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
1313 return;
1314 }
1315 break;
Jeff Brown5912f952013-07-01 19:10:31 -07001316 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001317 case InputEventType::MOTION: {
1318 if (mMotionEventPool.size() < mMaxPoolSize) {
1319 mMotionEventPool.push(
1320 std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
1321 return;
1322 }
1323 break;
Jeff Brown5912f952013-07-01 19:10:31 -07001324 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001325 case InputEventType::FOCUS: {
1326 if (mFocusEventPool.size() < mMaxPoolSize) {
1327 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
1328 return;
1329 }
1330 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001331 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001332 case InputEventType::CAPTURE: {
1333 if (mCaptureEventPool.size() < mMaxPoolSize) {
1334 mCaptureEventPool.push(
1335 std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event)));
1336 return;
1337 }
1338 break;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001339 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001340 case InputEventType::DRAG: {
1341 if (mDragEventPool.size() < mMaxPoolSize) {
1342 mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event)));
1343 return;
1344 }
1345 break;
arthurhung7632c332020-12-30 16:58:01 +08001346 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001347 case InputEventType::TOUCH_MODE: {
1348 if (mTouchModeEventPool.size() < mMaxPoolSize) {
1349 mTouchModeEventPool.push(
1350 std::unique_ptr<TouchModeEvent>(static_cast<TouchModeEvent*>(event)));
1351 return;
1352 }
1353 break;
Antonio Kantekeb4a30c2021-09-28 17:49:49 -07001354 }
Jeff Brown5912f952013-07-01 19:10:31 -07001355 }
1356 delete event;
1357}
1358
1359} // namespace android