blob: 1d7bd5f692d8004b9ba80d8b8827ec0dbd6162ca [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 Pradhan6b384612021-05-14 16:56:25 -070063} // namespace
64
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -080065const char* motionClassificationToString(MotionClassification classification) {
66 switch (classification) {
67 case MotionClassification::NONE:
68 return "NONE";
69 case MotionClassification::AMBIGUOUS_GESTURE:
70 return "AMBIGUOUS_GESTURE";
71 case MotionClassification::DEEP_PRESS:
72 return "DEEP_PRESS";
Harry Cutts2800fb02022-09-15 13:49:23 +000073 case MotionClassification::TWO_FINGER_SWIPE:
74 return "TWO_FINGER_SWIPE";
Harry Cuttsc5748d12022-12-02 17:30:18 +000075 case MotionClassification::MULTI_FINGER_SWIPE:
76 return "MULTI_FINGER_SWIPE";
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -080077 }
78}
79
Siarhei Vishniakoud5fe5182022-07-20 23:28:40 +000080const char* motionToolTypeToString(int32_t toolType) {
81 switch (toolType) {
82 case AMOTION_EVENT_TOOL_TYPE_UNKNOWN:
83 return "UNKNOWN";
84 case AMOTION_EVENT_TOOL_TYPE_FINGER:
85 return "FINGER";
86 case AMOTION_EVENT_TOOL_TYPE_STYLUS:
87 return "STYLUS";
88 case AMOTION_EVENT_TOOL_TYPE_MOUSE:
89 return "MOUSE";
90 case AMOTION_EVENT_TOOL_TYPE_ERASER:
91 return "ERASER";
92 case AMOTION_EVENT_TOOL_TYPE_PALM:
93 return "PALM";
94 default:
95 return "INVALID";
96 }
97}
98
Garfield Tan84b087e2020-01-23 10:49:05 -080099// --- IdGenerator ---
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700100#if defined(__ANDROID__)
101[[maybe_unused]]
102#endif
103static status_t
104getRandomBytes(uint8_t* data, size_t size) {
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700105 int ret = TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY | O_CLOEXEC | O_NOFOLLOW));
106 if (ret == -1) {
107 return -errno;
108 }
109
110 base::unique_fd fd(ret);
111 if (!base::ReadFully(fd, data, size)) {
112 return -errno;
113 }
114 return OK;
115}
116
Garfield Tan84b087e2020-01-23 10:49:05 -0800117IdGenerator::IdGenerator(Source source) : mSource(source) {}
118
119int32_t IdGenerator::nextId() const {
120 constexpr uint32_t SEQUENCE_NUMBER_MASK = ~SOURCE_MASK;
121 int32_t id = 0;
122
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700123#if defined(__ANDROID__)
124 // On device, prefer 'getrandom' to '/dev/urandom' because it's faster.
125 constexpr size_t BUF_LEN = sizeof(id);
126 size_t totalBytes = 0;
127 while (totalBytes < BUF_LEN) {
128 ssize_t bytes = TEMP_FAILURE_RETRY(getrandom(&id, BUF_LEN, GRND_NONBLOCK));
129 if (CC_UNLIKELY(bytes < 0)) {
130 ALOGW("Failed to fill in random number for sequence number: %s.", strerror(errno));
131 id = 0;
132 break;
133 }
134 totalBytes += bytes;
135 }
136#else
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700137#if defined(__linux__)
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700138 // On host, <sys/random.h> / GRND_NONBLOCK is not available
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700139 while (true) {
140 status_t result = getRandomBytes(reinterpret_cast<uint8_t*>(&id), sizeof(id));
141 if (result == OK) {
Garfield Tan84b087e2020-01-23 10:49:05 -0800142 break;
143 }
Garfield Tan84b087e2020-01-23 10:49:05 -0800144 }
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700145#endif // __linux__
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700146#endif // __ANDROID__
Garfield Tan84b087e2020-01-23 10:49:05 -0800147 return (id & SEQUENCE_NUMBER_MASK) | static_cast<int32_t>(mSource);
148}
149
Jeff Brown5912f952013-07-01 19:10:31 -0700150// --- InputEvent ---
151
Prabir Pradhande69f8a2021-11-18 16:40:34 +0000152vec2 transformWithoutTranslation(const ui::Transform& transform, const vec2& xy) {
153 const vec2 transformedXy = transform.transform(xy);
154 const vec2 transformedOrigin = transform.transform(0, 0);
155 return transformedXy - transformedOrigin;
156}
157
Prabir Pradhane2e10b42022-11-17 20:59:36 +0000158float transformAngle(const ui::Transform& transform, float angleRadians) {
159 // Construct and transform a vector oriented at the specified clockwise angle from vertical.
160 // Coordinate system: down is increasing Y, right is increasing X.
161 float x = sinf(angleRadians);
162 float y = -cosf(angleRadians);
163 vec2 transformedPoint = transform.transform(x, y);
164
165 // Determine how the origin is transformed by the matrix so that we
166 // can transform orientation vectors.
167 const vec2 origin = transform.transform(0, 0);
168
169 transformedPoint.x -= origin.x;
170 transformedPoint.y -= origin.y;
171
172 // Derive the transformed vector's clockwise angle from vertical.
173 // The return value of atan2f is in range [-pi, pi] which conforms to the orientation API.
174 return atan2f(transformedPoint.x, -transformedPoint.y);
175}
176
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800177const char* inputEventTypeToString(int32_t type) {
178 switch (type) {
179 case AINPUT_EVENT_TYPE_KEY: {
180 return "KEY";
181 }
182 case AINPUT_EVENT_TYPE_MOTION: {
183 return "MOTION";
184 }
185 case AINPUT_EVENT_TYPE_FOCUS: {
186 return "FOCUS";
187 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800188 case AINPUT_EVENT_TYPE_CAPTURE: {
189 return "CAPTURE";
190 }
arthurhung7632c332020-12-30 16:58:01 +0800191 case AINPUT_EVENT_TYPE_DRAG: {
192 return "DRAG";
193 }
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700194 case AINPUT_EVENT_TYPE_TOUCH_MODE: {
195 return "TOUCH_MODE";
196 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800197 }
198 return "UNKNOWN";
199}
200
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800201std::string inputEventSourceToString(int32_t source) {
202 if (source == AINPUT_SOURCE_UNKNOWN) {
203 return "UNKNOWN";
204 }
205 if (source == static_cast<int32_t>(AINPUT_SOURCE_ANY)) {
206 return "ANY";
207 }
208 static const std::map<int32_t, const char*> SOURCES{
209 {AINPUT_SOURCE_KEYBOARD, "KEYBOARD"},
210 {AINPUT_SOURCE_DPAD, "DPAD"},
211 {AINPUT_SOURCE_GAMEPAD, "GAMEPAD"},
212 {AINPUT_SOURCE_TOUCHSCREEN, "TOUCHSCREEN"},
213 {AINPUT_SOURCE_MOUSE, "MOUSE"},
214 {AINPUT_SOURCE_STYLUS, "STYLUS"},
215 {AINPUT_SOURCE_BLUETOOTH_STYLUS, "BLUETOOTH_STYLUS"},
216 {AINPUT_SOURCE_TRACKBALL, "TRACKBALL"},
217 {AINPUT_SOURCE_MOUSE_RELATIVE, "MOUSE_RELATIVE"},
218 {AINPUT_SOURCE_TOUCHPAD, "TOUCHPAD"},
219 {AINPUT_SOURCE_TOUCH_NAVIGATION, "TOUCH_NAVIGATION"},
220 {AINPUT_SOURCE_JOYSTICK, "JOYSTICK"},
221 {AINPUT_SOURCE_HDMI, "HDMI"},
222 {AINPUT_SOURCE_SENSOR, "SENSOR"},
223 {AINPUT_SOURCE_ROTARY_ENCODER, "ROTARY_ENCODER"},
224 };
225 std::string result;
226 for (const auto& [source_entry, str] : SOURCES) {
227 if ((source & source_entry) == source_entry) {
228 if (!result.empty()) {
229 result += " | ";
230 }
231 result += str;
232 }
233 }
234 if (result.empty()) {
235 result = StringPrintf("0x%08x", source);
236 }
237 return result;
238}
239
240bool isFromSource(uint32_t source, uint32_t test) {
241 return (source & test) == test;
242}
243
Prabir Pradhane5626962022-10-27 20:30:53 +0000244bool isStylusToolType(uint32_t toolType) {
245 return toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS || toolType == AMOTION_EVENT_TOOL_TYPE_ERASER;
246}
247
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800248VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
249 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
250 event.getSource(), event.getDisplayId()},
251 event.getAction(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800252 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800253 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800254 event.getKeyCode(),
255 event.getScanCode(),
256 event.getMetaState(),
257 event.getRepeatCount()};
258}
259
260VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
261 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
262 event.getSource(), event.getDisplayId()},
263 event.getRawX(0),
264 event.getRawY(0),
265 event.getActionMasked(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800266 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800267 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800268 event.getMetaState(),
269 event.getButtonState()};
270}
271
Garfield Tan4cc839f2020-01-24 11:26:14 -0800272void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600273 std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800274 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700275 mDeviceId = deviceId;
276 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100277 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600278 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700279}
280
281void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800282 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700283 mDeviceId = from.mDeviceId;
284 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100285 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600286 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700287}
288
Garfield Tan4cc839f2020-01-24 11:26:14 -0800289int32_t InputEvent::nextId() {
290 static IdGenerator idGen(IdGenerator::Source::OTHER);
291 return idGen.nextId();
292}
293
Jeff Brown5912f952013-07-01 19:10:31 -0700294// --- KeyEvent ---
295
Michael Wright872db4f2014-04-22 15:03:51 -0700296const char* KeyEvent::getLabel(int32_t keyCode) {
Chris Ye4958d062020-08-20 13:21:10 -0700297 return InputEventLookup::getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700298}
299
Michael Wright872db4f2014-04-22 15:03:51 -0700300int32_t KeyEvent::getKeyCodeFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700301 return InputEventLookup::getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700302}
303
Garfield Tan4cc839f2020-01-24 11:26:14 -0800304void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600305 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
306 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
307 nsecs_t downTime, nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800308 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700309 mAction = action;
310 mFlags = flags;
311 mKeyCode = keyCode;
312 mScanCode = scanCode;
313 mMetaState = metaState;
314 mRepeatCount = repeatCount;
315 mDownTime = downTime;
316 mEventTime = eventTime;
317}
318
319void KeyEvent::initialize(const KeyEvent& from) {
320 InputEvent::initialize(from);
321 mAction = from.mAction;
322 mFlags = from.mFlags;
323 mKeyCode = from.mKeyCode;
324 mScanCode = from.mScanCode;
325 mMetaState = from.mMetaState;
326 mRepeatCount = from.mRepeatCount;
327 mDownTime = from.mDownTime;
328 mEventTime = from.mEventTime;
329}
330
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700331const char* KeyEvent::actionToString(int32_t action) {
332 // Convert KeyEvent action to string
333 switch (action) {
334 case AKEY_EVENT_ACTION_DOWN:
335 return "DOWN";
336 case AKEY_EVENT_ACTION_UP:
337 return "UP";
338 case AKEY_EVENT_ACTION_MULTIPLE:
339 return "MULTIPLE";
340 }
341 return "UNKNOWN";
342}
Jeff Brown5912f952013-07-01 19:10:31 -0700343
344// --- PointerCoords ---
345
346float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700347 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700348 return 0;
349 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700350 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700351}
352
353status_t PointerCoords::setAxisValue(int32_t axis, float value) {
354 if (axis < 0 || axis > 63) {
355 return NAME_NOT_FOUND;
356 }
357
Michael Wright38dcdff2014-03-19 12:06:10 -0700358 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
359 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700360 if (value == 0) {
361 return OK; // axes with value 0 do not need to be stored
362 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700363
364 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700365 if (count >= MAX_AXES) {
366 tooManyAxes(axis);
367 return NO_MEMORY;
368 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700369 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700370 for (uint32_t i = count; i > index; i--) {
371 values[i] = values[i - 1];
372 }
373 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700374
Jeff Brown5912f952013-07-01 19:10:31 -0700375 values[index] = value;
376 return OK;
377}
378
379static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
380 float value = c.getAxisValue(axis);
381 if (value != 0) {
382 c.setAxisValue(axis, value * scaleFactor);
383 }
384}
385
Robert Carre07e1032018-11-26 12:55:53 -0800386void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700387 // No need to scale pressure or size since they are normalized.
388 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800389
390 // If there is a global scale factor, it is included in the windowX/YScale
391 // so we don't need to apply it twice to the X/Y axes.
392 // However we don't want to apply any windowXYScale not included in the global scale
393 // to the TOUCH_MAJOR/MINOR coordinates.
394 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
395 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
396 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
397 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
398 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
399 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
Prabir Pradhanc6523582021-05-14 18:02:55 -0700400 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_X, windowXScale);
401 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_Y, windowYScale);
Robert Carre07e1032018-11-26 12:55:53 -0800402}
403
Brett Chabotfaa986c2020-11-04 17:39:36 -0800404#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700405status_t PointerCoords::readFromParcel(Parcel* parcel) {
406 bits = parcel->readInt64();
407
Michael Wright38dcdff2014-03-19 12:06:10 -0700408 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700409 if (count > MAX_AXES) {
410 return BAD_VALUE;
411 }
412
413 for (uint32_t i = 0; i < count; i++) {
414 values[i] = parcel->readFloat();
415 }
Philip Quinnafb31282022-12-20 18:17:55 -0800416
417 isResampled = parcel->readBool();
Jeff Brown5912f952013-07-01 19:10:31 -0700418 return OK;
419}
420
421status_t PointerCoords::writeToParcel(Parcel* parcel) const {
422 parcel->writeInt64(bits);
423
Michael Wright38dcdff2014-03-19 12:06:10 -0700424 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700425 for (uint32_t i = 0; i < count; i++) {
426 parcel->writeFloat(values[i]);
427 }
Philip Quinnafb31282022-12-20 18:17:55 -0800428
429 parcel->writeBool(isResampled);
Jeff Brown5912f952013-07-01 19:10:31 -0700430 return OK;
431}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800432#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700433
434void PointerCoords::tooManyAxes(int axis) {
435 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
436 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
437}
438
439bool PointerCoords::operator==(const PointerCoords& other) const {
440 if (bits != other.bits) {
441 return false;
442 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700443 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700444 for (uint32_t i = 0; i < count; i++) {
445 if (values[i] != other.values[i]) {
446 return false;
447 }
448 }
Philip Quinnafb31282022-12-20 18:17:55 -0800449 if (isResampled != other.isResampled) {
450 return false;
451 }
Jeff Brown5912f952013-07-01 19:10:31 -0700452 return true;
453}
454
chaviwc01e1372020-07-01 12:37:31 -0700455void PointerCoords::transform(const ui::Transform& transform) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700456 const vec2 xy = transform.transform(getXYValue());
457 setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
458 setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
459
Prabir Pradhanc6523582021-05-14 18:02:55 -0700460 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_X) ||
461 BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_Y)) {
462 const ui::Transform rotation(transform.getOrientation());
463 const vec2 relativeXy = rotation.transform(getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
464 getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
465 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
466 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
467 }
468
Prabir Pradhan6b384612021-05-14 16:56:25 -0700469 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) {
470 const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
471 setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val));
472 }
chaviwc01e1372020-07-01 12:37:31 -0700473}
Jeff Brown5912f952013-07-01 19:10:31 -0700474
475// --- PointerProperties ---
476
477bool PointerProperties::operator==(const PointerProperties& other) const {
478 return id == other.id
479 && toolType == other.toolType;
480}
481
482void PointerProperties::copyFrom(const PointerProperties& other) {
483 id = other.id;
484 toolType = other.toolType;
485}
486
487
488// --- MotionEvent ---
489
Garfield Tan4cc839f2020-01-24 11:26:14 -0800490void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600491 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
492 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700493 int32_t buttonState, MotionClassification classification,
494 const ui::Transform& transform, float xPrecision, float yPrecision,
Evan Rosky84f07f02021-04-16 10:42:42 -0700495 float rawXCursorPosition, float rawYCursorPosition,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700496 const ui::Transform& rawTransform, nsecs_t downTime, nsecs_t eventTime,
Evan Rosky09576692021-07-01 12:22:09 -0700497 size_t pointerCount, const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700498 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800499 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700500 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100501 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700502 mFlags = flags;
503 mEdgeFlags = edgeFlags;
504 mMetaState = metaState;
505 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800506 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700507 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700508 mXPrecision = xPrecision;
509 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700510 mRawXCursorPosition = rawXCursorPosition;
511 mRawYCursorPosition = rawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700512 mRawTransform = rawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700513 mDownTime = downTime;
514 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800515 mPointerProperties.insert(mPointerProperties.end(), &pointerProperties[0],
516 &pointerProperties[pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700517 mSampleEventTimes.clear();
518 mSamplePointerCoords.clear();
519 addSample(eventTime, pointerCoords);
520}
521
522void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800523 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
524 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700525 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100526 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700527 mFlags = other->mFlags;
528 mEdgeFlags = other->mEdgeFlags;
529 mMetaState = other->mMetaState;
530 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800531 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700532 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700533 mXPrecision = other->mXPrecision;
534 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700535 mRawXCursorPosition = other->mRawXCursorPosition;
536 mRawYCursorPosition = other->mRawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700537 mRawTransform = other->mRawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700538 mDownTime = other->mDownTime;
539 mPointerProperties = other->mPointerProperties;
540
541 if (keepHistory) {
542 mSampleEventTimes = other->mSampleEventTimes;
543 mSamplePointerCoords = other->mSamplePointerCoords;
544 } else {
545 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500546 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700547 mSamplePointerCoords.clear();
548 size_t pointerCount = other->getPointerCount();
549 size_t historySize = other->getHistorySize();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800550 mSamplePointerCoords
551 .insert(mSamplePointerCoords.end(),
552 &other->mSamplePointerCoords[historySize * pointerCount],
553 &other->mSamplePointerCoords[historySize * pointerCount + pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700554 }
555}
556
557void MotionEvent::addSample(
558 int64_t eventTime,
559 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500560 mSampleEventTimes.push_back(eventTime);
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800561 mSamplePointerCoords.insert(mSamplePointerCoords.end(), &pointerCoords[0],
562 &pointerCoords[getPointerCount()]);
Jeff Brown5912f952013-07-01 19:10:31 -0700563}
564
Michael Wright635422b2022-12-02 00:43:56 +0000565std::optional<ui::Rotation> MotionEvent::getSurfaceRotation() const {
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800566 // The surface rotation is the rotation from the window's coordinate space to that of the
567 // display. Since the event's transform takes display space coordinates to window space, the
568 // returned surface rotation is the inverse of the rotation for the surface.
569 switch (mTransform.getOrientation()) {
570 case ui::Transform::ROT_0:
Michael Wright635422b2022-12-02 00:43:56 +0000571 return ui::ROTATION_0;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800572 case ui::Transform::ROT_90:
Michael Wright635422b2022-12-02 00:43:56 +0000573 return ui::ROTATION_270;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800574 case ui::Transform::ROT_180:
Michael Wright635422b2022-12-02 00:43:56 +0000575 return ui::ROTATION_180;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800576 case ui::Transform::ROT_270:
Michael Wright635422b2022-12-02 00:43:56 +0000577 return ui::ROTATION_90;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800578 default:
Michael Wright635422b2022-12-02 00:43:56 +0000579 return std::nullopt;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800580 }
581}
582
Garfield Tan00f511d2019-06-12 16:55:40 -0700583float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700584 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
585 return vals.x;
Garfield Tan00f511d2019-06-12 16:55:40 -0700586}
587
588float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700589 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
590 return vals.y;
Garfield Tan00f511d2019-06-12 16:55:40 -0700591}
592
Garfield Tan937bb832019-07-25 17:48:31 -0700593void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700594 ui::Transform inverse = mTransform.inverse();
595 vec2 vals = inverse.transform(x, y);
596 mRawXCursorPosition = vals.x;
597 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700598}
599
Jeff Brown5912f952013-07-01 19:10:31 -0700600const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000601 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
602 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
603 }
604 const size_t position = getHistorySize() * getPointerCount() + pointerIndex;
605 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
606 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
607 }
608 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700609}
610
611float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
Evan Rosky84f07f02021-04-16 10:42:42 -0700612 return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700613}
614
615float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700616 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700617}
618
619const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
620 size_t pointerIndex, size_t historicalIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000621 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
622 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
623 }
624 if (CC_UNLIKELY(historicalIndex < 0 || historicalIndex > getHistorySize())) {
625 LOG(FATAL) << __func__ << ": Invalid historical index " << historicalIndex << " for "
626 << *this;
627 }
628 const size_t position = historicalIndex * getPointerCount() + pointerIndex;
629 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
630 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
631 }
632 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700633}
634
635float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan6b384612021-05-14 16:56:25 -0700636 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700637 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
638 return calculateTransformedAxisValue(axis, mSource, mRawTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700639}
640
641float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan9f388812021-05-13 16:54:53 -0700642 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700643 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
644 return calculateTransformedAxisValue(axis, mSource, mTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700645}
646
647ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
648 size_t pointerCount = mPointerProperties.size();
649 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800650 if (mPointerProperties[i].id == pointerId) {
Jeff Brown5912f952013-07-01 19:10:31 -0700651 return i;
652 }
653 }
654 return -1;
655}
656
657void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700658 float currXOffset = mTransform.tx();
659 float currYOffset = mTransform.ty();
660 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700661}
662
Robert Carre07e1032018-11-26 12:55:53 -0800663void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700664 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700665 mRawTransform.set(mRawTransform.tx() * globalScaleFactor,
666 mRawTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800667 mXPrecision *= globalScaleFactor;
668 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700669
670 size_t numSamples = mSamplePointerCoords.size();
671 for (size_t i = 0; i < numSamples; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800672 mSamplePointerCoords[i].scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700673 }
674}
675
chaviw9eaa22c2020-07-01 16:21:27 -0700676void MotionEvent::transform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700677 // We want to preserve the raw axes values stored in the PointerCoords, so we just update the
678 // transform using the values passed in.
chaviw9eaa22c2020-07-01 16:21:27 -0700679 ui::Transform newTransform;
680 newTransform.set(matrix);
681 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700682}
683
Evan Roskyd4d4d802021-05-03 20:12:21 -0700684void MotionEvent::applyTransform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700685 ui::Transform transform;
686 transform.set(matrix);
Evan Roskyd4d4d802021-05-03 20:12:21 -0700687
688 // Apply the transformation to all samples.
Prabir Pradhan6b384612021-05-14 16:56:25 -0700689 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
690 [&transform](PointerCoords& c) { c.transform(transform); });
Prabir Pradhan4b19bd02021-06-01 17:34:59 -0700691
692 if (mRawXCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION &&
693 mRawYCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) {
694 const vec2 cursor = transform.transform(mRawXCursorPosition, mRawYCursorPosition);
695 mRawXCursorPosition = cursor.x;
696 mRawYCursorPosition = cursor.y;
697 }
Evan Roskyd4d4d802021-05-03 20:12:21 -0700698}
699
Brett Chabotfaa986c2020-11-04 17:39:36 -0800700#ifdef __linux__
chaviw9eaa22c2020-07-01 16:21:27 -0700701static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
702 float dsdx, dtdx, tx, dtdy, dsdy, ty;
703 status_t status = parcel.readFloat(&dsdx);
704 status |= parcel.readFloat(&dtdx);
705 status |= parcel.readFloat(&tx);
706 status |= parcel.readFloat(&dtdy);
707 status |= parcel.readFloat(&dsdy);
708 status |= parcel.readFloat(&ty);
709
710 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
711 return status;
712}
713
714static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
715 status_t status = parcel.writeFloat(transform.dsdx());
716 status |= parcel.writeFloat(transform.dtdx());
717 status |= parcel.writeFloat(transform.tx());
718 status |= parcel.writeFloat(transform.dtdy());
719 status |= parcel.writeFloat(transform.dsdy());
720 status |= parcel.writeFloat(transform.ty());
721 return status;
722}
723
Jeff Brown5912f952013-07-01 19:10:31 -0700724status_t MotionEvent::readFromParcel(Parcel* parcel) {
725 size_t pointerCount = parcel->readInt32();
726 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800727 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
728 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700729 return BAD_VALUE;
730 }
731
Garfield Tan4cc839f2020-01-24 11:26:14 -0800732 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700733 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600734 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800735 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600736 std::vector<uint8_t> hmac;
737 status_t result = parcel->readByteVector(&hmac);
738 if (result != OK || hmac.size() != 32) {
739 return BAD_VALUE;
740 }
741 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700742 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100743 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700744 mFlags = parcel->readInt32();
745 mEdgeFlags = parcel->readInt32();
746 mMetaState = parcel->readInt32();
747 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800748 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700749
750 result = android::readFromParcel(mTransform, *parcel);
751 if (result != OK) {
752 return result;
753 }
Jeff Brown5912f952013-07-01 19:10:31 -0700754 mXPrecision = parcel->readFloat();
755 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700756 mRawXCursorPosition = parcel->readFloat();
757 mRawYCursorPosition = parcel->readFloat();
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700758
759 result = android::readFromParcel(mRawTransform, *parcel);
760 if (result != OK) {
761 return result;
762 }
Jeff Brown5912f952013-07-01 19:10:31 -0700763 mDownTime = parcel->readInt64();
764
765 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800766 mPointerProperties.reserve(pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700767 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500768 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700769 mSamplePointerCoords.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800770 mSamplePointerCoords.reserve(sampleCount * pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700771
772 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800773 mPointerProperties.push_back({});
774 PointerProperties& properties = mPointerProperties.back();
Jeff Brown5912f952013-07-01 19:10:31 -0700775 properties.id = parcel->readInt32();
776 properties.toolType = parcel->readInt32();
777 }
778
Dan Austinc94fc452015-09-22 14:22:41 -0700779 while (sampleCount > 0) {
780 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500781 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700782 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800783 mSamplePointerCoords.push_back({});
784 status_t status = mSamplePointerCoords.back().readFromParcel(parcel);
Jeff Brown5912f952013-07-01 19:10:31 -0700785 if (status) {
786 return status;
787 }
788 }
789 }
790 return OK;
791}
792
793status_t MotionEvent::writeToParcel(Parcel* parcel) const {
794 size_t pointerCount = mPointerProperties.size();
795 size_t sampleCount = mSampleEventTimes.size();
796
797 parcel->writeInt32(pointerCount);
798 parcel->writeInt32(sampleCount);
799
Garfield Tan4cc839f2020-01-24 11:26:14 -0800800 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700801 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600802 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800803 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600804 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
805 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700806 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100807 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700808 parcel->writeInt32(mFlags);
809 parcel->writeInt32(mEdgeFlags);
810 parcel->writeInt32(mMetaState);
811 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800812 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700813
814 status_t result = android::writeToParcel(mTransform, *parcel);
815 if (result != OK) {
816 return result;
817 }
Jeff Brown5912f952013-07-01 19:10:31 -0700818 parcel->writeFloat(mXPrecision);
819 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700820 parcel->writeFloat(mRawXCursorPosition);
821 parcel->writeFloat(mRawYCursorPosition);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700822
823 result = android::writeToParcel(mRawTransform, *parcel);
824 if (result != OK) {
825 return result;
826 }
Jeff Brown5912f952013-07-01 19:10:31 -0700827 parcel->writeInt64(mDownTime);
828
829 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800830 const PointerProperties& properties = mPointerProperties[i];
Jeff Brown5912f952013-07-01 19:10:31 -0700831 parcel->writeInt32(properties.id);
832 parcel->writeInt32(properties.toolType);
833 }
834
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800835 const PointerCoords* pc = mSamplePointerCoords.data();
Jeff Brown5912f952013-07-01 19:10:31 -0700836 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500837 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700838 for (size_t i = 0; i < pointerCount; i++) {
839 status_t status = (pc++)->writeToParcel(parcel);
840 if (status) {
841 return status;
842 }
843 }
844 }
845 return OK;
846}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800847#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700848
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600849bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700850 if (isFromSource(source, AINPUT_SOURCE_CLASS_POINTER)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700851 // Specifically excludes HOVER_MOVE and SCROLL.
852 switch (action & AMOTION_EVENT_ACTION_MASK) {
853 case AMOTION_EVENT_ACTION_DOWN:
854 case AMOTION_EVENT_ACTION_MOVE:
855 case AMOTION_EVENT_ACTION_UP:
856 case AMOTION_EVENT_ACTION_POINTER_DOWN:
857 case AMOTION_EVENT_ACTION_POINTER_UP:
858 case AMOTION_EVENT_ACTION_CANCEL:
859 case AMOTION_EVENT_ACTION_OUTSIDE:
860 return true;
861 }
862 }
863 return false;
864}
865
Michael Wright872db4f2014-04-22 15:03:51 -0700866const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700867 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700868}
869
870int32_t MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700871 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700872}
873
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500874std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700875 // Convert MotionEvent action to string
876 switch (action & AMOTION_EVENT_ACTION_MASK) {
877 case AMOTION_EVENT_ACTION_DOWN:
878 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700879 case AMOTION_EVENT_ACTION_UP:
880 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500881 case AMOTION_EVENT_ACTION_MOVE:
882 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700883 case AMOTION_EVENT_ACTION_CANCEL:
884 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500885 case AMOTION_EVENT_ACTION_OUTSIDE:
886 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700887 case AMOTION_EVENT_ACTION_POINTER_DOWN:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000888 return StringPrintf("POINTER_DOWN(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700889 case AMOTION_EVENT_ACTION_POINTER_UP:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000890 return StringPrintf("POINTER_UP(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500891 case AMOTION_EVENT_ACTION_HOVER_MOVE:
892 return "HOVER_MOVE";
893 case AMOTION_EVENT_ACTION_SCROLL:
894 return "SCROLL";
895 case AMOTION_EVENT_ACTION_HOVER_ENTER:
896 return "HOVER_ENTER";
897 case AMOTION_EVENT_ACTION_HOVER_EXIT:
898 return "HOVER_EXIT";
899 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
900 return "BUTTON_PRESS";
901 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
902 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700903 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500904 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700905}
906
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700907// Apply the given transformation to the point without checking whether the entire transform
908// should be disregarded altogether for the provided source.
909static inline vec2 calculateTransformedXYUnchecked(uint32_t source, const ui::Transform& transform,
910 const vec2& xy) {
911 return shouldDisregardOffset(source) ? transformWithoutTranslation(transform, xy)
912 : transform.transform(xy);
913}
914
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700915vec2 MotionEvent::calculateTransformedXY(uint32_t source, const ui::Transform& transform,
916 const vec2& xy) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700917 if (shouldDisregardTransformation(source)) {
918 return xy;
919 }
920 return calculateTransformedXYUnchecked(source, transform, xy);
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700921}
922
Prabir Pradhan8e6ce222022-02-24 09:08:54 -0800923// Keep in sync with calculateTransformedCoords.
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700924float MotionEvent::calculateTransformedAxisValue(int32_t axis, uint32_t source,
925 const ui::Transform& transform,
926 const PointerCoords& coords) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700927 if (shouldDisregardTransformation(source)) {
928 return coords.getAxisValue(axis);
929 }
930
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700931 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700932 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700933 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
934 return xy[axis];
935 }
936
937 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
938 const vec2 relativeXy =
939 transformWithoutTranslation(transform,
940 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
941 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
942 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
943 }
944
945 if (axis == AMOTION_EVENT_AXIS_ORIENTATION) {
946 return transformAngle(transform, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION));
947 }
948
949 return coords.getAxisValue(axis);
950}
951
Prabir Pradhan8e6ce222022-02-24 09:08:54 -0800952// Keep in sync with calculateTransformedAxisValue. This is an optimization of
953// calculateTransformedAxisValue for all PointerCoords axes.
954PointerCoords MotionEvent::calculateTransformedCoords(uint32_t source,
955 const ui::Transform& transform,
956 const PointerCoords& coords) {
957 if (shouldDisregardTransformation(source)) {
958 return coords;
959 }
960 PointerCoords out = coords;
961
962 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
963 out.setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
964 out.setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
965
966 const vec2 relativeXy =
967 transformWithoutTranslation(transform,
968 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
969 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
970 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
971 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
972
973 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
974 transformAngle(transform,
975 coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)));
976
977 return out;
978}
979
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000980std::ostream& operator<<(std::ostream& out, const MotionEvent& event) {
981 out << "MotionEvent { action=" << MotionEvent::actionToString(event.getAction());
982 if (event.getActionButton() != 0) {
983 out << ", actionButton=" << std::to_string(event.getActionButton());
984 }
985 const size_t pointerCount = event.getPointerCount();
hupeng3aa5a51a2022-09-02 16:00:18 +0800986 LOG_ALWAYS_FATAL_IF(pointerCount > MAX_POINTERS, "Too many pointers : pointerCount = %zu",
987 pointerCount);
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000988 for (size_t i = 0; i < pointerCount; i++) {
989 out << ", id[" << i << "]=" << event.getPointerId(i);
990 float x = event.getX(i);
991 float y = event.getY(i);
992 if (x != 0 || y != 0) {
993 out << ", x[" << i << "]=" << x;
994 out << ", y[" << i << "]=" << y;
995 }
996 int toolType = event.getToolType(i);
997 if (toolType != AMOTION_EVENT_TOOL_TYPE_FINGER) {
998 out << ", toolType[" << i << "]=" << toolType;
999 }
1000 }
1001 if (event.getButtonState() != 0) {
1002 out << ", buttonState=" << event.getButtonState();
1003 }
1004 if (event.getClassification() != MotionClassification::NONE) {
1005 out << ", classification=" << motionClassificationToString(event.getClassification());
1006 }
1007 if (event.getMetaState() != 0) {
1008 out << ", metaState=" << event.getMetaState();
1009 }
1010 if (event.getEdgeFlags() != 0) {
1011 out << ", edgeFlags=" << event.getEdgeFlags();
1012 }
1013 if (pointerCount != 1) {
1014 out << ", pointerCount=" << pointerCount;
1015 }
1016 if (event.getHistorySize() != 0) {
1017 out << ", historySize=" << event.getHistorySize();
1018 }
1019 out << ", eventTime=" << event.getEventTime();
1020 out << ", downTime=" << event.getDownTime();
1021 out << ", deviceId=" << event.getDeviceId();
1022 out << ", source=" << inputEventSourceToString(event.getSource());
1023 out << ", displayId=" << event.getDisplayId();
1024 out << ", eventId=" << event.getId();
1025 out << "}";
1026 return out;
1027}
1028
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001029// --- FocusEvent ---
1030
Antonio Kantek3cfec7b2021-11-05 18:26:17 -07001031void FocusEvent::initialize(int32_t id, bool hasFocus) {
Garfield Tan4cc839f2020-01-24 11:26:14 -08001032 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -06001033 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001034 mHasFocus = hasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001035}
1036
1037void FocusEvent::initialize(const FocusEvent& from) {
1038 InputEvent::initialize(from);
1039 mHasFocus = from.mHasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001040}
Jeff Brown5912f952013-07-01 19:10:31 -07001041
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001042// --- CaptureEvent ---
1043
1044void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) {
1045 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1046 ADISPLAY_ID_NONE, INVALID_HMAC);
1047 mPointerCaptureEnabled = pointerCaptureEnabled;
1048}
1049
1050void CaptureEvent::initialize(const CaptureEvent& from) {
1051 InputEvent::initialize(from);
1052 mPointerCaptureEnabled = from.mPointerCaptureEnabled;
1053}
1054
arthurhung7632c332020-12-30 16:58:01 +08001055// --- DragEvent ---
1056
1057void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) {
1058 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1059 ADISPLAY_ID_NONE, INVALID_HMAC);
1060 mIsExiting = isExiting;
1061 mX = x;
1062 mY = y;
1063}
1064
1065void DragEvent::initialize(const DragEvent& from) {
1066 InputEvent::initialize(from);
1067 mIsExiting = from.mIsExiting;
1068 mX = from.mX;
1069 mY = from.mY;
1070}
1071
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001072// --- TouchModeEvent ---
1073
1074void TouchModeEvent::initialize(int32_t id, bool isInTouchMode) {
1075 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1076 ADISPLAY_ID_NONE, INVALID_HMAC);
1077 mIsInTouchMode = isInTouchMode;
1078}
1079
1080void TouchModeEvent::initialize(const TouchModeEvent& from) {
1081 InputEvent::initialize(from);
1082 mIsInTouchMode = from.mIsInTouchMode;
1083}
1084
Jeff Brown5912f952013-07-01 19:10:31 -07001085// --- PooledInputEventFactory ---
1086
1087PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
1088 mMaxPoolSize(maxPoolSize) {
1089}
1090
1091PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -07001092}
1093
1094KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001095 if (mKeyEventPool.empty()) {
1096 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001097 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001098 KeyEvent* event = mKeyEventPool.front().release();
1099 mKeyEventPool.pop();
1100 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001101}
1102
1103MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001104 if (mMotionEventPool.empty()) {
1105 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001106 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001107 MotionEvent* event = mMotionEventPool.front().release();
1108 mMotionEventPool.pop();
1109 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001110}
1111
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001112FocusEvent* PooledInputEventFactory::createFocusEvent() {
1113 if (mFocusEventPool.empty()) {
1114 return new FocusEvent();
1115 }
1116 FocusEvent* event = mFocusEventPool.front().release();
1117 mFocusEventPool.pop();
1118 return event;
1119}
1120
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001121CaptureEvent* PooledInputEventFactory::createCaptureEvent() {
1122 if (mCaptureEventPool.empty()) {
1123 return new CaptureEvent();
1124 }
1125 CaptureEvent* event = mCaptureEventPool.front().release();
1126 mCaptureEventPool.pop();
1127 return event;
1128}
1129
arthurhung7632c332020-12-30 16:58:01 +08001130DragEvent* PooledInputEventFactory::createDragEvent() {
1131 if (mDragEventPool.empty()) {
1132 return new DragEvent();
1133 }
1134 DragEvent* event = mDragEventPool.front().release();
1135 mDragEventPool.pop();
1136 return event;
1137}
1138
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001139TouchModeEvent* PooledInputEventFactory::createTouchModeEvent() {
1140 if (mTouchModeEventPool.empty()) {
1141 return new TouchModeEvent();
1142 }
1143 TouchModeEvent* event = mTouchModeEventPool.front().release();
1144 mTouchModeEventPool.pop();
1145 return event;
1146}
1147
Jeff Brown5912f952013-07-01 19:10:31 -07001148void PooledInputEventFactory::recycle(InputEvent* event) {
1149 switch (event->getType()) {
1150 case AINPUT_EVENT_TYPE_KEY:
1151 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001152 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -07001153 return;
1154 }
1155 break;
1156 case AINPUT_EVENT_TYPE_MOTION:
1157 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001158 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -07001159 return;
1160 }
1161 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001162 case AINPUT_EVENT_TYPE_FOCUS:
1163 if (mFocusEventPool.size() < mMaxPoolSize) {
1164 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
1165 return;
1166 }
1167 break;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001168 case AINPUT_EVENT_TYPE_CAPTURE:
1169 if (mCaptureEventPool.size() < mMaxPoolSize) {
1170 mCaptureEventPool.push(
1171 std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event)));
1172 return;
1173 }
1174 break;
arthurhung7632c332020-12-30 16:58:01 +08001175 case AINPUT_EVENT_TYPE_DRAG:
1176 if (mDragEventPool.size() < mMaxPoolSize) {
1177 mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event)));
1178 return;
1179 }
1180 break;
Antonio Kantekeb4a30c2021-09-28 17:49:49 -07001181 case AINPUT_EVENT_TYPE_TOUCH_MODE:
1182 if (mTouchModeEventPool.size() < mMaxPoolSize) {
1183 mTouchModeEventPool.push(
1184 std::unique_ptr<TouchModeEvent>(static_cast<TouchModeEvent*>(event)));
1185 return;
1186 }
1187 break;
Jeff Brown5912f952013-07-01 19:10:31 -07001188 }
1189 delete event;
1190}
1191
1192} // namespace android