blob: 000775b42a7ba18ba44eb71c5c54bd507b5eb3c0 [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>
Michael Wright635422b2022-12-02 00:43:56 +000024#include <optional>
Jeff Brown5912f952013-07-01 19:10:31 -070025
Siarhei Vishniakou31977182022-09-30 08:51:23 -070026#include <android-base/file.h>
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +000027#include <android-base/logging.h>
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050028#include <android-base/stringprintf.h>
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +000029#include <cutils/compiler.h>
chaviw98318de2021-05-19 16:45:23 -050030#include <gui/constants.h>
Prabir Pradhan092f3a92021-11-25 10:53:27 -080031#include <input/DisplayViewport.h>
Jeff Brown5912f952013-07-01 19:10:31 -070032#include <input/Input.h>
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080033#include <input/InputDevice.h>
Michael Wright872db4f2014-04-22 15:03:51 -070034#include <input/InputEventLabels.h>
Jeff Brown5912f952013-07-01 19:10:31 -070035
Brett Chabotfaa986c2020-11-04 17:39:36 -080036#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -070037#include <binder/Parcel.h>
Brett Chabotfaa986c2020-11-04 17:39:36 -080038#endif
Siarhei Vishniakou63740b92022-10-20 10:28:08 -070039#if defined(__ANDROID__)
40#include <sys/random.h>
41#endif
Jeff Brown5912f952013-07-01 19:10:31 -070042
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050043using android::base::StringPrintf;
44
Jeff Brown5912f952013-07-01 19:10:31 -070045namespace android {
46
Prabir Pradhan6b384612021-05-14 16:56:25 -070047namespace {
48
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070049bool shouldDisregardTransformation(uint32_t source) {
Prabir Pradhan258e2b92022-06-24 18:37:04 +000050 // Do not apply any transformations to axes from joysticks, touchpads, or relative mice.
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070051 return isFromSource(source, AINPUT_SOURCE_CLASS_JOYSTICK) ||
Prabir Pradhan258e2b92022-06-24 18:37:04 +000052 isFromSource(source, AINPUT_SOURCE_CLASS_POSITION) ||
53 isFromSource(source, AINPUT_SOURCE_MOUSE_RELATIVE);
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070054}
55
56bool shouldDisregardOffset(uint32_t source) {
Prabir Pradhan9f388812021-05-13 16:54:53 -070057 // Pointer events are the only type of events that refer to absolute coordinates on the display,
58 // so we should apply the entire window transform. For other types of events, we should make
59 // sure to not apply the window translation/offset.
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070060 return !isFromSource(source, AINPUT_SOURCE_CLASS_POINTER);
Prabir Pradhan9f388812021-05-13 16:54:53 -070061}
62
Prabir Pradhan6b384612021-05-14 16:56:25 -070063} // namespace
64
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -080065const char* motionClassificationToString(MotionClassification classification) {
66 switch (classification) {
67 case MotionClassification::NONE:
68 return "NONE";
69 case MotionClassification::AMBIGUOUS_GESTURE:
70 return "AMBIGUOUS_GESTURE";
71 case MotionClassification::DEEP_PRESS:
72 return "DEEP_PRESS";
Harry Cutts2800fb02022-09-15 13:49:23 +000073 case MotionClassification::TWO_FINGER_SWIPE:
74 return "TWO_FINGER_SWIPE";
Harry Cuttsc5748d12022-12-02 17:30:18 +000075 case MotionClassification::MULTI_FINGER_SWIPE:
76 return "MULTI_FINGER_SWIPE";
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -080077 }
78}
79
Siarhei Vishniakoud5fe5182022-07-20 23:28:40 +000080const char* motionToolTypeToString(int32_t toolType) {
81 switch (toolType) {
82 case AMOTION_EVENT_TOOL_TYPE_UNKNOWN:
83 return "UNKNOWN";
84 case AMOTION_EVENT_TOOL_TYPE_FINGER:
85 return "FINGER";
86 case AMOTION_EVENT_TOOL_TYPE_STYLUS:
87 return "STYLUS";
88 case AMOTION_EVENT_TOOL_TYPE_MOUSE:
89 return "MOUSE";
90 case AMOTION_EVENT_TOOL_TYPE_ERASER:
91 return "ERASER";
92 case AMOTION_EVENT_TOOL_TYPE_PALM:
93 return "PALM";
94 default:
95 return "INVALID";
96 }
97}
98
Garfield Tan84b087e2020-01-23 10:49:05 -080099// --- IdGenerator ---
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700100#if defined(__ANDROID__)
101[[maybe_unused]]
102#endif
103static status_t
104getRandomBytes(uint8_t* data, size_t size) {
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700105 int ret = TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY | O_CLOEXEC | O_NOFOLLOW));
106 if (ret == -1) {
107 return -errno;
108 }
109
110 base::unique_fd fd(ret);
111 if (!base::ReadFully(fd, data, size)) {
112 return -errno;
113 }
114 return OK;
115}
116
Garfield Tan84b087e2020-01-23 10:49:05 -0800117IdGenerator::IdGenerator(Source source) : mSource(source) {}
118
119int32_t IdGenerator::nextId() const {
120 constexpr uint32_t SEQUENCE_NUMBER_MASK = ~SOURCE_MASK;
121 int32_t id = 0;
122
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700123#if defined(__ANDROID__)
124 // On device, prefer 'getrandom' to '/dev/urandom' because it's faster.
125 constexpr size_t BUF_LEN = sizeof(id);
126 size_t totalBytes = 0;
127 while (totalBytes < BUF_LEN) {
128 ssize_t bytes = TEMP_FAILURE_RETRY(getrandom(&id, BUF_LEN, GRND_NONBLOCK));
129 if (CC_UNLIKELY(bytes < 0)) {
130 ALOGW("Failed to fill in random number for sequence number: %s.", strerror(errno));
131 id = 0;
132 break;
133 }
134 totalBytes += bytes;
135 }
136#else
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700137#if defined(__linux__)
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700138 // On host, <sys/random.h> / GRND_NONBLOCK is not available
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700139 while (true) {
140 status_t result = getRandomBytes(reinterpret_cast<uint8_t*>(&id), sizeof(id));
141 if (result == OK) {
Garfield Tan84b087e2020-01-23 10:49:05 -0800142 break;
143 }
Garfield Tan84b087e2020-01-23 10:49:05 -0800144 }
Siarhei Vishniakou31977182022-09-30 08:51:23 -0700145#endif // __linux__
Siarhei Vishniakou63740b92022-10-20 10:28:08 -0700146#endif // __ANDROID__
Garfield Tan84b087e2020-01-23 10:49:05 -0800147 return (id & SEQUENCE_NUMBER_MASK) | static_cast<int32_t>(mSource);
148}
149
Jeff Brown5912f952013-07-01 19:10:31 -0700150// --- InputEvent ---
151
Prabir Pradhande69f8a2021-11-18 16:40:34 +0000152vec2 transformWithoutTranslation(const ui::Transform& transform, const vec2& xy) {
153 const vec2 transformedXy = transform.transform(xy);
154 const vec2 transformedOrigin = transform.transform(0, 0);
155 return transformedXy - transformedOrigin;
156}
157
Prabir Pradhane2e10b42022-11-17 20:59:36 +0000158float transformAngle(const ui::Transform& transform, float angleRadians) {
159 // Construct and transform a vector oriented at the specified clockwise angle from vertical.
160 // Coordinate system: down is increasing Y, right is increasing X.
161 float x = sinf(angleRadians);
162 float y = -cosf(angleRadians);
163 vec2 transformedPoint = transform.transform(x, y);
164
165 // Determine how the origin is transformed by the matrix so that we
166 // can transform orientation vectors.
167 const vec2 origin = transform.transform(0, 0);
168
169 transformedPoint.x -= origin.x;
170 transformedPoint.y -= origin.y;
171
172 // Derive the transformed vector's clockwise angle from vertical.
173 // The return value of atan2f is in range [-pi, pi] which conforms to the orientation API.
174 return atan2f(transformedPoint.x, -transformedPoint.y);
175}
176
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800177const char* inputEventTypeToString(int32_t type) {
178 switch (type) {
179 case AINPUT_EVENT_TYPE_KEY: {
180 return "KEY";
181 }
182 case AINPUT_EVENT_TYPE_MOTION: {
183 return "MOTION";
184 }
185 case AINPUT_EVENT_TYPE_FOCUS: {
186 return "FOCUS";
187 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800188 case AINPUT_EVENT_TYPE_CAPTURE: {
189 return "CAPTURE";
190 }
arthurhung7632c332020-12-30 16:58:01 +0800191 case AINPUT_EVENT_TYPE_DRAG: {
192 return "DRAG";
193 }
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700194 case AINPUT_EVENT_TYPE_TOUCH_MODE: {
195 return "TOUCH_MODE";
196 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800197 }
198 return "UNKNOWN";
199}
200
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800201std::string inputEventSourceToString(int32_t source) {
202 if (source == AINPUT_SOURCE_UNKNOWN) {
203 return "UNKNOWN";
204 }
205 if (source == static_cast<int32_t>(AINPUT_SOURCE_ANY)) {
206 return "ANY";
207 }
208 static const std::map<int32_t, const char*> SOURCES{
209 {AINPUT_SOURCE_KEYBOARD, "KEYBOARD"},
210 {AINPUT_SOURCE_DPAD, "DPAD"},
211 {AINPUT_SOURCE_GAMEPAD, "GAMEPAD"},
212 {AINPUT_SOURCE_TOUCHSCREEN, "TOUCHSCREEN"},
213 {AINPUT_SOURCE_MOUSE, "MOUSE"},
214 {AINPUT_SOURCE_STYLUS, "STYLUS"},
215 {AINPUT_SOURCE_BLUETOOTH_STYLUS, "BLUETOOTH_STYLUS"},
216 {AINPUT_SOURCE_TRACKBALL, "TRACKBALL"},
217 {AINPUT_SOURCE_MOUSE_RELATIVE, "MOUSE_RELATIVE"},
218 {AINPUT_SOURCE_TOUCHPAD, "TOUCHPAD"},
219 {AINPUT_SOURCE_TOUCH_NAVIGATION, "TOUCH_NAVIGATION"},
220 {AINPUT_SOURCE_JOYSTICK, "JOYSTICK"},
221 {AINPUT_SOURCE_HDMI, "HDMI"},
222 {AINPUT_SOURCE_SENSOR, "SENSOR"},
223 {AINPUT_SOURCE_ROTARY_ENCODER, "ROTARY_ENCODER"},
224 };
225 std::string result;
226 for (const auto& [source_entry, str] : SOURCES) {
227 if ((source & source_entry) == source_entry) {
228 if (!result.empty()) {
229 result += " | ";
230 }
231 result += str;
232 }
233 }
234 if (result.empty()) {
235 result = StringPrintf("0x%08x", source);
236 }
237 return result;
238}
239
240bool isFromSource(uint32_t source, uint32_t test) {
241 return (source & test) == test;
242}
243
Prabir Pradhane5626962022-10-27 20:30:53 +0000244bool isStylusToolType(uint32_t toolType) {
245 return toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS || toolType == AMOTION_EVENT_TOOL_TYPE_ERASER;
246}
247
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800248VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
249 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
250 event.getSource(), event.getDisplayId()},
251 event.getAction(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800252 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800253 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800254 event.getKeyCode(),
255 event.getScanCode(),
256 event.getMetaState(),
257 event.getRepeatCount()};
258}
259
260VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
261 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
262 event.getSource(), event.getDisplayId()},
263 event.getRawX(0),
264 event.getRawY(0),
265 event.getActionMasked(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800266 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800267 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800268 event.getMetaState(),
269 event.getButtonState()};
270}
271
Garfield Tan4cc839f2020-01-24 11:26:14 -0800272void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600273 std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800274 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700275 mDeviceId = deviceId;
276 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100277 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600278 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700279}
280
281void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800282 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700283 mDeviceId = from.mDeviceId;
284 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100285 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600286 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700287}
288
Garfield Tan4cc839f2020-01-24 11:26:14 -0800289int32_t InputEvent::nextId() {
290 static IdGenerator idGen(IdGenerator::Source::OTHER);
291 return idGen.nextId();
292}
293
Jeff Brown5912f952013-07-01 19:10:31 -0700294// --- KeyEvent ---
295
Michael Wright872db4f2014-04-22 15:03:51 -0700296const char* KeyEvent::getLabel(int32_t keyCode) {
Chris Ye4958d062020-08-20 13:21:10 -0700297 return InputEventLookup::getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700298}
299
Michael Wright872db4f2014-04-22 15:03:51 -0700300int32_t KeyEvent::getKeyCodeFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700301 return InputEventLookup::getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700302}
303
Garfield Tan4cc839f2020-01-24 11:26:14 -0800304void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600305 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
306 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
307 nsecs_t downTime, nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800308 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700309 mAction = action;
310 mFlags = flags;
311 mKeyCode = keyCode;
312 mScanCode = scanCode;
313 mMetaState = metaState;
314 mRepeatCount = repeatCount;
315 mDownTime = downTime;
316 mEventTime = eventTime;
317}
318
319void KeyEvent::initialize(const KeyEvent& from) {
320 InputEvent::initialize(from);
321 mAction = from.mAction;
322 mFlags = from.mFlags;
323 mKeyCode = from.mKeyCode;
324 mScanCode = from.mScanCode;
325 mMetaState = from.mMetaState;
326 mRepeatCount = from.mRepeatCount;
327 mDownTime = from.mDownTime;
328 mEventTime = from.mEventTime;
329}
330
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700331const char* KeyEvent::actionToString(int32_t action) {
332 // Convert KeyEvent action to string
333 switch (action) {
334 case AKEY_EVENT_ACTION_DOWN:
335 return "DOWN";
336 case AKEY_EVENT_ACTION_UP:
337 return "UP";
338 case AKEY_EVENT_ACTION_MULTIPLE:
339 return "MULTIPLE";
340 }
341 return "UNKNOWN";
342}
Jeff Brown5912f952013-07-01 19:10:31 -0700343
344// --- PointerCoords ---
345
346float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700347 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700348 return 0;
349 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700350 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700351}
352
353status_t PointerCoords::setAxisValue(int32_t axis, float value) {
354 if (axis < 0 || axis > 63) {
355 return NAME_NOT_FOUND;
356 }
357
Michael Wright38dcdff2014-03-19 12:06:10 -0700358 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
359 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700360 if (value == 0) {
361 return OK; // axes with value 0 do not need to be stored
362 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700363
364 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700365 if (count >= MAX_AXES) {
366 tooManyAxes(axis);
367 return NO_MEMORY;
368 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700369 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700370 for (uint32_t i = count; i > index; i--) {
371 values[i] = values[i - 1];
372 }
373 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700374
Jeff Brown5912f952013-07-01 19:10:31 -0700375 values[index] = value;
376 return OK;
377}
378
379static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
380 float value = c.getAxisValue(axis);
381 if (value != 0) {
382 c.setAxisValue(axis, value * scaleFactor);
383 }
384}
385
Robert Carre07e1032018-11-26 12:55:53 -0800386void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700387 // No need to scale pressure or size since they are normalized.
388 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800389
390 // If there is a global scale factor, it is included in the windowX/YScale
391 // so we don't need to apply it twice to the X/Y axes.
392 // However we don't want to apply any windowXYScale not included in the global scale
393 // to the TOUCH_MAJOR/MINOR coordinates.
394 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
395 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
396 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
397 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
398 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
399 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
Prabir Pradhanc6523582021-05-14 18:02:55 -0700400 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_X, windowXScale);
401 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_Y, windowYScale);
Robert Carre07e1032018-11-26 12:55:53 -0800402}
403
Brett Chabotfaa986c2020-11-04 17:39:36 -0800404#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700405status_t PointerCoords::readFromParcel(Parcel* parcel) {
406 bits = parcel->readInt64();
407
Michael Wright38dcdff2014-03-19 12:06:10 -0700408 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700409 if (count > MAX_AXES) {
410 return BAD_VALUE;
411 }
412
413 for (uint32_t i = 0; i < count; i++) {
414 values[i] = parcel->readFloat();
415 }
416 return OK;
417}
418
419status_t PointerCoords::writeToParcel(Parcel* parcel) const {
420 parcel->writeInt64(bits);
421
Michael Wright38dcdff2014-03-19 12:06:10 -0700422 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700423 for (uint32_t i = 0; i < count; i++) {
424 parcel->writeFloat(values[i]);
425 }
426 return OK;
427}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800428#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700429
430void PointerCoords::tooManyAxes(int axis) {
431 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
432 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
433}
434
435bool PointerCoords::operator==(const PointerCoords& other) const {
436 if (bits != other.bits) {
437 return false;
438 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700439 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700440 for (uint32_t i = 0; i < count; i++) {
441 if (values[i] != other.values[i]) {
442 return false;
443 }
444 }
445 return true;
446}
447
chaviwc01e1372020-07-01 12:37:31 -0700448void PointerCoords::transform(const ui::Transform& transform) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700449 const vec2 xy = transform.transform(getXYValue());
450 setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
451 setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
452
Prabir Pradhanc6523582021-05-14 18:02:55 -0700453 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_X) ||
454 BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_Y)) {
455 const ui::Transform rotation(transform.getOrientation());
456 const vec2 relativeXy = rotation.transform(getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
457 getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
458 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
459 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
460 }
461
Prabir Pradhan6b384612021-05-14 16:56:25 -0700462 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) {
463 const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
464 setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val));
465 }
chaviwc01e1372020-07-01 12:37:31 -0700466}
Jeff Brown5912f952013-07-01 19:10:31 -0700467
468// --- PointerProperties ---
469
470bool PointerProperties::operator==(const PointerProperties& other) const {
471 return id == other.id
472 && toolType == other.toolType;
473}
474
475void PointerProperties::copyFrom(const PointerProperties& other) {
476 id = other.id;
477 toolType = other.toolType;
478}
479
480
481// --- MotionEvent ---
482
Garfield Tan4cc839f2020-01-24 11:26:14 -0800483void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600484 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
485 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700486 int32_t buttonState, MotionClassification classification,
487 const ui::Transform& transform, float xPrecision, float yPrecision,
Evan Rosky84f07f02021-04-16 10:42:42 -0700488 float rawXCursorPosition, float rawYCursorPosition,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700489 const ui::Transform& rawTransform, nsecs_t downTime, nsecs_t eventTime,
Evan Rosky09576692021-07-01 12:22:09 -0700490 size_t pointerCount, const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700491 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800492 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700493 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100494 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700495 mFlags = flags;
496 mEdgeFlags = edgeFlags;
497 mMetaState = metaState;
498 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800499 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700500 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700501 mXPrecision = xPrecision;
502 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700503 mRawXCursorPosition = rawXCursorPosition;
504 mRawYCursorPosition = rawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700505 mRawTransform = rawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700506 mDownTime = downTime;
507 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800508 mPointerProperties.insert(mPointerProperties.end(), &pointerProperties[0],
509 &pointerProperties[pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700510 mSampleEventTimes.clear();
511 mSamplePointerCoords.clear();
512 addSample(eventTime, pointerCoords);
513}
514
515void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800516 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
517 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700518 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100519 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700520 mFlags = other->mFlags;
521 mEdgeFlags = other->mEdgeFlags;
522 mMetaState = other->mMetaState;
523 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800524 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700525 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700526 mXPrecision = other->mXPrecision;
527 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700528 mRawXCursorPosition = other->mRawXCursorPosition;
529 mRawYCursorPosition = other->mRawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700530 mRawTransform = other->mRawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700531 mDownTime = other->mDownTime;
532 mPointerProperties = other->mPointerProperties;
533
534 if (keepHistory) {
535 mSampleEventTimes = other->mSampleEventTimes;
536 mSamplePointerCoords = other->mSamplePointerCoords;
537 } else {
538 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500539 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700540 mSamplePointerCoords.clear();
541 size_t pointerCount = other->getPointerCount();
542 size_t historySize = other->getHistorySize();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800543 mSamplePointerCoords
544 .insert(mSamplePointerCoords.end(),
545 &other->mSamplePointerCoords[historySize * pointerCount],
546 &other->mSamplePointerCoords[historySize * pointerCount + pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700547 }
548}
549
550void MotionEvent::addSample(
551 int64_t eventTime,
552 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500553 mSampleEventTimes.push_back(eventTime);
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800554 mSamplePointerCoords.insert(mSamplePointerCoords.end(), &pointerCoords[0],
555 &pointerCoords[getPointerCount()]);
Jeff Brown5912f952013-07-01 19:10:31 -0700556}
557
Michael Wright635422b2022-12-02 00:43:56 +0000558std::optional<ui::Rotation> MotionEvent::getSurfaceRotation() const {
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800559 // The surface rotation is the rotation from the window's coordinate space to that of the
560 // display. Since the event's transform takes display space coordinates to window space, the
561 // returned surface rotation is the inverse of the rotation for the surface.
562 switch (mTransform.getOrientation()) {
563 case ui::Transform::ROT_0:
Michael Wright635422b2022-12-02 00:43:56 +0000564 return ui::ROTATION_0;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800565 case ui::Transform::ROT_90:
Michael Wright635422b2022-12-02 00:43:56 +0000566 return ui::ROTATION_270;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800567 case ui::Transform::ROT_180:
Michael Wright635422b2022-12-02 00:43:56 +0000568 return ui::ROTATION_180;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800569 case ui::Transform::ROT_270:
Michael Wright635422b2022-12-02 00:43:56 +0000570 return ui::ROTATION_90;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800571 default:
Michael Wright635422b2022-12-02 00:43:56 +0000572 return std::nullopt;
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800573 }
574}
575
Garfield Tan00f511d2019-06-12 16:55:40 -0700576float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700577 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
578 return vals.x;
Garfield Tan00f511d2019-06-12 16:55:40 -0700579}
580
581float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700582 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
583 return vals.y;
Garfield Tan00f511d2019-06-12 16:55:40 -0700584}
585
Garfield Tan937bb832019-07-25 17:48:31 -0700586void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700587 ui::Transform inverse = mTransform.inverse();
588 vec2 vals = inverse.transform(x, y);
589 mRawXCursorPosition = vals.x;
590 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700591}
592
Jeff Brown5912f952013-07-01 19:10:31 -0700593const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000594 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
595 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
596 }
597 const size_t position = getHistorySize() * getPointerCount() + pointerIndex;
598 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
599 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
600 }
601 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700602}
603
604float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
Evan Rosky84f07f02021-04-16 10:42:42 -0700605 return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700606}
607
608float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700609 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700610}
611
612const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
613 size_t pointerIndex, size_t historicalIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000614 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
615 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
616 }
617 if (CC_UNLIKELY(historicalIndex < 0 || historicalIndex > getHistorySize())) {
618 LOG(FATAL) << __func__ << ": Invalid historical index " << historicalIndex << " for "
619 << *this;
620 }
621 const size_t position = historicalIndex * getPointerCount() + pointerIndex;
622 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
623 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
624 }
625 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700626}
627
628float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan6b384612021-05-14 16:56:25 -0700629 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700630 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
631 return calculateTransformedAxisValue(axis, mSource, mRawTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700632}
633
634float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan9f388812021-05-13 16:54:53 -0700635 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700636 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
637 return calculateTransformedAxisValue(axis, mSource, mTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700638}
639
640ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
641 size_t pointerCount = mPointerProperties.size();
642 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800643 if (mPointerProperties[i].id == pointerId) {
Jeff Brown5912f952013-07-01 19:10:31 -0700644 return i;
645 }
646 }
647 return -1;
648}
649
650void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700651 float currXOffset = mTransform.tx();
652 float currYOffset = mTransform.ty();
653 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700654}
655
Robert Carre07e1032018-11-26 12:55:53 -0800656void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700657 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700658 mRawTransform.set(mRawTransform.tx() * globalScaleFactor,
659 mRawTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800660 mXPrecision *= globalScaleFactor;
661 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700662
663 size_t numSamples = mSamplePointerCoords.size();
664 for (size_t i = 0; i < numSamples; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800665 mSamplePointerCoords[i].scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700666 }
667}
668
chaviw9eaa22c2020-07-01 16:21:27 -0700669void MotionEvent::transform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700670 // We want to preserve the raw axes values stored in the PointerCoords, so we just update the
671 // transform using the values passed in.
chaviw9eaa22c2020-07-01 16:21:27 -0700672 ui::Transform newTransform;
673 newTransform.set(matrix);
674 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700675}
676
Evan Roskyd4d4d802021-05-03 20:12:21 -0700677void MotionEvent::applyTransform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700678 ui::Transform transform;
679 transform.set(matrix);
Evan Roskyd4d4d802021-05-03 20:12:21 -0700680
681 // Apply the transformation to all samples.
Prabir Pradhan6b384612021-05-14 16:56:25 -0700682 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
683 [&transform](PointerCoords& c) { c.transform(transform); });
Prabir Pradhan4b19bd02021-06-01 17:34:59 -0700684
685 if (mRawXCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION &&
686 mRawYCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) {
687 const vec2 cursor = transform.transform(mRawXCursorPosition, mRawYCursorPosition);
688 mRawXCursorPosition = cursor.x;
689 mRawYCursorPosition = cursor.y;
690 }
Evan Roskyd4d4d802021-05-03 20:12:21 -0700691}
692
Brett Chabotfaa986c2020-11-04 17:39:36 -0800693#ifdef __linux__
chaviw9eaa22c2020-07-01 16:21:27 -0700694static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
695 float dsdx, dtdx, tx, dtdy, dsdy, ty;
696 status_t status = parcel.readFloat(&dsdx);
697 status |= parcel.readFloat(&dtdx);
698 status |= parcel.readFloat(&tx);
699 status |= parcel.readFloat(&dtdy);
700 status |= parcel.readFloat(&dsdy);
701 status |= parcel.readFloat(&ty);
702
703 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
704 return status;
705}
706
707static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
708 status_t status = parcel.writeFloat(transform.dsdx());
709 status |= parcel.writeFloat(transform.dtdx());
710 status |= parcel.writeFloat(transform.tx());
711 status |= parcel.writeFloat(transform.dtdy());
712 status |= parcel.writeFloat(transform.dsdy());
713 status |= parcel.writeFloat(transform.ty());
714 return status;
715}
716
Jeff Brown5912f952013-07-01 19:10:31 -0700717status_t MotionEvent::readFromParcel(Parcel* parcel) {
718 size_t pointerCount = parcel->readInt32();
719 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800720 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
721 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700722 return BAD_VALUE;
723 }
724
Garfield Tan4cc839f2020-01-24 11:26:14 -0800725 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700726 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600727 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800728 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600729 std::vector<uint8_t> hmac;
730 status_t result = parcel->readByteVector(&hmac);
731 if (result != OK || hmac.size() != 32) {
732 return BAD_VALUE;
733 }
734 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700735 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100736 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700737 mFlags = parcel->readInt32();
738 mEdgeFlags = parcel->readInt32();
739 mMetaState = parcel->readInt32();
740 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800741 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700742
743 result = android::readFromParcel(mTransform, *parcel);
744 if (result != OK) {
745 return result;
746 }
Jeff Brown5912f952013-07-01 19:10:31 -0700747 mXPrecision = parcel->readFloat();
748 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700749 mRawXCursorPosition = parcel->readFloat();
750 mRawYCursorPosition = parcel->readFloat();
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700751
752 result = android::readFromParcel(mRawTransform, *parcel);
753 if (result != OK) {
754 return result;
755 }
Jeff Brown5912f952013-07-01 19:10:31 -0700756 mDownTime = parcel->readInt64();
757
758 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800759 mPointerProperties.reserve(pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700760 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500761 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700762 mSamplePointerCoords.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800763 mSamplePointerCoords.reserve(sampleCount * pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700764
765 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800766 mPointerProperties.push_back({});
767 PointerProperties& properties = mPointerProperties.back();
Jeff Brown5912f952013-07-01 19:10:31 -0700768 properties.id = parcel->readInt32();
769 properties.toolType = parcel->readInt32();
770 }
771
Dan Austinc94fc452015-09-22 14:22:41 -0700772 while (sampleCount > 0) {
773 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500774 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700775 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800776 mSamplePointerCoords.push_back({});
777 status_t status = mSamplePointerCoords.back().readFromParcel(parcel);
Jeff Brown5912f952013-07-01 19:10:31 -0700778 if (status) {
779 return status;
780 }
781 }
782 }
783 return OK;
784}
785
786status_t MotionEvent::writeToParcel(Parcel* parcel) const {
787 size_t pointerCount = mPointerProperties.size();
788 size_t sampleCount = mSampleEventTimes.size();
789
790 parcel->writeInt32(pointerCount);
791 parcel->writeInt32(sampleCount);
792
Garfield Tan4cc839f2020-01-24 11:26:14 -0800793 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700794 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600795 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800796 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600797 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
798 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700799 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100800 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700801 parcel->writeInt32(mFlags);
802 parcel->writeInt32(mEdgeFlags);
803 parcel->writeInt32(mMetaState);
804 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800805 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700806
807 status_t result = android::writeToParcel(mTransform, *parcel);
808 if (result != OK) {
809 return result;
810 }
Jeff Brown5912f952013-07-01 19:10:31 -0700811 parcel->writeFloat(mXPrecision);
812 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700813 parcel->writeFloat(mRawXCursorPosition);
814 parcel->writeFloat(mRawYCursorPosition);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700815
816 result = android::writeToParcel(mRawTransform, *parcel);
817 if (result != OK) {
818 return result;
819 }
Jeff Brown5912f952013-07-01 19:10:31 -0700820 parcel->writeInt64(mDownTime);
821
822 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800823 const PointerProperties& properties = mPointerProperties[i];
Jeff Brown5912f952013-07-01 19:10:31 -0700824 parcel->writeInt32(properties.id);
825 parcel->writeInt32(properties.toolType);
826 }
827
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800828 const PointerCoords* pc = mSamplePointerCoords.data();
Jeff Brown5912f952013-07-01 19:10:31 -0700829 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500830 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700831 for (size_t i = 0; i < pointerCount; i++) {
832 status_t status = (pc++)->writeToParcel(parcel);
833 if (status) {
834 return status;
835 }
836 }
837 }
838 return OK;
839}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800840#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700841
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600842bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700843 if (isFromSource(source, AINPUT_SOURCE_CLASS_POINTER)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700844 // Specifically excludes HOVER_MOVE and SCROLL.
845 switch (action & AMOTION_EVENT_ACTION_MASK) {
846 case AMOTION_EVENT_ACTION_DOWN:
847 case AMOTION_EVENT_ACTION_MOVE:
848 case AMOTION_EVENT_ACTION_UP:
849 case AMOTION_EVENT_ACTION_POINTER_DOWN:
850 case AMOTION_EVENT_ACTION_POINTER_UP:
851 case AMOTION_EVENT_ACTION_CANCEL:
852 case AMOTION_EVENT_ACTION_OUTSIDE:
853 return true;
854 }
855 }
856 return false;
857}
858
Michael Wright872db4f2014-04-22 15:03:51 -0700859const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700860 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700861}
862
863int32_t MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700864 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700865}
866
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500867std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700868 // Convert MotionEvent action to string
869 switch (action & AMOTION_EVENT_ACTION_MASK) {
870 case AMOTION_EVENT_ACTION_DOWN:
871 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700872 case AMOTION_EVENT_ACTION_UP:
873 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500874 case AMOTION_EVENT_ACTION_MOVE:
875 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700876 case AMOTION_EVENT_ACTION_CANCEL:
877 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500878 case AMOTION_EVENT_ACTION_OUTSIDE:
879 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700880 case AMOTION_EVENT_ACTION_POINTER_DOWN:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000881 return StringPrintf("POINTER_DOWN(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700882 case AMOTION_EVENT_ACTION_POINTER_UP:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000883 return StringPrintf("POINTER_UP(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500884 case AMOTION_EVENT_ACTION_HOVER_MOVE:
885 return "HOVER_MOVE";
886 case AMOTION_EVENT_ACTION_SCROLL:
887 return "SCROLL";
888 case AMOTION_EVENT_ACTION_HOVER_ENTER:
889 return "HOVER_ENTER";
890 case AMOTION_EVENT_ACTION_HOVER_EXIT:
891 return "HOVER_EXIT";
892 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
893 return "BUTTON_PRESS";
894 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
895 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700896 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500897 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700898}
899
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700900// Apply the given transformation to the point without checking whether the entire transform
901// should be disregarded altogether for the provided source.
902static inline vec2 calculateTransformedXYUnchecked(uint32_t source, const ui::Transform& transform,
903 const vec2& xy) {
904 return shouldDisregardOffset(source) ? transformWithoutTranslation(transform, xy)
905 : transform.transform(xy);
906}
907
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700908vec2 MotionEvent::calculateTransformedXY(uint32_t source, const ui::Transform& transform,
909 const vec2& xy) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700910 if (shouldDisregardTransformation(source)) {
911 return xy;
912 }
913 return calculateTransformedXYUnchecked(source, transform, xy);
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700914}
915
Prabir Pradhan8e6ce222022-02-24 09:08:54 -0800916// Keep in sync with calculateTransformedCoords.
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700917float MotionEvent::calculateTransformedAxisValue(int32_t axis, uint32_t source,
918 const ui::Transform& transform,
919 const PointerCoords& coords) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700920 if (shouldDisregardTransformation(source)) {
921 return coords.getAxisValue(axis);
922 }
923
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700924 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700925 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700926 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
927 return xy[axis];
928 }
929
930 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
931 const vec2 relativeXy =
932 transformWithoutTranslation(transform,
933 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
934 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
935 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
936 }
937
938 if (axis == AMOTION_EVENT_AXIS_ORIENTATION) {
939 return transformAngle(transform, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION));
940 }
941
942 return coords.getAxisValue(axis);
943}
944
Prabir Pradhan8e6ce222022-02-24 09:08:54 -0800945// Keep in sync with calculateTransformedAxisValue. This is an optimization of
946// calculateTransformedAxisValue for all PointerCoords axes.
947PointerCoords MotionEvent::calculateTransformedCoords(uint32_t source,
948 const ui::Transform& transform,
949 const PointerCoords& coords) {
950 if (shouldDisregardTransformation(source)) {
951 return coords;
952 }
953 PointerCoords out = coords;
954
955 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
956 out.setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
957 out.setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
958
959 const vec2 relativeXy =
960 transformWithoutTranslation(transform,
961 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
962 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
963 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
964 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
965
966 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
967 transformAngle(transform,
968 coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)));
969
970 return out;
971}
972
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000973std::ostream& operator<<(std::ostream& out, const MotionEvent& event) {
974 out << "MotionEvent { action=" << MotionEvent::actionToString(event.getAction());
975 if (event.getActionButton() != 0) {
976 out << ", actionButton=" << std::to_string(event.getActionButton());
977 }
978 const size_t pointerCount = event.getPointerCount();
hupeng3aa5a51a2022-09-02 16:00:18 +0800979 LOG_ALWAYS_FATAL_IF(pointerCount > MAX_POINTERS, "Too many pointers : pointerCount = %zu",
980 pointerCount);
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000981 for (size_t i = 0; i < pointerCount; i++) {
982 out << ", id[" << i << "]=" << event.getPointerId(i);
983 float x = event.getX(i);
984 float y = event.getY(i);
985 if (x != 0 || y != 0) {
986 out << ", x[" << i << "]=" << x;
987 out << ", y[" << i << "]=" << y;
988 }
989 int toolType = event.getToolType(i);
990 if (toolType != AMOTION_EVENT_TOOL_TYPE_FINGER) {
991 out << ", toolType[" << i << "]=" << toolType;
992 }
993 }
994 if (event.getButtonState() != 0) {
995 out << ", buttonState=" << event.getButtonState();
996 }
997 if (event.getClassification() != MotionClassification::NONE) {
998 out << ", classification=" << motionClassificationToString(event.getClassification());
999 }
1000 if (event.getMetaState() != 0) {
1001 out << ", metaState=" << event.getMetaState();
1002 }
1003 if (event.getEdgeFlags() != 0) {
1004 out << ", edgeFlags=" << event.getEdgeFlags();
1005 }
1006 if (pointerCount != 1) {
1007 out << ", pointerCount=" << pointerCount;
1008 }
1009 if (event.getHistorySize() != 0) {
1010 out << ", historySize=" << event.getHistorySize();
1011 }
1012 out << ", eventTime=" << event.getEventTime();
1013 out << ", downTime=" << event.getDownTime();
1014 out << ", deviceId=" << event.getDeviceId();
1015 out << ", source=" << inputEventSourceToString(event.getSource());
1016 out << ", displayId=" << event.getDisplayId();
1017 out << ", eventId=" << event.getId();
1018 out << "}";
1019 return out;
1020}
1021
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001022// --- FocusEvent ---
1023
Antonio Kantek3cfec7b2021-11-05 18:26:17 -07001024void FocusEvent::initialize(int32_t id, bool hasFocus) {
Garfield Tan4cc839f2020-01-24 11:26:14 -08001025 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -06001026 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001027 mHasFocus = hasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001028}
1029
1030void FocusEvent::initialize(const FocusEvent& from) {
1031 InputEvent::initialize(from);
1032 mHasFocus = from.mHasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001033}
Jeff Brown5912f952013-07-01 19:10:31 -07001034
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001035// --- CaptureEvent ---
1036
1037void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) {
1038 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1039 ADISPLAY_ID_NONE, INVALID_HMAC);
1040 mPointerCaptureEnabled = pointerCaptureEnabled;
1041}
1042
1043void CaptureEvent::initialize(const CaptureEvent& from) {
1044 InputEvent::initialize(from);
1045 mPointerCaptureEnabled = from.mPointerCaptureEnabled;
1046}
1047
arthurhung7632c332020-12-30 16:58:01 +08001048// --- DragEvent ---
1049
1050void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) {
1051 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1052 ADISPLAY_ID_NONE, INVALID_HMAC);
1053 mIsExiting = isExiting;
1054 mX = x;
1055 mY = y;
1056}
1057
1058void DragEvent::initialize(const DragEvent& from) {
1059 InputEvent::initialize(from);
1060 mIsExiting = from.mIsExiting;
1061 mX = from.mX;
1062 mY = from.mY;
1063}
1064
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001065// --- TouchModeEvent ---
1066
1067void TouchModeEvent::initialize(int32_t id, bool isInTouchMode) {
1068 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1069 ADISPLAY_ID_NONE, INVALID_HMAC);
1070 mIsInTouchMode = isInTouchMode;
1071}
1072
1073void TouchModeEvent::initialize(const TouchModeEvent& from) {
1074 InputEvent::initialize(from);
1075 mIsInTouchMode = from.mIsInTouchMode;
1076}
1077
Jeff Brown5912f952013-07-01 19:10:31 -07001078// --- PooledInputEventFactory ---
1079
1080PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
1081 mMaxPoolSize(maxPoolSize) {
1082}
1083
1084PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -07001085}
1086
1087KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001088 if (mKeyEventPool.empty()) {
1089 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001090 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001091 KeyEvent* event = mKeyEventPool.front().release();
1092 mKeyEventPool.pop();
1093 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001094}
1095
1096MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001097 if (mMotionEventPool.empty()) {
1098 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001099 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001100 MotionEvent* event = mMotionEventPool.front().release();
1101 mMotionEventPool.pop();
1102 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001103}
1104
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001105FocusEvent* PooledInputEventFactory::createFocusEvent() {
1106 if (mFocusEventPool.empty()) {
1107 return new FocusEvent();
1108 }
1109 FocusEvent* event = mFocusEventPool.front().release();
1110 mFocusEventPool.pop();
1111 return event;
1112}
1113
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001114CaptureEvent* PooledInputEventFactory::createCaptureEvent() {
1115 if (mCaptureEventPool.empty()) {
1116 return new CaptureEvent();
1117 }
1118 CaptureEvent* event = mCaptureEventPool.front().release();
1119 mCaptureEventPool.pop();
1120 return event;
1121}
1122
arthurhung7632c332020-12-30 16:58:01 +08001123DragEvent* PooledInputEventFactory::createDragEvent() {
1124 if (mDragEventPool.empty()) {
1125 return new DragEvent();
1126 }
1127 DragEvent* event = mDragEventPool.front().release();
1128 mDragEventPool.pop();
1129 return event;
1130}
1131
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001132TouchModeEvent* PooledInputEventFactory::createTouchModeEvent() {
1133 if (mTouchModeEventPool.empty()) {
1134 return new TouchModeEvent();
1135 }
1136 TouchModeEvent* event = mTouchModeEventPool.front().release();
1137 mTouchModeEventPool.pop();
1138 return event;
1139}
1140
Jeff Brown5912f952013-07-01 19:10:31 -07001141void PooledInputEventFactory::recycle(InputEvent* event) {
1142 switch (event->getType()) {
1143 case AINPUT_EVENT_TYPE_KEY:
1144 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001145 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -07001146 return;
1147 }
1148 break;
1149 case AINPUT_EVENT_TYPE_MOTION:
1150 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001151 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -07001152 return;
1153 }
1154 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001155 case AINPUT_EVENT_TYPE_FOCUS:
1156 if (mFocusEventPool.size() < mMaxPoolSize) {
1157 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
1158 return;
1159 }
1160 break;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001161 case AINPUT_EVENT_TYPE_CAPTURE:
1162 if (mCaptureEventPool.size() < mMaxPoolSize) {
1163 mCaptureEventPool.push(
1164 std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event)));
1165 return;
1166 }
1167 break;
arthurhung7632c332020-12-30 16:58:01 +08001168 case AINPUT_EVENT_TYPE_DRAG:
1169 if (mDragEventPool.size() < mMaxPoolSize) {
1170 mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event)));
1171 return;
1172 }
1173 break;
Antonio Kantekeb4a30c2021-09-28 17:49:49 -07001174 case AINPUT_EVENT_TYPE_TOUCH_MODE:
1175 if (mTouchModeEventPool.size() < mMaxPoolSize) {
1176 mTouchModeEventPool.push(
1177 std::unique_ptr<TouchModeEvent>(static_cast<TouchModeEvent*>(event)));
1178 return;
1179 }
1180 break;
Jeff Brown5912f952013-07-01 19:10:31 -07001181 }
1182 delete event;
1183}
1184
1185} // namespace android