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