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