blob: d893cb99bae184f05dc55965387842db6fd28242 [file] [log] [blame]
Jeff Brown5912f952013-07-01 19:10:31 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Input"
18//#define LOG_NDEBUG 0
19
chaviw09c8d2d2020-08-24 15:48:26 -070020#include <attestation/HmacKeyManager.h>
Garfield Tan84b087e2020-01-23 10:49:05 -080021#include <cutils/compiler.h>
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050022#include <inttypes.h>
Garfield Tan84b087e2020-01-23 10:49:05 -080023#include <string.h>
Michael Wright635422b2022-12-02 00:43:56 +000024#include <optional>
Jeff Brown5912f952013-07-01 19:10:31 -070025
Siarhei Vishniakou31977182022-09-30 08:51:23 -070026#include <android-base/file.h>
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +000027#include <android-base/logging.h>
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050028#include <android-base/stringprintf.h>
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +000029#include <cutils/compiler.h>
chaviw98318de2021-05-19 16:45:23 -050030#include <gui/constants.h>
Prabir Pradhan092f3a92021-11-25 10:53:27 -080031#include <input/DisplayViewport.h>
Jeff Brown5912f952013-07-01 19:10:31 -070032#include <input/Input.h>
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080033#include <input/InputDevice.h>
Michael Wright872db4f2014-04-22 15:03:51 -070034#include <input/InputEventLabels.h>
Jeff Brown5912f952013-07-01 19:10:31 -070035
Brett Chabotfaa986c2020-11-04 17:39:36 -080036#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -070037#include <binder/Parcel.h>
Brett Chabotfaa986c2020-11-04 17:39:36 -080038#endif
Siarhei Vishniakou63740b92022-10-20 10:28:08 -070039#if defined(__ANDROID__)
40#include <sys/random.h>
41#endif
Jeff Brown5912f952013-07-01 19:10:31 -070042
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050043using android::base::StringPrintf;
44
Jeff Brown5912f952013-07-01 19:10:31 -070045namespace android {
46
Prabir Pradhan6b384612021-05-14 16:56:25 -070047namespace {
48
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070049bool shouldDisregardTransformation(uint32_t source) {
Prabir Pradhan258e2b92022-06-24 18:37:04 +000050 // Do not apply any transformations to axes from joysticks, touchpads, or relative mice.
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070051 return isFromSource(source, AINPUT_SOURCE_CLASS_JOYSTICK) ||
Prabir Pradhan258e2b92022-06-24 18:37:04 +000052 isFromSource(source, AINPUT_SOURCE_CLASS_POSITION) ||
53 isFromSource(source, AINPUT_SOURCE_MOUSE_RELATIVE);
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070054}
55
56bool shouldDisregardOffset(uint32_t source) {
Prabir Pradhan9f388812021-05-13 16:54:53 -070057 // Pointer events are the only type of events that refer to absolute coordinates on the display,
58 // so we should apply the entire window transform. For other types of events, we should make
59 // sure to not apply the window translation/offset.
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070060 return !isFromSource(source, AINPUT_SOURCE_CLASS_POINTER);
Prabir Pradhan9f388812021-05-13 16:54:53 -070061}
62
Prabir Pradhan6b384612021-05-14 16:56:25 -070063} // namespace
64
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -080065const char* motionClassificationToString(MotionClassification classification) {
66 switch (classification) {
67 case MotionClassification::NONE:
68 return "NONE";
69 case MotionClassification::AMBIGUOUS_GESTURE:
70 return "AMBIGUOUS_GESTURE";
71 case MotionClassification::DEEP_PRESS:
72 return "DEEP_PRESS";
Harry Cutts2800fb02022-09-15 13:49:23 +000073 case MotionClassification::TWO_FINGER_SWIPE:
74 return "TWO_FINGER_SWIPE";
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -080075 }
76}
77
Siarhei Vishniakoud5fe5182022-07-20 23:28:40 +000078const char* motionToolTypeToString(int32_t toolType) {
79 switch (toolType) {
80 case AMOTION_EVENT_TOOL_TYPE_UNKNOWN:
81 return "UNKNOWN";
82 case AMOTION_EVENT_TOOL_TYPE_FINGER:
83 return "FINGER";
84 case AMOTION_EVENT_TOOL_TYPE_STYLUS:
85 return "STYLUS";
86 case AMOTION_EVENT_TOOL_TYPE_MOUSE:
87 return "MOUSE";
88 case AMOTION_EVENT_TOOL_TYPE_ERASER:
89 return "ERASER";
90 case AMOTION_EVENT_TOOL_TYPE_PALM:
91 return "PALM";
92 default:
93 return "INVALID";
94 }
95}
96
Garfield Tan84b087e2020-01-23 10:49:05 -080097// --- IdGenerator ---
Siarhei Vishniakou63740b92022-10-20 10:28:08 -070098#if defined(__ANDROID__)
99[[maybe_unused]]
100#endif
101static status_t
102getRandomBytes(uint8_t* data, size_t size) {
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700103 int ret = TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY | O_CLOEXEC | O_NOFOLLOW));
104 if (ret == -1) {
105 return -errno;
106 }
107
108 base::unique_fd fd(ret);
109 if (!base::ReadFully(fd, data, size)) {
110 return -errno;
111 }
112 return OK;
113}
114
Garfield Tan84b087e2020-01-23 10:49:05 -0800115IdGenerator::IdGenerator(Source source) : mSource(source) {}
116
117int32_t IdGenerator::nextId() const {
118 constexpr uint32_t SEQUENCE_NUMBER_MASK = ~SOURCE_MASK;
119 int32_t id = 0;
120
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700121#if defined(__ANDROID__)
122 // On device, prefer 'getrandom' to '/dev/urandom' because it's faster.
123 constexpr size_t BUF_LEN = sizeof(id);
124 size_t totalBytes = 0;
125 while (totalBytes < BUF_LEN) {
126 ssize_t bytes = TEMP_FAILURE_RETRY(getrandom(&id, BUF_LEN, GRND_NONBLOCK));
127 if (CC_UNLIKELY(bytes < 0)) {
128 ALOGW("Failed to fill in random number for sequence number: %s.", strerror(errno));
129 id = 0;
130 break;
131 }
132 totalBytes += bytes;
133 }
134#else
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700135#if defined(__linux__)
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700136 // On host, <sys/random.h> / GRND_NONBLOCK is not available
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700137 while (true) {
138 status_t result = getRandomBytes(reinterpret_cast<uint8_t*>(&id), sizeof(id));
139 if (result == OK) {
Garfield Tan84b087e2020-01-23 10:49:05 -0800140 break;
141 }
Garfield Tan84b087e2020-01-23 10:49:05 -0800142 }
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700143#endif // __linux__
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700144#endif // __ANDROID__
Garfield Tan84b087e2020-01-23 10:49:05 -0800145 return (id & SEQUENCE_NUMBER_MASK) | static_cast<int32_t>(mSource);
146}
147
Jeff Brown5912f952013-07-01 19:10:31 -0700148// --- InputEvent ---
149
Prabir Pradhande69f8a2021-11-18 16:40:34 +0000150vec2 transformWithoutTranslation(const ui::Transform& transform, const vec2& xy) {
151 const vec2 transformedXy = transform.transform(xy);
152 const vec2 transformedOrigin = transform.transform(0, 0);
153 return transformedXy - transformedOrigin;
154}
155
Prabir Pradhane2e10b42022-11-17 20:59:36 +0000156float transformAngle(const ui::Transform& transform, float angleRadians) {
157 // Construct and transform a vector oriented at the specified clockwise angle from vertical.
158 // Coordinate system: down is increasing Y, right is increasing X.
159 float x = sinf(angleRadians);
160 float y = -cosf(angleRadians);
161 vec2 transformedPoint = transform.transform(x, y);
162
163 // Determine how the origin is transformed by the matrix so that we
164 // can transform orientation vectors.
165 const vec2 origin = transform.transform(0, 0);
166
167 transformedPoint.x -= origin.x;
168 transformedPoint.y -= origin.y;
169
170 // Derive the transformed vector's clockwise angle from vertical.
171 // The return value of atan2f is in range [-pi, pi] which conforms to the orientation API.
172 return atan2f(transformedPoint.x, -transformedPoint.y);
173}
174
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800175const char* inputEventTypeToString(int32_t type) {
176 switch (type) {
177 case AINPUT_EVENT_TYPE_KEY: {
178 return "KEY";
179 }
180 case AINPUT_EVENT_TYPE_MOTION: {
181 return "MOTION";
182 }
183 case AINPUT_EVENT_TYPE_FOCUS: {
184 return "FOCUS";
185 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800186 case AINPUT_EVENT_TYPE_CAPTURE: {
187 return "CAPTURE";
188 }
arthurhung7632c332020-12-30 16:58:01 +0800189 case AINPUT_EVENT_TYPE_DRAG: {
190 return "DRAG";
191 }
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700192 case AINPUT_EVENT_TYPE_TOUCH_MODE: {
193 return "TOUCH_MODE";
194 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800195 }
196 return "UNKNOWN";
197}
198
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800199std::string inputEventSourceToString(int32_t source) {
200 if (source == AINPUT_SOURCE_UNKNOWN) {
201 return "UNKNOWN";
202 }
203 if (source == static_cast<int32_t>(AINPUT_SOURCE_ANY)) {
204 return "ANY";
205 }
206 static const std::map<int32_t, const char*> SOURCES{
207 {AINPUT_SOURCE_KEYBOARD, "KEYBOARD"},
208 {AINPUT_SOURCE_DPAD, "DPAD"},
209 {AINPUT_SOURCE_GAMEPAD, "GAMEPAD"},
210 {AINPUT_SOURCE_TOUCHSCREEN, "TOUCHSCREEN"},
211 {AINPUT_SOURCE_MOUSE, "MOUSE"},
212 {AINPUT_SOURCE_STYLUS, "STYLUS"},
213 {AINPUT_SOURCE_BLUETOOTH_STYLUS, "BLUETOOTH_STYLUS"},
214 {AINPUT_SOURCE_TRACKBALL, "TRACKBALL"},
215 {AINPUT_SOURCE_MOUSE_RELATIVE, "MOUSE_RELATIVE"},
216 {AINPUT_SOURCE_TOUCHPAD, "TOUCHPAD"},
217 {AINPUT_SOURCE_TOUCH_NAVIGATION, "TOUCH_NAVIGATION"},
218 {AINPUT_SOURCE_JOYSTICK, "JOYSTICK"},
219 {AINPUT_SOURCE_HDMI, "HDMI"},
220 {AINPUT_SOURCE_SENSOR, "SENSOR"},
221 {AINPUT_SOURCE_ROTARY_ENCODER, "ROTARY_ENCODER"},
222 };
223 std::string result;
224 for (const auto& [source_entry, str] : SOURCES) {
225 if ((source & source_entry) == source_entry) {
226 if (!result.empty()) {
227 result += " | ";
228 }
229 result += str;
230 }
231 }
232 if (result.empty()) {
233 result = StringPrintf("0x%08x", source);
234 }
235 return result;
236}
237
238bool isFromSource(uint32_t source, uint32_t test) {
239 return (source & test) == test;
240}
241
Prabir Pradhane5626962022-10-27 20:30:53 +0000242bool isStylusToolType(uint32_t toolType) {
243 return toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS || toolType == AMOTION_EVENT_TOOL_TYPE_ERASER;
244}
245
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800246VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
247 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
248 event.getSource(), event.getDisplayId()},
249 event.getAction(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800250 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800251 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800252 event.getKeyCode(),
253 event.getScanCode(),
254 event.getMetaState(),
255 event.getRepeatCount()};
256}
257
258VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
259 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
260 event.getSource(), event.getDisplayId()},
261 event.getRawX(0),
262 event.getRawY(0),
263 event.getActionMasked(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800264 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800265 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800266 event.getMetaState(),
267 event.getButtonState()};
268}
269
Garfield Tan4cc839f2020-01-24 11:26:14 -0800270void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600271 std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800272 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700273 mDeviceId = deviceId;
274 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100275 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600276 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700277}
278
279void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800280 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700281 mDeviceId = from.mDeviceId;
282 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100283 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600284 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700285}
286
Garfield Tan4cc839f2020-01-24 11:26:14 -0800287int32_t InputEvent::nextId() {
288 static IdGenerator idGen(IdGenerator::Source::OTHER);
289 return idGen.nextId();
290}
291
Jeff Brown5912f952013-07-01 19:10:31 -0700292// --- KeyEvent ---
293
Michael Wright872db4f2014-04-22 15:03:51 -0700294const char* KeyEvent::getLabel(int32_t keyCode) {
Chris Ye4958d062020-08-20 13:21:10 -0700295 return InputEventLookup::getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700296}
297
Michael Wright872db4f2014-04-22 15:03:51 -0700298int32_t KeyEvent::getKeyCodeFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700299 return InputEventLookup::getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700300}
301
Garfield Tan4cc839f2020-01-24 11:26:14 -0800302void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600303 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
304 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
305 nsecs_t downTime, nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800306 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700307 mAction = action;
308 mFlags = flags;
309 mKeyCode = keyCode;
310 mScanCode = scanCode;
311 mMetaState = metaState;
312 mRepeatCount = repeatCount;
313 mDownTime = downTime;
314 mEventTime = eventTime;
315}
316
317void KeyEvent::initialize(const KeyEvent& from) {
318 InputEvent::initialize(from);
319 mAction = from.mAction;
320 mFlags = from.mFlags;
321 mKeyCode = from.mKeyCode;
322 mScanCode = from.mScanCode;
323 mMetaState = from.mMetaState;
324 mRepeatCount = from.mRepeatCount;
325 mDownTime = from.mDownTime;
326 mEventTime = from.mEventTime;
327}
328
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700329const char* KeyEvent::actionToString(int32_t action) {
330 // Convert KeyEvent action to string
331 switch (action) {
332 case AKEY_EVENT_ACTION_DOWN:
333 return "DOWN";
334 case AKEY_EVENT_ACTION_UP:
335 return "UP";
336 case AKEY_EVENT_ACTION_MULTIPLE:
337 return "MULTIPLE";
338 }
339 return "UNKNOWN";
340}
Jeff Brown5912f952013-07-01 19:10:31 -0700341
342// --- PointerCoords ---
343
344float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700345 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700346 return 0;
347 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700348 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700349}
350
351status_t PointerCoords::setAxisValue(int32_t axis, float value) {
352 if (axis < 0 || axis > 63) {
353 return NAME_NOT_FOUND;
354 }
355
Michael Wright38dcdff2014-03-19 12:06:10 -0700356 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
357 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700358 if (value == 0) {
359 return OK; // axes with value 0 do not need to be stored
360 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700361
362 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700363 if (count >= MAX_AXES) {
364 tooManyAxes(axis);
365 return NO_MEMORY;
366 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700367 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700368 for (uint32_t i = count; i > index; i--) {
369 values[i] = values[i - 1];
370 }
371 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700372
Jeff Brown5912f952013-07-01 19:10:31 -0700373 values[index] = value;
374 return OK;
375}
376
377static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
378 float value = c.getAxisValue(axis);
379 if (value != 0) {
380 c.setAxisValue(axis, value * scaleFactor);
381 }
382}
383
Robert Carre07e1032018-11-26 12:55:53 -0800384void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700385 // No need to scale pressure or size since they are normalized.
386 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800387
388 // If there is a global scale factor, it is included in the windowX/YScale
389 // so we don't need to apply it twice to the X/Y axes.
390 // However we don't want to apply any windowXYScale not included in the global scale
391 // to the TOUCH_MAJOR/MINOR coordinates.
392 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
393 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
394 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
395 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
396 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
397 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
Prabir Pradhanc6523582021-05-14 18:02:55 -0700398 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_X, windowXScale);
399 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_Y, windowYScale);
Robert Carre07e1032018-11-26 12:55:53 -0800400}
401
Brett Chabotfaa986c2020-11-04 17:39:36 -0800402#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700403status_t PointerCoords::readFromParcel(Parcel* parcel) {
404 bits = parcel->readInt64();
405
Michael Wright38dcdff2014-03-19 12:06:10 -0700406 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700407 if (count > MAX_AXES) {
408 return BAD_VALUE;
409 }
410
411 for (uint32_t i = 0; i < count; i++) {
412 values[i] = parcel->readFloat();
413 }
414 return OK;
415}
416
417status_t PointerCoords::writeToParcel(Parcel* parcel) const {
418 parcel->writeInt64(bits);
419
Michael Wright38dcdff2014-03-19 12:06:10 -0700420 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700421 for (uint32_t i = 0; i < count; i++) {
422 parcel->writeFloat(values[i]);
423 }
424 return OK;
425}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800426#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700427
428void PointerCoords::tooManyAxes(int axis) {
429 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
430 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
431}
432
433bool PointerCoords::operator==(const PointerCoords& other) const {
434 if (bits != other.bits) {
435 return false;
436 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700437 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700438 for (uint32_t i = 0; i < count; i++) {
439 if (values[i] != other.values[i]) {
440 return false;
441 }
442 }
443 return true;
444}
445
chaviwc01e1372020-07-01 12:37:31 -0700446void PointerCoords::transform(const ui::Transform& transform) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700447 const vec2 xy = transform.transform(getXYValue());
448 setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
449 setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
450
Prabir Pradhanc6523582021-05-14 18:02:55 -0700451 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_X) ||
452 BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_Y)) {
453 const ui::Transform rotation(transform.getOrientation());
454 const vec2 relativeXy = rotation.transform(getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
455 getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
456 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
457 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
458 }
459
Prabir Pradhan6b384612021-05-14 16:56:25 -0700460 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) {
461 const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
462 setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val));
463 }
chaviwc01e1372020-07-01 12:37:31 -0700464}
Jeff Brown5912f952013-07-01 19:10:31 -0700465
466// --- PointerProperties ---
467
468bool PointerProperties::operator==(const PointerProperties& other) const {
469 return id == other.id
470 && toolType == other.toolType;
471}
472
473void PointerProperties::copyFrom(const PointerProperties& other) {
474 id = other.id;
475 toolType = other.toolType;
476}
477
478
479// --- MotionEvent ---
480
Garfield Tan4cc839f2020-01-24 11:26:14 -0800481void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600482 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
483 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700484 int32_t buttonState, MotionClassification classification,
485 const ui::Transform& transform, float xPrecision, float yPrecision,
Evan Rosky84f07f02021-04-16 10:42:42 -0700486 float rawXCursorPosition, float rawYCursorPosition,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700487 const ui::Transform& rawTransform, nsecs_t downTime, nsecs_t eventTime,
Evan Rosky09576692021-07-01 12:22:09 -0700488 size_t pointerCount, const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700489 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800490 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700491 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100492 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700493 mFlags = flags;
494 mEdgeFlags = edgeFlags;
495 mMetaState = metaState;
496 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800497 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700498 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700499 mXPrecision = xPrecision;
500 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700501 mRawXCursorPosition = rawXCursorPosition;
502 mRawYCursorPosition = rawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700503 mRawTransform = rawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700504 mDownTime = downTime;
505 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800506 mPointerProperties.insert(mPointerProperties.end(), &pointerProperties[0],
507 &pointerProperties[pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700508 mSampleEventTimes.clear();
509 mSamplePointerCoords.clear();
510 addSample(eventTime, pointerCoords);
511}
512
513void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800514 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
515 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700516 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100517 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700518 mFlags = other->mFlags;
519 mEdgeFlags = other->mEdgeFlags;
520 mMetaState = other->mMetaState;
521 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800522 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700523 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700524 mXPrecision = other->mXPrecision;
525 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700526 mRawXCursorPosition = other->mRawXCursorPosition;
527 mRawYCursorPosition = other->mRawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700528 mRawTransform = other->mRawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700529 mDownTime = other->mDownTime;
530 mPointerProperties = other->mPointerProperties;
531
532 if (keepHistory) {
533 mSampleEventTimes = other->mSampleEventTimes;
534 mSamplePointerCoords = other->mSamplePointerCoords;
535 } else {
536 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500537 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700538 mSamplePointerCoords.clear();
539 size_t pointerCount = other->getPointerCount();
540 size_t historySize = other->getHistorySize();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800541 mSamplePointerCoords
542 .insert(mSamplePointerCoords.end(),
543 &other->mSamplePointerCoords[historySize * pointerCount],
544 &other->mSamplePointerCoords[historySize * pointerCount + pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700545 }
546}
547
548void MotionEvent::addSample(
549 int64_t eventTime,
550 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500551 mSampleEventTimes.push_back(eventTime);
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800552 mSamplePointerCoords.insert(mSamplePointerCoords.end(), &pointerCoords[0],
553 &pointerCoords[getPointerCount()]);
Jeff Brown5912f952013-07-01 19:10:31 -0700554}
555
Michael Wright635422b2022-12-02 00:43:56 +0000556std::optional<ui::Rotation> MotionEvent::getSurfaceRotation() const {
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800557 // The surface rotation is the rotation from the window's coordinate space to that of the
558 // display. Since the event's transform takes display space coordinates to window space, the
559 // returned surface rotation is the inverse of the rotation for the surface.
560 switch (mTransform.getOrientation()) {
561 case ui::Transform::ROT_0:
Michael Wright635422b2022-12-02 00:43:56 +0000562 return ui::ROTATION_0;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800563 case ui::Transform::ROT_90:
Michael Wright635422b2022-12-02 00:43:56 +0000564 return ui::ROTATION_270;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800565 case ui::Transform::ROT_180:
Michael Wright635422b2022-12-02 00:43:56 +0000566 return ui::ROTATION_180;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800567 case ui::Transform::ROT_270:
Michael Wright635422b2022-12-02 00:43:56 +0000568 return ui::ROTATION_90;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800569 default:
Michael Wright635422b2022-12-02 00:43:56 +0000570 return std::nullopt;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800571 }
572}
573
Garfield Tan00f511d2019-06-12 16:55:40 -0700574float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700575 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
576 return vals.x;
Garfield Tan00f511d2019-06-12 16:55:40 -0700577}
578
579float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700580 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
581 return vals.y;
Garfield Tan00f511d2019-06-12 16:55:40 -0700582}
583
Garfield Tan937bb832019-07-25 17:48:31 -0700584void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700585 ui::Transform inverse = mTransform.inverse();
586 vec2 vals = inverse.transform(x, y);
587 mRawXCursorPosition = vals.x;
588 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700589}
590
Jeff Brown5912f952013-07-01 19:10:31 -0700591const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000592 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
593 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
594 }
595 const size_t position = getHistorySize() * getPointerCount() + pointerIndex;
596 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
597 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
598 }
599 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700600}
601
602float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
Evan Rosky84f07f02021-04-16 10:42:42 -0700603 return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700604}
605
606float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700607 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700608}
609
610const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
611 size_t pointerIndex, size_t historicalIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000612 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
613 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
614 }
615 if (CC_UNLIKELY(historicalIndex < 0 || historicalIndex > getHistorySize())) {
616 LOG(FATAL) << __func__ << ": Invalid historical index " << historicalIndex << " for "
617 << *this;
618 }
619 const size_t position = historicalIndex * getPointerCount() + pointerIndex;
620 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
621 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
622 }
623 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700624}
625
626float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan6b384612021-05-14 16:56:25 -0700627 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700628 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
629 return calculateTransformedAxisValue(axis, mSource, mRawTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700630}
631
632float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan9f388812021-05-13 16:54:53 -0700633 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700634 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
635 return calculateTransformedAxisValue(axis, mSource, mTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700636}
637
638ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
639 size_t pointerCount = mPointerProperties.size();
640 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800641 if (mPointerProperties[i].id == pointerId) {
Jeff Brown5912f952013-07-01 19:10:31 -0700642 return i;
643 }
644 }
645 return -1;
646}
647
648void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700649 float currXOffset = mTransform.tx();
650 float currYOffset = mTransform.ty();
651 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700652}
653
Robert Carre07e1032018-11-26 12:55:53 -0800654void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700655 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700656 mRawTransform.set(mRawTransform.tx() * globalScaleFactor,
657 mRawTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800658 mXPrecision *= globalScaleFactor;
659 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700660
661 size_t numSamples = mSamplePointerCoords.size();
662 for (size_t i = 0; i < numSamples; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800663 mSamplePointerCoords[i].scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700664 }
665}
666
chaviw9eaa22c2020-07-01 16:21:27 -0700667void MotionEvent::transform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700668 // We want to preserve the raw axes values stored in the PointerCoords, so we just update the
669 // transform using the values passed in.
chaviw9eaa22c2020-07-01 16:21:27 -0700670 ui::Transform newTransform;
671 newTransform.set(matrix);
672 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700673}
674
Evan Roskyd4d4d802021-05-03 20:12:21 -0700675void MotionEvent::applyTransform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700676 ui::Transform transform;
677 transform.set(matrix);
Evan Roskyd4d4d802021-05-03 20:12:21 -0700678
679 // Apply the transformation to all samples.
Prabir Pradhan6b384612021-05-14 16:56:25 -0700680 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
681 [&transform](PointerCoords& c) { c.transform(transform); });
Prabir Pradhan4b19bd02021-06-01 17:34:59 -0700682
683 if (mRawXCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION &&
684 mRawYCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) {
685 const vec2 cursor = transform.transform(mRawXCursorPosition, mRawYCursorPosition);
686 mRawXCursorPosition = cursor.x;
687 mRawYCursorPosition = cursor.y;
688 }
Evan Roskyd4d4d802021-05-03 20:12:21 -0700689}
690
Brett Chabotfaa986c2020-11-04 17:39:36 -0800691#ifdef __linux__
chaviw9eaa22c2020-07-01 16:21:27 -0700692static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
693 float dsdx, dtdx, tx, dtdy, dsdy, ty;
694 status_t status = parcel.readFloat(&dsdx);
695 status |= parcel.readFloat(&dtdx);
696 status |= parcel.readFloat(&tx);
697 status |= parcel.readFloat(&dtdy);
698 status |= parcel.readFloat(&dsdy);
699 status |= parcel.readFloat(&ty);
700
701 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
702 return status;
703}
704
705static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
706 status_t status = parcel.writeFloat(transform.dsdx());
707 status |= parcel.writeFloat(transform.dtdx());
708 status |= parcel.writeFloat(transform.tx());
709 status |= parcel.writeFloat(transform.dtdy());
710 status |= parcel.writeFloat(transform.dsdy());
711 status |= parcel.writeFloat(transform.ty());
712 return status;
713}
714
Jeff Brown5912f952013-07-01 19:10:31 -0700715status_t MotionEvent::readFromParcel(Parcel* parcel) {
716 size_t pointerCount = parcel->readInt32();
717 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800718 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
719 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700720 return BAD_VALUE;
721 }
722
Garfield Tan4cc839f2020-01-24 11:26:14 -0800723 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700724 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600725 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800726 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600727 std::vector<uint8_t> hmac;
728 status_t result = parcel->readByteVector(&hmac);
729 if (result != OK || hmac.size() != 32) {
730 return BAD_VALUE;
731 }
732 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700733 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100734 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700735 mFlags = parcel->readInt32();
736 mEdgeFlags = parcel->readInt32();
737 mMetaState = parcel->readInt32();
738 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800739 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700740
741 result = android::readFromParcel(mTransform, *parcel);
742 if (result != OK) {
743 return result;
744 }
Jeff Brown5912f952013-07-01 19:10:31 -0700745 mXPrecision = parcel->readFloat();
746 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700747 mRawXCursorPosition = parcel->readFloat();
748 mRawYCursorPosition = parcel->readFloat();
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700749
750 result = android::readFromParcel(mRawTransform, *parcel);
751 if (result != OK) {
752 return result;
753 }
Jeff Brown5912f952013-07-01 19:10:31 -0700754 mDownTime = parcel->readInt64();
755
756 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800757 mPointerProperties.reserve(pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700758 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500759 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700760 mSamplePointerCoords.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800761 mSamplePointerCoords.reserve(sampleCount * pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700762
763 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800764 mPointerProperties.push_back({});
765 PointerProperties& properties = mPointerProperties.back();
Jeff Brown5912f952013-07-01 19:10:31 -0700766 properties.id = parcel->readInt32();
767 properties.toolType = parcel->readInt32();
768 }
769
Dan Austinc94fc452015-09-22 14:22:41 -0700770 while (sampleCount > 0) {
771 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500772 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700773 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800774 mSamplePointerCoords.push_back({});
775 status_t status = mSamplePointerCoords.back().readFromParcel(parcel);
Jeff Brown5912f952013-07-01 19:10:31 -0700776 if (status) {
777 return status;
778 }
779 }
780 }
781 return OK;
782}
783
784status_t MotionEvent::writeToParcel(Parcel* parcel) const {
785 size_t pointerCount = mPointerProperties.size();
786 size_t sampleCount = mSampleEventTimes.size();
787
788 parcel->writeInt32(pointerCount);
789 parcel->writeInt32(sampleCount);
790
Garfield Tan4cc839f2020-01-24 11:26:14 -0800791 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700792 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600793 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800794 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600795 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
796 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700797 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100798 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700799 parcel->writeInt32(mFlags);
800 parcel->writeInt32(mEdgeFlags);
801 parcel->writeInt32(mMetaState);
802 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800803 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700804
805 status_t result = android::writeToParcel(mTransform, *parcel);
806 if (result != OK) {
807 return result;
808 }
Jeff Brown5912f952013-07-01 19:10:31 -0700809 parcel->writeFloat(mXPrecision);
810 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700811 parcel->writeFloat(mRawXCursorPosition);
812 parcel->writeFloat(mRawYCursorPosition);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700813
814 result = android::writeToParcel(mRawTransform, *parcel);
815 if (result != OK) {
816 return result;
817 }
Jeff Brown5912f952013-07-01 19:10:31 -0700818 parcel->writeInt64(mDownTime);
819
820 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800821 const PointerProperties& properties = mPointerProperties[i];
Jeff Brown5912f952013-07-01 19:10:31 -0700822 parcel->writeInt32(properties.id);
823 parcel->writeInt32(properties.toolType);
824 }
825
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800826 const PointerCoords* pc = mSamplePointerCoords.data();
Jeff Brown5912f952013-07-01 19:10:31 -0700827 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500828 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700829 for (size_t i = 0; i < pointerCount; i++) {
830 status_t status = (pc++)->writeToParcel(parcel);
831 if (status) {
832 return status;
833 }
834 }
835 }
836 return OK;
837}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800838#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700839
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600840bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700841 if (isFromSource(source, AINPUT_SOURCE_CLASS_POINTER)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700842 // Specifically excludes HOVER_MOVE and SCROLL.
843 switch (action & AMOTION_EVENT_ACTION_MASK) {
844 case AMOTION_EVENT_ACTION_DOWN:
845 case AMOTION_EVENT_ACTION_MOVE:
846 case AMOTION_EVENT_ACTION_UP:
847 case AMOTION_EVENT_ACTION_POINTER_DOWN:
848 case AMOTION_EVENT_ACTION_POINTER_UP:
849 case AMOTION_EVENT_ACTION_CANCEL:
850 case AMOTION_EVENT_ACTION_OUTSIDE:
851 return true;
852 }
853 }
854 return false;
855}
856
Michael Wright872db4f2014-04-22 15:03:51 -0700857const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700858 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700859}
860
861int32_t MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700862 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700863}
864
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500865std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700866 // Convert MotionEvent action to string
867 switch (action & AMOTION_EVENT_ACTION_MASK) {
868 case AMOTION_EVENT_ACTION_DOWN:
869 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700870 case AMOTION_EVENT_ACTION_UP:
871 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500872 case AMOTION_EVENT_ACTION_MOVE:
873 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700874 case AMOTION_EVENT_ACTION_CANCEL:
875 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500876 case AMOTION_EVENT_ACTION_OUTSIDE:
877 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700878 case AMOTION_EVENT_ACTION_POINTER_DOWN:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000879 return StringPrintf("POINTER_DOWN(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700880 case AMOTION_EVENT_ACTION_POINTER_UP:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000881 return StringPrintf("POINTER_UP(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500882 case AMOTION_EVENT_ACTION_HOVER_MOVE:
883 return "HOVER_MOVE";
884 case AMOTION_EVENT_ACTION_SCROLL:
885 return "SCROLL";
886 case AMOTION_EVENT_ACTION_HOVER_ENTER:
887 return "HOVER_ENTER";
888 case AMOTION_EVENT_ACTION_HOVER_EXIT:
889 return "HOVER_EXIT";
890 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
891 return "BUTTON_PRESS";
892 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
893 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700894 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500895 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700896}
897
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700898// Apply the given transformation to the point without checking whether the entire transform
899// should be disregarded altogether for the provided source.
900static inline vec2 calculateTransformedXYUnchecked(uint32_t source, const ui::Transform& transform,
901 const vec2& xy) {
902 return shouldDisregardOffset(source) ? transformWithoutTranslation(transform, xy)
903 : transform.transform(xy);
904}
905
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700906vec2 MotionEvent::calculateTransformedXY(uint32_t source, const ui::Transform& transform,
907 const vec2& xy) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700908 if (shouldDisregardTransformation(source)) {
909 return xy;
910 }
911 return calculateTransformedXYUnchecked(source, transform, xy);
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700912}
913
Prabir Pradhan8e6ce222022-02-24 09:08:54 -0800914// Keep in sync with calculateTransformedCoords.
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700915float MotionEvent::calculateTransformedAxisValue(int32_t axis, uint32_t source,
916 const ui::Transform& transform,
917 const PointerCoords& coords) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700918 if (shouldDisregardTransformation(source)) {
919 return coords.getAxisValue(axis);
920 }
921
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700922 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700923 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700924 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
925 return xy[axis];
926 }
927
928 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
929 const vec2 relativeXy =
930 transformWithoutTranslation(transform,
931 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
932 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
933 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
934 }
935
936 if (axis == AMOTION_EVENT_AXIS_ORIENTATION) {
937 return transformAngle(transform, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION));
938 }
939
940 return coords.getAxisValue(axis);
941}
942
Prabir Pradhan8e6ce222022-02-24 09:08:54 -0800943// Keep in sync with calculateTransformedAxisValue. This is an optimization of
944// calculateTransformedAxisValue for all PointerCoords axes.
945PointerCoords MotionEvent::calculateTransformedCoords(uint32_t source,
946 const ui::Transform& transform,
947 const PointerCoords& coords) {
948 if (shouldDisregardTransformation(source)) {
949 return coords;
950 }
951 PointerCoords out = coords;
952
953 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
954 out.setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
955 out.setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
956
957 const vec2 relativeXy =
958 transformWithoutTranslation(transform,
959 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
960 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
961 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
962 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
963
964 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
965 transformAngle(transform,
966 coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)));
967
968 return out;
969}
970
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000971std::ostream& operator<<(std::ostream& out, const MotionEvent& event) {
972 out << "MotionEvent { action=" << MotionEvent::actionToString(event.getAction());
973 if (event.getActionButton() != 0) {
974 out << ", actionButton=" << std::to_string(event.getActionButton());
975 }
976 const size_t pointerCount = event.getPointerCount();
hupeng3aa5a51a2022-09-02 16:00:18 +0800977 LOG_ALWAYS_FATAL_IF(pointerCount > MAX_POINTERS, "Too many pointers : pointerCount = %zu",
978 pointerCount);
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000979 for (size_t i = 0; i < pointerCount; i++) {
980 out << ", id[" << i << "]=" << event.getPointerId(i);
981 float x = event.getX(i);
982 float y = event.getY(i);
983 if (x != 0 || y != 0) {
984 out << ", x[" << i << "]=" << x;
985 out << ", y[" << i << "]=" << y;
986 }
987 int toolType = event.getToolType(i);
988 if (toolType != AMOTION_EVENT_TOOL_TYPE_FINGER) {
989 out << ", toolType[" << i << "]=" << toolType;
990 }
991 }
992 if (event.getButtonState() != 0) {
993 out << ", buttonState=" << event.getButtonState();
994 }
995 if (event.getClassification() != MotionClassification::NONE) {
996 out << ", classification=" << motionClassificationToString(event.getClassification());
997 }
998 if (event.getMetaState() != 0) {
999 out << ", metaState=" << event.getMetaState();
1000 }
1001 if (event.getEdgeFlags() != 0) {
1002 out << ", edgeFlags=" << event.getEdgeFlags();
1003 }
1004 if (pointerCount != 1) {
1005 out << ", pointerCount=" << pointerCount;
1006 }
1007 if (event.getHistorySize() != 0) {
1008 out << ", historySize=" << event.getHistorySize();
1009 }
1010 out << ", eventTime=" << event.getEventTime();
1011 out << ", downTime=" << event.getDownTime();
1012 out << ", deviceId=" << event.getDeviceId();
1013 out << ", source=" << inputEventSourceToString(event.getSource());
1014 out << ", displayId=" << event.getDisplayId();
1015 out << ", eventId=" << event.getId();
1016 out << "}";
1017 return out;
1018}
1019
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001020// --- FocusEvent ---
1021
Antonio Kantek3cfec7b2021-11-05 18:26:17 -07001022void FocusEvent::initialize(int32_t id, bool hasFocus) {
Garfield Tan4cc839f2020-01-24 11:26:14 -08001023 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -06001024 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001025 mHasFocus = hasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001026}
1027
1028void FocusEvent::initialize(const FocusEvent& from) {
1029 InputEvent::initialize(from);
1030 mHasFocus = from.mHasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001031}
Jeff Brown5912f952013-07-01 19:10:31 -07001032
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001033// --- CaptureEvent ---
1034
1035void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) {
1036 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1037 ADISPLAY_ID_NONE, INVALID_HMAC);
1038 mPointerCaptureEnabled = pointerCaptureEnabled;
1039}
1040
1041void CaptureEvent::initialize(const CaptureEvent& from) {
1042 InputEvent::initialize(from);
1043 mPointerCaptureEnabled = from.mPointerCaptureEnabled;
1044}
1045
arthurhung7632c332020-12-30 16:58:01 +08001046// --- DragEvent ---
1047
1048void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) {
1049 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1050 ADISPLAY_ID_NONE, INVALID_HMAC);
1051 mIsExiting = isExiting;
1052 mX = x;
1053 mY = y;
1054}
1055
1056void DragEvent::initialize(const DragEvent& from) {
1057 InputEvent::initialize(from);
1058 mIsExiting = from.mIsExiting;
1059 mX = from.mX;
1060 mY = from.mY;
1061}
1062
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001063// --- TouchModeEvent ---
1064
1065void TouchModeEvent::initialize(int32_t id, bool isInTouchMode) {
1066 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1067 ADISPLAY_ID_NONE, INVALID_HMAC);
1068 mIsInTouchMode = isInTouchMode;
1069}
1070
1071void TouchModeEvent::initialize(const TouchModeEvent& from) {
1072 InputEvent::initialize(from);
1073 mIsInTouchMode = from.mIsInTouchMode;
1074}
1075
Jeff Brown5912f952013-07-01 19:10:31 -07001076// --- PooledInputEventFactory ---
1077
1078PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
1079 mMaxPoolSize(maxPoolSize) {
1080}
1081
1082PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -07001083}
1084
1085KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001086 if (mKeyEventPool.empty()) {
1087 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001088 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001089 KeyEvent* event = mKeyEventPool.front().release();
1090 mKeyEventPool.pop();
1091 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001092}
1093
1094MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001095 if (mMotionEventPool.empty()) {
1096 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001097 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001098 MotionEvent* event = mMotionEventPool.front().release();
1099 mMotionEventPool.pop();
1100 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001101}
1102
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001103FocusEvent* PooledInputEventFactory::createFocusEvent() {
1104 if (mFocusEventPool.empty()) {
1105 return new FocusEvent();
1106 }
1107 FocusEvent* event = mFocusEventPool.front().release();
1108 mFocusEventPool.pop();
1109 return event;
1110}
1111
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001112CaptureEvent* PooledInputEventFactory::createCaptureEvent() {
1113 if (mCaptureEventPool.empty()) {
1114 return new CaptureEvent();
1115 }
1116 CaptureEvent* event = mCaptureEventPool.front().release();
1117 mCaptureEventPool.pop();
1118 return event;
1119}
1120
arthurhung7632c332020-12-30 16:58:01 +08001121DragEvent* PooledInputEventFactory::createDragEvent() {
1122 if (mDragEventPool.empty()) {
1123 return new DragEvent();
1124 }
1125 DragEvent* event = mDragEventPool.front().release();
1126 mDragEventPool.pop();
1127 return event;
1128}
1129
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001130TouchModeEvent* PooledInputEventFactory::createTouchModeEvent() {
1131 if (mTouchModeEventPool.empty()) {
1132 return new TouchModeEvent();
1133 }
1134 TouchModeEvent* event = mTouchModeEventPool.front().release();
1135 mTouchModeEventPool.pop();
1136 return event;
1137}
1138
Jeff Brown5912f952013-07-01 19:10:31 -07001139void PooledInputEventFactory::recycle(InputEvent* event) {
1140 switch (event->getType()) {
1141 case AINPUT_EVENT_TYPE_KEY:
1142 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001143 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -07001144 return;
1145 }
1146 break;
1147 case AINPUT_EVENT_TYPE_MOTION:
1148 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001149 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -07001150 return;
1151 }
1152 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001153 case AINPUT_EVENT_TYPE_FOCUS:
1154 if (mFocusEventPool.size() < mMaxPoolSize) {
1155 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
1156 return;
1157 }
1158 break;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001159 case AINPUT_EVENT_TYPE_CAPTURE:
1160 if (mCaptureEventPool.size() < mMaxPoolSize) {
1161 mCaptureEventPool.push(
1162 std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event)));
1163 return;
1164 }
1165 break;
arthurhung7632c332020-12-30 16:58:01 +08001166 case AINPUT_EVENT_TYPE_DRAG:
1167 if (mDragEventPool.size() < mMaxPoolSize) {
1168 mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event)));
1169 return;
1170 }
1171 break;
Antonio Kantekeb4a30c2021-09-28 17:49:49 -07001172 case AINPUT_EVENT_TYPE_TOUCH_MODE:
1173 if (mTouchModeEventPool.size() < mMaxPoolSize) {
1174 mTouchModeEventPool.push(
1175 std::unique_ptr<TouchModeEvent>(static_cast<TouchModeEvent*>(event)));
1176 return;
1177 }
1178 break;
Jeff Brown5912f952013-07-01 19:10:31 -07001179 }
1180 delete event;
1181}
1182
1183} // namespace android