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