blob: a6f6b14bae6b2a6d03594898a2bf10f121015fd5 [file] [log] [blame]
Jeff Brown5912f952013-07-01 19:10:31 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Input"
18//#define LOG_NDEBUG 0
19
chaviw09c8d2d2020-08-24 15:48:26 -070020#include <attestation/HmacKeyManager.h>
Garfield Tan84b087e2020-01-23 10:49:05 -080021#include <cutils/compiler.h>
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050022#include <inttypes.h>
Garfield Tan84b087e2020-01-23 10:49:05 -080023#include <string.h>
Jeff Brown5912f952013-07-01 19:10:31 -070024
Siarhei Vishniakou31977182022-09-30 08:51:23 -070025#include <android-base/file.h>
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +000026#include <android-base/logging.h>
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050027#include <android-base/stringprintf.h>
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +000028#include <cutils/compiler.h>
chaviw98318de2021-05-19 16:45:23 -050029#include <gui/constants.h>
Prabir Pradhan092f3a92021-11-25 10:53:27 -080030#include <input/DisplayViewport.h>
Jeff Brown5912f952013-07-01 19:10:31 -070031#include <input/Input.h>
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080032#include <input/InputDevice.h>
Michael Wright872db4f2014-04-22 15:03:51 -070033#include <input/InputEventLabels.h>
Jeff Brown5912f952013-07-01 19:10:31 -070034
Brett Chabotfaa986c2020-11-04 17:39:36 -080035#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -070036#include <binder/Parcel.h>
Brett Chabotfaa986c2020-11-04 17:39:36 -080037#endif
Jeff Brown5912f952013-07-01 19:10:31 -070038
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050039using android::base::StringPrintf;
40
Jeff Brown5912f952013-07-01 19:10:31 -070041namespace android {
42
Prabir Pradhan6b384612021-05-14 16:56:25 -070043namespace {
44
45float transformAngle(const ui::Transform& transform, float angleRadians) {
46 // Construct and transform a vector oriented at the specified clockwise angle from vertical.
47 // Coordinate system: down is increasing Y, right is increasing X.
48 float x = sinf(angleRadians);
49 float y = -cosf(angleRadians);
50 vec2 transformedPoint = transform.transform(x, y);
51
52 // Determine how the origin is transformed by the matrix so that we
53 // can transform orientation vectors.
54 const vec2 origin = transform.transform(0, 0);
55
56 transformedPoint.x -= origin.x;
57 transformedPoint.y -= origin.y;
58
59 // Derive the transformed vector's clockwise angle from vertical.
Prabir Pradhand2b02672021-10-19 11:24:45 -070060 // The return value of atan2f is in range [-pi, pi] which conforms to the orientation API.
61 return atan2f(transformedPoint.x, -transformedPoint.y);
Prabir Pradhan6b384612021-05-14 16:56:25 -070062}
63
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070064bool shouldDisregardTransformation(uint32_t source) {
Prabir Pradhan258e2b92022-06-24 18:37:04 +000065 // Do not apply any transformations to axes from joysticks, touchpads, or relative mice.
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070066 return isFromSource(source, AINPUT_SOURCE_CLASS_JOYSTICK) ||
Prabir Pradhan258e2b92022-06-24 18:37:04 +000067 isFromSource(source, AINPUT_SOURCE_CLASS_POSITION) ||
68 isFromSource(source, AINPUT_SOURCE_MOUSE_RELATIVE);
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070069}
70
71bool shouldDisregardOffset(uint32_t source) {
Prabir Pradhan9f388812021-05-13 16:54:53 -070072 // Pointer events are the only type of events that refer to absolute coordinates on the display,
73 // so we should apply the entire window transform. For other types of events, we should make
74 // sure to not apply the window translation/offset.
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070075 return !isFromSource(source, AINPUT_SOURCE_CLASS_POINTER);
Prabir Pradhan9f388812021-05-13 16:54:53 -070076}
77
Prabir Pradhan6b384612021-05-14 16:56:25 -070078} // namespace
79
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -080080const char* motionClassificationToString(MotionClassification classification) {
81 switch (classification) {
82 case MotionClassification::NONE:
83 return "NONE";
84 case MotionClassification::AMBIGUOUS_GESTURE:
85 return "AMBIGUOUS_GESTURE";
86 case MotionClassification::DEEP_PRESS:
87 return "DEEP_PRESS";
Harry Cutts2800fb02022-09-15 13:49:23 +000088 case MotionClassification::TWO_FINGER_SWIPE:
89 return "TWO_FINGER_SWIPE";
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -080090 }
91}
92
Siarhei Vishniakoud5fe5182022-07-20 23:28:40 +000093const char* motionToolTypeToString(int32_t toolType) {
94 switch (toolType) {
95 case AMOTION_EVENT_TOOL_TYPE_UNKNOWN:
96 return "UNKNOWN";
97 case AMOTION_EVENT_TOOL_TYPE_FINGER:
98 return "FINGER";
99 case AMOTION_EVENT_TOOL_TYPE_STYLUS:
100 return "STYLUS";
101 case AMOTION_EVENT_TOOL_TYPE_MOUSE:
102 return "MOUSE";
103 case AMOTION_EVENT_TOOL_TYPE_ERASER:
104 return "ERASER";
105 case AMOTION_EVENT_TOOL_TYPE_PALM:
106 return "PALM";
107 default:
108 return "INVALID";
109 }
110}
111
Garfield Tan84b087e2020-01-23 10:49:05 -0800112// --- IdGenerator ---
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700113
114static status_t getRandomBytes(uint8_t* data, size_t size) {
115 int ret = TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY | O_CLOEXEC | O_NOFOLLOW));
116 if (ret == -1) {
117 return -errno;
118 }
119
120 base::unique_fd fd(ret);
121 if (!base::ReadFully(fd, data, size)) {
122 return -errno;
123 }
124 return OK;
125}
126
Garfield Tan84b087e2020-01-23 10:49:05 -0800127IdGenerator::IdGenerator(Source source) : mSource(source) {}
128
129int32_t IdGenerator::nextId() const {
130 constexpr uint32_t SEQUENCE_NUMBER_MASK = ~SOURCE_MASK;
131 int32_t id = 0;
132
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700133#if defined(__linux__)
134 while (true) {
135 status_t result = getRandomBytes(reinterpret_cast<uint8_t*>(&id), sizeof(id));
136 if (result == OK) {
Garfield Tan84b087e2020-01-23 10:49:05 -0800137 break;
138 }
Garfield Tan84b087e2020-01-23 10:49:05 -0800139 }
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700140#endif // __linux__
Garfield Tan84b087e2020-01-23 10:49:05 -0800141 return (id & SEQUENCE_NUMBER_MASK) | static_cast<int32_t>(mSource);
142}
143
Jeff Brown5912f952013-07-01 19:10:31 -0700144// --- InputEvent ---
145
Prabir Pradhande69f8a2021-11-18 16:40:34 +0000146vec2 transformWithoutTranslation(const ui::Transform& transform, const vec2& xy) {
147 const vec2 transformedXy = transform.transform(xy);
148 const vec2 transformedOrigin = transform.transform(0, 0);
149 return transformedXy - transformedOrigin;
150}
151
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800152const char* inputEventTypeToString(int32_t type) {
153 switch (type) {
154 case AINPUT_EVENT_TYPE_KEY: {
155 return "KEY";
156 }
157 case AINPUT_EVENT_TYPE_MOTION: {
158 return "MOTION";
159 }
160 case AINPUT_EVENT_TYPE_FOCUS: {
161 return "FOCUS";
162 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800163 case AINPUT_EVENT_TYPE_CAPTURE: {
164 return "CAPTURE";
165 }
arthurhung7632c332020-12-30 16:58:01 +0800166 case AINPUT_EVENT_TYPE_DRAG: {
167 return "DRAG";
168 }
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700169 case AINPUT_EVENT_TYPE_TOUCH_MODE: {
170 return "TOUCH_MODE";
171 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800172 }
173 return "UNKNOWN";
174}
175
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800176std::string inputEventSourceToString(int32_t source) {
177 if (source == AINPUT_SOURCE_UNKNOWN) {
178 return "UNKNOWN";
179 }
180 if (source == static_cast<int32_t>(AINPUT_SOURCE_ANY)) {
181 return "ANY";
182 }
183 static const std::map<int32_t, const char*> SOURCES{
184 {AINPUT_SOURCE_KEYBOARD, "KEYBOARD"},
185 {AINPUT_SOURCE_DPAD, "DPAD"},
186 {AINPUT_SOURCE_GAMEPAD, "GAMEPAD"},
187 {AINPUT_SOURCE_TOUCHSCREEN, "TOUCHSCREEN"},
188 {AINPUT_SOURCE_MOUSE, "MOUSE"},
189 {AINPUT_SOURCE_STYLUS, "STYLUS"},
190 {AINPUT_SOURCE_BLUETOOTH_STYLUS, "BLUETOOTH_STYLUS"},
191 {AINPUT_SOURCE_TRACKBALL, "TRACKBALL"},
192 {AINPUT_SOURCE_MOUSE_RELATIVE, "MOUSE_RELATIVE"},
193 {AINPUT_SOURCE_TOUCHPAD, "TOUCHPAD"},
194 {AINPUT_SOURCE_TOUCH_NAVIGATION, "TOUCH_NAVIGATION"},
195 {AINPUT_SOURCE_JOYSTICK, "JOYSTICK"},
196 {AINPUT_SOURCE_HDMI, "HDMI"},
197 {AINPUT_SOURCE_SENSOR, "SENSOR"},
198 {AINPUT_SOURCE_ROTARY_ENCODER, "ROTARY_ENCODER"},
199 };
200 std::string result;
201 for (const auto& [source_entry, str] : SOURCES) {
202 if ((source & source_entry) == source_entry) {
203 if (!result.empty()) {
204 result += " | ";
205 }
206 result += str;
207 }
208 }
209 if (result.empty()) {
210 result = StringPrintf("0x%08x", source);
211 }
212 return result;
213}
214
215bool isFromSource(uint32_t source, uint32_t test) {
216 return (source & test) == test;
217}
218
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800219VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
220 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
221 event.getSource(), event.getDisplayId()},
222 event.getAction(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800223 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800224 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800225 event.getKeyCode(),
226 event.getScanCode(),
227 event.getMetaState(),
228 event.getRepeatCount()};
229}
230
231VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
232 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
233 event.getSource(), event.getDisplayId()},
234 event.getRawX(0),
235 event.getRawY(0),
236 event.getActionMasked(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800237 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800238 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800239 event.getMetaState(),
240 event.getButtonState()};
241}
242
Garfield Tan4cc839f2020-01-24 11:26:14 -0800243void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600244 std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800245 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700246 mDeviceId = deviceId;
247 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100248 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600249 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700250}
251
252void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800253 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700254 mDeviceId = from.mDeviceId;
255 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100256 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600257 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700258}
259
Garfield Tan4cc839f2020-01-24 11:26:14 -0800260int32_t InputEvent::nextId() {
261 static IdGenerator idGen(IdGenerator::Source::OTHER);
262 return idGen.nextId();
263}
264
Jeff Brown5912f952013-07-01 19:10:31 -0700265// --- KeyEvent ---
266
Michael Wright872db4f2014-04-22 15:03:51 -0700267const char* KeyEvent::getLabel(int32_t keyCode) {
Chris Ye4958d062020-08-20 13:21:10 -0700268 return InputEventLookup::getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700269}
270
Michael Wright872db4f2014-04-22 15:03:51 -0700271int32_t KeyEvent::getKeyCodeFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700272 return InputEventLookup::getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700273}
274
Garfield Tan4cc839f2020-01-24 11:26:14 -0800275void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600276 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
277 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
278 nsecs_t downTime, nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800279 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700280 mAction = action;
281 mFlags = flags;
282 mKeyCode = keyCode;
283 mScanCode = scanCode;
284 mMetaState = metaState;
285 mRepeatCount = repeatCount;
286 mDownTime = downTime;
287 mEventTime = eventTime;
288}
289
290void KeyEvent::initialize(const KeyEvent& from) {
291 InputEvent::initialize(from);
292 mAction = from.mAction;
293 mFlags = from.mFlags;
294 mKeyCode = from.mKeyCode;
295 mScanCode = from.mScanCode;
296 mMetaState = from.mMetaState;
297 mRepeatCount = from.mRepeatCount;
298 mDownTime = from.mDownTime;
299 mEventTime = from.mEventTime;
300}
301
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700302const char* KeyEvent::actionToString(int32_t action) {
303 // Convert KeyEvent action to string
304 switch (action) {
305 case AKEY_EVENT_ACTION_DOWN:
306 return "DOWN";
307 case AKEY_EVENT_ACTION_UP:
308 return "UP";
309 case AKEY_EVENT_ACTION_MULTIPLE:
310 return "MULTIPLE";
311 }
312 return "UNKNOWN";
313}
Jeff Brown5912f952013-07-01 19:10:31 -0700314
315// --- PointerCoords ---
316
317float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700318 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700319 return 0;
320 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700321 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700322}
323
324status_t PointerCoords::setAxisValue(int32_t axis, float value) {
325 if (axis < 0 || axis > 63) {
326 return NAME_NOT_FOUND;
327 }
328
Michael Wright38dcdff2014-03-19 12:06:10 -0700329 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
330 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700331 if (value == 0) {
332 return OK; // axes with value 0 do not need to be stored
333 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700334
335 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700336 if (count >= MAX_AXES) {
337 tooManyAxes(axis);
338 return NO_MEMORY;
339 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700340 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700341 for (uint32_t i = count; i > index; i--) {
342 values[i] = values[i - 1];
343 }
344 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700345
Jeff Brown5912f952013-07-01 19:10:31 -0700346 values[index] = value;
347 return OK;
348}
349
350static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
351 float value = c.getAxisValue(axis);
352 if (value != 0) {
353 c.setAxisValue(axis, value * scaleFactor);
354 }
355}
356
Robert Carre07e1032018-11-26 12:55:53 -0800357void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700358 // No need to scale pressure or size since they are normalized.
359 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800360
361 // If there is a global scale factor, it is included in the windowX/YScale
362 // so we don't need to apply it twice to the X/Y axes.
363 // However we don't want to apply any windowXYScale not included in the global scale
364 // to the TOUCH_MAJOR/MINOR coordinates.
365 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
366 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
367 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
368 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
369 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
370 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
Prabir Pradhanc6523582021-05-14 18:02:55 -0700371 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_X, windowXScale);
372 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_Y, windowYScale);
Robert Carre07e1032018-11-26 12:55:53 -0800373}
374
Brett Chabotfaa986c2020-11-04 17:39:36 -0800375#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700376status_t PointerCoords::readFromParcel(Parcel* parcel) {
377 bits = parcel->readInt64();
378
Michael Wright38dcdff2014-03-19 12:06:10 -0700379 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700380 if (count > MAX_AXES) {
381 return BAD_VALUE;
382 }
383
384 for (uint32_t i = 0; i < count; i++) {
385 values[i] = parcel->readFloat();
386 }
387 return OK;
388}
389
390status_t PointerCoords::writeToParcel(Parcel* parcel) const {
391 parcel->writeInt64(bits);
392
Michael Wright38dcdff2014-03-19 12:06:10 -0700393 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700394 for (uint32_t i = 0; i < count; i++) {
395 parcel->writeFloat(values[i]);
396 }
397 return OK;
398}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800399#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700400
401void PointerCoords::tooManyAxes(int axis) {
402 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
403 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
404}
405
406bool PointerCoords::operator==(const PointerCoords& other) const {
407 if (bits != other.bits) {
408 return false;
409 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700410 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700411 for (uint32_t i = 0; i < count; i++) {
412 if (values[i] != other.values[i]) {
413 return false;
414 }
415 }
416 return true;
417}
418
419void PointerCoords::copyFrom(const PointerCoords& other) {
420 bits = other.bits;
Michael Wright38dcdff2014-03-19 12:06:10 -0700421 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700422 for (uint32_t i = 0; i < count; i++) {
423 values[i] = other.values[i];
424 }
425}
426
chaviwc01e1372020-07-01 12:37:31 -0700427void PointerCoords::transform(const ui::Transform& transform) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700428 const vec2 xy = transform.transform(getXYValue());
429 setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
430 setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
431
Prabir Pradhanc6523582021-05-14 18:02:55 -0700432 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_X) ||
433 BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_Y)) {
434 const ui::Transform rotation(transform.getOrientation());
435 const vec2 relativeXy = rotation.transform(getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
436 getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
437 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
438 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
439 }
440
Prabir Pradhan6b384612021-05-14 16:56:25 -0700441 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) {
442 const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
443 setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val));
444 }
chaviwc01e1372020-07-01 12:37:31 -0700445}
Jeff Brown5912f952013-07-01 19:10:31 -0700446
447// --- PointerProperties ---
448
449bool PointerProperties::operator==(const PointerProperties& other) const {
450 return id == other.id
451 && toolType == other.toolType;
452}
453
454void PointerProperties::copyFrom(const PointerProperties& other) {
455 id = other.id;
456 toolType = other.toolType;
457}
458
459
460// --- MotionEvent ---
461
Garfield Tan4cc839f2020-01-24 11:26:14 -0800462void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600463 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
464 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700465 int32_t buttonState, MotionClassification classification,
466 const ui::Transform& transform, float xPrecision, float yPrecision,
Evan Rosky84f07f02021-04-16 10:42:42 -0700467 float rawXCursorPosition, float rawYCursorPosition,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700468 const ui::Transform& rawTransform, nsecs_t downTime, nsecs_t eventTime,
Evan Rosky09576692021-07-01 12:22:09 -0700469 size_t pointerCount, const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700470 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800471 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700472 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100473 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700474 mFlags = flags;
475 mEdgeFlags = edgeFlags;
476 mMetaState = metaState;
477 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800478 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700479 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700480 mXPrecision = xPrecision;
481 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700482 mRawXCursorPosition = rawXCursorPosition;
483 mRawYCursorPosition = rawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700484 mRawTransform = rawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700485 mDownTime = downTime;
486 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800487 mPointerProperties.insert(mPointerProperties.end(), &pointerProperties[0],
488 &pointerProperties[pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700489 mSampleEventTimes.clear();
490 mSamplePointerCoords.clear();
491 addSample(eventTime, pointerCoords);
492}
493
494void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800495 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
496 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700497 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100498 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700499 mFlags = other->mFlags;
500 mEdgeFlags = other->mEdgeFlags;
501 mMetaState = other->mMetaState;
502 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800503 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700504 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700505 mXPrecision = other->mXPrecision;
506 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700507 mRawXCursorPosition = other->mRawXCursorPosition;
508 mRawYCursorPosition = other->mRawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700509 mRawTransform = other->mRawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700510 mDownTime = other->mDownTime;
511 mPointerProperties = other->mPointerProperties;
512
513 if (keepHistory) {
514 mSampleEventTimes = other->mSampleEventTimes;
515 mSamplePointerCoords = other->mSamplePointerCoords;
516 } else {
517 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500518 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700519 mSamplePointerCoords.clear();
520 size_t pointerCount = other->getPointerCount();
521 size_t historySize = other->getHistorySize();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800522 mSamplePointerCoords
523 .insert(mSamplePointerCoords.end(),
524 &other->mSamplePointerCoords[historySize * pointerCount],
525 &other->mSamplePointerCoords[historySize * pointerCount + pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700526 }
527}
528
529void MotionEvent::addSample(
530 int64_t eventTime,
531 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500532 mSampleEventTimes.push_back(eventTime);
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800533 mSamplePointerCoords.insert(mSamplePointerCoords.end(), &pointerCoords[0],
534 &pointerCoords[getPointerCount()]);
Jeff Brown5912f952013-07-01 19:10:31 -0700535}
536
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800537int MotionEvent::getSurfaceRotation() const {
538 // The surface rotation is the rotation from the window's coordinate space to that of the
539 // display. Since the event's transform takes display space coordinates to window space, the
540 // returned surface rotation is the inverse of the rotation for the surface.
541 switch (mTransform.getOrientation()) {
542 case ui::Transform::ROT_0:
543 return DISPLAY_ORIENTATION_0;
544 case ui::Transform::ROT_90:
545 return DISPLAY_ORIENTATION_270;
546 case ui::Transform::ROT_180:
547 return DISPLAY_ORIENTATION_180;
548 case ui::Transform::ROT_270:
549 return DISPLAY_ORIENTATION_90;
550 default:
551 return -1;
552 }
553}
554
Garfield Tan00f511d2019-06-12 16:55:40 -0700555float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700556 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
557 return vals.x;
Garfield Tan00f511d2019-06-12 16:55:40 -0700558}
559
560float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700561 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
562 return vals.y;
Garfield Tan00f511d2019-06-12 16:55:40 -0700563}
564
Garfield Tan937bb832019-07-25 17:48:31 -0700565void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700566 ui::Transform inverse = mTransform.inverse();
567 vec2 vals = inverse.transform(x, y);
568 mRawXCursorPosition = vals.x;
569 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700570}
571
Jeff Brown5912f952013-07-01 19:10:31 -0700572const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000573 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
574 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
575 }
576 const size_t position = getHistorySize() * getPointerCount() + pointerIndex;
577 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
578 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
579 }
580 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700581}
582
583float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
Evan Rosky84f07f02021-04-16 10:42:42 -0700584 return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700585}
586
587float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700588 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700589}
590
591const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
592 size_t pointerIndex, size_t historicalIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000593 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
594 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
595 }
596 if (CC_UNLIKELY(historicalIndex < 0 || historicalIndex > getHistorySize())) {
597 LOG(FATAL) << __func__ << ": Invalid historical index " << historicalIndex << " for "
598 << *this;
599 }
600 const size_t position = historicalIndex * getPointerCount() + pointerIndex;
601 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
602 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
603 }
604 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700605}
606
607float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan6b384612021-05-14 16:56:25 -0700608 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700609 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
610 return calculateTransformedAxisValue(axis, mSource, mRawTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700611}
612
613float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan9f388812021-05-13 16:54:53 -0700614 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700615 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
616 return calculateTransformedAxisValue(axis, mSource, mTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700617}
618
619ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
620 size_t pointerCount = mPointerProperties.size();
621 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800622 if (mPointerProperties[i].id == pointerId) {
Jeff Brown5912f952013-07-01 19:10:31 -0700623 return i;
624 }
625 }
626 return -1;
627}
628
629void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700630 float currXOffset = mTransform.tx();
631 float currYOffset = mTransform.ty();
632 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700633}
634
Robert Carre07e1032018-11-26 12:55:53 -0800635void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700636 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700637 mRawTransform.set(mRawTransform.tx() * globalScaleFactor,
638 mRawTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800639 mXPrecision *= globalScaleFactor;
640 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700641
642 size_t numSamples = mSamplePointerCoords.size();
643 for (size_t i = 0; i < numSamples; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800644 mSamplePointerCoords[i].scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700645 }
646}
647
chaviw9eaa22c2020-07-01 16:21:27 -0700648void MotionEvent::transform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700649 // We want to preserve the raw axes values stored in the PointerCoords, so we just update the
650 // transform using the values passed in.
chaviw9eaa22c2020-07-01 16:21:27 -0700651 ui::Transform newTransform;
652 newTransform.set(matrix);
653 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700654}
655
Evan Roskyd4d4d802021-05-03 20:12:21 -0700656void MotionEvent::applyTransform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700657 ui::Transform transform;
658 transform.set(matrix);
Evan Roskyd4d4d802021-05-03 20:12:21 -0700659
660 // Apply the transformation to all samples.
Prabir Pradhan6b384612021-05-14 16:56:25 -0700661 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
662 [&transform](PointerCoords& c) { c.transform(transform); });
Prabir Pradhan4b19bd02021-06-01 17:34:59 -0700663
664 if (mRawXCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION &&
665 mRawYCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) {
666 const vec2 cursor = transform.transform(mRawXCursorPosition, mRawYCursorPosition);
667 mRawXCursorPosition = cursor.x;
668 mRawYCursorPosition = cursor.y;
669 }
Evan Roskyd4d4d802021-05-03 20:12:21 -0700670}
671
Brett Chabotfaa986c2020-11-04 17:39:36 -0800672#ifdef __linux__
chaviw9eaa22c2020-07-01 16:21:27 -0700673static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
674 float dsdx, dtdx, tx, dtdy, dsdy, ty;
675 status_t status = parcel.readFloat(&dsdx);
676 status |= parcel.readFloat(&dtdx);
677 status |= parcel.readFloat(&tx);
678 status |= parcel.readFloat(&dtdy);
679 status |= parcel.readFloat(&dsdy);
680 status |= parcel.readFloat(&ty);
681
682 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
683 return status;
684}
685
686static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
687 status_t status = parcel.writeFloat(transform.dsdx());
688 status |= parcel.writeFloat(transform.dtdx());
689 status |= parcel.writeFloat(transform.tx());
690 status |= parcel.writeFloat(transform.dtdy());
691 status |= parcel.writeFloat(transform.dsdy());
692 status |= parcel.writeFloat(transform.ty());
693 return status;
694}
695
Jeff Brown5912f952013-07-01 19:10:31 -0700696status_t MotionEvent::readFromParcel(Parcel* parcel) {
697 size_t pointerCount = parcel->readInt32();
698 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800699 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
700 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700701 return BAD_VALUE;
702 }
703
Garfield Tan4cc839f2020-01-24 11:26:14 -0800704 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700705 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600706 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800707 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600708 std::vector<uint8_t> hmac;
709 status_t result = parcel->readByteVector(&hmac);
710 if (result != OK || hmac.size() != 32) {
711 return BAD_VALUE;
712 }
713 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700714 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100715 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700716 mFlags = parcel->readInt32();
717 mEdgeFlags = parcel->readInt32();
718 mMetaState = parcel->readInt32();
719 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800720 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700721
722 result = android::readFromParcel(mTransform, *parcel);
723 if (result != OK) {
724 return result;
725 }
Jeff Brown5912f952013-07-01 19:10:31 -0700726 mXPrecision = parcel->readFloat();
727 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700728 mRawXCursorPosition = parcel->readFloat();
729 mRawYCursorPosition = parcel->readFloat();
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700730
731 result = android::readFromParcel(mRawTransform, *parcel);
732 if (result != OK) {
733 return result;
734 }
Jeff Brown5912f952013-07-01 19:10:31 -0700735 mDownTime = parcel->readInt64();
736
737 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800738 mPointerProperties.reserve(pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700739 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500740 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700741 mSamplePointerCoords.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800742 mSamplePointerCoords.reserve(sampleCount * pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700743
744 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800745 mPointerProperties.push_back({});
746 PointerProperties& properties = mPointerProperties.back();
Jeff Brown5912f952013-07-01 19:10:31 -0700747 properties.id = parcel->readInt32();
748 properties.toolType = parcel->readInt32();
749 }
750
Dan Austinc94fc452015-09-22 14:22:41 -0700751 while (sampleCount > 0) {
752 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500753 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700754 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800755 mSamplePointerCoords.push_back({});
756 status_t status = mSamplePointerCoords.back().readFromParcel(parcel);
Jeff Brown5912f952013-07-01 19:10:31 -0700757 if (status) {
758 return status;
759 }
760 }
761 }
762 return OK;
763}
764
765status_t MotionEvent::writeToParcel(Parcel* parcel) const {
766 size_t pointerCount = mPointerProperties.size();
767 size_t sampleCount = mSampleEventTimes.size();
768
769 parcel->writeInt32(pointerCount);
770 parcel->writeInt32(sampleCount);
771
Garfield Tan4cc839f2020-01-24 11:26:14 -0800772 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700773 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600774 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800775 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600776 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
777 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700778 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100779 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700780 parcel->writeInt32(mFlags);
781 parcel->writeInt32(mEdgeFlags);
782 parcel->writeInt32(mMetaState);
783 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800784 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700785
786 status_t result = android::writeToParcel(mTransform, *parcel);
787 if (result != OK) {
788 return result;
789 }
Jeff Brown5912f952013-07-01 19:10:31 -0700790 parcel->writeFloat(mXPrecision);
791 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700792 parcel->writeFloat(mRawXCursorPosition);
793 parcel->writeFloat(mRawYCursorPosition);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700794
795 result = android::writeToParcel(mRawTransform, *parcel);
796 if (result != OK) {
797 return result;
798 }
Jeff Brown5912f952013-07-01 19:10:31 -0700799 parcel->writeInt64(mDownTime);
800
801 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800802 const PointerProperties& properties = mPointerProperties[i];
Jeff Brown5912f952013-07-01 19:10:31 -0700803 parcel->writeInt32(properties.id);
804 parcel->writeInt32(properties.toolType);
805 }
806
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800807 const PointerCoords* pc = mSamplePointerCoords.data();
Jeff Brown5912f952013-07-01 19:10:31 -0700808 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500809 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700810 for (size_t i = 0; i < pointerCount; i++) {
811 status_t status = (pc++)->writeToParcel(parcel);
812 if (status) {
813 return status;
814 }
815 }
816 }
817 return OK;
818}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800819#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700820
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600821bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700822 if (isFromSource(source, AINPUT_SOURCE_CLASS_POINTER)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700823 // Specifically excludes HOVER_MOVE and SCROLL.
824 switch (action & AMOTION_EVENT_ACTION_MASK) {
825 case AMOTION_EVENT_ACTION_DOWN:
826 case AMOTION_EVENT_ACTION_MOVE:
827 case AMOTION_EVENT_ACTION_UP:
828 case AMOTION_EVENT_ACTION_POINTER_DOWN:
829 case AMOTION_EVENT_ACTION_POINTER_UP:
830 case AMOTION_EVENT_ACTION_CANCEL:
831 case AMOTION_EVENT_ACTION_OUTSIDE:
832 return true;
833 }
834 }
835 return false;
836}
837
Michael Wright872db4f2014-04-22 15:03:51 -0700838const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700839 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700840}
841
842int32_t MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700843 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700844}
845
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500846std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700847 // Convert MotionEvent action to string
848 switch (action & AMOTION_EVENT_ACTION_MASK) {
849 case AMOTION_EVENT_ACTION_DOWN:
850 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700851 case AMOTION_EVENT_ACTION_UP:
852 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500853 case AMOTION_EVENT_ACTION_MOVE:
854 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700855 case AMOTION_EVENT_ACTION_CANCEL:
856 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500857 case AMOTION_EVENT_ACTION_OUTSIDE:
858 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700859 case AMOTION_EVENT_ACTION_POINTER_DOWN:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000860 return StringPrintf("POINTER_DOWN(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700861 case AMOTION_EVENT_ACTION_POINTER_UP:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000862 return StringPrintf("POINTER_UP(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500863 case AMOTION_EVENT_ACTION_HOVER_MOVE:
864 return "HOVER_MOVE";
865 case AMOTION_EVENT_ACTION_SCROLL:
866 return "SCROLL";
867 case AMOTION_EVENT_ACTION_HOVER_ENTER:
868 return "HOVER_ENTER";
869 case AMOTION_EVENT_ACTION_HOVER_EXIT:
870 return "HOVER_EXIT";
871 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
872 return "BUTTON_PRESS";
873 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
874 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700875 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500876 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700877}
878
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700879// Apply the given transformation to the point without checking whether the entire transform
880// should be disregarded altogether for the provided source.
881static inline vec2 calculateTransformedXYUnchecked(uint32_t source, const ui::Transform& transform,
882 const vec2& xy) {
883 return shouldDisregardOffset(source) ? transformWithoutTranslation(transform, xy)
884 : transform.transform(xy);
885}
886
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700887vec2 MotionEvent::calculateTransformedXY(uint32_t source, const ui::Transform& transform,
888 const vec2& xy) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700889 if (shouldDisregardTransformation(source)) {
890 return xy;
891 }
892 return calculateTransformedXYUnchecked(source, transform, xy);
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700893}
894
Prabir Pradhan8e6ce222022-02-24 09:08:54 -0800895// Keep in sync with calculateTransformedCoords.
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700896float MotionEvent::calculateTransformedAxisValue(int32_t axis, uint32_t source,
897 const ui::Transform& transform,
898 const PointerCoords& coords) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700899 if (shouldDisregardTransformation(source)) {
900 return coords.getAxisValue(axis);
901 }
902
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700903 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700904 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700905 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
906 return xy[axis];
907 }
908
909 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
910 const vec2 relativeXy =
911 transformWithoutTranslation(transform,
912 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
913 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
914 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
915 }
916
917 if (axis == AMOTION_EVENT_AXIS_ORIENTATION) {
918 return transformAngle(transform, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION));
919 }
920
921 return coords.getAxisValue(axis);
922}
923
Prabir Pradhan8e6ce222022-02-24 09:08:54 -0800924// Keep in sync with calculateTransformedAxisValue. This is an optimization of
925// calculateTransformedAxisValue for all PointerCoords axes.
926PointerCoords MotionEvent::calculateTransformedCoords(uint32_t source,
927 const ui::Transform& transform,
928 const PointerCoords& coords) {
929 if (shouldDisregardTransformation(source)) {
930 return coords;
931 }
932 PointerCoords out = coords;
933
934 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
935 out.setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
936 out.setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
937
938 const vec2 relativeXy =
939 transformWithoutTranslation(transform,
940 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
941 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
942 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
943 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
944
945 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
946 transformAngle(transform,
947 coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)));
948
949 return out;
950}
951
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000952std::ostream& operator<<(std::ostream& out, const MotionEvent& event) {
953 out << "MotionEvent { action=" << MotionEvent::actionToString(event.getAction());
954 if (event.getActionButton() != 0) {
955 out << ", actionButton=" << std::to_string(event.getActionButton());
956 }
957 const size_t pointerCount = event.getPointerCount();
hupeng3aa5a51a2022-09-02 16:00:18 +0800958 LOG_ALWAYS_FATAL_IF(pointerCount > MAX_POINTERS, "Too many pointers : pointerCount = %zu",
959 pointerCount);
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000960 for (size_t i = 0; i < pointerCount; i++) {
961 out << ", id[" << i << "]=" << event.getPointerId(i);
962 float x = event.getX(i);
963 float y = event.getY(i);
964 if (x != 0 || y != 0) {
965 out << ", x[" << i << "]=" << x;
966 out << ", y[" << i << "]=" << y;
967 }
968 int toolType = event.getToolType(i);
969 if (toolType != AMOTION_EVENT_TOOL_TYPE_FINGER) {
970 out << ", toolType[" << i << "]=" << toolType;
971 }
972 }
973 if (event.getButtonState() != 0) {
974 out << ", buttonState=" << event.getButtonState();
975 }
976 if (event.getClassification() != MotionClassification::NONE) {
977 out << ", classification=" << motionClassificationToString(event.getClassification());
978 }
979 if (event.getMetaState() != 0) {
980 out << ", metaState=" << event.getMetaState();
981 }
982 if (event.getEdgeFlags() != 0) {
983 out << ", edgeFlags=" << event.getEdgeFlags();
984 }
985 if (pointerCount != 1) {
986 out << ", pointerCount=" << pointerCount;
987 }
988 if (event.getHistorySize() != 0) {
989 out << ", historySize=" << event.getHistorySize();
990 }
991 out << ", eventTime=" << event.getEventTime();
992 out << ", downTime=" << event.getDownTime();
993 out << ", deviceId=" << event.getDeviceId();
994 out << ", source=" << inputEventSourceToString(event.getSource());
995 out << ", displayId=" << event.getDisplayId();
996 out << ", eventId=" << event.getId();
997 out << "}";
998 return out;
999}
1000
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001001// --- FocusEvent ---
1002
Antonio Kantek3cfec7b2021-11-05 18:26:17 -07001003void FocusEvent::initialize(int32_t id, bool hasFocus) {
Garfield Tan4cc839f2020-01-24 11:26:14 -08001004 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -06001005 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001006 mHasFocus = hasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001007}
1008
1009void FocusEvent::initialize(const FocusEvent& from) {
1010 InputEvent::initialize(from);
1011 mHasFocus = from.mHasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001012}
Jeff Brown5912f952013-07-01 19:10:31 -07001013
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001014// --- CaptureEvent ---
1015
1016void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) {
1017 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1018 ADISPLAY_ID_NONE, INVALID_HMAC);
1019 mPointerCaptureEnabled = pointerCaptureEnabled;
1020}
1021
1022void CaptureEvent::initialize(const CaptureEvent& from) {
1023 InputEvent::initialize(from);
1024 mPointerCaptureEnabled = from.mPointerCaptureEnabled;
1025}
1026
arthurhung7632c332020-12-30 16:58:01 +08001027// --- DragEvent ---
1028
1029void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) {
1030 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1031 ADISPLAY_ID_NONE, INVALID_HMAC);
1032 mIsExiting = isExiting;
1033 mX = x;
1034 mY = y;
1035}
1036
1037void DragEvent::initialize(const DragEvent& from) {
1038 InputEvent::initialize(from);
1039 mIsExiting = from.mIsExiting;
1040 mX = from.mX;
1041 mY = from.mY;
1042}
1043
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001044// --- TouchModeEvent ---
1045
1046void TouchModeEvent::initialize(int32_t id, bool isInTouchMode) {
1047 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1048 ADISPLAY_ID_NONE, INVALID_HMAC);
1049 mIsInTouchMode = isInTouchMode;
1050}
1051
1052void TouchModeEvent::initialize(const TouchModeEvent& from) {
1053 InputEvent::initialize(from);
1054 mIsInTouchMode = from.mIsInTouchMode;
1055}
1056
Jeff Brown5912f952013-07-01 19:10:31 -07001057// --- PooledInputEventFactory ---
1058
1059PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
1060 mMaxPoolSize(maxPoolSize) {
1061}
1062
1063PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -07001064}
1065
1066KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001067 if (mKeyEventPool.empty()) {
1068 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001069 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001070 KeyEvent* event = mKeyEventPool.front().release();
1071 mKeyEventPool.pop();
1072 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001073}
1074
1075MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001076 if (mMotionEventPool.empty()) {
1077 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001078 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001079 MotionEvent* event = mMotionEventPool.front().release();
1080 mMotionEventPool.pop();
1081 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001082}
1083
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001084FocusEvent* PooledInputEventFactory::createFocusEvent() {
1085 if (mFocusEventPool.empty()) {
1086 return new FocusEvent();
1087 }
1088 FocusEvent* event = mFocusEventPool.front().release();
1089 mFocusEventPool.pop();
1090 return event;
1091}
1092
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001093CaptureEvent* PooledInputEventFactory::createCaptureEvent() {
1094 if (mCaptureEventPool.empty()) {
1095 return new CaptureEvent();
1096 }
1097 CaptureEvent* event = mCaptureEventPool.front().release();
1098 mCaptureEventPool.pop();
1099 return event;
1100}
1101
arthurhung7632c332020-12-30 16:58:01 +08001102DragEvent* PooledInputEventFactory::createDragEvent() {
1103 if (mDragEventPool.empty()) {
1104 return new DragEvent();
1105 }
1106 DragEvent* event = mDragEventPool.front().release();
1107 mDragEventPool.pop();
1108 return event;
1109}
1110
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001111TouchModeEvent* PooledInputEventFactory::createTouchModeEvent() {
1112 if (mTouchModeEventPool.empty()) {
1113 return new TouchModeEvent();
1114 }
1115 TouchModeEvent* event = mTouchModeEventPool.front().release();
1116 mTouchModeEventPool.pop();
1117 return event;
1118}
1119
Jeff Brown5912f952013-07-01 19:10:31 -07001120void PooledInputEventFactory::recycle(InputEvent* event) {
1121 switch (event->getType()) {
1122 case AINPUT_EVENT_TYPE_KEY:
1123 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001124 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -07001125 return;
1126 }
1127 break;
1128 case AINPUT_EVENT_TYPE_MOTION:
1129 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001130 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -07001131 return;
1132 }
1133 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001134 case AINPUT_EVENT_TYPE_FOCUS:
1135 if (mFocusEventPool.size() < mMaxPoolSize) {
1136 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
1137 return;
1138 }
1139 break;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001140 case AINPUT_EVENT_TYPE_CAPTURE:
1141 if (mCaptureEventPool.size() < mMaxPoolSize) {
1142 mCaptureEventPool.push(
1143 std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event)));
1144 return;
1145 }
1146 break;
arthurhung7632c332020-12-30 16:58:01 +08001147 case AINPUT_EVENT_TYPE_DRAG:
1148 if (mDragEventPool.size() < mMaxPoolSize) {
1149 mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event)));
1150 return;
1151 }
1152 break;
Antonio Kantekeb4a30c2021-09-28 17:49:49 -07001153 case AINPUT_EVENT_TYPE_TOUCH_MODE:
1154 if (mTouchModeEventPool.size() < mMaxPoolSize) {
1155 mTouchModeEventPool.push(
1156 std::unique_ptr<TouchModeEvent>(static_cast<TouchModeEvent*>(event)));
1157 return;
1158 }
1159 break;
Jeff Brown5912f952013-07-01 19:10:31 -07001160 }
1161 delete event;
1162}
1163
1164} // namespace android