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); |
Prabir Pradhan | c652358 | 2021-05-14 18:02:55 -0700 | [diff] [blame] | 321 | scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_X, windowXScale); |
| 322 | scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_Y, windowYScale); |
Robert Carr | e07e103 | 2018-11-26 12:55:53 -0800 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | void PointerCoords::scale(float globalScaleFactor) { |
| 326 | scale(globalScaleFactor, globalScaleFactor, globalScaleFactor); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 329 | void PointerCoords::applyOffset(float xOffset, float yOffset) { |
| 330 | setAxisValue(AMOTION_EVENT_AXIS_X, getX() + xOffset); |
| 331 | setAxisValue(AMOTION_EVENT_AXIS_Y, getY() + yOffset); |
| 332 | } |
| 333 | |
Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 334 | #ifdef __linux__ |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 335 | status_t PointerCoords::readFromParcel(Parcel* parcel) { |
| 336 | bits = parcel->readInt64(); |
| 337 | |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 338 | uint32_t count = BitSet64::count(bits); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 339 | if (count > MAX_AXES) { |
| 340 | return BAD_VALUE; |
| 341 | } |
| 342 | |
| 343 | for (uint32_t i = 0; i < count; i++) { |
| 344 | values[i] = parcel->readFloat(); |
| 345 | } |
| 346 | return OK; |
| 347 | } |
| 348 | |
| 349 | status_t PointerCoords::writeToParcel(Parcel* parcel) const { |
| 350 | parcel->writeInt64(bits); |
| 351 | |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 352 | uint32_t count = BitSet64::count(bits); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 353 | for (uint32_t i = 0; i < count; i++) { |
| 354 | parcel->writeFloat(values[i]); |
| 355 | } |
| 356 | return OK; |
| 357 | } |
Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 358 | #endif |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 359 | |
| 360 | void PointerCoords::tooManyAxes(int axis) { |
| 361 | ALOGW("Could not set value for axis %d because the PointerCoords structure is full and " |
| 362 | "cannot contain more than %d axis values.", axis, int(MAX_AXES)); |
| 363 | } |
| 364 | |
| 365 | bool PointerCoords::operator==(const PointerCoords& other) const { |
| 366 | if (bits != other.bits) { |
| 367 | return false; |
| 368 | } |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 369 | uint32_t count = BitSet64::count(bits); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 370 | for (uint32_t i = 0; i < count; i++) { |
| 371 | if (values[i] != other.values[i]) { |
| 372 | return false; |
| 373 | } |
| 374 | } |
| 375 | return true; |
| 376 | } |
| 377 | |
| 378 | void PointerCoords::copyFrom(const PointerCoords& other) { |
| 379 | bits = other.bits; |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 380 | uint32_t count = BitSet64::count(bits); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 381 | for (uint32_t i = 0; i < count; i++) { |
| 382 | values[i] = other.values[i]; |
| 383 | } |
| 384 | } |
| 385 | |
chaviw | c01e137 | 2020-07-01 12:37:31 -0700 | [diff] [blame] | 386 | void PointerCoords::transform(const ui::Transform& transform) { |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 387 | const vec2 xy = transform.transform(getXYValue()); |
| 388 | setAxisValue(AMOTION_EVENT_AXIS_X, xy.x); |
| 389 | setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y); |
| 390 | |
Prabir Pradhan | c652358 | 2021-05-14 18:02:55 -0700 | [diff] [blame] | 391 | if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_X) || |
| 392 | BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_Y)) { |
| 393 | const ui::Transform rotation(transform.getOrientation()); |
| 394 | const vec2 relativeXy = rotation.transform(getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X), |
| 395 | getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)); |
| 396 | setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x); |
| 397 | setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y); |
| 398 | } |
| 399 | |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 400 | if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) { |
| 401 | const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION); |
| 402 | setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val)); |
| 403 | } |
chaviw | c01e137 | 2020-07-01 12:37:31 -0700 | [diff] [blame] | 404 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 405 | |
| 406 | // --- PointerProperties --- |
| 407 | |
| 408 | bool PointerProperties::operator==(const PointerProperties& other) const { |
| 409 | return id == other.id |
| 410 | && toolType == other.toolType; |
| 411 | } |
| 412 | |
| 413 | void PointerProperties::copyFrom(const PointerProperties& other) { |
| 414 | id = other.id; |
| 415 | toolType = other.toolType; |
| 416 | } |
| 417 | |
| 418 | |
| 419 | // --- MotionEvent --- |
| 420 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 421 | 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] | 422 | std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton, |
| 423 | int32_t flags, int32_t edgeFlags, int32_t metaState, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 424 | int32_t buttonState, MotionClassification classification, |
| 425 | const ui::Transform& transform, float xPrecision, float yPrecision, |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 426 | float rawXCursorPosition, float rawYCursorPosition, |
| 427 | int32_t displayWidth, int32_t displayHeight, nsecs_t downTime, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 428 | nsecs_t eventTime, size_t pointerCount, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 429 | const PointerProperties* pointerProperties, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 430 | const PointerCoords* pointerCoords) { |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 431 | InputEvent::initialize(id, deviceId, source, displayId, hmac); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 432 | mAction = action; |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 433 | mActionButton = actionButton; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 434 | mFlags = flags; |
| 435 | mEdgeFlags = edgeFlags; |
| 436 | mMetaState = metaState; |
| 437 | mButtonState = buttonState; |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 438 | mClassification = classification; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 439 | mTransform = transform; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 440 | mXPrecision = xPrecision; |
| 441 | mYPrecision = yPrecision; |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 442 | mRawXCursorPosition = rawXCursorPosition; |
| 443 | mRawYCursorPosition = rawYCursorPosition; |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 444 | mDisplayWidth = displayWidth; |
| 445 | mDisplayHeight = displayHeight; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 446 | mDownTime = downTime; |
| 447 | mPointerProperties.clear(); |
| 448 | mPointerProperties.appendArray(pointerProperties, pointerCount); |
| 449 | mSampleEventTimes.clear(); |
| 450 | mSamplePointerCoords.clear(); |
| 451 | addSample(eventTime, pointerCoords); |
| 452 | } |
| 453 | |
| 454 | void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) { |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 455 | InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId, |
| 456 | other->mHmac); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 457 | mAction = other->mAction; |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 458 | mActionButton = other->mActionButton; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 459 | mFlags = other->mFlags; |
| 460 | mEdgeFlags = other->mEdgeFlags; |
| 461 | mMetaState = other->mMetaState; |
| 462 | mButtonState = other->mButtonState; |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 463 | mClassification = other->mClassification; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 464 | mTransform = other->mTransform; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 465 | mXPrecision = other->mXPrecision; |
| 466 | mYPrecision = other->mYPrecision; |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 467 | mRawXCursorPosition = other->mRawXCursorPosition; |
| 468 | mRawYCursorPosition = other->mRawYCursorPosition; |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 469 | mDisplayWidth = other->mDisplayWidth; |
| 470 | mDisplayHeight = other->mDisplayHeight; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 471 | mDownTime = other->mDownTime; |
| 472 | mPointerProperties = other->mPointerProperties; |
| 473 | |
| 474 | if (keepHistory) { |
| 475 | mSampleEventTimes = other->mSampleEventTimes; |
| 476 | mSamplePointerCoords = other->mSamplePointerCoords; |
| 477 | } else { |
| 478 | mSampleEventTimes.clear(); |
Siarhei Vishniakou | 46a2774 | 2020-09-09 13:57:28 -0500 | [diff] [blame] | 479 | mSampleEventTimes.push_back(other->getEventTime()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 480 | mSamplePointerCoords.clear(); |
| 481 | size_t pointerCount = other->getPointerCount(); |
| 482 | size_t historySize = other->getHistorySize(); |
| 483 | mSamplePointerCoords.appendArray(other->mSamplePointerCoords.array() |
| 484 | + (historySize * pointerCount), pointerCount); |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | void MotionEvent::addSample( |
| 489 | int64_t eventTime, |
| 490 | const PointerCoords* pointerCoords) { |
Siarhei Vishniakou | 46a2774 | 2020-09-09 13:57:28 -0500 | [diff] [blame] | 491 | mSampleEventTimes.push_back(eventTime); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 492 | mSamplePointerCoords.appendArray(pointerCoords, getPointerCount()); |
| 493 | } |
| 494 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 495 | float MotionEvent::getXCursorPosition() const { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 496 | vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition()); |
| 497 | return vals.x; |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | float MotionEvent::getYCursorPosition() const { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 501 | vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition()); |
| 502 | return vals.y; |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 503 | } |
| 504 | |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 505 | void MotionEvent::setCursorPosition(float x, float y) { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 506 | ui::Transform inverse = mTransform.inverse(); |
| 507 | vec2 vals = inverse.transform(x, y); |
| 508 | mRawXCursorPosition = vals.x; |
| 509 | mRawYCursorPosition = vals.y; |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 510 | } |
| 511 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 512 | const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const { |
| 513 | return &mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex]; |
| 514 | } |
| 515 | |
| 516 | float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const { |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 517 | return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 521 | return getHistoricalAxisValue(axis, pointerIndex, getHistorySize()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | const PointerCoords* MotionEvent::getHistoricalRawPointerCoords( |
| 525 | size_t pointerIndex, size_t historicalIndex) const { |
| 526 | return &mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex]; |
| 527 | } |
| 528 | |
| 529 | float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex, |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 530 | size_t historicalIndex) const { |
| 531 | const PointerCoords* coords = getHistoricalRawPointerCoords(pointerIndex, historicalIndex); |
| 532 | |
| 533 | if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) { |
| 534 | // For compatibility, convert raw coordinates into "oriented screen space". Once app |
| 535 | // developers are educated about getRaw, we can consider removing this. |
Prabir Pradhan | 9f38881 | 2021-05-13 16:54:53 -0700 | [diff] [blame] | 536 | const vec2 xy = shouldDisregardWindowTranslation(mSource) |
| 537 | ? rotatePoint(mTransform, coords->getX(), coords->getY()) |
| 538 | : rotatePoint(mTransform, coords->getX(), coords->getY(), mDisplayWidth, |
| 539 | mDisplayHeight); |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 540 | static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1); |
| 541 | return xy[axis]; |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 542 | } |
| 543 | |
Prabir Pradhan | c652358 | 2021-05-14 18:02:55 -0700 | [diff] [blame] | 544 | if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) { |
| 545 | // For compatibility, since we convert raw coordinates into "oriented screen space", we |
| 546 | // need to convert the relative axes into the same orientation for consistency. |
| 547 | const vec2 relativeXy = |
| 548 | rotatePoint(mTransform, coords->getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X), |
| 549 | coords->getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)); |
| 550 | return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y; |
| 551 | } |
| 552 | |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 553 | return coords->getAxisValue(axis); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex, |
Prabir Pradhan | 9f38881 | 2021-05-13 16:54:53 -0700 | [diff] [blame] | 557 | size_t historicalIndex) const { |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 558 | const PointerCoords* coords = getHistoricalRawPointerCoords(pointerIndex, historicalIndex); |
| 559 | |
| 560 | if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) { |
Prabir Pradhan | 9f38881 | 2021-05-13 16:54:53 -0700 | [diff] [blame] | 561 | const vec2 xy = shouldDisregardWindowTranslation(mSource) |
| 562 | ? applyTransformWithoutTranslation(mTransform, coords->getX(), coords->getY()) |
| 563 | : mTransform.transform(coords->getXYValue()); |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 564 | static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1); |
| 565 | return xy[axis]; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 566 | } |
| 567 | |
Prabir Pradhan | c652358 | 2021-05-14 18:02:55 -0700 | [diff] [blame] | 568 | if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) { |
| 569 | const vec2 relativeXy = |
| 570 | applyTransformWithoutTranslation(mTransform, |
| 571 | coords->getAxisValue( |
| 572 | AMOTION_EVENT_AXIS_RELATIVE_X), |
| 573 | coords->getAxisValue( |
| 574 | AMOTION_EVENT_AXIS_RELATIVE_Y)); |
| 575 | return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y; |
| 576 | } |
| 577 | |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 578 | return coords->getAxisValue(axis); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const { |
| 582 | size_t pointerCount = mPointerProperties.size(); |
| 583 | for (size_t i = 0; i < pointerCount; i++) { |
| 584 | if (mPointerProperties.itemAt(i).id == pointerId) { |
| 585 | return i; |
| 586 | } |
| 587 | } |
| 588 | return -1; |
| 589 | } |
| 590 | |
| 591 | void MotionEvent::offsetLocation(float xOffset, float yOffset) { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 592 | float currXOffset = mTransform.tx(); |
| 593 | float currYOffset = mTransform.ty(); |
| 594 | mTransform.set(currXOffset + xOffset, currYOffset + yOffset); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 595 | } |
| 596 | |
Robert Carr | e07e103 | 2018-11-26 12:55:53 -0800 | [diff] [blame] | 597 | void MotionEvent::scale(float globalScaleFactor) { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 598 | mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor); |
Robert Carr | e07e103 | 2018-11-26 12:55:53 -0800 | [diff] [blame] | 599 | mXPrecision *= globalScaleFactor; |
| 600 | mYPrecision *= globalScaleFactor; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 601 | |
| 602 | size_t numSamples = mSamplePointerCoords.size(); |
| 603 | for (size_t i = 0; i < numSamples; i++) { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 604 | mSamplePointerCoords.editItemAt(i).scale(globalScaleFactor, globalScaleFactor, |
| 605 | globalScaleFactor); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 606 | } |
| 607 | } |
| 608 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 609 | void MotionEvent::transform(const std::array<float, 9>& matrix) { |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 610 | // We want to preserve the raw axes values stored in the PointerCoords, so we just update the |
| 611 | // transform using the values passed in. |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 612 | ui::Transform newTransform; |
| 613 | newTransform.set(matrix); |
| 614 | mTransform = newTransform * mTransform; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 615 | |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 616 | // We need to update the AXIS_ORIENTATION value here to maintain the old behavior where the |
| 617 | // orientation angle is not affected by the initial transformation set in the MotionEvent. |
| 618 | std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(), |
| 619 | [&newTransform](PointerCoords& c) { |
| 620 | float orientation = c.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION); |
| 621 | c.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, |
| 622 | transformAngle(newTransform, orientation)); |
| 623 | }); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 624 | } |
| 625 | |
Evan Rosky | d4d4d80 | 2021-05-03 20:12:21 -0700 | [diff] [blame] | 626 | void MotionEvent::applyTransform(const std::array<float, 9>& matrix) { |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 627 | ui::Transform transform; |
| 628 | transform.set(matrix); |
Evan Rosky | d4d4d80 | 2021-05-03 20:12:21 -0700 | [diff] [blame] | 629 | |
| 630 | // Apply the transformation to all samples. |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 631 | std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(), |
| 632 | [&transform](PointerCoords& c) { c.transform(transform); }); |
Prabir Pradhan | 4b19bd0 | 2021-06-01 17:34:59 -0700 | [diff] [blame^] | 633 | |
| 634 | if (mRawXCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION && |
| 635 | mRawYCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) { |
| 636 | const vec2 cursor = transform.transform(mRawXCursorPosition, mRawYCursorPosition); |
| 637 | mRawXCursorPosition = cursor.x; |
| 638 | mRawYCursorPosition = cursor.y; |
| 639 | } |
Evan Rosky | d4d4d80 | 2021-05-03 20:12:21 -0700 | [diff] [blame] | 640 | } |
| 641 | |
Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 642 | #ifdef __linux__ |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 643 | static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) { |
| 644 | float dsdx, dtdx, tx, dtdy, dsdy, ty; |
| 645 | status_t status = parcel.readFloat(&dsdx); |
| 646 | status |= parcel.readFloat(&dtdx); |
| 647 | status |= parcel.readFloat(&tx); |
| 648 | status |= parcel.readFloat(&dtdy); |
| 649 | status |= parcel.readFloat(&dsdy); |
| 650 | status |= parcel.readFloat(&ty); |
| 651 | |
| 652 | transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1}); |
| 653 | return status; |
| 654 | } |
| 655 | |
| 656 | static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) { |
| 657 | status_t status = parcel.writeFloat(transform.dsdx()); |
| 658 | status |= parcel.writeFloat(transform.dtdx()); |
| 659 | status |= parcel.writeFloat(transform.tx()); |
| 660 | status |= parcel.writeFloat(transform.dtdy()); |
| 661 | status |= parcel.writeFloat(transform.dsdy()); |
| 662 | status |= parcel.writeFloat(transform.ty()); |
| 663 | return status; |
| 664 | } |
| 665 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 666 | status_t MotionEvent::readFromParcel(Parcel* parcel) { |
| 667 | size_t pointerCount = parcel->readInt32(); |
| 668 | size_t sampleCount = parcel->readInt32(); |
Flanker | 552a8a5 | 2015-09-07 15:28:58 +0800 | [diff] [blame] | 669 | if (pointerCount == 0 || pointerCount > MAX_POINTERS || |
| 670 | sampleCount == 0 || sampleCount > MAX_SAMPLES) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 671 | return BAD_VALUE; |
| 672 | } |
| 673 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 674 | mId = parcel->readInt32(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 675 | mDeviceId = parcel->readInt32(); |
Siarhei Vishniakou | 3826d47 | 2020-01-27 10:44:40 -0600 | [diff] [blame] | 676 | mSource = parcel->readUint32(); |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 677 | mDisplayId = parcel->readInt32(); |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 678 | std::vector<uint8_t> hmac; |
| 679 | status_t result = parcel->readByteVector(&hmac); |
| 680 | if (result != OK || hmac.size() != 32) { |
| 681 | return BAD_VALUE; |
| 682 | } |
| 683 | std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 684 | mAction = parcel->readInt32(); |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 685 | mActionButton = parcel->readInt32(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 686 | mFlags = parcel->readInt32(); |
| 687 | mEdgeFlags = parcel->readInt32(); |
| 688 | mMetaState = parcel->readInt32(); |
| 689 | mButtonState = parcel->readInt32(); |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 690 | mClassification = static_cast<MotionClassification>(parcel->readByte()); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 691 | |
| 692 | result = android::readFromParcel(mTransform, *parcel); |
| 693 | if (result != OK) { |
| 694 | return result; |
| 695 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 696 | mXPrecision = parcel->readFloat(); |
| 697 | mYPrecision = parcel->readFloat(); |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 698 | mRawXCursorPosition = parcel->readFloat(); |
| 699 | mRawYCursorPosition = parcel->readFloat(); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 700 | mDisplayWidth = parcel->readInt32(); |
| 701 | mDisplayHeight = parcel->readInt32(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 702 | mDownTime = parcel->readInt64(); |
| 703 | |
| 704 | mPointerProperties.clear(); |
| 705 | mPointerProperties.setCapacity(pointerCount); |
| 706 | mSampleEventTimes.clear(); |
Siarhei Vishniakou | 46a2774 | 2020-09-09 13:57:28 -0500 | [diff] [blame] | 707 | mSampleEventTimes.reserve(sampleCount); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 708 | mSamplePointerCoords.clear(); |
| 709 | mSamplePointerCoords.setCapacity(sampleCount * pointerCount); |
| 710 | |
| 711 | for (size_t i = 0; i < pointerCount; i++) { |
| 712 | mPointerProperties.push(); |
| 713 | PointerProperties& properties = mPointerProperties.editTop(); |
| 714 | properties.id = parcel->readInt32(); |
| 715 | properties.toolType = parcel->readInt32(); |
| 716 | } |
| 717 | |
Dan Austin | c94fc45 | 2015-09-22 14:22:41 -0700 | [diff] [blame] | 718 | while (sampleCount > 0) { |
| 719 | sampleCount--; |
Siarhei Vishniakou | 46a2774 | 2020-09-09 13:57:28 -0500 | [diff] [blame] | 720 | mSampleEventTimes.push_back(parcel->readInt64()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 721 | for (size_t i = 0; i < pointerCount; i++) { |
| 722 | mSamplePointerCoords.push(); |
| 723 | status_t status = mSamplePointerCoords.editTop().readFromParcel(parcel); |
| 724 | if (status) { |
| 725 | return status; |
| 726 | } |
| 727 | } |
| 728 | } |
| 729 | return OK; |
| 730 | } |
| 731 | |
| 732 | status_t MotionEvent::writeToParcel(Parcel* parcel) const { |
| 733 | size_t pointerCount = mPointerProperties.size(); |
| 734 | size_t sampleCount = mSampleEventTimes.size(); |
| 735 | |
| 736 | parcel->writeInt32(pointerCount); |
| 737 | parcel->writeInt32(sampleCount); |
| 738 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 739 | parcel->writeInt32(mId); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 740 | parcel->writeInt32(mDeviceId); |
Siarhei Vishniakou | 3826d47 | 2020-01-27 10:44:40 -0600 | [diff] [blame] | 741 | parcel->writeUint32(mSource); |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 742 | parcel->writeInt32(mDisplayId); |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 743 | std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end()); |
| 744 | parcel->writeByteVector(hmac); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 745 | parcel->writeInt32(mAction); |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 746 | parcel->writeInt32(mActionButton); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 747 | parcel->writeInt32(mFlags); |
| 748 | parcel->writeInt32(mEdgeFlags); |
| 749 | parcel->writeInt32(mMetaState); |
| 750 | parcel->writeInt32(mButtonState); |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 751 | parcel->writeByte(static_cast<int8_t>(mClassification)); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 752 | |
| 753 | status_t result = android::writeToParcel(mTransform, *parcel); |
| 754 | if (result != OK) { |
| 755 | return result; |
| 756 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 757 | parcel->writeFloat(mXPrecision); |
| 758 | parcel->writeFloat(mYPrecision); |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 759 | parcel->writeFloat(mRawXCursorPosition); |
| 760 | parcel->writeFloat(mRawYCursorPosition); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 761 | parcel->writeInt32(mDisplayWidth); |
| 762 | parcel->writeInt32(mDisplayHeight); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 763 | parcel->writeInt64(mDownTime); |
| 764 | |
| 765 | for (size_t i = 0; i < pointerCount; i++) { |
| 766 | const PointerProperties& properties = mPointerProperties.itemAt(i); |
| 767 | parcel->writeInt32(properties.id); |
| 768 | parcel->writeInt32(properties.toolType); |
| 769 | } |
| 770 | |
| 771 | const PointerCoords* pc = mSamplePointerCoords.array(); |
| 772 | for (size_t h = 0; h < sampleCount; h++) { |
Siarhei Vishniakou | 46a2774 | 2020-09-09 13:57:28 -0500 | [diff] [blame] | 773 | parcel->writeInt64(mSampleEventTimes[h]); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 774 | for (size_t i = 0; i < pointerCount; i++) { |
| 775 | status_t status = (pc++)->writeToParcel(parcel); |
| 776 | if (status) { |
| 777 | return status; |
| 778 | } |
| 779 | } |
| 780 | } |
| 781 | return OK; |
| 782 | } |
Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 783 | #endif |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 784 | |
Siarhei Vishniakou | 3826d47 | 2020-01-27 10:44:40 -0600 | [diff] [blame] | 785 | bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 786 | if (source & AINPUT_SOURCE_CLASS_POINTER) { |
| 787 | // Specifically excludes HOVER_MOVE and SCROLL. |
| 788 | switch (action & AMOTION_EVENT_ACTION_MASK) { |
| 789 | case AMOTION_EVENT_ACTION_DOWN: |
| 790 | case AMOTION_EVENT_ACTION_MOVE: |
| 791 | case AMOTION_EVENT_ACTION_UP: |
| 792 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 793 | case AMOTION_EVENT_ACTION_POINTER_UP: |
| 794 | case AMOTION_EVENT_ACTION_CANCEL: |
| 795 | case AMOTION_EVENT_ACTION_OUTSIDE: |
| 796 | return true; |
| 797 | } |
| 798 | } |
| 799 | return false; |
| 800 | } |
| 801 | |
Michael Wright | 872db4f | 2014-04-22 15:03:51 -0700 | [diff] [blame] | 802 | const char* MotionEvent::getLabel(int32_t axis) { |
Chris Ye | 4958d06 | 2020-08-20 13:21:10 -0700 | [diff] [blame] | 803 | return InputEventLookup::getAxisLabel(axis); |
Michael Wright | 872db4f | 2014-04-22 15:03:51 -0700 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | int32_t MotionEvent::getAxisFromLabel(const char* label) { |
Chris Ye | 4958d06 | 2020-08-20 13:21:10 -0700 | [diff] [blame] | 807 | return InputEventLookup::getAxisByLabel(label); |
Michael Wright | 872db4f | 2014-04-22 15:03:51 -0700 | [diff] [blame] | 808 | } |
| 809 | |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame] | 810 | std::string MotionEvent::actionToString(int32_t action) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 811 | // Convert MotionEvent action to string |
| 812 | switch (action & AMOTION_EVENT_ACTION_MASK) { |
| 813 | case AMOTION_EVENT_ACTION_DOWN: |
| 814 | return "DOWN"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 815 | case AMOTION_EVENT_ACTION_UP: |
| 816 | return "UP"; |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame] | 817 | case AMOTION_EVENT_ACTION_MOVE: |
| 818 | return "MOVE"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 819 | case AMOTION_EVENT_ACTION_CANCEL: |
| 820 | return "CANCEL"; |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame] | 821 | case AMOTION_EVENT_ACTION_OUTSIDE: |
| 822 | return "OUTSIDE"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 823 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 824 | return "POINTER_DOWN"; |
| 825 | case AMOTION_EVENT_ACTION_POINTER_UP: |
| 826 | return "POINTER_UP"; |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame] | 827 | case AMOTION_EVENT_ACTION_HOVER_MOVE: |
| 828 | return "HOVER_MOVE"; |
| 829 | case AMOTION_EVENT_ACTION_SCROLL: |
| 830 | return "SCROLL"; |
| 831 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 832 | return "HOVER_ENTER"; |
| 833 | case AMOTION_EVENT_ACTION_HOVER_EXIT: |
| 834 | return "HOVER_EXIT"; |
| 835 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: |
| 836 | return "BUTTON_PRESS"; |
| 837 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: |
| 838 | return "BUTTON_RELEASE"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 839 | } |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame] | 840 | return android::base::StringPrintf("%" PRId32, action); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 841 | } |
| 842 | |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 843 | // --- FocusEvent --- |
| 844 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 845 | void FocusEvent::initialize(int32_t id, bool hasFocus, bool inTouchMode) { |
| 846 | InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 847 | ADISPLAY_ID_NONE, INVALID_HMAC); |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 848 | mHasFocus = hasFocus; |
| 849 | mInTouchMode = inTouchMode; |
| 850 | } |
| 851 | |
| 852 | void FocusEvent::initialize(const FocusEvent& from) { |
| 853 | InputEvent::initialize(from); |
| 854 | mHasFocus = from.mHasFocus; |
| 855 | mInTouchMode = from.mInTouchMode; |
| 856 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 857 | |
Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 858 | // --- CaptureEvent --- |
| 859 | |
| 860 | void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) { |
| 861 | InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN, |
| 862 | ADISPLAY_ID_NONE, INVALID_HMAC); |
| 863 | mPointerCaptureEnabled = pointerCaptureEnabled; |
| 864 | } |
| 865 | |
| 866 | void CaptureEvent::initialize(const CaptureEvent& from) { |
| 867 | InputEvent::initialize(from); |
| 868 | mPointerCaptureEnabled = from.mPointerCaptureEnabled; |
| 869 | } |
| 870 | |
arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 871 | // --- DragEvent --- |
| 872 | |
| 873 | void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) { |
| 874 | InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN, |
| 875 | ADISPLAY_ID_NONE, INVALID_HMAC); |
| 876 | mIsExiting = isExiting; |
| 877 | mX = x; |
| 878 | mY = y; |
| 879 | } |
| 880 | |
| 881 | void DragEvent::initialize(const DragEvent& from) { |
| 882 | InputEvent::initialize(from); |
| 883 | mIsExiting = from.mIsExiting; |
| 884 | mX = from.mX; |
| 885 | mY = from.mY; |
| 886 | } |
| 887 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 888 | // --- PooledInputEventFactory --- |
| 889 | |
| 890 | PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) : |
| 891 | mMaxPoolSize(maxPoolSize) { |
| 892 | } |
| 893 | |
| 894 | PooledInputEventFactory::~PooledInputEventFactory() { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | KeyEvent* PooledInputEventFactory::createKeyEvent() { |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 898 | if (mKeyEventPool.empty()) { |
| 899 | return new KeyEvent(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 900 | } |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 901 | KeyEvent* event = mKeyEventPool.front().release(); |
| 902 | mKeyEventPool.pop(); |
| 903 | return event; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | MotionEvent* PooledInputEventFactory::createMotionEvent() { |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 907 | if (mMotionEventPool.empty()) { |
| 908 | return new MotionEvent(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 909 | } |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 910 | MotionEvent* event = mMotionEventPool.front().release(); |
| 911 | mMotionEventPool.pop(); |
| 912 | return event; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 913 | } |
| 914 | |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 915 | FocusEvent* PooledInputEventFactory::createFocusEvent() { |
| 916 | if (mFocusEventPool.empty()) { |
| 917 | return new FocusEvent(); |
| 918 | } |
| 919 | FocusEvent* event = mFocusEventPool.front().release(); |
| 920 | mFocusEventPool.pop(); |
| 921 | return event; |
| 922 | } |
| 923 | |
Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 924 | CaptureEvent* PooledInputEventFactory::createCaptureEvent() { |
| 925 | if (mCaptureEventPool.empty()) { |
| 926 | return new CaptureEvent(); |
| 927 | } |
| 928 | CaptureEvent* event = mCaptureEventPool.front().release(); |
| 929 | mCaptureEventPool.pop(); |
| 930 | return event; |
| 931 | } |
| 932 | |
arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 933 | DragEvent* PooledInputEventFactory::createDragEvent() { |
| 934 | if (mDragEventPool.empty()) { |
| 935 | return new DragEvent(); |
| 936 | } |
| 937 | DragEvent* event = mDragEventPool.front().release(); |
| 938 | mDragEventPool.pop(); |
| 939 | return event; |
| 940 | } |
| 941 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 942 | void PooledInputEventFactory::recycle(InputEvent* event) { |
| 943 | switch (event->getType()) { |
| 944 | case AINPUT_EVENT_TYPE_KEY: |
| 945 | if (mKeyEventPool.size() < mMaxPoolSize) { |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 946 | mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event))); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 947 | return; |
| 948 | } |
| 949 | break; |
| 950 | case AINPUT_EVENT_TYPE_MOTION: |
| 951 | if (mMotionEventPool.size() < mMaxPoolSize) { |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 952 | mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event))); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 953 | return; |
| 954 | } |
| 955 | break; |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 956 | case AINPUT_EVENT_TYPE_FOCUS: |
| 957 | if (mFocusEventPool.size() < mMaxPoolSize) { |
| 958 | mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event))); |
| 959 | return; |
| 960 | } |
| 961 | break; |
Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 962 | case AINPUT_EVENT_TYPE_CAPTURE: |
| 963 | if (mCaptureEventPool.size() < mMaxPoolSize) { |
| 964 | mCaptureEventPool.push( |
| 965 | std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event))); |
| 966 | return; |
| 967 | } |
| 968 | break; |
arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 969 | case AINPUT_EVENT_TYPE_DRAG: |
| 970 | if (mDragEventPool.size() < mMaxPoolSize) { |
| 971 | mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event))); |
| 972 | return; |
| 973 | } |
| 974 | break; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 975 | } |
| 976 | delete event; |
| 977 | } |
| 978 | |
| 979 | } // namespace android |