blob: c7964393e06baa58b853e93d57eb6dc5c83d2f4b [file] [log] [blame]
Jeff Brown5912f952013-07-01 19:10:31 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Input"
18//#define LOG_NDEBUG 0
19
chaviw09c8d2d2020-08-24 15:48:26 -070020#include <attestation/HmacKeyManager.h>
Garfield Tan84b087e2020-01-23 10:49:05 -080021#include <cutils/compiler.h>
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050022#include <inttypes.h>
Garfield Tan84b087e2020-01-23 10:49:05 -080023#include <string.h>
Michael Wright635422b2022-12-02 00:43:56 +000024#include <optional>
Jeff Brown5912f952013-07-01 19:10:31 -070025
Siarhei Vishniakou31977182022-09-30 08:51:23 -070026#include <android-base/file.h>
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +000027#include <android-base/logging.h>
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050028#include <android-base/stringprintf.h>
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +000029#include <cutils/compiler.h>
chaviw98318de2021-05-19 16:45:23 -050030#include <gui/constants.h>
Prabir Pradhan092f3a92021-11-25 10:53:27 -080031#include <input/DisplayViewport.h>
Jeff Brown5912f952013-07-01 19:10:31 -070032#include <input/Input.h>
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080033#include <input/InputDevice.h>
Michael Wright872db4f2014-04-22 15:03:51 -070034#include <input/InputEventLabels.h>
Jeff Brown5912f952013-07-01 19:10:31 -070035
Brett Chabotfaa986c2020-11-04 17:39:36 -080036#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -070037#include <binder/Parcel.h>
Brett Chabotfaa986c2020-11-04 17:39:36 -080038#endif
Siarhei Vishniakou63740b92022-10-20 10:28:08 -070039#if defined(__ANDROID__)
40#include <sys/random.h>
41#endif
Jeff Brown5912f952013-07-01 19:10:31 -070042
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050043using android::base::StringPrintf;
44
Jeff Brown5912f952013-07-01 19:10:31 -070045namespace android {
46
Prabir Pradhan6b384612021-05-14 16:56:25 -070047namespace {
48
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070049bool shouldDisregardTransformation(uint32_t source) {
Prabir Pradhan258e2b92022-06-24 18:37:04 +000050 // Do not apply any transformations to axes from joysticks, touchpads, or relative mice.
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070051 return isFromSource(source, AINPUT_SOURCE_CLASS_JOYSTICK) ||
Prabir Pradhan258e2b92022-06-24 18:37:04 +000052 isFromSource(source, AINPUT_SOURCE_CLASS_POSITION) ||
53 isFromSource(source, AINPUT_SOURCE_MOUSE_RELATIVE);
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070054}
55
56bool shouldDisregardOffset(uint32_t source) {
Prabir Pradhan9f388812021-05-13 16:54:53 -070057 // Pointer events are the only type of events that refer to absolute coordinates on the display,
58 // so we should apply the entire window transform. For other types of events, we should make
59 // sure to not apply the window translation/offset.
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070060 return !isFromSource(source, AINPUT_SOURCE_CLASS_POINTER);
Prabir Pradhan9f388812021-05-13 16:54:53 -070061}
62
Prabir Pradhan6b384612021-05-14 16:56:25 -070063} // namespace
64
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -080065const char* motionClassificationToString(MotionClassification classification) {
66 switch (classification) {
67 case MotionClassification::NONE:
68 return "NONE";
69 case MotionClassification::AMBIGUOUS_GESTURE:
70 return "AMBIGUOUS_GESTURE";
71 case MotionClassification::DEEP_PRESS:
72 return "DEEP_PRESS";
Harry Cutts2800fb02022-09-15 13:49:23 +000073 case MotionClassification::TWO_FINGER_SWIPE:
74 return "TWO_FINGER_SWIPE";
Harry Cuttsc5748d12022-12-02 17:30:18 +000075 case MotionClassification::MULTI_FINGER_SWIPE:
76 return "MULTI_FINGER_SWIPE";
Harry Cuttsb1e83552022-12-20 11:02:26 +000077 case MotionClassification::PINCH:
78 return "PINCH";
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -080079 }
80}
81
Siarhei Vishniakoud5fe5182022-07-20 23:28:40 +000082const char* motionToolTypeToString(int32_t toolType) {
83 switch (toolType) {
84 case AMOTION_EVENT_TOOL_TYPE_UNKNOWN:
85 return "UNKNOWN";
86 case AMOTION_EVENT_TOOL_TYPE_FINGER:
87 return "FINGER";
88 case AMOTION_EVENT_TOOL_TYPE_STYLUS:
89 return "STYLUS";
90 case AMOTION_EVENT_TOOL_TYPE_MOUSE:
91 return "MOUSE";
92 case AMOTION_EVENT_TOOL_TYPE_ERASER:
93 return "ERASER";
94 case AMOTION_EVENT_TOOL_TYPE_PALM:
95 return "PALM";
96 default:
97 return "INVALID";
98 }
99}
100
Garfield Tan84b087e2020-01-23 10:49:05 -0800101// --- IdGenerator ---
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700102#if defined(__ANDROID__)
103[[maybe_unused]]
104#endif
105static status_t
106getRandomBytes(uint8_t* data, size_t size) {
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700107 int ret = TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY | O_CLOEXEC | O_NOFOLLOW));
108 if (ret == -1) {
109 return -errno;
110 }
111
112 base::unique_fd fd(ret);
113 if (!base::ReadFully(fd, data, size)) {
114 return -errno;
115 }
116 return OK;
117}
118
Garfield Tan84b087e2020-01-23 10:49:05 -0800119IdGenerator::IdGenerator(Source source) : mSource(source) {}
120
121int32_t IdGenerator::nextId() const {
122 constexpr uint32_t SEQUENCE_NUMBER_MASK = ~SOURCE_MASK;
123 int32_t id = 0;
124
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700125#if defined(__ANDROID__)
126 // On device, prefer 'getrandom' to '/dev/urandom' because it's faster.
127 constexpr size_t BUF_LEN = sizeof(id);
128 size_t totalBytes = 0;
129 while (totalBytes < BUF_LEN) {
130 ssize_t bytes = TEMP_FAILURE_RETRY(getrandom(&id, BUF_LEN, GRND_NONBLOCK));
131 if (CC_UNLIKELY(bytes < 0)) {
132 ALOGW("Failed to fill in random number for sequence number: %s.", strerror(errno));
133 id = 0;
134 break;
135 }
136 totalBytes += bytes;
137 }
138#else
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700139#if defined(__linux__)
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700140 // On host, <sys/random.h> / GRND_NONBLOCK is not available
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700141 while (true) {
142 status_t result = getRandomBytes(reinterpret_cast<uint8_t*>(&id), sizeof(id));
143 if (result == OK) {
Garfield Tan84b087e2020-01-23 10:49:05 -0800144 break;
145 }
Garfield Tan84b087e2020-01-23 10:49:05 -0800146 }
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700147#endif // __linux__
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700148#endif // __ANDROID__
Garfield Tan84b087e2020-01-23 10:49:05 -0800149 return (id & SEQUENCE_NUMBER_MASK) | static_cast<int32_t>(mSource);
150}
151
Jeff Brown5912f952013-07-01 19:10:31 -0700152// --- InputEvent ---
153
Prabir Pradhande69f8a2021-11-18 16:40:34 +0000154vec2 transformWithoutTranslation(const ui::Transform& transform, const vec2& xy) {
155 const vec2 transformedXy = transform.transform(xy);
156 const vec2 transformedOrigin = transform.transform(0, 0);
157 return transformedXy - transformedOrigin;
158}
159
Prabir Pradhane2e10b42022-11-17 20:59:36 +0000160float transformAngle(const ui::Transform& transform, float angleRadians) {
161 // Construct and transform a vector oriented at the specified clockwise angle from vertical.
162 // Coordinate system: down is increasing Y, right is increasing X.
163 float x = sinf(angleRadians);
164 float y = -cosf(angleRadians);
165 vec2 transformedPoint = transform.transform(x, y);
166
167 // Determine how the origin is transformed by the matrix so that we
168 // can transform orientation vectors.
169 const vec2 origin = transform.transform(0, 0);
170
171 transformedPoint.x -= origin.x;
172 transformedPoint.y -= origin.y;
173
174 // Derive the transformed vector's clockwise angle from vertical.
175 // The return value of atan2f is in range [-pi, pi] which conforms to the orientation API.
176 return atan2f(transformedPoint.x, -transformedPoint.y);
177}
178
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800179const char* inputEventTypeToString(int32_t type) {
180 switch (type) {
181 case AINPUT_EVENT_TYPE_KEY: {
182 return "KEY";
183 }
184 case AINPUT_EVENT_TYPE_MOTION: {
185 return "MOTION";
186 }
187 case AINPUT_EVENT_TYPE_FOCUS: {
188 return "FOCUS";
189 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800190 case AINPUT_EVENT_TYPE_CAPTURE: {
191 return "CAPTURE";
192 }
arthurhung7632c332020-12-30 16:58:01 +0800193 case AINPUT_EVENT_TYPE_DRAG: {
194 return "DRAG";
195 }
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700196 case AINPUT_EVENT_TYPE_TOUCH_MODE: {
197 return "TOUCH_MODE";
198 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800199 }
200 return "UNKNOWN";
201}
202
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800203std::string inputEventSourceToString(int32_t source) {
204 if (source == AINPUT_SOURCE_UNKNOWN) {
205 return "UNKNOWN";
206 }
207 if (source == static_cast<int32_t>(AINPUT_SOURCE_ANY)) {
208 return "ANY";
209 }
210 static const std::map<int32_t, const char*> SOURCES{
211 {AINPUT_SOURCE_KEYBOARD, "KEYBOARD"},
212 {AINPUT_SOURCE_DPAD, "DPAD"},
213 {AINPUT_SOURCE_GAMEPAD, "GAMEPAD"},
214 {AINPUT_SOURCE_TOUCHSCREEN, "TOUCHSCREEN"},
215 {AINPUT_SOURCE_MOUSE, "MOUSE"},
216 {AINPUT_SOURCE_STYLUS, "STYLUS"},
217 {AINPUT_SOURCE_BLUETOOTH_STYLUS, "BLUETOOTH_STYLUS"},
218 {AINPUT_SOURCE_TRACKBALL, "TRACKBALL"},
219 {AINPUT_SOURCE_MOUSE_RELATIVE, "MOUSE_RELATIVE"},
220 {AINPUT_SOURCE_TOUCHPAD, "TOUCHPAD"},
221 {AINPUT_SOURCE_TOUCH_NAVIGATION, "TOUCH_NAVIGATION"},
222 {AINPUT_SOURCE_JOYSTICK, "JOYSTICK"},
223 {AINPUT_SOURCE_HDMI, "HDMI"},
224 {AINPUT_SOURCE_SENSOR, "SENSOR"},
225 {AINPUT_SOURCE_ROTARY_ENCODER, "ROTARY_ENCODER"},
226 };
227 std::string result;
228 for (const auto& [source_entry, str] : SOURCES) {
229 if ((source & source_entry) == source_entry) {
230 if (!result.empty()) {
231 result += " | ";
232 }
233 result += str;
234 }
235 }
236 if (result.empty()) {
237 result = StringPrintf("0x%08x", source);
238 }
239 return result;
240}
241
242bool isFromSource(uint32_t source, uint32_t test) {
243 return (source & test) == test;
244}
245
Prabir Pradhane5626962022-10-27 20:30:53 +0000246bool isStylusToolType(uint32_t toolType) {
247 return toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS || toolType == AMOTION_EVENT_TOOL_TYPE_ERASER;
248}
249
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800250VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
251 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
252 event.getSource(), event.getDisplayId()},
253 event.getAction(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800254 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800255 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800256 event.getKeyCode(),
257 event.getScanCode(),
258 event.getMetaState(),
259 event.getRepeatCount()};
260}
261
262VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
263 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
264 event.getSource(), event.getDisplayId()},
265 event.getRawX(0),
266 event.getRawY(0),
267 event.getActionMasked(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800268 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800269 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800270 event.getMetaState(),
271 event.getButtonState()};
272}
273
Garfield Tan4cc839f2020-01-24 11:26:14 -0800274void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600275 std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800276 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700277 mDeviceId = deviceId;
278 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100279 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600280 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700281}
282
283void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800284 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700285 mDeviceId = from.mDeviceId;
286 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100287 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600288 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700289}
290
Garfield Tan4cc839f2020-01-24 11:26:14 -0800291int32_t InputEvent::nextId() {
292 static IdGenerator idGen(IdGenerator::Source::OTHER);
293 return idGen.nextId();
294}
295
Jeff Brown5912f952013-07-01 19:10:31 -0700296// --- KeyEvent ---
297
Michael Wright872db4f2014-04-22 15:03:51 -0700298const char* KeyEvent::getLabel(int32_t keyCode) {
Chris Ye4958d062020-08-20 13:21:10 -0700299 return InputEventLookup::getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700300}
301
Michael Wright872db4f2014-04-22 15:03:51 -0700302int32_t KeyEvent::getKeyCodeFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700303 return InputEventLookup::getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700304}
305
Garfield Tan4cc839f2020-01-24 11:26:14 -0800306void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600307 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
308 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
309 nsecs_t downTime, nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800310 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700311 mAction = action;
312 mFlags = flags;
313 mKeyCode = keyCode;
314 mScanCode = scanCode;
315 mMetaState = metaState;
316 mRepeatCount = repeatCount;
317 mDownTime = downTime;
318 mEventTime = eventTime;
319}
320
321void KeyEvent::initialize(const KeyEvent& from) {
322 InputEvent::initialize(from);
323 mAction = from.mAction;
324 mFlags = from.mFlags;
325 mKeyCode = from.mKeyCode;
326 mScanCode = from.mScanCode;
327 mMetaState = from.mMetaState;
328 mRepeatCount = from.mRepeatCount;
329 mDownTime = from.mDownTime;
330 mEventTime = from.mEventTime;
331}
332
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700333const char* KeyEvent::actionToString(int32_t action) {
334 // Convert KeyEvent action to string
335 switch (action) {
336 case AKEY_EVENT_ACTION_DOWN:
337 return "DOWN";
338 case AKEY_EVENT_ACTION_UP:
339 return "UP";
340 case AKEY_EVENT_ACTION_MULTIPLE:
341 return "MULTIPLE";
342 }
343 return "UNKNOWN";
344}
Jeff Brown5912f952013-07-01 19:10:31 -0700345
346// --- PointerCoords ---
347
348float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700349 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700350 return 0;
351 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700352 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700353}
354
355status_t PointerCoords::setAxisValue(int32_t axis, float value) {
356 if (axis < 0 || axis > 63) {
357 return NAME_NOT_FOUND;
358 }
359
Michael Wright38dcdff2014-03-19 12:06:10 -0700360 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
361 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700362 if (value == 0) {
363 return OK; // axes with value 0 do not need to be stored
364 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700365
366 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700367 if (count >= MAX_AXES) {
368 tooManyAxes(axis);
369 return NO_MEMORY;
370 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700371 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700372 for (uint32_t i = count; i > index; i--) {
373 values[i] = values[i - 1];
374 }
375 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700376
Jeff Brown5912f952013-07-01 19:10:31 -0700377 values[index] = value;
378 return OK;
379}
380
381static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
382 float value = c.getAxisValue(axis);
383 if (value != 0) {
384 c.setAxisValue(axis, value * scaleFactor);
385 }
386}
387
Robert Carre07e1032018-11-26 12:55:53 -0800388void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700389 // No need to scale pressure or size since they are normalized.
390 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800391
392 // If there is a global scale factor, it is included in the windowX/YScale
393 // so we don't need to apply it twice to the X/Y axes.
394 // However we don't want to apply any windowXYScale not included in the global scale
395 // to the TOUCH_MAJOR/MINOR coordinates.
396 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
397 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
398 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
399 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
400 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
401 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
Prabir Pradhanc6523582021-05-14 18:02:55 -0700402 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_X, windowXScale);
403 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_Y, windowYScale);
Robert Carre07e1032018-11-26 12:55:53 -0800404}
405
Brett Chabotfaa986c2020-11-04 17:39:36 -0800406#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700407status_t PointerCoords::readFromParcel(Parcel* parcel) {
408 bits = parcel->readInt64();
409
Michael Wright38dcdff2014-03-19 12:06:10 -0700410 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700411 if (count > MAX_AXES) {
412 return BAD_VALUE;
413 }
414
415 for (uint32_t i = 0; i < count; i++) {
416 values[i] = parcel->readFloat();
417 }
Philip Quinnafb31282022-12-20 18:17:55 -0800418
419 isResampled = parcel->readBool();
Jeff Brown5912f952013-07-01 19:10:31 -0700420 return OK;
421}
422
423status_t PointerCoords::writeToParcel(Parcel* parcel) const {
424 parcel->writeInt64(bits);
425
Michael Wright38dcdff2014-03-19 12:06:10 -0700426 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700427 for (uint32_t i = 0; i < count; i++) {
428 parcel->writeFloat(values[i]);
429 }
Philip Quinnafb31282022-12-20 18:17:55 -0800430
431 parcel->writeBool(isResampled);
Jeff Brown5912f952013-07-01 19:10:31 -0700432 return OK;
433}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800434#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700435
436void PointerCoords::tooManyAxes(int axis) {
437 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
438 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
439}
440
441bool PointerCoords::operator==(const PointerCoords& other) const {
442 if (bits != other.bits) {
443 return false;
444 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700445 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700446 for (uint32_t i = 0; i < count; i++) {
447 if (values[i] != other.values[i]) {
448 return false;
449 }
450 }
Philip Quinnafb31282022-12-20 18:17:55 -0800451 if (isResampled != other.isResampled) {
452 return false;
453 }
Jeff Brown5912f952013-07-01 19:10:31 -0700454 return true;
455}
456
chaviwc01e1372020-07-01 12:37:31 -0700457void PointerCoords::transform(const ui::Transform& transform) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700458 const vec2 xy = transform.transform(getXYValue());
459 setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
460 setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
461
Prabir Pradhanc6523582021-05-14 18:02:55 -0700462 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_X) ||
463 BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_Y)) {
464 const ui::Transform rotation(transform.getOrientation());
465 const vec2 relativeXy = rotation.transform(getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
466 getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
467 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
468 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
469 }
470
Prabir Pradhan6b384612021-05-14 16:56:25 -0700471 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) {
472 const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
473 setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val));
474 }
chaviwc01e1372020-07-01 12:37:31 -0700475}
Jeff Brown5912f952013-07-01 19:10:31 -0700476
477// --- PointerProperties ---
478
479bool PointerProperties::operator==(const PointerProperties& other) const {
480 return id == other.id
481 && toolType == other.toolType;
482}
483
484void PointerProperties::copyFrom(const PointerProperties& other) {
485 id = other.id;
486 toolType = other.toolType;
487}
488
489
490// --- MotionEvent ---
491
Garfield Tan4cc839f2020-01-24 11:26:14 -0800492void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600493 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
494 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700495 int32_t buttonState, MotionClassification classification,
496 const ui::Transform& transform, float xPrecision, float yPrecision,
Evan Rosky84f07f02021-04-16 10:42:42 -0700497 float rawXCursorPosition, float rawYCursorPosition,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700498 const ui::Transform& rawTransform, nsecs_t downTime, nsecs_t eventTime,
Evan Rosky09576692021-07-01 12:22:09 -0700499 size_t pointerCount, const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700500 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800501 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700502 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100503 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700504 mFlags = flags;
505 mEdgeFlags = edgeFlags;
506 mMetaState = metaState;
507 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800508 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700509 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700510 mXPrecision = xPrecision;
511 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700512 mRawXCursorPosition = rawXCursorPosition;
513 mRawYCursorPosition = rawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700514 mRawTransform = rawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700515 mDownTime = downTime;
516 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800517 mPointerProperties.insert(mPointerProperties.end(), &pointerProperties[0],
518 &pointerProperties[pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700519 mSampleEventTimes.clear();
520 mSamplePointerCoords.clear();
521 addSample(eventTime, pointerCoords);
522}
523
524void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800525 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
526 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700527 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100528 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700529 mFlags = other->mFlags;
530 mEdgeFlags = other->mEdgeFlags;
531 mMetaState = other->mMetaState;
532 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800533 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700534 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700535 mXPrecision = other->mXPrecision;
536 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700537 mRawXCursorPosition = other->mRawXCursorPosition;
538 mRawYCursorPosition = other->mRawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700539 mRawTransform = other->mRawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700540 mDownTime = other->mDownTime;
541 mPointerProperties = other->mPointerProperties;
542
543 if (keepHistory) {
544 mSampleEventTimes = other->mSampleEventTimes;
545 mSamplePointerCoords = other->mSamplePointerCoords;
546 } else {
547 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500548 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700549 mSamplePointerCoords.clear();
550 size_t pointerCount = other->getPointerCount();
551 size_t historySize = other->getHistorySize();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800552 mSamplePointerCoords
553 .insert(mSamplePointerCoords.end(),
554 &other->mSamplePointerCoords[historySize * pointerCount],
555 &other->mSamplePointerCoords[historySize * pointerCount + pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700556 }
557}
558
559void MotionEvent::addSample(
560 int64_t eventTime,
561 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500562 mSampleEventTimes.push_back(eventTime);
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800563 mSamplePointerCoords.insert(mSamplePointerCoords.end(), &pointerCoords[0],
564 &pointerCoords[getPointerCount()]);
Jeff Brown5912f952013-07-01 19:10:31 -0700565}
566
Michael Wright635422b2022-12-02 00:43:56 +0000567std::optional<ui::Rotation> MotionEvent::getSurfaceRotation() const {
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800568 // The surface rotation is the rotation from the window's coordinate space to that of the
569 // display. Since the event's transform takes display space coordinates to window space, the
570 // returned surface rotation is the inverse of the rotation for the surface.
571 switch (mTransform.getOrientation()) {
572 case ui::Transform::ROT_0:
Michael Wright635422b2022-12-02 00:43:56 +0000573 return ui::ROTATION_0;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800574 case ui::Transform::ROT_90:
Michael Wright635422b2022-12-02 00:43:56 +0000575 return ui::ROTATION_270;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800576 case ui::Transform::ROT_180:
Michael Wright635422b2022-12-02 00:43:56 +0000577 return ui::ROTATION_180;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800578 case ui::Transform::ROT_270:
Michael Wright635422b2022-12-02 00:43:56 +0000579 return ui::ROTATION_90;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800580 default:
Michael Wright635422b2022-12-02 00:43:56 +0000581 return std::nullopt;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800582 }
583}
584
Garfield Tan00f511d2019-06-12 16:55:40 -0700585float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700586 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
587 return vals.x;
Garfield Tan00f511d2019-06-12 16:55:40 -0700588}
589
590float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700591 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
592 return vals.y;
Garfield Tan00f511d2019-06-12 16:55:40 -0700593}
594
Garfield Tan937bb832019-07-25 17:48:31 -0700595void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700596 ui::Transform inverse = mTransform.inverse();
597 vec2 vals = inverse.transform(x, y);
598 mRawXCursorPosition = vals.x;
599 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700600}
601
Jeff Brown5912f952013-07-01 19:10:31 -0700602const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000603 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
604 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
605 }
606 const size_t position = getHistorySize() * getPointerCount() + pointerIndex;
607 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
608 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
609 }
610 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700611}
612
613float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
Evan Rosky84f07f02021-04-16 10:42:42 -0700614 return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700615}
616
617float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700618 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700619}
620
621const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
622 size_t pointerIndex, size_t historicalIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000623 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
624 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
625 }
626 if (CC_UNLIKELY(historicalIndex < 0 || historicalIndex > getHistorySize())) {
627 LOG(FATAL) << __func__ << ": Invalid historical index " << historicalIndex << " for "
628 << *this;
629 }
630 const size_t position = historicalIndex * getPointerCount() + pointerIndex;
631 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
632 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
633 }
634 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700635}
636
637float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan6b384612021-05-14 16:56:25 -0700638 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700639 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
640 return calculateTransformedAxisValue(axis, mSource, mRawTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700641}
642
643float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan9f388812021-05-13 16:54:53 -0700644 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700645 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
646 return calculateTransformedAxisValue(axis, mSource, mTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700647}
648
649ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
650 size_t pointerCount = mPointerProperties.size();
651 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800652 if (mPointerProperties[i].id == pointerId) {
Jeff Brown5912f952013-07-01 19:10:31 -0700653 return i;
654 }
655 }
656 return -1;
657}
658
659void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700660 float currXOffset = mTransform.tx();
661 float currYOffset = mTransform.ty();
662 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700663}
664
Robert Carre07e1032018-11-26 12:55:53 -0800665void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700666 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700667 mRawTransform.set(mRawTransform.tx() * globalScaleFactor,
668 mRawTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800669 mXPrecision *= globalScaleFactor;
670 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700671
672 size_t numSamples = mSamplePointerCoords.size();
673 for (size_t i = 0; i < numSamples; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800674 mSamplePointerCoords[i].scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700675 }
676}
677
chaviw9eaa22c2020-07-01 16:21:27 -0700678void MotionEvent::transform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700679 // We want to preserve the raw axes values stored in the PointerCoords, so we just update the
680 // transform using the values passed in.
chaviw9eaa22c2020-07-01 16:21:27 -0700681 ui::Transform newTransform;
682 newTransform.set(matrix);
683 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700684}
685
Evan Roskyd4d4d802021-05-03 20:12:21 -0700686void MotionEvent::applyTransform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700687 ui::Transform transform;
688 transform.set(matrix);
Evan Roskyd4d4d802021-05-03 20:12:21 -0700689
690 // Apply the transformation to all samples.
Prabir Pradhan6b384612021-05-14 16:56:25 -0700691 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
692 [&transform](PointerCoords& c) { c.transform(transform); });
Prabir Pradhan4b19bd02021-06-01 17:34:59 -0700693
694 if (mRawXCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION &&
695 mRawYCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) {
696 const vec2 cursor = transform.transform(mRawXCursorPosition, mRawYCursorPosition);
697 mRawXCursorPosition = cursor.x;
698 mRawYCursorPosition = cursor.y;
699 }
Evan Roskyd4d4d802021-05-03 20:12:21 -0700700}
701
Brett Chabotfaa986c2020-11-04 17:39:36 -0800702#ifdef __linux__
chaviw9eaa22c2020-07-01 16:21:27 -0700703static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
704 float dsdx, dtdx, tx, dtdy, dsdy, ty;
705 status_t status = parcel.readFloat(&dsdx);
706 status |= parcel.readFloat(&dtdx);
707 status |= parcel.readFloat(&tx);
708 status |= parcel.readFloat(&dtdy);
709 status |= parcel.readFloat(&dsdy);
710 status |= parcel.readFloat(&ty);
711
712 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
713 return status;
714}
715
716static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
717 status_t status = parcel.writeFloat(transform.dsdx());
718 status |= parcel.writeFloat(transform.dtdx());
719 status |= parcel.writeFloat(transform.tx());
720 status |= parcel.writeFloat(transform.dtdy());
721 status |= parcel.writeFloat(transform.dsdy());
722 status |= parcel.writeFloat(transform.ty());
723 return status;
724}
725
Jeff Brown5912f952013-07-01 19:10:31 -0700726status_t MotionEvent::readFromParcel(Parcel* parcel) {
727 size_t pointerCount = parcel->readInt32();
728 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800729 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
730 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700731 return BAD_VALUE;
732 }
733
Garfield Tan4cc839f2020-01-24 11:26:14 -0800734 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700735 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600736 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800737 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600738 std::vector<uint8_t> hmac;
739 status_t result = parcel->readByteVector(&hmac);
740 if (result != OK || hmac.size() != 32) {
741 return BAD_VALUE;
742 }
743 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700744 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100745 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700746 mFlags = parcel->readInt32();
747 mEdgeFlags = parcel->readInt32();
748 mMetaState = parcel->readInt32();
749 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800750 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700751
752 result = android::readFromParcel(mTransform, *parcel);
753 if (result != OK) {
754 return result;
755 }
Jeff Brown5912f952013-07-01 19:10:31 -0700756 mXPrecision = parcel->readFloat();
757 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700758 mRawXCursorPosition = parcel->readFloat();
759 mRawYCursorPosition = parcel->readFloat();
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700760
761 result = android::readFromParcel(mRawTransform, *parcel);
762 if (result != OK) {
763 return result;
764 }
Jeff Brown5912f952013-07-01 19:10:31 -0700765 mDownTime = parcel->readInt64();
766
767 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800768 mPointerProperties.reserve(pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700769 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500770 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700771 mSamplePointerCoords.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800772 mSamplePointerCoords.reserve(sampleCount * pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700773
774 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800775 mPointerProperties.push_back({});
776 PointerProperties& properties = mPointerProperties.back();
Jeff Brown5912f952013-07-01 19:10:31 -0700777 properties.id = parcel->readInt32();
778 properties.toolType = parcel->readInt32();
779 }
780
Dan Austinc94fc452015-09-22 14:22:41 -0700781 while (sampleCount > 0) {
782 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500783 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700784 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800785 mSamplePointerCoords.push_back({});
786 status_t status = mSamplePointerCoords.back().readFromParcel(parcel);
Jeff Brown5912f952013-07-01 19:10:31 -0700787 if (status) {
788 return status;
789 }
790 }
791 }
792 return OK;
793}
794
795status_t MotionEvent::writeToParcel(Parcel* parcel) const {
796 size_t pointerCount = mPointerProperties.size();
797 size_t sampleCount = mSampleEventTimes.size();
798
799 parcel->writeInt32(pointerCount);
800 parcel->writeInt32(sampleCount);
801
Garfield Tan4cc839f2020-01-24 11:26:14 -0800802 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700803 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600804 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800805 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600806 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
807 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700808 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100809 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700810 parcel->writeInt32(mFlags);
811 parcel->writeInt32(mEdgeFlags);
812 parcel->writeInt32(mMetaState);
813 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800814 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700815
816 status_t result = android::writeToParcel(mTransform, *parcel);
817 if (result != OK) {
818 return result;
819 }
Jeff Brown5912f952013-07-01 19:10:31 -0700820 parcel->writeFloat(mXPrecision);
821 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700822 parcel->writeFloat(mRawXCursorPosition);
823 parcel->writeFloat(mRawYCursorPosition);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700824
825 result = android::writeToParcel(mRawTransform, *parcel);
826 if (result != OK) {
827 return result;
828 }
Jeff Brown5912f952013-07-01 19:10:31 -0700829 parcel->writeInt64(mDownTime);
830
831 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800832 const PointerProperties& properties = mPointerProperties[i];
Jeff Brown5912f952013-07-01 19:10:31 -0700833 parcel->writeInt32(properties.id);
834 parcel->writeInt32(properties.toolType);
835 }
836
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800837 const PointerCoords* pc = mSamplePointerCoords.data();
Jeff Brown5912f952013-07-01 19:10:31 -0700838 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500839 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700840 for (size_t i = 0; i < pointerCount; i++) {
841 status_t status = (pc++)->writeToParcel(parcel);
842 if (status) {
843 return status;
844 }
845 }
846 }
847 return OK;
848}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800849#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700850
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600851bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700852 if (isFromSource(source, AINPUT_SOURCE_CLASS_POINTER)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700853 // Specifically excludes HOVER_MOVE and SCROLL.
854 switch (action & AMOTION_EVENT_ACTION_MASK) {
855 case AMOTION_EVENT_ACTION_DOWN:
856 case AMOTION_EVENT_ACTION_MOVE:
857 case AMOTION_EVENT_ACTION_UP:
858 case AMOTION_EVENT_ACTION_POINTER_DOWN:
859 case AMOTION_EVENT_ACTION_POINTER_UP:
860 case AMOTION_EVENT_ACTION_CANCEL:
861 case AMOTION_EVENT_ACTION_OUTSIDE:
862 return true;
863 }
864 }
865 return false;
866}
867
Michael Wright872db4f2014-04-22 15:03:51 -0700868const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700869 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700870}
871
872int32_t MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700873 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700874}
875
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500876std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700877 // Convert MotionEvent action to string
878 switch (action & AMOTION_EVENT_ACTION_MASK) {
879 case AMOTION_EVENT_ACTION_DOWN:
880 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700881 case AMOTION_EVENT_ACTION_UP:
882 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500883 case AMOTION_EVENT_ACTION_MOVE:
884 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700885 case AMOTION_EVENT_ACTION_CANCEL:
886 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500887 case AMOTION_EVENT_ACTION_OUTSIDE:
888 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700889 case AMOTION_EVENT_ACTION_POINTER_DOWN:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000890 return StringPrintf("POINTER_DOWN(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700891 case AMOTION_EVENT_ACTION_POINTER_UP:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000892 return StringPrintf("POINTER_UP(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500893 case AMOTION_EVENT_ACTION_HOVER_MOVE:
894 return "HOVER_MOVE";
895 case AMOTION_EVENT_ACTION_SCROLL:
896 return "SCROLL";
897 case AMOTION_EVENT_ACTION_HOVER_ENTER:
898 return "HOVER_ENTER";
899 case AMOTION_EVENT_ACTION_HOVER_EXIT:
900 return "HOVER_EXIT";
901 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
902 return "BUTTON_PRESS";
903 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
904 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700905 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500906 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700907}
908
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700909// Apply the given transformation to the point without checking whether the entire transform
910// should be disregarded altogether for the provided source.
911static inline vec2 calculateTransformedXYUnchecked(uint32_t source, const ui::Transform& transform,
912 const vec2& xy) {
913 return shouldDisregardOffset(source) ? transformWithoutTranslation(transform, xy)
914 : transform.transform(xy);
915}
916
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700917vec2 MotionEvent::calculateTransformedXY(uint32_t source, const ui::Transform& transform,
918 const vec2& xy) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700919 if (shouldDisregardTransformation(source)) {
920 return xy;
921 }
922 return calculateTransformedXYUnchecked(source, transform, xy);
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700923}
924
Prabir Pradhan8e6ce222022-02-24 09:08:54 -0800925// Keep in sync with calculateTransformedCoords.
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700926float MotionEvent::calculateTransformedAxisValue(int32_t axis, uint32_t source,
927 const ui::Transform& transform,
928 const PointerCoords& coords) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700929 if (shouldDisregardTransformation(source)) {
930 return coords.getAxisValue(axis);
931 }
932
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700933 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700934 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700935 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
936 return xy[axis];
937 }
938
939 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
940 const vec2 relativeXy =
941 transformWithoutTranslation(transform,
942 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
943 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
944 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
945 }
946
947 if (axis == AMOTION_EVENT_AXIS_ORIENTATION) {
948 return transformAngle(transform, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION));
949 }
950
951 return coords.getAxisValue(axis);
952}
953
Prabir Pradhan8e6ce222022-02-24 09:08:54 -0800954// Keep in sync with calculateTransformedAxisValue. This is an optimization of
955// calculateTransformedAxisValue for all PointerCoords axes.
956PointerCoords MotionEvent::calculateTransformedCoords(uint32_t source,
957 const ui::Transform& transform,
958 const PointerCoords& coords) {
959 if (shouldDisregardTransformation(source)) {
960 return coords;
961 }
962 PointerCoords out = coords;
963
964 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
965 out.setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
966 out.setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
967
968 const vec2 relativeXy =
969 transformWithoutTranslation(transform,
970 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
971 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
972 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
973 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
974
975 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
976 transformAngle(transform,
977 coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)));
978
979 return out;
980}
981
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000982std::ostream& operator<<(std::ostream& out, const MotionEvent& event) {
983 out << "MotionEvent { action=" << MotionEvent::actionToString(event.getAction());
984 if (event.getActionButton() != 0) {
985 out << ", actionButton=" << std::to_string(event.getActionButton());
986 }
987 const size_t pointerCount = event.getPointerCount();
hupeng3aa5a51a2022-09-02 16:00:18 +0800988 LOG_ALWAYS_FATAL_IF(pointerCount > MAX_POINTERS, "Too many pointers : pointerCount = %zu",
989 pointerCount);
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000990 for (size_t i = 0; i < pointerCount; i++) {
991 out << ", id[" << i << "]=" << event.getPointerId(i);
992 float x = event.getX(i);
993 float y = event.getY(i);
994 if (x != 0 || y != 0) {
995 out << ", x[" << i << "]=" << x;
996 out << ", y[" << i << "]=" << y;
997 }
998 int toolType = event.getToolType(i);
999 if (toolType != AMOTION_EVENT_TOOL_TYPE_FINGER) {
1000 out << ", toolType[" << i << "]=" << toolType;
1001 }
1002 }
1003 if (event.getButtonState() != 0) {
1004 out << ", buttonState=" << event.getButtonState();
1005 }
1006 if (event.getClassification() != MotionClassification::NONE) {
1007 out << ", classification=" << motionClassificationToString(event.getClassification());
1008 }
1009 if (event.getMetaState() != 0) {
1010 out << ", metaState=" << event.getMetaState();
1011 }
1012 if (event.getEdgeFlags() != 0) {
1013 out << ", edgeFlags=" << event.getEdgeFlags();
1014 }
1015 if (pointerCount != 1) {
1016 out << ", pointerCount=" << pointerCount;
1017 }
1018 if (event.getHistorySize() != 0) {
1019 out << ", historySize=" << event.getHistorySize();
1020 }
1021 out << ", eventTime=" << event.getEventTime();
1022 out << ", downTime=" << event.getDownTime();
1023 out << ", deviceId=" << event.getDeviceId();
1024 out << ", source=" << inputEventSourceToString(event.getSource());
1025 out << ", displayId=" << event.getDisplayId();
1026 out << ", eventId=" << event.getId();
1027 out << "}";
1028 return out;
1029}
1030
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001031// --- FocusEvent ---
1032
Antonio Kantek3cfec7b2021-11-05 18:26:17 -07001033void FocusEvent::initialize(int32_t id, bool hasFocus) {
Garfield Tan4cc839f2020-01-24 11:26:14 -08001034 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -06001035 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001036 mHasFocus = hasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001037}
1038
1039void FocusEvent::initialize(const FocusEvent& from) {
1040 InputEvent::initialize(from);
1041 mHasFocus = from.mHasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001042}
Jeff Brown5912f952013-07-01 19:10:31 -07001043
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001044// --- CaptureEvent ---
1045
1046void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) {
1047 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1048 ADISPLAY_ID_NONE, INVALID_HMAC);
1049 mPointerCaptureEnabled = pointerCaptureEnabled;
1050}
1051
1052void CaptureEvent::initialize(const CaptureEvent& from) {
1053 InputEvent::initialize(from);
1054 mPointerCaptureEnabled = from.mPointerCaptureEnabled;
1055}
1056
arthurhung7632c332020-12-30 16:58:01 +08001057// --- DragEvent ---
1058
1059void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) {
1060 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1061 ADISPLAY_ID_NONE, INVALID_HMAC);
1062 mIsExiting = isExiting;
1063 mX = x;
1064 mY = y;
1065}
1066
1067void DragEvent::initialize(const DragEvent& from) {
1068 InputEvent::initialize(from);
1069 mIsExiting = from.mIsExiting;
1070 mX = from.mX;
1071 mY = from.mY;
1072}
1073
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001074// --- TouchModeEvent ---
1075
1076void TouchModeEvent::initialize(int32_t id, bool isInTouchMode) {
1077 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1078 ADISPLAY_ID_NONE, INVALID_HMAC);
1079 mIsInTouchMode = isInTouchMode;
1080}
1081
1082void TouchModeEvent::initialize(const TouchModeEvent& from) {
1083 InputEvent::initialize(from);
1084 mIsInTouchMode = from.mIsInTouchMode;
1085}
1086
Jeff Brown5912f952013-07-01 19:10:31 -07001087// --- PooledInputEventFactory ---
1088
1089PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
1090 mMaxPoolSize(maxPoolSize) {
1091}
1092
1093PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -07001094}
1095
1096KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001097 if (mKeyEventPool.empty()) {
1098 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001099 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001100 KeyEvent* event = mKeyEventPool.front().release();
1101 mKeyEventPool.pop();
1102 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001103}
1104
1105MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001106 if (mMotionEventPool.empty()) {
1107 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001108 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001109 MotionEvent* event = mMotionEventPool.front().release();
1110 mMotionEventPool.pop();
1111 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001112}
1113
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001114FocusEvent* PooledInputEventFactory::createFocusEvent() {
1115 if (mFocusEventPool.empty()) {
1116 return new FocusEvent();
1117 }
1118 FocusEvent* event = mFocusEventPool.front().release();
1119 mFocusEventPool.pop();
1120 return event;
1121}
1122
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001123CaptureEvent* PooledInputEventFactory::createCaptureEvent() {
1124 if (mCaptureEventPool.empty()) {
1125 return new CaptureEvent();
1126 }
1127 CaptureEvent* event = mCaptureEventPool.front().release();
1128 mCaptureEventPool.pop();
1129 return event;
1130}
1131
arthurhung7632c332020-12-30 16:58:01 +08001132DragEvent* PooledInputEventFactory::createDragEvent() {
1133 if (mDragEventPool.empty()) {
1134 return new DragEvent();
1135 }
1136 DragEvent* event = mDragEventPool.front().release();
1137 mDragEventPool.pop();
1138 return event;
1139}
1140
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001141TouchModeEvent* PooledInputEventFactory::createTouchModeEvent() {
1142 if (mTouchModeEventPool.empty()) {
1143 return new TouchModeEvent();
1144 }
1145 TouchModeEvent* event = mTouchModeEventPool.front().release();
1146 mTouchModeEventPool.pop();
1147 return event;
1148}
1149
Jeff Brown5912f952013-07-01 19:10:31 -07001150void PooledInputEventFactory::recycle(InputEvent* event) {
1151 switch (event->getType()) {
1152 case AINPUT_EVENT_TYPE_KEY:
1153 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001154 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -07001155 return;
1156 }
1157 break;
1158 case AINPUT_EVENT_TYPE_MOTION:
1159 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001160 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -07001161 return;
1162 }
1163 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001164 case AINPUT_EVENT_TYPE_FOCUS:
1165 if (mFocusEventPool.size() < mMaxPoolSize) {
1166 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
1167 return;
1168 }
1169 break;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001170 case AINPUT_EVENT_TYPE_CAPTURE:
1171 if (mCaptureEventPool.size() < mMaxPoolSize) {
1172 mCaptureEventPool.push(
1173 std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event)));
1174 return;
1175 }
1176 break;
arthurhung7632c332020-12-30 16:58:01 +08001177 case AINPUT_EVENT_TYPE_DRAG:
1178 if (mDragEventPool.size() < mMaxPoolSize) {
1179 mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event)));
1180 return;
1181 }
1182 break;
Antonio Kantekeb4a30c2021-09-28 17:49:49 -07001183 case AINPUT_EVENT_TYPE_TOUCH_MODE:
1184 if (mTouchModeEventPool.size() < mMaxPoolSize) {
1185 mTouchModeEventPool.push(
1186 std::unique_ptr<TouchModeEvent>(static_cast<TouchModeEvent*>(event)));
1187 return;
1188 }
1189 break;
Jeff Brown5912f952013-07-01 19:10:31 -07001190 }
1191 delete event;
1192}
1193
1194} // namespace android