blob: 6f065670b819bd380e7fff28314e7f159d42bb1e [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 Pradhan0909dc12023-03-09 20:11:09 +0000120// Due to precision limitations when working with floating points, transforming - namely
121// scaling - floating points can lead to minute errors. We round transformed values to approximately
122// three decimal places so that values like 0.99997 show up as 1.0.
123inline float roundTransformedCoords(float val) {
124 // Use a power to two to approximate three decimal places to potentially reduce some cycles.
125 // This should be at least as precise as MotionEvent::ROUNDING_PRECISION.
126 return std::round(val * 1024.f) / 1024.f;
127}
128
129inline vec2 roundTransformedCoords(vec2 p) {
130 return {roundTransformedCoords(p.x), roundTransformedCoords(p.y)};
131}
132
Prabir Pradhande69f8a2021-11-18 16:40:34 +0000133vec2 transformWithoutTranslation(const ui::Transform& transform, const vec2& xy) {
134 const vec2 transformedXy = transform.transform(xy);
135 const vec2 transformedOrigin = transform.transform(0, 0);
Prabir Pradhan0909dc12023-03-09 20:11:09 +0000136 return roundTransformedCoords(transformedXy - transformedOrigin);
Prabir Pradhande69f8a2021-11-18 16:40:34 +0000137}
138
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800139const char* inputEventTypeToString(int32_t type) {
140 switch (type) {
141 case AINPUT_EVENT_TYPE_KEY: {
142 return "KEY";
143 }
144 case AINPUT_EVENT_TYPE_MOTION: {
145 return "MOTION";
146 }
147 case AINPUT_EVENT_TYPE_FOCUS: {
148 return "FOCUS";
149 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800150 case AINPUT_EVENT_TYPE_CAPTURE: {
151 return "CAPTURE";
152 }
arthurhung7632c332020-12-30 16:58:01 +0800153 case AINPUT_EVENT_TYPE_DRAG: {
154 return "DRAG";
155 }
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700156 case AINPUT_EVENT_TYPE_TOUCH_MODE: {
157 return "TOUCH_MODE";
158 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800159 }
160 return "UNKNOWN";
161}
162
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800163std::string inputEventSourceToString(int32_t source) {
164 if (source == AINPUT_SOURCE_UNKNOWN) {
165 return "UNKNOWN";
166 }
167 if (source == static_cast<int32_t>(AINPUT_SOURCE_ANY)) {
168 return "ANY";
169 }
170 static const std::map<int32_t, const char*> SOURCES{
171 {AINPUT_SOURCE_KEYBOARD, "KEYBOARD"},
172 {AINPUT_SOURCE_DPAD, "DPAD"},
173 {AINPUT_SOURCE_GAMEPAD, "GAMEPAD"},
174 {AINPUT_SOURCE_TOUCHSCREEN, "TOUCHSCREEN"},
175 {AINPUT_SOURCE_MOUSE, "MOUSE"},
176 {AINPUT_SOURCE_STYLUS, "STYLUS"},
177 {AINPUT_SOURCE_BLUETOOTH_STYLUS, "BLUETOOTH_STYLUS"},
178 {AINPUT_SOURCE_TRACKBALL, "TRACKBALL"},
179 {AINPUT_SOURCE_MOUSE_RELATIVE, "MOUSE_RELATIVE"},
180 {AINPUT_SOURCE_TOUCHPAD, "TOUCHPAD"},
181 {AINPUT_SOURCE_TOUCH_NAVIGATION, "TOUCH_NAVIGATION"},
182 {AINPUT_SOURCE_JOYSTICK, "JOYSTICK"},
183 {AINPUT_SOURCE_HDMI, "HDMI"},
184 {AINPUT_SOURCE_SENSOR, "SENSOR"},
185 {AINPUT_SOURCE_ROTARY_ENCODER, "ROTARY_ENCODER"},
186 };
187 std::string result;
188 for (const auto& [source_entry, str] : SOURCES) {
189 if ((source & source_entry) == source_entry) {
190 if (!result.empty()) {
191 result += " | ";
192 }
193 result += str;
194 }
195 }
196 if (result.empty()) {
197 result = StringPrintf("0x%08x", source);
198 }
199 return result;
200}
201
202bool isFromSource(uint32_t source, uint32_t test) {
203 return (source & test) == test;
204}
205
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800206VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
207 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
208 event.getSource(), event.getDisplayId()},
209 event.getAction(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800210 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800211 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800212 event.getKeyCode(),
213 event.getScanCode(),
214 event.getMetaState(),
215 event.getRepeatCount()};
216}
217
218VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
219 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
220 event.getSource(), event.getDisplayId()},
221 event.getRawX(0),
222 event.getRawY(0),
223 event.getActionMasked(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800224 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800225 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800226 event.getMetaState(),
227 event.getButtonState()};
228}
229
Garfield Tan4cc839f2020-01-24 11:26:14 -0800230void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600231 std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800232 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700233 mDeviceId = deviceId;
234 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100235 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600236 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700237}
238
239void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800240 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700241 mDeviceId = from.mDeviceId;
242 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100243 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600244 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700245}
246
Garfield Tan4cc839f2020-01-24 11:26:14 -0800247int32_t InputEvent::nextId() {
248 static IdGenerator idGen(IdGenerator::Source::OTHER);
249 return idGen.nextId();
250}
251
Jeff Brown5912f952013-07-01 19:10:31 -0700252// --- KeyEvent ---
253
Michael Wright872db4f2014-04-22 15:03:51 -0700254const char* KeyEvent::getLabel(int32_t keyCode) {
Chris Ye4958d062020-08-20 13:21:10 -0700255 return InputEventLookup::getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700256}
257
Michael Wright872db4f2014-04-22 15:03:51 -0700258int32_t KeyEvent::getKeyCodeFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700259 return InputEventLookup::getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700260}
261
Garfield Tan4cc839f2020-01-24 11:26:14 -0800262void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600263 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
264 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
265 nsecs_t downTime, nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800266 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700267 mAction = action;
268 mFlags = flags;
269 mKeyCode = keyCode;
270 mScanCode = scanCode;
271 mMetaState = metaState;
272 mRepeatCount = repeatCount;
273 mDownTime = downTime;
274 mEventTime = eventTime;
275}
276
277void KeyEvent::initialize(const KeyEvent& from) {
278 InputEvent::initialize(from);
279 mAction = from.mAction;
280 mFlags = from.mFlags;
281 mKeyCode = from.mKeyCode;
282 mScanCode = from.mScanCode;
283 mMetaState = from.mMetaState;
284 mRepeatCount = from.mRepeatCount;
285 mDownTime = from.mDownTime;
286 mEventTime = from.mEventTime;
287}
288
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700289const char* KeyEvent::actionToString(int32_t action) {
290 // Convert KeyEvent action to string
291 switch (action) {
292 case AKEY_EVENT_ACTION_DOWN:
293 return "DOWN";
294 case AKEY_EVENT_ACTION_UP:
295 return "UP";
296 case AKEY_EVENT_ACTION_MULTIPLE:
297 return "MULTIPLE";
298 }
299 return "UNKNOWN";
300}
Jeff Brown5912f952013-07-01 19:10:31 -0700301
302// --- PointerCoords ---
303
304float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700305 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700306 return 0;
307 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700308 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700309}
310
311status_t PointerCoords::setAxisValue(int32_t axis, float value) {
312 if (axis < 0 || axis > 63) {
313 return NAME_NOT_FOUND;
314 }
315
Michael Wright38dcdff2014-03-19 12:06:10 -0700316 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
317 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700318 if (value == 0) {
319 return OK; // axes with value 0 do not need to be stored
320 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700321
322 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700323 if (count >= MAX_AXES) {
324 tooManyAxes(axis);
325 return NO_MEMORY;
326 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700327 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700328 for (uint32_t i = count; i > index; i--) {
329 values[i] = values[i - 1];
330 }
331 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700332
Jeff Brown5912f952013-07-01 19:10:31 -0700333 values[index] = value;
334 return OK;
335}
336
337static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
338 float value = c.getAxisValue(axis);
339 if (value != 0) {
340 c.setAxisValue(axis, value * scaleFactor);
341 }
342}
343
Robert Carre07e1032018-11-26 12:55:53 -0800344void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700345 // No need to scale pressure or size since they are normalized.
346 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800347
348 // If there is a global scale factor, it is included in the windowX/YScale
349 // so we don't need to apply it twice to the X/Y axes.
350 // However we don't want to apply any windowXYScale not included in the global scale
351 // to the TOUCH_MAJOR/MINOR coordinates.
352 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
353 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
354 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
355 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
356 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
357 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
Prabir Pradhanc6523582021-05-14 18:02:55 -0700358 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_X, windowXScale);
359 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_Y, windowYScale);
Robert Carre07e1032018-11-26 12:55:53 -0800360}
361
Brett Chabotfaa986c2020-11-04 17:39:36 -0800362#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700363status_t PointerCoords::readFromParcel(Parcel* parcel) {
364 bits = parcel->readInt64();
365
Michael Wright38dcdff2014-03-19 12:06:10 -0700366 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700367 if (count > MAX_AXES) {
368 return BAD_VALUE;
369 }
370
371 for (uint32_t i = 0; i < count; i++) {
372 values[i] = parcel->readFloat();
373 }
374 return OK;
375}
376
377status_t PointerCoords::writeToParcel(Parcel* parcel) const {
378 parcel->writeInt64(bits);
379
Michael Wright38dcdff2014-03-19 12:06:10 -0700380 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700381 for (uint32_t i = 0; i < count; i++) {
382 parcel->writeFloat(values[i]);
383 }
384 return OK;
385}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800386#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700387
388void PointerCoords::tooManyAxes(int axis) {
389 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
390 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
391}
392
393bool PointerCoords::operator==(const PointerCoords& other) const {
394 if (bits != other.bits) {
395 return false;
396 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700397 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700398 for (uint32_t i = 0; i < count; i++) {
399 if (values[i] != other.values[i]) {
400 return false;
401 }
402 }
403 return true;
404}
405
406void PointerCoords::copyFrom(const PointerCoords& other) {
407 bits = other.bits;
Michael Wright38dcdff2014-03-19 12:06:10 -0700408 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700409 for (uint32_t i = 0; i < count; i++) {
410 values[i] = other.values[i];
411 }
412}
413
chaviwc01e1372020-07-01 12:37:31 -0700414void PointerCoords::transform(const ui::Transform& transform) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700415 const vec2 xy = transform.transform(getXYValue());
416 setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
417 setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
418
Prabir Pradhanc6523582021-05-14 18:02:55 -0700419 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_X) ||
420 BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_Y)) {
421 const ui::Transform rotation(transform.getOrientation());
422 const vec2 relativeXy = rotation.transform(getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
423 getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
424 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
425 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
426 }
427
Prabir Pradhan6b384612021-05-14 16:56:25 -0700428 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) {
429 const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
430 setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val));
431 }
chaviwc01e1372020-07-01 12:37:31 -0700432}
Jeff Brown5912f952013-07-01 19:10:31 -0700433
434// --- PointerProperties ---
435
436bool PointerProperties::operator==(const PointerProperties& other) const {
437 return id == other.id
438 && toolType == other.toolType;
439}
440
441void PointerProperties::copyFrom(const PointerProperties& other) {
442 id = other.id;
443 toolType = other.toolType;
444}
445
446
447// --- MotionEvent ---
448
Garfield Tan4cc839f2020-01-24 11:26:14 -0800449void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600450 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
451 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700452 int32_t buttonState, MotionClassification classification,
453 const ui::Transform& transform, float xPrecision, float yPrecision,
Evan Rosky84f07f02021-04-16 10:42:42 -0700454 float rawXCursorPosition, float rawYCursorPosition,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700455 const ui::Transform& rawTransform, nsecs_t downTime, nsecs_t eventTime,
Evan Rosky09576692021-07-01 12:22:09 -0700456 size_t pointerCount, const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700457 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800458 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700459 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100460 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700461 mFlags = flags;
462 mEdgeFlags = edgeFlags;
463 mMetaState = metaState;
464 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800465 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700466 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700467 mXPrecision = xPrecision;
468 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700469 mRawXCursorPosition = rawXCursorPosition;
470 mRawYCursorPosition = rawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700471 mRawTransform = rawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700472 mDownTime = downTime;
473 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800474 mPointerProperties.insert(mPointerProperties.end(), &pointerProperties[0],
475 &pointerProperties[pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700476 mSampleEventTimes.clear();
477 mSamplePointerCoords.clear();
478 addSample(eventTime, pointerCoords);
479}
480
481void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800482 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
483 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700484 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100485 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700486 mFlags = other->mFlags;
487 mEdgeFlags = other->mEdgeFlags;
488 mMetaState = other->mMetaState;
489 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800490 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700491 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700492 mXPrecision = other->mXPrecision;
493 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700494 mRawXCursorPosition = other->mRawXCursorPosition;
495 mRawYCursorPosition = other->mRawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700496 mRawTransform = other->mRawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700497 mDownTime = other->mDownTime;
498 mPointerProperties = other->mPointerProperties;
499
500 if (keepHistory) {
501 mSampleEventTimes = other->mSampleEventTimes;
502 mSamplePointerCoords = other->mSamplePointerCoords;
503 } else {
504 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500505 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700506 mSamplePointerCoords.clear();
507 size_t pointerCount = other->getPointerCount();
508 size_t historySize = other->getHistorySize();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800509 mSamplePointerCoords
510 .insert(mSamplePointerCoords.end(),
511 &other->mSamplePointerCoords[historySize * pointerCount],
512 &other->mSamplePointerCoords[historySize * pointerCount + pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700513 }
514}
515
516void MotionEvent::addSample(
517 int64_t eventTime,
518 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500519 mSampleEventTimes.push_back(eventTime);
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800520 mSamplePointerCoords.insert(mSamplePointerCoords.end(), &pointerCoords[0],
521 &pointerCoords[getPointerCount()]);
Jeff Brown5912f952013-07-01 19:10:31 -0700522}
523
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800524int MotionEvent::getSurfaceRotation() const {
525 // The surface rotation is the rotation from the window's coordinate space to that of the
526 // display. Since the event's transform takes display space coordinates to window space, the
527 // returned surface rotation is the inverse of the rotation for the surface.
528 switch (mTransform.getOrientation()) {
529 case ui::Transform::ROT_0:
530 return DISPLAY_ORIENTATION_0;
531 case ui::Transform::ROT_90:
532 return DISPLAY_ORIENTATION_270;
533 case ui::Transform::ROT_180:
534 return DISPLAY_ORIENTATION_180;
535 case ui::Transform::ROT_270:
536 return DISPLAY_ORIENTATION_90;
537 default:
538 return -1;
539 }
540}
541
Garfield Tan00f511d2019-06-12 16:55:40 -0700542float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700543 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
Prabir Pradhan0909dc12023-03-09 20:11:09 +0000544 return roundTransformedCoords(vals.x);
Garfield Tan00f511d2019-06-12 16:55:40 -0700545}
546
547float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700548 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
Prabir Pradhan0909dc12023-03-09 20:11:09 +0000549 return roundTransformedCoords(vals.y);
Garfield Tan00f511d2019-06-12 16:55:40 -0700550}
551
Garfield Tan937bb832019-07-25 17:48:31 -0700552void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700553 ui::Transform inverse = mTransform.inverse();
554 vec2 vals = inverse.transform(x, y);
555 mRawXCursorPosition = vals.x;
556 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700557}
558
Jeff Brown5912f952013-07-01 19:10:31 -0700559const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000560 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
561 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
562 }
563 const size_t position = getHistorySize() * getPointerCount() + pointerIndex;
564 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
565 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
566 }
567 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700568}
569
570float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
Evan Rosky84f07f02021-04-16 10:42:42 -0700571 return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700572}
573
574float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700575 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700576}
577
578const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
579 size_t pointerIndex, size_t historicalIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000580 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
581 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
582 }
583 if (CC_UNLIKELY(historicalIndex < 0 || historicalIndex > getHistorySize())) {
584 LOG(FATAL) << __func__ << ": Invalid historical index " << historicalIndex << " for "
585 << *this;
586 }
587 const size_t position = historicalIndex * getPointerCount() + pointerIndex;
588 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
589 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
590 }
591 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700592}
593
594float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan6b384612021-05-14 16:56:25 -0700595 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700596 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
597 return calculateTransformedAxisValue(axis, mSource, mRawTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700598}
599
600float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan9f388812021-05-13 16:54:53 -0700601 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700602 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
603 return calculateTransformedAxisValue(axis, mSource, mTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700604}
605
606ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
607 size_t pointerCount = mPointerProperties.size();
608 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800609 if (mPointerProperties[i].id == pointerId) {
Jeff Brown5912f952013-07-01 19:10:31 -0700610 return i;
611 }
612 }
613 return -1;
614}
615
616void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700617 float currXOffset = mTransform.tx();
618 float currYOffset = mTransform.ty();
619 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700620}
621
Robert Carre07e1032018-11-26 12:55:53 -0800622void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700623 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700624 mRawTransform.set(mRawTransform.tx() * globalScaleFactor,
625 mRawTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800626 mXPrecision *= globalScaleFactor;
627 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700628
629 size_t numSamples = mSamplePointerCoords.size();
630 for (size_t i = 0; i < numSamples; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800631 mSamplePointerCoords[i].scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700632 }
633}
634
chaviw9eaa22c2020-07-01 16:21:27 -0700635void MotionEvent::transform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700636 // We want to preserve the raw axes values stored in the PointerCoords, so we just update the
637 // transform using the values passed in.
chaviw9eaa22c2020-07-01 16:21:27 -0700638 ui::Transform newTransform;
639 newTransform.set(matrix);
640 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700641}
642
Evan Roskyd4d4d802021-05-03 20:12:21 -0700643void MotionEvent::applyTransform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700644 ui::Transform transform;
645 transform.set(matrix);
Evan Roskyd4d4d802021-05-03 20:12:21 -0700646
647 // Apply the transformation to all samples.
Prabir Pradhan6b384612021-05-14 16:56:25 -0700648 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
649 [&transform](PointerCoords& c) { c.transform(transform); });
Prabir Pradhan4b19bd02021-06-01 17:34:59 -0700650
651 if (mRawXCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION &&
652 mRawYCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) {
653 const vec2 cursor = transform.transform(mRawXCursorPosition, mRawYCursorPosition);
654 mRawXCursorPosition = cursor.x;
655 mRawYCursorPosition = cursor.y;
656 }
Evan Roskyd4d4d802021-05-03 20:12:21 -0700657}
658
Brett Chabotfaa986c2020-11-04 17:39:36 -0800659#ifdef __linux__
chaviw9eaa22c2020-07-01 16:21:27 -0700660static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
661 float dsdx, dtdx, tx, dtdy, dsdy, ty;
662 status_t status = parcel.readFloat(&dsdx);
663 status |= parcel.readFloat(&dtdx);
664 status |= parcel.readFloat(&tx);
665 status |= parcel.readFloat(&dtdy);
666 status |= parcel.readFloat(&dsdy);
667 status |= parcel.readFloat(&ty);
668
669 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
670 return status;
671}
672
673static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
674 status_t status = parcel.writeFloat(transform.dsdx());
675 status |= parcel.writeFloat(transform.dtdx());
676 status |= parcel.writeFloat(transform.tx());
677 status |= parcel.writeFloat(transform.dtdy());
678 status |= parcel.writeFloat(transform.dsdy());
679 status |= parcel.writeFloat(transform.ty());
680 return status;
681}
682
Jeff Brown5912f952013-07-01 19:10:31 -0700683status_t MotionEvent::readFromParcel(Parcel* parcel) {
684 size_t pointerCount = parcel->readInt32();
685 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800686 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
687 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700688 return BAD_VALUE;
689 }
690
Garfield Tan4cc839f2020-01-24 11:26:14 -0800691 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700692 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600693 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800694 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600695 std::vector<uint8_t> hmac;
696 status_t result = parcel->readByteVector(&hmac);
697 if (result != OK || hmac.size() != 32) {
698 return BAD_VALUE;
699 }
700 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700701 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100702 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700703 mFlags = parcel->readInt32();
704 mEdgeFlags = parcel->readInt32();
705 mMetaState = parcel->readInt32();
706 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800707 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700708
709 result = android::readFromParcel(mTransform, *parcel);
710 if (result != OK) {
711 return result;
712 }
Jeff Brown5912f952013-07-01 19:10:31 -0700713 mXPrecision = parcel->readFloat();
714 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700715 mRawXCursorPosition = parcel->readFloat();
716 mRawYCursorPosition = parcel->readFloat();
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700717
718 result = android::readFromParcel(mRawTransform, *parcel);
719 if (result != OK) {
720 return result;
721 }
Jeff Brown5912f952013-07-01 19:10:31 -0700722 mDownTime = parcel->readInt64();
723
724 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800725 mPointerProperties.reserve(pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700726 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500727 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700728 mSamplePointerCoords.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800729 mSamplePointerCoords.reserve(sampleCount * pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700730
731 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800732 mPointerProperties.push_back({});
733 PointerProperties& properties = mPointerProperties.back();
Jeff Brown5912f952013-07-01 19:10:31 -0700734 properties.id = parcel->readInt32();
735 properties.toolType = parcel->readInt32();
736 }
737
Dan Austinc94fc452015-09-22 14:22:41 -0700738 while (sampleCount > 0) {
739 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500740 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700741 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800742 mSamplePointerCoords.push_back({});
743 status_t status = mSamplePointerCoords.back().readFromParcel(parcel);
Jeff Brown5912f952013-07-01 19:10:31 -0700744 if (status) {
745 return status;
746 }
747 }
748 }
749 return OK;
750}
751
752status_t MotionEvent::writeToParcel(Parcel* parcel) const {
753 size_t pointerCount = mPointerProperties.size();
754 size_t sampleCount = mSampleEventTimes.size();
755
756 parcel->writeInt32(pointerCount);
757 parcel->writeInt32(sampleCount);
758
Garfield Tan4cc839f2020-01-24 11:26:14 -0800759 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700760 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600761 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800762 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600763 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
764 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700765 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100766 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700767 parcel->writeInt32(mFlags);
768 parcel->writeInt32(mEdgeFlags);
769 parcel->writeInt32(mMetaState);
770 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800771 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700772
773 status_t result = android::writeToParcel(mTransform, *parcel);
774 if (result != OK) {
775 return result;
776 }
Jeff Brown5912f952013-07-01 19:10:31 -0700777 parcel->writeFloat(mXPrecision);
778 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700779 parcel->writeFloat(mRawXCursorPosition);
780 parcel->writeFloat(mRawYCursorPosition);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700781
782 result = android::writeToParcel(mRawTransform, *parcel);
783 if (result != OK) {
784 return result;
785 }
Jeff Brown5912f952013-07-01 19:10:31 -0700786 parcel->writeInt64(mDownTime);
787
788 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800789 const PointerProperties& properties = mPointerProperties[i];
Jeff Brown5912f952013-07-01 19:10:31 -0700790 parcel->writeInt32(properties.id);
791 parcel->writeInt32(properties.toolType);
792 }
793
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800794 const PointerCoords* pc = mSamplePointerCoords.data();
Jeff Brown5912f952013-07-01 19:10:31 -0700795 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500796 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700797 for (size_t i = 0; i < pointerCount; i++) {
798 status_t status = (pc++)->writeToParcel(parcel);
799 if (status) {
800 return status;
801 }
802 }
803 }
804 return OK;
805}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800806#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700807
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600808bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700809 if (isFromSource(source, AINPUT_SOURCE_CLASS_POINTER)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700810 // Specifically excludes HOVER_MOVE and SCROLL.
811 switch (action & AMOTION_EVENT_ACTION_MASK) {
812 case AMOTION_EVENT_ACTION_DOWN:
813 case AMOTION_EVENT_ACTION_MOVE:
814 case AMOTION_EVENT_ACTION_UP:
815 case AMOTION_EVENT_ACTION_POINTER_DOWN:
816 case AMOTION_EVENT_ACTION_POINTER_UP:
817 case AMOTION_EVENT_ACTION_CANCEL:
818 case AMOTION_EVENT_ACTION_OUTSIDE:
819 return true;
820 }
821 }
822 return false;
823}
824
Michael Wright872db4f2014-04-22 15:03:51 -0700825const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700826 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700827}
828
829int32_t MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700830 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700831}
832
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500833std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700834 // Convert MotionEvent action to string
835 switch (action & AMOTION_EVENT_ACTION_MASK) {
836 case AMOTION_EVENT_ACTION_DOWN:
837 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700838 case AMOTION_EVENT_ACTION_UP:
839 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500840 case AMOTION_EVENT_ACTION_MOVE:
841 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700842 case AMOTION_EVENT_ACTION_CANCEL:
843 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500844 case AMOTION_EVENT_ACTION_OUTSIDE:
845 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700846 case AMOTION_EVENT_ACTION_POINTER_DOWN:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000847 return StringPrintf("POINTER_DOWN(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700848 case AMOTION_EVENT_ACTION_POINTER_UP:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000849 return StringPrintf("POINTER_UP(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500850 case AMOTION_EVENT_ACTION_HOVER_MOVE:
851 return "HOVER_MOVE";
852 case AMOTION_EVENT_ACTION_SCROLL:
853 return "SCROLL";
854 case AMOTION_EVENT_ACTION_HOVER_ENTER:
855 return "HOVER_ENTER";
856 case AMOTION_EVENT_ACTION_HOVER_EXIT:
857 return "HOVER_EXIT";
858 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
859 return "BUTTON_PRESS";
860 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
861 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700862 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500863 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700864}
865
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700866// Apply the given transformation to the point without checking whether the entire transform
867// should be disregarded altogether for the provided source.
868static inline vec2 calculateTransformedXYUnchecked(uint32_t source, const ui::Transform& transform,
869 const vec2& xy) {
870 return shouldDisregardOffset(source) ? transformWithoutTranslation(transform, xy)
Prabir Pradhan0909dc12023-03-09 20:11:09 +0000871 : roundTransformedCoords(transform.transform(xy));
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700872}
873
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700874vec2 MotionEvent::calculateTransformedXY(uint32_t source, const ui::Transform& transform,
875 const vec2& xy) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700876 if (shouldDisregardTransformation(source)) {
877 return xy;
878 }
879 return calculateTransformedXYUnchecked(source, transform, xy);
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700880}
881
Prabir Pradhan8e6ce222022-02-24 09:08:54 -0800882// Keep in sync with calculateTransformedCoords.
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700883float MotionEvent::calculateTransformedAxisValue(int32_t axis, uint32_t source,
884 const ui::Transform& transform,
885 const PointerCoords& coords) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700886 if (shouldDisregardTransformation(source)) {
887 return coords.getAxisValue(axis);
888 }
889
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700890 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700891 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700892 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
893 return xy[axis];
894 }
895
896 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
897 const vec2 relativeXy =
898 transformWithoutTranslation(transform,
899 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
900 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
901 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
902 }
903
904 if (axis == AMOTION_EVENT_AXIS_ORIENTATION) {
905 return transformAngle(transform, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION));
906 }
907
908 return coords.getAxisValue(axis);
909}
910
Prabir Pradhan8e6ce222022-02-24 09:08:54 -0800911// Keep in sync with calculateTransformedAxisValue. This is an optimization of
912// calculateTransformedAxisValue for all PointerCoords axes.
913PointerCoords MotionEvent::calculateTransformedCoords(uint32_t source,
914 const ui::Transform& transform,
915 const PointerCoords& coords) {
916 if (shouldDisregardTransformation(source)) {
917 return coords;
918 }
919 PointerCoords out = coords;
920
921 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
922 out.setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
923 out.setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
924
925 const vec2 relativeXy =
926 transformWithoutTranslation(transform,
927 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
928 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
929 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
930 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
931
932 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
933 transformAngle(transform,
934 coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)));
935
936 return out;
937}
938
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000939std::ostream& operator<<(std::ostream& out, const MotionEvent& event) {
940 out << "MotionEvent { action=" << MotionEvent::actionToString(event.getAction());
941 if (event.getActionButton() != 0) {
942 out << ", actionButton=" << std::to_string(event.getActionButton());
943 }
944 const size_t pointerCount = event.getPointerCount();
hupeng3aa5a51a2022-09-02 16:00:18 +0800945 LOG_ALWAYS_FATAL_IF(pointerCount > MAX_POINTERS, "Too many pointers : pointerCount = %zu",
946 pointerCount);
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000947 for (size_t i = 0; i < pointerCount; i++) {
948 out << ", id[" << i << "]=" << event.getPointerId(i);
949 float x = event.getX(i);
950 float y = event.getY(i);
951 if (x != 0 || y != 0) {
952 out << ", x[" << i << "]=" << x;
953 out << ", y[" << i << "]=" << y;
954 }
955 int toolType = event.getToolType(i);
956 if (toolType != AMOTION_EVENT_TOOL_TYPE_FINGER) {
957 out << ", toolType[" << i << "]=" << toolType;
958 }
959 }
960 if (event.getButtonState() != 0) {
961 out << ", buttonState=" << event.getButtonState();
962 }
963 if (event.getClassification() != MotionClassification::NONE) {
964 out << ", classification=" << motionClassificationToString(event.getClassification());
965 }
966 if (event.getMetaState() != 0) {
967 out << ", metaState=" << event.getMetaState();
968 }
969 if (event.getEdgeFlags() != 0) {
970 out << ", edgeFlags=" << event.getEdgeFlags();
971 }
972 if (pointerCount != 1) {
973 out << ", pointerCount=" << pointerCount;
974 }
975 if (event.getHistorySize() != 0) {
976 out << ", historySize=" << event.getHistorySize();
977 }
978 out << ", eventTime=" << event.getEventTime();
979 out << ", downTime=" << event.getDownTime();
980 out << ", deviceId=" << event.getDeviceId();
981 out << ", source=" << inputEventSourceToString(event.getSource());
982 out << ", displayId=" << event.getDisplayId();
983 out << ", eventId=" << event.getId();
984 out << "}";
985 return out;
986}
987
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800988// --- FocusEvent ---
989
Antonio Kantek3cfec7b2021-11-05 18:26:17 -0700990void FocusEvent::initialize(int32_t id, bool hasFocus) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800991 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600992 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800993 mHasFocus = hasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800994}
995
996void FocusEvent::initialize(const FocusEvent& from) {
997 InputEvent::initialize(from);
998 mHasFocus = from.mHasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800999}
Jeff Brown5912f952013-07-01 19:10:31 -07001000
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001001// --- CaptureEvent ---
1002
1003void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) {
1004 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1005 ADISPLAY_ID_NONE, INVALID_HMAC);
1006 mPointerCaptureEnabled = pointerCaptureEnabled;
1007}
1008
1009void CaptureEvent::initialize(const CaptureEvent& from) {
1010 InputEvent::initialize(from);
1011 mPointerCaptureEnabled = from.mPointerCaptureEnabled;
1012}
1013
arthurhung7632c332020-12-30 16:58:01 +08001014// --- DragEvent ---
1015
1016void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) {
1017 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1018 ADISPLAY_ID_NONE, INVALID_HMAC);
1019 mIsExiting = isExiting;
1020 mX = x;
1021 mY = y;
1022}
1023
1024void DragEvent::initialize(const DragEvent& from) {
1025 InputEvent::initialize(from);
1026 mIsExiting = from.mIsExiting;
1027 mX = from.mX;
1028 mY = from.mY;
1029}
1030
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001031// --- TouchModeEvent ---
1032
1033void TouchModeEvent::initialize(int32_t id, bool isInTouchMode) {
1034 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1035 ADISPLAY_ID_NONE, INVALID_HMAC);
1036 mIsInTouchMode = isInTouchMode;
1037}
1038
1039void TouchModeEvent::initialize(const TouchModeEvent& from) {
1040 InputEvent::initialize(from);
1041 mIsInTouchMode = from.mIsInTouchMode;
1042}
1043
Jeff Brown5912f952013-07-01 19:10:31 -07001044// --- PooledInputEventFactory ---
1045
1046PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
1047 mMaxPoolSize(maxPoolSize) {
1048}
1049
1050PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -07001051}
1052
1053KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001054 if (mKeyEventPool.empty()) {
1055 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001056 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001057 KeyEvent* event = mKeyEventPool.front().release();
1058 mKeyEventPool.pop();
1059 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001060}
1061
1062MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001063 if (mMotionEventPool.empty()) {
1064 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001065 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001066 MotionEvent* event = mMotionEventPool.front().release();
1067 mMotionEventPool.pop();
1068 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001069}
1070
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001071FocusEvent* PooledInputEventFactory::createFocusEvent() {
1072 if (mFocusEventPool.empty()) {
1073 return new FocusEvent();
1074 }
1075 FocusEvent* event = mFocusEventPool.front().release();
1076 mFocusEventPool.pop();
1077 return event;
1078}
1079
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001080CaptureEvent* PooledInputEventFactory::createCaptureEvent() {
1081 if (mCaptureEventPool.empty()) {
1082 return new CaptureEvent();
1083 }
1084 CaptureEvent* event = mCaptureEventPool.front().release();
1085 mCaptureEventPool.pop();
1086 return event;
1087}
1088
arthurhung7632c332020-12-30 16:58:01 +08001089DragEvent* PooledInputEventFactory::createDragEvent() {
1090 if (mDragEventPool.empty()) {
1091 return new DragEvent();
1092 }
1093 DragEvent* event = mDragEventPool.front().release();
1094 mDragEventPool.pop();
1095 return event;
1096}
1097
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001098TouchModeEvent* PooledInputEventFactory::createTouchModeEvent() {
1099 if (mTouchModeEventPool.empty()) {
1100 return new TouchModeEvent();
1101 }
1102 TouchModeEvent* event = mTouchModeEventPool.front().release();
1103 mTouchModeEventPool.pop();
1104 return event;
1105}
1106
Jeff Brown5912f952013-07-01 19:10:31 -07001107void PooledInputEventFactory::recycle(InputEvent* event) {
1108 switch (event->getType()) {
1109 case AINPUT_EVENT_TYPE_KEY:
1110 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001111 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -07001112 return;
1113 }
1114 break;
1115 case AINPUT_EVENT_TYPE_MOTION:
1116 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001117 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -07001118 return;
1119 }
1120 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001121 case AINPUT_EVENT_TYPE_FOCUS:
1122 if (mFocusEventPool.size() < mMaxPoolSize) {
1123 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
1124 return;
1125 }
1126 break;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001127 case AINPUT_EVENT_TYPE_CAPTURE:
1128 if (mCaptureEventPool.size() < mMaxPoolSize) {
1129 mCaptureEventPool.push(
1130 std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event)));
1131 return;
1132 }
1133 break;
arthurhung7632c332020-12-30 16:58:01 +08001134 case AINPUT_EVENT_TYPE_DRAG:
1135 if (mDragEventPool.size() < mMaxPoolSize) {
1136 mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event)));
1137 return;
1138 }
1139 break;
Antonio Kantekeb4a30c2021-09-28 17:49:49 -07001140 case AINPUT_EVENT_TYPE_TOUCH_MODE:
1141 if (mTouchModeEventPool.size() < mMaxPoolSize) {
1142 mTouchModeEventPool.push(
1143 std::unique_ptr<TouchModeEvent>(static_cast<TouchModeEvent*>(event)));
1144 return;
1145 }
1146 break;
Jeff Brown5912f952013-07-01 19:10:31 -07001147 }
1148 delete event;
1149}
1150
1151} // namespace android