blob: 4127f7ce1068128b25c290ed33e3d85d114ecccd [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) {
67 // Do not apply any transformations to axes from joysticks or touchpads.
68 return isFromSource(source, AINPUT_SOURCE_CLASS_JOYSTICK) ||
69 isFromSource(source, AINPUT_SOURCE_CLASS_POSITION);
70}
71
72bool shouldDisregardOffset(uint32_t source) {
Prabir Pradhan9f388812021-05-13 16:54:53 -070073 // Pointer events are the only type of events that refer to absolute coordinates on the display,
74 // so we should apply the entire window transform. For other types of events, we should make
75 // sure to not apply the window translation/offset.
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070076 return !isFromSource(source, AINPUT_SOURCE_CLASS_POINTER);
Prabir Pradhan9f388812021-05-13 16:54:53 -070077}
78
Prabir Pradhan6b384612021-05-14 16:56:25 -070079} // namespace
80
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -080081const char* motionClassificationToString(MotionClassification classification) {
82 switch (classification) {
83 case MotionClassification::NONE:
84 return "NONE";
85 case MotionClassification::AMBIGUOUS_GESTURE:
86 return "AMBIGUOUS_GESTURE";
87 case MotionClassification::DEEP_PRESS:
88 return "DEEP_PRESS";
89 }
90}
91
Garfield Tan84b087e2020-01-23 10:49:05 -080092// --- IdGenerator ---
93IdGenerator::IdGenerator(Source source) : mSource(source) {}
94
95int32_t IdGenerator::nextId() const {
96 constexpr uint32_t SEQUENCE_NUMBER_MASK = ~SOURCE_MASK;
97 int32_t id = 0;
98
99// Avoid building against syscall getrandom(2) on host, which will fail build on Mac. Host doesn't
100// use sequence number so just always return mSource.
101#ifdef __ANDROID__
102 constexpr size_t BUF_LEN = sizeof(id);
103 size_t totalBytes = 0;
104 while (totalBytes < BUF_LEN) {
105 ssize_t bytes = TEMP_FAILURE_RETRY(getrandom(&id, BUF_LEN, GRND_NONBLOCK));
106 if (CC_UNLIKELY(bytes < 0)) {
107 ALOGW("Failed to fill in random number for sequence number: %s.", strerror(errno));
108 id = 0;
109 break;
110 }
111 totalBytes += bytes;
112 }
113#endif // __ANDROID__
114
115 return (id & SEQUENCE_NUMBER_MASK) | static_cast<int32_t>(mSource);
116}
117
Jeff Brown5912f952013-07-01 19:10:31 -0700118// --- InputEvent ---
119
Prabir Pradhande69f8a2021-11-18 16:40:34 +0000120vec2 transformWithoutTranslation(const ui::Transform& transform, const vec2& xy) {
121 const vec2 transformedXy = transform.transform(xy);
122 const vec2 transformedOrigin = transform.transform(0, 0);
123 return transformedXy - transformedOrigin;
124}
125
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800126const char* inputEventTypeToString(int32_t type) {
127 switch (type) {
128 case AINPUT_EVENT_TYPE_KEY: {
129 return "KEY";
130 }
131 case AINPUT_EVENT_TYPE_MOTION: {
132 return "MOTION";
133 }
134 case AINPUT_EVENT_TYPE_FOCUS: {
135 return "FOCUS";
136 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800137 case AINPUT_EVENT_TYPE_CAPTURE: {
138 return "CAPTURE";
139 }
arthurhung7632c332020-12-30 16:58:01 +0800140 case AINPUT_EVENT_TYPE_DRAG: {
141 return "DRAG";
142 }
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700143 case AINPUT_EVENT_TYPE_TOUCH_MODE: {
144 return "TOUCH_MODE";
145 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800146 }
147 return "UNKNOWN";
148}
149
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800150std::string inputEventSourceToString(int32_t source) {
151 if (source == AINPUT_SOURCE_UNKNOWN) {
152 return "UNKNOWN";
153 }
154 if (source == static_cast<int32_t>(AINPUT_SOURCE_ANY)) {
155 return "ANY";
156 }
157 static const std::map<int32_t, const char*> SOURCES{
158 {AINPUT_SOURCE_KEYBOARD, "KEYBOARD"},
159 {AINPUT_SOURCE_DPAD, "DPAD"},
160 {AINPUT_SOURCE_GAMEPAD, "GAMEPAD"},
161 {AINPUT_SOURCE_TOUCHSCREEN, "TOUCHSCREEN"},
162 {AINPUT_SOURCE_MOUSE, "MOUSE"},
163 {AINPUT_SOURCE_STYLUS, "STYLUS"},
164 {AINPUT_SOURCE_BLUETOOTH_STYLUS, "BLUETOOTH_STYLUS"},
165 {AINPUT_SOURCE_TRACKBALL, "TRACKBALL"},
166 {AINPUT_SOURCE_MOUSE_RELATIVE, "MOUSE_RELATIVE"},
167 {AINPUT_SOURCE_TOUCHPAD, "TOUCHPAD"},
168 {AINPUT_SOURCE_TOUCH_NAVIGATION, "TOUCH_NAVIGATION"},
169 {AINPUT_SOURCE_JOYSTICK, "JOYSTICK"},
170 {AINPUT_SOURCE_HDMI, "HDMI"},
171 {AINPUT_SOURCE_SENSOR, "SENSOR"},
172 {AINPUT_SOURCE_ROTARY_ENCODER, "ROTARY_ENCODER"},
173 };
174 std::string result;
175 for (const auto& [source_entry, str] : SOURCES) {
176 if ((source & source_entry) == source_entry) {
177 if (!result.empty()) {
178 result += " | ";
179 }
180 result += str;
181 }
182 }
183 if (result.empty()) {
184 result = StringPrintf("0x%08x", source);
185 }
186 return result;
187}
188
189bool isFromSource(uint32_t source, uint32_t test) {
190 return (source & test) == test;
191}
192
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800193VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
194 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
195 event.getSource(), event.getDisplayId()},
196 event.getAction(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800197 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800198 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800199 event.getKeyCode(),
200 event.getScanCode(),
201 event.getMetaState(),
202 event.getRepeatCount()};
203}
204
205VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
206 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
207 event.getSource(), event.getDisplayId()},
208 event.getRawX(0),
209 event.getRawY(0),
210 event.getActionMasked(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800211 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800212 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800213 event.getMetaState(),
214 event.getButtonState()};
215}
216
Garfield Tan4cc839f2020-01-24 11:26:14 -0800217void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600218 std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800219 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700220 mDeviceId = deviceId;
221 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100222 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600223 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700224}
225
226void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800227 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700228 mDeviceId = from.mDeviceId;
229 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100230 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600231 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700232}
233
Garfield Tan4cc839f2020-01-24 11:26:14 -0800234int32_t InputEvent::nextId() {
235 static IdGenerator idGen(IdGenerator::Source::OTHER);
236 return idGen.nextId();
237}
238
Jeff Brown5912f952013-07-01 19:10:31 -0700239// --- KeyEvent ---
240
Michael Wright872db4f2014-04-22 15:03:51 -0700241const char* KeyEvent::getLabel(int32_t keyCode) {
Chris Ye4958d062020-08-20 13:21:10 -0700242 return InputEventLookup::getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700243}
244
Michael Wright872db4f2014-04-22 15:03:51 -0700245int32_t KeyEvent::getKeyCodeFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700246 return InputEventLookup::getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700247}
248
Garfield Tan4cc839f2020-01-24 11:26:14 -0800249void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600250 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
251 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
252 nsecs_t downTime, nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800253 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700254 mAction = action;
255 mFlags = flags;
256 mKeyCode = keyCode;
257 mScanCode = scanCode;
258 mMetaState = metaState;
259 mRepeatCount = repeatCount;
260 mDownTime = downTime;
261 mEventTime = eventTime;
262}
263
264void KeyEvent::initialize(const KeyEvent& from) {
265 InputEvent::initialize(from);
266 mAction = from.mAction;
267 mFlags = from.mFlags;
268 mKeyCode = from.mKeyCode;
269 mScanCode = from.mScanCode;
270 mMetaState = from.mMetaState;
271 mRepeatCount = from.mRepeatCount;
272 mDownTime = from.mDownTime;
273 mEventTime = from.mEventTime;
274}
275
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700276const char* KeyEvent::actionToString(int32_t action) {
277 // Convert KeyEvent action to string
278 switch (action) {
279 case AKEY_EVENT_ACTION_DOWN:
280 return "DOWN";
281 case AKEY_EVENT_ACTION_UP:
282 return "UP";
283 case AKEY_EVENT_ACTION_MULTIPLE:
284 return "MULTIPLE";
285 }
286 return "UNKNOWN";
287}
Jeff Brown5912f952013-07-01 19:10:31 -0700288
289// --- PointerCoords ---
290
291float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700292 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700293 return 0;
294 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700295 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700296}
297
298status_t PointerCoords::setAxisValue(int32_t axis, float value) {
299 if (axis < 0 || axis > 63) {
300 return NAME_NOT_FOUND;
301 }
302
Michael Wright38dcdff2014-03-19 12:06:10 -0700303 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
304 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700305 if (value == 0) {
306 return OK; // axes with value 0 do not need to be stored
307 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700308
309 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700310 if (count >= MAX_AXES) {
311 tooManyAxes(axis);
312 return NO_MEMORY;
313 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700314 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700315 for (uint32_t i = count; i > index; i--) {
316 values[i] = values[i - 1];
317 }
318 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700319
Jeff Brown5912f952013-07-01 19:10:31 -0700320 values[index] = value;
321 return OK;
322}
323
324static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
325 float value = c.getAxisValue(axis);
326 if (value != 0) {
327 c.setAxisValue(axis, value * scaleFactor);
328 }
329}
330
Robert Carre07e1032018-11-26 12:55:53 -0800331void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700332 // No need to scale pressure or size since they are normalized.
333 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800334
335 // If there is a global scale factor, it is included in the windowX/YScale
336 // so we don't need to apply it twice to the X/Y axes.
337 // However we don't want to apply any windowXYScale not included in the global scale
338 // to the TOUCH_MAJOR/MINOR coordinates.
339 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
340 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
341 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
342 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
343 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
344 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
Prabir Pradhanc6523582021-05-14 18:02:55 -0700345 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_X, windowXScale);
346 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_Y, windowYScale);
Robert Carre07e1032018-11-26 12:55:53 -0800347}
348
Brett Chabotfaa986c2020-11-04 17:39:36 -0800349#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700350status_t PointerCoords::readFromParcel(Parcel* parcel) {
351 bits = parcel->readInt64();
352
Michael Wright38dcdff2014-03-19 12:06:10 -0700353 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700354 if (count > MAX_AXES) {
355 return BAD_VALUE;
356 }
357
358 for (uint32_t i = 0; i < count; i++) {
359 values[i] = parcel->readFloat();
360 }
361 return OK;
362}
363
364status_t PointerCoords::writeToParcel(Parcel* parcel) const {
365 parcel->writeInt64(bits);
366
Michael Wright38dcdff2014-03-19 12:06:10 -0700367 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700368 for (uint32_t i = 0; i < count; i++) {
369 parcel->writeFloat(values[i]);
370 }
371 return OK;
372}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800373#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700374
375void PointerCoords::tooManyAxes(int axis) {
376 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
377 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
378}
379
380bool PointerCoords::operator==(const PointerCoords& other) const {
381 if (bits != other.bits) {
382 return false;
383 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700384 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700385 for (uint32_t i = 0; i < count; i++) {
386 if (values[i] != other.values[i]) {
387 return false;
388 }
389 }
390 return true;
391}
392
393void PointerCoords::copyFrom(const PointerCoords& other) {
394 bits = other.bits;
Michael Wright38dcdff2014-03-19 12:06:10 -0700395 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700396 for (uint32_t i = 0; i < count; i++) {
397 values[i] = other.values[i];
398 }
399}
400
chaviwc01e1372020-07-01 12:37:31 -0700401void PointerCoords::transform(const ui::Transform& transform) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700402 const vec2 xy = transform.transform(getXYValue());
403 setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
404 setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
405
Prabir Pradhanc6523582021-05-14 18:02:55 -0700406 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_X) ||
407 BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_Y)) {
408 const ui::Transform rotation(transform.getOrientation());
409 const vec2 relativeXy = rotation.transform(getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
410 getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
411 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
412 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
413 }
414
Prabir Pradhan6b384612021-05-14 16:56:25 -0700415 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) {
416 const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
417 setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val));
418 }
chaviwc01e1372020-07-01 12:37:31 -0700419}
Jeff Brown5912f952013-07-01 19:10:31 -0700420
421// --- PointerProperties ---
422
423bool PointerProperties::operator==(const PointerProperties& other) const {
424 return id == other.id
425 && toolType == other.toolType;
426}
427
428void PointerProperties::copyFrom(const PointerProperties& other) {
429 id = other.id;
430 toolType = other.toolType;
431}
432
433
434// --- MotionEvent ---
435
Garfield Tan4cc839f2020-01-24 11:26:14 -0800436void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600437 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
438 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700439 int32_t buttonState, MotionClassification classification,
440 const ui::Transform& transform, float xPrecision, float yPrecision,
Evan Rosky84f07f02021-04-16 10:42:42 -0700441 float rawXCursorPosition, float rawYCursorPosition,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700442 const ui::Transform& rawTransform, nsecs_t downTime, nsecs_t eventTime,
Evan Rosky09576692021-07-01 12:22:09 -0700443 size_t pointerCount, const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700444 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800445 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700446 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100447 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700448 mFlags = flags;
449 mEdgeFlags = edgeFlags;
450 mMetaState = metaState;
451 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800452 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700453 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700454 mXPrecision = xPrecision;
455 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700456 mRawXCursorPosition = rawXCursorPosition;
457 mRawYCursorPosition = rawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700458 mRawTransform = rawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700459 mDownTime = downTime;
460 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800461 mPointerProperties.insert(mPointerProperties.end(), &pointerProperties[0],
462 &pointerProperties[pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700463 mSampleEventTimes.clear();
464 mSamplePointerCoords.clear();
465 addSample(eventTime, pointerCoords);
466}
467
468void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800469 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
470 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700471 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100472 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700473 mFlags = other->mFlags;
474 mEdgeFlags = other->mEdgeFlags;
475 mMetaState = other->mMetaState;
476 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800477 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700478 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700479 mXPrecision = other->mXPrecision;
480 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700481 mRawXCursorPosition = other->mRawXCursorPosition;
482 mRawYCursorPosition = other->mRawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700483 mRawTransform = other->mRawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700484 mDownTime = other->mDownTime;
485 mPointerProperties = other->mPointerProperties;
486
487 if (keepHistory) {
488 mSampleEventTimes = other->mSampleEventTimes;
489 mSamplePointerCoords = other->mSamplePointerCoords;
490 } else {
491 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500492 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700493 mSamplePointerCoords.clear();
494 size_t pointerCount = other->getPointerCount();
495 size_t historySize = other->getHistorySize();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800496 mSamplePointerCoords
497 .insert(mSamplePointerCoords.end(),
498 &other->mSamplePointerCoords[historySize * pointerCount],
499 &other->mSamplePointerCoords[historySize * pointerCount + pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700500 }
501}
502
503void MotionEvent::addSample(
504 int64_t eventTime,
505 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500506 mSampleEventTimes.push_back(eventTime);
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800507 mSamplePointerCoords.insert(mSamplePointerCoords.end(), &pointerCoords[0],
508 &pointerCoords[getPointerCount()]);
Jeff Brown5912f952013-07-01 19:10:31 -0700509}
510
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800511int MotionEvent::getSurfaceRotation() const {
512 // The surface rotation is the rotation from the window's coordinate space to that of the
513 // display. Since the event's transform takes display space coordinates to window space, the
514 // returned surface rotation is the inverse of the rotation for the surface.
515 switch (mTransform.getOrientation()) {
516 case ui::Transform::ROT_0:
517 return DISPLAY_ORIENTATION_0;
518 case ui::Transform::ROT_90:
519 return DISPLAY_ORIENTATION_270;
520 case ui::Transform::ROT_180:
521 return DISPLAY_ORIENTATION_180;
522 case ui::Transform::ROT_270:
523 return DISPLAY_ORIENTATION_90;
524 default:
525 return -1;
526 }
527}
528
Garfield Tan00f511d2019-06-12 16:55:40 -0700529float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700530 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
531 return vals.x;
Garfield Tan00f511d2019-06-12 16:55:40 -0700532}
533
534float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700535 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
536 return vals.y;
Garfield Tan00f511d2019-06-12 16:55:40 -0700537}
538
Garfield Tan937bb832019-07-25 17:48:31 -0700539void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700540 ui::Transform inverse = mTransform.inverse();
541 vec2 vals = inverse.transform(x, y);
542 mRawXCursorPosition = vals.x;
543 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700544}
545
Jeff Brown5912f952013-07-01 19:10:31 -0700546const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000547 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
548 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
549 }
550 const size_t position = getHistorySize() * getPointerCount() + pointerIndex;
551 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
552 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
553 }
554 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700555}
556
557float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
Evan Rosky84f07f02021-04-16 10:42:42 -0700558 return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700559}
560
561float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700562 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700563}
564
565const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
566 size_t pointerIndex, size_t historicalIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000567 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
568 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
569 }
570 if (CC_UNLIKELY(historicalIndex < 0 || historicalIndex > getHistorySize())) {
571 LOG(FATAL) << __func__ << ": Invalid historical index " << historicalIndex << " for "
572 << *this;
573 }
574 const size_t position = historicalIndex * getPointerCount() + pointerIndex;
575 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
576 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
577 }
578 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700579}
580
581float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan6b384612021-05-14 16:56:25 -0700582 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700583 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
584 return calculateTransformedAxisValue(axis, mSource, mRawTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700585}
586
587float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan9f388812021-05-13 16:54:53 -0700588 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700589 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
590 return calculateTransformedAxisValue(axis, mSource, mTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700591}
592
593ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
594 size_t pointerCount = mPointerProperties.size();
595 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800596 if (mPointerProperties[i].id == pointerId) {
Jeff Brown5912f952013-07-01 19:10:31 -0700597 return i;
598 }
599 }
600 return -1;
601}
602
603void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700604 float currXOffset = mTransform.tx();
605 float currYOffset = mTransform.ty();
606 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700607}
608
Robert Carre07e1032018-11-26 12:55:53 -0800609void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700610 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700611 mRawTransform.set(mRawTransform.tx() * globalScaleFactor,
612 mRawTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800613 mXPrecision *= globalScaleFactor;
614 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700615
616 size_t numSamples = mSamplePointerCoords.size();
617 for (size_t i = 0; i < numSamples; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800618 mSamplePointerCoords[i].scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700619 }
620}
621
chaviw9eaa22c2020-07-01 16:21:27 -0700622void MotionEvent::transform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700623 // We want to preserve the raw axes values stored in the PointerCoords, so we just update the
624 // transform using the values passed in.
chaviw9eaa22c2020-07-01 16:21:27 -0700625 ui::Transform newTransform;
626 newTransform.set(matrix);
627 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700628}
629
Evan Roskyd4d4d802021-05-03 20:12:21 -0700630void MotionEvent::applyTransform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700631 ui::Transform transform;
632 transform.set(matrix);
Evan Roskyd4d4d802021-05-03 20:12:21 -0700633
634 // Apply the transformation to all samples.
Prabir Pradhan6b384612021-05-14 16:56:25 -0700635 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
636 [&transform](PointerCoords& c) { c.transform(transform); });
Prabir Pradhan4b19bd02021-06-01 17:34:59 -0700637
638 if (mRawXCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION &&
639 mRawYCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) {
640 const vec2 cursor = transform.transform(mRawXCursorPosition, mRawYCursorPosition);
641 mRawXCursorPosition = cursor.x;
642 mRawYCursorPosition = cursor.y;
643 }
Evan Roskyd4d4d802021-05-03 20:12:21 -0700644}
645
Brett Chabotfaa986c2020-11-04 17:39:36 -0800646#ifdef __linux__
chaviw9eaa22c2020-07-01 16:21:27 -0700647static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
648 float dsdx, dtdx, tx, dtdy, dsdy, ty;
649 status_t status = parcel.readFloat(&dsdx);
650 status |= parcel.readFloat(&dtdx);
651 status |= parcel.readFloat(&tx);
652 status |= parcel.readFloat(&dtdy);
653 status |= parcel.readFloat(&dsdy);
654 status |= parcel.readFloat(&ty);
655
656 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
657 return status;
658}
659
660static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
661 status_t status = parcel.writeFloat(transform.dsdx());
662 status |= parcel.writeFloat(transform.dtdx());
663 status |= parcel.writeFloat(transform.tx());
664 status |= parcel.writeFloat(transform.dtdy());
665 status |= parcel.writeFloat(transform.dsdy());
666 status |= parcel.writeFloat(transform.ty());
667 return status;
668}
669
Jeff Brown5912f952013-07-01 19:10:31 -0700670status_t MotionEvent::readFromParcel(Parcel* parcel) {
671 size_t pointerCount = parcel->readInt32();
672 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800673 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
674 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700675 return BAD_VALUE;
676 }
677
Garfield Tan4cc839f2020-01-24 11:26:14 -0800678 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700679 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600680 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800681 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600682 std::vector<uint8_t> hmac;
683 status_t result = parcel->readByteVector(&hmac);
684 if (result != OK || hmac.size() != 32) {
685 return BAD_VALUE;
686 }
687 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700688 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100689 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700690 mFlags = parcel->readInt32();
691 mEdgeFlags = parcel->readInt32();
692 mMetaState = parcel->readInt32();
693 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800694 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700695
696 result = android::readFromParcel(mTransform, *parcel);
697 if (result != OK) {
698 return result;
699 }
Jeff Brown5912f952013-07-01 19:10:31 -0700700 mXPrecision = parcel->readFloat();
701 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700702 mRawXCursorPosition = parcel->readFloat();
703 mRawYCursorPosition = parcel->readFloat();
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700704
705 result = android::readFromParcel(mRawTransform, *parcel);
706 if (result != OK) {
707 return result;
708 }
Jeff Brown5912f952013-07-01 19:10:31 -0700709 mDownTime = parcel->readInt64();
710
711 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800712 mPointerProperties.reserve(pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700713 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500714 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700715 mSamplePointerCoords.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800716 mSamplePointerCoords.reserve(sampleCount * pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700717
718 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800719 mPointerProperties.push_back({});
720 PointerProperties& properties = mPointerProperties.back();
Jeff Brown5912f952013-07-01 19:10:31 -0700721 properties.id = parcel->readInt32();
722 properties.toolType = parcel->readInt32();
723 }
724
Dan Austinc94fc452015-09-22 14:22:41 -0700725 while (sampleCount > 0) {
726 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500727 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700728 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800729 mSamplePointerCoords.push_back({});
730 status_t status = mSamplePointerCoords.back().readFromParcel(parcel);
Jeff Brown5912f952013-07-01 19:10:31 -0700731 if (status) {
732 return status;
733 }
734 }
735 }
736 return OK;
737}
738
739status_t MotionEvent::writeToParcel(Parcel* parcel) const {
740 size_t pointerCount = mPointerProperties.size();
741 size_t sampleCount = mSampleEventTimes.size();
742
743 parcel->writeInt32(pointerCount);
744 parcel->writeInt32(sampleCount);
745
Garfield Tan4cc839f2020-01-24 11:26:14 -0800746 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700747 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600748 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800749 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600750 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
751 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700752 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100753 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700754 parcel->writeInt32(mFlags);
755 parcel->writeInt32(mEdgeFlags);
756 parcel->writeInt32(mMetaState);
757 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800758 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700759
760 status_t result = android::writeToParcel(mTransform, *parcel);
761 if (result != OK) {
762 return result;
763 }
Jeff Brown5912f952013-07-01 19:10:31 -0700764 parcel->writeFloat(mXPrecision);
765 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700766 parcel->writeFloat(mRawXCursorPosition);
767 parcel->writeFloat(mRawYCursorPosition);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700768
769 result = android::writeToParcel(mRawTransform, *parcel);
770 if (result != OK) {
771 return result;
772 }
Jeff Brown5912f952013-07-01 19:10:31 -0700773 parcel->writeInt64(mDownTime);
774
775 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800776 const PointerProperties& properties = mPointerProperties[i];
Jeff Brown5912f952013-07-01 19:10:31 -0700777 parcel->writeInt32(properties.id);
778 parcel->writeInt32(properties.toolType);
779 }
780
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800781 const PointerCoords* pc = mSamplePointerCoords.data();
Jeff Brown5912f952013-07-01 19:10:31 -0700782 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500783 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700784 for (size_t i = 0; i < pointerCount; i++) {
785 status_t status = (pc++)->writeToParcel(parcel);
786 if (status) {
787 return status;
788 }
789 }
790 }
791 return OK;
792}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800793#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700794
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600795bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700796 if (isFromSource(source, AINPUT_SOURCE_CLASS_POINTER)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700797 // Specifically excludes HOVER_MOVE and SCROLL.
798 switch (action & AMOTION_EVENT_ACTION_MASK) {
799 case AMOTION_EVENT_ACTION_DOWN:
800 case AMOTION_EVENT_ACTION_MOVE:
801 case AMOTION_EVENT_ACTION_UP:
802 case AMOTION_EVENT_ACTION_POINTER_DOWN:
803 case AMOTION_EVENT_ACTION_POINTER_UP:
804 case AMOTION_EVENT_ACTION_CANCEL:
805 case AMOTION_EVENT_ACTION_OUTSIDE:
806 return true;
807 }
808 }
809 return false;
810}
811
Michael Wright872db4f2014-04-22 15:03:51 -0700812const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700813 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700814}
815
816int32_t MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700817 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700818}
819
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500820std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700821 // Convert MotionEvent action to string
822 switch (action & AMOTION_EVENT_ACTION_MASK) {
823 case AMOTION_EVENT_ACTION_DOWN:
824 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700825 case AMOTION_EVENT_ACTION_UP:
826 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500827 case AMOTION_EVENT_ACTION_MOVE:
828 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700829 case AMOTION_EVENT_ACTION_CANCEL:
830 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500831 case AMOTION_EVENT_ACTION_OUTSIDE:
832 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700833 case AMOTION_EVENT_ACTION_POINTER_DOWN:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000834 return StringPrintf("POINTER_DOWN(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700835 case AMOTION_EVENT_ACTION_POINTER_UP:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000836 return StringPrintf("POINTER_UP(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500837 case AMOTION_EVENT_ACTION_HOVER_MOVE:
838 return "HOVER_MOVE";
839 case AMOTION_EVENT_ACTION_SCROLL:
840 return "SCROLL";
841 case AMOTION_EVENT_ACTION_HOVER_ENTER:
842 return "HOVER_ENTER";
843 case AMOTION_EVENT_ACTION_HOVER_EXIT:
844 return "HOVER_EXIT";
845 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
846 return "BUTTON_PRESS";
847 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
848 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700849 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500850 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700851}
852
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700853// Apply the given transformation to the point without checking whether the entire transform
854// should be disregarded altogether for the provided source.
855static inline vec2 calculateTransformedXYUnchecked(uint32_t source, const ui::Transform& transform,
856 const vec2& xy) {
857 return shouldDisregardOffset(source) ? transformWithoutTranslation(transform, xy)
858 : transform.transform(xy);
859}
860
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700861vec2 MotionEvent::calculateTransformedXY(uint32_t source, const ui::Transform& transform,
862 const vec2& xy) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700863 if (shouldDisregardTransformation(source)) {
864 return xy;
865 }
866 return calculateTransformedXYUnchecked(source, transform, xy);
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700867}
868
Prabir Pradhan8e6ce222022-02-24 09:08:54 -0800869// Keep in sync with calculateTransformedCoords.
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700870float MotionEvent::calculateTransformedAxisValue(int32_t axis, uint32_t source,
871 const ui::Transform& transform,
872 const PointerCoords& coords) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700873 if (shouldDisregardTransformation(source)) {
874 return coords.getAxisValue(axis);
875 }
876
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700877 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700878 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700879 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
880 return xy[axis];
881 }
882
883 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
884 const vec2 relativeXy =
885 transformWithoutTranslation(transform,
886 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
887 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
888 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
889 }
890
891 if (axis == AMOTION_EVENT_AXIS_ORIENTATION) {
892 return transformAngle(transform, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION));
893 }
894
895 return coords.getAxisValue(axis);
896}
897
Prabir Pradhan8e6ce222022-02-24 09:08:54 -0800898// Keep in sync with calculateTransformedAxisValue. This is an optimization of
899// calculateTransformedAxisValue for all PointerCoords axes.
900PointerCoords MotionEvent::calculateTransformedCoords(uint32_t source,
901 const ui::Transform& transform,
902 const PointerCoords& coords) {
903 if (shouldDisregardTransformation(source)) {
904 return coords;
905 }
906 PointerCoords out = coords;
907
908 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
909 out.setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
910 out.setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
911
912 const vec2 relativeXy =
913 transformWithoutTranslation(transform,
914 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
915 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
916 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
917 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
918
919 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
920 transformAngle(transform,
921 coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)));
922
923 return out;
924}
925
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000926std::ostream& operator<<(std::ostream& out, const MotionEvent& event) {
927 out << "MotionEvent { action=" << MotionEvent::actionToString(event.getAction());
928 if (event.getActionButton() != 0) {
929 out << ", actionButton=" << std::to_string(event.getActionButton());
930 }
931 const size_t pointerCount = event.getPointerCount();
hupeng3aa5a51a2022-09-02 16:00:18 +0800932 LOG_ALWAYS_FATAL_IF(pointerCount > MAX_POINTERS, "Too many pointers : pointerCount = %zu",
933 pointerCount);
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000934 for (size_t i = 0; i < pointerCount; i++) {
935 out << ", id[" << i << "]=" << event.getPointerId(i);
936 float x = event.getX(i);
937 float y = event.getY(i);
938 if (x != 0 || y != 0) {
939 out << ", x[" << i << "]=" << x;
940 out << ", y[" << i << "]=" << y;
941 }
942 int toolType = event.getToolType(i);
943 if (toolType != AMOTION_EVENT_TOOL_TYPE_FINGER) {
944 out << ", toolType[" << i << "]=" << toolType;
945 }
946 }
947 if (event.getButtonState() != 0) {
948 out << ", buttonState=" << event.getButtonState();
949 }
950 if (event.getClassification() != MotionClassification::NONE) {
951 out << ", classification=" << motionClassificationToString(event.getClassification());
952 }
953 if (event.getMetaState() != 0) {
954 out << ", metaState=" << event.getMetaState();
955 }
956 if (event.getEdgeFlags() != 0) {
957 out << ", edgeFlags=" << event.getEdgeFlags();
958 }
959 if (pointerCount != 1) {
960 out << ", pointerCount=" << pointerCount;
961 }
962 if (event.getHistorySize() != 0) {
963 out << ", historySize=" << event.getHistorySize();
964 }
965 out << ", eventTime=" << event.getEventTime();
966 out << ", downTime=" << event.getDownTime();
967 out << ", deviceId=" << event.getDeviceId();
968 out << ", source=" << inputEventSourceToString(event.getSource());
969 out << ", displayId=" << event.getDisplayId();
970 out << ", eventId=" << event.getId();
971 out << "}";
972 return out;
973}
974
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800975// --- FocusEvent ---
976
Antonio Kantek3cfec7b2021-11-05 18:26:17 -0700977void FocusEvent::initialize(int32_t id, bool hasFocus) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800978 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600979 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800980 mHasFocus = hasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800981}
982
983void FocusEvent::initialize(const FocusEvent& from) {
984 InputEvent::initialize(from);
985 mHasFocus = from.mHasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800986}
Jeff Brown5912f952013-07-01 19:10:31 -0700987
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800988// --- CaptureEvent ---
989
990void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) {
991 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
992 ADISPLAY_ID_NONE, INVALID_HMAC);
993 mPointerCaptureEnabled = pointerCaptureEnabled;
994}
995
996void CaptureEvent::initialize(const CaptureEvent& from) {
997 InputEvent::initialize(from);
998 mPointerCaptureEnabled = from.mPointerCaptureEnabled;
999}
1000
arthurhung7632c332020-12-30 16:58:01 +08001001// --- DragEvent ---
1002
1003void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) {
1004 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1005 ADISPLAY_ID_NONE, INVALID_HMAC);
1006 mIsExiting = isExiting;
1007 mX = x;
1008 mY = y;
1009}
1010
1011void DragEvent::initialize(const DragEvent& from) {
1012 InputEvent::initialize(from);
1013 mIsExiting = from.mIsExiting;
1014 mX = from.mX;
1015 mY = from.mY;
1016}
1017
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001018// --- TouchModeEvent ---
1019
1020void TouchModeEvent::initialize(int32_t id, bool isInTouchMode) {
1021 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1022 ADISPLAY_ID_NONE, INVALID_HMAC);
1023 mIsInTouchMode = isInTouchMode;
1024}
1025
1026void TouchModeEvent::initialize(const TouchModeEvent& from) {
1027 InputEvent::initialize(from);
1028 mIsInTouchMode = from.mIsInTouchMode;
1029}
1030
Jeff Brown5912f952013-07-01 19:10:31 -07001031// --- PooledInputEventFactory ---
1032
1033PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
1034 mMaxPoolSize(maxPoolSize) {
1035}
1036
1037PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -07001038}
1039
1040KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001041 if (mKeyEventPool.empty()) {
1042 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001043 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001044 KeyEvent* event = mKeyEventPool.front().release();
1045 mKeyEventPool.pop();
1046 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001047}
1048
1049MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001050 if (mMotionEventPool.empty()) {
1051 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001052 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001053 MotionEvent* event = mMotionEventPool.front().release();
1054 mMotionEventPool.pop();
1055 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001056}
1057
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001058FocusEvent* PooledInputEventFactory::createFocusEvent() {
1059 if (mFocusEventPool.empty()) {
1060 return new FocusEvent();
1061 }
1062 FocusEvent* event = mFocusEventPool.front().release();
1063 mFocusEventPool.pop();
1064 return event;
1065}
1066
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001067CaptureEvent* PooledInputEventFactory::createCaptureEvent() {
1068 if (mCaptureEventPool.empty()) {
1069 return new CaptureEvent();
1070 }
1071 CaptureEvent* event = mCaptureEventPool.front().release();
1072 mCaptureEventPool.pop();
1073 return event;
1074}
1075
arthurhung7632c332020-12-30 16:58:01 +08001076DragEvent* PooledInputEventFactory::createDragEvent() {
1077 if (mDragEventPool.empty()) {
1078 return new DragEvent();
1079 }
1080 DragEvent* event = mDragEventPool.front().release();
1081 mDragEventPool.pop();
1082 return event;
1083}
1084
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001085TouchModeEvent* PooledInputEventFactory::createTouchModeEvent() {
1086 if (mTouchModeEventPool.empty()) {
1087 return new TouchModeEvent();
1088 }
1089 TouchModeEvent* event = mTouchModeEventPool.front().release();
1090 mTouchModeEventPool.pop();
1091 return event;
1092}
1093
Jeff Brown5912f952013-07-01 19:10:31 -07001094void PooledInputEventFactory::recycle(InputEvent* event) {
1095 switch (event->getType()) {
1096 case AINPUT_EVENT_TYPE_KEY:
1097 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001098 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -07001099 return;
1100 }
1101 break;
1102 case AINPUT_EVENT_TYPE_MOTION:
1103 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001104 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -07001105 return;
1106 }
1107 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001108 case AINPUT_EVENT_TYPE_FOCUS:
1109 if (mFocusEventPool.size() < mMaxPoolSize) {
1110 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
1111 return;
1112 }
1113 break;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001114 case AINPUT_EVENT_TYPE_CAPTURE:
1115 if (mCaptureEventPool.size() < mMaxPoolSize) {
1116 mCaptureEventPool.push(
1117 std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event)));
1118 return;
1119 }
1120 break;
arthurhung7632c332020-12-30 16:58:01 +08001121 case AINPUT_EVENT_TYPE_DRAG:
1122 if (mDragEventPool.size() < mMaxPoolSize) {
1123 mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event)));
1124 return;
1125 }
1126 break;
Antonio Kantekeb4a30c2021-09-28 17:49:49 -07001127 case AINPUT_EVENT_TYPE_TOUCH_MODE:
1128 if (mTouchModeEventPool.size() < mMaxPoolSize) {
1129 mTouchModeEventPool.push(
1130 std::unique_ptr<TouchModeEvent>(static_cast<TouchModeEvent*>(event)));
1131 return;
1132 }
1133 break;
Jeff Brown5912f952013-07-01 19:10:31 -07001134 }
1135 delete event;
1136}
1137
1138} // namespace android