blob: 375b68433d5d49c5cc3ffbf4640f673b0d7ed118 [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 Vishniakou4ded0b02022-05-26 00:36:48 +000025#include <android-base/logging.h>
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050026#include <android-base/stringprintf.h>
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +000027#include <cutils/compiler.h>
chaviw98318de2021-05-19 16:45:23 -050028#include <gui/constants.h>
Prabir Pradhan092f3a92021-11-25 10:53:27 -080029#include <input/DisplayViewport.h>
Jeff Brown5912f952013-07-01 19:10:31 -070030#include <input/Input.h>
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080031#include <input/InputDevice.h>
Michael Wright872db4f2014-04-22 15:03:51 -070032#include <input/InputEventLabels.h>
Jeff Brown5912f952013-07-01 19:10:31 -070033
Brett Chabotfaa986c2020-11-04 17:39:36 -080034#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -070035#include <binder/Parcel.h>
Brett Chabotfaa986c2020-11-04 17:39:36 -080036#endif
Brett Chabot58208522020-09-09 13:55:24 -070037#ifdef __ANDROID__
Garfield Tan84b087e2020-01-23 10:49:05 -080038#include <sys/random.h>
Jeff Brown5912f952013-07-01 19:10:31 -070039#endif
40
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050041using android::base::StringPrintf;
42
Jeff Brown5912f952013-07-01 19:10:31 -070043namespace android {
44
Prabir Pradhan6b384612021-05-14 16:56:25 -070045namespace {
46
47float transformAngle(const ui::Transform& transform, float angleRadians) {
48 // Construct and transform a vector oriented at the specified clockwise angle from vertical.
49 // Coordinate system: down is increasing Y, right is increasing X.
50 float x = sinf(angleRadians);
51 float y = -cosf(angleRadians);
52 vec2 transformedPoint = transform.transform(x, y);
53
54 // Determine how the origin is transformed by the matrix so that we
55 // can transform orientation vectors.
56 const vec2 origin = transform.transform(0, 0);
57
58 transformedPoint.x -= origin.x;
59 transformedPoint.y -= origin.y;
60
61 // Derive the transformed vector's clockwise angle from vertical.
Prabir Pradhand2b02672021-10-19 11:24:45 -070062 // The return value of atan2f is in range [-pi, pi] which conforms to the orientation API.
63 return atan2f(transformedPoint.x, -transformedPoint.y);
Prabir Pradhan6b384612021-05-14 16:56:25 -070064}
65
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070066bool shouldDisregardTransformation(uint32_t source) {
Prabir Pradhan258e2b92022-06-24 18:37:04 +000067 // Do not apply any transformations to axes from joysticks, touchpads, or relative mice.
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070068 return isFromSource(source, AINPUT_SOURCE_CLASS_JOYSTICK) ||
Prabir Pradhan258e2b92022-06-24 18:37:04 +000069 isFromSource(source, AINPUT_SOURCE_CLASS_POSITION) ||
70 isFromSource(source, AINPUT_SOURCE_MOUSE_RELATIVE);
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070071}
72
73bool shouldDisregardOffset(uint32_t source) {
Prabir Pradhan9f388812021-05-13 16:54:53 -070074 // Pointer events are the only type of events that refer to absolute coordinates on the display,
75 // so we should apply the entire window transform. For other types of events, we should make
76 // sure to not apply the window translation/offset.
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070077 return !isFromSource(source, AINPUT_SOURCE_CLASS_POINTER);
Prabir Pradhan9f388812021-05-13 16:54:53 -070078}
79
Prabir Pradhan6b384612021-05-14 16:56:25 -070080} // namespace
81
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -080082const char* motionClassificationToString(MotionClassification classification) {
83 switch (classification) {
84 case MotionClassification::NONE:
85 return "NONE";
86 case MotionClassification::AMBIGUOUS_GESTURE:
87 return "AMBIGUOUS_GESTURE";
88 case MotionClassification::DEEP_PRESS:
89 return "DEEP_PRESS";
90 }
91}
92
Garfield Tan84b087e2020-01-23 10:49:05 -080093// --- IdGenerator ---
94IdGenerator::IdGenerator(Source source) : mSource(source) {}
95
96int32_t IdGenerator::nextId() const {
97 constexpr uint32_t SEQUENCE_NUMBER_MASK = ~SOURCE_MASK;
98 int32_t id = 0;
99
100// Avoid building against syscall getrandom(2) on host, which will fail build on Mac. Host doesn't
101// use sequence number so just always return mSource.
102#ifdef __ANDROID__
103 constexpr size_t BUF_LEN = sizeof(id);
104 size_t totalBytes = 0;
105 while (totalBytes < BUF_LEN) {
106 ssize_t bytes = TEMP_FAILURE_RETRY(getrandom(&id, BUF_LEN, GRND_NONBLOCK));
107 if (CC_UNLIKELY(bytes < 0)) {
108 ALOGW("Failed to fill in random number for sequence number: %s.", strerror(errno));
109 id = 0;
110 break;
111 }
112 totalBytes += bytes;
113 }
114#endif // __ANDROID__
115
116 return (id & SEQUENCE_NUMBER_MASK) | static_cast<int32_t>(mSource);
117}
118
Jeff Brown5912f952013-07-01 19:10:31 -0700119// --- InputEvent ---
120
Prabir Pradhande69f8a2021-11-18 16:40:34 +0000121vec2 transformWithoutTranslation(const ui::Transform& transform, const vec2& xy) {
122 const vec2 transformedXy = transform.transform(xy);
123 const vec2 transformedOrigin = transform.transform(0, 0);
124 return transformedXy - transformedOrigin;
125}
126
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800127const char* inputEventTypeToString(int32_t type) {
128 switch (type) {
129 case AINPUT_EVENT_TYPE_KEY: {
130 return "KEY";
131 }
132 case AINPUT_EVENT_TYPE_MOTION: {
133 return "MOTION";
134 }
135 case AINPUT_EVENT_TYPE_FOCUS: {
136 return "FOCUS";
137 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800138 case AINPUT_EVENT_TYPE_CAPTURE: {
139 return "CAPTURE";
140 }
arthurhung7632c332020-12-30 16:58:01 +0800141 case AINPUT_EVENT_TYPE_DRAG: {
142 return "DRAG";
143 }
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700144 case AINPUT_EVENT_TYPE_TOUCH_MODE: {
145 return "TOUCH_MODE";
146 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800147 }
148 return "UNKNOWN";
149}
150
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800151std::string inputEventSourceToString(int32_t source) {
152 if (source == AINPUT_SOURCE_UNKNOWN) {
153 return "UNKNOWN";
154 }
155 if (source == static_cast<int32_t>(AINPUT_SOURCE_ANY)) {
156 return "ANY";
157 }
158 static const std::map<int32_t, const char*> SOURCES{
159 {AINPUT_SOURCE_KEYBOARD, "KEYBOARD"},
160 {AINPUT_SOURCE_DPAD, "DPAD"},
161 {AINPUT_SOURCE_GAMEPAD, "GAMEPAD"},
162 {AINPUT_SOURCE_TOUCHSCREEN, "TOUCHSCREEN"},
163 {AINPUT_SOURCE_MOUSE, "MOUSE"},
164 {AINPUT_SOURCE_STYLUS, "STYLUS"},
165 {AINPUT_SOURCE_BLUETOOTH_STYLUS, "BLUETOOTH_STYLUS"},
166 {AINPUT_SOURCE_TRACKBALL, "TRACKBALL"},
167 {AINPUT_SOURCE_MOUSE_RELATIVE, "MOUSE_RELATIVE"},
168 {AINPUT_SOURCE_TOUCHPAD, "TOUCHPAD"},
169 {AINPUT_SOURCE_TOUCH_NAVIGATION, "TOUCH_NAVIGATION"},
170 {AINPUT_SOURCE_JOYSTICK, "JOYSTICK"},
171 {AINPUT_SOURCE_HDMI, "HDMI"},
172 {AINPUT_SOURCE_SENSOR, "SENSOR"},
173 {AINPUT_SOURCE_ROTARY_ENCODER, "ROTARY_ENCODER"},
174 };
175 std::string result;
176 for (const auto& [source_entry, str] : SOURCES) {
177 if ((source & source_entry) == source_entry) {
178 if (!result.empty()) {
179 result += " | ";
180 }
181 result += str;
182 }
183 }
184 if (result.empty()) {
185 result = StringPrintf("0x%08x", source);
186 }
187 return result;
188}
189
190bool isFromSource(uint32_t source, uint32_t test) {
191 return (source & test) == test;
192}
193
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800194VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
195 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
196 event.getSource(), event.getDisplayId()},
197 event.getAction(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800198 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800199 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800200 event.getKeyCode(),
201 event.getScanCode(),
202 event.getMetaState(),
203 event.getRepeatCount()};
204}
205
206VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
207 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
208 event.getSource(), event.getDisplayId()},
209 event.getRawX(0),
210 event.getRawY(0),
211 event.getActionMasked(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800212 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800213 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800214 event.getMetaState(),
215 event.getButtonState()};
216}
217
Garfield Tan4cc839f2020-01-24 11:26:14 -0800218void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600219 std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800220 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700221 mDeviceId = deviceId;
222 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100223 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600224 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700225}
226
227void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800228 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700229 mDeviceId = from.mDeviceId;
230 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100231 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600232 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700233}
234
Garfield Tan4cc839f2020-01-24 11:26:14 -0800235int32_t InputEvent::nextId() {
236 static IdGenerator idGen(IdGenerator::Source::OTHER);
237 return idGen.nextId();
238}
239
Jeff Brown5912f952013-07-01 19:10:31 -0700240// --- KeyEvent ---
241
Michael Wright872db4f2014-04-22 15:03:51 -0700242const char* KeyEvent::getLabel(int32_t keyCode) {
Chris Ye4958d062020-08-20 13:21:10 -0700243 return InputEventLookup::getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700244}
245
Michael Wright872db4f2014-04-22 15:03:51 -0700246int32_t KeyEvent::getKeyCodeFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700247 return InputEventLookup::getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700248}
249
Garfield Tan4cc839f2020-01-24 11:26:14 -0800250void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600251 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
252 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
253 nsecs_t downTime, nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800254 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700255 mAction = action;
256 mFlags = flags;
257 mKeyCode = keyCode;
258 mScanCode = scanCode;
259 mMetaState = metaState;
260 mRepeatCount = repeatCount;
261 mDownTime = downTime;
262 mEventTime = eventTime;
263}
264
265void KeyEvent::initialize(const KeyEvent& from) {
266 InputEvent::initialize(from);
267 mAction = from.mAction;
268 mFlags = from.mFlags;
269 mKeyCode = from.mKeyCode;
270 mScanCode = from.mScanCode;
271 mMetaState = from.mMetaState;
272 mRepeatCount = from.mRepeatCount;
273 mDownTime = from.mDownTime;
274 mEventTime = from.mEventTime;
275}
276
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700277const char* KeyEvent::actionToString(int32_t action) {
278 // Convert KeyEvent action to string
279 switch (action) {
280 case AKEY_EVENT_ACTION_DOWN:
281 return "DOWN";
282 case AKEY_EVENT_ACTION_UP:
283 return "UP";
284 case AKEY_EVENT_ACTION_MULTIPLE:
285 return "MULTIPLE";
286 }
287 return "UNKNOWN";
288}
Jeff Brown5912f952013-07-01 19:10:31 -0700289
290// --- PointerCoords ---
291
292float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700293 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700294 return 0;
295 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700296 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700297}
298
299status_t PointerCoords::setAxisValue(int32_t axis, float value) {
300 if (axis < 0 || axis > 63) {
301 return NAME_NOT_FOUND;
302 }
303
Michael Wright38dcdff2014-03-19 12:06:10 -0700304 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
305 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700306 if (value == 0) {
307 return OK; // axes with value 0 do not need to be stored
308 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700309
310 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700311 if (count >= MAX_AXES) {
312 tooManyAxes(axis);
313 return NO_MEMORY;
314 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700315 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700316 for (uint32_t i = count; i > index; i--) {
317 values[i] = values[i - 1];
318 }
319 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700320
Jeff Brown5912f952013-07-01 19:10:31 -0700321 values[index] = value;
322 return OK;
323}
324
325static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
326 float value = c.getAxisValue(axis);
327 if (value != 0) {
328 c.setAxisValue(axis, value * scaleFactor);
329 }
330}
331
Robert Carre07e1032018-11-26 12:55:53 -0800332void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700333 // No need to scale pressure or size since they are normalized.
334 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800335
336 // If there is a global scale factor, it is included in the windowX/YScale
337 // so we don't need to apply it twice to the X/Y axes.
338 // However we don't want to apply any windowXYScale not included in the global scale
339 // to the TOUCH_MAJOR/MINOR coordinates.
340 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
341 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
342 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
343 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
344 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
345 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
Prabir Pradhanc6523582021-05-14 18:02:55 -0700346 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_X, windowXScale);
347 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_Y, windowYScale);
Robert Carre07e1032018-11-26 12:55:53 -0800348}
349
Brett Chabotfaa986c2020-11-04 17:39:36 -0800350#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700351status_t PointerCoords::readFromParcel(Parcel* parcel) {
352 bits = parcel->readInt64();
353
Michael Wright38dcdff2014-03-19 12:06:10 -0700354 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700355 if (count > MAX_AXES) {
356 return BAD_VALUE;
357 }
358
359 for (uint32_t i = 0; i < count; i++) {
360 values[i] = parcel->readFloat();
361 }
362 return OK;
363}
364
365status_t PointerCoords::writeToParcel(Parcel* parcel) const {
366 parcel->writeInt64(bits);
367
Michael Wright38dcdff2014-03-19 12:06:10 -0700368 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700369 for (uint32_t i = 0; i < count; i++) {
370 parcel->writeFloat(values[i]);
371 }
372 return OK;
373}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800374#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700375
376void PointerCoords::tooManyAxes(int axis) {
377 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
378 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
379}
380
381bool PointerCoords::operator==(const PointerCoords& other) const {
382 if (bits != other.bits) {
383 return false;
384 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700385 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700386 for (uint32_t i = 0; i < count; i++) {
387 if (values[i] != other.values[i]) {
388 return false;
389 }
390 }
391 return true;
392}
393
394void PointerCoords::copyFrom(const PointerCoords& other) {
395 bits = other.bits;
Michael Wright38dcdff2014-03-19 12:06:10 -0700396 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700397 for (uint32_t i = 0; i < count; i++) {
398 values[i] = other.values[i];
399 }
400}
401
chaviwc01e1372020-07-01 12:37:31 -0700402void PointerCoords::transform(const ui::Transform& transform) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700403 const vec2 xy = transform.transform(getXYValue());
404 setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
405 setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
406
Prabir Pradhanc6523582021-05-14 18:02:55 -0700407 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_X) ||
408 BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_Y)) {
409 const ui::Transform rotation(transform.getOrientation());
410 const vec2 relativeXy = rotation.transform(getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
411 getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
412 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
413 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
414 }
415
Prabir Pradhan6b384612021-05-14 16:56:25 -0700416 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) {
417 const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
418 setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val));
419 }
chaviwc01e1372020-07-01 12:37:31 -0700420}
Jeff Brown5912f952013-07-01 19:10:31 -0700421
422// --- PointerProperties ---
423
424bool PointerProperties::operator==(const PointerProperties& other) const {
425 return id == other.id
426 && toolType == other.toolType;
427}
428
429void PointerProperties::copyFrom(const PointerProperties& other) {
430 id = other.id;
431 toolType = other.toolType;
432}
433
434
435// --- MotionEvent ---
436
Garfield Tan4cc839f2020-01-24 11:26:14 -0800437void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600438 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
439 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700440 int32_t buttonState, MotionClassification classification,
441 const ui::Transform& transform, float xPrecision, float yPrecision,
Evan Rosky84f07f02021-04-16 10:42:42 -0700442 float rawXCursorPosition, float rawYCursorPosition,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700443 const ui::Transform& rawTransform, nsecs_t downTime, nsecs_t eventTime,
Evan Rosky09576692021-07-01 12:22:09 -0700444 size_t pointerCount, const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700445 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800446 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700447 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100448 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700449 mFlags = flags;
450 mEdgeFlags = edgeFlags;
451 mMetaState = metaState;
452 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800453 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700454 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700455 mXPrecision = xPrecision;
456 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700457 mRawXCursorPosition = rawXCursorPosition;
458 mRawYCursorPosition = rawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700459 mRawTransform = rawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700460 mDownTime = downTime;
461 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800462 mPointerProperties.insert(mPointerProperties.end(), &pointerProperties[0],
463 &pointerProperties[pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700464 mSampleEventTimes.clear();
465 mSamplePointerCoords.clear();
466 addSample(eventTime, pointerCoords);
467}
468
469void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800470 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
471 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700472 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100473 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700474 mFlags = other->mFlags;
475 mEdgeFlags = other->mEdgeFlags;
476 mMetaState = other->mMetaState;
477 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800478 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700479 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700480 mXPrecision = other->mXPrecision;
481 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700482 mRawXCursorPosition = other->mRawXCursorPosition;
483 mRawYCursorPosition = other->mRawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700484 mRawTransform = other->mRawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700485 mDownTime = other->mDownTime;
486 mPointerProperties = other->mPointerProperties;
487
488 if (keepHistory) {
489 mSampleEventTimes = other->mSampleEventTimes;
490 mSamplePointerCoords = other->mSamplePointerCoords;
491 } else {
492 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500493 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700494 mSamplePointerCoords.clear();
495 size_t pointerCount = other->getPointerCount();
496 size_t historySize = other->getHistorySize();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800497 mSamplePointerCoords
498 .insert(mSamplePointerCoords.end(),
499 &other->mSamplePointerCoords[historySize * pointerCount],
500 &other->mSamplePointerCoords[historySize * pointerCount + pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700501 }
502}
503
504void MotionEvent::addSample(
505 int64_t eventTime,
506 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500507 mSampleEventTimes.push_back(eventTime);
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800508 mSamplePointerCoords.insert(mSamplePointerCoords.end(), &pointerCoords[0],
509 &pointerCoords[getPointerCount()]);
Jeff Brown5912f952013-07-01 19:10:31 -0700510}
511
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800512int MotionEvent::getSurfaceRotation() const {
513 // The surface rotation is the rotation from the window's coordinate space to that of the
514 // display. Since the event's transform takes display space coordinates to window space, the
515 // returned surface rotation is the inverse of the rotation for the surface.
516 switch (mTransform.getOrientation()) {
517 case ui::Transform::ROT_0:
518 return DISPLAY_ORIENTATION_0;
519 case ui::Transform::ROT_90:
520 return DISPLAY_ORIENTATION_270;
521 case ui::Transform::ROT_180:
522 return DISPLAY_ORIENTATION_180;
523 case ui::Transform::ROT_270:
524 return DISPLAY_ORIENTATION_90;
525 default:
526 return -1;
527 }
528}
529
Garfield Tan00f511d2019-06-12 16:55:40 -0700530float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700531 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
532 return vals.x;
Garfield Tan00f511d2019-06-12 16:55:40 -0700533}
534
535float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700536 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
537 return vals.y;
Garfield Tan00f511d2019-06-12 16:55:40 -0700538}
539
Garfield Tan937bb832019-07-25 17:48:31 -0700540void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700541 ui::Transform inverse = mTransform.inverse();
542 vec2 vals = inverse.transform(x, y);
543 mRawXCursorPosition = vals.x;
544 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700545}
546
Jeff Brown5912f952013-07-01 19:10:31 -0700547const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000548 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
549 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
550 }
551 const size_t position = getHistorySize() * getPointerCount() + pointerIndex;
552 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
553 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
554 }
555 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700556}
557
558float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
Evan Rosky84f07f02021-04-16 10:42:42 -0700559 return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700560}
561
562float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700563 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700564}
565
566const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
567 size_t pointerIndex, size_t historicalIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000568 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
569 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
570 }
571 if (CC_UNLIKELY(historicalIndex < 0 || historicalIndex > getHistorySize())) {
572 LOG(FATAL) << __func__ << ": Invalid historical index " << historicalIndex << " for "
573 << *this;
574 }
575 const size_t position = historicalIndex * getPointerCount() + pointerIndex;
576 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
577 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
578 }
579 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700580}
581
582float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan6b384612021-05-14 16:56:25 -0700583 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700584 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
585 return calculateTransformedAxisValue(axis, mSource, mRawTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700586}
587
588float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan9f388812021-05-13 16:54:53 -0700589 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700590 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
591 return calculateTransformedAxisValue(axis, mSource, mTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700592}
593
594ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
595 size_t pointerCount = mPointerProperties.size();
596 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800597 if (mPointerProperties[i].id == pointerId) {
Jeff Brown5912f952013-07-01 19:10:31 -0700598 return i;
599 }
600 }
601 return -1;
602}
603
604void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700605 float currXOffset = mTransform.tx();
606 float currYOffset = mTransform.ty();
607 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700608}
609
Robert Carre07e1032018-11-26 12:55:53 -0800610void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700611 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700612 mRawTransform.set(mRawTransform.tx() * globalScaleFactor,
613 mRawTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800614 mXPrecision *= globalScaleFactor;
615 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700616
617 size_t numSamples = mSamplePointerCoords.size();
618 for (size_t i = 0; i < numSamples; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800619 mSamplePointerCoords[i].scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700620 }
621}
622
chaviw9eaa22c2020-07-01 16:21:27 -0700623void MotionEvent::transform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700624 // We want to preserve the raw axes values stored in the PointerCoords, so we just update the
625 // transform using the values passed in.
chaviw9eaa22c2020-07-01 16:21:27 -0700626 ui::Transform newTransform;
627 newTransform.set(matrix);
628 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700629}
630
Evan Roskyd4d4d802021-05-03 20:12:21 -0700631void MotionEvent::applyTransform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700632 ui::Transform transform;
633 transform.set(matrix);
Evan Roskyd4d4d802021-05-03 20:12:21 -0700634
635 // Apply the transformation to all samples.
Prabir Pradhan6b384612021-05-14 16:56:25 -0700636 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
637 [&transform](PointerCoords& c) { c.transform(transform); });
Prabir Pradhan4b19bd02021-06-01 17:34:59 -0700638
639 if (mRawXCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION &&
640 mRawYCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) {
641 const vec2 cursor = transform.transform(mRawXCursorPosition, mRawYCursorPosition);
642 mRawXCursorPosition = cursor.x;
643 mRawYCursorPosition = cursor.y;
644 }
Evan Roskyd4d4d802021-05-03 20:12:21 -0700645}
646
Brett Chabotfaa986c2020-11-04 17:39:36 -0800647#ifdef __linux__
chaviw9eaa22c2020-07-01 16:21:27 -0700648static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
649 float dsdx, dtdx, tx, dtdy, dsdy, ty;
650 status_t status = parcel.readFloat(&dsdx);
651 status |= parcel.readFloat(&dtdx);
652 status |= parcel.readFloat(&tx);
653 status |= parcel.readFloat(&dtdy);
654 status |= parcel.readFloat(&dsdy);
655 status |= parcel.readFloat(&ty);
656
657 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
658 return status;
659}
660
661static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
662 status_t status = parcel.writeFloat(transform.dsdx());
663 status |= parcel.writeFloat(transform.dtdx());
664 status |= parcel.writeFloat(transform.tx());
665 status |= parcel.writeFloat(transform.dtdy());
666 status |= parcel.writeFloat(transform.dsdy());
667 status |= parcel.writeFloat(transform.ty());
668 return status;
669}
670
Jeff Brown5912f952013-07-01 19:10:31 -0700671status_t MotionEvent::readFromParcel(Parcel* parcel) {
672 size_t pointerCount = parcel->readInt32();
673 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800674 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
675 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700676 return BAD_VALUE;
677 }
678
Garfield Tan4cc839f2020-01-24 11:26:14 -0800679 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700680 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600681 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800682 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600683 std::vector<uint8_t> hmac;
684 status_t result = parcel->readByteVector(&hmac);
685 if (result != OK || hmac.size() != 32) {
686 return BAD_VALUE;
687 }
688 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700689 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100690 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700691 mFlags = parcel->readInt32();
692 mEdgeFlags = parcel->readInt32();
693 mMetaState = parcel->readInt32();
694 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800695 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700696
697 result = android::readFromParcel(mTransform, *parcel);
698 if (result != OK) {
699 return result;
700 }
Jeff Brown5912f952013-07-01 19:10:31 -0700701 mXPrecision = parcel->readFloat();
702 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700703 mRawXCursorPosition = parcel->readFloat();
704 mRawYCursorPosition = parcel->readFloat();
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700705
706 result = android::readFromParcel(mRawTransform, *parcel);
707 if (result != OK) {
708 return result;
709 }
Jeff Brown5912f952013-07-01 19:10:31 -0700710 mDownTime = parcel->readInt64();
711
712 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800713 mPointerProperties.reserve(pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700714 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500715 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700716 mSamplePointerCoords.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800717 mSamplePointerCoords.reserve(sampleCount * pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700718
719 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800720 mPointerProperties.push_back({});
721 PointerProperties& properties = mPointerProperties.back();
Jeff Brown5912f952013-07-01 19:10:31 -0700722 properties.id = parcel->readInt32();
723 properties.toolType = parcel->readInt32();
724 }
725
Dan Austinc94fc452015-09-22 14:22:41 -0700726 while (sampleCount > 0) {
727 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500728 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700729 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800730 mSamplePointerCoords.push_back({});
731 status_t status = mSamplePointerCoords.back().readFromParcel(parcel);
Jeff Brown5912f952013-07-01 19:10:31 -0700732 if (status) {
733 return status;
734 }
735 }
736 }
737 return OK;
738}
739
740status_t MotionEvent::writeToParcel(Parcel* parcel) const {
741 size_t pointerCount = mPointerProperties.size();
742 size_t sampleCount = mSampleEventTimes.size();
743
744 parcel->writeInt32(pointerCount);
745 parcel->writeInt32(sampleCount);
746
Garfield Tan4cc839f2020-01-24 11:26:14 -0800747 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700748 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600749 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800750 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600751 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
752 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700753 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100754 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700755 parcel->writeInt32(mFlags);
756 parcel->writeInt32(mEdgeFlags);
757 parcel->writeInt32(mMetaState);
758 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800759 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700760
761 status_t result = android::writeToParcel(mTransform, *parcel);
762 if (result != OK) {
763 return result;
764 }
Jeff Brown5912f952013-07-01 19:10:31 -0700765 parcel->writeFloat(mXPrecision);
766 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700767 parcel->writeFloat(mRawXCursorPosition);
768 parcel->writeFloat(mRawYCursorPosition);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700769
770 result = android::writeToParcel(mRawTransform, *parcel);
771 if (result != OK) {
772 return result;
773 }
Jeff Brown5912f952013-07-01 19:10:31 -0700774 parcel->writeInt64(mDownTime);
775
776 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800777 const PointerProperties& properties = mPointerProperties[i];
Jeff Brown5912f952013-07-01 19:10:31 -0700778 parcel->writeInt32(properties.id);
779 parcel->writeInt32(properties.toolType);
780 }
781
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800782 const PointerCoords* pc = mSamplePointerCoords.data();
Jeff Brown5912f952013-07-01 19:10:31 -0700783 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500784 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700785 for (size_t i = 0; i < pointerCount; i++) {
786 status_t status = (pc++)->writeToParcel(parcel);
787 if (status) {
788 return status;
789 }
790 }
791 }
792 return OK;
793}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800794#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700795
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600796bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700797 if (isFromSource(source, AINPUT_SOURCE_CLASS_POINTER)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700798 // Specifically excludes HOVER_MOVE and SCROLL.
799 switch (action & AMOTION_EVENT_ACTION_MASK) {
800 case AMOTION_EVENT_ACTION_DOWN:
801 case AMOTION_EVENT_ACTION_MOVE:
802 case AMOTION_EVENT_ACTION_UP:
803 case AMOTION_EVENT_ACTION_POINTER_DOWN:
804 case AMOTION_EVENT_ACTION_POINTER_UP:
805 case AMOTION_EVENT_ACTION_CANCEL:
806 case AMOTION_EVENT_ACTION_OUTSIDE:
807 return true;
808 }
809 }
810 return false;
811}
812
Michael Wright872db4f2014-04-22 15:03:51 -0700813const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700814 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700815}
816
817int32_t MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700818 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700819}
820
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500821std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700822 // Convert MotionEvent action to string
823 switch (action & AMOTION_EVENT_ACTION_MASK) {
824 case AMOTION_EVENT_ACTION_DOWN:
825 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700826 case AMOTION_EVENT_ACTION_UP:
827 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500828 case AMOTION_EVENT_ACTION_MOVE:
829 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700830 case AMOTION_EVENT_ACTION_CANCEL:
831 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500832 case AMOTION_EVENT_ACTION_OUTSIDE:
833 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700834 case AMOTION_EVENT_ACTION_POINTER_DOWN:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000835 return StringPrintf("POINTER_DOWN(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700836 case AMOTION_EVENT_ACTION_POINTER_UP:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000837 return StringPrintf("POINTER_UP(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500838 case AMOTION_EVENT_ACTION_HOVER_MOVE:
839 return "HOVER_MOVE";
840 case AMOTION_EVENT_ACTION_SCROLL:
841 return "SCROLL";
842 case AMOTION_EVENT_ACTION_HOVER_ENTER:
843 return "HOVER_ENTER";
844 case AMOTION_EVENT_ACTION_HOVER_EXIT:
845 return "HOVER_EXIT";
846 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
847 return "BUTTON_PRESS";
848 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
849 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700850 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500851 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700852}
853
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700854// Apply the given transformation to the point without checking whether the entire transform
855// should be disregarded altogether for the provided source.
856static inline vec2 calculateTransformedXYUnchecked(uint32_t source, const ui::Transform& transform,
857 const vec2& xy) {
858 return shouldDisregardOffset(source) ? transformWithoutTranslation(transform, xy)
859 : transform.transform(xy);
860}
861
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700862vec2 MotionEvent::calculateTransformedXY(uint32_t source, const ui::Transform& transform,
863 const vec2& xy) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700864 if (shouldDisregardTransformation(source)) {
865 return xy;
866 }
867 return calculateTransformedXYUnchecked(source, transform, xy);
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700868}
869
Prabir Pradhan8e6ce222022-02-24 09:08:54 -0800870// Keep in sync with calculateTransformedCoords.
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700871float MotionEvent::calculateTransformedAxisValue(int32_t axis, uint32_t source,
872 const ui::Transform& transform,
873 const PointerCoords& coords) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700874 if (shouldDisregardTransformation(source)) {
875 return coords.getAxisValue(axis);
876 }
877
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700878 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700879 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700880 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
881 return xy[axis];
882 }
883
884 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
885 const vec2 relativeXy =
886 transformWithoutTranslation(transform,
887 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
888 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
889 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
890 }
891
892 if (axis == AMOTION_EVENT_AXIS_ORIENTATION) {
893 return transformAngle(transform, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION));
894 }
895
896 return coords.getAxisValue(axis);
897}
898
Prabir Pradhan8e6ce222022-02-24 09:08:54 -0800899// Keep in sync with calculateTransformedAxisValue. This is an optimization of
900// calculateTransformedAxisValue for all PointerCoords axes.
901PointerCoords MotionEvent::calculateTransformedCoords(uint32_t source,
902 const ui::Transform& transform,
903 const PointerCoords& coords) {
904 if (shouldDisregardTransformation(source)) {
905 return coords;
906 }
907 PointerCoords out = coords;
908
909 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
910 out.setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
911 out.setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
912
913 const vec2 relativeXy =
914 transformWithoutTranslation(transform,
915 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
916 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
917 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
918 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
919
920 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
921 transformAngle(transform,
922 coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)));
923
924 return out;
925}
926
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000927std::ostream& operator<<(std::ostream& out, const MotionEvent& event) {
928 out << "MotionEvent { action=" << MotionEvent::actionToString(event.getAction());
929 if (event.getActionButton() != 0) {
930 out << ", actionButton=" << std::to_string(event.getActionButton());
931 }
932 const size_t pointerCount = event.getPointerCount();
933 for (size_t i = 0; i < pointerCount; i++) {
934 out << ", id[" << i << "]=" << event.getPointerId(i);
935 float x = event.getX(i);
936 float y = event.getY(i);
937 if (x != 0 || y != 0) {
938 out << ", x[" << i << "]=" << x;
939 out << ", y[" << i << "]=" << y;
940 }
941 int toolType = event.getToolType(i);
942 if (toolType != AMOTION_EVENT_TOOL_TYPE_FINGER) {
943 out << ", toolType[" << i << "]=" << toolType;
944 }
945 }
946 if (event.getButtonState() != 0) {
947 out << ", buttonState=" << event.getButtonState();
948 }
949 if (event.getClassification() != MotionClassification::NONE) {
950 out << ", classification=" << motionClassificationToString(event.getClassification());
951 }
952 if (event.getMetaState() != 0) {
953 out << ", metaState=" << event.getMetaState();
954 }
955 if (event.getEdgeFlags() != 0) {
956 out << ", edgeFlags=" << event.getEdgeFlags();
957 }
958 if (pointerCount != 1) {
959 out << ", pointerCount=" << pointerCount;
960 }
961 if (event.getHistorySize() != 0) {
962 out << ", historySize=" << event.getHistorySize();
963 }
964 out << ", eventTime=" << event.getEventTime();
965 out << ", downTime=" << event.getDownTime();
966 out << ", deviceId=" << event.getDeviceId();
967 out << ", source=" << inputEventSourceToString(event.getSource());
968 out << ", displayId=" << event.getDisplayId();
969 out << ", eventId=" << event.getId();
970 out << "}";
971 return out;
972}
973
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800974// --- FocusEvent ---
975
Antonio Kantek3cfec7b2021-11-05 18:26:17 -0700976void FocusEvent::initialize(int32_t id, bool hasFocus) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800977 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600978 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800979 mHasFocus = hasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800980}
981
982void FocusEvent::initialize(const FocusEvent& from) {
983 InputEvent::initialize(from);
984 mHasFocus = from.mHasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800985}
Jeff Brown5912f952013-07-01 19:10:31 -0700986
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800987// --- CaptureEvent ---
988
989void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) {
990 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
991 ADISPLAY_ID_NONE, INVALID_HMAC);
992 mPointerCaptureEnabled = pointerCaptureEnabled;
993}
994
995void CaptureEvent::initialize(const CaptureEvent& from) {
996 InputEvent::initialize(from);
997 mPointerCaptureEnabled = from.mPointerCaptureEnabled;
998}
999
arthurhung7632c332020-12-30 16:58:01 +08001000// --- DragEvent ---
1001
1002void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) {
1003 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1004 ADISPLAY_ID_NONE, INVALID_HMAC);
1005 mIsExiting = isExiting;
1006 mX = x;
1007 mY = y;
1008}
1009
1010void DragEvent::initialize(const DragEvent& from) {
1011 InputEvent::initialize(from);
1012 mIsExiting = from.mIsExiting;
1013 mX = from.mX;
1014 mY = from.mY;
1015}
1016
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001017// --- TouchModeEvent ---
1018
1019void TouchModeEvent::initialize(int32_t id, bool isInTouchMode) {
1020 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1021 ADISPLAY_ID_NONE, INVALID_HMAC);
1022 mIsInTouchMode = isInTouchMode;
1023}
1024
1025void TouchModeEvent::initialize(const TouchModeEvent& from) {
1026 InputEvent::initialize(from);
1027 mIsInTouchMode = from.mIsInTouchMode;
1028}
1029
Jeff Brown5912f952013-07-01 19:10:31 -07001030// --- PooledInputEventFactory ---
1031
1032PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
1033 mMaxPoolSize(maxPoolSize) {
1034}
1035
1036PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -07001037}
1038
1039KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001040 if (mKeyEventPool.empty()) {
1041 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001042 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001043 KeyEvent* event = mKeyEventPool.front().release();
1044 mKeyEventPool.pop();
1045 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001046}
1047
1048MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001049 if (mMotionEventPool.empty()) {
1050 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001051 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001052 MotionEvent* event = mMotionEventPool.front().release();
1053 mMotionEventPool.pop();
1054 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001055}
1056
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001057FocusEvent* PooledInputEventFactory::createFocusEvent() {
1058 if (mFocusEventPool.empty()) {
1059 return new FocusEvent();
1060 }
1061 FocusEvent* event = mFocusEventPool.front().release();
1062 mFocusEventPool.pop();
1063 return event;
1064}
1065
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001066CaptureEvent* PooledInputEventFactory::createCaptureEvent() {
1067 if (mCaptureEventPool.empty()) {
1068 return new CaptureEvent();
1069 }
1070 CaptureEvent* event = mCaptureEventPool.front().release();
1071 mCaptureEventPool.pop();
1072 return event;
1073}
1074
arthurhung7632c332020-12-30 16:58:01 +08001075DragEvent* PooledInputEventFactory::createDragEvent() {
1076 if (mDragEventPool.empty()) {
1077 return new DragEvent();
1078 }
1079 DragEvent* event = mDragEventPool.front().release();
1080 mDragEventPool.pop();
1081 return event;
1082}
1083
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001084TouchModeEvent* PooledInputEventFactory::createTouchModeEvent() {
1085 if (mTouchModeEventPool.empty()) {
1086 return new TouchModeEvent();
1087 }
1088 TouchModeEvent* event = mTouchModeEventPool.front().release();
1089 mTouchModeEventPool.pop();
1090 return event;
1091}
1092
Jeff Brown5912f952013-07-01 19:10:31 -07001093void PooledInputEventFactory::recycle(InputEvent* event) {
1094 switch (event->getType()) {
1095 case AINPUT_EVENT_TYPE_KEY:
1096 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001097 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -07001098 return;
1099 }
1100 break;
1101 case AINPUT_EVENT_TYPE_MOTION:
1102 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001103 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -07001104 return;
1105 }
1106 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001107 case AINPUT_EVENT_TYPE_FOCUS:
1108 if (mFocusEventPool.size() < mMaxPoolSize) {
1109 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
1110 return;
1111 }
1112 break;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001113 case AINPUT_EVENT_TYPE_CAPTURE:
1114 if (mCaptureEventPool.size() < mMaxPoolSize) {
1115 mCaptureEventPool.push(
1116 std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event)));
1117 return;
1118 }
1119 break;
arthurhung7632c332020-12-30 16:58:01 +08001120 case AINPUT_EVENT_TYPE_DRAG:
1121 if (mDragEventPool.size() < mMaxPoolSize) {
1122 mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event)));
1123 return;
1124 }
1125 break;
Antonio Kantekeb4a30c2021-09-28 17:49:49 -07001126 case AINPUT_EVENT_TYPE_TOUCH_MODE:
1127 if (mTouchModeEventPool.size() < mMaxPoolSize) {
1128 mTouchModeEventPool.push(
1129 std::unique_ptr<TouchModeEvent>(static_cast<TouchModeEvent*>(event)));
1130 return;
1131 }
1132 break;
Jeff Brown5912f952013-07-01 19:10:31 -07001133 }
1134 delete event;
1135}
1136
1137} // namespace android