Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 20 | #include <attestation/HmacKeyManager.h> |
Garfield Tan | 84b087e | 2020-01-23 10:49:05 -0800 | [diff] [blame] | 21 | #include <cutils/compiler.h> |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame] | 22 | #include <inttypes.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 23 | #include <limits.h> |
Garfield Tan | 84b087e | 2020-01-23 10:49:05 -0800 | [diff] [blame] | 24 | #include <string.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 25 | |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame] | 26 | #include <android-base/stringprintf.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 27 | #include <input/Input.h> |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 28 | #include <input/InputDevice.h> |
Michael Wright | 872db4f | 2014-04-22 15:03:51 -0700 | [diff] [blame] | 29 | #include <input/InputEventLabels.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 30 | |
Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 31 | #ifdef __linux__ |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 32 | #include <binder/Parcel.h> |
Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 33 | #endif |
Brett Chabot | 5820852 | 2020-09-09 13:55:24 -0700 | [diff] [blame] | 34 | #ifdef __ANDROID__ |
Garfield Tan | 84b087e | 2020-01-23 10:49:05 -0800 | [diff] [blame] | 35 | #include <sys/random.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 36 | #endif |
| 37 | |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame] | 38 | using android::base::StringPrintf; |
| 39 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 40 | namespace android { |
| 41 | |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 42 | namespace { |
| 43 | |
| 44 | float transformAngle(const ui::Transform& transform, float angleRadians) { |
| 45 | // Construct and transform a vector oriented at the specified clockwise angle from vertical. |
| 46 | // Coordinate system: down is increasing Y, right is increasing X. |
| 47 | float x = sinf(angleRadians); |
| 48 | float y = -cosf(angleRadians); |
| 49 | vec2 transformedPoint = transform.transform(x, y); |
| 50 | |
| 51 | // Determine how the origin is transformed by the matrix so that we |
| 52 | // can transform orientation vectors. |
| 53 | const vec2 origin = transform.transform(0, 0); |
| 54 | |
| 55 | transformedPoint.x -= origin.x; |
| 56 | transformedPoint.y -= origin.y; |
| 57 | |
| 58 | // Derive the transformed vector's clockwise angle from vertical. |
| 59 | float result = atan2f(transformedPoint.x, -transformedPoint.y); |
| 60 | if (result < -M_PI_2) { |
| 61 | result += M_PI; |
| 62 | } else if (result > M_PI_2) { |
| 63 | result -= M_PI; |
| 64 | } |
| 65 | return result; |
| 66 | } |
| 67 | |
| 68 | // Rotates the given point to the transform's orientation. If the display width and height are |
| 69 | // provided, the point is rotated in the screen space. Otherwise, the point is rotated about the |
| 70 | // origin. This helper is used to avoid the extra overhead of creating new Transforms. |
| 71 | vec2 rotatePoint(const ui::Transform& transform, float x, float y, int32_t displayWidth = 0, |
| 72 | int32_t displayHeight = 0) { |
| 73 | // 0x7 encapsulates all 3 rotations (see ui::Transform::RotationFlags) |
| 74 | static const int ALL_ROTATIONS_MASK = 0x7; |
| 75 | const uint32_t orientation = (transform.getOrientation() & ALL_ROTATIONS_MASK); |
| 76 | if (orientation == ui::Transform::ROT_0) { |
| 77 | return {x, y}; |
| 78 | } |
| 79 | |
| 80 | vec2 xy(x, y); |
| 81 | if (orientation == ui::Transform::ROT_90) { |
| 82 | xy.x = displayHeight - y; |
| 83 | xy.y = x; |
| 84 | } else if (orientation == ui::Transform::ROT_180) { |
| 85 | xy.x = displayWidth - x; |
| 86 | xy.y = displayHeight - y; |
| 87 | } else if (orientation == ui::Transform::ROT_270) { |
| 88 | xy.x = y; |
| 89 | xy.y = displayWidth - x; |
| 90 | } |
| 91 | return xy; |
| 92 | } |
| 93 | |
Prabir Pradhan | 9f38881 | 2021-05-13 16:54:53 -0700 | [diff] [blame^] | 94 | vec2 applyTransformWithoutTranslation(const ui::Transform& transform, float x, float y) { |
| 95 | const vec2 transformedXy = transform.transform(x, y); |
| 96 | const vec2 transformedOrigin = transform.transform(0, 0); |
| 97 | return transformedXy - transformedOrigin; |
| 98 | } |
| 99 | |
| 100 | bool shouldDisregardWindowTranslation(uint32_t source) { |
| 101 | // Pointer events are the only type of events that refer to absolute coordinates on the display, |
| 102 | // so we should apply the entire window transform. For other types of events, we should make |
| 103 | // sure to not apply the window translation/offset. |
| 104 | return (source & AINPUT_SOURCE_CLASS_POINTER) == 0; |
| 105 | } |
| 106 | |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 107 | } // namespace |
| 108 | |
Siarhei Vishniakou | 16a2e30 | 2019-01-14 19:21:45 -0800 | [diff] [blame] | 109 | const char* motionClassificationToString(MotionClassification classification) { |
| 110 | switch (classification) { |
| 111 | case MotionClassification::NONE: |
| 112 | return "NONE"; |
| 113 | case MotionClassification::AMBIGUOUS_GESTURE: |
| 114 | return "AMBIGUOUS_GESTURE"; |
| 115 | case MotionClassification::DEEP_PRESS: |
| 116 | return "DEEP_PRESS"; |
| 117 | } |
| 118 | } |
| 119 | |
Garfield Tan | 84b087e | 2020-01-23 10:49:05 -0800 | [diff] [blame] | 120 | // --- IdGenerator --- |
| 121 | IdGenerator::IdGenerator(Source source) : mSource(source) {} |
| 122 | |
| 123 | int32_t IdGenerator::nextId() const { |
| 124 | constexpr uint32_t SEQUENCE_NUMBER_MASK = ~SOURCE_MASK; |
| 125 | int32_t id = 0; |
| 126 | |
| 127 | // Avoid building against syscall getrandom(2) on host, which will fail build on Mac. Host doesn't |
| 128 | // use sequence number so just always return mSource. |
| 129 | #ifdef __ANDROID__ |
| 130 | constexpr size_t BUF_LEN = sizeof(id); |
| 131 | size_t totalBytes = 0; |
| 132 | while (totalBytes < BUF_LEN) { |
| 133 | ssize_t bytes = TEMP_FAILURE_RETRY(getrandom(&id, BUF_LEN, GRND_NONBLOCK)); |
| 134 | if (CC_UNLIKELY(bytes < 0)) { |
| 135 | ALOGW("Failed to fill in random number for sequence number: %s.", strerror(errno)); |
| 136 | id = 0; |
| 137 | break; |
| 138 | } |
| 139 | totalBytes += bytes; |
| 140 | } |
| 141 | #endif // __ANDROID__ |
| 142 | |
| 143 | return (id & SEQUENCE_NUMBER_MASK) | static_cast<int32_t>(mSource); |
| 144 | } |
| 145 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 146 | // --- InputEvent --- |
| 147 | |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 148 | const 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 Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 159 | case AINPUT_EVENT_TYPE_CAPTURE: { |
| 160 | return "CAPTURE"; |
| 161 | } |
arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 162 | case AINPUT_EVENT_TYPE_DRAG: { |
| 163 | return "DRAG"; |
| 164 | } |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 165 | } |
| 166 | return "UNKNOWN"; |
| 167 | } |
| 168 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 169 | VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) { |
| 170 | return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(), |
| 171 | event.getSource(), event.getDisplayId()}, |
| 172 | event.getAction(), |
| 173 | event.getDownTime(), |
| 174 | event.getFlags() & VERIFIED_KEY_EVENT_FLAGS, |
| 175 | event.getKeyCode(), |
| 176 | event.getScanCode(), |
| 177 | event.getMetaState(), |
| 178 | event.getRepeatCount()}; |
| 179 | } |
| 180 | |
| 181 | VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) { |
| 182 | return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(), |
| 183 | event.getSource(), event.getDisplayId()}, |
| 184 | event.getRawX(0), |
| 185 | event.getRawY(0), |
| 186 | event.getActionMasked(), |
| 187 | event.getDownTime(), |
| 188 | event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS, |
| 189 | event.getMetaState(), |
| 190 | event.getButtonState()}; |
| 191 | } |
| 192 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 193 | void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 194 | std::array<uint8_t, 32> hmac) { |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 195 | mId = id; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 196 | mDeviceId = deviceId; |
| 197 | mSource = source; |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 198 | mDisplayId = displayId; |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 199 | mHmac = hmac; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | void InputEvent::initialize(const InputEvent& from) { |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 203 | mId = from.mId; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 204 | mDeviceId = from.mDeviceId; |
| 205 | mSource = from.mSource; |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 206 | mDisplayId = from.mDisplayId; |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 207 | mHmac = from.mHmac; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 210 | int32_t InputEvent::nextId() { |
| 211 | static IdGenerator idGen(IdGenerator::Source::OTHER); |
| 212 | return idGen.nextId(); |
| 213 | } |
| 214 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 215 | // --- KeyEvent --- |
| 216 | |
Michael Wright | 872db4f | 2014-04-22 15:03:51 -0700 | [diff] [blame] | 217 | const char* KeyEvent::getLabel(int32_t keyCode) { |
Chris Ye | 4958d06 | 2020-08-20 13:21:10 -0700 | [diff] [blame] | 218 | return InputEventLookup::getLabelByKeyCode(keyCode); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Michael Wright | 872db4f | 2014-04-22 15:03:51 -0700 | [diff] [blame] | 221 | int32_t KeyEvent::getKeyCodeFromLabel(const char* label) { |
Chris Ye | 4958d06 | 2020-08-20 13:21:10 -0700 | [diff] [blame] | 222 | return InputEventLookup::getKeyCodeByLabel(label); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 225 | void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 226 | std::array<uint8_t, 32> hmac, int32_t action, int32_t flags, |
| 227 | int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount, |
| 228 | nsecs_t downTime, nsecs_t eventTime) { |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 229 | InputEvent::initialize(id, deviceId, source, displayId, hmac); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 230 | mAction = action; |
| 231 | mFlags = flags; |
| 232 | mKeyCode = keyCode; |
| 233 | mScanCode = scanCode; |
| 234 | mMetaState = metaState; |
| 235 | mRepeatCount = repeatCount; |
| 236 | mDownTime = downTime; |
| 237 | mEventTime = eventTime; |
| 238 | } |
| 239 | |
| 240 | void KeyEvent::initialize(const KeyEvent& from) { |
| 241 | InputEvent::initialize(from); |
| 242 | mAction = from.mAction; |
| 243 | mFlags = from.mFlags; |
| 244 | mKeyCode = from.mKeyCode; |
| 245 | mScanCode = from.mScanCode; |
| 246 | mMetaState = from.mMetaState; |
| 247 | mRepeatCount = from.mRepeatCount; |
| 248 | mDownTime = from.mDownTime; |
| 249 | mEventTime = from.mEventTime; |
| 250 | } |
| 251 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 252 | const char* KeyEvent::actionToString(int32_t action) { |
| 253 | // Convert KeyEvent action to string |
| 254 | switch (action) { |
| 255 | case AKEY_EVENT_ACTION_DOWN: |
| 256 | return "DOWN"; |
| 257 | case AKEY_EVENT_ACTION_UP: |
| 258 | return "UP"; |
| 259 | case AKEY_EVENT_ACTION_MULTIPLE: |
| 260 | return "MULTIPLE"; |
| 261 | } |
| 262 | return "UNKNOWN"; |
| 263 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 264 | |
| 265 | // --- PointerCoords --- |
| 266 | |
| 267 | float PointerCoords::getAxisValue(int32_t axis) const { |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 268 | if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){ |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 269 | return 0; |
| 270 | } |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 271 | return values[BitSet64::getIndexOfBit(bits, axis)]; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | status_t PointerCoords::setAxisValue(int32_t axis, float value) { |
| 275 | if (axis < 0 || axis > 63) { |
| 276 | return NAME_NOT_FOUND; |
| 277 | } |
| 278 | |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 279 | uint32_t index = BitSet64::getIndexOfBit(bits, axis); |
| 280 | if (!BitSet64::hasBit(bits, axis)) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 281 | if (value == 0) { |
| 282 | return OK; // axes with value 0 do not need to be stored |
| 283 | } |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 284 | |
| 285 | uint32_t count = BitSet64::count(bits); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 286 | if (count >= MAX_AXES) { |
| 287 | tooManyAxes(axis); |
| 288 | return NO_MEMORY; |
| 289 | } |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 290 | BitSet64::markBit(bits, axis); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 291 | for (uint32_t i = count; i > index; i--) { |
| 292 | values[i] = values[i - 1]; |
| 293 | } |
| 294 | } |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 295 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 296 | values[index] = value; |
| 297 | return OK; |
| 298 | } |
| 299 | |
| 300 | static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) { |
| 301 | float value = c.getAxisValue(axis); |
| 302 | if (value != 0) { |
| 303 | c.setAxisValue(axis, value * scaleFactor); |
| 304 | } |
| 305 | } |
| 306 | |
Robert Carr | e07e103 | 2018-11-26 12:55:53 -0800 | [diff] [blame] | 307 | void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 308 | // No need to scale pressure or size since they are normalized. |
| 309 | // No need to scale orientation since it is meaningless to do so. |
Robert Carr | e07e103 | 2018-11-26 12:55:53 -0800 | [diff] [blame] | 310 | |
| 311 | // If there is a global scale factor, it is included in the windowX/YScale |
| 312 | // so we don't need to apply it twice to the X/Y axes. |
| 313 | // However we don't want to apply any windowXYScale not included in the global scale |
| 314 | // to the TOUCH_MAJOR/MINOR coordinates. |
| 315 | scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale); |
| 316 | scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale); |
| 317 | scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor); |
| 318 | scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor); |
| 319 | scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor); |
| 320 | scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor); |
| 321 | } |
| 322 | |
| 323 | void PointerCoords::scale(float globalScaleFactor) { |
| 324 | scale(globalScaleFactor, globalScaleFactor, globalScaleFactor); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 327 | void PointerCoords::applyOffset(float xOffset, float yOffset) { |
| 328 | setAxisValue(AMOTION_EVENT_AXIS_X, getX() + xOffset); |
| 329 | setAxisValue(AMOTION_EVENT_AXIS_Y, getY() + yOffset); |
| 330 | } |
| 331 | |
Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 332 | #ifdef __linux__ |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 333 | status_t PointerCoords::readFromParcel(Parcel* parcel) { |
| 334 | bits = parcel->readInt64(); |
| 335 | |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 336 | uint32_t count = BitSet64::count(bits); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 337 | if (count > MAX_AXES) { |
| 338 | return BAD_VALUE; |
| 339 | } |
| 340 | |
| 341 | for (uint32_t i = 0; i < count; i++) { |
| 342 | values[i] = parcel->readFloat(); |
| 343 | } |
| 344 | return OK; |
| 345 | } |
| 346 | |
| 347 | status_t PointerCoords::writeToParcel(Parcel* parcel) const { |
| 348 | parcel->writeInt64(bits); |
| 349 | |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 350 | uint32_t count = BitSet64::count(bits); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 351 | for (uint32_t i = 0; i < count; i++) { |
| 352 | parcel->writeFloat(values[i]); |
| 353 | } |
| 354 | return OK; |
| 355 | } |
Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 356 | #endif |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 357 | |
| 358 | void PointerCoords::tooManyAxes(int axis) { |
| 359 | ALOGW("Could not set value for axis %d because the PointerCoords structure is full and " |
| 360 | "cannot contain more than %d axis values.", axis, int(MAX_AXES)); |
| 361 | } |
| 362 | |
| 363 | bool PointerCoords::operator==(const PointerCoords& other) const { |
| 364 | if (bits != other.bits) { |
| 365 | return false; |
| 366 | } |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 367 | uint32_t count = BitSet64::count(bits); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 368 | for (uint32_t i = 0; i < count; i++) { |
| 369 | if (values[i] != other.values[i]) { |
| 370 | return false; |
| 371 | } |
| 372 | } |
| 373 | return true; |
| 374 | } |
| 375 | |
| 376 | void PointerCoords::copyFrom(const PointerCoords& other) { |
| 377 | bits = other.bits; |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 378 | uint32_t count = BitSet64::count(bits); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 379 | for (uint32_t i = 0; i < count; i++) { |
| 380 | values[i] = other.values[i]; |
| 381 | } |
| 382 | } |
| 383 | |
chaviw | c01e137 | 2020-07-01 12:37:31 -0700 | [diff] [blame] | 384 | void PointerCoords::transform(const ui::Transform& transform) { |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 385 | const vec2 xy = transform.transform(getXYValue()); |
| 386 | setAxisValue(AMOTION_EVENT_AXIS_X, xy.x); |
| 387 | setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y); |
| 388 | |
| 389 | if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) { |
| 390 | const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION); |
| 391 | setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val)); |
| 392 | } |
chaviw | c01e137 | 2020-07-01 12:37:31 -0700 | [diff] [blame] | 393 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 394 | |
| 395 | // --- PointerProperties --- |
| 396 | |
| 397 | bool PointerProperties::operator==(const PointerProperties& other) const { |
| 398 | return id == other.id |
| 399 | && toolType == other.toolType; |
| 400 | } |
| 401 | |
| 402 | void PointerProperties::copyFrom(const PointerProperties& other) { |
| 403 | id = other.id; |
| 404 | toolType = other.toolType; |
| 405 | } |
| 406 | |
| 407 | |
| 408 | // --- MotionEvent --- |
| 409 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 410 | void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 411 | std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton, |
| 412 | int32_t flags, int32_t edgeFlags, int32_t metaState, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 413 | int32_t buttonState, MotionClassification classification, |
| 414 | const ui::Transform& transform, float xPrecision, float yPrecision, |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 415 | float rawXCursorPosition, float rawYCursorPosition, |
| 416 | int32_t displayWidth, int32_t displayHeight, nsecs_t downTime, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 417 | nsecs_t eventTime, size_t pointerCount, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 418 | const PointerProperties* pointerProperties, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 419 | const PointerCoords* pointerCoords) { |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 420 | InputEvent::initialize(id, deviceId, source, displayId, hmac); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 421 | mAction = action; |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 422 | mActionButton = actionButton; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 423 | mFlags = flags; |
| 424 | mEdgeFlags = edgeFlags; |
| 425 | mMetaState = metaState; |
| 426 | mButtonState = buttonState; |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 427 | mClassification = classification; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 428 | mTransform = transform; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 429 | mXPrecision = xPrecision; |
| 430 | mYPrecision = yPrecision; |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 431 | mRawXCursorPosition = rawXCursorPosition; |
| 432 | mRawYCursorPosition = rawYCursorPosition; |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 433 | mDisplayWidth = displayWidth; |
| 434 | mDisplayHeight = displayHeight; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 435 | mDownTime = downTime; |
| 436 | mPointerProperties.clear(); |
| 437 | mPointerProperties.appendArray(pointerProperties, pointerCount); |
| 438 | mSampleEventTimes.clear(); |
| 439 | mSamplePointerCoords.clear(); |
| 440 | addSample(eventTime, pointerCoords); |
| 441 | } |
| 442 | |
| 443 | void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) { |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 444 | InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId, |
| 445 | other->mHmac); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 446 | mAction = other->mAction; |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 447 | mActionButton = other->mActionButton; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 448 | mFlags = other->mFlags; |
| 449 | mEdgeFlags = other->mEdgeFlags; |
| 450 | mMetaState = other->mMetaState; |
| 451 | mButtonState = other->mButtonState; |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 452 | mClassification = other->mClassification; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 453 | mTransform = other->mTransform; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 454 | mXPrecision = other->mXPrecision; |
| 455 | mYPrecision = other->mYPrecision; |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 456 | mRawXCursorPosition = other->mRawXCursorPosition; |
| 457 | mRawYCursorPosition = other->mRawYCursorPosition; |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 458 | mDisplayWidth = other->mDisplayWidth; |
| 459 | mDisplayHeight = other->mDisplayHeight; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 460 | mDownTime = other->mDownTime; |
| 461 | mPointerProperties = other->mPointerProperties; |
| 462 | |
| 463 | if (keepHistory) { |
| 464 | mSampleEventTimes = other->mSampleEventTimes; |
| 465 | mSamplePointerCoords = other->mSamplePointerCoords; |
| 466 | } else { |
| 467 | mSampleEventTimes.clear(); |
Siarhei Vishniakou | 46a2774 | 2020-09-09 13:57:28 -0500 | [diff] [blame] | 468 | mSampleEventTimes.push_back(other->getEventTime()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 469 | mSamplePointerCoords.clear(); |
| 470 | size_t pointerCount = other->getPointerCount(); |
| 471 | size_t historySize = other->getHistorySize(); |
| 472 | mSamplePointerCoords.appendArray(other->mSamplePointerCoords.array() |
| 473 | + (historySize * pointerCount), pointerCount); |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | void MotionEvent::addSample( |
| 478 | int64_t eventTime, |
| 479 | const PointerCoords* pointerCoords) { |
Siarhei Vishniakou | 46a2774 | 2020-09-09 13:57:28 -0500 | [diff] [blame] | 480 | mSampleEventTimes.push_back(eventTime); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 481 | mSamplePointerCoords.appendArray(pointerCoords, getPointerCount()); |
| 482 | } |
| 483 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 484 | float MotionEvent::getXCursorPosition() const { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 485 | vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition()); |
| 486 | return vals.x; |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | float MotionEvent::getYCursorPosition() const { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 490 | vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition()); |
| 491 | return vals.y; |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 492 | } |
| 493 | |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 494 | void MotionEvent::setCursorPosition(float x, float y) { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 495 | ui::Transform inverse = mTransform.inverse(); |
| 496 | vec2 vals = inverse.transform(x, y); |
| 497 | mRawXCursorPosition = vals.x; |
| 498 | mRawYCursorPosition = vals.y; |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 501 | const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const { |
| 502 | return &mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex]; |
| 503 | } |
| 504 | |
| 505 | float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const { |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 506 | return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 510 | return getHistoricalAxisValue(axis, pointerIndex, getHistorySize()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | const PointerCoords* MotionEvent::getHistoricalRawPointerCoords( |
| 514 | size_t pointerIndex, size_t historicalIndex) const { |
| 515 | return &mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex]; |
| 516 | } |
| 517 | |
| 518 | float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex, |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 519 | size_t historicalIndex) const { |
| 520 | const PointerCoords* coords = getHistoricalRawPointerCoords(pointerIndex, historicalIndex); |
| 521 | |
| 522 | if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) { |
| 523 | // For compatibility, convert raw coordinates into "oriented screen space". Once app |
| 524 | // developers are educated about getRaw, we can consider removing this. |
Prabir Pradhan | 9f38881 | 2021-05-13 16:54:53 -0700 | [diff] [blame^] | 525 | const vec2 xy = shouldDisregardWindowTranslation(mSource) |
| 526 | ? rotatePoint(mTransform, coords->getX(), coords->getY()) |
| 527 | : rotatePoint(mTransform, coords->getX(), coords->getY(), mDisplayWidth, |
| 528 | mDisplayHeight); |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 529 | static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1); |
| 530 | return xy[axis]; |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 531 | } |
| 532 | |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 533 | return coords->getAxisValue(axis); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex, |
Prabir Pradhan | 9f38881 | 2021-05-13 16:54:53 -0700 | [diff] [blame^] | 537 | size_t historicalIndex) const { |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 538 | const PointerCoords* coords = getHistoricalRawPointerCoords(pointerIndex, historicalIndex); |
| 539 | |
| 540 | if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) { |
Prabir Pradhan | 9f38881 | 2021-05-13 16:54:53 -0700 | [diff] [blame^] | 541 | const vec2 xy = shouldDisregardWindowTranslation(mSource) |
| 542 | ? applyTransformWithoutTranslation(mTransform, coords->getX(), coords->getY()) |
| 543 | : mTransform.transform(coords->getXYValue()); |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 544 | static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1); |
| 545 | return xy[axis]; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 546 | } |
| 547 | |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 548 | return coords->getAxisValue(axis); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const { |
| 552 | size_t pointerCount = mPointerProperties.size(); |
| 553 | for (size_t i = 0; i < pointerCount; i++) { |
| 554 | if (mPointerProperties.itemAt(i).id == pointerId) { |
| 555 | return i; |
| 556 | } |
| 557 | } |
| 558 | return -1; |
| 559 | } |
| 560 | |
| 561 | void MotionEvent::offsetLocation(float xOffset, float yOffset) { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 562 | float currXOffset = mTransform.tx(); |
| 563 | float currYOffset = mTransform.ty(); |
| 564 | mTransform.set(currXOffset + xOffset, currYOffset + yOffset); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 565 | } |
| 566 | |
Robert Carr | e07e103 | 2018-11-26 12:55:53 -0800 | [diff] [blame] | 567 | void MotionEvent::scale(float globalScaleFactor) { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 568 | mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor); |
Robert Carr | e07e103 | 2018-11-26 12:55:53 -0800 | [diff] [blame] | 569 | mXPrecision *= globalScaleFactor; |
| 570 | mYPrecision *= globalScaleFactor; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 571 | |
| 572 | size_t numSamples = mSamplePointerCoords.size(); |
| 573 | for (size_t i = 0; i < numSamples; i++) { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 574 | mSamplePointerCoords.editItemAt(i).scale(globalScaleFactor, globalScaleFactor, |
| 575 | globalScaleFactor); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 576 | } |
| 577 | } |
| 578 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 579 | void MotionEvent::transform(const std::array<float, 9>& matrix) { |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 580 | // We want to preserve the raw axes values stored in the PointerCoords, so we just update the |
| 581 | // transform using the values passed in. |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 582 | ui::Transform newTransform; |
| 583 | newTransform.set(matrix); |
| 584 | mTransform = newTransform * mTransform; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 585 | |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 586 | // We need to update the AXIS_ORIENTATION value here to maintain the old behavior where the |
| 587 | // orientation angle is not affected by the initial transformation set in the MotionEvent. |
| 588 | std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(), |
| 589 | [&newTransform](PointerCoords& c) { |
| 590 | float orientation = c.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION); |
| 591 | c.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, |
| 592 | transformAngle(newTransform, orientation)); |
| 593 | }); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 594 | } |
| 595 | |
Evan Rosky | d4d4d80 | 2021-05-03 20:12:21 -0700 | [diff] [blame] | 596 | void MotionEvent::applyTransform(const std::array<float, 9>& matrix) { |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 597 | ui::Transform transform; |
| 598 | transform.set(matrix); |
Evan Rosky | d4d4d80 | 2021-05-03 20:12:21 -0700 | [diff] [blame] | 599 | |
| 600 | // Apply the transformation to all samples. |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 601 | std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(), |
| 602 | [&transform](PointerCoords& c) { c.transform(transform); }); |
Evan Rosky | d4d4d80 | 2021-05-03 20:12:21 -0700 | [diff] [blame] | 603 | } |
| 604 | |
Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 605 | #ifdef __linux__ |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 606 | static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) { |
| 607 | float dsdx, dtdx, tx, dtdy, dsdy, ty; |
| 608 | status_t status = parcel.readFloat(&dsdx); |
| 609 | status |= parcel.readFloat(&dtdx); |
| 610 | status |= parcel.readFloat(&tx); |
| 611 | status |= parcel.readFloat(&dtdy); |
| 612 | status |= parcel.readFloat(&dsdy); |
| 613 | status |= parcel.readFloat(&ty); |
| 614 | |
| 615 | transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1}); |
| 616 | return status; |
| 617 | } |
| 618 | |
| 619 | static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) { |
| 620 | status_t status = parcel.writeFloat(transform.dsdx()); |
| 621 | status |= parcel.writeFloat(transform.dtdx()); |
| 622 | status |= parcel.writeFloat(transform.tx()); |
| 623 | status |= parcel.writeFloat(transform.dtdy()); |
| 624 | status |= parcel.writeFloat(transform.dsdy()); |
| 625 | status |= parcel.writeFloat(transform.ty()); |
| 626 | return status; |
| 627 | } |
| 628 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 629 | status_t MotionEvent::readFromParcel(Parcel* parcel) { |
| 630 | size_t pointerCount = parcel->readInt32(); |
| 631 | size_t sampleCount = parcel->readInt32(); |
Flanker | 552a8a5 | 2015-09-07 15:28:58 +0800 | [diff] [blame] | 632 | if (pointerCount == 0 || pointerCount > MAX_POINTERS || |
| 633 | sampleCount == 0 || sampleCount > MAX_SAMPLES) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 634 | return BAD_VALUE; |
| 635 | } |
| 636 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 637 | mId = parcel->readInt32(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 638 | mDeviceId = parcel->readInt32(); |
Siarhei Vishniakou | 3826d47 | 2020-01-27 10:44:40 -0600 | [diff] [blame] | 639 | mSource = parcel->readUint32(); |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 640 | mDisplayId = parcel->readInt32(); |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 641 | std::vector<uint8_t> hmac; |
| 642 | status_t result = parcel->readByteVector(&hmac); |
| 643 | if (result != OK || hmac.size() != 32) { |
| 644 | return BAD_VALUE; |
| 645 | } |
| 646 | std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 647 | mAction = parcel->readInt32(); |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 648 | mActionButton = parcel->readInt32(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 649 | mFlags = parcel->readInt32(); |
| 650 | mEdgeFlags = parcel->readInt32(); |
| 651 | mMetaState = parcel->readInt32(); |
| 652 | mButtonState = parcel->readInt32(); |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 653 | mClassification = static_cast<MotionClassification>(parcel->readByte()); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 654 | |
| 655 | result = android::readFromParcel(mTransform, *parcel); |
| 656 | if (result != OK) { |
| 657 | return result; |
| 658 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 659 | mXPrecision = parcel->readFloat(); |
| 660 | mYPrecision = parcel->readFloat(); |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 661 | mRawXCursorPosition = parcel->readFloat(); |
| 662 | mRawYCursorPosition = parcel->readFloat(); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 663 | mDisplayWidth = parcel->readInt32(); |
| 664 | mDisplayHeight = parcel->readInt32(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 665 | mDownTime = parcel->readInt64(); |
| 666 | |
| 667 | mPointerProperties.clear(); |
| 668 | mPointerProperties.setCapacity(pointerCount); |
| 669 | mSampleEventTimes.clear(); |
Siarhei Vishniakou | 46a2774 | 2020-09-09 13:57:28 -0500 | [diff] [blame] | 670 | mSampleEventTimes.reserve(sampleCount); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 671 | mSamplePointerCoords.clear(); |
| 672 | mSamplePointerCoords.setCapacity(sampleCount * pointerCount); |
| 673 | |
| 674 | for (size_t i = 0; i < pointerCount; i++) { |
| 675 | mPointerProperties.push(); |
| 676 | PointerProperties& properties = mPointerProperties.editTop(); |
| 677 | properties.id = parcel->readInt32(); |
| 678 | properties.toolType = parcel->readInt32(); |
| 679 | } |
| 680 | |
Dan Austin | c94fc45 | 2015-09-22 14:22:41 -0700 | [diff] [blame] | 681 | while (sampleCount > 0) { |
| 682 | sampleCount--; |
Siarhei Vishniakou | 46a2774 | 2020-09-09 13:57:28 -0500 | [diff] [blame] | 683 | mSampleEventTimes.push_back(parcel->readInt64()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 684 | for (size_t i = 0; i < pointerCount; i++) { |
| 685 | mSamplePointerCoords.push(); |
| 686 | status_t status = mSamplePointerCoords.editTop().readFromParcel(parcel); |
| 687 | if (status) { |
| 688 | return status; |
| 689 | } |
| 690 | } |
| 691 | } |
| 692 | return OK; |
| 693 | } |
| 694 | |
| 695 | status_t MotionEvent::writeToParcel(Parcel* parcel) const { |
| 696 | size_t pointerCount = mPointerProperties.size(); |
| 697 | size_t sampleCount = mSampleEventTimes.size(); |
| 698 | |
| 699 | parcel->writeInt32(pointerCount); |
| 700 | parcel->writeInt32(sampleCount); |
| 701 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 702 | parcel->writeInt32(mId); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 703 | parcel->writeInt32(mDeviceId); |
Siarhei Vishniakou | 3826d47 | 2020-01-27 10:44:40 -0600 | [diff] [blame] | 704 | parcel->writeUint32(mSource); |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 705 | parcel->writeInt32(mDisplayId); |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 706 | std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end()); |
| 707 | parcel->writeByteVector(hmac); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 708 | parcel->writeInt32(mAction); |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 709 | parcel->writeInt32(mActionButton); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 710 | parcel->writeInt32(mFlags); |
| 711 | parcel->writeInt32(mEdgeFlags); |
| 712 | parcel->writeInt32(mMetaState); |
| 713 | parcel->writeInt32(mButtonState); |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 714 | parcel->writeByte(static_cast<int8_t>(mClassification)); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 715 | |
| 716 | status_t result = android::writeToParcel(mTransform, *parcel); |
| 717 | if (result != OK) { |
| 718 | return result; |
| 719 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 720 | parcel->writeFloat(mXPrecision); |
| 721 | parcel->writeFloat(mYPrecision); |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 722 | parcel->writeFloat(mRawXCursorPosition); |
| 723 | parcel->writeFloat(mRawYCursorPosition); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 724 | parcel->writeInt32(mDisplayWidth); |
| 725 | parcel->writeInt32(mDisplayHeight); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 726 | parcel->writeInt64(mDownTime); |
| 727 | |
| 728 | for (size_t i = 0; i < pointerCount; i++) { |
| 729 | const PointerProperties& properties = mPointerProperties.itemAt(i); |
| 730 | parcel->writeInt32(properties.id); |
| 731 | parcel->writeInt32(properties.toolType); |
| 732 | } |
| 733 | |
| 734 | const PointerCoords* pc = mSamplePointerCoords.array(); |
| 735 | for (size_t h = 0; h < sampleCount; h++) { |
Siarhei Vishniakou | 46a2774 | 2020-09-09 13:57:28 -0500 | [diff] [blame] | 736 | parcel->writeInt64(mSampleEventTimes[h]); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 737 | for (size_t i = 0; i < pointerCount; i++) { |
| 738 | status_t status = (pc++)->writeToParcel(parcel); |
| 739 | if (status) { |
| 740 | return status; |
| 741 | } |
| 742 | } |
| 743 | } |
| 744 | return OK; |
| 745 | } |
Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 746 | #endif |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 747 | |
Siarhei Vishniakou | 3826d47 | 2020-01-27 10:44:40 -0600 | [diff] [blame] | 748 | bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 749 | if (source & AINPUT_SOURCE_CLASS_POINTER) { |
| 750 | // Specifically excludes HOVER_MOVE and SCROLL. |
| 751 | switch (action & AMOTION_EVENT_ACTION_MASK) { |
| 752 | case AMOTION_EVENT_ACTION_DOWN: |
| 753 | case AMOTION_EVENT_ACTION_MOVE: |
| 754 | case AMOTION_EVENT_ACTION_UP: |
| 755 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 756 | case AMOTION_EVENT_ACTION_POINTER_UP: |
| 757 | case AMOTION_EVENT_ACTION_CANCEL: |
| 758 | case AMOTION_EVENT_ACTION_OUTSIDE: |
| 759 | return true; |
| 760 | } |
| 761 | } |
| 762 | return false; |
| 763 | } |
| 764 | |
Michael Wright | 872db4f | 2014-04-22 15:03:51 -0700 | [diff] [blame] | 765 | const char* MotionEvent::getLabel(int32_t axis) { |
Chris Ye | 4958d06 | 2020-08-20 13:21:10 -0700 | [diff] [blame] | 766 | return InputEventLookup::getAxisLabel(axis); |
Michael Wright | 872db4f | 2014-04-22 15:03:51 -0700 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | int32_t MotionEvent::getAxisFromLabel(const char* label) { |
Chris Ye | 4958d06 | 2020-08-20 13:21:10 -0700 | [diff] [blame] | 770 | return InputEventLookup::getAxisByLabel(label); |
Michael Wright | 872db4f | 2014-04-22 15:03:51 -0700 | [diff] [blame] | 771 | } |
| 772 | |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame] | 773 | std::string MotionEvent::actionToString(int32_t action) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 774 | // Convert MotionEvent action to string |
| 775 | switch (action & AMOTION_EVENT_ACTION_MASK) { |
| 776 | case AMOTION_EVENT_ACTION_DOWN: |
| 777 | return "DOWN"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 778 | case AMOTION_EVENT_ACTION_UP: |
| 779 | return "UP"; |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame] | 780 | case AMOTION_EVENT_ACTION_MOVE: |
| 781 | return "MOVE"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 782 | case AMOTION_EVENT_ACTION_CANCEL: |
| 783 | return "CANCEL"; |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame] | 784 | case AMOTION_EVENT_ACTION_OUTSIDE: |
| 785 | return "OUTSIDE"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 786 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 787 | return "POINTER_DOWN"; |
| 788 | case AMOTION_EVENT_ACTION_POINTER_UP: |
| 789 | return "POINTER_UP"; |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame] | 790 | case AMOTION_EVENT_ACTION_HOVER_MOVE: |
| 791 | return "HOVER_MOVE"; |
| 792 | case AMOTION_EVENT_ACTION_SCROLL: |
| 793 | return "SCROLL"; |
| 794 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 795 | return "HOVER_ENTER"; |
| 796 | case AMOTION_EVENT_ACTION_HOVER_EXIT: |
| 797 | return "HOVER_EXIT"; |
| 798 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: |
| 799 | return "BUTTON_PRESS"; |
| 800 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: |
| 801 | return "BUTTON_RELEASE"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 802 | } |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame] | 803 | return android::base::StringPrintf("%" PRId32, action); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 804 | } |
| 805 | |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 806 | // --- FocusEvent --- |
| 807 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 808 | void FocusEvent::initialize(int32_t id, bool hasFocus, bool inTouchMode) { |
| 809 | InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 810 | ADISPLAY_ID_NONE, INVALID_HMAC); |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 811 | mHasFocus = hasFocus; |
| 812 | mInTouchMode = inTouchMode; |
| 813 | } |
| 814 | |
| 815 | void FocusEvent::initialize(const FocusEvent& from) { |
| 816 | InputEvent::initialize(from); |
| 817 | mHasFocus = from.mHasFocus; |
| 818 | mInTouchMode = from.mInTouchMode; |
| 819 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 820 | |
Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 821 | // --- CaptureEvent --- |
| 822 | |
| 823 | void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) { |
| 824 | InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN, |
| 825 | ADISPLAY_ID_NONE, INVALID_HMAC); |
| 826 | mPointerCaptureEnabled = pointerCaptureEnabled; |
| 827 | } |
| 828 | |
| 829 | void CaptureEvent::initialize(const CaptureEvent& from) { |
| 830 | InputEvent::initialize(from); |
| 831 | mPointerCaptureEnabled = from.mPointerCaptureEnabled; |
| 832 | } |
| 833 | |
arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 834 | // --- DragEvent --- |
| 835 | |
| 836 | void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) { |
| 837 | InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN, |
| 838 | ADISPLAY_ID_NONE, INVALID_HMAC); |
| 839 | mIsExiting = isExiting; |
| 840 | mX = x; |
| 841 | mY = y; |
| 842 | } |
| 843 | |
| 844 | void DragEvent::initialize(const DragEvent& from) { |
| 845 | InputEvent::initialize(from); |
| 846 | mIsExiting = from.mIsExiting; |
| 847 | mX = from.mX; |
| 848 | mY = from.mY; |
| 849 | } |
| 850 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 851 | // --- PooledInputEventFactory --- |
| 852 | |
| 853 | PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) : |
| 854 | mMaxPoolSize(maxPoolSize) { |
| 855 | } |
| 856 | |
| 857 | PooledInputEventFactory::~PooledInputEventFactory() { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | KeyEvent* PooledInputEventFactory::createKeyEvent() { |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 861 | if (mKeyEventPool.empty()) { |
| 862 | return new KeyEvent(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 863 | } |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 864 | KeyEvent* event = mKeyEventPool.front().release(); |
| 865 | mKeyEventPool.pop(); |
| 866 | return event; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | MotionEvent* PooledInputEventFactory::createMotionEvent() { |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 870 | if (mMotionEventPool.empty()) { |
| 871 | return new MotionEvent(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 872 | } |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 873 | MotionEvent* event = mMotionEventPool.front().release(); |
| 874 | mMotionEventPool.pop(); |
| 875 | return event; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 876 | } |
| 877 | |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 878 | FocusEvent* PooledInputEventFactory::createFocusEvent() { |
| 879 | if (mFocusEventPool.empty()) { |
| 880 | return new FocusEvent(); |
| 881 | } |
| 882 | FocusEvent* event = mFocusEventPool.front().release(); |
| 883 | mFocusEventPool.pop(); |
| 884 | return event; |
| 885 | } |
| 886 | |
Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 887 | CaptureEvent* PooledInputEventFactory::createCaptureEvent() { |
| 888 | if (mCaptureEventPool.empty()) { |
| 889 | return new CaptureEvent(); |
| 890 | } |
| 891 | CaptureEvent* event = mCaptureEventPool.front().release(); |
| 892 | mCaptureEventPool.pop(); |
| 893 | return event; |
| 894 | } |
| 895 | |
arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 896 | DragEvent* PooledInputEventFactory::createDragEvent() { |
| 897 | if (mDragEventPool.empty()) { |
| 898 | return new DragEvent(); |
| 899 | } |
| 900 | DragEvent* event = mDragEventPool.front().release(); |
| 901 | mDragEventPool.pop(); |
| 902 | return event; |
| 903 | } |
| 904 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 905 | void PooledInputEventFactory::recycle(InputEvent* event) { |
| 906 | switch (event->getType()) { |
| 907 | case AINPUT_EVENT_TYPE_KEY: |
| 908 | if (mKeyEventPool.size() < mMaxPoolSize) { |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 909 | mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event))); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 910 | return; |
| 911 | } |
| 912 | break; |
| 913 | case AINPUT_EVENT_TYPE_MOTION: |
| 914 | if (mMotionEventPool.size() < mMaxPoolSize) { |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 915 | mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event))); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 916 | return; |
| 917 | } |
| 918 | break; |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 919 | case AINPUT_EVENT_TYPE_FOCUS: |
| 920 | if (mFocusEventPool.size() < mMaxPoolSize) { |
| 921 | mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event))); |
| 922 | return; |
| 923 | } |
| 924 | break; |
Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 925 | case AINPUT_EVENT_TYPE_CAPTURE: |
| 926 | if (mCaptureEventPool.size() < mMaxPoolSize) { |
| 927 | mCaptureEventPool.push( |
| 928 | std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event))); |
| 929 | return; |
| 930 | } |
| 931 | break; |
arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 932 | case AINPUT_EVENT_TYPE_DRAG: |
| 933 | if (mDragEventPool.size() < mMaxPoolSize) { |
| 934 | mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event))); |
| 935 | return; |
| 936 | } |
| 937 | break; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 938 | } |
| 939 | delete event; |
| 940 | } |
| 941 | |
| 942 | } // namespace android |