blob: 3685f54a53733a3ca98b28f6cbc4f255d6a51e78 [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>
Jeff Brown5912f952013-07-01 19:10:31 -070024
Siarhei Vishniakou31977182022-09-30 08:51:23 -070025#include <android-base/file.h>
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +000026#include <android-base/logging.h>
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050027#include <android-base/stringprintf.h>
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +000028#include <cutils/compiler.h>
chaviw98318de2021-05-19 16:45:23 -050029#include <gui/constants.h>
Prabir Pradhan092f3a92021-11-25 10:53:27 -080030#include <input/DisplayViewport.h>
Jeff Brown5912f952013-07-01 19:10:31 -070031#include <input/Input.h>
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080032#include <input/InputDevice.h>
Michael Wright872db4f2014-04-22 15:03:51 -070033#include <input/InputEventLabels.h>
Jeff Brown5912f952013-07-01 19:10:31 -070034
Brett Chabotfaa986c2020-11-04 17:39:36 -080035#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -070036#include <binder/Parcel.h>
Brett Chabotfaa986c2020-11-04 17:39:36 -080037#endif
Siarhei Vishniakou63740b92022-10-20 10:28:08 -070038#if defined(__ANDROID__)
39#include <sys/random.h>
40#endif
Jeff Brown5912f952013-07-01 19:10:31 -070041
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050042using android::base::StringPrintf;
43
Jeff Brown5912f952013-07-01 19:10:31 -070044namespace android {
45
Prabir Pradhan6b384612021-05-14 16:56:25 -070046namespace {
47
48float transformAngle(const ui::Transform& transform, float angleRadians) {
49 // Construct and transform a vector oriented at the specified clockwise angle from vertical.
50 // Coordinate system: down is increasing Y, right is increasing X.
51 float x = sinf(angleRadians);
52 float y = -cosf(angleRadians);
53 vec2 transformedPoint = transform.transform(x, y);
54
55 // Determine how the origin is transformed by the matrix so that we
56 // can transform orientation vectors.
57 const vec2 origin = transform.transform(0, 0);
58
59 transformedPoint.x -= origin.x;
60 transformedPoint.y -= origin.y;
61
62 // Derive the transformed vector's clockwise angle from vertical.
Prabir Pradhand2b02672021-10-19 11:24:45 -070063 // The return value of atan2f is in range [-pi, pi] which conforms to the orientation API.
64 return atan2f(transformedPoint.x, -transformedPoint.y);
Prabir Pradhan6b384612021-05-14 16:56:25 -070065}
66
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070067bool shouldDisregardTransformation(uint32_t source) {
Prabir Pradhan258e2b92022-06-24 18:37:04 +000068 // Do not apply any transformations to axes from joysticks, touchpads, or relative mice.
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070069 return isFromSource(source, AINPUT_SOURCE_CLASS_JOYSTICK) ||
Prabir Pradhan258e2b92022-06-24 18:37:04 +000070 isFromSource(source, AINPUT_SOURCE_CLASS_POSITION) ||
71 isFromSource(source, AINPUT_SOURCE_MOUSE_RELATIVE);
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070072}
73
74bool shouldDisregardOffset(uint32_t source) {
Prabir Pradhan9f388812021-05-13 16:54:53 -070075 // Pointer events are the only type of events that refer to absolute coordinates on the display,
76 // so we should apply the entire window transform. For other types of events, we should make
77 // sure to not apply the window translation/offset.
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070078 return !isFromSource(source, AINPUT_SOURCE_CLASS_POINTER);
Prabir Pradhan9f388812021-05-13 16:54:53 -070079}
80
Prabir Pradhan6b384612021-05-14 16:56:25 -070081} // namespace
82
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -080083const char* motionClassificationToString(MotionClassification classification) {
84 switch (classification) {
85 case MotionClassification::NONE:
86 return "NONE";
87 case MotionClassification::AMBIGUOUS_GESTURE:
88 return "AMBIGUOUS_GESTURE";
89 case MotionClassification::DEEP_PRESS:
90 return "DEEP_PRESS";
Harry Cutts2800fb02022-09-15 13:49:23 +000091 case MotionClassification::TWO_FINGER_SWIPE:
92 return "TWO_FINGER_SWIPE";
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -080093 }
94}
95
Siarhei Vishniakoud5fe5182022-07-20 23:28:40 +000096const char* motionToolTypeToString(int32_t toolType) {
97 switch (toolType) {
98 case AMOTION_EVENT_TOOL_TYPE_UNKNOWN:
99 return "UNKNOWN";
100 case AMOTION_EVENT_TOOL_TYPE_FINGER:
101 return "FINGER";
102 case AMOTION_EVENT_TOOL_TYPE_STYLUS:
103 return "STYLUS";
104 case AMOTION_EVENT_TOOL_TYPE_MOUSE:
105 return "MOUSE";
106 case AMOTION_EVENT_TOOL_TYPE_ERASER:
107 return "ERASER";
108 case AMOTION_EVENT_TOOL_TYPE_PALM:
109 return "PALM";
110 default:
111 return "INVALID";
112 }
113}
114
Garfield Tan84b087e2020-01-23 10:49:05 -0800115// --- IdGenerator ---
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700116#if defined(__ANDROID__)
117[[maybe_unused]]
118#endif
119static status_t
120getRandomBytes(uint8_t* data, size_t size) {
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700121 int ret = TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY | O_CLOEXEC | O_NOFOLLOW));
122 if (ret == -1) {
123 return -errno;
124 }
125
126 base::unique_fd fd(ret);
127 if (!base::ReadFully(fd, data, size)) {
128 return -errno;
129 }
130 return OK;
131}
132
Garfield Tan84b087e2020-01-23 10:49:05 -0800133IdGenerator::IdGenerator(Source source) : mSource(source) {}
134
135int32_t IdGenerator::nextId() const {
136 constexpr uint32_t SEQUENCE_NUMBER_MASK = ~SOURCE_MASK;
137 int32_t id = 0;
138
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700139#if defined(__ANDROID__)
140 // On device, prefer 'getrandom' to '/dev/urandom' because it's faster.
141 constexpr size_t BUF_LEN = sizeof(id);
142 size_t totalBytes = 0;
143 while (totalBytes < BUF_LEN) {
144 ssize_t bytes = TEMP_FAILURE_RETRY(getrandom(&id, BUF_LEN, GRND_NONBLOCK));
145 if (CC_UNLIKELY(bytes < 0)) {
146 ALOGW("Failed to fill in random number for sequence number: %s.", strerror(errno));
147 id = 0;
148 break;
149 }
150 totalBytes += bytes;
151 }
152#else
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700153#if defined(__linux__)
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700154 // On host, <sys/random.h> / GRND_NONBLOCK is not available
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700155 while (true) {
156 status_t result = getRandomBytes(reinterpret_cast<uint8_t*>(&id), sizeof(id));
157 if (result == OK) {
Garfield Tan84b087e2020-01-23 10:49:05 -0800158 break;
159 }
Garfield Tan84b087e2020-01-23 10:49:05 -0800160 }
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700161#endif // __linux__
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700162#endif // __ANDROID__
Garfield Tan84b087e2020-01-23 10:49:05 -0800163 return (id & SEQUENCE_NUMBER_MASK) | static_cast<int32_t>(mSource);
164}
165
Jeff Brown5912f952013-07-01 19:10:31 -0700166// --- InputEvent ---
167
Prabir Pradhande69f8a2021-11-18 16:40:34 +0000168vec2 transformWithoutTranslation(const ui::Transform& transform, const vec2& xy) {
169 const vec2 transformedXy = transform.transform(xy);
170 const vec2 transformedOrigin = transform.transform(0, 0);
171 return transformedXy - transformedOrigin;
172}
173
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800174const char* inputEventTypeToString(int32_t type) {
175 switch (type) {
176 case AINPUT_EVENT_TYPE_KEY: {
177 return "KEY";
178 }
179 case AINPUT_EVENT_TYPE_MOTION: {
180 return "MOTION";
181 }
182 case AINPUT_EVENT_TYPE_FOCUS: {
183 return "FOCUS";
184 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800185 case AINPUT_EVENT_TYPE_CAPTURE: {
186 return "CAPTURE";
187 }
arthurhung7632c332020-12-30 16:58:01 +0800188 case AINPUT_EVENT_TYPE_DRAG: {
189 return "DRAG";
190 }
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700191 case AINPUT_EVENT_TYPE_TOUCH_MODE: {
192 return "TOUCH_MODE";
193 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800194 }
195 return "UNKNOWN";
196}
197
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800198std::string inputEventSourceToString(int32_t source) {
199 if (source == AINPUT_SOURCE_UNKNOWN) {
200 return "UNKNOWN";
201 }
202 if (source == static_cast<int32_t>(AINPUT_SOURCE_ANY)) {
203 return "ANY";
204 }
205 static const std::map<int32_t, const char*> SOURCES{
206 {AINPUT_SOURCE_KEYBOARD, "KEYBOARD"},
207 {AINPUT_SOURCE_DPAD, "DPAD"},
208 {AINPUT_SOURCE_GAMEPAD, "GAMEPAD"},
209 {AINPUT_SOURCE_TOUCHSCREEN, "TOUCHSCREEN"},
210 {AINPUT_SOURCE_MOUSE, "MOUSE"},
211 {AINPUT_SOURCE_STYLUS, "STYLUS"},
212 {AINPUT_SOURCE_BLUETOOTH_STYLUS, "BLUETOOTH_STYLUS"},
213 {AINPUT_SOURCE_TRACKBALL, "TRACKBALL"},
214 {AINPUT_SOURCE_MOUSE_RELATIVE, "MOUSE_RELATIVE"},
215 {AINPUT_SOURCE_TOUCHPAD, "TOUCHPAD"},
216 {AINPUT_SOURCE_TOUCH_NAVIGATION, "TOUCH_NAVIGATION"},
217 {AINPUT_SOURCE_JOYSTICK, "JOYSTICK"},
218 {AINPUT_SOURCE_HDMI, "HDMI"},
219 {AINPUT_SOURCE_SENSOR, "SENSOR"},
220 {AINPUT_SOURCE_ROTARY_ENCODER, "ROTARY_ENCODER"},
221 };
222 std::string result;
223 for (const auto& [source_entry, str] : SOURCES) {
224 if ((source & source_entry) == source_entry) {
225 if (!result.empty()) {
226 result += " | ";
227 }
228 result += str;
229 }
230 }
231 if (result.empty()) {
232 result = StringPrintf("0x%08x", source);
233 }
234 return result;
235}
236
237bool isFromSource(uint32_t source, uint32_t test) {
238 return (source & test) == test;
239}
240
Prabir Pradhane5626962022-10-27 20:30:53 +0000241bool isStylusToolType(uint32_t toolType) {
242 return toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS || toolType == AMOTION_EVENT_TOOL_TYPE_ERASER;
243}
244
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800245VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
246 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
247 event.getSource(), event.getDisplayId()},
248 event.getAction(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800249 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800250 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800251 event.getKeyCode(),
252 event.getScanCode(),
253 event.getMetaState(),
254 event.getRepeatCount()};
255}
256
257VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
258 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
259 event.getSource(), event.getDisplayId()},
260 event.getRawX(0),
261 event.getRawY(0),
262 event.getActionMasked(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800263 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800264 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800265 event.getMetaState(),
266 event.getButtonState()};
267}
268
Garfield Tan4cc839f2020-01-24 11:26:14 -0800269void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600270 std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800271 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700272 mDeviceId = deviceId;
273 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100274 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600275 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700276}
277
278void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800279 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700280 mDeviceId = from.mDeviceId;
281 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100282 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600283 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700284}
285
Garfield Tan4cc839f2020-01-24 11:26:14 -0800286int32_t InputEvent::nextId() {
287 static IdGenerator idGen(IdGenerator::Source::OTHER);
288 return idGen.nextId();
289}
290
Jeff Brown5912f952013-07-01 19:10:31 -0700291// --- KeyEvent ---
292
Michael Wright872db4f2014-04-22 15:03:51 -0700293const char* KeyEvent::getLabel(int32_t keyCode) {
Chris Ye4958d062020-08-20 13:21:10 -0700294 return InputEventLookup::getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700295}
296
Michael Wright872db4f2014-04-22 15:03:51 -0700297int32_t KeyEvent::getKeyCodeFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700298 return InputEventLookup::getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700299}
300
Garfield Tan4cc839f2020-01-24 11:26:14 -0800301void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600302 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
303 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
304 nsecs_t downTime, nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800305 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700306 mAction = action;
307 mFlags = flags;
308 mKeyCode = keyCode;
309 mScanCode = scanCode;
310 mMetaState = metaState;
311 mRepeatCount = repeatCount;
312 mDownTime = downTime;
313 mEventTime = eventTime;
314}
315
316void KeyEvent::initialize(const KeyEvent& from) {
317 InputEvent::initialize(from);
318 mAction = from.mAction;
319 mFlags = from.mFlags;
320 mKeyCode = from.mKeyCode;
321 mScanCode = from.mScanCode;
322 mMetaState = from.mMetaState;
323 mRepeatCount = from.mRepeatCount;
324 mDownTime = from.mDownTime;
325 mEventTime = from.mEventTime;
326}
327
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700328const char* KeyEvent::actionToString(int32_t action) {
329 // Convert KeyEvent action to string
330 switch (action) {
331 case AKEY_EVENT_ACTION_DOWN:
332 return "DOWN";
333 case AKEY_EVENT_ACTION_UP:
334 return "UP";
335 case AKEY_EVENT_ACTION_MULTIPLE:
336 return "MULTIPLE";
337 }
338 return "UNKNOWN";
339}
Jeff Brown5912f952013-07-01 19:10:31 -0700340
341// --- PointerCoords ---
342
343float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700344 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700345 return 0;
346 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700347 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700348}
349
350status_t PointerCoords::setAxisValue(int32_t axis, float value) {
351 if (axis < 0 || axis > 63) {
352 return NAME_NOT_FOUND;
353 }
354
Michael Wright38dcdff2014-03-19 12:06:10 -0700355 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
356 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700357 if (value == 0) {
358 return OK; // axes with value 0 do not need to be stored
359 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700360
361 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700362 if (count >= MAX_AXES) {
363 tooManyAxes(axis);
364 return NO_MEMORY;
365 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700366 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700367 for (uint32_t i = count; i > index; i--) {
368 values[i] = values[i - 1];
369 }
370 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700371
Jeff Brown5912f952013-07-01 19:10:31 -0700372 values[index] = value;
373 return OK;
374}
375
376static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
377 float value = c.getAxisValue(axis);
378 if (value != 0) {
379 c.setAxisValue(axis, value * scaleFactor);
380 }
381}
382
Robert Carre07e1032018-11-26 12:55:53 -0800383void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700384 // No need to scale pressure or size since they are normalized.
385 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800386
387 // If there is a global scale factor, it is included in the windowX/YScale
388 // so we don't need to apply it twice to the X/Y axes.
389 // However we don't want to apply any windowXYScale not included in the global scale
390 // to the TOUCH_MAJOR/MINOR coordinates.
391 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
392 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
393 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
394 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
395 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
396 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
Prabir Pradhanc6523582021-05-14 18:02:55 -0700397 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_X, windowXScale);
398 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_Y, windowYScale);
Robert Carre07e1032018-11-26 12:55:53 -0800399}
400
Brett Chabotfaa986c2020-11-04 17:39:36 -0800401#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700402status_t PointerCoords::readFromParcel(Parcel* parcel) {
403 bits = parcel->readInt64();
404
Michael Wright38dcdff2014-03-19 12:06:10 -0700405 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700406 if (count > MAX_AXES) {
407 return BAD_VALUE;
408 }
409
410 for (uint32_t i = 0; i < count; i++) {
411 values[i] = parcel->readFloat();
412 }
413 return OK;
414}
415
416status_t PointerCoords::writeToParcel(Parcel* parcel) const {
417 parcel->writeInt64(bits);
418
Michael Wright38dcdff2014-03-19 12:06:10 -0700419 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700420 for (uint32_t i = 0; i < count; i++) {
421 parcel->writeFloat(values[i]);
422 }
423 return OK;
424}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800425#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700426
427void PointerCoords::tooManyAxes(int axis) {
428 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
429 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
430}
431
432bool PointerCoords::operator==(const PointerCoords& other) const {
433 if (bits != other.bits) {
434 return false;
435 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700436 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700437 for (uint32_t i = 0; i < count; i++) {
438 if (values[i] != other.values[i]) {
439 return false;
440 }
441 }
442 return true;
443}
444
chaviwc01e1372020-07-01 12:37:31 -0700445void PointerCoords::transform(const ui::Transform& transform) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700446 const vec2 xy = transform.transform(getXYValue());
447 setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
448 setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
449
Prabir Pradhanc6523582021-05-14 18:02:55 -0700450 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_X) ||
451 BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_Y)) {
452 const ui::Transform rotation(transform.getOrientation());
453 const vec2 relativeXy = rotation.transform(getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
454 getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
455 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
456 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
457 }
458
Prabir Pradhan6b384612021-05-14 16:56:25 -0700459 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) {
460 const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
461 setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val));
462 }
chaviwc01e1372020-07-01 12:37:31 -0700463}
Jeff Brown5912f952013-07-01 19:10:31 -0700464
465// --- PointerProperties ---
466
467bool PointerProperties::operator==(const PointerProperties& other) const {
468 return id == other.id
469 && toolType == other.toolType;
470}
471
472void PointerProperties::copyFrom(const PointerProperties& other) {
473 id = other.id;
474 toolType = other.toolType;
475}
476
477
478// --- MotionEvent ---
479
Garfield Tan4cc839f2020-01-24 11:26:14 -0800480void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600481 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
482 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700483 int32_t buttonState, MotionClassification classification,
484 const ui::Transform& transform, float xPrecision, float yPrecision,
Evan Rosky84f07f02021-04-16 10:42:42 -0700485 float rawXCursorPosition, float rawYCursorPosition,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700486 const ui::Transform& rawTransform, nsecs_t downTime, nsecs_t eventTime,
Evan Rosky09576692021-07-01 12:22:09 -0700487 size_t pointerCount, const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700488 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800489 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700490 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100491 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700492 mFlags = flags;
493 mEdgeFlags = edgeFlags;
494 mMetaState = metaState;
495 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800496 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700497 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700498 mXPrecision = xPrecision;
499 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700500 mRawXCursorPosition = rawXCursorPosition;
501 mRawYCursorPosition = rawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700502 mRawTransform = rawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700503 mDownTime = downTime;
504 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800505 mPointerProperties.insert(mPointerProperties.end(), &pointerProperties[0],
506 &pointerProperties[pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700507 mSampleEventTimes.clear();
508 mSamplePointerCoords.clear();
509 addSample(eventTime, pointerCoords);
510}
511
512void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800513 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
514 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700515 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100516 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700517 mFlags = other->mFlags;
518 mEdgeFlags = other->mEdgeFlags;
519 mMetaState = other->mMetaState;
520 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800521 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700522 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700523 mXPrecision = other->mXPrecision;
524 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700525 mRawXCursorPosition = other->mRawXCursorPosition;
526 mRawYCursorPosition = other->mRawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700527 mRawTransform = other->mRawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700528 mDownTime = other->mDownTime;
529 mPointerProperties = other->mPointerProperties;
530
531 if (keepHistory) {
532 mSampleEventTimes = other->mSampleEventTimes;
533 mSamplePointerCoords = other->mSamplePointerCoords;
534 } else {
535 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500536 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700537 mSamplePointerCoords.clear();
538 size_t pointerCount = other->getPointerCount();
539 size_t historySize = other->getHistorySize();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800540 mSamplePointerCoords
541 .insert(mSamplePointerCoords.end(),
542 &other->mSamplePointerCoords[historySize * pointerCount],
543 &other->mSamplePointerCoords[historySize * pointerCount + pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700544 }
545}
546
547void MotionEvent::addSample(
548 int64_t eventTime,
549 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500550 mSampleEventTimes.push_back(eventTime);
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800551 mSamplePointerCoords.insert(mSamplePointerCoords.end(), &pointerCoords[0],
552 &pointerCoords[getPointerCount()]);
Jeff Brown5912f952013-07-01 19:10:31 -0700553}
554
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800555int MotionEvent::getSurfaceRotation() const {
556 // The surface rotation is the rotation from the window's coordinate space to that of the
557 // display. Since the event's transform takes display space coordinates to window space, the
558 // returned surface rotation is the inverse of the rotation for the surface.
559 switch (mTransform.getOrientation()) {
560 case ui::Transform::ROT_0:
561 return DISPLAY_ORIENTATION_0;
562 case ui::Transform::ROT_90:
563 return DISPLAY_ORIENTATION_270;
564 case ui::Transform::ROT_180:
565 return DISPLAY_ORIENTATION_180;
566 case ui::Transform::ROT_270:
567 return DISPLAY_ORIENTATION_90;
568 default:
569 return -1;
570 }
571}
572
Garfield Tan00f511d2019-06-12 16:55:40 -0700573float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700574 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
575 return vals.x;
Garfield Tan00f511d2019-06-12 16:55:40 -0700576}
577
578float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700579 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
580 return vals.y;
Garfield Tan00f511d2019-06-12 16:55:40 -0700581}
582
Garfield Tan937bb832019-07-25 17:48:31 -0700583void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700584 ui::Transform inverse = mTransform.inverse();
585 vec2 vals = inverse.transform(x, y);
586 mRawXCursorPosition = vals.x;
587 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700588}
589
Jeff Brown5912f952013-07-01 19:10:31 -0700590const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000591 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
592 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
593 }
594 const size_t position = getHistorySize() * getPointerCount() + pointerIndex;
595 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
596 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
597 }
598 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700599}
600
601float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
Evan Rosky84f07f02021-04-16 10:42:42 -0700602 return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700603}
604
605float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700606 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700607}
608
609const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
610 size_t pointerIndex, size_t historicalIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000611 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
612 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
613 }
614 if (CC_UNLIKELY(historicalIndex < 0 || historicalIndex > getHistorySize())) {
615 LOG(FATAL) << __func__ << ": Invalid historical index " << historicalIndex << " for "
616 << *this;
617 }
618 const size_t position = historicalIndex * getPointerCount() + pointerIndex;
619 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
620 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
621 }
622 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700623}
624
625float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan6b384612021-05-14 16:56:25 -0700626 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700627 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
628 return calculateTransformedAxisValue(axis, mSource, mRawTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700629}
630
631float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan9f388812021-05-13 16:54:53 -0700632 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700633 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
634 return calculateTransformedAxisValue(axis, mSource, mTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700635}
636
637ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
638 size_t pointerCount = mPointerProperties.size();
639 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800640 if (mPointerProperties[i].id == pointerId) {
Jeff Brown5912f952013-07-01 19:10:31 -0700641 return i;
642 }
643 }
644 return -1;
645}
646
647void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700648 float currXOffset = mTransform.tx();
649 float currYOffset = mTransform.ty();
650 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700651}
652
Robert Carre07e1032018-11-26 12:55:53 -0800653void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700654 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700655 mRawTransform.set(mRawTransform.tx() * globalScaleFactor,
656 mRawTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800657 mXPrecision *= globalScaleFactor;
658 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700659
660 size_t numSamples = mSamplePointerCoords.size();
661 for (size_t i = 0; i < numSamples; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800662 mSamplePointerCoords[i].scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700663 }
664}
665
chaviw9eaa22c2020-07-01 16:21:27 -0700666void MotionEvent::transform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700667 // We want to preserve the raw axes values stored in the PointerCoords, so we just update the
668 // transform using the values passed in.
chaviw9eaa22c2020-07-01 16:21:27 -0700669 ui::Transform newTransform;
670 newTransform.set(matrix);
671 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700672}
673
Evan Roskyd4d4d802021-05-03 20:12:21 -0700674void MotionEvent::applyTransform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700675 ui::Transform transform;
676 transform.set(matrix);
Evan Roskyd4d4d802021-05-03 20:12:21 -0700677
678 // Apply the transformation to all samples.
Prabir Pradhan6b384612021-05-14 16:56:25 -0700679 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
680 [&transform](PointerCoords& c) { c.transform(transform); });
Prabir Pradhan4b19bd02021-06-01 17:34:59 -0700681
682 if (mRawXCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION &&
683 mRawYCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) {
684 const vec2 cursor = transform.transform(mRawXCursorPosition, mRawYCursorPosition);
685 mRawXCursorPosition = cursor.x;
686 mRawYCursorPosition = cursor.y;
687 }
Evan Roskyd4d4d802021-05-03 20:12:21 -0700688}
689
Brett Chabotfaa986c2020-11-04 17:39:36 -0800690#ifdef __linux__
chaviw9eaa22c2020-07-01 16:21:27 -0700691static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
692 float dsdx, dtdx, tx, dtdy, dsdy, ty;
693 status_t status = parcel.readFloat(&dsdx);
694 status |= parcel.readFloat(&dtdx);
695 status |= parcel.readFloat(&tx);
696 status |= parcel.readFloat(&dtdy);
697 status |= parcel.readFloat(&dsdy);
698 status |= parcel.readFloat(&ty);
699
700 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
701 return status;
702}
703
704static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
705 status_t status = parcel.writeFloat(transform.dsdx());
706 status |= parcel.writeFloat(transform.dtdx());
707 status |= parcel.writeFloat(transform.tx());
708 status |= parcel.writeFloat(transform.dtdy());
709 status |= parcel.writeFloat(transform.dsdy());
710 status |= parcel.writeFloat(transform.ty());
711 return status;
712}
713
Jeff Brown5912f952013-07-01 19:10:31 -0700714status_t MotionEvent::readFromParcel(Parcel* parcel) {
715 size_t pointerCount = parcel->readInt32();
716 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800717 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
718 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700719 return BAD_VALUE;
720 }
721
Garfield Tan4cc839f2020-01-24 11:26:14 -0800722 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700723 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600724 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800725 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600726 std::vector<uint8_t> hmac;
727 status_t result = parcel->readByteVector(&hmac);
728 if (result != OK || hmac.size() != 32) {
729 return BAD_VALUE;
730 }
731 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700732 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100733 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700734 mFlags = parcel->readInt32();
735 mEdgeFlags = parcel->readInt32();
736 mMetaState = parcel->readInt32();
737 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800738 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700739
740 result = android::readFromParcel(mTransform, *parcel);
741 if (result != OK) {
742 return result;
743 }
Jeff Brown5912f952013-07-01 19:10:31 -0700744 mXPrecision = parcel->readFloat();
745 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700746 mRawXCursorPosition = parcel->readFloat();
747 mRawYCursorPosition = parcel->readFloat();
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700748
749 result = android::readFromParcel(mRawTransform, *parcel);
750 if (result != OK) {
751 return result;
752 }
Jeff Brown5912f952013-07-01 19:10:31 -0700753 mDownTime = parcel->readInt64();
754
755 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800756 mPointerProperties.reserve(pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700757 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500758 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700759 mSamplePointerCoords.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800760 mSamplePointerCoords.reserve(sampleCount * pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700761
762 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800763 mPointerProperties.push_back({});
764 PointerProperties& properties = mPointerProperties.back();
Jeff Brown5912f952013-07-01 19:10:31 -0700765 properties.id = parcel->readInt32();
766 properties.toolType = parcel->readInt32();
767 }
768
Dan Austinc94fc452015-09-22 14:22:41 -0700769 while (sampleCount > 0) {
770 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500771 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700772 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800773 mSamplePointerCoords.push_back({});
774 status_t status = mSamplePointerCoords.back().readFromParcel(parcel);
Jeff Brown5912f952013-07-01 19:10:31 -0700775 if (status) {
776 return status;
777 }
778 }
779 }
780 return OK;
781}
782
783status_t MotionEvent::writeToParcel(Parcel* parcel) const {
784 size_t pointerCount = mPointerProperties.size();
785 size_t sampleCount = mSampleEventTimes.size();
786
787 parcel->writeInt32(pointerCount);
788 parcel->writeInt32(sampleCount);
789
Garfield Tan4cc839f2020-01-24 11:26:14 -0800790 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700791 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600792 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800793 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600794 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
795 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700796 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100797 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700798 parcel->writeInt32(mFlags);
799 parcel->writeInt32(mEdgeFlags);
800 parcel->writeInt32(mMetaState);
801 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800802 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700803
804 status_t result = android::writeToParcel(mTransform, *parcel);
805 if (result != OK) {
806 return result;
807 }
Jeff Brown5912f952013-07-01 19:10:31 -0700808 parcel->writeFloat(mXPrecision);
809 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700810 parcel->writeFloat(mRawXCursorPosition);
811 parcel->writeFloat(mRawYCursorPosition);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700812
813 result = android::writeToParcel(mRawTransform, *parcel);
814 if (result != OK) {
815 return result;
816 }
Jeff Brown5912f952013-07-01 19:10:31 -0700817 parcel->writeInt64(mDownTime);
818
819 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800820 const PointerProperties& properties = mPointerProperties[i];
Jeff Brown5912f952013-07-01 19:10:31 -0700821 parcel->writeInt32(properties.id);
822 parcel->writeInt32(properties.toolType);
823 }
824
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800825 const PointerCoords* pc = mSamplePointerCoords.data();
Jeff Brown5912f952013-07-01 19:10:31 -0700826 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500827 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700828 for (size_t i = 0; i < pointerCount; i++) {
829 status_t status = (pc++)->writeToParcel(parcel);
830 if (status) {
831 return status;
832 }
833 }
834 }
835 return OK;
836}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800837#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700838
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600839bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700840 if (isFromSource(source, AINPUT_SOURCE_CLASS_POINTER)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700841 // Specifically excludes HOVER_MOVE and SCROLL.
842 switch (action & AMOTION_EVENT_ACTION_MASK) {
843 case AMOTION_EVENT_ACTION_DOWN:
844 case AMOTION_EVENT_ACTION_MOVE:
845 case AMOTION_EVENT_ACTION_UP:
846 case AMOTION_EVENT_ACTION_POINTER_DOWN:
847 case AMOTION_EVENT_ACTION_POINTER_UP:
848 case AMOTION_EVENT_ACTION_CANCEL:
849 case AMOTION_EVENT_ACTION_OUTSIDE:
850 return true;
851 }
852 }
853 return false;
854}
855
Michael Wright872db4f2014-04-22 15:03:51 -0700856const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700857 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700858}
859
860int32_t MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700861 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700862}
863
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500864std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700865 // Convert MotionEvent action to string
866 switch (action & AMOTION_EVENT_ACTION_MASK) {
867 case AMOTION_EVENT_ACTION_DOWN:
868 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700869 case AMOTION_EVENT_ACTION_UP:
870 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500871 case AMOTION_EVENT_ACTION_MOVE:
872 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700873 case AMOTION_EVENT_ACTION_CANCEL:
874 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500875 case AMOTION_EVENT_ACTION_OUTSIDE:
876 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700877 case AMOTION_EVENT_ACTION_POINTER_DOWN:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000878 return StringPrintf("POINTER_DOWN(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700879 case AMOTION_EVENT_ACTION_POINTER_UP:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000880 return StringPrintf("POINTER_UP(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500881 case AMOTION_EVENT_ACTION_HOVER_MOVE:
882 return "HOVER_MOVE";
883 case AMOTION_EVENT_ACTION_SCROLL:
884 return "SCROLL";
885 case AMOTION_EVENT_ACTION_HOVER_ENTER:
886 return "HOVER_ENTER";
887 case AMOTION_EVENT_ACTION_HOVER_EXIT:
888 return "HOVER_EXIT";
889 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
890 return "BUTTON_PRESS";
891 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
892 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700893 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500894 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700895}
896
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700897// Apply the given transformation to the point without checking whether the entire transform
898// should be disregarded altogether for the provided source.
899static inline vec2 calculateTransformedXYUnchecked(uint32_t source, const ui::Transform& transform,
900 const vec2& xy) {
901 return shouldDisregardOffset(source) ? transformWithoutTranslation(transform, xy)
902 : transform.transform(xy);
903}
904
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700905vec2 MotionEvent::calculateTransformedXY(uint32_t source, const ui::Transform& transform,
906 const vec2& xy) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700907 if (shouldDisregardTransformation(source)) {
908 return xy;
909 }
910 return calculateTransformedXYUnchecked(source, transform, xy);
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700911}
912
Prabir Pradhan8e6ce222022-02-24 09:08:54 -0800913// Keep in sync with calculateTransformedCoords.
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700914float MotionEvent::calculateTransformedAxisValue(int32_t axis, uint32_t source,
915 const ui::Transform& transform,
916 const PointerCoords& coords) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700917 if (shouldDisregardTransformation(source)) {
918 return coords.getAxisValue(axis);
919 }
920
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700921 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700922 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700923 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
924 return xy[axis];
925 }
926
927 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
928 const vec2 relativeXy =
929 transformWithoutTranslation(transform,
930 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
931 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
932 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
933 }
934
935 if (axis == AMOTION_EVENT_AXIS_ORIENTATION) {
936 return transformAngle(transform, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION));
937 }
938
939 return coords.getAxisValue(axis);
940}
941
Prabir Pradhan8e6ce222022-02-24 09:08:54 -0800942// Keep in sync with calculateTransformedAxisValue. This is an optimization of
943// calculateTransformedAxisValue for all PointerCoords axes.
944PointerCoords MotionEvent::calculateTransformedCoords(uint32_t source,
945 const ui::Transform& transform,
946 const PointerCoords& coords) {
947 if (shouldDisregardTransformation(source)) {
948 return coords;
949 }
950 PointerCoords out = coords;
951
952 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
953 out.setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
954 out.setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
955
956 const vec2 relativeXy =
957 transformWithoutTranslation(transform,
958 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
959 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
960 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
961 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
962
963 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
964 transformAngle(transform,
965 coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)));
966
967 return out;
968}
969
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000970std::ostream& operator<<(std::ostream& out, const MotionEvent& event) {
971 out << "MotionEvent { action=" << MotionEvent::actionToString(event.getAction());
972 if (event.getActionButton() != 0) {
973 out << ", actionButton=" << std::to_string(event.getActionButton());
974 }
975 const size_t pointerCount = event.getPointerCount();
hupeng3aa5a51a2022-09-02 16:00:18 +0800976 LOG_ALWAYS_FATAL_IF(pointerCount > MAX_POINTERS, "Too many pointers : pointerCount = %zu",
977 pointerCount);
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000978 for (size_t i = 0; i < pointerCount; i++) {
979 out << ", id[" << i << "]=" << event.getPointerId(i);
980 float x = event.getX(i);
981 float y = event.getY(i);
982 if (x != 0 || y != 0) {
983 out << ", x[" << i << "]=" << x;
984 out << ", y[" << i << "]=" << y;
985 }
986 int toolType = event.getToolType(i);
987 if (toolType != AMOTION_EVENT_TOOL_TYPE_FINGER) {
988 out << ", toolType[" << i << "]=" << toolType;
989 }
990 }
991 if (event.getButtonState() != 0) {
992 out << ", buttonState=" << event.getButtonState();
993 }
994 if (event.getClassification() != MotionClassification::NONE) {
995 out << ", classification=" << motionClassificationToString(event.getClassification());
996 }
997 if (event.getMetaState() != 0) {
998 out << ", metaState=" << event.getMetaState();
999 }
1000 if (event.getEdgeFlags() != 0) {
1001 out << ", edgeFlags=" << event.getEdgeFlags();
1002 }
1003 if (pointerCount != 1) {
1004 out << ", pointerCount=" << pointerCount;
1005 }
1006 if (event.getHistorySize() != 0) {
1007 out << ", historySize=" << event.getHistorySize();
1008 }
1009 out << ", eventTime=" << event.getEventTime();
1010 out << ", downTime=" << event.getDownTime();
1011 out << ", deviceId=" << event.getDeviceId();
1012 out << ", source=" << inputEventSourceToString(event.getSource());
1013 out << ", displayId=" << event.getDisplayId();
1014 out << ", eventId=" << event.getId();
1015 out << "}";
1016 return out;
1017}
1018
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001019// --- FocusEvent ---
1020
Antonio Kantek3cfec7b2021-11-05 18:26:17 -07001021void FocusEvent::initialize(int32_t id, bool hasFocus) {
Garfield Tan4cc839f2020-01-24 11:26:14 -08001022 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -06001023 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001024 mHasFocus = hasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001025}
1026
1027void FocusEvent::initialize(const FocusEvent& from) {
1028 InputEvent::initialize(from);
1029 mHasFocus = from.mHasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001030}
Jeff Brown5912f952013-07-01 19:10:31 -07001031
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001032// --- CaptureEvent ---
1033
1034void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) {
1035 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1036 ADISPLAY_ID_NONE, INVALID_HMAC);
1037 mPointerCaptureEnabled = pointerCaptureEnabled;
1038}
1039
1040void CaptureEvent::initialize(const CaptureEvent& from) {
1041 InputEvent::initialize(from);
1042 mPointerCaptureEnabled = from.mPointerCaptureEnabled;
1043}
1044
arthurhung7632c332020-12-30 16:58:01 +08001045// --- DragEvent ---
1046
1047void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) {
1048 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1049 ADISPLAY_ID_NONE, INVALID_HMAC);
1050 mIsExiting = isExiting;
1051 mX = x;
1052 mY = y;
1053}
1054
1055void DragEvent::initialize(const DragEvent& from) {
1056 InputEvent::initialize(from);
1057 mIsExiting = from.mIsExiting;
1058 mX = from.mX;
1059 mY = from.mY;
1060}
1061
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001062// --- TouchModeEvent ---
1063
1064void TouchModeEvent::initialize(int32_t id, bool isInTouchMode) {
1065 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1066 ADISPLAY_ID_NONE, INVALID_HMAC);
1067 mIsInTouchMode = isInTouchMode;
1068}
1069
1070void TouchModeEvent::initialize(const TouchModeEvent& from) {
1071 InputEvent::initialize(from);
1072 mIsInTouchMode = from.mIsInTouchMode;
1073}
1074
Jeff Brown5912f952013-07-01 19:10:31 -07001075// --- PooledInputEventFactory ---
1076
1077PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
1078 mMaxPoolSize(maxPoolSize) {
1079}
1080
1081PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -07001082}
1083
1084KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001085 if (mKeyEventPool.empty()) {
1086 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001087 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001088 KeyEvent* event = mKeyEventPool.front().release();
1089 mKeyEventPool.pop();
1090 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001091}
1092
1093MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001094 if (mMotionEventPool.empty()) {
1095 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001096 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001097 MotionEvent* event = mMotionEventPool.front().release();
1098 mMotionEventPool.pop();
1099 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001100}
1101
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001102FocusEvent* PooledInputEventFactory::createFocusEvent() {
1103 if (mFocusEventPool.empty()) {
1104 return new FocusEvent();
1105 }
1106 FocusEvent* event = mFocusEventPool.front().release();
1107 mFocusEventPool.pop();
1108 return event;
1109}
1110
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001111CaptureEvent* PooledInputEventFactory::createCaptureEvent() {
1112 if (mCaptureEventPool.empty()) {
1113 return new CaptureEvent();
1114 }
1115 CaptureEvent* event = mCaptureEventPool.front().release();
1116 mCaptureEventPool.pop();
1117 return event;
1118}
1119
arthurhung7632c332020-12-30 16:58:01 +08001120DragEvent* PooledInputEventFactory::createDragEvent() {
1121 if (mDragEventPool.empty()) {
1122 return new DragEvent();
1123 }
1124 DragEvent* event = mDragEventPool.front().release();
1125 mDragEventPool.pop();
1126 return event;
1127}
1128
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001129TouchModeEvent* PooledInputEventFactory::createTouchModeEvent() {
1130 if (mTouchModeEventPool.empty()) {
1131 return new TouchModeEvent();
1132 }
1133 TouchModeEvent* event = mTouchModeEventPool.front().release();
1134 mTouchModeEventPool.pop();
1135 return event;
1136}
1137
Jeff Brown5912f952013-07-01 19:10:31 -07001138void PooledInputEventFactory::recycle(InputEvent* event) {
1139 switch (event->getType()) {
1140 case AINPUT_EVENT_TYPE_KEY:
1141 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001142 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -07001143 return;
1144 }
1145 break;
1146 case AINPUT_EVENT_TYPE_MOTION:
1147 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001148 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -07001149 return;
1150 }
1151 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001152 case AINPUT_EVENT_TYPE_FOCUS:
1153 if (mFocusEventPool.size() < mMaxPoolSize) {
1154 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
1155 return;
1156 }
1157 break;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001158 case AINPUT_EVENT_TYPE_CAPTURE:
1159 if (mCaptureEventPool.size() < mMaxPoolSize) {
1160 mCaptureEventPool.push(
1161 std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event)));
1162 return;
1163 }
1164 break;
arthurhung7632c332020-12-30 16:58:01 +08001165 case AINPUT_EVENT_TYPE_DRAG:
1166 if (mDragEventPool.size() < mMaxPoolSize) {
1167 mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event)));
1168 return;
1169 }
1170 break;
Antonio Kantekeb4a30c2021-09-28 17:49:49 -07001171 case AINPUT_EVENT_TYPE_TOUCH_MODE:
1172 if (mTouchModeEventPool.size() < mMaxPoolSize) {
1173 mTouchModeEventPool.push(
1174 std::unique_ptr<TouchModeEvent>(static_cast<TouchModeEvent*>(event)));
1175 return;
1176 }
1177 break;
Jeff Brown5912f952013-07-01 19:10:31 -07001178 }
1179 delete event;
1180}
1181
1182} // namespace android