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 | |
Alex Chau | f7c99f3 | 2021-12-03 10:37:54 +0000 | [diff] [blame^] | 346 | void PointerCoords::applyOffset(float xOffset, float yOffset) { |
| 347 | setAxisValue(AMOTION_EVENT_AXIS_X, getX() + xOffset); |
| 348 | setAxisValue(AMOTION_EVENT_AXIS_Y, getY() + yOffset); |
| 349 | } |
| 350 | |
Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 351 | #ifdef __linux__ |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 352 | status_t PointerCoords::readFromParcel(Parcel* parcel) { |
| 353 | bits = parcel->readInt64(); |
| 354 | |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 355 | uint32_t count = BitSet64::count(bits); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 356 | if (count > MAX_AXES) { |
| 357 | return BAD_VALUE; |
| 358 | } |
| 359 | |
| 360 | for (uint32_t i = 0; i < count; i++) { |
| 361 | values[i] = parcel->readFloat(); |
| 362 | } |
| 363 | return OK; |
| 364 | } |
| 365 | |
| 366 | status_t PointerCoords::writeToParcel(Parcel* parcel) const { |
| 367 | parcel->writeInt64(bits); |
| 368 | |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 369 | uint32_t count = BitSet64::count(bits); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 370 | for (uint32_t i = 0; i < count; i++) { |
| 371 | parcel->writeFloat(values[i]); |
| 372 | } |
| 373 | return OK; |
| 374 | } |
Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 375 | #endif |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 376 | |
| 377 | void PointerCoords::tooManyAxes(int axis) { |
| 378 | ALOGW("Could not set value for axis %d because the PointerCoords structure is full and " |
| 379 | "cannot contain more than %d axis values.", axis, int(MAX_AXES)); |
| 380 | } |
| 381 | |
| 382 | bool PointerCoords::operator==(const PointerCoords& other) const { |
| 383 | if (bits != other.bits) { |
| 384 | return false; |
| 385 | } |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 386 | uint32_t count = BitSet64::count(bits); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 387 | for (uint32_t i = 0; i < count; i++) { |
| 388 | if (values[i] != other.values[i]) { |
| 389 | return false; |
| 390 | } |
| 391 | } |
| 392 | return true; |
| 393 | } |
| 394 | |
| 395 | void PointerCoords::copyFrom(const PointerCoords& other) { |
| 396 | bits = other.bits; |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 397 | uint32_t count = BitSet64::count(bits); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 398 | for (uint32_t i = 0; i < count; i++) { |
| 399 | values[i] = other.values[i]; |
| 400 | } |
| 401 | } |
| 402 | |
chaviw | c01e137 | 2020-07-01 12:37:31 -0700 | [diff] [blame] | 403 | void PointerCoords::transform(const ui::Transform& transform) { |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 404 | const vec2 xy = transform.transform(getXYValue()); |
| 405 | setAxisValue(AMOTION_EVENT_AXIS_X, xy.x); |
| 406 | setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y); |
| 407 | |
Prabir Pradhan | c652358 | 2021-05-14 18:02:55 -0700 | [diff] [blame] | 408 | if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_X) || |
| 409 | BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_Y)) { |
| 410 | const ui::Transform rotation(transform.getOrientation()); |
| 411 | const vec2 relativeXy = rotation.transform(getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X), |
| 412 | getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)); |
| 413 | setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x); |
| 414 | setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y); |
| 415 | } |
| 416 | |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 417 | if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) { |
| 418 | const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION); |
| 419 | setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val)); |
| 420 | } |
chaviw | c01e137 | 2020-07-01 12:37:31 -0700 | [diff] [blame] | 421 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 422 | |
| 423 | // --- PointerProperties --- |
| 424 | |
| 425 | bool PointerProperties::operator==(const PointerProperties& other) const { |
| 426 | return id == other.id |
| 427 | && toolType == other.toolType; |
| 428 | } |
| 429 | |
| 430 | void PointerProperties::copyFrom(const PointerProperties& other) { |
| 431 | id = other.id; |
| 432 | toolType = other.toolType; |
| 433 | } |
| 434 | |
| 435 | |
| 436 | // --- MotionEvent --- |
| 437 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 438 | 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] | 439 | std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton, |
| 440 | int32_t flags, int32_t edgeFlags, int32_t metaState, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 441 | int32_t buttonState, MotionClassification classification, |
| 442 | const ui::Transform& transform, float xPrecision, float yPrecision, |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 443 | float rawXCursorPosition, float rawYCursorPosition, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 444 | const ui::Transform& rawTransform, nsecs_t downTime, nsecs_t eventTime, |
Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 445 | size_t pointerCount, const PointerProperties* pointerProperties, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 446 | const PointerCoords* pointerCoords) { |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 447 | InputEvent::initialize(id, deviceId, source, displayId, hmac); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 448 | mAction = action; |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 449 | mActionButton = actionButton; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 450 | mFlags = flags; |
| 451 | mEdgeFlags = edgeFlags; |
| 452 | mMetaState = metaState; |
| 453 | mButtonState = buttonState; |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 454 | mClassification = classification; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 455 | mTransform = transform; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 456 | mXPrecision = xPrecision; |
| 457 | mYPrecision = yPrecision; |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 458 | mRawXCursorPosition = rawXCursorPosition; |
| 459 | mRawYCursorPosition = rawYCursorPosition; |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 460 | mRawTransform = rawTransform; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 461 | mDownTime = downTime; |
| 462 | mPointerProperties.clear(); |
| 463 | mPointerProperties.appendArray(pointerProperties, pointerCount); |
| 464 | mSampleEventTimes.clear(); |
| 465 | mSamplePointerCoords.clear(); |
| 466 | addSample(eventTime, pointerCoords); |
| 467 | } |
| 468 | |
| 469 | void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) { |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 470 | InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId, |
| 471 | other->mHmac); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 472 | mAction = other->mAction; |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 473 | mActionButton = other->mActionButton; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 474 | mFlags = other->mFlags; |
| 475 | mEdgeFlags = other->mEdgeFlags; |
| 476 | mMetaState = other->mMetaState; |
| 477 | mButtonState = other->mButtonState; |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 478 | mClassification = other->mClassification; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 479 | mTransform = other->mTransform; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 480 | mXPrecision = other->mXPrecision; |
| 481 | mYPrecision = other->mYPrecision; |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 482 | mRawXCursorPosition = other->mRawXCursorPosition; |
| 483 | mRawYCursorPosition = other->mRawYCursorPosition; |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 484 | mRawTransform = other->mRawTransform; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 485 | mDownTime = other->mDownTime; |
| 486 | mPointerProperties = other->mPointerProperties; |
| 487 | |
| 488 | if (keepHistory) { |
| 489 | mSampleEventTimes = other->mSampleEventTimes; |
| 490 | mSamplePointerCoords = other->mSamplePointerCoords; |
| 491 | } else { |
| 492 | mSampleEventTimes.clear(); |
Siarhei Vishniakou | 46a2774 | 2020-09-09 13:57:28 -0500 | [diff] [blame] | 493 | mSampleEventTimes.push_back(other->getEventTime()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 494 | mSamplePointerCoords.clear(); |
| 495 | size_t pointerCount = other->getPointerCount(); |
| 496 | size_t historySize = other->getHistorySize(); |
| 497 | mSamplePointerCoords.appendArray(other->mSamplePointerCoords.array() |
| 498 | + (historySize * pointerCount), pointerCount); |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | void MotionEvent::addSample( |
| 503 | int64_t eventTime, |
| 504 | const PointerCoords* pointerCoords) { |
Siarhei Vishniakou | 46a2774 | 2020-09-09 13:57:28 -0500 | [diff] [blame] | 505 | mSampleEventTimes.push_back(eventTime); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 506 | mSamplePointerCoords.appendArray(pointerCoords, getPointerCount()); |
| 507 | } |
| 508 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 509 | float MotionEvent::getXCursorPosition() const { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 510 | vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition()); |
| 511 | return vals.x; |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | float MotionEvent::getYCursorPosition() const { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 515 | vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition()); |
| 516 | return vals.y; |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 517 | } |
| 518 | |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 519 | void MotionEvent::setCursorPosition(float x, float y) { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 520 | ui::Transform inverse = mTransform.inverse(); |
| 521 | vec2 vals = inverse.transform(x, y); |
| 522 | mRawXCursorPosition = vals.x; |
| 523 | mRawYCursorPosition = vals.y; |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 524 | } |
| 525 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 526 | const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const { |
| 527 | return &mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex]; |
| 528 | } |
| 529 | |
| 530 | float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const { |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 531 | return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 535 | return getHistoricalAxisValue(axis, pointerIndex, getHistorySize()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | const PointerCoords* MotionEvent::getHistoricalRawPointerCoords( |
| 539 | size_t pointerIndex, size_t historicalIndex) const { |
| 540 | return &mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex]; |
| 541 | } |
| 542 | |
| 543 | float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex, |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 544 | size_t historicalIndex) const { |
Prabir Pradhan | 9eb02c0 | 2021-10-19 14:02:20 -0700 | [diff] [blame] | 545 | const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex); |
| 546 | return calculateTransformedAxisValue(axis, mSource, mRawTransform, coords); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex, |
Prabir Pradhan | 9f38881 | 2021-05-13 16:54:53 -0700 | [diff] [blame] | 550 | size_t historicalIndex) const { |
Prabir Pradhan | 9eb02c0 | 2021-10-19 14:02:20 -0700 | [diff] [blame] | 551 | const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex); |
| 552 | return calculateTransformedAxisValue(axis, mSource, mTransform, coords); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const { |
| 556 | size_t pointerCount = mPointerProperties.size(); |
| 557 | for (size_t i = 0; i < pointerCount; i++) { |
| 558 | if (mPointerProperties.itemAt(i).id == pointerId) { |
| 559 | return i; |
| 560 | } |
| 561 | } |
| 562 | return -1; |
| 563 | } |
| 564 | |
| 565 | void MotionEvent::offsetLocation(float xOffset, float yOffset) { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 566 | float currXOffset = mTransform.tx(); |
| 567 | float currYOffset = mTransform.ty(); |
| 568 | mTransform.set(currXOffset + xOffset, currYOffset + yOffset); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 569 | } |
| 570 | |
Robert Carr | e07e103 | 2018-11-26 12:55:53 -0800 | [diff] [blame] | 571 | void MotionEvent::scale(float globalScaleFactor) { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 572 | mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor); |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 573 | mRawTransform.set(mRawTransform.tx() * globalScaleFactor, |
| 574 | mRawTransform.ty() * globalScaleFactor); |
Robert Carr | e07e103 | 2018-11-26 12:55:53 -0800 | [diff] [blame] | 575 | mXPrecision *= globalScaleFactor; |
| 576 | mYPrecision *= globalScaleFactor; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 577 | |
| 578 | size_t numSamples = mSamplePointerCoords.size(); |
| 579 | for (size_t i = 0; i < numSamples; i++) { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 580 | mSamplePointerCoords.editItemAt(i).scale(globalScaleFactor, globalScaleFactor, |
| 581 | globalScaleFactor); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 582 | } |
| 583 | } |
| 584 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 585 | void MotionEvent::transform(const std::array<float, 9>& matrix) { |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 586 | // We want to preserve the raw axes values stored in the PointerCoords, so we just update the |
| 587 | // transform using the values passed in. |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 588 | ui::Transform newTransform; |
| 589 | newTransform.set(matrix); |
| 590 | mTransform = newTransform * mTransform; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Evan Rosky | d4d4d80 | 2021-05-03 20:12:21 -0700 | [diff] [blame] | 593 | void MotionEvent::applyTransform(const std::array<float, 9>& matrix) { |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 594 | ui::Transform transform; |
| 595 | transform.set(matrix); |
Evan Rosky | d4d4d80 | 2021-05-03 20:12:21 -0700 | [diff] [blame] | 596 | |
| 597 | // Apply the transformation to all samples. |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 598 | std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(), |
| 599 | [&transform](PointerCoords& c) { c.transform(transform); }); |
Prabir Pradhan | 4b19bd0 | 2021-06-01 17:34:59 -0700 | [diff] [blame] | 600 | |
| 601 | if (mRawXCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION && |
| 602 | mRawYCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) { |
| 603 | const vec2 cursor = transform.transform(mRawXCursorPosition, mRawYCursorPosition); |
| 604 | mRawXCursorPosition = cursor.x; |
| 605 | mRawYCursorPosition = cursor.y; |
| 606 | } |
Evan Rosky | d4d4d80 | 2021-05-03 20:12:21 -0700 | [diff] [blame] | 607 | } |
| 608 | |
Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 609 | #ifdef __linux__ |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 610 | static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) { |
| 611 | float dsdx, dtdx, tx, dtdy, dsdy, ty; |
| 612 | status_t status = parcel.readFloat(&dsdx); |
| 613 | status |= parcel.readFloat(&dtdx); |
| 614 | status |= parcel.readFloat(&tx); |
| 615 | status |= parcel.readFloat(&dtdy); |
| 616 | status |= parcel.readFloat(&dsdy); |
| 617 | status |= parcel.readFloat(&ty); |
| 618 | |
| 619 | transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1}); |
| 620 | return status; |
| 621 | } |
| 622 | |
| 623 | static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) { |
| 624 | status_t status = parcel.writeFloat(transform.dsdx()); |
| 625 | status |= parcel.writeFloat(transform.dtdx()); |
| 626 | status |= parcel.writeFloat(transform.tx()); |
| 627 | status |= parcel.writeFloat(transform.dtdy()); |
| 628 | status |= parcel.writeFloat(transform.dsdy()); |
| 629 | status |= parcel.writeFloat(transform.ty()); |
| 630 | return status; |
| 631 | } |
| 632 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 633 | status_t MotionEvent::readFromParcel(Parcel* parcel) { |
| 634 | size_t pointerCount = parcel->readInt32(); |
| 635 | size_t sampleCount = parcel->readInt32(); |
Flanker | 552a8a5 | 2015-09-07 15:28:58 +0800 | [diff] [blame] | 636 | if (pointerCount == 0 || pointerCount > MAX_POINTERS || |
| 637 | sampleCount == 0 || sampleCount > MAX_SAMPLES) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 638 | return BAD_VALUE; |
| 639 | } |
| 640 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 641 | mId = parcel->readInt32(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 642 | mDeviceId = parcel->readInt32(); |
Siarhei Vishniakou | 3826d47 | 2020-01-27 10:44:40 -0600 | [diff] [blame] | 643 | mSource = parcel->readUint32(); |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 644 | mDisplayId = parcel->readInt32(); |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 645 | std::vector<uint8_t> hmac; |
| 646 | status_t result = parcel->readByteVector(&hmac); |
| 647 | if (result != OK || hmac.size() != 32) { |
| 648 | return BAD_VALUE; |
| 649 | } |
| 650 | std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 651 | mAction = parcel->readInt32(); |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 652 | mActionButton = parcel->readInt32(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 653 | mFlags = parcel->readInt32(); |
| 654 | mEdgeFlags = parcel->readInt32(); |
| 655 | mMetaState = parcel->readInt32(); |
| 656 | mButtonState = parcel->readInt32(); |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 657 | mClassification = static_cast<MotionClassification>(parcel->readByte()); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 658 | |
| 659 | result = android::readFromParcel(mTransform, *parcel); |
| 660 | if (result != OK) { |
| 661 | return result; |
| 662 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 663 | mXPrecision = parcel->readFloat(); |
| 664 | mYPrecision = parcel->readFloat(); |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 665 | mRawXCursorPosition = parcel->readFloat(); |
| 666 | mRawYCursorPosition = parcel->readFloat(); |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 667 | |
| 668 | result = android::readFromParcel(mRawTransform, *parcel); |
| 669 | if (result != OK) { |
| 670 | return result; |
| 671 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 672 | mDownTime = parcel->readInt64(); |
| 673 | |
| 674 | mPointerProperties.clear(); |
| 675 | mPointerProperties.setCapacity(pointerCount); |
| 676 | mSampleEventTimes.clear(); |
Siarhei Vishniakou | 46a2774 | 2020-09-09 13:57:28 -0500 | [diff] [blame] | 677 | mSampleEventTimes.reserve(sampleCount); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 678 | mSamplePointerCoords.clear(); |
| 679 | mSamplePointerCoords.setCapacity(sampleCount * pointerCount); |
| 680 | |
| 681 | for (size_t i = 0; i < pointerCount; i++) { |
| 682 | mPointerProperties.push(); |
| 683 | PointerProperties& properties = mPointerProperties.editTop(); |
| 684 | properties.id = parcel->readInt32(); |
| 685 | properties.toolType = parcel->readInt32(); |
| 686 | } |
| 687 | |
Dan Austin | c94fc45 | 2015-09-22 14:22:41 -0700 | [diff] [blame] | 688 | while (sampleCount > 0) { |
| 689 | sampleCount--; |
Siarhei Vishniakou | 46a2774 | 2020-09-09 13:57:28 -0500 | [diff] [blame] | 690 | mSampleEventTimes.push_back(parcel->readInt64()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 691 | for (size_t i = 0; i < pointerCount; i++) { |
| 692 | mSamplePointerCoords.push(); |
| 693 | status_t status = mSamplePointerCoords.editTop().readFromParcel(parcel); |
| 694 | if (status) { |
| 695 | return status; |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | return OK; |
| 700 | } |
| 701 | |
| 702 | status_t MotionEvent::writeToParcel(Parcel* parcel) const { |
| 703 | size_t pointerCount = mPointerProperties.size(); |
| 704 | size_t sampleCount = mSampleEventTimes.size(); |
| 705 | |
| 706 | parcel->writeInt32(pointerCount); |
| 707 | parcel->writeInt32(sampleCount); |
| 708 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 709 | parcel->writeInt32(mId); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 710 | parcel->writeInt32(mDeviceId); |
Siarhei Vishniakou | 3826d47 | 2020-01-27 10:44:40 -0600 | [diff] [blame] | 711 | parcel->writeUint32(mSource); |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 712 | parcel->writeInt32(mDisplayId); |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 713 | std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end()); |
| 714 | parcel->writeByteVector(hmac); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 715 | parcel->writeInt32(mAction); |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 716 | parcel->writeInt32(mActionButton); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 717 | parcel->writeInt32(mFlags); |
| 718 | parcel->writeInt32(mEdgeFlags); |
| 719 | parcel->writeInt32(mMetaState); |
| 720 | parcel->writeInt32(mButtonState); |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 721 | parcel->writeByte(static_cast<int8_t>(mClassification)); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 722 | |
| 723 | status_t result = android::writeToParcel(mTransform, *parcel); |
| 724 | if (result != OK) { |
| 725 | return result; |
| 726 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 727 | parcel->writeFloat(mXPrecision); |
| 728 | parcel->writeFloat(mYPrecision); |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 729 | parcel->writeFloat(mRawXCursorPosition); |
| 730 | parcel->writeFloat(mRawYCursorPosition); |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 731 | |
| 732 | result = android::writeToParcel(mRawTransform, *parcel); |
| 733 | if (result != OK) { |
| 734 | return result; |
| 735 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 736 | parcel->writeInt64(mDownTime); |
| 737 | |
| 738 | for (size_t i = 0; i < pointerCount; i++) { |
| 739 | const PointerProperties& properties = mPointerProperties.itemAt(i); |
| 740 | parcel->writeInt32(properties.id); |
| 741 | parcel->writeInt32(properties.toolType); |
| 742 | } |
| 743 | |
| 744 | const PointerCoords* pc = mSamplePointerCoords.array(); |
| 745 | for (size_t h = 0; h < sampleCount; h++) { |
Siarhei Vishniakou | 46a2774 | 2020-09-09 13:57:28 -0500 | [diff] [blame] | 746 | parcel->writeInt64(mSampleEventTimes[h]); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 747 | for (size_t i = 0; i < pointerCount; i++) { |
| 748 | status_t status = (pc++)->writeToParcel(parcel); |
| 749 | if (status) { |
| 750 | return status; |
| 751 | } |
| 752 | } |
| 753 | } |
| 754 | return OK; |
| 755 | } |
Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 756 | #endif |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 757 | |
Siarhei Vishniakou | 3826d47 | 2020-01-27 10:44:40 -0600 | [diff] [blame] | 758 | bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) { |
Prabir Pradhan | 7e1ee56 | 2021-10-26 10:19:49 -0700 | [diff] [blame] | 759 | if (isFromSource(source, AINPUT_SOURCE_CLASS_POINTER)) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 760 | // Specifically excludes HOVER_MOVE and SCROLL. |
| 761 | switch (action & AMOTION_EVENT_ACTION_MASK) { |
| 762 | case AMOTION_EVENT_ACTION_DOWN: |
| 763 | case AMOTION_EVENT_ACTION_MOVE: |
| 764 | case AMOTION_EVENT_ACTION_UP: |
| 765 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 766 | case AMOTION_EVENT_ACTION_POINTER_UP: |
| 767 | case AMOTION_EVENT_ACTION_CANCEL: |
| 768 | case AMOTION_EVENT_ACTION_OUTSIDE: |
| 769 | return true; |
| 770 | } |
| 771 | } |
| 772 | return false; |
| 773 | } |
| 774 | |
Michael Wright | 872db4f | 2014-04-22 15:03:51 -0700 | [diff] [blame] | 775 | const char* MotionEvent::getLabel(int32_t axis) { |
Chris Ye | 4958d06 | 2020-08-20 13:21:10 -0700 | [diff] [blame] | 776 | return InputEventLookup::getAxisLabel(axis); |
Michael Wright | 872db4f | 2014-04-22 15:03:51 -0700 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | int32_t MotionEvent::getAxisFromLabel(const char* label) { |
Chris Ye | 4958d06 | 2020-08-20 13:21:10 -0700 | [diff] [blame] | 780 | return InputEventLookup::getAxisByLabel(label); |
Michael Wright | 872db4f | 2014-04-22 15:03:51 -0700 | [diff] [blame] | 781 | } |
| 782 | |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame] | 783 | std::string MotionEvent::actionToString(int32_t action) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 784 | // Convert MotionEvent action to string |
| 785 | switch (action & AMOTION_EVENT_ACTION_MASK) { |
| 786 | case AMOTION_EVENT_ACTION_DOWN: |
| 787 | return "DOWN"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 788 | case AMOTION_EVENT_ACTION_UP: |
| 789 | return "UP"; |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame] | 790 | case AMOTION_EVENT_ACTION_MOVE: |
| 791 | return "MOVE"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 792 | case AMOTION_EVENT_ACTION_CANCEL: |
| 793 | return "CANCEL"; |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame] | 794 | case AMOTION_EVENT_ACTION_OUTSIDE: |
| 795 | return "OUTSIDE"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 796 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 797 | return StringPrintf("POINTER_DOWN(%" PRId32 ")", MotionEvent::getActionIndex(action)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 798 | case AMOTION_EVENT_ACTION_POINTER_UP: |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 799 | return StringPrintf("POINTER_UP(%" PRId32 ")", MotionEvent::getActionIndex(action)); |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame] | 800 | case AMOTION_EVENT_ACTION_HOVER_MOVE: |
| 801 | return "HOVER_MOVE"; |
| 802 | case AMOTION_EVENT_ACTION_SCROLL: |
| 803 | return "SCROLL"; |
| 804 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 805 | return "HOVER_ENTER"; |
| 806 | case AMOTION_EVENT_ACTION_HOVER_EXIT: |
| 807 | return "HOVER_EXIT"; |
| 808 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: |
| 809 | return "BUTTON_PRESS"; |
| 810 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: |
| 811 | return "BUTTON_RELEASE"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 812 | } |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame] | 813 | return android::base::StringPrintf("%" PRId32, action); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 814 | } |
| 815 | |
Prabir Pradhan | 7e1ee56 | 2021-10-26 10:19:49 -0700 | [diff] [blame] | 816 | // Apply the given transformation to the point without checking whether the entire transform |
| 817 | // should be disregarded altogether for the provided source. |
| 818 | static inline vec2 calculateTransformedXYUnchecked(uint32_t source, const ui::Transform& transform, |
| 819 | const vec2& xy) { |
| 820 | return shouldDisregardOffset(source) ? transformWithoutTranslation(transform, xy) |
| 821 | : transform.transform(xy); |
| 822 | } |
| 823 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 824 | vec2 MotionEvent::calculateTransformedXY(uint32_t source, const ui::Transform& transform, |
| 825 | const vec2& xy) { |
Prabir Pradhan | 7e1ee56 | 2021-10-26 10:19:49 -0700 | [diff] [blame] | 826 | if (shouldDisregardTransformation(source)) { |
| 827 | return xy; |
| 828 | } |
| 829 | return calculateTransformedXYUnchecked(source, transform, xy); |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 830 | } |
| 831 | |
Prabir Pradhan | 9eb02c0 | 2021-10-19 14:02:20 -0700 | [diff] [blame] | 832 | float MotionEvent::calculateTransformedAxisValue(int32_t axis, uint32_t source, |
| 833 | const ui::Transform& transform, |
| 834 | const PointerCoords& coords) { |
Prabir Pradhan | 7e1ee56 | 2021-10-26 10:19:49 -0700 | [diff] [blame] | 835 | if (shouldDisregardTransformation(source)) { |
| 836 | return coords.getAxisValue(axis); |
| 837 | } |
| 838 | |
Prabir Pradhan | 9eb02c0 | 2021-10-19 14:02:20 -0700 | [diff] [blame] | 839 | if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) { |
Prabir Pradhan | 7e1ee56 | 2021-10-26 10:19:49 -0700 | [diff] [blame] | 840 | const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue()); |
Prabir Pradhan | 9eb02c0 | 2021-10-19 14:02:20 -0700 | [diff] [blame] | 841 | static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1); |
| 842 | return xy[axis]; |
| 843 | } |
| 844 | |
| 845 | if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) { |
| 846 | const vec2 relativeXy = |
| 847 | transformWithoutTranslation(transform, |
| 848 | {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X), |
| 849 | coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)}); |
| 850 | return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y; |
| 851 | } |
| 852 | |
| 853 | if (axis == AMOTION_EVENT_AXIS_ORIENTATION) { |
| 854 | return transformAngle(transform, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 855 | } |
| 856 | |
| 857 | return coords.getAxisValue(axis); |
| 858 | } |
| 859 | |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 860 | // --- FocusEvent --- |
| 861 | |
Antonio Kantek | 3cfec7b | 2021-11-05 18:26:17 -0700 | [diff] [blame] | 862 | void FocusEvent::initialize(int32_t id, bool hasFocus) { |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 863 | InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 864 | ADISPLAY_ID_NONE, INVALID_HMAC); |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 865 | mHasFocus = hasFocus; |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | void FocusEvent::initialize(const FocusEvent& from) { |
| 869 | InputEvent::initialize(from); |
| 870 | mHasFocus = from.mHasFocus; |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 871 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 872 | |
Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 873 | // --- CaptureEvent --- |
| 874 | |
| 875 | void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) { |
| 876 | InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN, |
| 877 | ADISPLAY_ID_NONE, INVALID_HMAC); |
| 878 | mPointerCaptureEnabled = pointerCaptureEnabled; |
| 879 | } |
| 880 | |
| 881 | void CaptureEvent::initialize(const CaptureEvent& from) { |
| 882 | InputEvent::initialize(from); |
| 883 | mPointerCaptureEnabled = from.mPointerCaptureEnabled; |
| 884 | } |
| 885 | |
arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 886 | // --- DragEvent --- |
| 887 | |
| 888 | void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) { |
| 889 | InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN, |
| 890 | ADISPLAY_ID_NONE, INVALID_HMAC); |
| 891 | mIsExiting = isExiting; |
| 892 | mX = x; |
| 893 | mY = y; |
| 894 | } |
| 895 | |
| 896 | void DragEvent::initialize(const DragEvent& from) { |
| 897 | InputEvent::initialize(from); |
| 898 | mIsExiting = from.mIsExiting; |
| 899 | mX = from.mX; |
| 900 | mY = from.mY; |
| 901 | } |
| 902 | |
Antonio Kantek | 7cdf8ef | 2021-07-13 18:04:53 -0700 | [diff] [blame] | 903 | // --- TouchModeEvent --- |
| 904 | |
| 905 | void TouchModeEvent::initialize(int32_t id, bool isInTouchMode) { |
| 906 | InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN, |
| 907 | ADISPLAY_ID_NONE, INVALID_HMAC); |
| 908 | mIsInTouchMode = isInTouchMode; |
| 909 | } |
| 910 | |
| 911 | void TouchModeEvent::initialize(const TouchModeEvent& from) { |
| 912 | InputEvent::initialize(from); |
| 913 | mIsInTouchMode = from.mIsInTouchMode; |
| 914 | } |
| 915 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 916 | // --- PooledInputEventFactory --- |
| 917 | |
| 918 | PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) : |
| 919 | mMaxPoolSize(maxPoolSize) { |
| 920 | } |
| 921 | |
| 922 | PooledInputEventFactory::~PooledInputEventFactory() { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | KeyEvent* PooledInputEventFactory::createKeyEvent() { |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 926 | if (mKeyEventPool.empty()) { |
| 927 | return new KeyEvent(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 928 | } |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 929 | KeyEvent* event = mKeyEventPool.front().release(); |
| 930 | mKeyEventPool.pop(); |
| 931 | return event; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | MotionEvent* PooledInputEventFactory::createMotionEvent() { |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 935 | if (mMotionEventPool.empty()) { |
| 936 | return new MotionEvent(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 937 | } |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 938 | MotionEvent* event = mMotionEventPool.front().release(); |
| 939 | mMotionEventPool.pop(); |
| 940 | return event; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 941 | } |
| 942 | |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 943 | FocusEvent* PooledInputEventFactory::createFocusEvent() { |
| 944 | if (mFocusEventPool.empty()) { |
| 945 | return new FocusEvent(); |
| 946 | } |
| 947 | FocusEvent* event = mFocusEventPool.front().release(); |
| 948 | mFocusEventPool.pop(); |
| 949 | return event; |
| 950 | } |
| 951 | |
Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 952 | CaptureEvent* PooledInputEventFactory::createCaptureEvent() { |
| 953 | if (mCaptureEventPool.empty()) { |
| 954 | return new CaptureEvent(); |
| 955 | } |
| 956 | CaptureEvent* event = mCaptureEventPool.front().release(); |
| 957 | mCaptureEventPool.pop(); |
| 958 | return event; |
| 959 | } |
| 960 | |
arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 961 | DragEvent* PooledInputEventFactory::createDragEvent() { |
| 962 | if (mDragEventPool.empty()) { |
| 963 | return new DragEvent(); |
| 964 | } |
| 965 | DragEvent* event = mDragEventPool.front().release(); |
| 966 | mDragEventPool.pop(); |
| 967 | return event; |
| 968 | } |
| 969 | |
Antonio Kantek | 7cdf8ef | 2021-07-13 18:04:53 -0700 | [diff] [blame] | 970 | TouchModeEvent* PooledInputEventFactory::createTouchModeEvent() { |
| 971 | if (mTouchModeEventPool.empty()) { |
| 972 | return new TouchModeEvent(); |
| 973 | } |
| 974 | TouchModeEvent* event = mTouchModeEventPool.front().release(); |
| 975 | mTouchModeEventPool.pop(); |
| 976 | return event; |
| 977 | } |
| 978 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 979 | void PooledInputEventFactory::recycle(InputEvent* event) { |
| 980 | switch (event->getType()) { |
| 981 | case AINPUT_EVENT_TYPE_KEY: |
| 982 | if (mKeyEventPool.size() < mMaxPoolSize) { |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 983 | mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event))); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 984 | return; |
| 985 | } |
| 986 | break; |
| 987 | case AINPUT_EVENT_TYPE_MOTION: |
| 988 | if (mMotionEventPool.size() < mMaxPoolSize) { |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 989 | mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event))); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 990 | return; |
| 991 | } |
| 992 | break; |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 993 | case AINPUT_EVENT_TYPE_FOCUS: |
| 994 | if (mFocusEventPool.size() < mMaxPoolSize) { |
| 995 | mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event))); |
| 996 | return; |
| 997 | } |
| 998 | break; |
Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 999 | case AINPUT_EVENT_TYPE_CAPTURE: |
| 1000 | if (mCaptureEventPool.size() < mMaxPoolSize) { |
| 1001 | mCaptureEventPool.push( |
| 1002 | std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event))); |
| 1003 | return; |
| 1004 | } |
| 1005 | break; |
arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 1006 | case AINPUT_EVENT_TYPE_DRAG: |
| 1007 | if (mDragEventPool.size() < mMaxPoolSize) { |
| 1008 | mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event))); |
| 1009 | return; |
| 1010 | } |
| 1011 | break; |
Antonio Kantek | eb4a30c | 2021-09-28 17:49:49 -0700 | [diff] [blame] | 1012 | case AINPUT_EVENT_TYPE_TOUCH_MODE: |
| 1013 | if (mTouchModeEventPool.size() < mMaxPoolSize) { |
| 1014 | mTouchModeEventPool.push( |
| 1015 | std::unique_ptr<TouchModeEvent>(static_cast<TouchModeEvent*>(event))); |
| 1016 | return; |
| 1017 | } |
| 1018 | break; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1019 | } |
| 1020 | delete event; |
| 1021 | } |
| 1022 | |
| 1023 | } // namespace android |