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