blob: 155cb040fb87f9c3c3cba057b63c61e2eb723c72 [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
Siarhei Vishniakoud5fe5182022-07-20 23:28:40 +000093const char* motionToolTypeToString(int32_t toolType) {
94 switch (toolType) {
95 case AMOTION_EVENT_TOOL_TYPE_UNKNOWN:
96 return "UNKNOWN";
97 case AMOTION_EVENT_TOOL_TYPE_FINGER:
98 return "FINGER";
99 case AMOTION_EVENT_TOOL_TYPE_STYLUS:
100 return "STYLUS";
101 case AMOTION_EVENT_TOOL_TYPE_MOUSE:
102 return "MOUSE";
103 case AMOTION_EVENT_TOOL_TYPE_ERASER:
104 return "ERASER";
105 case AMOTION_EVENT_TOOL_TYPE_PALM:
106 return "PALM";
107 default:
108 return "INVALID";
109 }
110}
111
Garfield Tan84b087e2020-01-23 10:49:05 -0800112// --- IdGenerator ---
113IdGenerator::IdGenerator(Source source) : mSource(source) {}
114
115int32_t IdGenerator::nextId() const {
116 constexpr uint32_t SEQUENCE_NUMBER_MASK = ~SOURCE_MASK;
117 int32_t id = 0;
118
119// Avoid building against syscall getrandom(2) on host, which will fail build on Mac. Host doesn't
120// use sequence number so just always return mSource.
121#ifdef __ANDROID__
122 constexpr size_t BUF_LEN = sizeof(id);
123 size_t totalBytes = 0;
124 while (totalBytes < BUF_LEN) {
125 ssize_t bytes = TEMP_FAILURE_RETRY(getrandom(&id, BUF_LEN, GRND_NONBLOCK));
126 if (CC_UNLIKELY(bytes < 0)) {
127 ALOGW("Failed to fill in random number for sequence number: %s.", strerror(errno));
128 id = 0;
129 break;
130 }
131 totalBytes += bytes;
132 }
133#endif // __ANDROID__
134
135 return (id & SEQUENCE_NUMBER_MASK) | static_cast<int32_t>(mSource);
136}
137
Jeff Brown5912f952013-07-01 19:10:31 -0700138// --- InputEvent ---
139
Prabir Pradhande69f8a2021-11-18 16:40:34 +0000140vec2 transformWithoutTranslation(const ui::Transform& transform, const vec2& xy) {
141 const vec2 transformedXy = transform.transform(xy);
142 const vec2 transformedOrigin = transform.transform(0, 0);
143 return transformedXy - transformedOrigin;
144}
145
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800146const char* inputEventTypeToString(int32_t type) {
147 switch (type) {
148 case AINPUT_EVENT_TYPE_KEY: {
149 return "KEY";
150 }
151 case AINPUT_EVENT_TYPE_MOTION: {
152 return "MOTION";
153 }
154 case AINPUT_EVENT_TYPE_FOCUS: {
155 return "FOCUS";
156 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800157 case AINPUT_EVENT_TYPE_CAPTURE: {
158 return "CAPTURE";
159 }
arthurhung7632c332020-12-30 16:58:01 +0800160 case AINPUT_EVENT_TYPE_DRAG: {
161 return "DRAG";
162 }
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700163 case AINPUT_EVENT_TYPE_TOUCH_MODE: {
164 return "TOUCH_MODE";
165 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800166 }
167 return "UNKNOWN";
168}
169
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800170std::string inputEventSourceToString(int32_t source) {
171 if (source == AINPUT_SOURCE_UNKNOWN) {
172 return "UNKNOWN";
173 }
174 if (source == static_cast<int32_t>(AINPUT_SOURCE_ANY)) {
175 return "ANY";
176 }
177 static const std::map<int32_t, const char*> SOURCES{
178 {AINPUT_SOURCE_KEYBOARD, "KEYBOARD"},
179 {AINPUT_SOURCE_DPAD, "DPAD"},
180 {AINPUT_SOURCE_GAMEPAD, "GAMEPAD"},
181 {AINPUT_SOURCE_TOUCHSCREEN, "TOUCHSCREEN"},
182 {AINPUT_SOURCE_MOUSE, "MOUSE"},
183 {AINPUT_SOURCE_STYLUS, "STYLUS"},
184 {AINPUT_SOURCE_BLUETOOTH_STYLUS, "BLUETOOTH_STYLUS"},
185 {AINPUT_SOURCE_TRACKBALL, "TRACKBALL"},
186 {AINPUT_SOURCE_MOUSE_RELATIVE, "MOUSE_RELATIVE"},
187 {AINPUT_SOURCE_TOUCHPAD, "TOUCHPAD"},
188 {AINPUT_SOURCE_TOUCH_NAVIGATION, "TOUCH_NAVIGATION"},
189 {AINPUT_SOURCE_JOYSTICK, "JOYSTICK"},
190 {AINPUT_SOURCE_HDMI, "HDMI"},
191 {AINPUT_SOURCE_SENSOR, "SENSOR"},
192 {AINPUT_SOURCE_ROTARY_ENCODER, "ROTARY_ENCODER"},
193 };
194 std::string result;
195 for (const auto& [source_entry, str] : SOURCES) {
196 if ((source & source_entry) == source_entry) {
197 if (!result.empty()) {
198 result += " | ";
199 }
200 result += str;
201 }
202 }
203 if (result.empty()) {
204 result = StringPrintf("0x%08x", source);
205 }
206 return result;
207}
208
209bool isFromSource(uint32_t source, uint32_t test) {
210 return (source & test) == test;
211}
212
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800213VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
214 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
215 event.getSource(), event.getDisplayId()},
216 event.getAction(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800217 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800218 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800219 event.getKeyCode(),
220 event.getScanCode(),
221 event.getMetaState(),
222 event.getRepeatCount()};
223}
224
225VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
226 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
227 event.getSource(), event.getDisplayId()},
228 event.getRawX(0),
229 event.getRawY(0),
230 event.getActionMasked(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800231 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800232 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800233 event.getMetaState(),
234 event.getButtonState()};
235}
236
Garfield Tan4cc839f2020-01-24 11:26:14 -0800237void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600238 std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800239 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700240 mDeviceId = deviceId;
241 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100242 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600243 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700244}
245
246void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800247 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700248 mDeviceId = from.mDeviceId;
249 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100250 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600251 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700252}
253
Garfield Tan4cc839f2020-01-24 11:26:14 -0800254int32_t InputEvent::nextId() {
255 static IdGenerator idGen(IdGenerator::Source::OTHER);
256 return idGen.nextId();
257}
258
Jeff Brown5912f952013-07-01 19:10:31 -0700259// --- KeyEvent ---
260
Michael Wright872db4f2014-04-22 15:03:51 -0700261const char* KeyEvent::getLabel(int32_t keyCode) {
Chris Ye4958d062020-08-20 13:21:10 -0700262 return InputEventLookup::getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700263}
264
Michael Wright872db4f2014-04-22 15:03:51 -0700265int32_t KeyEvent::getKeyCodeFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700266 return InputEventLookup::getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700267}
268
Garfield Tan4cc839f2020-01-24 11:26:14 -0800269void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600270 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
271 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
272 nsecs_t downTime, nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800273 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700274 mAction = action;
275 mFlags = flags;
276 mKeyCode = keyCode;
277 mScanCode = scanCode;
278 mMetaState = metaState;
279 mRepeatCount = repeatCount;
280 mDownTime = downTime;
281 mEventTime = eventTime;
282}
283
284void KeyEvent::initialize(const KeyEvent& from) {
285 InputEvent::initialize(from);
286 mAction = from.mAction;
287 mFlags = from.mFlags;
288 mKeyCode = from.mKeyCode;
289 mScanCode = from.mScanCode;
290 mMetaState = from.mMetaState;
291 mRepeatCount = from.mRepeatCount;
292 mDownTime = from.mDownTime;
293 mEventTime = from.mEventTime;
294}
295
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700296const char* KeyEvent::actionToString(int32_t action) {
297 // Convert KeyEvent action to string
298 switch (action) {
299 case AKEY_EVENT_ACTION_DOWN:
300 return "DOWN";
301 case AKEY_EVENT_ACTION_UP:
302 return "UP";
303 case AKEY_EVENT_ACTION_MULTIPLE:
304 return "MULTIPLE";
305 }
306 return "UNKNOWN";
307}
Jeff Brown5912f952013-07-01 19:10:31 -0700308
309// --- PointerCoords ---
310
311float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700312 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700313 return 0;
314 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700315 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700316}
317
318status_t PointerCoords::setAxisValue(int32_t axis, float value) {
319 if (axis < 0 || axis > 63) {
320 return NAME_NOT_FOUND;
321 }
322
Michael Wright38dcdff2014-03-19 12:06:10 -0700323 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
324 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700325 if (value == 0) {
326 return OK; // axes with value 0 do not need to be stored
327 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700328
329 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700330 if (count >= MAX_AXES) {
331 tooManyAxes(axis);
332 return NO_MEMORY;
333 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700334 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700335 for (uint32_t i = count; i > index; i--) {
336 values[i] = values[i - 1];
337 }
338 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700339
Jeff Brown5912f952013-07-01 19:10:31 -0700340 values[index] = value;
341 return OK;
342}
343
344static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
345 float value = c.getAxisValue(axis);
346 if (value != 0) {
347 c.setAxisValue(axis, value * scaleFactor);
348 }
349}
350
Robert Carre07e1032018-11-26 12:55:53 -0800351void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700352 // No need to scale pressure or size since they are normalized.
353 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800354
355 // If there is a global scale factor, it is included in the windowX/YScale
356 // so we don't need to apply it twice to the X/Y axes.
357 // However we don't want to apply any windowXYScale not included in the global scale
358 // to the TOUCH_MAJOR/MINOR coordinates.
359 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
360 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
361 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
362 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
363 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
364 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
Prabir Pradhanc6523582021-05-14 18:02:55 -0700365 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_X, windowXScale);
366 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_Y, windowYScale);
Robert Carre07e1032018-11-26 12:55:53 -0800367}
368
Brett Chabotfaa986c2020-11-04 17:39:36 -0800369#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700370status_t PointerCoords::readFromParcel(Parcel* parcel) {
371 bits = parcel->readInt64();
372
Michael Wright38dcdff2014-03-19 12:06:10 -0700373 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700374 if (count > MAX_AXES) {
375 return BAD_VALUE;
376 }
377
378 for (uint32_t i = 0; i < count; i++) {
379 values[i] = parcel->readFloat();
380 }
381 return OK;
382}
383
384status_t PointerCoords::writeToParcel(Parcel* parcel) const {
385 parcel->writeInt64(bits);
386
Michael Wright38dcdff2014-03-19 12:06:10 -0700387 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700388 for (uint32_t i = 0; i < count; i++) {
389 parcel->writeFloat(values[i]);
390 }
391 return OK;
392}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800393#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700394
395void PointerCoords::tooManyAxes(int axis) {
396 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
397 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
398}
399
400bool PointerCoords::operator==(const PointerCoords& other) const {
401 if (bits != other.bits) {
402 return false;
403 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700404 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700405 for (uint32_t i = 0; i < count; i++) {
406 if (values[i] != other.values[i]) {
407 return false;
408 }
409 }
410 return true;
411}
412
413void PointerCoords::copyFrom(const PointerCoords& other) {
414 bits = other.bits;
Michael Wright38dcdff2014-03-19 12:06:10 -0700415 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700416 for (uint32_t i = 0; i < count; i++) {
417 values[i] = other.values[i];
418 }
419}
420
chaviwc01e1372020-07-01 12:37:31 -0700421void PointerCoords::transform(const ui::Transform& transform) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700422 const vec2 xy = transform.transform(getXYValue());
423 setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
424 setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
425
Prabir Pradhanc6523582021-05-14 18:02:55 -0700426 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_X) ||
427 BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_Y)) {
428 const ui::Transform rotation(transform.getOrientation());
429 const vec2 relativeXy = rotation.transform(getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
430 getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
431 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
432 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
433 }
434
Prabir Pradhan6b384612021-05-14 16:56:25 -0700435 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) {
436 const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
437 setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val));
438 }
chaviwc01e1372020-07-01 12:37:31 -0700439}
Jeff Brown5912f952013-07-01 19:10:31 -0700440
441// --- PointerProperties ---
442
443bool PointerProperties::operator==(const PointerProperties& other) const {
444 return id == other.id
445 && toolType == other.toolType;
446}
447
448void PointerProperties::copyFrom(const PointerProperties& other) {
449 id = other.id;
450 toolType = other.toolType;
451}
452
453
454// --- MotionEvent ---
455
Garfield Tan4cc839f2020-01-24 11:26:14 -0800456void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600457 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
458 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700459 int32_t buttonState, MotionClassification classification,
460 const ui::Transform& transform, float xPrecision, float yPrecision,
Evan Rosky84f07f02021-04-16 10:42:42 -0700461 float rawXCursorPosition, float rawYCursorPosition,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700462 const ui::Transform& rawTransform, nsecs_t downTime, nsecs_t eventTime,
Evan Rosky09576692021-07-01 12:22:09 -0700463 size_t pointerCount, const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700464 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800465 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700466 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100467 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700468 mFlags = flags;
469 mEdgeFlags = edgeFlags;
470 mMetaState = metaState;
471 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800472 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700473 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700474 mXPrecision = xPrecision;
475 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700476 mRawXCursorPosition = rawXCursorPosition;
477 mRawYCursorPosition = rawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700478 mRawTransform = rawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700479 mDownTime = downTime;
480 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800481 mPointerProperties.insert(mPointerProperties.end(), &pointerProperties[0],
482 &pointerProperties[pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700483 mSampleEventTimes.clear();
484 mSamplePointerCoords.clear();
485 addSample(eventTime, pointerCoords);
486}
487
488void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800489 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
490 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700491 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100492 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700493 mFlags = other->mFlags;
494 mEdgeFlags = other->mEdgeFlags;
495 mMetaState = other->mMetaState;
496 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800497 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700498 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700499 mXPrecision = other->mXPrecision;
500 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700501 mRawXCursorPosition = other->mRawXCursorPosition;
502 mRawYCursorPosition = other->mRawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700503 mRawTransform = other->mRawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700504 mDownTime = other->mDownTime;
505 mPointerProperties = other->mPointerProperties;
506
507 if (keepHistory) {
508 mSampleEventTimes = other->mSampleEventTimes;
509 mSamplePointerCoords = other->mSamplePointerCoords;
510 } else {
511 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500512 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700513 mSamplePointerCoords.clear();
514 size_t pointerCount = other->getPointerCount();
515 size_t historySize = other->getHistorySize();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800516 mSamplePointerCoords
517 .insert(mSamplePointerCoords.end(),
518 &other->mSamplePointerCoords[historySize * pointerCount],
519 &other->mSamplePointerCoords[historySize * pointerCount + pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700520 }
521}
522
523void MotionEvent::addSample(
524 int64_t eventTime,
525 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500526 mSampleEventTimes.push_back(eventTime);
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800527 mSamplePointerCoords.insert(mSamplePointerCoords.end(), &pointerCoords[0],
528 &pointerCoords[getPointerCount()]);
Jeff Brown5912f952013-07-01 19:10:31 -0700529}
530
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800531int MotionEvent::getSurfaceRotation() const {
532 // The surface rotation is the rotation from the window's coordinate space to that of the
533 // display. Since the event's transform takes display space coordinates to window space, the
534 // returned surface rotation is the inverse of the rotation for the surface.
535 switch (mTransform.getOrientation()) {
536 case ui::Transform::ROT_0:
537 return DISPLAY_ORIENTATION_0;
538 case ui::Transform::ROT_90:
539 return DISPLAY_ORIENTATION_270;
540 case ui::Transform::ROT_180:
541 return DISPLAY_ORIENTATION_180;
542 case ui::Transform::ROT_270:
543 return DISPLAY_ORIENTATION_90;
544 default:
545 return -1;
546 }
547}
548
Garfield Tan00f511d2019-06-12 16:55:40 -0700549float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700550 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
551 return vals.x;
Garfield Tan00f511d2019-06-12 16:55:40 -0700552}
553
554float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700555 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
556 return vals.y;
Garfield Tan00f511d2019-06-12 16:55:40 -0700557}
558
Garfield Tan937bb832019-07-25 17:48:31 -0700559void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700560 ui::Transform inverse = mTransform.inverse();
561 vec2 vals = inverse.transform(x, y);
562 mRawXCursorPosition = vals.x;
563 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700564}
565
Jeff Brown5912f952013-07-01 19:10:31 -0700566const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) 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 const size_t position = getHistorySize() * getPointerCount() + pointerIndex;
571 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
572 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
573 }
574 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700575}
576
577float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
Evan Rosky84f07f02021-04-16 10:42:42 -0700578 return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700579}
580
581float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700582 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700583}
584
585const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
586 size_t pointerIndex, size_t historicalIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000587 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
588 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
589 }
590 if (CC_UNLIKELY(historicalIndex < 0 || historicalIndex > getHistorySize())) {
591 LOG(FATAL) << __func__ << ": Invalid historical index " << historicalIndex << " for "
592 << *this;
593 }
594 const size_t position = historicalIndex * getPointerCount() + pointerIndex;
595 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
596 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
597 }
598 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700599}
600
601float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan6b384612021-05-14 16:56:25 -0700602 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700603 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
604 return calculateTransformedAxisValue(axis, mSource, mRawTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700605}
606
607float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan9f388812021-05-13 16:54:53 -0700608 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700609 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
610 return calculateTransformedAxisValue(axis, mSource, mTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700611}
612
613ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
614 size_t pointerCount = mPointerProperties.size();
615 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800616 if (mPointerProperties[i].id == pointerId) {
Jeff Brown5912f952013-07-01 19:10:31 -0700617 return i;
618 }
619 }
620 return -1;
621}
622
623void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700624 float currXOffset = mTransform.tx();
625 float currYOffset = mTransform.ty();
626 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700627}
628
Robert Carre07e1032018-11-26 12:55:53 -0800629void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700630 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700631 mRawTransform.set(mRawTransform.tx() * globalScaleFactor,
632 mRawTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800633 mXPrecision *= globalScaleFactor;
634 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700635
636 size_t numSamples = mSamplePointerCoords.size();
637 for (size_t i = 0; i < numSamples; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800638 mSamplePointerCoords[i].scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700639 }
640}
641
chaviw9eaa22c2020-07-01 16:21:27 -0700642void MotionEvent::transform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700643 // We want to preserve the raw axes values stored in the PointerCoords, so we just update the
644 // transform using the values passed in.
chaviw9eaa22c2020-07-01 16:21:27 -0700645 ui::Transform newTransform;
646 newTransform.set(matrix);
647 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700648}
649
Evan Roskyd4d4d802021-05-03 20:12:21 -0700650void MotionEvent::applyTransform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700651 ui::Transform transform;
652 transform.set(matrix);
Evan Roskyd4d4d802021-05-03 20:12:21 -0700653
654 // Apply the transformation to all samples.
Prabir Pradhan6b384612021-05-14 16:56:25 -0700655 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
656 [&transform](PointerCoords& c) { c.transform(transform); });
Prabir Pradhan4b19bd02021-06-01 17:34:59 -0700657
658 if (mRawXCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION &&
659 mRawYCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) {
660 const vec2 cursor = transform.transform(mRawXCursorPosition, mRawYCursorPosition);
661 mRawXCursorPosition = cursor.x;
662 mRawYCursorPosition = cursor.y;
663 }
Evan Roskyd4d4d802021-05-03 20:12:21 -0700664}
665
Brett Chabotfaa986c2020-11-04 17:39:36 -0800666#ifdef __linux__
chaviw9eaa22c2020-07-01 16:21:27 -0700667static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
668 float dsdx, dtdx, tx, dtdy, dsdy, ty;
669 status_t status = parcel.readFloat(&dsdx);
670 status |= parcel.readFloat(&dtdx);
671 status |= parcel.readFloat(&tx);
672 status |= parcel.readFloat(&dtdy);
673 status |= parcel.readFloat(&dsdy);
674 status |= parcel.readFloat(&ty);
675
676 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
677 return status;
678}
679
680static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
681 status_t status = parcel.writeFloat(transform.dsdx());
682 status |= parcel.writeFloat(transform.dtdx());
683 status |= parcel.writeFloat(transform.tx());
684 status |= parcel.writeFloat(transform.dtdy());
685 status |= parcel.writeFloat(transform.dsdy());
686 status |= parcel.writeFloat(transform.ty());
687 return status;
688}
689
Jeff Brown5912f952013-07-01 19:10:31 -0700690status_t MotionEvent::readFromParcel(Parcel* parcel) {
691 size_t pointerCount = parcel->readInt32();
692 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800693 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
694 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700695 return BAD_VALUE;
696 }
697
Garfield Tan4cc839f2020-01-24 11:26:14 -0800698 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700699 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600700 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800701 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600702 std::vector<uint8_t> hmac;
703 status_t result = parcel->readByteVector(&hmac);
704 if (result != OK || hmac.size() != 32) {
705 return BAD_VALUE;
706 }
707 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700708 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100709 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700710 mFlags = parcel->readInt32();
711 mEdgeFlags = parcel->readInt32();
712 mMetaState = parcel->readInt32();
713 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800714 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700715
716 result = android::readFromParcel(mTransform, *parcel);
717 if (result != OK) {
718 return result;
719 }
Jeff Brown5912f952013-07-01 19:10:31 -0700720 mXPrecision = parcel->readFloat();
721 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700722 mRawXCursorPosition = parcel->readFloat();
723 mRawYCursorPosition = parcel->readFloat();
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700724
725 result = android::readFromParcel(mRawTransform, *parcel);
726 if (result != OK) {
727 return result;
728 }
Jeff Brown5912f952013-07-01 19:10:31 -0700729 mDownTime = parcel->readInt64();
730
731 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800732 mPointerProperties.reserve(pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700733 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500734 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700735 mSamplePointerCoords.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800736 mSamplePointerCoords.reserve(sampleCount * pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700737
738 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800739 mPointerProperties.push_back({});
740 PointerProperties& properties = mPointerProperties.back();
Jeff Brown5912f952013-07-01 19:10:31 -0700741 properties.id = parcel->readInt32();
742 properties.toolType = parcel->readInt32();
743 }
744
Dan Austinc94fc452015-09-22 14:22:41 -0700745 while (sampleCount > 0) {
746 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500747 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700748 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800749 mSamplePointerCoords.push_back({});
750 status_t status = mSamplePointerCoords.back().readFromParcel(parcel);
Jeff Brown5912f952013-07-01 19:10:31 -0700751 if (status) {
752 return status;
753 }
754 }
755 }
756 return OK;
757}
758
759status_t MotionEvent::writeToParcel(Parcel* parcel) const {
760 size_t pointerCount = mPointerProperties.size();
761 size_t sampleCount = mSampleEventTimes.size();
762
763 parcel->writeInt32(pointerCount);
764 parcel->writeInt32(sampleCount);
765
Garfield Tan4cc839f2020-01-24 11:26:14 -0800766 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700767 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600768 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800769 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600770 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
771 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700772 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100773 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700774 parcel->writeInt32(mFlags);
775 parcel->writeInt32(mEdgeFlags);
776 parcel->writeInt32(mMetaState);
777 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800778 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700779
780 status_t result = android::writeToParcel(mTransform, *parcel);
781 if (result != OK) {
782 return result;
783 }
Jeff Brown5912f952013-07-01 19:10:31 -0700784 parcel->writeFloat(mXPrecision);
785 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700786 parcel->writeFloat(mRawXCursorPosition);
787 parcel->writeFloat(mRawYCursorPosition);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700788
789 result = android::writeToParcel(mRawTransform, *parcel);
790 if (result != OK) {
791 return result;
792 }
Jeff Brown5912f952013-07-01 19:10:31 -0700793 parcel->writeInt64(mDownTime);
794
795 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800796 const PointerProperties& properties = mPointerProperties[i];
Jeff Brown5912f952013-07-01 19:10:31 -0700797 parcel->writeInt32(properties.id);
798 parcel->writeInt32(properties.toolType);
799 }
800
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800801 const PointerCoords* pc = mSamplePointerCoords.data();
Jeff Brown5912f952013-07-01 19:10:31 -0700802 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500803 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700804 for (size_t i = 0; i < pointerCount; i++) {
805 status_t status = (pc++)->writeToParcel(parcel);
806 if (status) {
807 return status;
808 }
809 }
810 }
811 return OK;
812}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800813#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700814
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600815bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700816 if (isFromSource(source, AINPUT_SOURCE_CLASS_POINTER)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700817 // Specifically excludes HOVER_MOVE and SCROLL.
818 switch (action & AMOTION_EVENT_ACTION_MASK) {
819 case AMOTION_EVENT_ACTION_DOWN:
820 case AMOTION_EVENT_ACTION_MOVE:
821 case AMOTION_EVENT_ACTION_UP:
822 case AMOTION_EVENT_ACTION_POINTER_DOWN:
823 case AMOTION_EVENT_ACTION_POINTER_UP:
824 case AMOTION_EVENT_ACTION_CANCEL:
825 case AMOTION_EVENT_ACTION_OUTSIDE:
826 return true;
827 }
828 }
829 return false;
830}
831
Michael Wright872db4f2014-04-22 15:03:51 -0700832const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700833 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700834}
835
836int32_t MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700837 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700838}
839
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500840std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700841 // Convert MotionEvent action to string
842 switch (action & AMOTION_EVENT_ACTION_MASK) {
843 case AMOTION_EVENT_ACTION_DOWN:
844 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700845 case AMOTION_EVENT_ACTION_UP:
846 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500847 case AMOTION_EVENT_ACTION_MOVE:
848 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700849 case AMOTION_EVENT_ACTION_CANCEL:
850 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500851 case AMOTION_EVENT_ACTION_OUTSIDE:
852 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700853 case AMOTION_EVENT_ACTION_POINTER_DOWN:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000854 return StringPrintf("POINTER_DOWN(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700855 case AMOTION_EVENT_ACTION_POINTER_UP:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000856 return StringPrintf("POINTER_UP(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500857 case AMOTION_EVENT_ACTION_HOVER_MOVE:
858 return "HOVER_MOVE";
859 case AMOTION_EVENT_ACTION_SCROLL:
860 return "SCROLL";
861 case AMOTION_EVENT_ACTION_HOVER_ENTER:
862 return "HOVER_ENTER";
863 case AMOTION_EVENT_ACTION_HOVER_EXIT:
864 return "HOVER_EXIT";
865 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
866 return "BUTTON_PRESS";
867 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
868 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700869 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500870 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700871}
872
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700873// Apply the given transformation to the point without checking whether the entire transform
874// should be disregarded altogether for the provided source.
875static inline vec2 calculateTransformedXYUnchecked(uint32_t source, const ui::Transform& transform,
876 const vec2& xy) {
877 return shouldDisregardOffset(source) ? transformWithoutTranslation(transform, xy)
878 : transform.transform(xy);
879}
880
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700881vec2 MotionEvent::calculateTransformedXY(uint32_t source, const ui::Transform& transform,
882 const vec2& xy) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700883 if (shouldDisregardTransformation(source)) {
884 return xy;
885 }
886 return calculateTransformedXYUnchecked(source, transform, xy);
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700887}
888
Prabir Pradhan8e6ce222022-02-24 09:08:54 -0800889// Keep in sync with calculateTransformedCoords.
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700890float MotionEvent::calculateTransformedAxisValue(int32_t axis, uint32_t source,
891 const ui::Transform& transform,
892 const PointerCoords& coords) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700893 if (shouldDisregardTransformation(source)) {
894 return coords.getAxisValue(axis);
895 }
896
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700897 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700898 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700899 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
900 return xy[axis];
901 }
902
903 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
904 const vec2 relativeXy =
905 transformWithoutTranslation(transform,
906 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
907 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
908 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
909 }
910
911 if (axis == AMOTION_EVENT_AXIS_ORIENTATION) {
912 return transformAngle(transform, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION));
913 }
914
915 return coords.getAxisValue(axis);
916}
917
Prabir Pradhan8e6ce222022-02-24 09:08:54 -0800918// Keep in sync with calculateTransformedAxisValue. This is an optimization of
919// calculateTransformedAxisValue for all PointerCoords axes.
920PointerCoords MotionEvent::calculateTransformedCoords(uint32_t source,
921 const ui::Transform& transform,
922 const PointerCoords& coords) {
923 if (shouldDisregardTransformation(source)) {
924 return coords;
925 }
926 PointerCoords out = coords;
927
928 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
929 out.setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
930 out.setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
931
932 const vec2 relativeXy =
933 transformWithoutTranslation(transform,
934 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
935 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
936 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
937 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
938
939 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
940 transformAngle(transform,
941 coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)));
942
943 return out;
944}
945
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000946std::ostream& operator<<(std::ostream& out, const MotionEvent& event) {
947 out << "MotionEvent { action=" << MotionEvent::actionToString(event.getAction());
948 if (event.getActionButton() != 0) {
949 out << ", actionButton=" << std::to_string(event.getActionButton());
950 }
951 const size_t pointerCount = event.getPointerCount();
952 for (size_t i = 0; i < pointerCount; i++) {
953 out << ", id[" << i << "]=" << event.getPointerId(i);
954 float x = event.getX(i);
955 float y = event.getY(i);
956 if (x != 0 || y != 0) {
957 out << ", x[" << i << "]=" << x;
958 out << ", y[" << i << "]=" << y;
959 }
960 int toolType = event.getToolType(i);
961 if (toolType != AMOTION_EVENT_TOOL_TYPE_FINGER) {
962 out << ", toolType[" << i << "]=" << toolType;
963 }
964 }
965 if (event.getButtonState() != 0) {
966 out << ", buttonState=" << event.getButtonState();
967 }
968 if (event.getClassification() != MotionClassification::NONE) {
969 out << ", classification=" << motionClassificationToString(event.getClassification());
970 }
971 if (event.getMetaState() != 0) {
972 out << ", metaState=" << event.getMetaState();
973 }
974 if (event.getEdgeFlags() != 0) {
975 out << ", edgeFlags=" << event.getEdgeFlags();
976 }
977 if (pointerCount != 1) {
978 out << ", pointerCount=" << pointerCount;
979 }
980 if (event.getHistorySize() != 0) {
981 out << ", historySize=" << event.getHistorySize();
982 }
983 out << ", eventTime=" << event.getEventTime();
984 out << ", downTime=" << event.getDownTime();
985 out << ", deviceId=" << event.getDeviceId();
986 out << ", source=" << inputEventSourceToString(event.getSource());
987 out << ", displayId=" << event.getDisplayId();
988 out << ", eventId=" << event.getId();
989 out << "}";
990 return out;
991}
992
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800993// --- FocusEvent ---
994
Antonio Kantek3cfec7b2021-11-05 18:26:17 -0700995void FocusEvent::initialize(int32_t id, bool hasFocus) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800996 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600997 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800998 mHasFocus = hasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800999}
1000
1001void FocusEvent::initialize(const FocusEvent& from) {
1002 InputEvent::initialize(from);
1003 mHasFocus = from.mHasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001004}
Jeff Brown5912f952013-07-01 19:10:31 -07001005
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001006// --- CaptureEvent ---
1007
1008void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) {
1009 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1010 ADISPLAY_ID_NONE, INVALID_HMAC);
1011 mPointerCaptureEnabled = pointerCaptureEnabled;
1012}
1013
1014void CaptureEvent::initialize(const CaptureEvent& from) {
1015 InputEvent::initialize(from);
1016 mPointerCaptureEnabled = from.mPointerCaptureEnabled;
1017}
1018
arthurhung7632c332020-12-30 16:58:01 +08001019// --- DragEvent ---
1020
1021void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) {
1022 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1023 ADISPLAY_ID_NONE, INVALID_HMAC);
1024 mIsExiting = isExiting;
1025 mX = x;
1026 mY = y;
1027}
1028
1029void DragEvent::initialize(const DragEvent& from) {
1030 InputEvent::initialize(from);
1031 mIsExiting = from.mIsExiting;
1032 mX = from.mX;
1033 mY = from.mY;
1034}
1035
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001036// --- TouchModeEvent ---
1037
1038void TouchModeEvent::initialize(int32_t id, bool isInTouchMode) {
1039 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1040 ADISPLAY_ID_NONE, INVALID_HMAC);
1041 mIsInTouchMode = isInTouchMode;
1042}
1043
1044void TouchModeEvent::initialize(const TouchModeEvent& from) {
1045 InputEvent::initialize(from);
1046 mIsInTouchMode = from.mIsInTouchMode;
1047}
1048
Jeff Brown5912f952013-07-01 19:10:31 -07001049// --- PooledInputEventFactory ---
1050
1051PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
1052 mMaxPoolSize(maxPoolSize) {
1053}
1054
1055PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -07001056}
1057
1058KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001059 if (mKeyEventPool.empty()) {
1060 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001061 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001062 KeyEvent* event = mKeyEventPool.front().release();
1063 mKeyEventPool.pop();
1064 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001065}
1066
1067MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001068 if (mMotionEventPool.empty()) {
1069 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001070 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001071 MotionEvent* event = mMotionEventPool.front().release();
1072 mMotionEventPool.pop();
1073 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001074}
1075
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001076FocusEvent* PooledInputEventFactory::createFocusEvent() {
1077 if (mFocusEventPool.empty()) {
1078 return new FocusEvent();
1079 }
1080 FocusEvent* event = mFocusEventPool.front().release();
1081 mFocusEventPool.pop();
1082 return event;
1083}
1084
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001085CaptureEvent* PooledInputEventFactory::createCaptureEvent() {
1086 if (mCaptureEventPool.empty()) {
1087 return new CaptureEvent();
1088 }
1089 CaptureEvent* event = mCaptureEventPool.front().release();
1090 mCaptureEventPool.pop();
1091 return event;
1092}
1093
arthurhung7632c332020-12-30 16:58:01 +08001094DragEvent* PooledInputEventFactory::createDragEvent() {
1095 if (mDragEventPool.empty()) {
1096 return new DragEvent();
1097 }
1098 DragEvent* event = mDragEventPool.front().release();
1099 mDragEventPool.pop();
1100 return event;
1101}
1102
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001103TouchModeEvent* PooledInputEventFactory::createTouchModeEvent() {
1104 if (mTouchModeEventPool.empty()) {
1105 return new TouchModeEvent();
1106 }
1107 TouchModeEvent* event = mTouchModeEventPool.front().release();
1108 mTouchModeEventPool.pop();
1109 return event;
1110}
1111
Jeff Brown5912f952013-07-01 19:10:31 -07001112void PooledInputEventFactory::recycle(InputEvent* event) {
1113 switch (event->getType()) {
1114 case AINPUT_EVENT_TYPE_KEY:
1115 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001116 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -07001117 return;
1118 }
1119 break;
1120 case AINPUT_EVENT_TYPE_MOTION:
1121 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001122 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -07001123 return;
1124 }
1125 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001126 case AINPUT_EVENT_TYPE_FOCUS:
1127 if (mFocusEventPool.size() < mMaxPoolSize) {
1128 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
1129 return;
1130 }
1131 break;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001132 case AINPUT_EVENT_TYPE_CAPTURE:
1133 if (mCaptureEventPool.size() < mMaxPoolSize) {
1134 mCaptureEventPool.push(
1135 std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event)));
1136 return;
1137 }
1138 break;
arthurhung7632c332020-12-30 16:58:01 +08001139 case AINPUT_EVENT_TYPE_DRAG:
1140 if (mDragEventPool.size() < mMaxPoolSize) {
1141 mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event)));
1142 return;
1143 }
1144 break;
Antonio Kantekeb4a30c2021-09-28 17:49:49 -07001145 case AINPUT_EVENT_TYPE_TOUCH_MODE:
1146 if (mTouchModeEventPool.size() < mMaxPoolSize) {
1147 mTouchModeEventPool.push(
1148 std::unique_ptr<TouchModeEvent>(static_cast<TouchModeEvent*>(event)));
1149 return;
1150 }
1151 break;
Jeff Brown5912f952013-07-01 19:10:31 -07001152 }
1153 delete event;
1154}
1155
1156} // namespace android