blob: c1eb8e2a1221ae670db0697441acdd2ef743d86c [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
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800241VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
242 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
243 event.getSource(), event.getDisplayId()},
244 event.getAction(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800245 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800246 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800247 event.getKeyCode(),
248 event.getScanCode(),
249 event.getMetaState(),
250 event.getRepeatCount()};
251}
252
253VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
254 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
255 event.getSource(), event.getDisplayId()},
256 event.getRawX(0),
257 event.getRawY(0),
258 event.getActionMasked(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800259 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800260 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800261 event.getMetaState(),
262 event.getButtonState()};
263}
264
Garfield Tan4cc839f2020-01-24 11:26:14 -0800265void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600266 std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800267 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700268 mDeviceId = deviceId;
269 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100270 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600271 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700272}
273
274void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800275 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700276 mDeviceId = from.mDeviceId;
277 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100278 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600279 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700280}
281
Garfield Tan4cc839f2020-01-24 11:26:14 -0800282int32_t InputEvent::nextId() {
283 static IdGenerator idGen(IdGenerator::Source::OTHER);
284 return idGen.nextId();
285}
286
Jeff Brown5912f952013-07-01 19:10:31 -0700287// --- KeyEvent ---
288
Michael Wright872db4f2014-04-22 15:03:51 -0700289const char* KeyEvent::getLabel(int32_t keyCode) {
Chris Ye4958d062020-08-20 13:21:10 -0700290 return InputEventLookup::getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700291}
292
Michael Wright872db4f2014-04-22 15:03:51 -0700293int32_t KeyEvent::getKeyCodeFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700294 return InputEventLookup::getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700295}
296
Garfield Tan4cc839f2020-01-24 11:26:14 -0800297void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600298 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
299 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
300 nsecs_t downTime, nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800301 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700302 mAction = action;
303 mFlags = flags;
304 mKeyCode = keyCode;
305 mScanCode = scanCode;
306 mMetaState = metaState;
307 mRepeatCount = repeatCount;
308 mDownTime = downTime;
309 mEventTime = eventTime;
310}
311
312void KeyEvent::initialize(const KeyEvent& from) {
313 InputEvent::initialize(from);
314 mAction = from.mAction;
315 mFlags = from.mFlags;
316 mKeyCode = from.mKeyCode;
317 mScanCode = from.mScanCode;
318 mMetaState = from.mMetaState;
319 mRepeatCount = from.mRepeatCount;
320 mDownTime = from.mDownTime;
321 mEventTime = from.mEventTime;
322}
323
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700324const char* KeyEvent::actionToString(int32_t action) {
325 // Convert KeyEvent action to string
326 switch (action) {
327 case AKEY_EVENT_ACTION_DOWN:
328 return "DOWN";
329 case AKEY_EVENT_ACTION_UP:
330 return "UP";
331 case AKEY_EVENT_ACTION_MULTIPLE:
332 return "MULTIPLE";
333 }
334 return "UNKNOWN";
335}
Jeff Brown5912f952013-07-01 19:10:31 -0700336
337// --- PointerCoords ---
338
339float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700340 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700341 return 0;
342 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700343 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700344}
345
346status_t PointerCoords::setAxisValue(int32_t axis, float value) {
347 if (axis < 0 || axis > 63) {
348 return NAME_NOT_FOUND;
349 }
350
Michael Wright38dcdff2014-03-19 12:06:10 -0700351 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
352 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700353 if (value == 0) {
354 return OK; // axes with value 0 do not need to be stored
355 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700356
357 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700358 if (count >= MAX_AXES) {
359 tooManyAxes(axis);
360 return NO_MEMORY;
361 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700362 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700363 for (uint32_t i = count; i > index; i--) {
364 values[i] = values[i - 1];
365 }
366 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700367
Jeff Brown5912f952013-07-01 19:10:31 -0700368 values[index] = value;
369 return OK;
370}
371
372static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
373 float value = c.getAxisValue(axis);
374 if (value != 0) {
375 c.setAxisValue(axis, value * scaleFactor);
376 }
377}
378
Robert Carre07e1032018-11-26 12:55:53 -0800379void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700380 // No need to scale pressure or size since they are normalized.
381 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800382
383 // If there is a global scale factor, it is included in the windowX/YScale
384 // so we don't need to apply it twice to the X/Y axes.
385 // However we don't want to apply any windowXYScale not included in the global scale
386 // to the TOUCH_MAJOR/MINOR coordinates.
387 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
388 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
389 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
390 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
391 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
392 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
Prabir Pradhanc6523582021-05-14 18:02:55 -0700393 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_X, windowXScale);
394 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_Y, windowYScale);
Robert Carre07e1032018-11-26 12:55:53 -0800395}
396
Brett Chabotfaa986c2020-11-04 17:39:36 -0800397#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700398status_t PointerCoords::readFromParcel(Parcel* parcel) {
399 bits = parcel->readInt64();
400
Michael Wright38dcdff2014-03-19 12:06:10 -0700401 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700402 if (count > MAX_AXES) {
403 return BAD_VALUE;
404 }
405
406 for (uint32_t i = 0; i < count; i++) {
407 values[i] = parcel->readFloat();
408 }
409 return OK;
410}
411
412status_t PointerCoords::writeToParcel(Parcel* parcel) const {
413 parcel->writeInt64(bits);
414
Michael Wright38dcdff2014-03-19 12:06:10 -0700415 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700416 for (uint32_t i = 0; i < count; i++) {
417 parcel->writeFloat(values[i]);
418 }
419 return OK;
420}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800421#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700422
423void PointerCoords::tooManyAxes(int axis) {
424 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
425 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
426}
427
428bool PointerCoords::operator==(const PointerCoords& other) const {
429 if (bits != other.bits) {
430 return false;
431 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700432 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700433 for (uint32_t i = 0; i < count; i++) {
434 if (values[i] != other.values[i]) {
435 return false;
436 }
437 }
438 return true;
439}
440
441void PointerCoords::copyFrom(const PointerCoords& other) {
442 bits = other.bits;
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 values[i] = other.values[i];
446 }
447}
448
chaviwc01e1372020-07-01 12:37:31 -0700449void PointerCoords::transform(const ui::Transform& transform) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700450 const vec2 xy = transform.transform(getXYValue());
451 setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
452 setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
453
Prabir Pradhanc6523582021-05-14 18:02:55 -0700454 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_X) ||
455 BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_Y)) {
456 const ui::Transform rotation(transform.getOrientation());
457 const vec2 relativeXy = rotation.transform(getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
458 getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
459 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
460 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
461 }
462
Prabir Pradhan6b384612021-05-14 16:56:25 -0700463 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) {
464 const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
465 setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val));
466 }
chaviwc01e1372020-07-01 12:37:31 -0700467}
Jeff Brown5912f952013-07-01 19:10:31 -0700468
469// --- PointerProperties ---
470
471bool PointerProperties::operator==(const PointerProperties& other) const {
472 return id == other.id
473 && toolType == other.toolType;
474}
475
476void PointerProperties::copyFrom(const PointerProperties& other) {
477 id = other.id;
478 toolType = other.toolType;
479}
480
481
482// --- MotionEvent ---
483
Garfield Tan4cc839f2020-01-24 11:26:14 -0800484void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600485 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
486 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700487 int32_t buttonState, MotionClassification classification,
488 const ui::Transform& transform, float xPrecision, float yPrecision,
Evan Rosky84f07f02021-04-16 10:42:42 -0700489 float rawXCursorPosition, float rawYCursorPosition,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700490 const ui::Transform& rawTransform, nsecs_t downTime, nsecs_t eventTime,
Evan Rosky09576692021-07-01 12:22:09 -0700491 size_t pointerCount, const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700492 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800493 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700494 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100495 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700496 mFlags = flags;
497 mEdgeFlags = edgeFlags;
498 mMetaState = metaState;
499 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800500 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700501 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700502 mXPrecision = xPrecision;
503 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700504 mRawXCursorPosition = rawXCursorPosition;
505 mRawYCursorPosition = rawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700506 mRawTransform = rawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700507 mDownTime = downTime;
508 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800509 mPointerProperties.insert(mPointerProperties.end(), &pointerProperties[0],
510 &pointerProperties[pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700511 mSampleEventTimes.clear();
512 mSamplePointerCoords.clear();
513 addSample(eventTime, pointerCoords);
514}
515
516void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800517 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
518 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700519 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100520 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700521 mFlags = other->mFlags;
522 mEdgeFlags = other->mEdgeFlags;
523 mMetaState = other->mMetaState;
524 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800525 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700526 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700527 mXPrecision = other->mXPrecision;
528 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700529 mRawXCursorPosition = other->mRawXCursorPosition;
530 mRawYCursorPosition = other->mRawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700531 mRawTransform = other->mRawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700532 mDownTime = other->mDownTime;
533 mPointerProperties = other->mPointerProperties;
534
535 if (keepHistory) {
536 mSampleEventTimes = other->mSampleEventTimes;
537 mSamplePointerCoords = other->mSamplePointerCoords;
538 } else {
539 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500540 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700541 mSamplePointerCoords.clear();
542 size_t pointerCount = other->getPointerCount();
543 size_t historySize = other->getHistorySize();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800544 mSamplePointerCoords
545 .insert(mSamplePointerCoords.end(),
546 &other->mSamplePointerCoords[historySize * pointerCount],
547 &other->mSamplePointerCoords[historySize * pointerCount + pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700548 }
549}
550
551void MotionEvent::addSample(
552 int64_t eventTime,
553 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500554 mSampleEventTimes.push_back(eventTime);
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800555 mSamplePointerCoords.insert(mSamplePointerCoords.end(), &pointerCoords[0],
556 &pointerCoords[getPointerCount()]);
Jeff Brown5912f952013-07-01 19:10:31 -0700557}
558
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800559int MotionEvent::getSurfaceRotation() const {
560 // The surface rotation is the rotation from the window's coordinate space to that of the
561 // display. Since the event's transform takes display space coordinates to window space, the
562 // returned surface rotation is the inverse of the rotation for the surface.
563 switch (mTransform.getOrientation()) {
564 case ui::Transform::ROT_0:
565 return DISPLAY_ORIENTATION_0;
566 case ui::Transform::ROT_90:
567 return DISPLAY_ORIENTATION_270;
568 case ui::Transform::ROT_180:
569 return DISPLAY_ORIENTATION_180;
570 case ui::Transform::ROT_270:
571 return DISPLAY_ORIENTATION_90;
572 default:
573 return -1;
574 }
575}
576
Garfield Tan00f511d2019-06-12 16:55:40 -0700577float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700578 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
579 return vals.x;
Garfield Tan00f511d2019-06-12 16:55:40 -0700580}
581
582float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700583 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
584 return vals.y;
Garfield Tan00f511d2019-06-12 16:55:40 -0700585}
586
Garfield Tan937bb832019-07-25 17:48:31 -0700587void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700588 ui::Transform inverse = mTransform.inverse();
589 vec2 vals = inverse.transform(x, y);
590 mRawXCursorPosition = vals.x;
591 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700592}
593
Jeff Brown5912f952013-07-01 19:10:31 -0700594const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000595 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
596 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
597 }
598 const size_t position = getHistorySize() * getPointerCount() + pointerIndex;
599 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
600 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
601 }
602 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700603}
604
605float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
Evan Rosky84f07f02021-04-16 10:42:42 -0700606 return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700607}
608
609float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700610 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700611}
612
613const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
614 size_t pointerIndex, size_t historicalIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000615 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
616 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
617 }
618 if (CC_UNLIKELY(historicalIndex < 0 || historicalIndex > getHistorySize())) {
619 LOG(FATAL) << __func__ << ": Invalid historical index " << historicalIndex << " for "
620 << *this;
621 }
622 const size_t position = historicalIndex * getPointerCount() + pointerIndex;
623 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
624 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
625 }
626 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700627}
628
629float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan6b384612021-05-14 16:56:25 -0700630 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700631 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
632 return calculateTransformedAxisValue(axis, mSource, mRawTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700633}
634
635float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan9f388812021-05-13 16:54:53 -0700636 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700637 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
638 return calculateTransformedAxisValue(axis, mSource, mTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700639}
640
641ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
642 size_t pointerCount = mPointerProperties.size();
643 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800644 if (mPointerProperties[i].id == pointerId) {
Jeff Brown5912f952013-07-01 19:10:31 -0700645 return i;
646 }
647 }
648 return -1;
649}
650
651void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700652 float currXOffset = mTransform.tx();
653 float currYOffset = mTransform.ty();
654 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700655}
656
Robert Carre07e1032018-11-26 12:55:53 -0800657void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700658 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700659 mRawTransform.set(mRawTransform.tx() * globalScaleFactor,
660 mRawTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800661 mXPrecision *= globalScaleFactor;
662 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700663
664 size_t numSamples = mSamplePointerCoords.size();
665 for (size_t i = 0; i < numSamples; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800666 mSamplePointerCoords[i].scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700667 }
668}
669
chaviw9eaa22c2020-07-01 16:21:27 -0700670void MotionEvent::transform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700671 // We want to preserve the raw axes values stored in the PointerCoords, so we just update the
672 // transform using the values passed in.
chaviw9eaa22c2020-07-01 16:21:27 -0700673 ui::Transform newTransform;
674 newTransform.set(matrix);
675 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700676}
677
Evan Roskyd4d4d802021-05-03 20:12:21 -0700678void MotionEvent::applyTransform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700679 ui::Transform transform;
680 transform.set(matrix);
Evan Roskyd4d4d802021-05-03 20:12:21 -0700681
682 // Apply the transformation to all samples.
Prabir Pradhan6b384612021-05-14 16:56:25 -0700683 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
684 [&transform](PointerCoords& c) { c.transform(transform); });
Prabir Pradhan4b19bd02021-06-01 17:34:59 -0700685
686 if (mRawXCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION &&
687 mRawYCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) {
688 const vec2 cursor = transform.transform(mRawXCursorPosition, mRawYCursorPosition);
689 mRawXCursorPosition = cursor.x;
690 mRawYCursorPosition = cursor.y;
691 }
Evan Roskyd4d4d802021-05-03 20:12:21 -0700692}
693
Brett Chabotfaa986c2020-11-04 17:39:36 -0800694#ifdef __linux__
chaviw9eaa22c2020-07-01 16:21:27 -0700695static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
696 float dsdx, dtdx, tx, dtdy, dsdy, ty;
697 status_t status = parcel.readFloat(&dsdx);
698 status |= parcel.readFloat(&dtdx);
699 status |= parcel.readFloat(&tx);
700 status |= parcel.readFloat(&dtdy);
701 status |= parcel.readFloat(&dsdy);
702 status |= parcel.readFloat(&ty);
703
704 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
705 return status;
706}
707
708static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
709 status_t status = parcel.writeFloat(transform.dsdx());
710 status |= parcel.writeFloat(transform.dtdx());
711 status |= parcel.writeFloat(transform.tx());
712 status |= parcel.writeFloat(transform.dtdy());
713 status |= parcel.writeFloat(transform.dsdy());
714 status |= parcel.writeFloat(transform.ty());
715 return status;
716}
717
Jeff Brown5912f952013-07-01 19:10:31 -0700718status_t MotionEvent::readFromParcel(Parcel* parcel) {
719 size_t pointerCount = parcel->readInt32();
720 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800721 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
722 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700723 return BAD_VALUE;
724 }
725
Garfield Tan4cc839f2020-01-24 11:26:14 -0800726 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700727 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600728 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800729 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600730 std::vector<uint8_t> hmac;
731 status_t result = parcel->readByteVector(&hmac);
732 if (result != OK || hmac.size() != 32) {
733 return BAD_VALUE;
734 }
735 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700736 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100737 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700738 mFlags = parcel->readInt32();
739 mEdgeFlags = parcel->readInt32();
740 mMetaState = parcel->readInt32();
741 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800742 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700743
744 result = android::readFromParcel(mTransform, *parcel);
745 if (result != OK) {
746 return result;
747 }
Jeff Brown5912f952013-07-01 19:10:31 -0700748 mXPrecision = parcel->readFloat();
749 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700750 mRawXCursorPosition = parcel->readFloat();
751 mRawYCursorPosition = parcel->readFloat();
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700752
753 result = android::readFromParcel(mRawTransform, *parcel);
754 if (result != OK) {
755 return result;
756 }
Jeff Brown5912f952013-07-01 19:10:31 -0700757 mDownTime = parcel->readInt64();
758
759 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800760 mPointerProperties.reserve(pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700761 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500762 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700763 mSamplePointerCoords.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800764 mSamplePointerCoords.reserve(sampleCount * pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700765
766 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800767 mPointerProperties.push_back({});
768 PointerProperties& properties = mPointerProperties.back();
Jeff Brown5912f952013-07-01 19:10:31 -0700769 properties.id = parcel->readInt32();
770 properties.toolType = parcel->readInt32();
771 }
772
Dan Austinc94fc452015-09-22 14:22:41 -0700773 while (sampleCount > 0) {
774 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500775 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700776 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800777 mSamplePointerCoords.push_back({});
778 status_t status = mSamplePointerCoords.back().readFromParcel(parcel);
Jeff Brown5912f952013-07-01 19:10:31 -0700779 if (status) {
780 return status;
781 }
782 }
783 }
784 return OK;
785}
786
787status_t MotionEvent::writeToParcel(Parcel* parcel) const {
788 size_t pointerCount = mPointerProperties.size();
789 size_t sampleCount = mSampleEventTimes.size();
790
791 parcel->writeInt32(pointerCount);
792 parcel->writeInt32(sampleCount);
793
Garfield Tan4cc839f2020-01-24 11:26:14 -0800794 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700795 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600796 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800797 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600798 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
799 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700800 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100801 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700802 parcel->writeInt32(mFlags);
803 parcel->writeInt32(mEdgeFlags);
804 parcel->writeInt32(mMetaState);
805 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800806 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700807
808 status_t result = android::writeToParcel(mTransform, *parcel);
809 if (result != OK) {
810 return result;
811 }
Jeff Brown5912f952013-07-01 19:10:31 -0700812 parcel->writeFloat(mXPrecision);
813 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700814 parcel->writeFloat(mRawXCursorPosition);
815 parcel->writeFloat(mRawYCursorPosition);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700816
817 result = android::writeToParcel(mRawTransform, *parcel);
818 if (result != OK) {
819 return result;
820 }
Jeff Brown5912f952013-07-01 19:10:31 -0700821 parcel->writeInt64(mDownTime);
822
823 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800824 const PointerProperties& properties = mPointerProperties[i];
Jeff Brown5912f952013-07-01 19:10:31 -0700825 parcel->writeInt32(properties.id);
826 parcel->writeInt32(properties.toolType);
827 }
828
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800829 const PointerCoords* pc = mSamplePointerCoords.data();
Jeff Brown5912f952013-07-01 19:10:31 -0700830 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500831 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700832 for (size_t i = 0; i < pointerCount; i++) {
833 status_t status = (pc++)->writeToParcel(parcel);
834 if (status) {
835 return status;
836 }
837 }
838 }
839 return OK;
840}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800841#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700842
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600843bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700844 if (isFromSource(source, AINPUT_SOURCE_CLASS_POINTER)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700845 // Specifically excludes HOVER_MOVE and SCROLL.
846 switch (action & AMOTION_EVENT_ACTION_MASK) {
847 case AMOTION_EVENT_ACTION_DOWN:
848 case AMOTION_EVENT_ACTION_MOVE:
849 case AMOTION_EVENT_ACTION_UP:
850 case AMOTION_EVENT_ACTION_POINTER_DOWN:
851 case AMOTION_EVENT_ACTION_POINTER_UP:
852 case AMOTION_EVENT_ACTION_CANCEL:
853 case AMOTION_EVENT_ACTION_OUTSIDE:
854 return true;
855 }
856 }
857 return false;
858}
859
Michael Wright872db4f2014-04-22 15:03:51 -0700860const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700861 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700862}
863
864int32_t MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700865 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700866}
867
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500868std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700869 // Convert MotionEvent action to string
870 switch (action & AMOTION_EVENT_ACTION_MASK) {
871 case AMOTION_EVENT_ACTION_DOWN:
872 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700873 case AMOTION_EVENT_ACTION_UP:
874 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500875 case AMOTION_EVENT_ACTION_MOVE:
876 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700877 case AMOTION_EVENT_ACTION_CANCEL:
878 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500879 case AMOTION_EVENT_ACTION_OUTSIDE:
880 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700881 case AMOTION_EVENT_ACTION_POINTER_DOWN:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000882 return StringPrintf("POINTER_DOWN(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700883 case AMOTION_EVENT_ACTION_POINTER_UP:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000884 return StringPrintf("POINTER_UP(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500885 case AMOTION_EVENT_ACTION_HOVER_MOVE:
886 return "HOVER_MOVE";
887 case AMOTION_EVENT_ACTION_SCROLL:
888 return "SCROLL";
889 case AMOTION_EVENT_ACTION_HOVER_ENTER:
890 return "HOVER_ENTER";
891 case AMOTION_EVENT_ACTION_HOVER_EXIT:
892 return "HOVER_EXIT";
893 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
894 return "BUTTON_PRESS";
895 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
896 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700897 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500898 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700899}
900
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700901// Apply the given transformation to the point without checking whether the entire transform
902// should be disregarded altogether for the provided source.
903static inline vec2 calculateTransformedXYUnchecked(uint32_t source, const ui::Transform& transform,
904 const vec2& xy) {
905 return shouldDisregardOffset(source) ? transformWithoutTranslation(transform, xy)
906 : transform.transform(xy);
907}
908
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700909vec2 MotionEvent::calculateTransformedXY(uint32_t source, const ui::Transform& transform,
910 const vec2& xy) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700911 if (shouldDisregardTransformation(source)) {
912 return xy;
913 }
914 return calculateTransformedXYUnchecked(source, transform, xy);
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700915}
916
Prabir Pradhan8e6ce222022-02-24 09:08:54 -0800917// Keep in sync with calculateTransformedCoords.
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700918float MotionEvent::calculateTransformedAxisValue(int32_t axis, uint32_t source,
919 const ui::Transform& transform,
920 const PointerCoords& coords) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700921 if (shouldDisregardTransformation(source)) {
922 return coords.getAxisValue(axis);
923 }
924
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700925 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700926 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700927 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
928 return xy[axis];
929 }
930
931 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
932 const vec2 relativeXy =
933 transformWithoutTranslation(transform,
934 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
935 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
936 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
937 }
938
939 if (axis == AMOTION_EVENT_AXIS_ORIENTATION) {
940 return transformAngle(transform, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION));
941 }
942
943 return coords.getAxisValue(axis);
944}
945
Prabir Pradhan8e6ce222022-02-24 09:08:54 -0800946// Keep in sync with calculateTransformedAxisValue. This is an optimization of
947// calculateTransformedAxisValue for all PointerCoords axes.
948PointerCoords MotionEvent::calculateTransformedCoords(uint32_t source,
949 const ui::Transform& transform,
950 const PointerCoords& coords) {
951 if (shouldDisregardTransformation(source)) {
952 return coords;
953 }
954 PointerCoords out = coords;
955
956 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
957 out.setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
958 out.setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
959
960 const vec2 relativeXy =
961 transformWithoutTranslation(transform,
962 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
963 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
964 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
965 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
966
967 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
968 transformAngle(transform,
969 coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)));
970
971 return out;
972}
973
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000974std::ostream& operator<<(std::ostream& out, const MotionEvent& event) {
975 out << "MotionEvent { action=" << MotionEvent::actionToString(event.getAction());
976 if (event.getActionButton() != 0) {
977 out << ", actionButton=" << std::to_string(event.getActionButton());
978 }
979 const size_t pointerCount = event.getPointerCount();
hupeng3aa5a51a2022-09-02 16:00:18 +0800980 LOG_ALWAYS_FATAL_IF(pointerCount > MAX_POINTERS, "Too many pointers : pointerCount = %zu",
981 pointerCount);
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000982 for (size_t i = 0; i < pointerCount; i++) {
983 out << ", id[" << i << "]=" << event.getPointerId(i);
984 float x = event.getX(i);
985 float y = event.getY(i);
986 if (x != 0 || y != 0) {
987 out << ", x[" << i << "]=" << x;
988 out << ", y[" << i << "]=" << y;
989 }
990 int toolType = event.getToolType(i);
991 if (toolType != AMOTION_EVENT_TOOL_TYPE_FINGER) {
992 out << ", toolType[" << i << "]=" << toolType;
993 }
994 }
995 if (event.getButtonState() != 0) {
996 out << ", buttonState=" << event.getButtonState();
997 }
998 if (event.getClassification() != MotionClassification::NONE) {
999 out << ", classification=" << motionClassificationToString(event.getClassification());
1000 }
1001 if (event.getMetaState() != 0) {
1002 out << ", metaState=" << event.getMetaState();
1003 }
1004 if (event.getEdgeFlags() != 0) {
1005 out << ", edgeFlags=" << event.getEdgeFlags();
1006 }
1007 if (pointerCount != 1) {
1008 out << ", pointerCount=" << pointerCount;
1009 }
1010 if (event.getHistorySize() != 0) {
1011 out << ", historySize=" << event.getHistorySize();
1012 }
1013 out << ", eventTime=" << event.getEventTime();
1014 out << ", downTime=" << event.getDownTime();
1015 out << ", deviceId=" << event.getDeviceId();
1016 out << ", source=" << inputEventSourceToString(event.getSource());
1017 out << ", displayId=" << event.getDisplayId();
1018 out << ", eventId=" << event.getId();
1019 out << "}";
1020 return out;
1021}
1022
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001023// --- FocusEvent ---
1024
Antonio Kantek3cfec7b2021-11-05 18:26:17 -07001025void FocusEvent::initialize(int32_t id, bool hasFocus) {
Garfield Tan4cc839f2020-01-24 11:26:14 -08001026 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -06001027 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001028 mHasFocus = hasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001029}
1030
1031void FocusEvent::initialize(const FocusEvent& from) {
1032 InputEvent::initialize(from);
1033 mHasFocus = from.mHasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001034}
Jeff Brown5912f952013-07-01 19:10:31 -07001035
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001036// --- CaptureEvent ---
1037
1038void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) {
1039 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1040 ADISPLAY_ID_NONE, INVALID_HMAC);
1041 mPointerCaptureEnabled = pointerCaptureEnabled;
1042}
1043
1044void CaptureEvent::initialize(const CaptureEvent& from) {
1045 InputEvent::initialize(from);
1046 mPointerCaptureEnabled = from.mPointerCaptureEnabled;
1047}
1048
arthurhung7632c332020-12-30 16:58:01 +08001049// --- DragEvent ---
1050
1051void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) {
1052 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1053 ADISPLAY_ID_NONE, INVALID_HMAC);
1054 mIsExiting = isExiting;
1055 mX = x;
1056 mY = y;
1057}
1058
1059void DragEvent::initialize(const DragEvent& from) {
1060 InputEvent::initialize(from);
1061 mIsExiting = from.mIsExiting;
1062 mX = from.mX;
1063 mY = from.mY;
1064}
1065
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001066// --- TouchModeEvent ---
1067
1068void TouchModeEvent::initialize(int32_t id, bool isInTouchMode) {
1069 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1070 ADISPLAY_ID_NONE, INVALID_HMAC);
1071 mIsInTouchMode = isInTouchMode;
1072}
1073
1074void TouchModeEvent::initialize(const TouchModeEvent& from) {
1075 InputEvent::initialize(from);
1076 mIsInTouchMode = from.mIsInTouchMode;
1077}
1078
Jeff Brown5912f952013-07-01 19:10:31 -07001079// --- PooledInputEventFactory ---
1080
1081PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
1082 mMaxPoolSize(maxPoolSize) {
1083}
1084
1085PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -07001086}
1087
1088KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001089 if (mKeyEventPool.empty()) {
1090 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001091 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001092 KeyEvent* event = mKeyEventPool.front().release();
1093 mKeyEventPool.pop();
1094 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001095}
1096
1097MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001098 if (mMotionEventPool.empty()) {
1099 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001100 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001101 MotionEvent* event = mMotionEventPool.front().release();
1102 mMotionEventPool.pop();
1103 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001104}
1105
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001106FocusEvent* PooledInputEventFactory::createFocusEvent() {
1107 if (mFocusEventPool.empty()) {
1108 return new FocusEvent();
1109 }
1110 FocusEvent* event = mFocusEventPool.front().release();
1111 mFocusEventPool.pop();
1112 return event;
1113}
1114
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001115CaptureEvent* PooledInputEventFactory::createCaptureEvent() {
1116 if (mCaptureEventPool.empty()) {
1117 return new CaptureEvent();
1118 }
1119 CaptureEvent* event = mCaptureEventPool.front().release();
1120 mCaptureEventPool.pop();
1121 return event;
1122}
1123
arthurhung7632c332020-12-30 16:58:01 +08001124DragEvent* PooledInputEventFactory::createDragEvent() {
1125 if (mDragEventPool.empty()) {
1126 return new DragEvent();
1127 }
1128 DragEvent* event = mDragEventPool.front().release();
1129 mDragEventPool.pop();
1130 return event;
1131}
1132
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001133TouchModeEvent* PooledInputEventFactory::createTouchModeEvent() {
1134 if (mTouchModeEventPool.empty()) {
1135 return new TouchModeEvent();
1136 }
1137 TouchModeEvent* event = mTouchModeEventPool.front().release();
1138 mTouchModeEventPool.pop();
1139 return event;
1140}
1141
Jeff Brown5912f952013-07-01 19:10:31 -07001142void PooledInputEventFactory::recycle(InputEvent* event) {
1143 switch (event->getType()) {
1144 case AINPUT_EVENT_TYPE_KEY:
1145 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001146 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -07001147 return;
1148 }
1149 break;
1150 case AINPUT_EVENT_TYPE_MOTION:
1151 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001152 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -07001153 return;
1154 }
1155 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001156 case AINPUT_EVENT_TYPE_FOCUS:
1157 if (mFocusEventPool.size() < mMaxPoolSize) {
1158 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
1159 return;
1160 }
1161 break;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001162 case AINPUT_EVENT_TYPE_CAPTURE:
1163 if (mCaptureEventPool.size() < mMaxPoolSize) {
1164 mCaptureEventPool.push(
1165 std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event)));
1166 return;
1167 }
1168 break;
arthurhung7632c332020-12-30 16:58:01 +08001169 case AINPUT_EVENT_TYPE_DRAG:
1170 if (mDragEventPool.size() < mMaxPoolSize) {
1171 mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event)));
1172 return;
1173 }
1174 break;
Antonio Kantekeb4a30c2021-09-28 17:49:49 -07001175 case AINPUT_EVENT_TYPE_TOUCH_MODE:
1176 if (mTouchModeEventPool.size() < mMaxPoolSize) {
1177 mTouchModeEventPool.push(
1178 std::unique_ptr<TouchModeEvent>(static_cast<TouchModeEvent*>(event)));
1179 return;
1180 }
1181 break;
Jeff Brown5912f952013-07-01 19:10:31 -07001182 }
1183 delete event;
1184}
1185
1186} // namespace android