blob: 0a3f1fd463c077124c53a296087e0ed9f3507ea7 [file] [log] [blame]
Jeff Brown5912f952013-07-01 19:10:31 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Input"
18//#define LOG_NDEBUG 0
19
chaviw09c8d2d2020-08-24 15:48:26 -070020#include <attestation/HmacKeyManager.h>
Garfield Tan84b087e2020-01-23 10:49:05 -080021#include <cutils/compiler.h>
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050022#include <inttypes.h>
Garfield Tan84b087e2020-01-23 10:49:05 -080023#include <string.h>
Michael Wright635422b2022-12-02 00:43:56 +000024#include <optional>
Jeff Brown5912f952013-07-01 19:10:31 -070025
Siarhei Vishniakou31977182022-09-30 08:51:23 -070026#include <android-base/file.h>
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +000027#include <android-base/logging.h>
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050028#include <android-base/stringprintf.h>
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +000029#include <cutils/compiler.h>
Prabir Pradhan092f3a92021-11-25 10:53:27 -080030#include <input/DisplayViewport.h>
Jeff Brown5912f952013-07-01 19:10:31 -070031#include <input/Input.h>
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080032#include <input/InputDevice.h>
Michael Wright872db4f2014-04-22 15:03:51 -070033#include <input/InputEventLabels.h>
Jeff Brown5912f952013-07-01 19:10:31 -070034
Jeff Brown5912f952013-07-01 19:10:31 -070035#include <binder/Parcel.h>
Siarhei Vishniakou63740b92022-10-20 10:28:08 -070036#if defined(__ANDROID__)
37#include <sys/random.h>
38#endif
Jeff Brown5912f952013-07-01 19:10:31 -070039
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050040using android::base::StringPrintf;
41
Jeff Brown5912f952013-07-01 19:10:31 -070042namespace android {
43
Prabir Pradhan6b384612021-05-14 16:56:25 -070044namespace {
45
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070046bool shouldDisregardTransformation(uint32_t source) {
Prabir Pradhan258e2b92022-06-24 18:37:04 +000047 // Do not apply any transformations to axes from joysticks, touchpads, or relative mice.
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070048 return isFromSource(source, AINPUT_SOURCE_CLASS_JOYSTICK) ||
Prabir Pradhan258e2b92022-06-24 18:37:04 +000049 isFromSource(source, AINPUT_SOURCE_CLASS_POSITION) ||
50 isFromSource(source, AINPUT_SOURCE_MOUSE_RELATIVE);
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070051}
52
53bool shouldDisregardOffset(uint32_t source) {
Prabir Pradhan9f388812021-05-13 16:54:53 -070054 // Pointer events are the only type of events that refer to absolute coordinates on the display,
55 // so we should apply the entire window transform. For other types of events, we should make
56 // sure to not apply the window translation/offset.
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070057 return !isFromSource(source, AINPUT_SOURCE_CLASS_POINTER);
Prabir Pradhan9f388812021-05-13 16:54:53 -070058}
59
Prabir Pradhanbf9b0a82024-02-29 02:23:50 +000060int32_t resolveActionForSplitMotionEvent(
61 int32_t action, int32_t flags, const std::vector<PointerProperties>& pointerProperties,
62 const std::vector<PointerProperties>& splitPointerProperties) {
63 LOG_ALWAYS_FATAL_IF(splitPointerProperties.empty());
64 const auto maskedAction = MotionEvent::getActionMasked(action);
65 if (maskedAction != AMOTION_EVENT_ACTION_POINTER_DOWN &&
66 maskedAction != AMOTION_EVENT_ACTION_POINTER_UP) {
67 // The action is unaffected by splitting this motion event.
68 return action;
69 }
70 const auto actionIndex = MotionEvent::getActionIndex(action);
71 if (CC_UNLIKELY(actionIndex >= pointerProperties.size())) {
72 LOG(FATAL) << "Action index is out of bounds, index: " << actionIndex;
73 }
74
75 const auto affectedPointerId = pointerProperties[actionIndex].id;
76 std::optional<uint32_t> splitActionIndex;
77 for (uint32_t i = 0; i < splitPointerProperties.size(); i++) {
78 if (affectedPointerId == splitPointerProperties[i].id) {
79 splitActionIndex = i;
80 break;
81 }
82 }
83 if (!splitActionIndex.has_value()) {
84 // The affected pointer is not part of the split motion event.
85 return AMOTION_EVENT_ACTION_MOVE;
86 }
87
88 if (splitPointerProperties.size() > 1) {
89 return maskedAction | (*splitActionIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
90 }
91
92 if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) {
93 return ((flags & AMOTION_EVENT_FLAG_CANCELED) != 0) ? AMOTION_EVENT_ACTION_CANCEL
94 : AMOTION_EVENT_ACTION_UP;
95 }
96 return AMOTION_EVENT_ACTION_DOWN;
97}
98
Prabir Pradhan9a53b552024-06-04 02:59:40 +000099float transformOrientation(const ui::Transform& transform, const PointerCoords& coords,
100 int32_t motionEventFlags) {
101 if ((motionEventFlags & AMOTION_EVENT_PRIVATE_FLAG_SUPPORTS_ORIENTATION) == 0) {
102 return 0;
103 }
104
105 const bool isDirectionalAngle =
106 (motionEventFlags & AMOTION_EVENT_PRIVATE_FLAG_SUPPORTS_DIRECTIONAL_ORIENTATION) != 0;
107
108 return transformAngle(transform, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
109 isDirectionalAngle);
110}
111
Prabir Pradhan6b384612021-05-14 16:56:25 -0700112} // namespace
113
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800114const char* motionClassificationToString(MotionClassification classification) {
115 switch (classification) {
116 case MotionClassification::NONE:
117 return "NONE";
118 case MotionClassification::AMBIGUOUS_GESTURE:
119 return "AMBIGUOUS_GESTURE";
120 case MotionClassification::DEEP_PRESS:
121 return "DEEP_PRESS";
Harry Cutts2800fb02022-09-15 13:49:23 +0000122 case MotionClassification::TWO_FINGER_SWIPE:
123 return "TWO_FINGER_SWIPE";
Harry Cuttsc5748d12022-12-02 17:30:18 +0000124 case MotionClassification::MULTI_FINGER_SWIPE:
125 return "MULTI_FINGER_SWIPE";
Harry Cuttsb1e83552022-12-20 11:02:26 +0000126 case MotionClassification::PINCH:
127 return "PINCH";
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800128 }
129}
130
Garfield Tan84b087e2020-01-23 10:49:05 -0800131// --- IdGenerator ---
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700132#if defined(__ANDROID__)
133[[maybe_unused]]
134#endif
135static status_t
136getRandomBytes(uint8_t* data, size_t size) {
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700137 int ret = TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY | O_CLOEXEC | O_NOFOLLOW));
138 if (ret == -1) {
139 return -errno;
140 }
141
142 base::unique_fd fd(ret);
143 if (!base::ReadFully(fd, data, size)) {
144 return -errno;
145 }
146 return OK;
147}
148
Garfield Tan84b087e2020-01-23 10:49:05 -0800149IdGenerator::IdGenerator(Source source) : mSource(source) {}
150
151int32_t IdGenerator::nextId() const {
152 constexpr uint32_t SEQUENCE_NUMBER_MASK = ~SOURCE_MASK;
153 int32_t id = 0;
154
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700155#if defined(__ANDROID__)
156 // On device, prefer 'getrandom' to '/dev/urandom' because it's faster.
157 constexpr size_t BUF_LEN = sizeof(id);
158 size_t totalBytes = 0;
159 while (totalBytes < BUF_LEN) {
160 ssize_t bytes = TEMP_FAILURE_RETRY(getrandom(&id, BUF_LEN, GRND_NONBLOCK));
161 if (CC_UNLIKELY(bytes < 0)) {
162 ALOGW("Failed to fill in random number for sequence number: %s.", strerror(errno));
163 id = 0;
164 break;
165 }
166 totalBytes += bytes;
167 }
168#else
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700169#if defined(__linux__)
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700170 // On host, <sys/random.h> / GRND_NONBLOCK is not available
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700171 while (true) {
172 status_t result = getRandomBytes(reinterpret_cast<uint8_t*>(&id), sizeof(id));
173 if (result == OK) {
Garfield Tan84b087e2020-01-23 10:49:05 -0800174 break;
175 }
Garfield Tan84b087e2020-01-23 10:49:05 -0800176 }
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700177#endif // __linux__
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700178#endif // __ANDROID__
Garfield Tan84b087e2020-01-23 10:49:05 -0800179 return (id & SEQUENCE_NUMBER_MASK) | static_cast<int32_t>(mSource);
180}
181
Jeff Brown5912f952013-07-01 19:10:31 -0700182// --- InputEvent ---
183
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000184// Due to precision limitations when working with floating points, transforming - namely
185// scaling - floating points can lead to minute errors. We round transformed values to approximately
186// three decimal places so that values like 0.99997 show up as 1.0.
187inline float roundTransformedCoords(float val) {
188 // Use a power to two to approximate three decimal places to potentially reduce some cycles.
189 // This should be at least as precise as MotionEvent::ROUNDING_PRECISION.
190 return std::round(val * 1024.f) / 1024.f;
191}
192
193inline vec2 roundTransformedCoords(vec2 p) {
194 return {roundTransformedCoords(p.x), roundTransformedCoords(p.y)};
195}
196
Prabir Pradhande69f8a2021-11-18 16:40:34 +0000197vec2 transformWithoutTranslation(const ui::Transform& transform, const vec2& xy) {
198 const vec2 transformedXy = transform.transform(xy);
199 const vec2 transformedOrigin = transform.transform(0, 0);
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000200 return roundTransformedCoords(transformedXy - transformedOrigin);
Prabir Pradhande69f8a2021-11-18 16:40:34 +0000201}
202
Prabir Pradhan9a53b552024-06-04 02:59:40 +0000203float transformAngle(const ui::Transform& transform, float angleRadians, bool isDirectional) {
Prabir Pradhane2e10b42022-11-17 20:59:36 +0000204 // Construct and transform a vector oriented at the specified clockwise angle from vertical.
205 // Coordinate system: down is increasing Y, right is increasing X.
206 float x = sinf(angleRadians);
207 float y = -cosf(angleRadians);
208 vec2 transformedPoint = transform.transform(x, y);
209
210 // Determine how the origin is transformed by the matrix so that we
211 // can transform orientation vectors.
212 const vec2 origin = transform.transform(0, 0);
213
214 transformedPoint.x -= origin.x;
215 transformedPoint.y -= origin.y;
216
Prabir Pradhan9a53b552024-06-04 02:59:40 +0000217 if (!isDirectional && transformedPoint.y > 0) {
218 // Limit the range of atan2f to [-pi/2, pi/2] by reversing the direction of the vector.
219 transformedPoint *= -1;
220 }
221
Prabir Pradhane2e10b42022-11-17 20:59:36 +0000222 // Derive the transformed vector's clockwise angle from vertical.
223 // The return value of atan2f is in range [-pi, pi] which conforms to the orientation API.
224 return atan2f(transformedPoint.x, -transformedPoint.y);
225}
226
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800227std::string inputEventSourceToString(int32_t source) {
228 if (source == AINPUT_SOURCE_UNKNOWN) {
229 return "UNKNOWN";
230 }
231 if (source == static_cast<int32_t>(AINPUT_SOURCE_ANY)) {
232 return "ANY";
233 }
234 static const std::map<int32_t, const char*> SOURCES{
235 {AINPUT_SOURCE_KEYBOARD, "KEYBOARD"},
236 {AINPUT_SOURCE_DPAD, "DPAD"},
237 {AINPUT_SOURCE_GAMEPAD, "GAMEPAD"},
238 {AINPUT_SOURCE_TOUCHSCREEN, "TOUCHSCREEN"},
239 {AINPUT_SOURCE_MOUSE, "MOUSE"},
240 {AINPUT_SOURCE_STYLUS, "STYLUS"},
241 {AINPUT_SOURCE_BLUETOOTH_STYLUS, "BLUETOOTH_STYLUS"},
242 {AINPUT_SOURCE_TRACKBALL, "TRACKBALL"},
243 {AINPUT_SOURCE_MOUSE_RELATIVE, "MOUSE_RELATIVE"},
244 {AINPUT_SOURCE_TOUCHPAD, "TOUCHPAD"},
245 {AINPUT_SOURCE_TOUCH_NAVIGATION, "TOUCH_NAVIGATION"},
246 {AINPUT_SOURCE_JOYSTICK, "JOYSTICK"},
247 {AINPUT_SOURCE_HDMI, "HDMI"},
248 {AINPUT_SOURCE_SENSOR, "SENSOR"},
249 {AINPUT_SOURCE_ROTARY_ENCODER, "ROTARY_ENCODER"},
250 };
251 std::string result;
252 for (const auto& [source_entry, str] : SOURCES) {
253 if ((source & source_entry) == source_entry) {
254 if (!result.empty()) {
255 result += " | ";
256 }
257 result += str;
258 }
259 }
260 if (result.empty()) {
261 result = StringPrintf("0x%08x", source);
262 }
263 return result;
264}
265
266bool isFromSource(uint32_t source, uint32_t test) {
267 return (source & test) == test;
268}
269
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700270bool isStylusToolType(ToolType toolType) {
271 return toolType == ToolType::STYLUS || toolType == ToolType::ERASER;
Prabir Pradhane5626962022-10-27 20:30:53 +0000272}
273
Siarhei Vishniakoudcc6e6e2023-10-18 09:20:07 -0700274bool isStylusEvent(uint32_t source, const std::vector<PointerProperties>& properties) {
275 if (!isFromSource(source, AINPUT_SOURCE_STYLUS)) {
276 return false;
277 }
278 // Need at least one stylus pointer for this event to be considered a stylus event
279 for (const PointerProperties& pointerProperties : properties) {
280 if (isStylusToolType(pointerProperties.toolType)) {
281 return true;
282 }
283 }
284 return false;
285}
286
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800287VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
288 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
289 event.getSource(), event.getDisplayId()},
290 event.getAction(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800291 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800292 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800293 event.getKeyCode(),
294 event.getScanCode(),
295 event.getMetaState(),
296 event.getRepeatCount()};
297}
298
299VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
300 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
301 event.getSource(), event.getDisplayId()},
302 event.getRawX(0),
303 event.getRawY(0),
304 event.getActionMasked(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800305 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800306 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800307 event.getMetaState(),
308 event.getButtonState()};
309}
310
Linnan Li13bf76a2024-05-05 19:18:02 +0800311void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source,
312 ui::LogicalDisplayId displayId, std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800313 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700314 mDeviceId = deviceId;
315 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100316 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600317 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700318}
319
320void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800321 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700322 mDeviceId = from.mDeviceId;
323 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100324 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600325 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700326}
327
Garfield Tan4cc839f2020-01-24 11:26:14 -0800328int32_t InputEvent::nextId() {
329 static IdGenerator idGen(IdGenerator::Source::OTHER);
330 return idGen.nextId();
331}
332
Siarhei Vishniakou63b63612023-04-12 11:00:23 -0700333std::ostream& operator<<(std::ostream& out, const InputEvent& event) {
334 switch (event.getType()) {
335 case InputEventType::KEY: {
336 const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event);
337 out << keyEvent;
338 return out;
339 }
340 case InputEventType::MOTION: {
341 const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event);
342 out << motionEvent;
343 return out;
344 }
345 case InputEventType::FOCUS: {
346 out << "FocusEvent";
347 return out;
348 }
349 case InputEventType::CAPTURE: {
350 out << "CaptureEvent";
351 return out;
352 }
353 case InputEventType::DRAG: {
354 out << "DragEvent";
355 return out;
356 }
357 case InputEventType::TOUCH_MODE: {
358 out << "TouchModeEvent";
359 return out;
360 }
361 }
362}
363
Jeff Brown5912f952013-07-01 19:10:31 -0700364// --- KeyEvent ---
365
Michael Wright872db4f2014-04-22 15:03:51 -0700366const char* KeyEvent::getLabel(int32_t keyCode) {
Chris Ye4958d062020-08-20 13:21:10 -0700367 return InputEventLookup::getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700368}
369
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800370std::optional<int> KeyEvent::getKeyCodeFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700371 return InputEventLookup::getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700372}
373
Linnan Li13bf76a2024-05-05 19:18:02 +0800374void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source,
375 ui::LogicalDisplayId displayId, std::array<uint8_t, 32> hmac,
376 int32_t action, int32_t flags, int32_t keyCode, int32_t scanCode,
377 int32_t metaState, int32_t repeatCount, nsecs_t downTime,
378 nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800379 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700380 mAction = action;
381 mFlags = flags;
382 mKeyCode = keyCode;
383 mScanCode = scanCode;
384 mMetaState = metaState;
385 mRepeatCount = repeatCount;
386 mDownTime = downTime;
387 mEventTime = eventTime;
388}
389
390void KeyEvent::initialize(const KeyEvent& from) {
391 InputEvent::initialize(from);
392 mAction = from.mAction;
393 mFlags = from.mFlags;
394 mKeyCode = from.mKeyCode;
395 mScanCode = from.mScanCode;
396 mMetaState = from.mMetaState;
397 mRepeatCount = from.mRepeatCount;
398 mDownTime = from.mDownTime;
399 mEventTime = from.mEventTime;
400}
401
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700402const char* KeyEvent::actionToString(int32_t action) {
403 // Convert KeyEvent action to string
404 switch (action) {
405 case AKEY_EVENT_ACTION_DOWN:
406 return "DOWN";
407 case AKEY_EVENT_ACTION_UP:
408 return "UP";
409 case AKEY_EVENT_ACTION_MULTIPLE:
410 return "MULTIPLE";
411 }
412 return "UNKNOWN";
413}
Jeff Brown5912f952013-07-01 19:10:31 -0700414
Siarhei Vishniakoud010b012023-01-18 15:00:53 -0800415std::ostream& operator<<(std::ostream& out, const KeyEvent& event) {
416 out << "KeyEvent { action=" << KeyEvent::actionToString(event.getAction());
417
418 out << ", keycode=" << event.getKeyCode() << "(" << KeyEvent::getLabel(event.getKeyCode())
419 << ")";
420
421 if (event.getMetaState() != 0) {
422 out << ", metaState=" << event.getMetaState();
423 }
424
425 out << ", eventTime=" << event.getEventTime();
426 out << ", downTime=" << event.getDownTime();
427 out << ", flags=" << std::hex << event.getFlags() << std::dec;
428 out << ", repeatCount=" << event.getRepeatCount();
429 out << ", deviceId=" << event.getDeviceId();
430 out << ", source=" << inputEventSourceToString(event.getSource());
431 out << ", displayId=" << event.getDisplayId();
Prabir Pradhanf5abab62024-02-01 20:51:32 +0000432 out << ", eventId=0x" << std::hex << event.getId() << std::dec;
Siarhei Vishniakoud010b012023-01-18 15:00:53 -0800433 out << "}";
434 return out;
435}
436
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -0800437std::ostream& operator<<(std::ostream& out, const PointerProperties& properties) {
438 out << "Pointer(id=" << properties.id << ", " << ftl::enum_string(properties.toolType) << ")";
439 return out;
440}
441
Jeff Brown5912f952013-07-01 19:10:31 -0700442// --- PointerCoords ---
443
444float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700445 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700446 return 0;
447 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700448 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700449}
450
451status_t PointerCoords::setAxisValue(int32_t axis, float value) {
452 if (axis < 0 || axis > 63) {
453 return NAME_NOT_FOUND;
454 }
455
Michael Wright38dcdff2014-03-19 12:06:10 -0700456 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
457 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700458 if (value == 0) {
459 return OK; // axes with value 0 do not need to be stored
460 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700461
462 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700463 if (count >= MAX_AXES) {
464 tooManyAxes(axis);
465 return NO_MEMORY;
466 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700467 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700468 for (uint32_t i = count; i > index; i--) {
469 values[i] = values[i - 1];
470 }
471 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700472
Jeff Brown5912f952013-07-01 19:10:31 -0700473 values[index] = value;
474 return OK;
475}
476
477static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
478 float value = c.getAxisValue(axis);
479 if (value != 0) {
480 c.setAxisValue(axis, value * scaleFactor);
481 }
482}
483
Robert Carre07e1032018-11-26 12:55:53 -0800484void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700485 // No need to scale pressure or size since they are normalized.
486 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800487
488 // If there is a global scale factor, it is included in the windowX/YScale
489 // so we don't need to apply it twice to the X/Y axes.
490 // However we don't want to apply any windowXYScale not included in the global scale
491 // to the TOUCH_MAJOR/MINOR coordinates.
492 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
493 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
494 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
495 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
496 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
497 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
Prabir Pradhanc6523582021-05-14 18:02:55 -0700498 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_X, windowXScale);
499 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_Y, windowYScale);
Robert Carre07e1032018-11-26 12:55:53 -0800500}
501
Jeff Brown5912f952013-07-01 19:10:31 -0700502status_t PointerCoords::readFromParcel(Parcel* parcel) {
503 bits = parcel->readInt64();
504
Michael Wright38dcdff2014-03-19 12:06:10 -0700505 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700506 if (count > MAX_AXES) {
507 return BAD_VALUE;
508 }
509
510 for (uint32_t i = 0; i < count; i++) {
511 values[i] = parcel->readFloat();
512 }
Philip Quinnafb31282022-12-20 18:17:55 -0800513
514 isResampled = parcel->readBool();
Jeff Brown5912f952013-07-01 19:10:31 -0700515 return OK;
516}
517
518status_t PointerCoords::writeToParcel(Parcel* parcel) const {
519 parcel->writeInt64(bits);
520
Michael Wright38dcdff2014-03-19 12:06:10 -0700521 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700522 for (uint32_t i = 0; i < count; i++) {
523 parcel->writeFloat(values[i]);
524 }
Philip Quinnafb31282022-12-20 18:17:55 -0800525
526 parcel->writeBool(isResampled);
Jeff Brown5912f952013-07-01 19:10:31 -0700527 return OK;
528}
Jeff Brown5912f952013-07-01 19:10:31 -0700529
530void PointerCoords::tooManyAxes(int axis) {
531 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
532 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
533}
534
535bool PointerCoords::operator==(const PointerCoords& other) const {
536 if (bits != other.bits) {
537 return false;
538 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700539 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700540 for (uint32_t i = 0; i < count; i++) {
541 if (values[i] != other.values[i]) {
542 return false;
543 }
544 }
Philip Quinnafb31282022-12-20 18:17:55 -0800545 if (isResampled != other.isResampled) {
546 return false;
547 }
Jeff Brown5912f952013-07-01 19:10:31 -0700548 return true;
549}
550
Prabir Pradhan9a53b552024-06-04 02:59:40 +0000551void PointerCoords::transform(const ui::Transform& transform, int32_t motionEventFlags) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700552 const vec2 xy = transform.transform(getXYValue());
553 setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
554 setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
555
Prabir Pradhanc6523582021-05-14 18:02:55 -0700556 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_X) ||
557 BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_Y)) {
558 const ui::Transform rotation(transform.getOrientation());
559 const vec2 relativeXy = rotation.transform(getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
560 getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
561 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
562 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
563 }
564
Prabir Pradhan9a53b552024-06-04 02:59:40 +0000565 if ((motionEventFlags & AMOTION_EVENT_PRIVATE_FLAG_SUPPORTS_ORIENTATION) != 0) {
566 setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
567 transformOrientation(transform, *this, motionEventFlags));
Prabir Pradhan6b384612021-05-14 16:56:25 -0700568 }
chaviwc01e1372020-07-01 12:37:31 -0700569}
Jeff Brown5912f952013-07-01 19:10:31 -0700570
Jeff Brown5912f952013-07-01 19:10:31 -0700571// --- MotionEvent ---
572
Linnan Li13bf76a2024-05-05 19:18:02 +0800573void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source,
574 ui::LogicalDisplayId displayId, std::array<uint8_t, 32> hmac,
575 int32_t action, int32_t actionButton, int32_t flags, int32_t edgeFlags,
576 int32_t metaState, int32_t buttonState,
577 MotionClassification classification, const ui::Transform& transform,
578 float xPrecision, float yPrecision, float rawXCursorPosition,
579 float rawYCursorPosition, const ui::Transform& rawTransform,
580 nsecs_t downTime, nsecs_t eventTime, size_t pointerCount,
581 const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700582 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800583 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700584 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100585 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700586 mFlags = flags;
587 mEdgeFlags = edgeFlags;
588 mMetaState = metaState;
589 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800590 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700591 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700592 mXPrecision = xPrecision;
593 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700594 mRawXCursorPosition = rawXCursorPosition;
595 mRawYCursorPosition = rawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700596 mRawTransform = rawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700597 mDownTime = downTime;
598 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800599 mPointerProperties.insert(mPointerProperties.end(), &pointerProperties[0],
600 &pointerProperties[pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700601 mSampleEventTimes.clear();
602 mSamplePointerCoords.clear();
603 addSample(eventTime, pointerCoords);
604}
605
606void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800607 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
608 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700609 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100610 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700611 mFlags = other->mFlags;
612 mEdgeFlags = other->mEdgeFlags;
613 mMetaState = other->mMetaState;
614 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800615 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700616 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700617 mXPrecision = other->mXPrecision;
618 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700619 mRawXCursorPosition = other->mRawXCursorPosition;
620 mRawYCursorPosition = other->mRawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700621 mRawTransform = other->mRawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700622 mDownTime = other->mDownTime;
623 mPointerProperties = other->mPointerProperties;
624
625 if (keepHistory) {
626 mSampleEventTimes = other->mSampleEventTimes;
627 mSamplePointerCoords = other->mSamplePointerCoords;
628 } else {
629 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500630 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700631 mSamplePointerCoords.clear();
632 size_t pointerCount = other->getPointerCount();
633 size_t historySize = other->getHistorySize();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800634 mSamplePointerCoords
635 .insert(mSamplePointerCoords.end(),
636 &other->mSamplePointerCoords[historySize * pointerCount],
637 &other->mSamplePointerCoords[historySize * pointerCount + pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700638 }
639}
640
Prabir Pradhanbf9b0a82024-02-29 02:23:50 +0000641void MotionEvent::splitFrom(const android::MotionEvent& other,
642 std::bitset<MAX_POINTER_ID + 1> splitPointerIds, int32_t newEventId) {
643 // TODO(b/327503168): The down time should be a parameter to the split function, because only
644 // the caller can know when the first event went down on the target.
645 const nsecs_t splitDownTime = other.mDownTime;
646
647 auto [action, pointerProperties, pointerCoords] =
648 split(other.getAction(), other.getFlags(), other.getHistorySize(),
649 other.mPointerProperties, other.mSamplePointerCoords, splitPointerIds);
650
651 // Initialize the event with zero pointers, and manually set the split pointers.
652 initialize(newEventId, other.mDeviceId, other.mSource, other.mDisplayId, /*hmac=*/{}, action,
653 other.mActionButton, other.mFlags, other.mEdgeFlags, other.mMetaState,
654 other.mButtonState, other.mClassification, other.mTransform, other.mXPrecision,
655 other.mYPrecision, other.mRawXCursorPosition, other.mRawYCursorPosition,
656 other.mRawTransform, splitDownTime, other.getEventTime(), /*pointerCount=*/0,
657 pointerProperties.data(), pointerCoords.data());
658 mPointerProperties = std::move(pointerProperties);
659 mSamplePointerCoords = std::move(pointerCoords);
660 mSampleEventTimes = other.mSampleEventTimes;
661}
662
Jeff Brown5912f952013-07-01 19:10:31 -0700663void MotionEvent::addSample(
664 int64_t eventTime,
665 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500666 mSampleEventTimes.push_back(eventTime);
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800667 mSamplePointerCoords.insert(mSamplePointerCoords.end(), &pointerCoords[0],
668 &pointerCoords[getPointerCount()]);
Jeff Brown5912f952013-07-01 19:10:31 -0700669}
670
Michael Wright635422b2022-12-02 00:43:56 +0000671std::optional<ui::Rotation> MotionEvent::getSurfaceRotation() const {
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800672 // The surface rotation is the rotation from the window's coordinate space to that of the
673 // display. Since the event's transform takes display space coordinates to window space, the
674 // returned surface rotation is the inverse of the rotation for the surface.
675 switch (mTransform.getOrientation()) {
676 case ui::Transform::ROT_0:
Michael Wright635422b2022-12-02 00:43:56 +0000677 return ui::ROTATION_0;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800678 case ui::Transform::ROT_90:
Michael Wright635422b2022-12-02 00:43:56 +0000679 return ui::ROTATION_270;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800680 case ui::Transform::ROT_180:
Michael Wright635422b2022-12-02 00:43:56 +0000681 return ui::ROTATION_180;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800682 case ui::Transform::ROT_270:
Michael Wright635422b2022-12-02 00:43:56 +0000683 return ui::ROTATION_90;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800684 default:
Michael Wright635422b2022-12-02 00:43:56 +0000685 return std::nullopt;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800686 }
687}
688
Garfield Tan00f511d2019-06-12 16:55:40 -0700689float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700690 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000691 return roundTransformedCoords(vals.x);
Garfield Tan00f511d2019-06-12 16:55:40 -0700692}
693
694float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700695 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
Prabir Pradhan00e029d2023-03-09 20:11:09 +0000696 return roundTransformedCoords(vals.y);
Garfield Tan00f511d2019-06-12 16:55:40 -0700697}
698
Garfield Tan937bb832019-07-25 17:48:31 -0700699void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700700 ui::Transform inverse = mTransform.inverse();
701 vec2 vals = inverse.transform(x, y);
702 mRawXCursorPosition = vals.x;
703 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700704}
705
Jeff Brown5912f952013-07-01 19:10:31 -0700706const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000707 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
708 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
709 }
710 const size_t position = getHistorySize() * getPointerCount() + pointerIndex;
711 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
712 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
713 }
714 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700715}
716
717float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
Evan Rosky84f07f02021-04-16 10:42:42 -0700718 return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700719}
720
721float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700722 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700723}
724
725const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
726 size_t pointerIndex, size_t historicalIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000727 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
728 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
729 }
730 if (CC_UNLIKELY(historicalIndex < 0 || historicalIndex > getHistorySize())) {
731 LOG(FATAL) << __func__ << ": Invalid historical index " << historicalIndex << " for "
732 << *this;
733 }
734 const size_t position = historicalIndex * getPointerCount() + pointerIndex;
735 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
736 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
737 }
738 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700739}
740
741float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan6b384612021-05-14 16:56:25 -0700742 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700743 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
Prabir Pradhan9a53b552024-06-04 02:59:40 +0000744 return calculateTransformedAxisValue(axis, mSource, mFlags, mRawTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700745}
746
747float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan9f388812021-05-13 16:54:53 -0700748 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700749 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
Prabir Pradhan9a53b552024-06-04 02:59:40 +0000750 return calculateTransformedAxisValue(axis, mSource, mFlags, mTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700751}
752
753ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
754 size_t pointerCount = mPointerProperties.size();
755 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800756 if (mPointerProperties[i].id == pointerId) {
Jeff Brown5912f952013-07-01 19:10:31 -0700757 return i;
758 }
759 }
760 return -1;
761}
762
763void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700764 float currXOffset = mTransform.tx();
765 float currYOffset = mTransform.ty();
766 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700767}
768
Prabir Pradhanadd8a4a2024-03-05 22:18:09 +0000769float MotionEvent::getRawXOffset() const {
770 // This is equivalent to the x-coordinate of the point that the origin of the raw coordinate
771 // space maps to.
772 return (mTransform * mRawTransform.inverse()).tx();
773}
774
775float MotionEvent::getRawYOffset() const {
776 // This is equivalent to the y-coordinate of the point that the origin of the raw coordinate
777 // space maps to.
778 return (mTransform * mRawTransform.inverse()).ty();
779}
780
Robert Carre07e1032018-11-26 12:55:53 -0800781void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700782 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700783 mRawTransform.set(mRawTransform.tx() * globalScaleFactor,
784 mRawTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800785 mXPrecision *= globalScaleFactor;
786 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700787
788 size_t numSamples = mSamplePointerCoords.size();
789 for (size_t i = 0; i < numSamples; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800790 mSamplePointerCoords[i].scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700791 }
792}
793
chaviw9eaa22c2020-07-01 16:21:27 -0700794void MotionEvent::transform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700795 // We want to preserve the raw axes values stored in the PointerCoords, so we just update the
796 // transform using the values passed in.
chaviw9eaa22c2020-07-01 16:21:27 -0700797 ui::Transform newTransform;
798 newTransform.set(matrix);
799 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700800}
801
Evan Roskyd4d4d802021-05-03 20:12:21 -0700802void MotionEvent::applyTransform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700803 ui::Transform transform;
804 transform.set(matrix);
Evan Roskyd4d4d802021-05-03 20:12:21 -0700805
806 // Apply the transformation to all samples.
Prabir Pradhan6b384612021-05-14 16:56:25 -0700807 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
Prabir Pradhan9a53b552024-06-04 02:59:40 +0000808 [&](PointerCoords& c) { c.transform(transform, mFlags); });
Prabir Pradhan4b19bd02021-06-01 17:34:59 -0700809
810 if (mRawXCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION &&
811 mRawYCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) {
812 const vec2 cursor = transform.transform(mRawXCursorPosition, mRawYCursorPosition);
813 mRawXCursorPosition = cursor.x;
814 mRawYCursorPosition = cursor.y;
815 }
Evan Roskyd4d4d802021-05-03 20:12:21 -0700816}
817
chaviw9eaa22c2020-07-01 16:21:27 -0700818static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
819 float dsdx, dtdx, tx, dtdy, dsdy, ty;
820 status_t status = parcel.readFloat(&dsdx);
821 status |= parcel.readFloat(&dtdx);
822 status |= parcel.readFloat(&tx);
823 status |= parcel.readFloat(&dtdy);
824 status |= parcel.readFloat(&dsdy);
825 status |= parcel.readFloat(&ty);
826
827 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
828 return status;
829}
830
831static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
832 status_t status = parcel.writeFloat(transform.dsdx());
833 status |= parcel.writeFloat(transform.dtdx());
834 status |= parcel.writeFloat(transform.tx());
835 status |= parcel.writeFloat(transform.dtdy());
836 status |= parcel.writeFloat(transform.dsdy());
837 status |= parcel.writeFloat(transform.ty());
838 return status;
839}
840
Jeff Brown5912f952013-07-01 19:10:31 -0700841status_t MotionEvent::readFromParcel(Parcel* parcel) {
842 size_t pointerCount = parcel->readInt32();
843 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800844 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
845 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700846 return BAD_VALUE;
847 }
848
Garfield Tan4cc839f2020-01-24 11:26:14 -0800849 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700850 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600851 mSource = parcel->readUint32();
Linnan Li13bf76a2024-05-05 19:18:02 +0800852 mDisplayId = ui::LogicalDisplayId{parcel->readInt32()};
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600853 std::vector<uint8_t> hmac;
854 status_t result = parcel->readByteVector(&hmac);
855 if (result != OK || hmac.size() != 32) {
856 return BAD_VALUE;
857 }
858 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700859 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100860 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700861 mFlags = parcel->readInt32();
862 mEdgeFlags = parcel->readInt32();
863 mMetaState = parcel->readInt32();
864 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800865 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700866
867 result = android::readFromParcel(mTransform, *parcel);
868 if (result != OK) {
869 return result;
870 }
Jeff Brown5912f952013-07-01 19:10:31 -0700871 mXPrecision = parcel->readFloat();
872 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700873 mRawXCursorPosition = parcel->readFloat();
874 mRawYCursorPosition = parcel->readFloat();
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700875
876 result = android::readFromParcel(mRawTransform, *parcel);
877 if (result != OK) {
878 return result;
879 }
Jeff Brown5912f952013-07-01 19:10:31 -0700880 mDownTime = parcel->readInt64();
881
882 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800883 mPointerProperties.reserve(pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700884 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500885 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700886 mSamplePointerCoords.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800887 mSamplePointerCoords.reserve(sampleCount * pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700888
889 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800890 mPointerProperties.push_back({});
891 PointerProperties& properties = mPointerProperties.back();
Jeff Brown5912f952013-07-01 19:10:31 -0700892 properties.id = parcel->readInt32();
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700893 properties.toolType = static_cast<ToolType>(parcel->readInt32());
Jeff Brown5912f952013-07-01 19:10:31 -0700894 }
895
Dan Austinc94fc452015-09-22 14:22:41 -0700896 while (sampleCount > 0) {
897 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500898 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700899 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800900 mSamplePointerCoords.push_back({});
901 status_t status = mSamplePointerCoords.back().readFromParcel(parcel);
Jeff Brown5912f952013-07-01 19:10:31 -0700902 if (status) {
903 return status;
904 }
905 }
906 }
907 return OK;
908}
909
910status_t MotionEvent::writeToParcel(Parcel* parcel) const {
911 size_t pointerCount = mPointerProperties.size();
912 size_t sampleCount = mSampleEventTimes.size();
913
914 parcel->writeInt32(pointerCount);
915 parcel->writeInt32(sampleCount);
916
Garfield Tan4cc839f2020-01-24 11:26:14 -0800917 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700918 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600919 parcel->writeUint32(mSource);
Linnan Li13bf76a2024-05-05 19:18:02 +0800920 parcel->writeInt32(mDisplayId.val());
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600921 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
922 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700923 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100924 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700925 parcel->writeInt32(mFlags);
926 parcel->writeInt32(mEdgeFlags);
927 parcel->writeInt32(mMetaState);
928 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800929 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700930
931 status_t result = android::writeToParcel(mTransform, *parcel);
932 if (result != OK) {
933 return result;
934 }
Jeff Brown5912f952013-07-01 19:10:31 -0700935 parcel->writeFloat(mXPrecision);
936 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700937 parcel->writeFloat(mRawXCursorPosition);
938 parcel->writeFloat(mRawYCursorPosition);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700939
940 result = android::writeToParcel(mRawTransform, *parcel);
941 if (result != OK) {
942 return result;
943 }
Jeff Brown5912f952013-07-01 19:10:31 -0700944 parcel->writeInt64(mDownTime);
945
946 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800947 const PointerProperties& properties = mPointerProperties[i];
Jeff Brown5912f952013-07-01 19:10:31 -0700948 parcel->writeInt32(properties.id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700949 parcel->writeInt32(static_cast<int32_t>(properties.toolType));
Jeff Brown5912f952013-07-01 19:10:31 -0700950 }
951
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800952 const PointerCoords* pc = mSamplePointerCoords.data();
Jeff Brown5912f952013-07-01 19:10:31 -0700953 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500954 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700955 for (size_t i = 0; i < pointerCount; i++) {
956 status_t status = (pc++)->writeToParcel(parcel);
957 if (status) {
958 return status;
959 }
960 }
961 }
962 return OK;
963}
Jeff Brown5912f952013-07-01 19:10:31 -0700964
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600965bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700966 if (isFromSource(source, AINPUT_SOURCE_CLASS_POINTER)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700967 // Specifically excludes HOVER_MOVE and SCROLL.
968 switch (action & AMOTION_EVENT_ACTION_MASK) {
969 case AMOTION_EVENT_ACTION_DOWN:
970 case AMOTION_EVENT_ACTION_MOVE:
971 case AMOTION_EVENT_ACTION_UP:
972 case AMOTION_EVENT_ACTION_POINTER_DOWN:
973 case AMOTION_EVENT_ACTION_POINTER_UP:
974 case AMOTION_EVENT_ACTION_CANCEL:
975 case AMOTION_EVENT_ACTION_OUTSIDE:
976 return true;
977 }
978 }
979 return false;
980}
981
Michael Wright872db4f2014-04-22 15:03:51 -0700982const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700983 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700984}
985
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800986std::optional<int> MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700987 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700988}
989
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500990std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700991 // Convert MotionEvent action to string
992 switch (action & AMOTION_EVENT_ACTION_MASK) {
993 case AMOTION_EVENT_ACTION_DOWN:
994 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700995 case AMOTION_EVENT_ACTION_UP:
996 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500997 case AMOTION_EVENT_ACTION_MOVE:
998 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700999 case AMOTION_EVENT_ACTION_CANCEL:
1000 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -05001001 case AMOTION_EVENT_ACTION_OUTSIDE:
1002 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001003 case AMOTION_EVENT_ACTION_POINTER_DOWN:
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001004 return StringPrintf("POINTER_DOWN(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001005 case AMOTION_EVENT_ACTION_POINTER_UP:
Siarhei Vishniakouca205502021-07-16 21:31:58 +00001006 return StringPrintf("POINTER_UP(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -05001007 case AMOTION_EVENT_ACTION_HOVER_MOVE:
1008 return "HOVER_MOVE";
1009 case AMOTION_EVENT_ACTION_SCROLL:
1010 return "SCROLL";
1011 case AMOTION_EVENT_ACTION_HOVER_ENTER:
1012 return "HOVER_ENTER";
1013 case AMOTION_EVENT_ACTION_HOVER_EXIT:
1014 return "HOVER_EXIT";
1015 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
1016 return "BUTTON_PRESS";
1017 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
1018 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001019 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -05001020 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001021}
1022
Prabir Pradhanbf9b0a82024-02-29 02:23:50 +00001023std::tuple<int32_t, std::vector<PointerProperties>, std::vector<PointerCoords>> MotionEvent::split(
1024 int32_t action, int32_t flags, int32_t historySize,
1025 const std::vector<PointerProperties>& pointerProperties,
1026 const std::vector<PointerCoords>& pointerCoords,
1027 std::bitset<MAX_POINTER_ID + 1> splitPointerIds) {
1028 LOG_ALWAYS_FATAL_IF(!splitPointerIds.any());
1029 const auto pointerCount = pointerProperties.size();
1030 LOG_ALWAYS_FATAL_IF(pointerCoords.size() != (pointerCount * (historySize + 1)));
1031 const auto splitCount = splitPointerIds.count();
1032
1033 std::vector<PointerProperties> splitPointerProperties;
1034 std::vector<PointerCoords> splitPointerCoords;
1035
1036 for (uint32_t i = 0; i < pointerCount; i++) {
1037 if (splitPointerIds.test(pointerProperties[i].id)) {
1038 splitPointerProperties.emplace_back(pointerProperties[i]);
1039 }
1040 }
1041 for (uint32_t i = 0; i < pointerCoords.size(); i++) {
1042 if (splitPointerIds.test(pointerProperties[i % pointerCount].id)) {
1043 splitPointerCoords.emplace_back(pointerCoords[i]);
1044 }
1045 }
1046 LOG_ALWAYS_FATAL_IF(splitPointerCoords.size() !=
1047 (splitPointerProperties.size() * (historySize + 1)));
1048
1049 if (CC_UNLIKELY(splitPointerProperties.size() != splitCount)) {
Prabir Pradhan1a41fe02024-03-11 18:38:40 +00001050 // TODO(b/329107108): Promote this to a fatal check once bugs in the caller are resolved.
1051 LOG(ERROR) << "Cannot split MotionEvent: Requested splitting " << splitCount
Prabir Pradhanbf9b0a82024-02-29 02:23:50 +00001052 << " pointers from the original event, but the original event only contained "
1053 << splitPointerProperties.size() << " of those pointers.";
1054 }
1055
1056 // TODO(b/327503168): Verify the splitDownTime here once it is used correctly.
1057
1058 const auto splitAction = resolveActionForSplitMotionEvent(action, flags, pointerProperties,
1059 splitPointerProperties);
1060 return {splitAction, splitPointerProperties, splitPointerCoords};
1061}
1062
Prabir Pradhan7e1ee562021-10-26 10:19:49 -07001063// Apply the given transformation to the point without checking whether the entire transform
1064// should be disregarded altogether for the provided source.
1065static inline vec2 calculateTransformedXYUnchecked(uint32_t source, const ui::Transform& transform,
1066 const vec2& xy) {
1067 return shouldDisregardOffset(source) ? transformWithoutTranslation(transform, xy)
Prabir Pradhan00e029d2023-03-09 20:11:09 +00001068 : roundTransformedCoords(transform.transform(xy));
Prabir Pradhan7e1ee562021-10-26 10:19:49 -07001069}
1070
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07001071vec2 MotionEvent::calculateTransformedXY(uint32_t source, const ui::Transform& transform,
1072 const vec2& xy) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -07001073 if (shouldDisregardTransformation(source)) {
1074 return xy;
1075 }
1076 return calculateTransformedXYUnchecked(source, transform, xy);
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07001077}
1078
Prabir Pradhan8e6ce222022-02-24 09:08:54 -08001079// Keep in sync with calculateTransformedCoords.
Prabir Pradhan9a53b552024-06-04 02:59:40 +00001080float MotionEvent::calculateTransformedAxisValue(int32_t axis, uint32_t source, int32_t flags,
Prabir Pradhan9eb02c02021-10-19 14:02:20 -07001081 const ui::Transform& transform,
1082 const PointerCoords& coords) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -07001083 if (shouldDisregardTransformation(source)) {
1084 return coords.getAxisValue(axis);
1085 }
1086
Prabir Pradhan9eb02c02021-10-19 14:02:20 -07001087 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -07001088 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
Prabir Pradhan9eb02c02021-10-19 14:02:20 -07001089 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
1090 return xy[axis];
1091 }
1092
1093 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
1094 const vec2 relativeXy =
1095 transformWithoutTranslation(transform,
1096 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
1097 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
1098 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
1099 }
1100
1101 if (axis == AMOTION_EVENT_AXIS_ORIENTATION) {
Prabir Pradhan9a53b552024-06-04 02:59:40 +00001102 return transformOrientation(transform, coords, flags);
Prabir Pradhan9eb02c02021-10-19 14:02:20 -07001103 }
1104
1105 return coords.getAxisValue(axis);
1106}
1107
Prabir Pradhan8e6ce222022-02-24 09:08:54 -08001108// Keep in sync with calculateTransformedAxisValue. This is an optimization of
1109// calculateTransformedAxisValue for all PointerCoords axes.
Prabir Pradhan9a53b552024-06-04 02:59:40 +00001110PointerCoords MotionEvent::calculateTransformedCoords(uint32_t source, int32_t flags,
Prabir Pradhan8e6ce222022-02-24 09:08:54 -08001111 const ui::Transform& transform,
1112 const PointerCoords& coords) {
1113 if (shouldDisregardTransformation(source)) {
1114 return coords;
1115 }
1116 PointerCoords out = coords;
1117
1118 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
1119 out.setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
1120 out.setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
1121
1122 const vec2 relativeXy =
1123 transformWithoutTranslation(transform,
1124 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
1125 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
1126 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
1127 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
1128
1129 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
Prabir Pradhan9a53b552024-06-04 02:59:40 +00001130 transformOrientation(transform, coords, flags));
Prabir Pradhan8e6ce222022-02-24 09:08:54 -08001131
1132 return out;
1133}
1134
Prabir Pradhan65a071a2024-01-05 20:52:09 +00001135bool MotionEvent::operator==(const android::MotionEvent& o) const {
1136 // We use NaN values to represent invalid cursor positions. Since NaN values are not equal
1137 // to themselves according to IEEE 754, we cannot use the default equality operator to compare
1138 // MotionEvents. Therefore we define a custom equality operator with special handling for NaNs.
1139 // clang-format off
1140 return InputEvent::operator==(static_cast<const InputEvent&>(o)) &&
1141 mAction == o.mAction &&
1142 mActionButton == o.mActionButton &&
1143 mFlags == o.mFlags &&
1144 mEdgeFlags == o.mEdgeFlags &&
1145 mMetaState == o.mMetaState &&
1146 mButtonState == o.mButtonState &&
1147 mClassification == o.mClassification &&
1148 mTransform == o.mTransform &&
1149 mXPrecision == o.mXPrecision &&
1150 mYPrecision == o.mYPrecision &&
1151 ((std::isnan(mRawXCursorPosition) && std::isnan(o.mRawXCursorPosition)) ||
1152 mRawXCursorPosition == o.mRawXCursorPosition) &&
1153 ((std::isnan(mRawYCursorPosition) && std::isnan(o.mRawYCursorPosition)) ||
1154 mRawYCursorPosition == o.mRawYCursorPosition) &&
1155 mRawTransform == o.mRawTransform && mDownTime == o.mDownTime &&
1156 mPointerProperties == o.mPointerProperties &&
1157 mSampleEventTimes == o.mSampleEventTimes &&
1158 mSamplePointerCoords == o.mSamplePointerCoords;
1159 // clang-format on
1160}
1161
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +00001162std::ostream& operator<<(std::ostream& out, const MotionEvent& event) {
1163 out << "MotionEvent { action=" << MotionEvent::actionToString(event.getAction());
1164 if (event.getActionButton() != 0) {
1165 out << ", actionButton=" << std::to_string(event.getActionButton());
1166 }
1167 const size_t pointerCount = event.getPointerCount();
hupeng3aa5a51a2022-09-02 16:00:18 +08001168 LOG_ALWAYS_FATAL_IF(pointerCount > MAX_POINTERS, "Too many pointers : pointerCount = %zu",
1169 pointerCount);
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +00001170 for (size_t i = 0; i < pointerCount; i++) {
1171 out << ", id[" << i << "]=" << event.getPointerId(i);
1172 float x = event.getX(i);
1173 float y = event.getY(i);
1174 if (x != 0 || y != 0) {
1175 out << ", x[" << i << "]=" << x;
1176 out << ", y[" << i << "]=" << y;
1177 }
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001178 ToolType toolType = event.getToolType(i);
1179 if (toolType != ToolType::FINGER) {
1180 out << ", toolType[" << i << "]=" << ftl::enum_string(toolType);
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +00001181 }
1182 }
1183 if (event.getButtonState() != 0) {
1184 out << ", buttonState=" << event.getButtonState();
1185 }
1186 if (event.getClassification() != MotionClassification::NONE) {
1187 out << ", classification=" << motionClassificationToString(event.getClassification());
1188 }
1189 if (event.getMetaState() != 0) {
1190 out << ", metaState=" << event.getMetaState();
1191 }
Prabir Pradhan65455c72024-02-13 21:46:41 +00001192 if (event.getFlags() != 0) {
1193 out << ", flags=0x" << std::hex << event.getFlags() << std::dec;
1194 }
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +00001195 if (event.getEdgeFlags() != 0) {
1196 out << ", edgeFlags=" << event.getEdgeFlags();
1197 }
1198 if (pointerCount != 1) {
1199 out << ", pointerCount=" << pointerCount;
1200 }
1201 if (event.getHistorySize() != 0) {
1202 out << ", historySize=" << event.getHistorySize();
1203 }
1204 out << ", eventTime=" << event.getEventTime();
1205 out << ", downTime=" << event.getDownTime();
1206 out << ", deviceId=" << event.getDeviceId();
1207 out << ", source=" << inputEventSourceToString(event.getSource());
1208 out << ", displayId=" << event.getDisplayId();
Prabir Pradhanf5abab62024-02-01 20:51:32 +00001209 out << ", eventId=0x" << std::hex << event.getId() << std::dec;
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +00001210 out << "}";
1211 return out;
1212}
1213
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001214// --- FocusEvent ---
1215
Antonio Kantek3cfec7b2021-11-05 18:26:17 -07001216void FocusEvent::initialize(int32_t id, bool hasFocus) {
Garfield Tan4cc839f2020-01-24 11:26:14 -08001217 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001218 ui::LogicalDisplayId::INVALID, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001219 mHasFocus = hasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001220}
1221
1222void FocusEvent::initialize(const FocusEvent& from) {
1223 InputEvent::initialize(from);
1224 mHasFocus = from.mHasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001225}
Jeff Brown5912f952013-07-01 19:10:31 -07001226
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001227// --- CaptureEvent ---
1228
1229void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) {
1230 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001231 ui::LogicalDisplayId::INVALID, INVALID_HMAC);
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001232 mPointerCaptureEnabled = pointerCaptureEnabled;
1233}
1234
1235void CaptureEvent::initialize(const CaptureEvent& from) {
1236 InputEvent::initialize(from);
1237 mPointerCaptureEnabled = from.mPointerCaptureEnabled;
1238}
1239
arthurhung7632c332020-12-30 16:58:01 +08001240// --- DragEvent ---
1241
1242void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) {
1243 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001244 ui::LogicalDisplayId::INVALID, INVALID_HMAC);
arthurhung7632c332020-12-30 16:58:01 +08001245 mIsExiting = isExiting;
1246 mX = x;
1247 mY = y;
1248}
1249
1250void DragEvent::initialize(const DragEvent& from) {
1251 InputEvent::initialize(from);
1252 mIsExiting = from.mIsExiting;
1253 mX = from.mX;
1254 mY = from.mY;
1255}
1256
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001257// --- TouchModeEvent ---
1258
1259void TouchModeEvent::initialize(int32_t id, bool isInTouchMode) {
1260 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07001261 ui::LogicalDisplayId::INVALID, INVALID_HMAC);
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001262 mIsInTouchMode = isInTouchMode;
1263}
1264
1265void TouchModeEvent::initialize(const TouchModeEvent& from) {
1266 InputEvent::initialize(from);
1267 mIsInTouchMode = from.mIsInTouchMode;
1268}
1269
Jeff Brown5912f952013-07-01 19:10:31 -07001270// --- PooledInputEventFactory ---
1271
1272PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
1273 mMaxPoolSize(maxPoolSize) {
1274}
1275
1276PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -07001277}
1278
1279KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001280 if (mKeyEventPool.empty()) {
1281 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001282 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001283 KeyEvent* event = mKeyEventPool.front().release();
1284 mKeyEventPool.pop();
1285 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001286}
1287
1288MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001289 if (mMotionEventPool.empty()) {
1290 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001291 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001292 MotionEvent* event = mMotionEventPool.front().release();
1293 mMotionEventPool.pop();
1294 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001295}
1296
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001297FocusEvent* PooledInputEventFactory::createFocusEvent() {
1298 if (mFocusEventPool.empty()) {
1299 return new FocusEvent();
1300 }
1301 FocusEvent* event = mFocusEventPool.front().release();
1302 mFocusEventPool.pop();
1303 return event;
1304}
1305
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001306CaptureEvent* PooledInputEventFactory::createCaptureEvent() {
1307 if (mCaptureEventPool.empty()) {
1308 return new CaptureEvent();
1309 }
1310 CaptureEvent* event = mCaptureEventPool.front().release();
1311 mCaptureEventPool.pop();
1312 return event;
1313}
1314
arthurhung7632c332020-12-30 16:58:01 +08001315DragEvent* PooledInputEventFactory::createDragEvent() {
1316 if (mDragEventPool.empty()) {
1317 return new DragEvent();
1318 }
1319 DragEvent* event = mDragEventPool.front().release();
1320 mDragEventPool.pop();
1321 return event;
1322}
1323
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001324TouchModeEvent* PooledInputEventFactory::createTouchModeEvent() {
1325 if (mTouchModeEventPool.empty()) {
1326 return new TouchModeEvent();
1327 }
1328 TouchModeEvent* event = mTouchModeEventPool.front().release();
1329 mTouchModeEventPool.pop();
1330 return event;
1331}
1332
Jeff Brown5912f952013-07-01 19:10:31 -07001333void PooledInputEventFactory::recycle(InputEvent* event) {
1334 switch (event->getType()) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001335 case InputEventType::KEY: {
1336 if (mKeyEventPool.size() < mMaxPoolSize) {
1337 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
1338 return;
1339 }
1340 break;
Jeff Brown5912f952013-07-01 19:10:31 -07001341 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001342 case InputEventType::MOTION: {
1343 if (mMotionEventPool.size() < mMaxPoolSize) {
1344 mMotionEventPool.push(
1345 std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
1346 return;
1347 }
1348 break;
Jeff Brown5912f952013-07-01 19:10:31 -07001349 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001350 case InputEventType::FOCUS: {
1351 if (mFocusEventPool.size() < mMaxPoolSize) {
1352 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
1353 return;
1354 }
1355 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001356 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001357 case InputEventType::CAPTURE: {
1358 if (mCaptureEventPool.size() < mMaxPoolSize) {
1359 mCaptureEventPool.push(
1360 std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event)));
1361 return;
1362 }
1363 break;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001364 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001365 case InputEventType::DRAG: {
1366 if (mDragEventPool.size() < mMaxPoolSize) {
1367 mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event)));
1368 return;
1369 }
1370 break;
arthurhung7632c332020-12-30 16:58:01 +08001371 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001372 case InputEventType::TOUCH_MODE: {
1373 if (mTouchModeEventPool.size() < mMaxPoolSize) {
1374 mTouchModeEventPool.push(
1375 std::unique_ptr<TouchModeEvent>(static_cast<TouchModeEvent*>(event)));
1376 return;
1377 }
1378 break;
Antonio Kantekeb4a30c2021-09-28 17:49:49 -07001379 }
Jeff Brown5912f952013-07-01 19:10:31 -07001380 }
1381 delete event;
1382}
1383
1384} // namespace android