blob: c247fdbe07e70e7edc3b76e121f9eb8ef1ac1871 [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 }
418 return OK;
419}
420
421status_t PointerCoords::writeToParcel(Parcel* parcel) const {
422 parcel->writeInt64(bits);
423
Michael Wright38dcdff2014-03-19 12:06:10 -0700424 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700425 for (uint32_t i = 0; i < count; i++) {
426 parcel->writeFloat(values[i]);
427 }
428 return OK;
429}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800430#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700431
432void PointerCoords::tooManyAxes(int axis) {
433 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
434 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
435}
436
437bool PointerCoords::operator==(const PointerCoords& other) const {
438 if (bits != other.bits) {
439 return false;
440 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700441 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700442 for (uint32_t i = 0; i < count; i++) {
443 if (values[i] != other.values[i]) {
444 return false;
445 }
446 }
447 return true;
448}
449
chaviwc01e1372020-07-01 12:37:31 -0700450void PointerCoords::transform(const ui::Transform& transform) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700451 const vec2 xy = transform.transform(getXYValue());
452 setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
453 setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
454
Prabir Pradhanc6523582021-05-14 18:02:55 -0700455 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_X) ||
456 BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_Y)) {
457 const ui::Transform rotation(transform.getOrientation());
458 const vec2 relativeXy = rotation.transform(getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
459 getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
460 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
461 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
462 }
463
Prabir Pradhan6b384612021-05-14 16:56:25 -0700464 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) {
465 const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
466 setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val));
467 }
chaviwc01e1372020-07-01 12:37:31 -0700468}
Jeff Brown5912f952013-07-01 19:10:31 -0700469
470// --- PointerProperties ---
471
472bool PointerProperties::operator==(const PointerProperties& other) const {
473 return id == other.id
474 && toolType == other.toolType;
475}
476
477void PointerProperties::copyFrom(const PointerProperties& other) {
478 id = other.id;
479 toolType = other.toolType;
480}
481
482
483// --- MotionEvent ---
484
Garfield Tan4cc839f2020-01-24 11:26:14 -0800485void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600486 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
487 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700488 int32_t buttonState, MotionClassification classification,
489 const ui::Transform& transform, float xPrecision, float yPrecision,
Evan Rosky84f07f02021-04-16 10:42:42 -0700490 float rawXCursorPosition, float rawYCursorPosition,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700491 const ui::Transform& rawTransform, nsecs_t downTime, nsecs_t eventTime,
Evan Rosky09576692021-07-01 12:22:09 -0700492 size_t pointerCount, const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700493 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800494 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700495 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100496 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700497 mFlags = flags;
498 mEdgeFlags = edgeFlags;
499 mMetaState = metaState;
500 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800501 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700502 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700503 mXPrecision = xPrecision;
504 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700505 mRawXCursorPosition = rawXCursorPosition;
506 mRawYCursorPosition = rawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700507 mRawTransform = rawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700508 mDownTime = downTime;
509 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800510 mPointerProperties.insert(mPointerProperties.end(), &pointerProperties[0],
511 &pointerProperties[pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700512 mSampleEventTimes.clear();
513 mSamplePointerCoords.clear();
514 addSample(eventTime, pointerCoords);
515}
516
517void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800518 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
519 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700520 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100521 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700522 mFlags = other->mFlags;
523 mEdgeFlags = other->mEdgeFlags;
524 mMetaState = other->mMetaState;
525 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800526 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700527 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700528 mXPrecision = other->mXPrecision;
529 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700530 mRawXCursorPosition = other->mRawXCursorPosition;
531 mRawYCursorPosition = other->mRawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700532 mRawTransform = other->mRawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700533 mDownTime = other->mDownTime;
534 mPointerProperties = other->mPointerProperties;
535
536 if (keepHistory) {
537 mSampleEventTimes = other->mSampleEventTimes;
538 mSamplePointerCoords = other->mSamplePointerCoords;
539 } else {
540 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500541 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700542 mSamplePointerCoords.clear();
543 size_t pointerCount = other->getPointerCount();
544 size_t historySize = other->getHistorySize();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800545 mSamplePointerCoords
546 .insert(mSamplePointerCoords.end(),
547 &other->mSamplePointerCoords[historySize * pointerCount],
548 &other->mSamplePointerCoords[historySize * pointerCount + pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700549 }
550}
551
552void MotionEvent::addSample(
553 int64_t eventTime,
554 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500555 mSampleEventTimes.push_back(eventTime);
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800556 mSamplePointerCoords.insert(mSamplePointerCoords.end(), &pointerCoords[0],
557 &pointerCoords[getPointerCount()]);
Jeff Brown5912f952013-07-01 19:10:31 -0700558}
559
Michael Wright635422b2022-12-02 00:43:56 +0000560std::optional<ui::Rotation> MotionEvent::getSurfaceRotation() const {
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800561 // The surface rotation is the rotation from the window's coordinate space to that of the
562 // display. Since the event's transform takes display space coordinates to window space, the
563 // returned surface rotation is the inverse of the rotation for the surface.
564 switch (mTransform.getOrientation()) {
565 case ui::Transform::ROT_0:
Michael Wright635422b2022-12-02 00:43:56 +0000566 return ui::ROTATION_0;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800567 case ui::Transform::ROT_90:
Michael Wright635422b2022-12-02 00:43:56 +0000568 return ui::ROTATION_270;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800569 case ui::Transform::ROT_180:
Michael Wright635422b2022-12-02 00:43:56 +0000570 return ui::ROTATION_180;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800571 case ui::Transform::ROT_270:
Michael Wright635422b2022-12-02 00:43:56 +0000572 return ui::ROTATION_90;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800573 default:
Michael Wright635422b2022-12-02 00:43:56 +0000574 return std::nullopt;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800575 }
576}
577
Garfield Tan00f511d2019-06-12 16:55:40 -0700578float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700579 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
580 return vals.x;
Garfield Tan00f511d2019-06-12 16:55:40 -0700581}
582
583float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700584 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
585 return vals.y;
Garfield Tan00f511d2019-06-12 16:55:40 -0700586}
587
Garfield Tan937bb832019-07-25 17:48:31 -0700588void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700589 ui::Transform inverse = mTransform.inverse();
590 vec2 vals = inverse.transform(x, y);
591 mRawXCursorPosition = vals.x;
592 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700593}
594
Jeff Brown5912f952013-07-01 19:10:31 -0700595const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000596 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
597 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
598 }
599 const size_t position = getHistorySize() * getPointerCount() + pointerIndex;
600 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
601 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
602 }
603 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700604}
605
606float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
Evan Rosky84f07f02021-04-16 10:42:42 -0700607 return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700608}
609
610float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700611 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700612}
613
614const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
615 size_t pointerIndex, size_t historicalIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000616 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
617 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
618 }
619 if (CC_UNLIKELY(historicalIndex < 0 || historicalIndex > getHistorySize())) {
620 LOG(FATAL) << __func__ << ": Invalid historical index " << historicalIndex << " for "
621 << *this;
622 }
623 const size_t position = historicalIndex * getPointerCount() + pointerIndex;
624 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
625 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
626 }
627 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700628}
629
630float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan6b384612021-05-14 16:56:25 -0700631 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700632 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
633 return calculateTransformedAxisValue(axis, mSource, mRawTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700634}
635
636float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan9f388812021-05-13 16:54:53 -0700637 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700638 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
639 return calculateTransformedAxisValue(axis, mSource, mTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700640}
641
642ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
643 size_t pointerCount = mPointerProperties.size();
644 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800645 if (mPointerProperties[i].id == pointerId) {
Jeff Brown5912f952013-07-01 19:10:31 -0700646 return i;
647 }
648 }
649 return -1;
650}
651
652void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700653 float currXOffset = mTransform.tx();
654 float currYOffset = mTransform.ty();
655 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700656}
657
Robert Carre07e1032018-11-26 12:55:53 -0800658void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700659 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700660 mRawTransform.set(mRawTransform.tx() * globalScaleFactor,
661 mRawTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800662 mXPrecision *= globalScaleFactor;
663 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700664
665 size_t numSamples = mSamplePointerCoords.size();
666 for (size_t i = 0; i < numSamples; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800667 mSamplePointerCoords[i].scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700668 }
669}
670
chaviw9eaa22c2020-07-01 16:21:27 -0700671void MotionEvent::transform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700672 // We want to preserve the raw axes values stored in the PointerCoords, so we just update the
673 // transform using the values passed in.
chaviw9eaa22c2020-07-01 16:21:27 -0700674 ui::Transform newTransform;
675 newTransform.set(matrix);
676 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700677}
678
Evan Roskyd4d4d802021-05-03 20:12:21 -0700679void MotionEvent::applyTransform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700680 ui::Transform transform;
681 transform.set(matrix);
Evan Roskyd4d4d802021-05-03 20:12:21 -0700682
683 // Apply the transformation to all samples.
Prabir Pradhan6b384612021-05-14 16:56:25 -0700684 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
685 [&transform](PointerCoords& c) { c.transform(transform); });
Prabir Pradhan4b19bd02021-06-01 17:34:59 -0700686
687 if (mRawXCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION &&
688 mRawYCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) {
689 const vec2 cursor = transform.transform(mRawXCursorPosition, mRawYCursorPosition);
690 mRawXCursorPosition = cursor.x;
691 mRawYCursorPosition = cursor.y;
692 }
Evan Roskyd4d4d802021-05-03 20:12:21 -0700693}
694
Brett Chabotfaa986c2020-11-04 17:39:36 -0800695#ifdef __linux__
chaviw9eaa22c2020-07-01 16:21:27 -0700696static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
697 float dsdx, dtdx, tx, dtdy, dsdy, ty;
698 status_t status = parcel.readFloat(&dsdx);
699 status |= parcel.readFloat(&dtdx);
700 status |= parcel.readFloat(&tx);
701 status |= parcel.readFloat(&dtdy);
702 status |= parcel.readFloat(&dsdy);
703 status |= parcel.readFloat(&ty);
704
705 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
706 return status;
707}
708
709static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
710 status_t status = parcel.writeFloat(transform.dsdx());
711 status |= parcel.writeFloat(transform.dtdx());
712 status |= parcel.writeFloat(transform.tx());
713 status |= parcel.writeFloat(transform.dtdy());
714 status |= parcel.writeFloat(transform.dsdy());
715 status |= parcel.writeFloat(transform.ty());
716 return status;
717}
718
Jeff Brown5912f952013-07-01 19:10:31 -0700719status_t MotionEvent::readFromParcel(Parcel* parcel) {
720 size_t pointerCount = parcel->readInt32();
721 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800722 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
723 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700724 return BAD_VALUE;
725 }
726
Garfield Tan4cc839f2020-01-24 11:26:14 -0800727 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700728 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600729 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800730 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600731 std::vector<uint8_t> hmac;
732 status_t result = parcel->readByteVector(&hmac);
733 if (result != OK || hmac.size() != 32) {
734 return BAD_VALUE;
735 }
736 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700737 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100738 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700739 mFlags = parcel->readInt32();
740 mEdgeFlags = parcel->readInt32();
741 mMetaState = parcel->readInt32();
742 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800743 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700744
745 result = android::readFromParcel(mTransform, *parcel);
746 if (result != OK) {
747 return result;
748 }
Jeff Brown5912f952013-07-01 19:10:31 -0700749 mXPrecision = parcel->readFloat();
750 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700751 mRawXCursorPosition = parcel->readFloat();
752 mRawYCursorPosition = parcel->readFloat();
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700753
754 result = android::readFromParcel(mRawTransform, *parcel);
755 if (result != OK) {
756 return result;
757 }
Jeff Brown5912f952013-07-01 19:10:31 -0700758 mDownTime = parcel->readInt64();
759
760 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800761 mPointerProperties.reserve(pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700762 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500763 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700764 mSamplePointerCoords.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800765 mSamplePointerCoords.reserve(sampleCount * pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700766
767 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800768 mPointerProperties.push_back({});
769 PointerProperties& properties = mPointerProperties.back();
Jeff Brown5912f952013-07-01 19:10:31 -0700770 properties.id = parcel->readInt32();
771 properties.toolType = parcel->readInt32();
772 }
773
Dan Austinc94fc452015-09-22 14:22:41 -0700774 while (sampleCount > 0) {
775 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500776 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700777 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800778 mSamplePointerCoords.push_back({});
779 status_t status = mSamplePointerCoords.back().readFromParcel(parcel);
Jeff Brown5912f952013-07-01 19:10:31 -0700780 if (status) {
781 return status;
782 }
783 }
784 }
785 return OK;
786}
787
788status_t MotionEvent::writeToParcel(Parcel* parcel) const {
789 size_t pointerCount = mPointerProperties.size();
790 size_t sampleCount = mSampleEventTimes.size();
791
792 parcel->writeInt32(pointerCount);
793 parcel->writeInt32(sampleCount);
794
Garfield Tan4cc839f2020-01-24 11:26:14 -0800795 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700796 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600797 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800798 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600799 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
800 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700801 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100802 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700803 parcel->writeInt32(mFlags);
804 parcel->writeInt32(mEdgeFlags);
805 parcel->writeInt32(mMetaState);
806 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800807 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700808
809 status_t result = android::writeToParcel(mTransform, *parcel);
810 if (result != OK) {
811 return result;
812 }
Jeff Brown5912f952013-07-01 19:10:31 -0700813 parcel->writeFloat(mXPrecision);
814 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700815 parcel->writeFloat(mRawXCursorPosition);
816 parcel->writeFloat(mRawYCursorPosition);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700817
818 result = android::writeToParcel(mRawTransform, *parcel);
819 if (result != OK) {
820 return result;
821 }
Jeff Brown5912f952013-07-01 19:10:31 -0700822 parcel->writeInt64(mDownTime);
823
824 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800825 const PointerProperties& properties = mPointerProperties[i];
Jeff Brown5912f952013-07-01 19:10:31 -0700826 parcel->writeInt32(properties.id);
827 parcel->writeInt32(properties.toolType);
828 }
829
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800830 const PointerCoords* pc = mSamplePointerCoords.data();
Jeff Brown5912f952013-07-01 19:10:31 -0700831 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500832 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700833 for (size_t i = 0; i < pointerCount; i++) {
834 status_t status = (pc++)->writeToParcel(parcel);
835 if (status) {
836 return status;
837 }
838 }
839 }
840 return OK;
841}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800842#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700843
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600844bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700845 if (isFromSource(source, AINPUT_SOURCE_CLASS_POINTER)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700846 // Specifically excludes HOVER_MOVE and SCROLL.
847 switch (action & AMOTION_EVENT_ACTION_MASK) {
848 case AMOTION_EVENT_ACTION_DOWN:
849 case AMOTION_EVENT_ACTION_MOVE:
850 case AMOTION_EVENT_ACTION_UP:
851 case AMOTION_EVENT_ACTION_POINTER_DOWN:
852 case AMOTION_EVENT_ACTION_POINTER_UP:
853 case AMOTION_EVENT_ACTION_CANCEL:
854 case AMOTION_EVENT_ACTION_OUTSIDE:
855 return true;
856 }
857 }
858 return false;
859}
860
Michael Wright872db4f2014-04-22 15:03:51 -0700861const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700862 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700863}
864
865int32_t MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700866 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700867}
868
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500869std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700870 // Convert MotionEvent action to string
871 switch (action & AMOTION_EVENT_ACTION_MASK) {
872 case AMOTION_EVENT_ACTION_DOWN:
873 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700874 case AMOTION_EVENT_ACTION_UP:
875 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500876 case AMOTION_EVENT_ACTION_MOVE:
877 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700878 case AMOTION_EVENT_ACTION_CANCEL:
879 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500880 case AMOTION_EVENT_ACTION_OUTSIDE:
881 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700882 case AMOTION_EVENT_ACTION_POINTER_DOWN:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000883 return StringPrintf("POINTER_DOWN(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700884 case AMOTION_EVENT_ACTION_POINTER_UP:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000885 return StringPrintf("POINTER_UP(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500886 case AMOTION_EVENT_ACTION_HOVER_MOVE:
887 return "HOVER_MOVE";
888 case AMOTION_EVENT_ACTION_SCROLL:
889 return "SCROLL";
890 case AMOTION_EVENT_ACTION_HOVER_ENTER:
891 return "HOVER_ENTER";
892 case AMOTION_EVENT_ACTION_HOVER_EXIT:
893 return "HOVER_EXIT";
894 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
895 return "BUTTON_PRESS";
896 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
897 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700898 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500899 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700900}
901
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700902// Apply the given transformation to the point without checking whether the entire transform
903// should be disregarded altogether for the provided source.
904static inline vec2 calculateTransformedXYUnchecked(uint32_t source, const ui::Transform& transform,
905 const vec2& xy) {
906 return shouldDisregardOffset(source) ? transformWithoutTranslation(transform, xy)
907 : transform.transform(xy);
908}
909
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700910vec2 MotionEvent::calculateTransformedXY(uint32_t source, const ui::Transform& transform,
911 const vec2& xy) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700912 if (shouldDisregardTransformation(source)) {
913 return xy;
914 }
915 return calculateTransformedXYUnchecked(source, transform, xy);
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700916}
917
Prabir Pradhan8e6ce222022-02-24 09:08:54 -0800918// Keep in sync with calculateTransformedCoords.
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700919float MotionEvent::calculateTransformedAxisValue(int32_t axis, uint32_t source,
920 const ui::Transform& transform,
921 const PointerCoords& coords) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700922 if (shouldDisregardTransformation(source)) {
923 return coords.getAxisValue(axis);
924 }
925
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700926 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700927 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700928 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
929 return xy[axis];
930 }
931
932 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
933 const vec2 relativeXy =
934 transformWithoutTranslation(transform,
935 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
936 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
937 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
938 }
939
940 if (axis == AMOTION_EVENT_AXIS_ORIENTATION) {
941 return transformAngle(transform, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION));
942 }
943
944 return coords.getAxisValue(axis);
945}
946
Prabir Pradhan8e6ce222022-02-24 09:08:54 -0800947// Keep in sync with calculateTransformedAxisValue. This is an optimization of
948// calculateTransformedAxisValue for all PointerCoords axes.
949PointerCoords MotionEvent::calculateTransformedCoords(uint32_t source,
950 const ui::Transform& transform,
951 const PointerCoords& coords) {
952 if (shouldDisregardTransformation(source)) {
953 return coords;
954 }
955 PointerCoords out = coords;
956
957 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
958 out.setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
959 out.setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
960
961 const vec2 relativeXy =
962 transformWithoutTranslation(transform,
963 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
964 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
965 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
966 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
967
968 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
969 transformAngle(transform,
970 coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)));
971
972 return out;
973}
974
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000975std::ostream& operator<<(std::ostream& out, const MotionEvent& event) {
976 out << "MotionEvent { action=" << MotionEvent::actionToString(event.getAction());
977 if (event.getActionButton() != 0) {
978 out << ", actionButton=" << std::to_string(event.getActionButton());
979 }
980 const size_t pointerCount = event.getPointerCount();
hupeng3aa5a51a2022-09-02 16:00:18 +0800981 LOG_ALWAYS_FATAL_IF(pointerCount > MAX_POINTERS, "Too many pointers : pointerCount = %zu",
982 pointerCount);
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000983 for (size_t i = 0; i < pointerCount; i++) {
984 out << ", id[" << i << "]=" << event.getPointerId(i);
985 float x = event.getX(i);
986 float y = event.getY(i);
987 if (x != 0 || y != 0) {
988 out << ", x[" << i << "]=" << x;
989 out << ", y[" << i << "]=" << y;
990 }
991 int toolType = event.getToolType(i);
992 if (toolType != AMOTION_EVENT_TOOL_TYPE_FINGER) {
993 out << ", toolType[" << i << "]=" << toolType;
994 }
995 }
996 if (event.getButtonState() != 0) {
997 out << ", buttonState=" << event.getButtonState();
998 }
999 if (event.getClassification() != MotionClassification::NONE) {
1000 out << ", classification=" << motionClassificationToString(event.getClassification());
1001 }
1002 if (event.getMetaState() != 0) {
1003 out << ", metaState=" << event.getMetaState();
1004 }
1005 if (event.getEdgeFlags() != 0) {
1006 out << ", edgeFlags=" << event.getEdgeFlags();
1007 }
1008 if (pointerCount != 1) {
1009 out << ", pointerCount=" << pointerCount;
1010 }
1011 if (event.getHistorySize() != 0) {
1012 out << ", historySize=" << event.getHistorySize();
1013 }
1014 out << ", eventTime=" << event.getEventTime();
1015 out << ", downTime=" << event.getDownTime();
1016 out << ", deviceId=" << event.getDeviceId();
1017 out << ", source=" << inputEventSourceToString(event.getSource());
1018 out << ", displayId=" << event.getDisplayId();
1019 out << ", eventId=" << event.getId();
1020 out << "}";
1021 return out;
1022}
1023
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001024// --- FocusEvent ---
1025
Antonio Kantek3cfec7b2021-11-05 18:26:17 -07001026void FocusEvent::initialize(int32_t id, bool hasFocus) {
Garfield Tan4cc839f2020-01-24 11:26:14 -08001027 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -06001028 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001029 mHasFocus = hasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001030}
1031
1032void FocusEvent::initialize(const FocusEvent& from) {
1033 InputEvent::initialize(from);
1034 mHasFocus = from.mHasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001035}
Jeff Brown5912f952013-07-01 19:10:31 -07001036
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001037// --- CaptureEvent ---
1038
1039void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) {
1040 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1041 ADISPLAY_ID_NONE, INVALID_HMAC);
1042 mPointerCaptureEnabled = pointerCaptureEnabled;
1043}
1044
1045void CaptureEvent::initialize(const CaptureEvent& from) {
1046 InputEvent::initialize(from);
1047 mPointerCaptureEnabled = from.mPointerCaptureEnabled;
1048}
1049
arthurhung7632c332020-12-30 16:58:01 +08001050// --- DragEvent ---
1051
1052void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) {
1053 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1054 ADISPLAY_ID_NONE, INVALID_HMAC);
1055 mIsExiting = isExiting;
1056 mX = x;
1057 mY = y;
1058}
1059
1060void DragEvent::initialize(const DragEvent& from) {
1061 InputEvent::initialize(from);
1062 mIsExiting = from.mIsExiting;
1063 mX = from.mX;
1064 mY = from.mY;
1065}
1066
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001067// --- TouchModeEvent ---
1068
1069void TouchModeEvent::initialize(int32_t id, bool isInTouchMode) {
1070 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1071 ADISPLAY_ID_NONE, INVALID_HMAC);
1072 mIsInTouchMode = isInTouchMode;
1073}
1074
1075void TouchModeEvent::initialize(const TouchModeEvent& from) {
1076 InputEvent::initialize(from);
1077 mIsInTouchMode = from.mIsInTouchMode;
1078}
1079
Jeff Brown5912f952013-07-01 19:10:31 -07001080// --- PooledInputEventFactory ---
1081
1082PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
1083 mMaxPoolSize(maxPoolSize) {
1084}
1085
1086PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -07001087}
1088
1089KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001090 if (mKeyEventPool.empty()) {
1091 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001092 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001093 KeyEvent* event = mKeyEventPool.front().release();
1094 mKeyEventPool.pop();
1095 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001096}
1097
1098MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001099 if (mMotionEventPool.empty()) {
1100 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001101 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001102 MotionEvent* event = mMotionEventPool.front().release();
1103 mMotionEventPool.pop();
1104 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001105}
1106
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001107FocusEvent* PooledInputEventFactory::createFocusEvent() {
1108 if (mFocusEventPool.empty()) {
1109 return new FocusEvent();
1110 }
1111 FocusEvent* event = mFocusEventPool.front().release();
1112 mFocusEventPool.pop();
1113 return event;
1114}
1115
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001116CaptureEvent* PooledInputEventFactory::createCaptureEvent() {
1117 if (mCaptureEventPool.empty()) {
1118 return new CaptureEvent();
1119 }
1120 CaptureEvent* event = mCaptureEventPool.front().release();
1121 mCaptureEventPool.pop();
1122 return event;
1123}
1124
arthurhung7632c332020-12-30 16:58:01 +08001125DragEvent* PooledInputEventFactory::createDragEvent() {
1126 if (mDragEventPool.empty()) {
1127 return new DragEvent();
1128 }
1129 DragEvent* event = mDragEventPool.front().release();
1130 mDragEventPool.pop();
1131 return event;
1132}
1133
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001134TouchModeEvent* PooledInputEventFactory::createTouchModeEvent() {
1135 if (mTouchModeEventPool.empty()) {
1136 return new TouchModeEvent();
1137 }
1138 TouchModeEvent* event = mTouchModeEventPool.front().release();
1139 mTouchModeEventPool.pop();
1140 return event;
1141}
1142
Jeff Brown5912f952013-07-01 19:10:31 -07001143void PooledInputEventFactory::recycle(InputEvent* event) {
1144 switch (event->getType()) {
1145 case AINPUT_EVENT_TYPE_KEY:
1146 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001147 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -07001148 return;
1149 }
1150 break;
1151 case AINPUT_EVENT_TYPE_MOTION:
1152 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001153 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -07001154 return;
1155 }
1156 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001157 case AINPUT_EVENT_TYPE_FOCUS:
1158 if (mFocusEventPool.size() < mMaxPoolSize) {
1159 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
1160 return;
1161 }
1162 break;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001163 case AINPUT_EVENT_TYPE_CAPTURE:
1164 if (mCaptureEventPool.size() < mMaxPoolSize) {
1165 mCaptureEventPool.push(
1166 std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event)));
1167 return;
1168 }
1169 break;
arthurhung7632c332020-12-30 16:58:01 +08001170 case AINPUT_EVENT_TYPE_DRAG:
1171 if (mDragEventPool.size() < mMaxPoolSize) {
1172 mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event)));
1173 return;
1174 }
1175 break;
Antonio Kantekeb4a30c2021-09-28 17:49:49 -07001176 case AINPUT_EVENT_TYPE_TOUCH_MODE:
1177 if (mTouchModeEventPool.size() < mMaxPoolSize) {
1178 mTouchModeEventPool.push(
1179 std::unique_ptr<TouchModeEvent>(static_cast<TouchModeEvent*>(event)));
1180 return;
1181 }
1182 break;
Jeff Brown5912f952013-07-01 19:10:31 -07001183 }
1184 delete event;
1185}
1186
1187} // namespace android