blob: ff9d9a94c9ba39e5286854f1ca76738bed70726b [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
Prabir Pradhanadd8a4a2024-03-05 22:18:09 +0000754float MotionEvent::getRawXOffset() const {
755 // This is equivalent to the x-coordinate of the point that the origin of the raw coordinate
756 // space maps to.
757 return (mTransform * mRawTransform.inverse()).tx();
758}
759
760float MotionEvent::getRawYOffset() const {
761 // This is equivalent to the y-coordinate of the point that the origin of the raw coordinate
762 // space maps to.
763 return (mTransform * mRawTransform.inverse()).ty();
764}
765
Robert Carre07e1032018-11-26 12:55:53 -0800766void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700767 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700768 mRawTransform.set(mRawTransform.tx() * globalScaleFactor,
769 mRawTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800770 mXPrecision *= globalScaleFactor;
771 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700772
773 size_t numSamples = mSamplePointerCoords.size();
774 for (size_t i = 0; i < numSamples; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800775 mSamplePointerCoords[i].scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700776 }
777}
778
chaviw9eaa22c2020-07-01 16:21:27 -0700779void MotionEvent::transform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700780 // We want to preserve the raw axes values stored in the PointerCoords, so we just update the
781 // transform using the values passed in.
chaviw9eaa22c2020-07-01 16:21:27 -0700782 ui::Transform newTransform;
783 newTransform.set(matrix);
784 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700785}
786
Evan Roskyd4d4d802021-05-03 20:12:21 -0700787void MotionEvent::applyTransform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700788 ui::Transform transform;
789 transform.set(matrix);
Evan Roskyd4d4d802021-05-03 20:12:21 -0700790
791 // Apply the transformation to all samples.
Prabir Pradhan6b384612021-05-14 16:56:25 -0700792 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
793 [&transform](PointerCoords& c) { c.transform(transform); });
Prabir Pradhan4b19bd02021-06-01 17:34:59 -0700794
795 if (mRawXCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION &&
796 mRawYCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) {
797 const vec2 cursor = transform.transform(mRawXCursorPosition, mRawYCursorPosition);
798 mRawXCursorPosition = cursor.x;
799 mRawYCursorPosition = cursor.y;
800 }
Evan Roskyd4d4d802021-05-03 20:12:21 -0700801}
802
Brett Chabotfaa986c2020-11-04 17:39:36 -0800803#ifdef __linux__
chaviw9eaa22c2020-07-01 16:21:27 -0700804static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
805 float dsdx, dtdx, tx, dtdy, dsdy, ty;
806 status_t status = parcel.readFloat(&dsdx);
807 status |= parcel.readFloat(&dtdx);
808 status |= parcel.readFloat(&tx);
809 status |= parcel.readFloat(&dtdy);
810 status |= parcel.readFloat(&dsdy);
811 status |= parcel.readFloat(&ty);
812
813 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
814 return status;
815}
816
817static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
818 status_t status = parcel.writeFloat(transform.dsdx());
819 status |= parcel.writeFloat(transform.dtdx());
820 status |= parcel.writeFloat(transform.tx());
821 status |= parcel.writeFloat(transform.dtdy());
822 status |= parcel.writeFloat(transform.dsdy());
823 status |= parcel.writeFloat(transform.ty());
824 return status;
825}
826
Jeff Brown5912f952013-07-01 19:10:31 -0700827status_t MotionEvent::readFromParcel(Parcel* parcel) {
828 size_t pointerCount = parcel->readInt32();
829 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800830 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
831 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700832 return BAD_VALUE;
833 }
834
Garfield Tan4cc839f2020-01-24 11:26:14 -0800835 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700836 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600837 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800838 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600839 std::vector<uint8_t> hmac;
840 status_t result = parcel->readByteVector(&hmac);
841 if (result != OK || hmac.size() != 32) {
842 return BAD_VALUE;
843 }
844 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700845 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100846 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700847 mFlags = parcel->readInt32();
848 mEdgeFlags = parcel->readInt32();
849 mMetaState = parcel->readInt32();
850 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800851 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700852
853 result = android::readFromParcel(mTransform, *parcel);
854 if (result != OK) {
855 return result;
856 }
Jeff Brown5912f952013-07-01 19:10:31 -0700857 mXPrecision = parcel->readFloat();
858 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700859 mRawXCursorPosition = parcel->readFloat();
860 mRawYCursorPosition = parcel->readFloat();
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700861
862 result = android::readFromParcel(mRawTransform, *parcel);
863 if (result != OK) {
864 return result;
865 }
Jeff Brown5912f952013-07-01 19:10:31 -0700866 mDownTime = parcel->readInt64();
867
868 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800869 mPointerProperties.reserve(pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700870 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500871 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700872 mSamplePointerCoords.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800873 mSamplePointerCoords.reserve(sampleCount * pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700874
875 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800876 mPointerProperties.push_back({});
877 PointerProperties& properties = mPointerProperties.back();
Jeff Brown5912f952013-07-01 19:10:31 -0700878 properties.id = parcel->readInt32();
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700879 properties.toolType = static_cast<ToolType>(parcel->readInt32());
Jeff Brown5912f952013-07-01 19:10:31 -0700880 }
881
Dan Austinc94fc452015-09-22 14:22:41 -0700882 while (sampleCount > 0) {
883 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500884 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700885 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800886 mSamplePointerCoords.push_back({});
887 status_t status = mSamplePointerCoords.back().readFromParcel(parcel);
Jeff Brown5912f952013-07-01 19:10:31 -0700888 if (status) {
889 return status;
890 }
891 }
892 }
893 return OK;
894}
895
896status_t MotionEvent::writeToParcel(Parcel* parcel) const {
897 size_t pointerCount = mPointerProperties.size();
898 size_t sampleCount = mSampleEventTimes.size();
899
900 parcel->writeInt32(pointerCount);
901 parcel->writeInt32(sampleCount);
902
Garfield Tan4cc839f2020-01-24 11:26:14 -0800903 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700904 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600905 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800906 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600907 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
908 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700909 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100910 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700911 parcel->writeInt32(mFlags);
912 parcel->writeInt32(mEdgeFlags);
913 parcel->writeInt32(mMetaState);
914 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800915 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700916
917 status_t result = android::writeToParcel(mTransform, *parcel);
918 if (result != OK) {
919 return result;
920 }
Jeff Brown5912f952013-07-01 19:10:31 -0700921 parcel->writeFloat(mXPrecision);
922 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700923 parcel->writeFloat(mRawXCursorPosition);
924 parcel->writeFloat(mRawYCursorPosition);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700925
926 result = android::writeToParcel(mRawTransform, *parcel);
927 if (result != OK) {
928 return result;
929 }
Jeff Brown5912f952013-07-01 19:10:31 -0700930 parcel->writeInt64(mDownTime);
931
932 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800933 const PointerProperties& properties = mPointerProperties[i];
Jeff Brown5912f952013-07-01 19:10:31 -0700934 parcel->writeInt32(properties.id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700935 parcel->writeInt32(static_cast<int32_t>(properties.toolType));
Jeff Brown5912f952013-07-01 19:10:31 -0700936 }
937
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800938 const PointerCoords* pc = mSamplePointerCoords.data();
Jeff Brown5912f952013-07-01 19:10:31 -0700939 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500940 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700941 for (size_t i = 0; i < pointerCount; i++) {
942 status_t status = (pc++)->writeToParcel(parcel);
943 if (status) {
944 return status;
945 }
946 }
947 }
948 return OK;
949}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800950#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700951
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600952bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700953 if (isFromSource(source, AINPUT_SOURCE_CLASS_POINTER)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700954 // Specifically excludes HOVER_MOVE and SCROLL.
955 switch (action & AMOTION_EVENT_ACTION_MASK) {
956 case AMOTION_EVENT_ACTION_DOWN:
957 case AMOTION_EVENT_ACTION_MOVE:
958 case AMOTION_EVENT_ACTION_UP:
959 case AMOTION_EVENT_ACTION_POINTER_DOWN:
960 case AMOTION_EVENT_ACTION_POINTER_UP:
961 case AMOTION_EVENT_ACTION_CANCEL:
962 case AMOTION_EVENT_ACTION_OUTSIDE:
963 return true;
964 }
965 }
966 return false;
967}
968
Michael Wright872db4f2014-04-22 15:03:51 -0700969const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700970 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700971}
972
Siarhei Vishniakou5df34932023-01-23 12:41:01 -0800973std::optional<int> MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700974 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700975}
976
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500977std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700978 // Convert MotionEvent action to string
979 switch (action & AMOTION_EVENT_ACTION_MASK) {
980 case AMOTION_EVENT_ACTION_DOWN:
981 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700982 case AMOTION_EVENT_ACTION_UP:
983 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500984 case AMOTION_EVENT_ACTION_MOVE:
985 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700986 case AMOTION_EVENT_ACTION_CANCEL:
987 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500988 case AMOTION_EVENT_ACTION_OUTSIDE:
989 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700990 case AMOTION_EVENT_ACTION_POINTER_DOWN:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000991 return StringPrintf("POINTER_DOWN(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700992 case AMOTION_EVENT_ACTION_POINTER_UP:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000993 return StringPrintf("POINTER_UP(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500994 case AMOTION_EVENT_ACTION_HOVER_MOVE:
995 return "HOVER_MOVE";
996 case AMOTION_EVENT_ACTION_SCROLL:
997 return "SCROLL";
998 case AMOTION_EVENT_ACTION_HOVER_ENTER:
999 return "HOVER_ENTER";
1000 case AMOTION_EVENT_ACTION_HOVER_EXIT:
1001 return "HOVER_EXIT";
1002 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
1003 return "BUTTON_PRESS";
1004 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
1005 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001006 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -05001007 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -07001008}
1009
Prabir Pradhanbf9b0a82024-02-29 02:23:50 +00001010std::tuple<int32_t, std::vector<PointerProperties>, std::vector<PointerCoords>> MotionEvent::split(
1011 int32_t action, int32_t flags, int32_t historySize,
1012 const std::vector<PointerProperties>& pointerProperties,
1013 const std::vector<PointerCoords>& pointerCoords,
1014 std::bitset<MAX_POINTER_ID + 1> splitPointerIds) {
1015 LOG_ALWAYS_FATAL_IF(!splitPointerIds.any());
1016 const auto pointerCount = pointerProperties.size();
1017 LOG_ALWAYS_FATAL_IF(pointerCoords.size() != (pointerCount * (historySize + 1)));
1018 const auto splitCount = splitPointerIds.count();
1019
1020 std::vector<PointerProperties> splitPointerProperties;
1021 std::vector<PointerCoords> splitPointerCoords;
1022
1023 for (uint32_t i = 0; i < pointerCount; i++) {
1024 if (splitPointerIds.test(pointerProperties[i].id)) {
1025 splitPointerProperties.emplace_back(pointerProperties[i]);
1026 }
1027 }
1028 for (uint32_t i = 0; i < pointerCoords.size(); i++) {
1029 if (splitPointerIds.test(pointerProperties[i % pointerCount].id)) {
1030 splitPointerCoords.emplace_back(pointerCoords[i]);
1031 }
1032 }
1033 LOG_ALWAYS_FATAL_IF(splitPointerCoords.size() !=
1034 (splitPointerProperties.size() * (historySize + 1)));
1035
1036 if (CC_UNLIKELY(splitPointerProperties.size() != splitCount)) {
1037 LOG(FATAL) << "Cannot split MotionEvent: Requested splitting " << splitCount
1038 << " pointers from the original event, but the original event only contained "
1039 << splitPointerProperties.size() << " of those pointers.";
1040 }
1041
1042 // TODO(b/327503168): Verify the splitDownTime here once it is used correctly.
1043
1044 const auto splitAction = resolveActionForSplitMotionEvent(action, flags, pointerProperties,
1045 splitPointerProperties);
1046 return {splitAction, splitPointerProperties, splitPointerCoords};
1047}
1048
Prabir Pradhan7e1ee562021-10-26 10:19:49 -07001049// Apply the given transformation to the point without checking whether the entire transform
1050// should be disregarded altogether for the provided source.
1051static inline vec2 calculateTransformedXYUnchecked(uint32_t source, const ui::Transform& transform,
1052 const vec2& xy) {
1053 return shouldDisregardOffset(source) ? transformWithoutTranslation(transform, xy)
Prabir Pradhan00e029d2023-03-09 20:11:09 +00001054 : roundTransformedCoords(transform.transform(xy));
Prabir Pradhan7e1ee562021-10-26 10:19:49 -07001055}
1056
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07001057vec2 MotionEvent::calculateTransformedXY(uint32_t source, const ui::Transform& transform,
1058 const vec2& xy) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -07001059 if (shouldDisregardTransformation(source)) {
1060 return xy;
1061 }
1062 return calculateTransformedXYUnchecked(source, transform, xy);
Prabir Pradhanb5cb9572021-09-24 06:35:16 -07001063}
1064
Prabir Pradhan8e6ce222022-02-24 09:08:54 -08001065// Keep in sync with calculateTransformedCoords.
Prabir Pradhan9eb02c02021-10-19 14:02:20 -07001066float MotionEvent::calculateTransformedAxisValue(int32_t axis, uint32_t source,
1067 const ui::Transform& transform,
1068 const PointerCoords& coords) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -07001069 if (shouldDisregardTransformation(source)) {
1070 return coords.getAxisValue(axis);
1071 }
1072
Prabir Pradhan9eb02c02021-10-19 14:02:20 -07001073 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -07001074 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
Prabir Pradhan9eb02c02021-10-19 14:02:20 -07001075 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
1076 return xy[axis];
1077 }
1078
1079 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
1080 const vec2 relativeXy =
1081 transformWithoutTranslation(transform,
1082 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
1083 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
1084 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
1085 }
1086
1087 if (axis == AMOTION_EVENT_AXIS_ORIENTATION) {
1088 return transformAngle(transform, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION));
1089 }
1090
1091 return coords.getAxisValue(axis);
1092}
1093
Prabir Pradhan8e6ce222022-02-24 09:08:54 -08001094// Keep in sync with calculateTransformedAxisValue. This is an optimization of
1095// calculateTransformedAxisValue for all PointerCoords axes.
1096PointerCoords MotionEvent::calculateTransformedCoords(uint32_t source,
1097 const ui::Transform& transform,
1098 const PointerCoords& coords) {
1099 if (shouldDisregardTransformation(source)) {
1100 return coords;
1101 }
1102 PointerCoords out = coords;
1103
1104 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
1105 out.setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
1106 out.setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
1107
1108 const vec2 relativeXy =
1109 transformWithoutTranslation(transform,
1110 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
1111 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
1112 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
1113 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
1114
1115 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
1116 transformAngle(transform,
1117 coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)));
1118
1119 return out;
1120}
1121
Prabir Pradhan65a071a2024-01-05 20:52:09 +00001122bool MotionEvent::operator==(const android::MotionEvent& o) const {
1123 // We use NaN values to represent invalid cursor positions. Since NaN values are not equal
1124 // to themselves according to IEEE 754, we cannot use the default equality operator to compare
1125 // MotionEvents. Therefore we define a custom equality operator with special handling for NaNs.
1126 // clang-format off
1127 return InputEvent::operator==(static_cast<const InputEvent&>(o)) &&
1128 mAction == o.mAction &&
1129 mActionButton == o.mActionButton &&
1130 mFlags == o.mFlags &&
1131 mEdgeFlags == o.mEdgeFlags &&
1132 mMetaState == o.mMetaState &&
1133 mButtonState == o.mButtonState &&
1134 mClassification == o.mClassification &&
1135 mTransform == o.mTransform &&
1136 mXPrecision == o.mXPrecision &&
1137 mYPrecision == o.mYPrecision &&
1138 ((std::isnan(mRawXCursorPosition) && std::isnan(o.mRawXCursorPosition)) ||
1139 mRawXCursorPosition == o.mRawXCursorPosition) &&
1140 ((std::isnan(mRawYCursorPosition) && std::isnan(o.mRawYCursorPosition)) ||
1141 mRawYCursorPosition == o.mRawYCursorPosition) &&
1142 mRawTransform == o.mRawTransform && mDownTime == o.mDownTime &&
1143 mPointerProperties == o.mPointerProperties &&
1144 mSampleEventTimes == o.mSampleEventTimes &&
1145 mSamplePointerCoords == o.mSamplePointerCoords;
1146 // clang-format on
1147}
1148
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +00001149std::ostream& operator<<(std::ostream& out, const MotionEvent& event) {
1150 out << "MotionEvent { action=" << MotionEvent::actionToString(event.getAction());
1151 if (event.getActionButton() != 0) {
1152 out << ", actionButton=" << std::to_string(event.getActionButton());
1153 }
1154 const size_t pointerCount = event.getPointerCount();
hupeng3aa5a51a2022-09-02 16:00:18 +08001155 LOG_ALWAYS_FATAL_IF(pointerCount > MAX_POINTERS, "Too many pointers : pointerCount = %zu",
1156 pointerCount);
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +00001157 for (size_t i = 0; i < pointerCount; i++) {
1158 out << ", id[" << i << "]=" << event.getPointerId(i);
1159 float x = event.getX(i);
1160 float y = event.getY(i);
1161 if (x != 0 || y != 0) {
1162 out << ", x[" << i << "]=" << x;
1163 out << ", y[" << i << "]=" << y;
1164 }
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001165 ToolType toolType = event.getToolType(i);
1166 if (toolType != ToolType::FINGER) {
1167 out << ", toolType[" << i << "]=" << ftl::enum_string(toolType);
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +00001168 }
1169 }
1170 if (event.getButtonState() != 0) {
1171 out << ", buttonState=" << event.getButtonState();
1172 }
1173 if (event.getClassification() != MotionClassification::NONE) {
1174 out << ", classification=" << motionClassificationToString(event.getClassification());
1175 }
1176 if (event.getMetaState() != 0) {
1177 out << ", metaState=" << event.getMetaState();
1178 }
Prabir Pradhan65455c72024-02-13 21:46:41 +00001179 if (event.getFlags() != 0) {
1180 out << ", flags=0x" << std::hex << event.getFlags() << std::dec;
1181 }
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +00001182 if (event.getEdgeFlags() != 0) {
1183 out << ", edgeFlags=" << event.getEdgeFlags();
1184 }
1185 if (pointerCount != 1) {
1186 out << ", pointerCount=" << pointerCount;
1187 }
1188 if (event.getHistorySize() != 0) {
1189 out << ", historySize=" << event.getHistorySize();
1190 }
1191 out << ", eventTime=" << event.getEventTime();
1192 out << ", downTime=" << event.getDownTime();
1193 out << ", deviceId=" << event.getDeviceId();
1194 out << ", source=" << inputEventSourceToString(event.getSource());
1195 out << ", displayId=" << event.getDisplayId();
Prabir Pradhanf5abab62024-02-01 20:51:32 +00001196 out << ", eventId=0x" << std::hex << event.getId() << std::dec;
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +00001197 out << "}";
1198 return out;
1199}
1200
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001201// --- FocusEvent ---
1202
Antonio Kantek3cfec7b2021-11-05 18:26:17 -07001203void FocusEvent::initialize(int32_t id, bool hasFocus) {
Garfield Tan4cc839f2020-01-24 11:26:14 -08001204 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -06001205 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001206 mHasFocus = hasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001207}
1208
1209void FocusEvent::initialize(const FocusEvent& from) {
1210 InputEvent::initialize(from);
1211 mHasFocus = from.mHasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001212}
Jeff Brown5912f952013-07-01 19:10:31 -07001213
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001214// --- CaptureEvent ---
1215
1216void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) {
1217 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1218 ADISPLAY_ID_NONE, INVALID_HMAC);
1219 mPointerCaptureEnabled = pointerCaptureEnabled;
1220}
1221
1222void CaptureEvent::initialize(const CaptureEvent& from) {
1223 InputEvent::initialize(from);
1224 mPointerCaptureEnabled = from.mPointerCaptureEnabled;
1225}
1226
arthurhung7632c332020-12-30 16:58:01 +08001227// --- DragEvent ---
1228
1229void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) {
1230 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1231 ADISPLAY_ID_NONE, INVALID_HMAC);
1232 mIsExiting = isExiting;
1233 mX = x;
1234 mY = y;
1235}
1236
1237void DragEvent::initialize(const DragEvent& from) {
1238 InputEvent::initialize(from);
1239 mIsExiting = from.mIsExiting;
1240 mX = from.mX;
1241 mY = from.mY;
1242}
1243
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001244// --- TouchModeEvent ---
1245
1246void TouchModeEvent::initialize(int32_t id, bool isInTouchMode) {
1247 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1248 ADISPLAY_ID_NONE, INVALID_HMAC);
1249 mIsInTouchMode = isInTouchMode;
1250}
1251
1252void TouchModeEvent::initialize(const TouchModeEvent& from) {
1253 InputEvent::initialize(from);
1254 mIsInTouchMode = from.mIsInTouchMode;
1255}
1256
Jeff Brown5912f952013-07-01 19:10:31 -07001257// --- PooledInputEventFactory ---
1258
1259PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
1260 mMaxPoolSize(maxPoolSize) {
1261}
1262
1263PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -07001264}
1265
1266KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001267 if (mKeyEventPool.empty()) {
1268 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001269 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001270 KeyEvent* event = mKeyEventPool.front().release();
1271 mKeyEventPool.pop();
1272 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001273}
1274
1275MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001276 if (mMotionEventPool.empty()) {
1277 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001278 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001279 MotionEvent* event = mMotionEventPool.front().release();
1280 mMotionEventPool.pop();
1281 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001282}
1283
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001284FocusEvent* PooledInputEventFactory::createFocusEvent() {
1285 if (mFocusEventPool.empty()) {
1286 return new FocusEvent();
1287 }
1288 FocusEvent* event = mFocusEventPool.front().release();
1289 mFocusEventPool.pop();
1290 return event;
1291}
1292
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001293CaptureEvent* PooledInputEventFactory::createCaptureEvent() {
1294 if (mCaptureEventPool.empty()) {
1295 return new CaptureEvent();
1296 }
1297 CaptureEvent* event = mCaptureEventPool.front().release();
1298 mCaptureEventPool.pop();
1299 return event;
1300}
1301
arthurhung7632c332020-12-30 16:58:01 +08001302DragEvent* PooledInputEventFactory::createDragEvent() {
1303 if (mDragEventPool.empty()) {
1304 return new DragEvent();
1305 }
1306 DragEvent* event = mDragEventPool.front().release();
1307 mDragEventPool.pop();
1308 return event;
1309}
1310
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001311TouchModeEvent* PooledInputEventFactory::createTouchModeEvent() {
1312 if (mTouchModeEventPool.empty()) {
1313 return new TouchModeEvent();
1314 }
1315 TouchModeEvent* event = mTouchModeEventPool.front().release();
1316 mTouchModeEventPool.pop();
1317 return event;
1318}
1319
Jeff Brown5912f952013-07-01 19:10:31 -07001320void PooledInputEventFactory::recycle(InputEvent* event) {
1321 switch (event->getType()) {
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001322 case InputEventType::KEY: {
1323 if (mKeyEventPool.size() < mMaxPoolSize) {
1324 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
1325 return;
1326 }
1327 break;
Jeff Brown5912f952013-07-01 19:10:31 -07001328 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001329 case InputEventType::MOTION: {
1330 if (mMotionEventPool.size() < mMaxPoolSize) {
1331 mMotionEventPool.push(
1332 std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
1333 return;
1334 }
1335 break;
Jeff Brown5912f952013-07-01 19:10:31 -07001336 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001337 case InputEventType::FOCUS: {
1338 if (mFocusEventPool.size() < mMaxPoolSize) {
1339 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
1340 return;
1341 }
1342 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001343 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001344 case InputEventType::CAPTURE: {
1345 if (mCaptureEventPool.size() < mMaxPoolSize) {
1346 mCaptureEventPool.push(
1347 std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event)));
1348 return;
1349 }
1350 break;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001351 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001352 case InputEventType::DRAG: {
1353 if (mDragEventPool.size() < mMaxPoolSize) {
1354 mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event)));
1355 return;
1356 }
1357 break;
arthurhung7632c332020-12-30 16:58:01 +08001358 }
Siarhei Vishniakou63b63612023-04-12 11:00:23 -07001359 case InputEventType::TOUCH_MODE: {
1360 if (mTouchModeEventPool.size() < mMaxPoolSize) {
1361 mTouchModeEventPool.push(
1362 std::unique_ptr<TouchModeEvent>(static_cast<TouchModeEvent*>(event)));
1363 return;
1364 }
1365 break;
Antonio Kantekeb4a30c2021-09-28 17:49:49 -07001366 }
Jeff Brown5912f952013-07-01 19:10:31 -07001367 }
1368 delete event;
1369}
1370
1371} // namespace android