blob: 579b28e58884acb696a17b90890c5b34a1d5d219 [file] [log] [blame]
Jeff Brown5912f952013-07-01 19:10:31 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Input"
18//#define LOG_NDEBUG 0
19
chaviw09c8d2d2020-08-24 15:48:26 -070020#include <attestation/HmacKeyManager.h>
Garfield Tan84b087e2020-01-23 10:49:05 -080021#include <cutils/compiler.h>
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050022#include <inttypes.h>
Garfield Tan84b087e2020-01-23 10:49:05 -080023#include <string.h>
Jeff Brown5912f952013-07-01 19:10:31 -070024
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +000025#include <android-base/logging.h>
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050026#include <android-base/stringprintf.h>
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +000027#include <cutils/compiler.h>
chaviw98318de2021-05-19 16:45:23 -050028#include <gui/constants.h>
Prabir Pradhan092f3a92021-11-25 10:53:27 -080029#include <input/DisplayViewport.h>
Jeff Brown5912f952013-07-01 19:10:31 -070030#include <input/Input.h>
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080031#include <input/InputDevice.h>
Michael Wright872db4f2014-04-22 15:03:51 -070032#include <input/InputEventLabels.h>
Jeff Brown5912f952013-07-01 19:10:31 -070033
Brett Chabotfaa986c2020-11-04 17:39:36 -080034#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -070035#include <binder/Parcel.h>
Brett Chabotfaa986c2020-11-04 17:39:36 -080036#endif
Brett Chabot58208522020-09-09 13:55:24 -070037#ifdef __ANDROID__
Garfield Tan84b087e2020-01-23 10:49:05 -080038#include <sys/random.h>
Jeff Brown5912f952013-07-01 19:10:31 -070039#endif
40
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -050041using android::base::StringPrintf;
42
Jeff Brown5912f952013-07-01 19:10:31 -070043namespace android {
44
Prabir Pradhan6b384612021-05-14 16:56:25 -070045namespace {
46
47float transformAngle(const ui::Transform& transform, float angleRadians) {
48 // Construct and transform a vector oriented at the specified clockwise angle from vertical.
49 // Coordinate system: down is increasing Y, right is increasing X.
50 float x = sinf(angleRadians);
51 float y = -cosf(angleRadians);
52 vec2 transformedPoint = transform.transform(x, y);
53
54 // Determine how the origin is transformed by the matrix so that we
55 // can transform orientation vectors.
56 const vec2 origin = transform.transform(0, 0);
57
58 transformedPoint.x -= origin.x;
59 transformedPoint.y -= origin.y;
60
61 // Derive the transformed vector's clockwise angle from vertical.
Prabir Pradhand2b02672021-10-19 11:24:45 -070062 // The return value of atan2f is in range [-pi, pi] which conforms to the orientation API.
63 return atan2f(transformedPoint.x, -transformedPoint.y);
Prabir Pradhan6b384612021-05-14 16:56:25 -070064}
65
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070066bool shouldDisregardTransformation(uint32_t source) {
Prabir Pradhan258e2b92022-06-24 18:37:04 +000067 // Do not apply any transformations to axes from joysticks, touchpads, or relative mice.
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070068 return isFromSource(source, AINPUT_SOURCE_CLASS_JOYSTICK) ||
Prabir Pradhan258e2b92022-06-24 18:37:04 +000069 isFromSource(source, AINPUT_SOURCE_CLASS_POSITION) ||
70 isFromSource(source, AINPUT_SOURCE_MOUSE_RELATIVE);
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070071}
72
73bool shouldDisregardOffset(uint32_t source) {
Prabir Pradhan9f388812021-05-13 16:54:53 -070074 // Pointer events are the only type of events that refer to absolute coordinates on the display,
75 // so we should apply the entire window transform. For other types of events, we should make
76 // sure to not apply the window translation/offset.
Prabir Pradhan7e1ee562021-10-26 10:19:49 -070077 return !isFromSource(source, AINPUT_SOURCE_CLASS_POINTER);
Prabir Pradhan9f388812021-05-13 16:54:53 -070078}
79
Prabir Pradhan6b384612021-05-14 16:56:25 -070080} // namespace
81
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -080082const char* motionClassificationToString(MotionClassification classification) {
83 switch (classification) {
84 case MotionClassification::NONE:
85 return "NONE";
86 case MotionClassification::AMBIGUOUS_GESTURE:
87 return "AMBIGUOUS_GESTURE";
88 case MotionClassification::DEEP_PRESS:
89 return "DEEP_PRESS";
Harry Cutts2800fb02022-09-15 13:49:23 +000090 case MotionClassification::TWO_FINGER_SWIPE:
91 return "TWO_FINGER_SWIPE";
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -080092 }
93}
94
Siarhei Vishniakoud5fe5182022-07-20 23:28:40 +000095const char* motionToolTypeToString(int32_t toolType) {
96 switch (toolType) {
97 case AMOTION_EVENT_TOOL_TYPE_UNKNOWN:
98 return "UNKNOWN";
99 case AMOTION_EVENT_TOOL_TYPE_FINGER:
100 return "FINGER";
101 case AMOTION_EVENT_TOOL_TYPE_STYLUS:
102 return "STYLUS";
103 case AMOTION_EVENT_TOOL_TYPE_MOUSE:
104 return "MOUSE";
105 case AMOTION_EVENT_TOOL_TYPE_ERASER:
106 return "ERASER";
107 case AMOTION_EVENT_TOOL_TYPE_PALM:
108 return "PALM";
109 default:
110 return "INVALID";
111 }
112}
113
Garfield Tan84b087e2020-01-23 10:49:05 -0800114// --- IdGenerator ---
115IdGenerator::IdGenerator(Source source) : mSource(source) {}
116
117int32_t IdGenerator::nextId() const {
118 constexpr uint32_t SEQUENCE_NUMBER_MASK = ~SOURCE_MASK;
119 int32_t id = 0;
120
121// Avoid building against syscall getrandom(2) on host, which will fail build on Mac. Host doesn't
122// use sequence number so just always return mSource.
123#ifdef __ANDROID__
124 constexpr size_t BUF_LEN = sizeof(id);
125 size_t totalBytes = 0;
126 while (totalBytes < BUF_LEN) {
127 ssize_t bytes = TEMP_FAILURE_RETRY(getrandom(&id, BUF_LEN, GRND_NONBLOCK));
128 if (CC_UNLIKELY(bytes < 0)) {
129 ALOGW("Failed to fill in random number for sequence number: %s.", strerror(errno));
130 id = 0;
131 break;
132 }
133 totalBytes += bytes;
134 }
135#endif // __ANDROID__
136
137 return (id & SEQUENCE_NUMBER_MASK) | static_cast<int32_t>(mSource);
138}
139
Jeff Brown5912f952013-07-01 19:10:31 -0700140// --- InputEvent ---
141
Prabir Pradhande69f8a2021-11-18 16:40:34 +0000142vec2 transformWithoutTranslation(const ui::Transform& transform, const vec2& xy) {
143 const vec2 transformedXy = transform.transform(xy);
144 const vec2 transformedOrigin = transform.transform(0, 0);
145 return transformedXy - transformedOrigin;
146}
147
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800148const char* inputEventTypeToString(int32_t type) {
149 switch (type) {
150 case AINPUT_EVENT_TYPE_KEY: {
151 return "KEY";
152 }
153 case AINPUT_EVENT_TYPE_MOTION: {
154 return "MOTION";
155 }
156 case AINPUT_EVENT_TYPE_FOCUS: {
157 return "FOCUS";
158 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800159 case AINPUT_EVENT_TYPE_CAPTURE: {
160 return "CAPTURE";
161 }
arthurhung7632c332020-12-30 16:58:01 +0800162 case AINPUT_EVENT_TYPE_DRAG: {
163 return "DRAG";
164 }
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700165 case AINPUT_EVENT_TYPE_TOUCH_MODE: {
166 return "TOUCH_MODE";
167 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800168 }
169 return "UNKNOWN";
170}
171
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800172std::string inputEventSourceToString(int32_t source) {
173 if (source == AINPUT_SOURCE_UNKNOWN) {
174 return "UNKNOWN";
175 }
176 if (source == static_cast<int32_t>(AINPUT_SOURCE_ANY)) {
177 return "ANY";
178 }
179 static const std::map<int32_t, const char*> SOURCES{
180 {AINPUT_SOURCE_KEYBOARD, "KEYBOARD"},
181 {AINPUT_SOURCE_DPAD, "DPAD"},
182 {AINPUT_SOURCE_GAMEPAD, "GAMEPAD"},
183 {AINPUT_SOURCE_TOUCHSCREEN, "TOUCHSCREEN"},
184 {AINPUT_SOURCE_MOUSE, "MOUSE"},
185 {AINPUT_SOURCE_STYLUS, "STYLUS"},
186 {AINPUT_SOURCE_BLUETOOTH_STYLUS, "BLUETOOTH_STYLUS"},
187 {AINPUT_SOURCE_TRACKBALL, "TRACKBALL"},
188 {AINPUT_SOURCE_MOUSE_RELATIVE, "MOUSE_RELATIVE"},
189 {AINPUT_SOURCE_TOUCHPAD, "TOUCHPAD"},
190 {AINPUT_SOURCE_TOUCH_NAVIGATION, "TOUCH_NAVIGATION"},
191 {AINPUT_SOURCE_JOYSTICK, "JOYSTICK"},
192 {AINPUT_SOURCE_HDMI, "HDMI"},
193 {AINPUT_SOURCE_SENSOR, "SENSOR"},
194 {AINPUT_SOURCE_ROTARY_ENCODER, "ROTARY_ENCODER"},
195 };
196 std::string result;
197 for (const auto& [source_entry, str] : SOURCES) {
198 if ((source & source_entry) == source_entry) {
199 if (!result.empty()) {
200 result += " | ";
201 }
202 result += str;
203 }
204 }
205 if (result.empty()) {
206 result = StringPrintf("0x%08x", source);
207 }
208 return result;
209}
210
211bool isFromSource(uint32_t source, uint32_t test) {
212 return (source & test) == test;
213}
214
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800215VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
216 return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
217 event.getSource(), event.getDisplayId()},
218 event.getAction(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800219 event.getFlags() & VERIFIED_KEY_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800220 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800221 event.getKeyCode(),
222 event.getScanCode(),
223 event.getMetaState(),
224 event.getRepeatCount()};
225}
226
227VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) {
228 return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(),
229 event.getSource(), event.getDisplayId()},
230 event.getRawX(0),
231 event.getRawY(0),
232 event.getActionMasked(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800233 event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -0800234 event.getDownTime(),
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800235 event.getMetaState(),
236 event.getButtonState()};
237}
238
Garfield Tan4cc839f2020-01-24 11:26:14 -0800239void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600240 std::array<uint8_t, 32> hmac) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800241 mId = id;
Jeff Brown5912f952013-07-01 19:10:31 -0700242 mDeviceId = deviceId;
243 mSource = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100244 mDisplayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600245 mHmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700246}
247
248void InputEvent::initialize(const InputEvent& from) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800249 mId = from.mId;
Jeff Brown5912f952013-07-01 19:10:31 -0700250 mDeviceId = from.mDeviceId;
251 mSource = from.mSource;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100252 mDisplayId = from.mDisplayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600253 mHmac = from.mHmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700254}
255
Garfield Tan4cc839f2020-01-24 11:26:14 -0800256int32_t InputEvent::nextId() {
257 static IdGenerator idGen(IdGenerator::Source::OTHER);
258 return idGen.nextId();
259}
260
Jeff Brown5912f952013-07-01 19:10:31 -0700261// --- KeyEvent ---
262
Michael Wright872db4f2014-04-22 15:03:51 -0700263const char* KeyEvent::getLabel(int32_t keyCode) {
Chris Ye4958d062020-08-20 13:21:10 -0700264 return InputEventLookup::getLabelByKeyCode(keyCode);
Jeff Brown5912f952013-07-01 19:10:31 -0700265}
266
Michael Wright872db4f2014-04-22 15:03:51 -0700267int32_t KeyEvent::getKeyCodeFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700268 return InputEventLookup::getKeyCodeByLabel(label);
Jeff Brown5912f952013-07-01 19:10:31 -0700269}
270
Garfield Tan4cc839f2020-01-24 11:26:14 -0800271void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600272 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
273 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
274 nsecs_t downTime, nsecs_t eventTime) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800275 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700276 mAction = action;
277 mFlags = flags;
278 mKeyCode = keyCode;
279 mScanCode = scanCode;
280 mMetaState = metaState;
281 mRepeatCount = repeatCount;
282 mDownTime = downTime;
283 mEventTime = eventTime;
284}
285
286void KeyEvent::initialize(const KeyEvent& from) {
287 InputEvent::initialize(from);
288 mAction = from.mAction;
289 mFlags = from.mFlags;
290 mKeyCode = from.mKeyCode;
291 mScanCode = from.mScanCode;
292 mMetaState = from.mMetaState;
293 mRepeatCount = from.mRepeatCount;
294 mDownTime = from.mDownTime;
295 mEventTime = from.mEventTime;
296}
297
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700298const char* KeyEvent::actionToString(int32_t action) {
299 // Convert KeyEvent action to string
300 switch (action) {
301 case AKEY_EVENT_ACTION_DOWN:
302 return "DOWN";
303 case AKEY_EVENT_ACTION_UP:
304 return "UP";
305 case AKEY_EVENT_ACTION_MULTIPLE:
306 return "MULTIPLE";
307 }
308 return "UNKNOWN";
309}
Jeff Brown5912f952013-07-01 19:10:31 -0700310
311// --- PointerCoords ---
312
313float PointerCoords::getAxisValue(int32_t axis) const {
Michael Wright38dcdff2014-03-19 12:06:10 -0700314 if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){
Jeff Brown5912f952013-07-01 19:10:31 -0700315 return 0;
316 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700317 return values[BitSet64::getIndexOfBit(bits, axis)];
Jeff Brown5912f952013-07-01 19:10:31 -0700318}
319
320status_t PointerCoords::setAxisValue(int32_t axis, float value) {
321 if (axis < 0 || axis > 63) {
322 return NAME_NOT_FOUND;
323 }
324
Michael Wright38dcdff2014-03-19 12:06:10 -0700325 uint32_t index = BitSet64::getIndexOfBit(bits, axis);
326 if (!BitSet64::hasBit(bits, axis)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700327 if (value == 0) {
328 return OK; // axes with value 0 do not need to be stored
329 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700330
331 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700332 if (count >= MAX_AXES) {
333 tooManyAxes(axis);
334 return NO_MEMORY;
335 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700336 BitSet64::markBit(bits, axis);
Jeff Brown5912f952013-07-01 19:10:31 -0700337 for (uint32_t i = count; i > index; i--) {
338 values[i] = values[i - 1];
339 }
340 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700341
Jeff Brown5912f952013-07-01 19:10:31 -0700342 values[index] = value;
343 return OK;
344}
345
346static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) {
347 float value = c.getAxisValue(axis);
348 if (value != 0) {
349 c.setAxisValue(axis, value * scaleFactor);
350 }
351}
352
Robert Carre07e1032018-11-26 12:55:53 -0800353void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) {
Jeff Brown5912f952013-07-01 19:10:31 -0700354 // No need to scale pressure or size since they are normalized.
355 // No need to scale orientation since it is meaningless to do so.
Robert Carre07e1032018-11-26 12:55:53 -0800356
357 // If there is a global scale factor, it is included in the windowX/YScale
358 // so we don't need to apply it twice to the X/Y axes.
359 // However we don't want to apply any windowXYScale not included in the global scale
360 // to the TOUCH_MAJOR/MINOR coordinates.
361 scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale);
362 scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale);
363 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor);
364 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor);
365 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor);
366 scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor);
Prabir Pradhanc6523582021-05-14 18:02:55 -0700367 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_X, windowXScale);
368 scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_Y, windowYScale);
Robert Carre07e1032018-11-26 12:55:53 -0800369}
370
Brett Chabotfaa986c2020-11-04 17:39:36 -0800371#ifdef __linux__
Jeff Brown5912f952013-07-01 19:10:31 -0700372status_t PointerCoords::readFromParcel(Parcel* parcel) {
373 bits = parcel->readInt64();
374
Michael Wright38dcdff2014-03-19 12:06:10 -0700375 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700376 if (count > MAX_AXES) {
377 return BAD_VALUE;
378 }
379
380 for (uint32_t i = 0; i < count; i++) {
381 values[i] = parcel->readFloat();
382 }
383 return OK;
384}
385
386status_t PointerCoords::writeToParcel(Parcel* parcel) const {
387 parcel->writeInt64(bits);
388
Michael Wright38dcdff2014-03-19 12:06:10 -0700389 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700390 for (uint32_t i = 0; i < count; i++) {
391 parcel->writeFloat(values[i]);
392 }
393 return OK;
394}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800395#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700396
397void PointerCoords::tooManyAxes(int axis) {
398 ALOGW("Could not set value for axis %d because the PointerCoords structure is full and "
399 "cannot contain more than %d axis values.", axis, int(MAX_AXES));
400}
401
402bool PointerCoords::operator==(const PointerCoords& other) const {
403 if (bits != other.bits) {
404 return false;
405 }
Michael Wright38dcdff2014-03-19 12:06:10 -0700406 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700407 for (uint32_t i = 0; i < count; i++) {
408 if (values[i] != other.values[i]) {
409 return false;
410 }
411 }
412 return true;
413}
414
415void PointerCoords::copyFrom(const PointerCoords& other) {
416 bits = other.bits;
Michael Wright38dcdff2014-03-19 12:06:10 -0700417 uint32_t count = BitSet64::count(bits);
Jeff Brown5912f952013-07-01 19:10:31 -0700418 for (uint32_t i = 0; i < count; i++) {
419 values[i] = other.values[i];
420 }
421}
422
chaviwc01e1372020-07-01 12:37:31 -0700423void PointerCoords::transform(const ui::Transform& transform) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700424 const vec2 xy = transform.transform(getXYValue());
425 setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
426 setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
427
Prabir Pradhanc6523582021-05-14 18:02:55 -0700428 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_X) ||
429 BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_Y)) {
430 const ui::Transform rotation(transform.getOrientation());
431 const vec2 relativeXy = rotation.transform(getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
432 getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
433 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
434 setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
435 }
436
Prabir Pradhan6b384612021-05-14 16:56:25 -0700437 if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) {
438 const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION);
439 setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val));
440 }
chaviwc01e1372020-07-01 12:37:31 -0700441}
Jeff Brown5912f952013-07-01 19:10:31 -0700442
443// --- PointerProperties ---
444
445bool PointerProperties::operator==(const PointerProperties& other) const {
446 return id == other.id
447 && toolType == other.toolType;
448}
449
450void PointerProperties::copyFrom(const PointerProperties& other) {
451 id = other.id;
452 toolType = other.toolType;
453}
454
455
456// --- MotionEvent ---
457
Garfield Tan4cc839f2020-01-24 11:26:14 -0800458void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600459 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
460 int32_t flags, int32_t edgeFlags, int32_t metaState,
chaviw9eaa22c2020-07-01 16:21:27 -0700461 int32_t buttonState, MotionClassification classification,
462 const ui::Transform& transform, float xPrecision, float yPrecision,
Evan Rosky84f07f02021-04-16 10:42:42 -0700463 float rawXCursorPosition, float rawYCursorPosition,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700464 const ui::Transform& rawTransform, nsecs_t downTime, nsecs_t eventTime,
Evan Rosky09576692021-07-01 12:22:09 -0700465 size_t pointerCount, const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700466 const PointerCoords* pointerCoords) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800467 InputEvent::initialize(id, deviceId, source, displayId, hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700468 mAction = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100469 mActionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700470 mFlags = flags;
471 mEdgeFlags = edgeFlags;
472 mMetaState = metaState;
473 mButtonState = buttonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800474 mClassification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700475 mTransform = transform;
Jeff Brown5912f952013-07-01 19:10:31 -0700476 mXPrecision = xPrecision;
477 mYPrecision = yPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700478 mRawXCursorPosition = rawXCursorPosition;
479 mRawYCursorPosition = rawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700480 mRawTransform = rawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700481 mDownTime = downTime;
482 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800483 mPointerProperties.insert(mPointerProperties.end(), &pointerProperties[0],
484 &pointerProperties[pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700485 mSampleEventTimes.clear();
486 mSamplePointerCoords.clear();
487 addSample(eventTime, pointerCoords);
488}
489
490void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
Garfield Tan4cc839f2020-01-24 11:26:14 -0800491 InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId,
492 other->mHmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700493 mAction = other->mAction;
Michael Wright7b159c92015-05-14 14:48:03 +0100494 mActionButton = other->mActionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700495 mFlags = other->mFlags;
496 mEdgeFlags = other->mEdgeFlags;
497 mMetaState = other->mMetaState;
498 mButtonState = other->mButtonState;
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800499 mClassification = other->mClassification;
chaviw9eaa22c2020-07-01 16:21:27 -0700500 mTransform = other->mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700501 mXPrecision = other->mXPrecision;
502 mYPrecision = other->mYPrecision;
Garfield Tan937bb832019-07-25 17:48:31 -0700503 mRawXCursorPosition = other->mRawXCursorPosition;
504 mRawYCursorPosition = other->mRawYCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700505 mRawTransform = other->mRawTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700506 mDownTime = other->mDownTime;
507 mPointerProperties = other->mPointerProperties;
508
509 if (keepHistory) {
510 mSampleEventTimes = other->mSampleEventTimes;
511 mSamplePointerCoords = other->mSamplePointerCoords;
512 } else {
513 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500514 mSampleEventTimes.push_back(other->getEventTime());
Jeff Brown5912f952013-07-01 19:10:31 -0700515 mSamplePointerCoords.clear();
516 size_t pointerCount = other->getPointerCount();
517 size_t historySize = other->getHistorySize();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800518 mSamplePointerCoords
519 .insert(mSamplePointerCoords.end(),
520 &other->mSamplePointerCoords[historySize * pointerCount],
521 &other->mSamplePointerCoords[historySize * pointerCount + pointerCount]);
Jeff Brown5912f952013-07-01 19:10:31 -0700522 }
523}
524
525void MotionEvent::addSample(
526 int64_t eventTime,
527 const PointerCoords* pointerCoords) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500528 mSampleEventTimes.push_back(eventTime);
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800529 mSamplePointerCoords.insert(mSamplePointerCoords.end(), &pointerCoords[0],
530 &pointerCoords[getPointerCount()]);
Jeff Brown5912f952013-07-01 19:10:31 -0700531}
532
Prabir Pradhan092f3a92021-11-25 10:53:27 -0800533int MotionEvent::getSurfaceRotation() const {
534 // The surface rotation is the rotation from the window's coordinate space to that of the
535 // display. Since the event's transform takes display space coordinates to window space, the
536 // returned surface rotation is the inverse of the rotation for the surface.
537 switch (mTransform.getOrientation()) {
538 case ui::Transform::ROT_0:
539 return DISPLAY_ORIENTATION_0;
540 case ui::Transform::ROT_90:
541 return DISPLAY_ORIENTATION_270;
542 case ui::Transform::ROT_180:
543 return DISPLAY_ORIENTATION_180;
544 case ui::Transform::ROT_270:
545 return DISPLAY_ORIENTATION_90;
546 default:
547 return -1;
548 }
549}
550
Garfield Tan00f511d2019-06-12 16:55:40 -0700551float MotionEvent::getXCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700552 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
553 return vals.x;
Garfield Tan00f511d2019-06-12 16:55:40 -0700554}
555
556float MotionEvent::getYCursorPosition() const {
chaviw9eaa22c2020-07-01 16:21:27 -0700557 vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition());
558 return vals.y;
Garfield Tan00f511d2019-06-12 16:55:40 -0700559}
560
Garfield Tan937bb832019-07-25 17:48:31 -0700561void MotionEvent::setCursorPosition(float x, float y) {
chaviw9eaa22c2020-07-01 16:21:27 -0700562 ui::Transform inverse = mTransform.inverse();
563 vec2 vals = inverse.transform(x, y);
564 mRawXCursorPosition = vals.x;
565 mRawYCursorPosition = vals.y;
Garfield Tan937bb832019-07-25 17:48:31 -0700566}
567
Jeff Brown5912f952013-07-01 19:10:31 -0700568const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000569 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
570 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
571 }
572 const size_t position = getHistorySize() * getPointerCount() + pointerIndex;
573 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
574 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
575 }
576 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700577}
578
579float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const {
Evan Rosky84f07f02021-04-16 10:42:42 -0700580 return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700581}
582
583float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const {
chaviw9eaa22c2020-07-01 16:21:27 -0700584 return getHistoricalAxisValue(axis, pointerIndex, getHistorySize());
Jeff Brown5912f952013-07-01 19:10:31 -0700585}
586
587const PointerCoords* MotionEvent::getHistoricalRawPointerCoords(
588 size_t pointerIndex, size_t historicalIndex) const {
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000589 if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) {
590 LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this;
591 }
592 if (CC_UNLIKELY(historicalIndex < 0 || historicalIndex > getHistorySize())) {
593 LOG(FATAL) << __func__ << ": Invalid historical index " << historicalIndex << " for "
594 << *this;
595 }
596 const size_t position = historicalIndex * getPointerCount() + pointerIndex;
597 if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) {
598 LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this;
599 }
600 return &mSamplePointerCoords[position];
Jeff Brown5912f952013-07-01 19:10:31 -0700601}
602
603float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan6b384612021-05-14 16:56:25 -0700604 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700605 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
606 return calculateTransformedAxisValue(axis, mSource, mRawTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700607}
608
609float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
Prabir Pradhan9f388812021-05-13 16:54:53 -0700610 size_t historicalIndex) const {
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700611 const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex);
612 return calculateTransformedAxisValue(axis, mSource, mTransform, coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700613}
614
615ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const {
616 size_t pointerCount = mPointerProperties.size();
617 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800618 if (mPointerProperties[i].id == pointerId) {
Jeff Brown5912f952013-07-01 19:10:31 -0700619 return i;
620 }
621 }
622 return -1;
623}
624
625void MotionEvent::offsetLocation(float xOffset, float yOffset) {
chaviw9eaa22c2020-07-01 16:21:27 -0700626 float currXOffset = mTransform.tx();
627 float currYOffset = mTransform.ty();
628 mTransform.set(currXOffset + xOffset, currYOffset + yOffset);
Jeff Brown5912f952013-07-01 19:10:31 -0700629}
630
Robert Carre07e1032018-11-26 12:55:53 -0800631void MotionEvent::scale(float globalScaleFactor) {
chaviw9eaa22c2020-07-01 16:21:27 -0700632 mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700633 mRawTransform.set(mRawTransform.tx() * globalScaleFactor,
634 mRawTransform.ty() * globalScaleFactor);
Robert Carre07e1032018-11-26 12:55:53 -0800635 mXPrecision *= globalScaleFactor;
636 mYPrecision *= globalScaleFactor;
Jeff Brown5912f952013-07-01 19:10:31 -0700637
638 size_t numSamples = mSamplePointerCoords.size();
639 for (size_t i = 0; i < numSamples; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800640 mSamplePointerCoords[i].scale(globalScaleFactor, globalScaleFactor, globalScaleFactor);
Jeff Brown5912f952013-07-01 19:10:31 -0700641 }
642}
643
chaviw9eaa22c2020-07-01 16:21:27 -0700644void MotionEvent::transform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700645 // We want to preserve the raw axes values stored in the PointerCoords, so we just update the
646 // transform using the values passed in.
chaviw9eaa22c2020-07-01 16:21:27 -0700647 ui::Transform newTransform;
648 newTransform.set(matrix);
649 mTransform = newTransform * mTransform;
Jeff Brown5912f952013-07-01 19:10:31 -0700650}
651
Evan Roskyd4d4d802021-05-03 20:12:21 -0700652void MotionEvent::applyTransform(const std::array<float, 9>& matrix) {
Prabir Pradhan6b384612021-05-14 16:56:25 -0700653 ui::Transform transform;
654 transform.set(matrix);
Evan Roskyd4d4d802021-05-03 20:12:21 -0700655
656 // Apply the transformation to all samples.
Prabir Pradhan6b384612021-05-14 16:56:25 -0700657 std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(),
658 [&transform](PointerCoords& c) { c.transform(transform); });
Prabir Pradhan4b19bd02021-06-01 17:34:59 -0700659
660 if (mRawXCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION &&
661 mRawYCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) {
662 const vec2 cursor = transform.transform(mRawXCursorPosition, mRawYCursorPosition);
663 mRawXCursorPosition = cursor.x;
664 mRawYCursorPosition = cursor.y;
665 }
Evan Roskyd4d4d802021-05-03 20:12:21 -0700666}
667
Brett Chabotfaa986c2020-11-04 17:39:36 -0800668#ifdef __linux__
chaviw9eaa22c2020-07-01 16:21:27 -0700669static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) {
670 float dsdx, dtdx, tx, dtdy, dsdy, ty;
671 status_t status = parcel.readFloat(&dsdx);
672 status |= parcel.readFloat(&dtdx);
673 status |= parcel.readFloat(&tx);
674 status |= parcel.readFloat(&dtdy);
675 status |= parcel.readFloat(&dsdy);
676 status |= parcel.readFloat(&ty);
677
678 transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1});
679 return status;
680}
681
682static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) {
683 status_t status = parcel.writeFloat(transform.dsdx());
684 status |= parcel.writeFloat(transform.dtdx());
685 status |= parcel.writeFloat(transform.tx());
686 status |= parcel.writeFloat(transform.dtdy());
687 status |= parcel.writeFloat(transform.dsdy());
688 status |= parcel.writeFloat(transform.ty());
689 return status;
690}
691
Jeff Brown5912f952013-07-01 19:10:31 -0700692status_t MotionEvent::readFromParcel(Parcel* parcel) {
693 size_t pointerCount = parcel->readInt32();
694 size_t sampleCount = parcel->readInt32();
Flanker552a8a52015-09-07 15:28:58 +0800695 if (pointerCount == 0 || pointerCount > MAX_POINTERS ||
696 sampleCount == 0 || sampleCount > MAX_SAMPLES) {
Jeff Brown5912f952013-07-01 19:10:31 -0700697 return BAD_VALUE;
698 }
699
Garfield Tan4cc839f2020-01-24 11:26:14 -0800700 mId = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700701 mDeviceId = parcel->readInt32();
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600702 mSource = parcel->readUint32();
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800703 mDisplayId = parcel->readInt32();
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600704 std::vector<uint8_t> hmac;
705 status_t result = parcel->readByteVector(&hmac);
706 if (result != OK || hmac.size() != 32) {
707 return BAD_VALUE;
708 }
709 std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin());
Jeff Brown5912f952013-07-01 19:10:31 -0700710 mAction = parcel->readInt32();
Michael Wright7b159c92015-05-14 14:48:03 +0100711 mActionButton = parcel->readInt32();
Jeff Brown5912f952013-07-01 19:10:31 -0700712 mFlags = parcel->readInt32();
713 mEdgeFlags = parcel->readInt32();
714 mMetaState = parcel->readInt32();
715 mButtonState = parcel->readInt32();
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800716 mClassification = static_cast<MotionClassification>(parcel->readByte());
chaviw9eaa22c2020-07-01 16:21:27 -0700717
718 result = android::readFromParcel(mTransform, *parcel);
719 if (result != OK) {
720 return result;
721 }
Jeff Brown5912f952013-07-01 19:10:31 -0700722 mXPrecision = parcel->readFloat();
723 mYPrecision = parcel->readFloat();
Garfield Tan937bb832019-07-25 17:48:31 -0700724 mRawXCursorPosition = parcel->readFloat();
725 mRawYCursorPosition = parcel->readFloat();
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700726
727 result = android::readFromParcel(mRawTransform, *parcel);
728 if (result != OK) {
729 return result;
730 }
Jeff Brown5912f952013-07-01 19:10:31 -0700731 mDownTime = parcel->readInt64();
732
733 mPointerProperties.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800734 mPointerProperties.reserve(pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700735 mSampleEventTimes.clear();
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500736 mSampleEventTimes.reserve(sampleCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700737 mSamplePointerCoords.clear();
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800738 mSamplePointerCoords.reserve(sampleCount * pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700739
740 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800741 mPointerProperties.push_back({});
742 PointerProperties& properties = mPointerProperties.back();
Jeff Brown5912f952013-07-01 19:10:31 -0700743 properties.id = parcel->readInt32();
744 properties.toolType = parcel->readInt32();
745 }
746
Dan Austinc94fc452015-09-22 14:22:41 -0700747 while (sampleCount > 0) {
748 sampleCount--;
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500749 mSampleEventTimes.push_back(parcel->readInt64());
Jeff Brown5912f952013-07-01 19:10:31 -0700750 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800751 mSamplePointerCoords.push_back({});
752 status_t status = mSamplePointerCoords.back().readFromParcel(parcel);
Jeff Brown5912f952013-07-01 19:10:31 -0700753 if (status) {
754 return status;
755 }
756 }
757 }
758 return OK;
759}
760
761status_t MotionEvent::writeToParcel(Parcel* parcel) const {
762 size_t pointerCount = mPointerProperties.size();
763 size_t sampleCount = mSampleEventTimes.size();
764
765 parcel->writeInt32(pointerCount);
766 parcel->writeInt32(sampleCount);
767
Garfield Tan4cc839f2020-01-24 11:26:14 -0800768 parcel->writeInt32(mId);
Jeff Brown5912f952013-07-01 19:10:31 -0700769 parcel->writeInt32(mDeviceId);
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600770 parcel->writeUint32(mSource);
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800771 parcel->writeInt32(mDisplayId);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600772 std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end());
773 parcel->writeByteVector(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700774 parcel->writeInt32(mAction);
Michael Wright7b159c92015-05-14 14:48:03 +0100775 parcel->writeInt32(mActionButton);
Jeff Brown5912f952013-07-01 19:10:31 -0700776 parcel->writeInt32(mFlags);
777 parcel->writeInt32(mEdgeFlags);
778 parcel->writeInt32(mMetaState);
779 parcel->writeInt32(mButtonState);
Siarhei Vishniakou49e59222018-12-28 18:17:15 -0800780 parcel->writeByte(static_cast<int8_t>(mClassification));
chaviw9eaa22c2020-07-01 16:21:27 -0700781
782 status_t result = android::writeToParcel(mTransform, *parcel);
783 if (result != OK) {
784 return result;
785 }
Jeff Brown5912f952013-07-01 19:10:31 -0700786 parcel->writeFloat(mXPrecision);
787 parcel->writeFloat(mYPrecision);
Garfield Tan937bb832019-07-25 17:48:31 -0700788 parcel->writeFloat(mRawXCursorPosition);
789 parcel->writeFloat(mRawYCursorPosition);
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700790
791 result = android::writeToParcel(mRawTransform, *parcel);
792 if (result != OK) {
793 return result;
794 }
Jeff Brown5912f952013-07-01 19:10:31 -0700795 parcel->writeInt64(mDownTime);
796
797 for (size_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800798 const PointerProperties& properties = mPointerProperties[i];
Jeff Brown5912f952013-07-01 19:10:31 -0700799 parcel->writeInt32(properties.id);
800 parcel->writeInt32(properties.toolType);
801 }
802
Siarhei Vishniakou6dbd0ce2022-01-13 01:24:14 -0800803 const PointerCoords* pc = mSamplePointerCoords.data();
Jeff Brown5912f952013-07-01 19:10:31 -0700804 for (size_t h = 0; h < sampleCount; h++) {
Siarhei Vishniakou46a27742020-09-09 13:57:28 -0500805 parcel->writeInt64(mSampleEventTimes[h]);
Jeff Brown5912f952013-07-01 19:10:31 -0700806 for (size_t i = 0; i < pointerCount; i++) {
807 status_t status = (pc++)->writeToParcel(parcel);
808 if (status) {
809 return status;
810 }
811 }
812 }
813 return OK;
814}
Brett Chabotfaa986c2020-11-04 17:39:36 -0800815#endif
Jeff Brown5912f952013-07-01 19:10:31 -0700816
Siarhei Vishniakou3826d472020-01-27 10:44:40 -0600817bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700818 if (isFromSource(source, AINPUT_SOURCE_CLASS_POINTER)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700819 // Specifically excludes HOVER_MOVE and SCROLL.
820 switch (action & AMOTION_EVENT_ACTION_MASK) {
821 case AMOTION_EVENT_ACTION_DOWN:
822 case AMOTION_EVENT_ACTION_MOVE:
823 case AMOTION_EVENT_ACTION_UP:
824 case AMOTION_EVENT_ACTION_POINTER_DOWN:
825 case AMOTION_EVENT_ACTION_POINTER_UP:
826 case AMOTION_EVENT_ACTION_CANCEL:
827 case AMOTION_EVENT_ACTION_OUTSIDE:
828 return true;
829 }
830 }
831 return false;
832}
833
Michael Wright872db4f2014-04-22 15:03:51 -0700834const char* MotionEvent::getLabel(int32_t axis) {
Chris Ye4958d062020-08-20 13:21:10 -0700835 return InputEventLookup::getAxisLabel(axis);
Michael Wright872db4f2014-04-22 15:03:51 -0700836}
837
838int32_t MotionEvent::getAxisFromLabel(const char* label) {
Chris Ye4958d062020-08-20 13:21:10 -0700839 return InputEventLookup::getAxisByLabel(label);
Michael Wright872db4f2014-04-22 15:03:51 -0700840}
841
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500842std::string MotionEvent::actionToString(int32_t action) {
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700843 // Convert MotionEvent action to string
844 switch (action & AMOTION_EVENT_ACTION_MASK) {
845 case AMOTION_EVENT_ACTION_DOWN:
846 return "DOWN";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700847 case AMOTION_EVENT_ACTION_UP:
848 return "UP";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500849 case AMOTION_EVENT_ACTION_MOVE:
850 return "MOVE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700851 case AMOTION_EVENT_ACTION_CANCEL:
852 return "CANCEL";
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500853 case AMOTION_EVENT_ACTION_OUTSIDE:
854 return "OUTSIDE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700855 case AMOTION_EVENT_ACTION_POINTER_DOWN:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000856 return StringPrintf("POINTER_DOWN(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700857 case AMOTION_EVENT_ACTION_POINTER_UP:
Siarhei Vishniakouca205502021-07-16 21:31:58 +0000858 return StringPrintf("POINTER_UP(%" PRId32 ")", MotionEvent::getActionIndex(action));
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500859 case AMOTION_EVENT_ACTION_HOVER_MOVE:
860 return "HOVER_MOVE";
861 case AMOTION_EVENT_ACTION_SCROLL:
862 return "SCROLL";
863 case AMOTION_EVENT_ACTION_HOVER_ENTER:
864 return "HOVER_ENTER";
865 case AMOTION_EVENT_ACTION_HOVER_EXIT:
866 return "HOVER_EXIT";
867 case AMOTION_EVENT_ACTION_BUTTON_PRESS:
868 return "BUTTON_PRESS";
869 case AMOTION_EVENT_ACTION_BUTTON_RELEASE:
870 return "BUTTON_RELEASE";
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700871 }
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500872 return android::base::StringPrintf("%" PRId32, action);
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700873}
874
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700875// Apply the given transformation to the point without checking whether the entire transform
876// should be disregarded altogether for the provided source.
877static inline vec2 calculateTransformedXYUnchecked(uint32_t source, const ui::Transform& transform,
878 const vec2& xy) {
879 return shouldDisregardOffset(source) ? transformWithoutTranslation(transform, xy)
880 : transform.transform(xy);
881}
882
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700883vec2 MotionEvent::calculateTransformedXY(uint32_t source, const ui::Transform& transform,
884 const vec2& xy) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700885 if (shouldDisregardTransformation(source)) {
886 return xy;
887 }
888 return calculateTransformedXYUnchecked(source, transform, xy);
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700889}
890
Prabir Pradhan8e6ce222022-02-24 09:08:54 -0800891// Keep in sync with calculateTransformedCoords.
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700892float MotionEvent::calculateTransformedAxisValue(int32_t axis, uint32_t source,
893 const ui::Transform& transform,
894 const PointerCoords& coords) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700895 if (shouldDisregardTransformation(source)) {
896 return coords.getAxisValue(axis);
897 }
898
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700899 if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
Prabir Pradhan7e1ee562021-10-26 10:19:49 -0700900 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
Prabir Pradhan9eb02c02021-10-19 14:02:20 -0700901 static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
902 return xy[axis];
903 }
904
905 if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
906 const vec2 relativeXy =
907 transformWithoutTranslation(transform,
908 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
909 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
910 return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y;
911 }
912
913 if (axis == AMOTION_EVENT_AXIS_ORIENTATION) {
914 return transformAngle(transform, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION));
915 }
916
917 return coords.getAxisValue(axis);
918}
919
Prabir Pradhan8e6ce222022-02-24 09:08:54 -0800920// Keep in sync with calculateTransformedAxisValue. This is an optimization of
921// calculateTransformedAxisValue for all PointerCoords axes.
922PointerCoords MotionEvent::calculateTransformedCoords(uint32_t source,
923 const ui::Transform& transform,
924 const PointerCoords& coords) {
925 if (shouldDisregardTransformation(source)) {
926 return coords;
927 }
928 PointerCoords out = coords;
929
930 const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue());
931 out.setAxisValue(AMOTION_EVENT_AXIS_X, xy.x);
932 out.setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y);
933
934 const vec2 relativeXy =
935 transformWithoutTranslation(transform,
936 {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X),
937 coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)});
938 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x);
939 out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y);
940
941 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
942 transformAngle(transform,
943 coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)));
944
945 return out;
946}
947
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000948std::ostream& operator<<(std::ostream& out, const MotionEvent& event) {
949 out << "MotionEvent { action=" << MotionEvent::actionToString(event.getAction());
950 if (event.getActionButton() != 0) {
951 out << ", actionButton=" << std::to_string(event.getActionButton());
952 }
953 const size_t pointerCount = event.getPointerCount();
hupeng3aa5a51a2022-09-02 16:00:18 +0800954 LOG_ALWAYS_FATAL_IF(pointerCount > MAX_POINTERS, "Too many pointers : pointerCount = %zu",
955 pointerCount);
Siarhei Vishniakou4ded0b02022-05-26 00:36:48 +0000956 for (size_t i = 0; i < pointerCount; i++) {
957 out << ", id[" << i << "]=" << event.getPointerId(i);
958 float x = event.getX(i);
959 float y = event.getY(i);
960 if (x != 0 || y != 0) {
961 out << ", x[" << i << "]=" << x;
962 out << ", y[" << i << "]=" << y;
963 }
964 int toolType = event.getToolType(i);
965 if (toolType != AMOTION_EVENT_TOOL_TYPE_FINGER) {
966 out << ", toolType[" << i << "]=" << toolType;
967 }
968 }
969 if (event.getButtonState() != 0) {
970 out << ", buttonState=" << event.getButtonState();
971 }
972 if (event.getClassification() != MotionClassification::NONE) {
973 out << ", classification=" << motionClassificationToString(event.getClassification());
974 }
975 if (event.getMetaState() != 0) {
976 out << ", metaState=" << event.getMetaState();
977 }
978 if (event.getEdgeFlags() != 0) {
979 out << ", edgeFlags=" << event.getEdgeFlags();
980 }
981 if (pointerCount != 1) {
982 out << ", pointerCount=" << pointerCount;
983 }
984 if (event.getHistorySize() != 0) {
985 out << ", historySize=" << event.getHistorySize();
986 }
987 out << ", eventTime=" << event.getEventTime();
988 out << ", downTime=" << event.getDownTime();
989 out << ", deviceId=" << event.getDeviceId();
990 out << ", source=" << inputEventSourceToString(event.getSource());
991 out << ", displayId=" << event.getDisplayId();
992 out << ", eventId=" << event.getId();
993 out << "}";
994 return out;
995}
996
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800997// --- FocusEvent ---
998
Antonio Kantek3cfec7b2021-11-05 18:26:17 -0700999void FocusEvent::initialize(int32_t id, bool hasFocus) {
Garfield Tan4cc839f2020-01-24 11:26:14 -08001000 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -06001001 ADISPLAY_ID_NONE, INVALID_HMAC);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001002 mHasFocus = hasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001003}
1004
1005void FocusEvent::initialize(const FocusEvent& from) {
1006 InputEvent::initialize(from);
1007 mHasFocus = from.mHasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001008}
Jeff Brown5912f952013-07-01 19:10:31 -07001009
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001010// --- CaptureEvent ---
1011
1012void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) {
1013 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1014 ADISPLAY_ID_NONE, INVALID_HMAC);
1015 mPointerCaptureEnabled = pointerCaptureEnabled;
1016}
1017
1018void CaptureEvent::initialize(const CaptureEvent& from) {
1019 InputEvent::initialize(from);
1020 mPointerCaptureEnabled = from.mPointerCaptureEnabled;
1021}
1022
arthurhung7632c332020-12-30 16:58:01 +08001023// --- DragEvent ---
1024
1025void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) {
1026 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1027 ADISPLAY_ID_NONE, INVALID_HMAC);
1028 mIsExiting = isExiting;
1029 mX = x;
1030 mY = y;
1031}
1032
1033void DragEvent::initialize(const DragEvent& from) {
1034 InputEvent::initialize(from);
1035 mIsExiting = from.mIsExiting;
1036 mX = from.mX;
1037 mY = from.mY;
1038}
1039
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001040// --- TouchModeEvent ---
1041
1042void TouchModeEvent::initialize(int32_t id, bool isInTouchMode) {
1043 InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
1044 ADISPLAY_ID_NONE, INVALID_HMAC);
1045 mIsInTouchMode = isInTouchMode;
1046}
1047
1048void TouchModeEvent::initialize(const TouchModeEvent& from) {
1049 InputEvent::initialize(from);
1050 mIsInTouchMode = from.mIsInTouchMode;
1051}
1052
Jeff Brown5912f952013-07-01 19:10:31 -07001053// --- PooledInputEventFactory ---
1054
1055PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
1056 mMaxPoolSize(maxPoolSize) {
1057}
1058
1059PooledInputEventFactory::~PooledInputEventFactory() {
Jeff Brown5912f952013-07-01 19:10:31 -07001060}
1061
1062KeyEvent* PooledInputEventFactory::createKeyEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001063 if (mKeyEventPool.empty()) {
1064 return new KeyEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001065 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001066 KeyEvent* event = mKeyEventPool.front().release();
1067 mKeyEventPool.pop();
1068 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001069}
1070
1071MotionEvent* PooledInputEventFactory::createMotionEvent() {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001072 if (mMotionEventPool.empty()) {
1073 return new MotionEvent();
Jeff Brown5912f952013-07-01 19:10:31 -07001074 }
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001075 MotionEvent* event = mMotionEventPool.front().release();
1076 mMotionEventPool.pop();
1077 return event;
Jeff Brown5912f952013-07-01 19:10:31 -07001078}
1079
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001080FocusEvent* PooledInputEventFactory::createFocusEvent() {
1081 if (mFocusEventPool.empty()) {
1082 return new FocusEvent();
1083 }
1084 FocusEvent* event = mFocusEventPool.front().release();
1085 mFocusEventPool.pop();
1086 return event;
1087}
1088
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001089CaptureEvent* PooledInputEventFactory::createCaptureEvent() {
1090 if (mCaptureEventPool.empty()) {
1091 return new CaptureEvent();
1092 }
1093 CaptureEvent* event = mCaptureEventPool.front().release();
1094 mCaptureEventPool.pop();
1095 return event;
1096}
1097
arthurhung7632c332020-12-30 16:58:01 +08001098DragEvent* PooledInputEventFactory::createDragEvent() {
1099 if (mDragEventPool.empty()) {
1100 return new DragEvent();
1101 }
1102 DragEvent* event = mDragEventPool.front().release();
1103 mDragEventPool.pop();
1104 return event;
1105}
1106
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001107TouchModeEvent* PooledInputEventFactory::createTouchModeEvent() {
1108 if (mTouchModeEventPool.empty()) {
1109 return new TouchModeEvent();
1110 }
1111 TouchModeEvent* event = mTouchModeEventPool.front().release();
1112 mTouchModeEventPool.pop();
1113 return event;
1114}
1115
Jeff Brown5912f952013-07-01 19:10:31 -07001116void PooledInputEventFactory::recycle(InputEvent* event) {
1117 switch (event->getType()) {
1118 case AINPUT_EVENT_TYPE_KEY:
1119 if (mKeyEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001120 mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -07001121 return;
1122 }
1123 break;
1124 case AINPUT_EVENT_TYPE_MOTION:
1125 if (mMotionEventPool.size() < mMaxPoolSize) {
Siarhei Vishniakou727a44e2019-11-23 12:59:16 -08001126 mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event)));
Jeff Brown5912f952013-07-01 19:10:31 -07001127 return;
1128 }
1129 break;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001130 case AINPUT_EVENT_TYPE_FOCUS:
1131 if (mFocusEventPool.size() < mMaxPoolSize) {
1132 mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event)));
1133 return;
1134 }
1135 break;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001136 case AINPUT_EVENT_TYPE_CAPTURE:
1137 if (mCaptureEventPool.size() < mMaxPoolSize) {
1138 mCaptureEventPool.push(
1139 std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event)));
1140 return;
1141 }
1142 break;
arthurhung7632c332020-12-30 16:58:01 +08001143 case AINPUT_EVENT_TYPE_DRAG:
1144 if (mDragEventPool.size() < mMaxPoolSize) {
1145 mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event)));
1146 return;
1147 }
1148 break;
Antonio Kantekeb4a30c2021-09-28 17:49:49 -07001149 case AINPUT_EVENT_TYPE_TOUCH_MODE:
1150 if (mTouchModeEventPool.size() < mMaxPoolSize) {
1151 mTouchModeEventPool.push(
1152 std::unique_ptr<TouchModeEvent>(static_cast<TouchModeEvent*>(event)));
1153 return;
1154 }
1155 break;
Jeff Brown5912f952013-07-01 19:10:31 -07001156 }
1157 delete event;
1158}
1159
1160} // namespace android