Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "Input" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 20 | #include <attestation/HmacKeyManager.h> |
Garfield Tan | 84b087e | 2020-01-23 10:49:05 -0800 | [diff] [blame] | 21 | #include <cutils/compiler.h> |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame] | 22 | #include <inttypes.h> |
Garfield Tan | 84b087e | 2020-01-23 10:49:05 -0800 | [diff] [blame] | 23 | #include <string.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 24 | |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame] | 25 | #include <android-base/stringprintf.h> |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 26 | #include <gui/constants.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 27 | #include <input/Input.h> |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 28 | #include <input/InputDevice.h> |
Michael Wright | 872db4f | 2014-04-22 15:03:51 -0700 | [diff] [blame] | 29 | #include <input/InputEventLabels.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 30 | |
Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 31 | #ifdef __linux__ |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 32 | #include <binder/Parcel.h> |
Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 33 | #endif |
Brett Chabot | 5820852 | 2020-09-09 13:55:24 -0700 | [diff] [blame] | 34 | #ifdef __ANDROID__ |
Garfield Tan | 84b087e | 2020-01-23 10:49:05 -0800 | [diff] [blame] | 35 | #include <sys/random.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 36 | #endif |
| 37 | |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame] | 38 | using android::base::StringPrintf; |
| 39 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 40 | namespace android { |
| 41 | |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 42 | namespace { |
| 43 | |
| 44 | float transformAngle(const ui::Transform& transform, float angleRadians) { |
| 45 | // Construct and transform a vector oriented at the specified clockwise angle from vertical. |
| 46 | // Coordinate system: down is increasing Y, right is increasing X. |
| 47 | float x = sinf(angleRadians); |
| 48 | float y = -cosf(angleRadians); |
| 49 | vec2 transformedPoint = transform.transform(x, y); |
| 50 | |
| 51 | // Determine how the origin is transformed by the matrix so that we |
| 52 | // can transform orientation vectors. |
| 53 | const vec2 origin = transform.transform(0, 0); |
| 54 | |
| 55 | transformedPoint.x -= origin.x; |
| 56 | transformedPoint.y -= origin.y; |
| 57 | |
| 58 | // Derive the transformed vector's clockwise angle from vertical. |
Prabir Pradhan | d2b0267 | 2021-10-19 11:24:45 -0700 | [diff] [blame] | 59 | // The return value of atan2f is in range [-pi, pi] which conforms to the orientation API. |
| 60 | return atan2f(transformedPoint.x, -transformedPoint.y); |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Prabir Pradhan | 259a212 | 2021-11-17 21:48:11 +0000 | [diff] [blame] | 63 | vec2 transformWithoutTranslation(const ui::Transform& transform, const vec2& xy) { |
| 64 | const vec2 transformedXy = transform.transform(xy); |
| 65 | const vec2 transformedOrigin = transform.transform(0, 0); |
| 66 | return transformedXy - transformedOrigin; |
| 67 | } |
| 68 | |
Prabir Pradhan | 7e1ee56 | 2021-10-26 10:19:49 -0700 | [diff] [blame] | 69 | bool shouldDisregardTransformation(uint32_t source) { |
| 70 | // Do not apply any transformations to axes from joysticks or touchpads. |
| 71 | return isFromSource(source, AINPUT_SOURCE_CLASS_JOYSTICK) || |
| 72 | isFromSource(source, AINPUT_SOURCE_CLASS_POSITION); |
| 73 | } |
| 74 | |
| 75 | bool shouldDisregardOffset(uint32_t source) { |
Prabir Pradhan | 9f38881 | 2021-05-13 16:54:53 -0700 | [diff] [blame] | 76 | // Pointer events are the only type of events that refer to absolute coordinates on the display, |
| 77 | // so we should apply the entire window transform. For other types of events, we should make |
| 78 | // sure to not apply the window translation/offset. |
Prabir Pradhan | 7e1ee56 | 2021-10-26 10:19:49 -0700 | [diff] [blame] | 79 | return !isFromSource(source, AINPUT_SOURCE_CLASS_POINTER); |
Prabir Pradhan | 9f38881 | 2021-05-13 16:54:53 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 82 | } // namespace |
| 83 | |
Siarhei Vishniakou | 16a2e30 | 2019-01-14 19:21:45 -0800 | [diff] [blame] | 84 | const char* motionClassificationToString(MotionClassification classification) { |
| 85 | switch (classification) { |
| 86 | case MotionClassification::NONE: |
| 87 | return "NONE"; |
| 88 | case MotionClassification::AMBIGUOUS_GESTURE: |
| 89 | return "AMBIGUOUS_GESTURE"; |
| 90 | case MotionClassification::DEEP_PRESS: |
| 91 | return "DEEP_PRESS"; |
| 92 | } |
| 93 | } |
| 94 | |
Garfield Tan | 84b087e | 2020-01-23 10:49:05 -0800 | [diff] [blame] | 95 | // --- IdGenerator --- |
| 96 | IdGenerator::IdGenerator(Source source) : mSource(source) {} |
| 97 | |
| 98 | int32_t IdGenerator::nextId() const { |
| 99 | constexpr uint32_t SEQUENCE_NUMBER_MASK = ~SOURCE_MASK; |
| 100 | int32_t id = 0; |
| 101 | |
| 102 | // Avoid building against syscall getrandom(2) on host, which will fail build on Mac. Host doesn't |
| 103 | // use sequence number so just always return mSource. |
| 104 | #ifdef __ANDROID__ |
| 105 | constexpr size_t BUF_LEN = sizeof(id); |
| 106 | size_t totalBytes = 0; |
| 107 | while (totalBytes < BUF_LEN) { |
| 108 | ssize_t bytes = TEMP_FAILURE_RETRY(getrandom(&id, BUF_LEN, GRND_NONBLOCK)); |
| 109 | if (CC_UNLIKELY(bytes < 0)) { |
| 110 | ALOGW("Failed to fill in random number for sequence number: %s.", strerror(errno)); |
| 111 | id = 0; |
| 112 | break; |
| 113 | } |
| 114 | totalBytes += bytes; |
| 115 | } |
| 116 | #endif // __ANDROID__ |
| 117 | |
| 118 | return (id & SEQUENCE_NUMBER_MASK) | static_cast<int32_t>(mSource); |
| 119 | } |
| 120 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 121 | // --- InputEvent --- |
| 122 | |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 123 | const char* inputEventTypeToString(int32_t type) { |
| 124 | switch (type) { |
| 125 | case AINPUT_EVENT_TYPE_KEY: { |
| 126 | return "KEY"; |
| 127 | } |
| 128 | case AINPUT_EVENT_TYPE_MOTION: { |
| 129 | return "MOTION"; |
| 130 | } |
| 131 | case AINPUT_EVENT_TYPE_FOCUS: { |
| 132 | return "FOCUS"; |
| 133 | } |
Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 134 | case AINPUT_EVENT_TYPE_CAPTURE: { |
| 135 | return "CAPTURE"; |
| 136 | } |
arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 137 | case AINPUT_EVENT_TYPE_DRAG: { |
| 138 | return "DRAG"; |
| 139 | } |
Antonio Kantek | 7cdf8ef | 2021-07-13 18:04:53 -0700 | [diff] [blame] | 140 | case AINPUT_EVENT_TYPE_TOUCH_MODE: { |
| 141 | return "TOUCH_MODE"; |
| 142 | } |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 143 | } |
| 144 | return "UNKNOWN"; |
| 145 | } |
| 146 | |
Siarhei Vishniakou | d948957 | 2021-11-12 20:08:38 -0800 | [diff] [blame] | 147 | std::string inputEventSourceToString(int32_t source) { |
| 148 | if (source == AINPUT_SOURCE_UNKNOWN) { |
| 149 | return "UNKNOWN"; |
| 150 | } |
| 151 | if (source == static_cast<int32_t>(AINPUT_SOURCE_ANY)) { |
| 152 | return "ANY"; |
| 153 | } |
| 154 | static const std::map<int32_t, const char*> SOURCES{ |
| 155 | {AINPUT_SOURCE_KEYBOARD, "KEYBOARD"}, |
| 156 | {AINPUT_SOURCE_DPAD, "DPAD"}, |
| 157 | {AINPUT_SOURCE_GAMEPAD, "GAMEPAD"}, |
| 158 | {AINPUT_SOURCE_TOUCHSCREEN, "TOUCHSCREEN"}, |
| 159 | {AINPUT_SOURCE_MOUSE, "MOUSE"}, |
| 160 | {AINPUT_SOURCE_STYLUS, "STYLUS"}, |
| 161 | {AINPUT_SOURCE_BLUETOOTH_STYLUS, "BLUETOOTH_STYLUS"}, |
| 162 | {AINPUT_SOURCE_TRACKBALL, "TRACKBALL"}, |
| 163 | {AINPUT_SOURCE_MOUSE_RELATIVE, "MOUSE_RELATIVE"}, |
| 164 | {AINPUT_SOURCE_TOUCHPAD, "TOUCHPAD"}, |
| 165 | {AINPUT_SOURCE_TOUCH_NAVIGATION, "TOUCH_NAVIGATION"}, |
| 166 | {AINPUT_SOURCE_JOYSTICK, "JOYSTICK"}, |
| 167 | {AINPUT_SOURCE_HDMI, "HDMI"}, |
| 168 | {AINPUT_SOURCE_SENSOR, "SENSOR"}, |
| 169 | {AINPUT_SOURCE_ROTARY_ENCODER, "ROTARY_ENCODER"}, |
| 170 | }; |
| 171 | std::string result; |
| 172 | for (const auto& [source_entry, str] : SOURCES) { |
| 173 | if ((source & source_entry) == source_entry) { |
| 174 | if (!result.empty()) { |
| 175 | result += " | "; |
| 176 | } |
| 177 | result += str; |
| 178 | } |
| 179 | } |
| 180 | if (result.empty()) { |
| 181 | result = StringPrintf("0x%08x", source); |
| 182 | } |
| 183 | return result; |
| 184 | } |
| 185 | |
| 186 | bool isFromSource(uint32_t source, uint32_t test) { |
| 187 | return (source & test) == test; |
| 188 | } |
| 189 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 190 | VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) { |
| 191 | return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(), |
| 192 | event.getSource(), event.getDisplayId()}, |
| 193 | event.getAction(), |
| 194 | event.getDownTime(), |
| 195 | event.getFlags() & VERIFIED_KEY_EVENT_FLAGS, |
| 196 | event.getKeyCode(), |
| 197 | event.getScanCode(), |
| 198 | event.getMetaState(), |
| 199 | event.getRepeatCount()}; |
| 200 | } |
| 201 | |
| 202 | VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) { |
| 203 | return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(), |
| 204 | event.getSource(), event.getDisplayId()}, |
| 205 | event.getRawX(0), |
| 206 | event.getRawY(0), |
| 207 | event.getActionMasked(), |
| 208 | event.getDownTime(), |
| 209 | event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS, |
| 210 | event.getMetaState(), |
| 211 | event.getButtonState()}; |
| 212 | } |
| 213 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 214 | 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] | 215 | std::array<uint8_t, 32> hmac) { |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 216 | mId = id; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 217 | mDeviceId = deviceId; |
| 218 | mSource = source; |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 219 | mDisplayId = displayId; |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 220 | mHmac = hmac; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | void InputEvent::initialize(const InputEvent& from) { |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 224 | mId = from.mId; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 225 | mDeviceId = from.mDeviceId; |
| 226 | mSource = from.mSource; |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 227 | mDisplayId = from.mDisplayId; |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 228 | mHmac = from.mHmac; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 231 | int32_t InputEvent::nextId() { |
| 232 | static IdGenerator idGen(IdGenerator::Source::OTHER); |
| 233 | return idGen.nextId(); |
| 234 | } |
| 235 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 236 | // --- KeyEvent --- |
| 237 | |
Michael Wright | 872db4f | 2014-04-22 15:03:51 -0700 | [diff] [blame] | 238 | const char* KeyEvent::getLabel(int32_t keyCode) { |
Chris Ye | 4958d06 | 2020-08-20 13:21:10 -0700 | [diff] [blame] | 239 | return InputEventLookup::getLabelByKeyCode(keyCode); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Michael Wright | 872db4f | 2014-04-22 15:03:51 -0700 | [diff] [blame] | 242 | int32_t KeyEvent::getKeyCodeFromLabel(const char* label) { |
Chris Ye | 4958d06 | 2020-08-20 13:21:10 -0700 | [diff] [blame] | 243 | return InputEventLookup::getKeyCodeByLabel(label); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 246 | 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] | 247 | std::array<uint8_t, 32> hmac, int32_t action, int32_t flags, |
| 248 | int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount, |
| 249 | nsecs_t downTime, nsecs_t eventTime) { |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 250 | InputEvent::initialize(id, deviceId, source, displayId, hmac); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 251 | mAction = action; |
| 252 | mFlags = flags; |
| 253 | mKeyCode = keyCode; |
| 254 | mScanCode = scanCode; |
| 255 | mMetaState = metaState; |
| 256 | mRepeatCount = repeatCount; |
| 257 | mDownTime = downTime; |
| 258 | mEventTime = eventTime; |
| 259 | } |
| 260 | |
| 261 | void KeyEvent::initialize(const KeyEvent& from) { |
| 262 | InputEvent::initialize(from); |
| 263 | mAction = from.mAction; |
| 264 | mFlags = from.mFlags; |
| 265 | mKeyCode = from.mKeyCode; |
| 266 | mScanCode = from.mScanCode; |
| 267 | mMetaState = from.mMetaState; |
| 268 | mRepeatCount = from.mRepeatCount; |
| 269 | mDownTime = from.mDownTime; |
| 270 | mEventTime = from.mEventTime; |
| 271 | } |
| 272 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 273 | const char* KeyEvent::actionToString(int32_t action) { |
| 274 | // Convert KeyEvent action to string |
| 275 | switch (action) { |
| 276 | case AKEY_EVENT_ACTION_DOWN: |
| 277 | return "DOWN"; |
| 278 | case AKEY_EVENT_ACTION_UP: |
| 279 | return "UP"; |
| 280 | case AKEY_EVENT_ACTION_MULTIPLE: |
| 281 | return "MULTIPLE"; |
| 282 | } |
| 283 | return "UNKNOWN"; |
| 284 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 285 | |
| 286 | // --- PointerCoords --- |
| 287 | |
| 288 | float PointerCoords::getAxisValue(int32_t axis) const { |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 289 | if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){ |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 290 | return 0; |
| 291 | } |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 292 | return values[BitSet64::getIndexOfBit(bits, axis)]; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | status_t PointerCoords::setAxisValue(int32_t axis, float value) { |
| 296 | if (axis < 0 || axis > 63) { |
| 297 | return NAME_NOT_FOUND; |
| 298 | } |
| 299 | |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 300 | uint32_t index = BitSet64::getIndexOfBit(bits, axis); |
| 301 | if (!BitSet64::hasBit(bits, axis)) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 302 | if (value == 0) { |
| 303 | return OK; // axes with value 0 do not need to be stored |
| 304 | } |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 305 | |
| 306 | uint32_t count = BitSet64::count(bits); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 307 | if (count >= MAX_AXES) { |
| 308 | tooManyAxes(axis); |
| 309 | return NO_MEMORY; |
| 310 | } |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 311 | BitSet64::markBit(bits, axis); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 312 | for (uint32_t i = count; i > index; i--) { |
| 313 | values[i] = values[i - 1]; |
| 314 | } |
| 315 | } |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 316 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 317 | values[index] = value; |
| 318 | return OK; |
| 319 | } |
| 320 | |
| 321 | static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) { |
| 322 | float value = c.getAxisValue(axis); |
| 323 | if (value != 0) { |
| 324 | c.setAxisValue(axis, value * scaleFactor); |
| 325 | } |
| 326 | } |
| 327 | |
Robert Carr | e07e103 | 2018-11-26 12:55:53 -0800 | [diff] [blame] | 328 | void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 329 | // No need to scale pressure or size since they are normalized. |
| 330 | // No need to scale orientation since it is meaningless to do so. |
Robert Carr | e07e103 | 2018-11-26 12:55:53 -0800 | [diff] [blame] | 331 | |
| 332 | // If there is a global scale factor, it is included in the windowX/YScale |
| 333 | // so we don't need to apply it twice to the X/Y axes. |
| 334 | // However we don't want to apply any windowXYScale not included in the global scale |
| 335 | // to the TOUCH_MAJOR/MINOR coordinates. |
| 336 | scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale); |
| 337 | scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale); |
| 338 | scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor); |
| 339 | scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor); |
| 340 | scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor); |
| 341 | scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor); |
Prabir Pradhan | c652358 | 2021-05-14 18:02:55 -0700 | [diff] [blame] | 342 | scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_X, windowXScale); |
| 343 | scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_Y, windowYScale); |
Robert Carr | e07e103 | 2018-11-26 12:55:53 -0800 | [diff] [blame] | 344 | } |
| 345 | |
Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 346 | #ifdef __linux__ |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 347 | status_t PointerCoords::readFromParcel(Parcel* parcel) { |
| 348 | bits = parcel->readInt64(); |
| 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 | if (count > MAX_AXES) { |
| 352 | return BAD_VALUE; |
| 353 | } |
| 354 | |
| 355 | for (uint32_t i = 0; i < count; i++) { |
| 356 | values[i] = parcel->readFloat(); |
| 357 | } |
| 358 | return OK; |
| 359 | } |
| 360 | |
| 361 | status_t PointerCoords::writeToParcel(Parcel* parcel) const { |
| 362 | parcel->writeInt64(bits); |
| 363 | |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 364 | uint32_t count = BitSet64::count(bits); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 365 | for (uint32_t i = 0; i < count; i++) { |
| 366 | parcel->writeFloat(values[i]); |
| 367 | } |
| 368 | return OK; |
| 369 | } |
Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 370 | #endif |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 371 | |
| 372 | void PointerCoords::tooManyAxes(int axis) { |
| 373 | ALOGW("Could not set value for axis %d because the PointerCoords structure is full and " |
| 374 | "cannot contain more than %d axis values.", axis, int(MAX_AXES)); |
| 375 | } |
| 376 | |
| 377 | bool PointerCoords::operator==(const PointerCoords& other) const { |
| 378 | if (bits != other.bits) { |
| 379 | return false; |
| 380 | } |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 381 | uint32_t count = BitSet64::count(bits); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 382 | for (uint32_t i = 0; i < count; i++) { |
| 383 | if (values[i] != other.values[i]) { |
| 384 | return false; |
| 385 | } |
| 386 | } |
| 387 | return true; |
| 388 | } |
| 389 | |
| 390 | void PointerCoords::copyFrom(const PointerCoords& other) { |
| 391 | bits = other.bits; |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 392 | uint32_t count = BitSet64::count(bits); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 393 | for (uint32_t i = 0; i < count; i++) { |
| 394 | values[i] = other.values[i]; |
| 395 | } |
| 396 | } |
| 397 | |
chaviw | c01e137 | 2020-07-01 12:37:31 -0700 | [diff] [blame] | 398 | void PointerCoords::transform(const ui::Transform& transform) { |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 399 | const vec2 xy = transform.transform(getXYValue()); |
| 400 | setAxisValue(AMOTION_EVENT_AXIS_X, xy.x); |
| 401 | setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y); |
| 402 | |
Prabir Pradhan | c652358 | 2021-05-14 18:02:55 -0700 | [diff] [blame] | 403 | if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_X) || |
| 404 | BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_Y)) { |
| 405 | const ui::Transform rotation(transform.getOrientation()); |
| 406 | const vec2 relativeXy = rotation.transform(getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X), |
| 407 | getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)); |
| 408 | setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x); |
| 409 | setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y); |
| 410 | } |
| 411 | |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 412 | if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) { |
| 413 | const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION); |
| 414 | setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val)); |
| 415 | } |
chaviw | c01e137 | 2020-07-01 12:37:31 -0700 | [diff] [blame] | 416 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 417 | |
| 418 | // --- PointerProperties --- |
| 419 | |
| 420 | bool PointerProperties::operator==(const PointerProperties& other) const { |
| 421 | return id == other.id |
| 422 | && toolType == other.toolType; |
| 423 | } |
| 424 | |
| 425 | void PointerProperties::copyFrom(const PointerProperties& other) { |
| 426 | id = other.id; |
| 427 | toolType = other.toolType; |
| 428 | } |
| 429 | |
| 430 | |
| 431 | // --- MotionEvent --- |
| 432 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 433 | 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] | 434 | std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton, |
| 435 | int32_t flags, int32_t edgeFlags, int32_t metaState, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 436 | int32_t buttonState, MotionClassification classification, |
| 437 | const ui::Transform& transform, float xPrecision, float yPrecision, |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 438 | float rawXCursorPosition, float rawYCursorPosition, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 439 | const ui::Transform& rawTransform, nsecs_t downTime, nsecs_t eventTime, |
Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 440 | size_t pointerCount, const PointerProperties* pointerProperties, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 441 | const PointerCoords* pointerCoords) { |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 442 | InputEvent::initialize(id, deviceId, source, displayId, hmac); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 443 | mAction = action; |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 444 | mActionButton = actionButton; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 445 | mFlags = flags; |
| 446 | mEdgeFlags = edgeFlags; |
| 447 | mMetaState = metaState; |
| 448 | mButtonState = buttonState; |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 449 | mClassification = classification; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 450 | mTransform = transform; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 451 | mXPrecision = xPrecision; |
| 452 | mYPrecision = yPrecision; |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 453 | mRawXCursorPosition = rawXCursorPosition; |
| 454 | mRawYCursorPosition = rawYCursorPosition; |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 455 | mRawTransform = rawTransform; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 456 | mDownTime = downTime; |
| 457 | mPointerProperties.clear(); |
| 458 | mPointerProperties.appendArray(pointerProperties, pointerCount); |
| 459 | mSampleEventTimes.clear(); |
| 460 | mSamplePointerCoords.clear(); |
| 461 | addSample(eventTime, pointerCoords); |
| 462 | } |
| 463 | |
| 464 | void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) { |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 465 | InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId, |
| 466 | other->mHmac); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 467 | mAction = other->mAction; |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 468 | mActionButton = other->mActionButton; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 469 | mFlags = other->mFlags; |
| 470 | mEdgeFlags = other->mEdgeFlags; |
| 471 | mMetaState = other->mMetaState; |
| 472 | mButtonState = other->mButtonState; |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 473 | mClassification = other->mClassification; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 474 | mTransform = other->mTransform; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 475 | mXPrecision = other->mXPrecision; |
| 476 | mYPrecision = other->mYPrecision; |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 477 | mRawXCursorPosition = other->mRawXCursorPosition; |
| 478 | mRawYCursorPosition = other->mRawYCursorPosition; |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 479 | mRawTransform = other->mRawTransform; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 480 | mDownTime = other->mDownTime; |
| 481 | mPointerProperties = other->mPointerProperties; |
| 482 | |
| 483 | if (keepHistory) { |
| 484 | mSampleEventTimes = other->mSampleEventTimes; |
| 485 | mSamplePointerCoords = other->mSamplePointerCoords; |
| 486 | } else { |
| 487 | mSampleEventTimes.clear(); |
Siarhei Vishniakou | 46a2774 | 2020-09-09 13:57:28 -0500 | [diff] [blame] | 488 | mSampleEventTimes.push_back(other->getEventTime()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 489 | mSamplePointerCoords.clear(); |
| 490 | size_t pointerCount = other->getPointerCount(); |
| 491 | size_t historySize = other->getHistorySize(); |
| 492 | mSamplePointerCoords.appendArray(other->mSamplePointerCoords.array() |
| 493 | + (historySize * pointerCount), pointerCount); |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | void MotionEvent::addSample( |
| 498 | int64_t eventTime, |
| 499 | const PointerCoords* pointerCoords) { |
Siarhei Vishniakou | 46a2774 | 2020-09-09 13:57:28 -0500 | [diff] [blame] | 500 | mSampleEventTimes.push_back(eventTime); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 501 | mSamplePointerCoords.appendArray(pointerCoords, getPointerCount()); |
| 502 | } |
| 503 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 504 | float MotionEvent::getXCursorPosition() const { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 505 | vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition()); |
| 506 | return vals.x; |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | float MotionEvent::getYCursorPosition() const { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 510 | vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition()); |
| 511 | return vals.y; |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 512 | } |
| 513 | |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 514 | void MotionEvent::setCursorPosition(float x, float y) { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 515 | ui::Transform inverse = mTransform.inverse(); |
| 516 | vec2 vals = inverse.transform(x, y); |
| 517 | mRawXCursorPosition = vals.x; |
| 518 | mRawYCursorPosition = vals.y; |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 519 | } |
| 520 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 521 | const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const { |
| 522 | return &mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex]; |
| 523 | } |
| 524 | |
| 525 | float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const { |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 526 | return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 530 | return getHistoricalAxisValue(axis, pointerIndex, getHistorySize()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | const PointerCoords* MotionEvent::getHistoricalRawPointerCoords( |
| 534 | size_t pointerIndex, size_t historicalIndex) const { |
| 535 | return &mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex]; |
| 536 | } |
| 537 | |
| 538 | float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex, |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 539 | size_t historicalIndex) const { |
Prabir Pradhan | 9eb02c0 | 2021-10-19 14:02:20 -0700 | [diff] [blame] | 540 | const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex); |
| 541 | return calculateTransformedAxisValue(axis, mSource, mRawTransform, coords); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex, |
Prabir Pradhan | 9f38881 | 2021-05-13 16:54:53 -0700 | [diff] [blame] | 545 | size_t historicalIndex) const { |
Prabir Pradhan | 9eb02c0 | 2021-10-19 14:02:20 -0700 | [diff] [blame] | 546 | const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex); |
| 547 | return calculateTransformedAxisValue(axis, mSource, mTransform, coords); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const { |
| 551 | size_t pointerCount = mPointerProperties.size(); |
| 552 | for (size_t i = 0; i < pointerCount; i++) { |
| 553 | if (mPointerProperties.itemAt(i).id == pointerId) { |
| 554 | return i; |
| 555 | } |
| 556 | } |
| 557 | return -1; |
| 558 | } |
| 559 | |
| 560 | void MotionEvent::offsetLocation(float xOffset, float yOffset) { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 561 | float currXOffset = mTransform.tx(); |
| 562 | float currYOffset = mTransform.ty(); |
| 563 | mTransform.set(currXOffset + xOffset, currYOffset + yOffset); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 564 | } |
| 565 | |
Robert Carr | e07e103 | 2018-11-26 12:55:53 -0800 | [diff] [blame] | 566 | void MotionEvent::scale(float globalScaleFactor) { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 567 | mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor); |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 568 | mRawTransform.set(mRawTransform.tx() * globalScaleFactor, |
| 569 | mRawTransform.ty() * globalScaleFactor); |
Robert Carr | e07e103 | 2018-11-26 12:55:53 -0800 | [diff] [blame] | 570 | mXPrecision *= globalScaleFactor; |
| 571 | mYPrecision *= globalScaleFactor; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 572 | |
| 573 | size_t numSamples = mSamplePointerCoords.size(); |
| 574 | for (size_t i = 0; i < numSamples; i++) { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 575 | mSamplePointerCoords.editItemAt(i).scale(globalScaleFactor, globalScaleFactor, |
| 576 | globalScaleFactor); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 577 | } |
| 578 | } |
| 579 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 580 | void MotionEvent::transform(const std::array<float, 9>& matrix) { |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 581 | // We want to preserve the raw axes values stored in the PointerCoords, so we just update the |
| 582 | // transform using the values passed in. |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 583 | ui::Transform newTransform; |
| 584 | newTransform.set(matrix); |
| 585 | mTransform = newTransform * mTransform; |
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) { |
Prabir Pradhan | 7e1ee56 | 2021-10-26 10:19:49 -0700 | [diff] [blame] | 754 | if (isFromSource(source, AINPUT_SOURCE_CLASS_POINTER)) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 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 | 7e1ee56 | 2021-10-26 10:19:49 -0700 | [diff] [blame] | 811 | // Apply the given transformation to the point without checking whether the entire transform |
| 812 | // should be disregarded altogether for the provided source. |
| 813 | static inline vec2 calculateTransformedXYUnchecked(uint32_t source, const ui::Transform& transform, |
| 814 | const vec2& xy) { |
| 815 | return shouldDisregardOffset(source) ? transformWithoutTranslation(transform, xy) |
| 816 | : transform.transform(xy); |
| 817 | } |
| 818 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 819 | vec2 MotionEvent::calculateTransformedXY(uint32_t source, const ui::Transform& transform, |
| 820 | const vec2& xy) { |
Prabir Pradhan | 7e1ee56 | 2021-10-26 10:19:49 -0700 | [diff] [blame] | 821 | if (shouldDisregardTransformation(source)) { |
| 822 | return xy; |
| 823 | } |
| 824 | return calculateTransformedXYUnchecked(source, transform, xy); |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 825 | } |
| 826 | |
Prabir Pradhan | 9eb02c0 | 2021-10-19 14:02:20 -0700 | [diff] [blame] | 827 | float MotionEvent::calculateTransformedAxisValue(int32_t axis, uint32_t source, |
| 828 | const ui::Transform& transform, |
| 829 | const PointerCoords& coords) { |
Prabir Pradhan | 7e1ee56 | 2021-10-26 10:19:49 -0700 | [diff] [blame] | 830 | if (shouldDisregardTransformation(source)) { |
| 831 | return coords.getAxisValue(axis); |
| 832 | } |
| 833 | |
Prabir Pradhan | 9eb02c0 | 2021-10-19 14:02:20 -0700 | [diff] [blame] | 834 | if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) { |
Prabir Pradhan | 7e1ee56 | 2021-10-26 10:19:49 -0700 | [diff] [blame] | 835 | const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue()); |
Prabir Pradhan | 9eb02c0 | 2021-10-19 14:02:20 -0700 | [diff] [blame] | 836 | static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1); |
| 837 | return xy[axis]; |
| 838 | } |
| 839 | |
| 840 | if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) { |
| 841 | const vec2 relativeXy = |
| 842 | transformWithoutTranslation(transform, |
| 843 | {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X), |
| 844 | coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)}); |
| 845 | return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y; |
| 846 | } |
| 847 | |
| 848 | if (axis == AMOTION_EVENT_AXIS_ORIENTATION) { |
| 849 | return transformAngle(transform, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 850 | } |
| 851 | |
| 852 | return coords.getAxisValue(axis); |
| 853 | } |
| 854 | |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 855 | // --- FocusEvent --- |
| 856 | |
Antonio Kantek | 3cfec7b | 2021-11-05 18:26:17 -0700 | [diff] [blame] | 857 | void FocusEvent::initialize(int32_t id, bool hasFocus) { |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 858 | InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 859 | ADISPLAY_ID_NONE, INVALID_HMAC); |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 860 | mHasFocus = hasFocus; |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | void FocusEvent::initialize(const FocusEvent& from) { |
| 864 | InputEvent::initialize(from); |
| 865 | mHasFocus = from.mHasFocus; |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 866 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 867 | |
Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 868 | // --- CaptureEvent --- |
| 869 | |
| 870 | void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) { |
| 871 | InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN, |
| 872 | ADISPLAY_ID_NONE, INVALID_HMAC); |
| 873 | mPointerCaptureEnabled = pointerCaptureEnabled; |
| 874 | } |
| 875 | |
| 876 | void CaptureEvent::initialize(const CaptureEvent& from) { |
| 877 | InputEvent::initialize(from); |
| 878 | mPointerCaptureEnabled = from.mPointerCaptureEnabled; |
| 879 | } |
| 880 | |
arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 881 | // --- DragEvent --- |
| 882 | |
| 883 | void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) { |
| 884 | InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN, |
| 885 | ADISPLAY_ID_NONE, INVALID_HMAC); |
| 886 | mIsExiting = isExiting; |
| 887 | mX = x; |
| 888 | mY = y; |
| 889 | } |
| 890 | |
| 891 | void DragEvent::initialize(const DragEvent& from) { |
| 892 | InputEvent::initialize(from); |
| 893 | mIsExiting = from.mIsExiting; |
| 894 | mX = from.mX; |
| 895 | mY = from.mY; |
| 896 | } |
| 897 | |
Antonio Kantek | 7cdf8ef | 2021-07-13 18:04:53 -0700 | [diff] [blame] | 898 | // --- TouchModeEvent --- |
| 899 | |
| 900 | void TouchModeEvent::initialize(int32_t id, bool isInTouchMode) { |
| 901 | InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN, |
| 902 | ADISPLAY_ID_NONE, INVALID_HMAC); |
| 903 | mIsInTouchMode = isInTouchMode; |
| 904 | } |
| 905 | |
| 906 | void TouchModeEvent::initialize(const TouchModeEvent& from) { |
| 907 | InputEvent::initialize(from); |
| 908 | mIsInTouchMode = from.mIsInTouchMode; |
| 909 | } |
| 910 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 911 | // --- PooledInputEventFactory --- |
| 912 | |
| 913 | PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) : |
| 914 | mMaxPoolSize(maxPoolSize) { |
| 915 | } |
| 916 | |
| 917 | PooledInputEventFactory::~PooledInputEventFactory() { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | KeyEvent* PooledInputEventFactory::createKeyEvent() { |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 921 | if (mKeyEventPool.empty()) { |
| 922 | return new KeyEvent(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 923 | } |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 924 | KeyEvent* event = mKeyEventPool.front().release(); |
| 925 | mKeyEventPool.pop(); |
| 926 | return event; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | MotionEvent* PooledInputEventFactory::createMotionEvent() { |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 930 | if (mMotionEventPool.empty()) { |
| 931 | return new MotionEvent(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 932 | } |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 933 | MotionEvent* event = mMotionEventPool.front().release(); |
| 934 | mMotionEventPool.pop(); |
| 935 | return event; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 936 | } |
| 937 | |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 938 | FocusEvent* PooledInputEventFactory::createFocusEvent() { |
| 939 | if (mFocusEventPool.empty()) { |
| 940 | return new FocusEvent(); |
| 941 | } |
| 942 | FocusEvent* event = mFocusEventPool.front().release(); |
| 943 | mFocusEventPool.pop(); |
| 944 | return event; |
| 945 | } |
| 946 | |
Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 947 | CaptureEvent* PooledInputEventFactory::createCaptureEvent() { |
| 948 | if (mCaptureEventPool.empty()) { |
| 949 | return new CaptureEvent(); |
| 950 | } |
| 951 | CaptureEvent* event = mCaptureEventPool.front().release(); |
| 952 | mCaptureEventPool.pop(); |
| 953 | return event; |
| 954 | } |
| 955 | |
arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 956 | DragEvent* PooledInputEventFactory::createDragEvent() { |
| 957 | if (mDragEventPool.empty()) { |
| 958 | return new DragEvent(); |
| 959 | } |
| 960 | DragEvent* event = mDragEventPool.front().release(); |
| 961 | mDragEventPool.pop(); |
| 962 | return event; |
| 963 | } |
| 964 | |
Antonio Kantek | 7cdf8ef | 2021-07-13 18:04:53 -0700 | [diff] [blame] | 965 | TouchModeEvent* PooledInputEventFactory::createTouchModeEvent() { |
| 966 | if (mTouchModeEventPool.empty()) { |
| 967 | return new TouchModeEvent(); |
| 968 | } |
| 969 | TouchModeEvent* event = mTouchModeEventPool.front().release(); |
| 970 | mTouchModeEventPool.pop(); |
| 971 | return event; |
| 972 | } |
| 973 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 974 | void PooledInputEventFactory::recycle(InputEvent* event) { |
| 975 | switch (event->getType()) { |
| 976 | case AINPUT_EVENT_TYPE_KEY: |
| 977 | if (mKeyEventPool.size() < mMaxPoolSize) { |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 978 | mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event))); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 979 | return; |
| 980 | } |
| 981 | break; |
| 982 | case AINPUT_EVENT_TYPE_MOTION: |
| 983 | if (mMotionEventPool.size() < mMaxPoolSize) { |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 984 | mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event))); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 985 | return; |
| 986 | } |
| 987 | break; |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 988 | case AINPUT_EVENT_TYPE_FOCUS: |
| 989 | if (mFocusEventPool.size() < mMaxPoolSize) { |
| 990 | mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event))); |
| 991 | return; |
| 992 | } |
| 993 | break; |
Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 994 | case AINPUT_EVENT_TYPE_CAPTURE: |
| 995 | if (mCaptureEventPool.size() < mMaxPoolSize) { |
| 996 | mCaptureEventPool.push( |
| 997 | std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event))); |
| 998 | return; |
| 999 | } |
| 1000 | break; |
arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 1001 | case AINPUT_EVENT_TYPE_DRAG: |
| 1002 | if (mDragEventPool.size() < mMaxPoolSize) { |
| 1003 | mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event))); |
| 1004 | return; |
| 1005 | } |
| 1006 | break; |
Antonio Kantek | eb4a30c | 2021-09-28 17:49:49 -0700 | [diff] [blame] | 1007 | case AINPUT_EVENT_TYPE_TOUCH_MODE: |
| 1008 | if (mTouchModeEventPool.size() < mMaxPoolSize) { |
| 1009 | mTouchModeEventPool.push( |
| 1010 | std::unique_ptr<TouchModeEvent>(static_cast<TouchModeEvent*>(event))); |
| 1011 | return; |
| 1012 | } |
| 1013 | break; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1014 | } |
| 1015 | delete event; |
| 1016 | } |
| 1017 | |
| 1018 | } // namespace android |