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> |
Michael Wright | 635422b | 2022-12-02 00:43:56 +0000 | [diff] [blame] | 24 | #include <optional> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 25 | |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 26 | #include <android-base/file.h> |
Siarhei Vishniakou | 4ded0b0 | 2022-05-26 00:36:48 +0000 | [diff] [blame] | 27 | #include <android-base/logging.h> |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame] | 28 | #include <android-base/stringprintf.h> |
Siarhei Vishniakou | 4ded0b0 | 2022-05-26 00:36:48 +0000 | [diff] [blame] | 29 | #include <cutils/compiler.h> |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 30 | #include <gui/constants.h> |
Prabir Pradhan | 092f3a9 | 2021-11-25 10:53:27 -0800 | [diff] [blame] | 31 | #include <input/DisplayViewport.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 32 | #include <input/Input.h> |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 33 | #include <input/InputDevice.h> |
Michael Wright | 872db4f | 2014-04-22 15:03:51 -0700 | [diff] [blame] | 34 | #include <input/InputEventLabels.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 35 | |
Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 36 | #ifdef __linux__ |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 37 | #include <binder/Parcel.h> |
Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 38 | #endif |
Siarhei Vishniakou | 63740b9 | 2022-10-20 10:28:08 -0700 | [diff] [blame] | 39 | #if defined(__ANDROID__) |
| 40 | #include <sys/random.h> |
| 41 | #endif |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 42 | |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame] | 43 | using android::base::StringPrintf; |
| 44 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 45 | namespace android { |
| 46 | |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 47 | namespace { |
| 48 | |
Prabir Pradhan | 7e1ee56 | 2021-10-26 10:19:49 -0700 | [diff] [blame] | 49 | bool shouldDisregardTransformation(uint32_t source) { |
Prabir Pradhan | 258e2b9 | 2022-06-24 18:37:04 +0000 | [diff] [blame] | 50 | // Do not apply any transformations to axes from joysticks, touchpads, or relative mice. |
Prabir Pradhan | 7e1ee56 | 2021-10-26 10:19:49 -0700 | [diff] [blame] | 51 | return isFromSource(source, AINPUT_SOURCE_CLASS_JOYSTICK) || |
Prabir Pradhan | 258e2b9 | 2022-06-24 18:37:04 +0000 | [diff] [blame] | 52 | isFromSource(source, AINPUT_SOURCE_CLASS_POSITION) || |
| 53 | isFromSource(source, AINPUT_SOURCE_MOUSE_RELATIVE); |
Prabir Pradhan | 7e1ee56 | 2021-10-26 10:19:49 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | bool shouldDisregardOffset(uint32_t source) { |
Prabir Pradhan | 9f38881 | 2021-05-13 16:54:53 -0700 | [diff] [blame] | 57 | // Pointer events are the only type of events that refer to absolute coordinates on the display, |
| 58 | // so we should apply the entire window transform. For other types of events, we should make |
| 59 | // sure to not apply the window translation/offset. |
Prabir Pradhan | 7e1ee56 | 2021-10-26 10:19:49 -0700 | [diff] [blame] | 60 | return !isFromSource(source, AINPUT_SOURCE_CLASS_POINTER); |
Prabir Pradhan | 9f38881 | 2021-05-13 16:54:53 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 63 | } // namespace |
| 64 | |
Siarhei Vishniakou | 16a2e30 | 2019-01-14 19:21:45 -0800 | [diff] [blame] | 65 | const char* motionClassificationToString(MotionClassification classification) { |
| 66 | switch (classification) { |
| 67 | case MotionClassification::NONE: |
| 68 | return "NONE"; |
| 69 | case MotionClassification::AMBIGUOUS_GESTURE: |
| 70 | return "AMBIGUOUS_GESTURE"; |
| 71 | case MotionClassification::DEEP_PRESS: |
| 72 | return "DEEP_PRESS"; |
Harry Cutts | 2800fb0 | 2022-09-15 13:49:23 +0000 | [diff] [blame] | 73 | case MotionClassification::TWO_FINGER_SWIPE: |
| 74 | return "TWO_FINGER_SWIPE"; |
Harry Cutts | c5748d1 | 2022-12-02 17:30:18 +0000 | [diff] [blame] | 75 | case MotionClassification::MULTI_FINGER_SWIPE: |
| 76 | return "MULTI_FINGER_SWIPE"; |
Harry Cutts | b1e8355 | 2022-12-20 11:02:26 +0000 | [diff] [blame] | 77 | case MotionClassification::PINCH: |
| 78 | return "PINCH"; |
Siarhei Vishniakou | 16a2e30 | 2019-01-14 19:21:45 -0800 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
Garfield Tan | 84b087e | 2020-01-23 10:49:05 -0800 | [diff] [blame] | 82 | // --- IdGenerator --- |
Siarhei Vishniakou | 63740b9 | 2022-10-20 10:28:08 -0700 | [diff] [blame] | 83 | #if defined(__ANDROID__) |
| 84 | [[maybe_unused]] |
| 85 | #endif |
| 86 | static status_t |
| 87 | getRandomBytes(uint8_t* data, size_t size) { |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 88 | int ret = TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY | O_CLOEXEC | O_NOFOLLOW)); |
| 89 | if (ret == -1) { |
| 90 | return -errno; |
| 91 | } |
| 92 | |
| 93 | base::unique_fd fd(ret); |
| 94 | if (!base::ReadFully(fd, data, size)) { |
| 95 | return -errno; |
| 96 | } |
| 97 | return OK; |
| 98 | } |
| 99 | |
Garfield Tan | 84b087e | 2020-01-23 10:49:05 -0800 | [diff] [blame] | 100 | IdGenerator::IdGenerator(Source source) : mSource(source) {} |
| 101 | |
| 102 | int32_t IdGenerator::nextId() const { |
| 103 | constexpr uint32_t SEQUENCE_NUMBER_MASK = ~SOURCE_MASK; |
| 104 | int32_t id = 0; |
| 105 | |
Siarhei Vishniakou | 63740b9 | 2022-10-20 10:28:08 -0700 | [diff] [blame] | 106 | #if defined(__ANDROID__) |
| 107 | // On device, prefer 'getrandom' to '/dev/urandom' because it's faster. |
| 108 | constexpr size_t BUF_LEN = sizeof(id); |
| 109 | size_t totalBytes = 0; |
| 110 | while (totalBytes < BUF_LEN) { |
| 111 | ssize_t bytes = TEMP_FAILURE_RETRY(getrandom(&id, BUF_LEN, GRND_NONBLOCK)); |
| 112 | if (CC_UNLIKELY(bytes < 0)) { |
| 113 | ALOGW("Failed to fill in random number for sequence number: %s.", strerror(errno)); |
| 114 | id = 0; |
| 115 | break; |
| 116 | } |
| 117 | totalBytes += bytes; |
| 118 | } |
| 119 | #else |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 120 | #if defined(__linux__) |
Siarhei Vishniakou | 63740b9 | 2022-10-20 10:28:08 -0700 | [diff] [blame] | 121 | // On host, <sys/random.h> / GRND_NONBLOCK is not available |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 122 | while (true) { |
| 123 | status_t result = getRandomBytes(reinterpret_cast<uint8_t*>(&id), sizeof(id)); |
| 124 | if (result == OK) { |
Garfield Tan | 84b087e | 2020-01-23 10:49:05 -0800 | [diff] [blame] | 125 | break; |
| 126 | } |
Garfield Tan | 84b087e | 2020-01-23 10:49:05 -0800 | [diff] [blame] | 127 | } |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 128 | #endif // __linux__ |
Siarhei Vishniakou | 63740b9 | 2022-10-20 10:28:08 -0700 | [diff] [blame] | 129 | #endif // __ANDROID__ |
Garfield Tan | 84b087e | 2020-01-23 10:49:05 -0800 | [diff] [blame] | 130 | return (id & SEQUENCE_NUMBER_MASK) | static_cast<int32_t>(mSource); |
| 131 | } |
| 132 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 133 | // --- InputEvent --- |
| 134 | |
Prabir Pradhan | 00e029d | 2023-03-09 20:11:09 +0000 | [diff] [blame] | 135 | // Due to precision limitations when working with floating points, transforming - namely |
| 136 | // scaling - floating points can lead to minute errors. We round transformed values to approximately |
| 137 | // three decimal places so that values like 0.99997 show up as 1.0. |
| 138 | inline float roundTransformedCoords(float val) { |
| 139 | // Use a power to two to approximate three decimal places to potentially reduce some cycles. |
| 140 | // This should be at least as precise as MotionEvent::ROUNDING_PRECISION. |
| 141 | return std::round(val * 1024.f) / 1024.f; |
| 142 | } |
| 143 | |
| 144 | inline vec2 roundTransformedCoords(vec2 p) { |
| 145 | return {roundTransformedCoords(p.x), roundTransformedCoords(p.y)}; |
| 146 | } |
| 147 | |
Prabir Pradhan | de69f8a | 2021-11-18 16:40:34 +0000 | [diff] [blame] | 148 | vec2 transformWithoutTranslation(const ui::Transform& transform, const vec2& xy) { |
| 149 | const vec2 transformedXy = transform.transform(xy); |
| 150 | const vec2 transformedOrigin = transform.transform(0, 0); |
Prabir Pradhan | 00e029d | 2023-03-09 20:11:09 +0000 | [diff] [blame] | 151 | return roundTransformedCoords(transformedXy - transformedOrigin); |
Prabir Pradhan | de69f8a | 2021-11-18 16:40:34 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Prabir Pradhan | e2e10b4 | 2022-11-17 20:59:36 +0000 | [diff] [blame] | 154 | float transformAngle(const ui::Transform& transform, float angleRadians) { |
| 155 | // Construct and transform a vector oriented at the specified clockwise angle from vertical. |
| 156 | // Coordinate system: down is increasing Y, right is increasing X. |
| 157 | float x = sinf(angleRadians); |
| 158 | float y = -cosf(angleRadians); |
| 159 | vec2 transformedPoint = transform.transform(x, y); |
| 160 | |
| 161 | // Determine how the origin is transformed by the matrix so that we |
| 162 | // can transform orientation vectors. |
| 163 | const vec2 origin = transform.transform(0, 0); |
| 164 | |
| 165 | transformedPoint.x -= origin.x; |
| 166 | transformedPoint.y -= origin.y; |
| 167 | |
| 168 | // Derive the transformed vector's clockwise angle from vertical. |
| 169 | // The return value of atan2f is in range [-pi, pi] which conforms to the orientation API. |
| 170 | return atan2f(transformedPoint.x, -transformedPoint.y); |
| 171 | } |
| 172 | |
Siarhei Vishniakou | d948957 | 2021-11-12 20:08:38 -0800 | [diff] [blame] | 173 | std::string inputEventSourceToString(int32_t source) { |
| 174 | if (source == AINPUT_SOURCE_UNKNOWN) { |
| 175 | return "UNKNOWN"; |
| 176 | } |
| 177 | if (source == static_cast<int32_t>(AINPUT_SOURCE_ANY)) { |
| 178 | return "ANY"; |
| 179 | } |
| 180 | static const std::map<int32_t, const char*> SOURCES{ |
| 181 | {AINPUT_SOURCE_KEYBOARD, "KEYBOARD"}, |
| 182 | {AINPUT_SOURCE_DPAD, "DPAD"}, |
| 183 | {AINPUT_SOURCE_GAMEPAD, "GAMEPAD"}, |
| 184 | {AINPUT_SOURCE_TOUCHSCREEN, "TOUCHSCREEN"}, |
| 185 | {AINPUT_SOURCE_MOUSE, "MOUSE"}, |
| 186 | {AINPUT_SOURCE_STYLUS, "STYLUS"}, |
| 187 | {AINPUT_SOURCE_BLUETOOTH_STYLUS, "BLUETOOTH_STYLUS"}, |
| 188 | {AINPUT_SOURCE_TRACKBALL, "TRACKBALL"}, |
| 189 | {AINPUT_SOURCE_MOUSE_RELATIVE, "MOUSE_RELATIVE"}, |
| 190 | {AINPUT_SOURCE_TOUCHPAD, "TOUCHPAD"}, |
| 191 | {AINPUT_SOURCE_TOUCH_NAVIGATION, "TOUCH_NAVIGATION"}, |
| 192 | {AINPUT_SOURCE_JOYSTICK, "JOYSTICK"}, |
| 193 | {AINPUT_SOURCE_HDMI, "HDMI"}, |
| 194 | {AINPUT_SOURCE_SENSOR, "SENSOR"}, |
| 195 | {AINPUT_SOURCE_ROTARY_ENCODER, "ROTARY_ENCODER"}, |
| 196 | }; |
| 197 | std::string result; |
| 198 | for (const auto& [source_entry, str] : SOURCES) { |
| 199 | if ((source & source_entry) == source_entry) { |
| 200 | if (!result.empty()) { |
| 201 | result += " | "; |
| 202 | } |
| 203 | result += str; |
| 204 | } |
| 205 | } |
| 206 | if (result.empty()) { |
| 207 | result = StringPrintf("0x%08x", source); |
| 208 | } |
| 209 | return result; |
| 210 | } |
| 211 | |
| 212 | bool isFromSource(uint32_t source, uint32_t test) { |
| 213 | return (source & test) == test; |
| 214 | } |
| 215 | |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 216 | bool isStylusToolType(ToolType toolType) { |
| 217 | return toolType == ToolType::STYLUS || toolType == ToolType::ERASER; |
Prabir Pradhan | e562696 | 2022-10-27 20:30:53 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Siarhei Vishniakou | dcc6e6e | 2023-10-18 09:20:07 -0700 | [diff] [blame] | 220 | bool isStylusEvent(uint32_t source, const std::vector<PointerProperties>& properties) { |
| 221 | if (!isFromSource(source, AINPUT_SOURCE_STYLUS)) { |
| 222 | return false; |
| 223 | } |
| 224 | // Need at least one stylus pointer for this event to be considered a stylus event |
| 225 | for (const PointerProperties& pointerProperties : properties) { |
| 226 | if (isStylusToolType(pointerProperties.toolType)) { |
| 227 | return true; |
| 228 | } |
| 229 | } |
| 230 | return false; |
| 231 | } |
| 232 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 233 | VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) { |
| 234 | return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(), |
| 235 | event.getSource(), event.getDisplayId()}, |
| 236 | event.getAction(), |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 237 | event.getFlags() & VERIFIED_KEY_EVENT_FLAGS, |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 238 | event.getDownTime(), |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 239 | event.getKeyCode(), |
| 240 | event.getScanCode(), |
| 241 | event.getMetaState(), |
| 242 | event.getRepeatCount()}; |
| 243 | } |
| 244 | |
| 245 | VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) { |
| 246 | return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(), |
| 247 | event.getSource(), event.getDisplayId()}, |
| 248 | event.getRawX(0), |
| 249 | event.getRawY(0), |
| 250 | event.getActionMasked(), |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 251 | event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS, |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 252 | event.getDownTime(), |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 253 | event.getMetaState(), |
| 254 | event.getButtonState()}; |
| 255 | } |
| 256 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 257 | 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] | 258 | std::array<uint8_t, 32> hmac) { |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 259 | mId = id; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 260 | mDeviceId = deviceId; |
| 261 | mSource = source; |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 262 | mDisplayId = displayId; |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 263 | mHmac = hmac; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | void InputEvent::initialize(const InputEvent& from) { |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 267 | mId = from.mId; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 268 | mDeviceId = from.mDeviceId; |
| 269 | mSource = from.mSource; |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 270 | mDisplayId = from.mDisplayId; |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 271 | mHmac = from.mHmac; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 274 | int32_t InputEvent::nextId() { |
| 275 | static IdGenerator idGen(IdGenerator::Source::OTHER); |
| 276 | return idGen.nextId(); |
| 277 | } |
| 278 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 279 | std::ostream& operator<<(std::ostream& out, const InputEvent& event) { |
| 280 | switch (event.getType()) { |
| 281 | case InputEventType::KEY: { |
| 282 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(event); |
| 283 | out << keyEvent; |
| 284 | return out; |
| 285 | } |
| 286 | case InputEventType::MOTION: { |
| 287 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(event); |
| 288 | out << motionEvent; |
| 289 | return out; |
| 290 | } |
| 291 | case InputEventType::FOCUS: { |
| 292 | out << "FocusEvent"; |
| 293 | return out; |
| 294 | } |
| 295 | case InputEventType::CAPTURE: { |
| 296 | out << "CaptureEvent"; |
| 297 | return out; |
| 298 | } |
| 299 | case InputEventType::DRAG: { |
| 300 | out << "DragEvent"; |
| 301 | return out; |
| 302 | } |
| 303 | case InputEventType::TOUCH_MODE: { |
| 304 | out << "TouchModeEvent"; |
| 305 | return out; |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 310 | // --- KeyEvent --- |
| 311 | |
Michael Wright | 872db4f | 2014-04-22 15:03:51 -0700 | [diff] [blame] | 312 | const char* KeyEvent::getLabel(int32_t keyCode) { |
Chris Ye | 4958d06 | 2020-08-20 13:21:10 -0700 | [diff] [blame] | 313 | return InputEventLookup::getLabelByKeyCode(keyCode); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Siarhei Vishniakou | 5df3493 | 2023-01-23 12:41:01 -0800 | [diff] [blame] | 316 | std::optional<int> KeyEvent::getKeyCodeFromLabel(const char* label) { |
Chris Ye | 4958d06 | 2020-08-20 13:21:10 -0700 | [diff] [blame] | 317 | return InputEventLookup::getKeyCodeByLabel(label); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 320 | 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] | 321 | std::array<uint8_t, 32> hmac, int32_t action, int32_t flags, |
| 322 | int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount, |
| 323 | nsecs_t downTime, nsecs_t eventTime) { |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 324 | InputEvent::initialize(id, deviceId, source, displayId, hmac); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 325 | mAction = action; |
| 326 | mFlags = flags; |
| 327 | mKeyCode = keyCode; |
| 328 | mScanCode = scanCode; |
| 329 | mMetaState = metaState; |
| 330 | mRepeatCount = repeatCount; |
| 331 | mDownTime = downTime; |
| 332 | mEventTime = eventTime; |
| 333 | } |
| 334 | |
| 335 | void KeyEvent::initialize(const KeyEvent& from) { |
| 336 | InputEvent::initialize(from); |
| 337 | mAction = from.mAction; |
| 338 | mFlags = from.mFlags; |
| 339 | mKeyCode = from.mKeyCode; |
| 340 | mScanCode = from.mScanCode; |
| 341 | mMetaState = from.mMetaState; |
| 342 | mRepeatCount = from.mRepeatCount; |
| 343 | mDownTime = from.mDownTime; |
| 344 | mEventTime = from.mEventTime; |
| 345 | } |
| 346 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 347 | const char* KeyEvent::actionToString(int32_t action) { |
| 348 | // Convert KeyEvent action to string |
| 349 | switch (action) { |
| 350 | case AKEY_EVENT_ACTION_DOWN: |
| 351 | return "DOWN"; |
| 352 | case AKEY_EVENT_ACTION_UP: |
| 353 | return "UP"; |
| 354 | case AKEY_EVENT_ACTION_MULTIPLE: |
| 355 | return "MULTIPLE"; |
| 356 | } |
| 357 | return "UNKNOWN"; |
| 358 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 359 | |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 360 | std::ostream& operator<<(std::ostream& out, const KeyEvent& event) { |
| 361 | out << "KeyEvent { action=" << KeyEvent::actionToString(event.getAction()); |
| 362 | |
| 363 | out << ", keycode=" << event.getKeyCode() << "(" << KeyEvent::getLabel(event.getKeyCode()) |
| 364 | << ")"; |
| 365 | |
| 366 | if (event.getMetaState() != 0) { |
| 367 | out << ", metaState=" << event.getMetaState(); |
| 368 | } |
| 369 | |
| 370 | out << ", eventTime=" << event.getEventTime(); |
| 371 | out << ", downTime=" << event.getDownTime(); |
| 372 | out << ", flags=" << std::hex << event.getFlags() << std::dec; |
| 373 | out << ", repeatCount=" << event.getRepeatCount(); |
| 374 | out << ", deviceId=" << event.getDeviceId(); |
| 375 | out << ", source=" << inputEventSourceToString(event.getSource()); |
| 376 | out << ", displayId=" << event.getDisplayId(); |
Prabir Pradhan | f5abab6 | 2024-02-01 20:51:32 +0000 | [diff] [blame] | 377 | out << ", eventId=0x" << std::hex << event.getId() << std::dec; |
Siarhei Vishniakou | d010b01 | 2023-01-18 15:00:53 -0800 | [diff] [blame] | 378 | out << "}"; |
| 379 | return out; |
| 380 | } |
| 381 | |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 382 | std::ostream& operator<<(std::ostream& out, const PointerProperties& properties) { |
| 383 | out << "Pointer(id=" << properties.id << ", " << ftl::enum_string(properties.toolType) << ")"; |
| 384 | return out; |
| 385 | } |
| 386 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 387 | // --- PointerCoords --- |
| 388 | |
| 389 | float PointerCoords::getAxisValue(int32_t axis) const { |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 390 | if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){ |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 391 | return 0; |
| 392 | } |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 393 | return values[BitSet64::getIndexOfBit(bits, axis)]; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | status_t PointerCoords::setAxisValue(int32_t axis, float value) { |
| 397 | if (axis < 0 || axis > 63) { |
| 398 | return NAME_NOT_FOUND; |
| 399 | } |
| 400 | |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 401 | uint32_t index = BitSet64::getIndexOfBit(bits, axis); |
| 402 | if (!BitSet64::hasBit(bits, axis)) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 403 | if (value == 0) { |
| 404 | return OK; // axes with value 0 do not need to be stored |
| 405 | } |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 406 | |
| 407 | uint32_t count = BitSet64::count(bits); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 408 | if (count >= MAX_AXES) { |
| 409 | tooManyAxes(axis); |
| 410 | return NO_MEMORY; |
| 411 | } |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 412 | BitSet64::markBit(bits, axis); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 413 | for (uint32_t i = count; i > index; i--) { |
| 414 | values[i] = values[i - 1]; |
| 415 | } |
| 416 | } |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 417 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 418 | values[index] = value; |
| 419 | return OK; |
| 420 | } |
| 421 | |
| 422 | static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) { |
| 423 | float value = c.getAxisValue(axis); |
| 424 | if (value != 0) { |
| 425 | c.setAxisValue(axis, value * scaleFactor); |
| 426 | } |
| 427 | } |
| 428 | |
Robert Carr | e07e103 | 2018-11-26 12:55:53 -0800 | [diff] [blame] | 429 | void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 430 | // No need to scale pressure or size since they are normalized. |
| 431 | // No need to scale orientation since it is meaningless to do so. |
Robert Carr | e07e103 | 2018-11-26 12:55:53 -0800 | [diff] [blame] | 432 | |
| 433 | // If there is a global scale factor, it is included in the windowX/YScale |
| 434 | // so we don't need to apply it twice to the X/Y axes. |
| 435 | // However we don't want to apply any windowXYScale not included in the global scale |
| 436 | // to the TOUCH_MAJOR/MINOR coordinates. |
| 437 | scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale); |
| 438 | scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale); |
| 439 | scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor); |
| 440 | scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor); |
| 441 | scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor); |
| 442 | scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor); |
Prabir Pradhan | c652358 | 2021-05-14 18:02:55 -0700 | [diff] [blame] | 443 | scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_X, windowXScale); |
| 444 | scaleAxisValue(*this, AMOTION_EVENT_AXIS_RELATIVE_Y, windowYScale); |
Robert Carr | e07e103 | 2018-11-26 12:55:53 -0800 | [diff] [blame] | 445 | } |
| 446 | |
Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 447 | #ifdef __linux__ |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 448 | status_t PointerCoords::readFromParcel(Parcel* parcel) { |
| 449 | bits = parcel->readInt64(); |
| 450 | |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 451 | uint32_t count = BitSet64::count(bits); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 452 | if (count > MAX_AXES) { |
| 453 | return BAD_VALUE; |
| 454 | } |
| 455 | |
| 456 | for (uint32_t i = 0; i < count; i++) { |
| 457 | values[i] = parcel->readFloat(); |
| 458 | } |
Philip Quinn | afb3128 | 2022-12-20 18:17:55 -0800 | [diff] [blame] | 459 | |
| 460 | isResampled = parcel->readBool(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 461 | return OK; |
| 462 | } |
| 463 | |
| 464 | status_t PointerCoords::writeToParcel(Parcel* parcel) const { |
| 465 | parcel->writeInt64(bits); |
| 466 | |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 467 | uint32_t count = BitSet64::count(bits); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 468 | for (uint32_t i = 0; i < count; i++) { |
| 469 | parcel->writeFloat(values[i]); |
| 470 | } |
Philip Quinn | afb3128 | 2022-12-20 18:17:55 -0800 | [diff] [blame] | 471 | |
| 472 | parcel->writeBool(isResampled); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 473 | return OK; |
| 474 | } |
Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 475 | #endif |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 476 | |
| 477 | void PointerCoords::tooManyAxes(int axis) { |
| 478 | ALOGW("Could not set value for axis %d because the PointerCoords structure is full and " |
| 479 | "cannot contain more than %d axis values.", axis, int(MAX_AXES)); |
| 480 | } |
| 481 | |
| 482 | bool PointerCoords::operator==(const PointerCoords& other) const { |
| 483 | if (bits != other.bits) { |
| 484 | return false; |
| 485 | } |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 486 | uint32_t count = BitSet64::count(bits); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 487 | for (uint32_t i = 0; i < count; i++) { |
| 488 | if (values[i] != other.values[i]) { |
| 489 | return false; |
| 490 | } |
| 491 | } |
Philip Quinn | afb3128 | 2022-12-20 18:17:55 -0800 | [diff] [blame] | 492 | if (isResampled != other.isResampled) { |
| 493 | return false; |
| 494 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 495 | return true; |
| 496 | } |
| 497 | |
chaviw | c01e137 | 2020-07-01 12:37:31 -0700 | [diff] [blame] | 498 | void PointerCoords::transform(const ui::Transform& transform) { |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 499 | const vec2 xy = transform.transform(getXYValue()); |
| 500 | setAxisValue(AMOTION_EVENT_AXIS_X, xy.x); |
| 501 | setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y); |
| 502 | |
Prabir Pradhan | c652358 | 2021-05-14 18:02:55 -0700 | [diff] [blame] | 503 | if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_X) || |
| 504 | BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_Y)) { |
| 505 | const ui::Transform rotation(transform.getOrientation()); |
| 506 | const vec2 relativeXy = rotation.transform(getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X), |
| 507 | getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)); |
| 508 | setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x); |
| 509 | setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y); |
| 510 | } |
| 511 | |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 512 | if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) { |
| 513 | const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION); |
| 514 | setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val)); |
| 515 | } |
chaviw | c01e137 | 2020-07-01 12:37:31 -0700 | [diff] [blame] | 516 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 517 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 518 | // --- MotionEvent --- |
| 519 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 520 | 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] | 521 | std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton, |
| 522 | int32_t flags, int32_t edgeFlags, int32_t metaState, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 523 | int32_t buttonState, MotionClassification classification, |
| 524 | const ui::Transform& transform, float xPrecision, float yPrecision, |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 525 | float rawXCursorPosition, float rawYCursorPosition, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 526 | const ui::Transform& rawTransform, nsecs_t downTime, nsecs_t eventTime, |
Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 527 | size_t pointerCount, const PointerProperties* pointerProperties, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 528 | const PointerCoords* pointerCoords) { |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 529 | InputEvent::initialize(id, deviceId, source, displayId, hmac); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 530 | mAction = action; |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 531 | mActionButton = actionButton; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 532 | mFlags = flags; |
| 533 | mEdgeFlags = edgeFlags; |
| 534 | mMetaState = metaState; |
| 535 | mButtonState = buttonState; |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 536 | mClassification = classification; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 537 | mTransform = transform; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 538 | mXPrecision = xPrecision; |
| 539 | mYPrecision = yPrecision; |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 540 | mRawXCursorPosition = rawXCursorPosition; |
| 541 | mRawYCursorPosition = rawYCursorPosition; |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 542 | mRawTransform = rawTransform; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 543 | mDownTime = downTime; |
| 544 | mPointerProperties.clear(); |
Siarhei Vishniakou | 6dbd0ce | 2022-01-13 01:24:14 -0800 | [diff] [blame] | 545 | mPointerProperties.insert(mPointerProperties.end(), &pointerProperties[0], |
| 546 | &pointerProperties[pointerCount]); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 547 | mSampleEventTimes.clear(); |
| 548 | mSamplePointerCoords.clear(); |
| 549 | addSample(eventTime, pointerCoords); |
| 550 | } |
| 551 | |
| 552 | void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) { |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 553 | InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId, |
| 554 | other->mHmac); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 555 | mAction = other->mAction; |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 556 | mActionButton = other->mActionButton; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 557 | mFlags = other->mFlags; |
| 558 | mEdgeFlags = other->mEdgeFlags; |
| 559 | mMetaState = other->mMetaState; |
| 560 | mButtonState = other->mButtonState; |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 561 | mClassification = other->mClassification; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 562 | mTransform = other->mTransform; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 563 | mXPrecision = other->mXPrecision; |
| 564 | mYPrecision = other->mYPrecision; |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 565 | mRawXCursorPosition = other->mRawXCursorPosition; |
| 566 | mRawYCursorPosition = other->mRawYCursorPosition; |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 567 | mRawTransform = other->mRawTransform; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 568 | mDownTime = other->mDownTime; |
| 569 | mPointerProperties = other->mPointerProperties; |
| 570 | |
| 571 | if (keepHistory) { |
| 572 | mSampleEventTimes = other->mSampleEventTimes; |
| 573 | mSamplePointerCoords = other->mSamplePointerCoords; |
| 574 | } else { |
| 575 | mSampleEventTimes.clear(); |
Siarhei Vishniakou | 46a2774 | 2020-09-09 13:57:28 -0500 | [diff] [blame] | 576 | mSampleEventTimes.push_back(other->getEventTime()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 577 | mSamplePointerCoords.clear(); |
| 578 | size_t pointerCount = other->getPointerCount(); |
| 579 | size_t historySize = other->getHistorySize(); |
Siarhei Vishniakou | 6dbd0ce | 2022-01-13 01:24:14 -0800 | [diff] [blame] | 580 | mSamplePointerCoords |
| 581 | .insert(mSamplePointerCoords.end(), |
| 582 | &other->mSamplePointerCoords[historySize * pointerCount], |
| 583 | &other->mSamplePointerCoords[historySize * pointerCount + pointerCount]); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 584 | } |
| 585 | } |
| 586 | |
| 587 | void MotionEvent::addSample( |
| 588 | int64_t eventTime, |
| 589 | const PointerCoords* pointerCoords) { |
Siarhei Vishniakou | 46a2774 | 2020-09-09 13:57:28 -0500 | [diff] [blame] | 590 | mSampleEventTimes.push_back(eventTime); |
Siarhei Vishniakou | 6dbd0ce | 2022-01-13 01:24:14 -0800 | [diff] [blame] | 591 | mSamplePointerCoords.insert(mSamplePointerCoords.end(), &pointerCoords[0], |
| 592 | &pointerCoords[getPointerCount()]); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 593 | } |
| 594 | |
Michael Wright | 635422b | 2022-12-02 00:43:56 +0000 | [diff] [blame] | 595 | std::optional<ui::Rotation> MotionEvent::getSurfaceRotation() const { |
Prabir Pradhan | 092f3a9 | 2021-11-25 10:53:27 -0800 | [diff] [blame] | 596 | // The surface rotation is the rotation from the window's coordinate space to that of the |
| 597 | // display. Since the event's transform takes display space coordinates to window space, the |
| 598 | // returned surface rotation is the inverse of the rotation for the surface. |
| 599 | switch (mTransform.getOrientation()) { |
| 600 | case ui::Transform::ROT_0: |
Michael Wright | 635422b | 2022-12-02 00:43:56 +0000 | [diff] [blame] | 601 | return ui::ROTATION_0; |
Prabir Pradhan | 092f3a9 | 2021-11-25 10:53:27 -0800 | [diff] [blame] | 602 | case ui::Transform::ROT_90: |
Michael Wright | 635422b | 2022-12-02 00:43:56 +0000 | [diff] [blame] | 603 | return ui::ROTATION_270; |
Prabir Pradhan | 092f3a9 | 2021-11-25 10:53:27 -0800 | [diff] [blame] | 604 | case ui::Transform::ROT_180: |
Michael Wright | 635422b | 2022-12-02 00:43:56 +0000 | [diff] [blame] | 605 | return ui::ROTATION_180; |
Prabir Pradhan | 092f3a9 | 2021-11-25 10:53:27 -0800 | [diff] [blame] | 606 | case ui::Transform::ROT_270: |
Michael Wright | 635422b | 2022-12-02 00:43:56 +0000 | [diff] [blame] | 607 | return ui::ROTATION_90; |
Prabir Pradhan | 092f3a9 | 2021-11-25 10:53:27 -0800 | [diff] [blame] | 608 | default: |
Michael Wright | 635422b | 2022-12-02 00:43:56 +0000 | [diff] [blame] | 609 | return std::nullopt; |
Prabir Pradhan | 092f3a9 | 2021-11-25 10:53:27 -0800 | [diff] [blame] | 610 | } |
| 611 | } |
| 612 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 613 | float MotionEvent::getXCursorPosition() const { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 614 | vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition()); |
Prabir Pradhan | 00e029d | 2023-03-09 20:11:09 +0000 | [diff] [blame] | 615 | return roundTransformedCoords(vals.x); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | float MotionEvent::getYCursorPosition() const { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 619 | vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition()); |
Prabir Pradhan | 00e029d | 2023-03-09 20:11:09 +0000 | [diff] [blame] | 620 | return roundTransformedCoords(vals.y); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 621 | } |
| 622 | |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 623 | void MotionEvent::setCursorPosition(float x, float y) { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 624 | ui::Transform inverse = mTransform.inverse(); |
| 625 | vec2 vals = inverse.transform(x, y); |
| 626 | mRawXCursorPosition = vals.x; |
| 627 | mRawYCursorPosition = vals.y; |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 628 | } |
| 629 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 630 | const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const { |
Siarhei Vishniakou | 4ded0b0 | 2022-05-26 00:36:48 +0000 | [diff] [blame] | 631 | if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) { |
| 632 | LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this; |
| 633 | } |
| 634 | const size_t position = getHistorySize() * getPointerCount() + pointerIndex; |
| 635 | if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) { |
| 636 | LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this; |
| 637 | } |
| 638 | return &mSamplePointerCoords[position]; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const { |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 642 | return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 646 | return getHistoricalAxisValue(axis, pointerIndex, getHistorySize()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | const PointerCoords* MotionEvent::getHistoricalRawPointerCoords( |
| 650 | size_t pointerIndex, size_t historicalIndex) const { |
Siarhei Vishniakou | 4ded0b0 | 2022-05-26 00:36:48 +0000 | [diff] [blame] | 651 | if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) { |
| 652 | LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this; |
| 653 | } |
| 654 | if (CC_UNLIKELY(historicalIndex < 0 || historicalIndex > getHistorySize())) { |
| 655 | LOG(FATAL) << __func__ << ": Invalid historical index " << historicalIndex << " for " |
| 656 | << *this; |
| 657 | } |
| 658 | const size_t position = historicalIndex * getPointerCount() + pointerIndex; |
| 659 | if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) { |
| 660 | LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this; |
| 661 | } |
| 662 | return &mSamplePointerCoords[position]; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex, |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 666 | size_t historicalIndex) const { |
Prabir Pradhan | 9eb02c0 | 2021-10-19 14:02:20 -0700 | [diff] [blame] | 667 | const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex); |
| 668 | return calculateTransformedAxisValue(axis, mSource, mRawTransform, coords); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex, |
Prabir Pradhan | 9f38881 | 2021-05-13 16:54:53 -0700 | [diff] [blame] | 672 | size_t historicalIndex) const { |
Prabir Pradhan | 9eb02c0 | 2021-10-19 14:02:20 -0700 | [diff] [blame] | 673 | const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex); |
| 674 | return calculateTransformedAxisValue(axis, mSource, mTransform, coords); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const { |
| 678 | size_t pointerCount = mPointerProperties.size(); |
| 679 | for (size_t i = 0; i < pointerCount; i++) { |
Siarhei Vishniakou | 6dbd0ce | 2022-01-13 01:24:14 -0800 | [diff] [blame] | 680 | if (mPointerProperties[i].id == pointerId) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 681 | return i; |
| 682 | } |
| 683 | } |
| 684 | return -1; |
| 685 | } |
| 686 | |
| 687 | void MotionEvent::offsetLocation(float xOffset, float yOffset) { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 688 | float currXOffset = mTransform.tx(); |
| 689 | float currYOffset = mTransform.ty(); |
| 690 | mTransform.set(currXOffset + xOffset, currYOffset + yOffset); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 691 | } |
| 692 | |
Robert Carr | e07e103 | 2018-11-26 12:55:53 -0800 | [diff] [blame] | 693 | void MotionEvent::scale(float globalScaleFactor) { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 694 | mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor); |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 695 | mRawTransform.set(mRawTransform.tx() * globalScaleFactor, |
| 696 | mRawTransform.ty() * globalScaleFactor); |
Robert Carr | e07e103 | 2018-11-26 12:55:53 -0800 | [diff] [blame] | 697 | mXPrecision *= globalScaleFactor; |
| 698 | mYPrecision *= globalScaleFactor; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 699 | |
| 700 | size_t numSamples = mSamplePointerCoords.size(); |
| 701 | for (size_t i = 0; i < numSamples; i++) { |
Siarhei Vishniakou | 6dbd0ce | 2022-01-13 01:24:14 -0800 | [diff] [blame] | 702 | mSamplePointerCoords[i].scale(globalScaleFactor, globalScaleFactor, globalScaleFactor); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 703 | } |
| 704 | } |
| 705 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 706 | void MotionEvent::transform(const std::array<float, 9>& matrix) { |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 707 | // We want to preserve the raw axes values stored in the PointerCoords, so we just update the |
| 708 | // transform using the values passed in. |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 709 | ui::Transform newTransform; |
| 710 | newTransform.set(matrix); |
| 711 | mTransform = newTransform * mTransform; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 712 | } |
| 713 | |
Evan Rosky | d4d4d80 | 2021-05-03 20:12:21 -0700 | [diff] [blame] | 714 | void MotionEvent::applyTransform(const std::array<float, 9>& matrix) { |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 715 | ui::Transform transform; |
| 716 | transform.set(matrix); |
Evan Rosky | d4d4d80 | 2021-05-03 20:12:21 -0700 | [diff] [blame] | 717 | |
| 718 | // Apply the transformation to all samples. |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 719 | std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(), |
| 720 | [&transform](PointerCoords& c) { c.transform(transform); }); |
Prabir Pradhan | 4b19bd0 | 2021-06-01 17:34:59 -0700 | [diff] [blame] | 721 | |
| 722 | if (mRawXCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION && |
| 723 | mRawYCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) { |
| 724 | const vec2 cursor = transform.transform(mRawXCursorPosition, mRawYCursorPosition); |
| 725 | mRawXCursorPosition = cursor.x; |
| 726 | mRawYCursorPosition = cursor.y; |
| 727 | } |
Evan Rosky | d4d4d80 | 2021-05-03 20:12:21 -0700 | [diff] [blame] | 728 | } |
| 729 | |
Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 730 | #ifdef __linux__ |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 731 | static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) { |
| 732 | float dsdx, dtdx, tx, dtdy, dsdy, ty; |
| 733 | status_t status = parcel.readFloat(&dsdx); |
| 734 | status |= parcel.readFloat(&dtdx); |
| 735 | status |= parcel.readFloat(&tx); |
| 736 | status |= parcel.readFloat(&dtdy); |
| 737 | status |= parcel.readFloat(&dsdy); |
| 738 | status |= parcel.readFloat(&ty); |
| 739 | |
| 740 | transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1}); |
| 741 | return status; |
| 742 | } |
| 743 | |
| 744 | static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) { |
| 745 | status_t status = parcel.writeFloat(transform.dsdx()); |
| 746 | status |= parcel.writeFloat(transform.dtdx()); |
| 747 | status |= parcel.writeFloat(transform.tx()); |
| 748 | status |= parcel.writeFloat(transform.dtdy()); |
| 749 | status |= parcel.writeFloat(transform.dsdy()); |
| 750 | status |= parcel.writeFloat(transform.ty()); |
| 751 | return status; |
| 752 | } |
| 753 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 754 | status_t MotionEvent::readFromParcel(Parcel* parcel) { |
| 755 | size_t pointerCount = parcel->readInt32(); |
| 756 | size_t sampleCount = parcel->readInt32(); |
Flanker | 552a8a5 | 2015-09-07 15:28:58 +0800 | [diff] [blame] | 757 | if (pointerCount == 0 || pointerCount > MAX_POINTERS || |
| 758 | sampleCount == 0 || sampleCount > MAX_SAMPLES) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 759 | return BAD_VALUE; |
| 760 | } |
| 761 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 762 | mId = parcel->readInt32(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 763 | mDeviceId = parcel->readInt32(); |
Siarhei Vishniakou | 3826d47 | 2020-01-27 10:44:40 -0600 | [diff] [blame] | 764 | mSource = parcel->readUint32(); |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 765 | mDisplayId = parcel->readInt32(); |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 766 | std::vector<uint8_t> hmac; |
| 767 | status_t result = parcel->readByteVector(&hmac); |
| 768 | if (result != OK || hmac.size() != 32) { |
| 769 | return BAD_VALUE; |
| 770 | } |
| 771 | std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 772 | mAction = parcel->readInt32(); |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 773 | mActionButton = parcel->readInt32(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 774 | mFlags = parcel->readInt32(); |
| 775 | mEdgeFlags = parcel->readInt32(); |
| 776 | mMetaState = parcel->readInt32(); |
| 777 | mButtonState = parcel->readInt32(); |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 778 | mClassification = static_cast<MotionClassification>(parcel->readByte()); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 779 | |
| 780 | result = android::readFromParcel(mTransform, *parcel); |
| 781 | if (result != OK) { |
| 782 | return result; |
| 783 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 784 | mXPrecision = parcel->readFloat(); |
| 785 | mYPrecision = parcel->readFloat(); |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 786 | mRawXCursorPosition = parcel->readFloat(); |
| 787 | mRawYCursorPosition = parcel->readFloat(); |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 788 | |
| 789 | result = android::readFromParcel(mRawTransform, *parcel); |
| 790 | if (result != OK) { |
| 791 | return result; |
| 792 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 793 | mDownTime = parcel->readInt64(); |
| 794 | |
| 795 | mPointerProperties.clear(); |
Siarhei Vishniakou | 6dbd0ce | 2022-01-13 01:24:14 -0800 | [diff] [blame] | 796 | mPointerProperties.reserve(pointerCount); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 797 | mSampleEventTimes.clear(); |
Siarhei Vishniakou | 46a2774 | 2020-09-09 13:57:28 -0500 | [diff] [blame] | 798 | mSampleEventTimes.reserve(sampleCount); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 799 | mSamplePointerCoords.clear(); |
Siarhei Vishniakou | 6dbd0ce | 2022-01-13 01:24:14 -0800 | [diff] [blame] | 800 | mSamplePointerCoords.reserve(sampleCount * pointerCount); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 801 | |
| 802 | for (size_t i = 0; i < pointerCount; i++) { |
Siarhei Vishniakou | 6dbd0ce | 2022-01-13 01:24:14 -0800 | [diff] [blame] | 803 | mPointerProperties.push_back({}); |
| 804 | PointerProperties& properties = mPointerProperties.back(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 805 | properties.id = parcel->readInt32(); |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 806 | properties.toolType = static_cast<ToolType>(parcel->readInt32()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 807 | } |
| 808 | |
Dan Austin | c94fc45 | 2015-09-22 14:22:41 -0700 | [diff] [blame] | 809 | while (sampleCount > 0) { |
| 810 | sampleCount--; |
Siarhei Vishniakou | 46a2774 | 2020-09-09 13:57:28 -0500 | [diff] [blame] | 811 | mSampleEventTimes.push_back(parcel->readInt64()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 812 | for (size_t i = 0; i < pointerCount; i++) { |
Siarhei Vishniakou | 6dbd0ce | 2022-01-13 01:24:14 -0800 | [diff] [blame] | 813 | mSamplePointerCoords.push_back({}); |
| 814 | status_t status = mSamplePointerCoords.back().readFromParcel(parcel); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 815 | if (status) { |
| 816 | return status; |
| 817 | } |
| 818 | } |
| 819 | } |
| 820 | return OK; |
| 821 | } |
| 822 | |
| 823 | status_t MotionEvent::writeToParcel(Parcel* parcel) const { |
| 824 | size_t pointerCount = mPointerProperties.size(); |
| 825 | size_t sampleCount = mSampleEventTimes.size(); |
| 826 | |
| 827 | parcel->writeInt32(pointerCount); |
| 828 | parcel->writeInt32(sampleCount); |
| 829 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 830 | parcel->writeInt32(mId); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 831 | parcel->writeInt32(mDeviceId); |
Siarhei Vishniakou | 3826d47 | 2020-01-27 10:44:40 -0600 | [diff] [blame] | 832 | parcel->writeUint32(mSource); |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 833 | parcel->writeInt32(mDisplayId); |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 834 | std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end()); |
| 835 | parcel->writeByteVector(hmac); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 836 | parcel->writeInt32(mAction); |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 837 | parcel->writeInt32(mActionButton); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 838 | parcel->writeInt32(mFlags); |
| 839 | parcel->writeInt32(mEdgeFlags); |
| 840 | parcel->writeInt32(mMetaState); |
| 841 | parcel->writeInt32(mButtonState); |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 842 | parcel->writeByte(static_cast<int8_t>(mClassification)); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 843 | |
| 844 | status_t result = android::writeToParcel(mTransform, *parcel); |
| 845 | if (result != OK) { |
| 846 | return result; |
| 847 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 848 | parcel->writeFloat(mXPrecision); |
| 849 | parcel->writeFloat(mYPrecision); |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 850 | parcel->writeFloat(mRawXCursorPosition); |
| 851 | parcel->writeFloat(mRawYCursorPosition); |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 852 | |
| 853 | result = android::writeToParcel(mRawTransform, *parcel); |
| 854 | if (result != OK) { |
| 855 | return result; |
| 856 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 857 | parcel->writeInt64(mDownTime); |
| 858 | |
| 859 | for (size_t i = 0; i < pointerCount; i++) { |
Siarhei Vishniakou | 6dbd0ce | 2022-01-13 01:24:14 -0800 | [diff] [blame] | 860 | const PointerProperties& properties = mPointerProperties[i]; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 861 | parcel->writeInt32(properties.id); |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 862 | parcel->writeInt32(static_cast<int32_t>(properties.toolType)); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 863 | } |
| 864 | |
Siarhei Vishniakou | 6dbd0ce | 2022-01-13 01:24:14 -0800 | [diff] [blame] | 865 | const PointerCoords* pc = mSamplePointerCoords.data(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 866 | for (size_t h = 0; h < sampleCount; h++) { |
Siarhei Vishniakou | 46a2774 | 2020-09-09 13:57:28 -0500 | [diff] [blame] | 867 | parcel->writeInt64(mSampleEventTimes[h]); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 868 | for (size_t i = 0; i < pointerCount; i++) { |
| 869 | status_t status = (pc++)->writeToParcel(parcel); |
| 870 | if (status) { |
| 871 | return status; |
| 872 | } |
| 873 | } |
| 874 | } |
| 875 | return OK; |
| 876 | } |
Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 877 | #endif |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 878 | |
Siarhei Vishniakou | 3826d47 | 2020-01-27 10:44:40 -0600 | [diff] [blame] | 879 | bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) { |
Prabir Pradhan | 7e1ee56 | 2021-10-26 10:19:49 -0700 | [diff] [blame] | 880 | if (isFromSource(source, AINPUT_SOURCE_CLASS_POINTER)) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 881 | // Specifically excludes HOVER_MOVE and SCROLL. |
| 882 | switch (action & AMOTION_EVENT_ACTION_MASK) { |
| 883 | case AMOTION_EVENT_ACTION_DOWN: |
| 884 | case AMOTION_EVENT_ACTION_MOVE: |
| 885 | case AMOTION_EVENT_ACTION_UP: |
| 886 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 887 | case AMOTION_EVENT_ACTION_POINTER_UP: |
| 888 | case AMOTION_EVENT_ACTION_CANCEL: |
| 889 | case AMOTION_EVENT_ACTION_OUTSIDE: |
| 890 | return true; |
| 891 | } |
| 892 | } |
| 893 | return false; |
| 894 | } |
| 895 | |
Michael Wright | 872db4f | 2014-04-22 15:03:51 -0700 | [diff] [blame] | 896 | const char* MotionEvent::getLabel(int32_t axis) { |
Chris Ye | 4958d06 | 2020-08-20 13:21:10 -0700 | [diff] [blame] | 897 | return InputEventLookup::getAxisLabel(axis); |
Michael Wright | 872db4f | 2014-04-22 15:03:51 -0700 | [diff] [blame] | 898 | } |
| 899 | |
Siarhei Vishniakou | 5df3493 | 2023-01-23 12:41:01 -0800 | [diff] [blame] | 900 | std::optional<int> MotionEvent::getAxisFromLabel(const char* label) { |
Chris Ye | 4958d06 | 2020-08-20 13:21:10 -0700 | [diff] [blame] | 901 | return InputEventLookup::getAxisByLabel(label); |
Michael Wright | 872db4f | 2014-04-22 15:03:51 -0700 | [diff] [blame] | 902 | } |
| 903 | |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame] | 904 | std::string MotionEvent::actionToString(int32_t action) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 905 | // Convert MotionEvent action to string |
| 906 | switch (action & AMOTION_EVENT_ACTION_MASK) { |
| 907 | case AMOTION_EVENT_ACTION_DOWN: |
| 908 | return "DOWN"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 909 | case AMOTION_EVENT_ACTION_UP: |
| 910 | return "UP"; |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame] | 911 | case AMOTION_EVENT_ACTION_MOVE: |
| 912 | return "MOVE"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 913 | case AMOTION_EVENT_ACTION_CANCEL: |
| 914 | return "CANCEL"; |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame] | 915 | case AMOTION_EVENT_ACTION_OUTSIDE: |
| 916 | return "OUTSIDE"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 917 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 918 | return StringPrintf("POINTER_DOWN(%" PRId32 ")", MotionEvent::getActionIndex(action)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 919 | case AMOTION_EVENT_ACTION_POINTER_UP: |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 920 | return StringPrintf("POINTER_UP(%" PRId32 ")", MotionEvent::getActionIndex(action)); |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame] | 921 | case AMOTION_EVENT_ACTION_HOVER_MOVE: |
| 922 | return "HOVER_MOVE"; |
| 923 | case AMOTION_EVENT_ACTION_SCROLL: |
| 924 | return "SCROLL"; |
| 925 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 926 | return "HOVER_ENTER"; |
| 927 | case AMOTION_EVENT_ACTION_HOVER_EXIT: |
| 928 | return "HOVER_EXIT"; |
| 929 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: |
| 930 | return "BUTTON_PRESS"; |
| 931 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: |
| 932 | return "BUTTON_RELEASE"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 933 | } |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame] | 934 | return android::base::StringPrintf("%" PRId32, action); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 935 | } |
| 936 | |
Prabir Pradhan | 7e1ee56 | 2021-10-26 10:19:49 -0700 | [diff] [blame] | 937 | // Apply the given transformation to the point without checking whether the entire transform |
| 938 | // should be disregarded altogether for the provided source. |
| 939 | static inline vec2 calculateTransformedXYUnchecked(uint32_t source, const ui::Transform& transform, |
| 940 | const vec2& xy) { |
| 941 | return shouldDisregardOffset(source) ? transformWithoutTranslation(transform, xy) |
Prabir Pradhan | 00e029d | 2023-03-09 20:11:09 +0000 | [diff] [blame] | 942 | : roundTransformedCoords(transform.transform(xy)); |
Prabir Pradhan | 7e1ee56 | 2021-10-26 10:19:49 -0700 | [diff] [blame] | 943 | } |
| 944 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 945 | vec2 MotionEvent::calculateTransformedXY(uint32_t source, const ui::Transform& transform, |
| 946 | const vec2& xy) { |
Prabir Pradhan | 7e1ee56 | 2021-10-26 10:19:49 -0700 | [diff] [blame] | 947 | if (shouldDisregardTransformation(source)) { |
| 948 | return xy; |
| 949 | } |
| 950 | return calculateTransformedXYUnchecked(source, transform, xy); |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 951 | } |
| 952 | |
Prabir Pradhan | 8e6ce22 | 2022-02-24 09:08:54 -0800 | [diff] [blame] | 953 | // Keep in sync with calculateTransformedCoords. |
Prabir Pradhan | 9eb02c0 | 2021-10-19 14:02:20 -0700 | [diff] [blame] | 954 | float MotionEvent::calculateTransformedAxisValue(int32_t axis, uint32_t source, |
| 955 | const ui::Transform& transform, |
| 956 | const PointerCoords& coords) { |
Prabir Pradhan | 7e1ee56 | 2021-10-26 10:19:49 -0700 | [diff] [blame] | 957 | if (shouldDisregardTransformation(source)) { |
| 958 | return coords.getAxisValue(axis); |
| 959 | } |
| 960 | |
Prabir Pradhan | 9eb02c0 | 2021-10-19 14:02:20 -0700 | [diff] [blame] | 961 | if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) { |
Prabir Pradhan | 7e1ee56 | 2021-10-26 10:19:49 -0700 | [diff] [blame] | 962 | const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue()); |
Prabir Pradhan | 9eb02c0 | 2021-10-19 14:02:20 -0700 | [diff] [blame] | 963 | static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1); |
| 964 | return xy[axis]; |
| 965 | } |
| 966 | |
| 967 | if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) { |
| 968 | const vec2 relativeXy = |
| 969 | transformWithoutTranslation(transform, |
| 970 | {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X), |
| 971 | coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)}); |
| 972 | return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y; |
| 973 | } |
| 974 | |
| 975 | if (axis == AMOTION_EVENT_AXIS_ORIENTATION) { |
| 976 | return transformAngle(transform, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 977 | } |
| 978 | |
| 979 | return coords.getAxisValue(axis); |
| 980 | } |
| 981 | |
Prabir Pradhan | 8e6ce22 | 2022-02-24 09:08:54 -0800 | [diff] [blame] | 982 | // Keep in sync with calculateTransformedAxisValue. This is an optimization of |
| 983 | // calculateTransformedAxisValue for all PointerCoords axes. |
| 984 | PointerCoords MotionEvent::calculateTransformedCoords(uint32_t source, |
| 985 | const ui::Transform& transform, |
| 986 | const PointerCoords& coords) { |
| 987 | if (shouldDisregardTransformation(source)) { |
| 988 | return coords; |
| 989 | } |
| 990 | PointerCoords out = coords; |
| 991 | |
| 992 | const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue()); |
| 993 | out.setAxisValue(AMOTION_EVENT_AXIS_X, xy.x); |
| 994 | out.setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y); |
| 995 | |
| 996 | const vec2 relativeXy = |
| 997 | transformWithoutTranslation(transform, |
| 998 | {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X), |
| 999 | coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)}); |
| 1000 | out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x); |
| 1001 | out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y); |
| 1002 | |
| 1003 | out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, |
| 1004 | transformAngle(transform, |
| 1005 | coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION))); |
| 1006 | |
| 1007 | return out; |
| 1008 | } |
| 1009 | |
Siarhei Vishniakou | 4ded0b0 | 2022-05-26 00:36:48 +0000 | [diff] [blame] | 1010 | std::ostream& operator<<(std::ostream& out, const MotionEvent& event) { |
| 1011 | out << "MotionEvent { action=" << MotionEvent::actionToString(event.getAction()); |
| 1012 | if (event.getActionButton() != 0) { |
| 1013 | out << ", actionButton=" << std::to_string(event.getActionButton()); |
| 1014 | } |
| 1015 | const size_t pointerCount = event.getPointerCount(); |
hupeng3 | aa5a51a | 2022-09-02 16:00:18 +0800 | [diff] [blame] | 1016 | LOG_ALWAYS_FATAL_IF(pointerCount > MAX_POINTERS, "Too many pointers : pointerCount = %zu", |
| 1017 | pointerCount); |
Siarhei Vishniakou | 4ded0b0 | 2022-05-26 00:36:48 +0000 | [diff] [blame] | 1018 | for (size_t i = 0; i < pointerCount; i++) { |
| 1019 | out << ", id[" << i << "]=" << event.getPointerId(i); |
| 1020 | float x = event.getX(i); |
| 1021 | float y = event.getY(i); |
| 1022 | if (x != 0 || y != 0) { |
| 1023 | out << ", x[" << i << "]=" << x; |
| 1024 | out << ", y[" << i << "]=" << y; |
| 1025 | } |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1026 | ToolType toolType = event.getToolType(i); |
| 1027 | if (toolType != ToolType::FINGER) { |
| 1028 | out << ", toolType[" << i << "]=" << ftl::enum_string(toolType); |
Siarhei Vishniakou | 4ded0b0 | 2022-05-26 00:36:48 +0000 | [diff] [blame] | 1029 | } |
| 1030 | } |
| 1031 | if (event.getButtonState() != 0) { |
| 1032 | out << ", buttonState=" << event.getButtonState(); |
| 1033 | } |
| 1034 | if (event.getClassification() != MotionClassification::NONE) { |
| 1035 | out << ", classification=" << motionClassificationToString(event.getClassification()); |
| 1036 | } |
| 1037 | if (event.getMetaState() != 0) { |
| 1038 | out << ", metaState=" << event.getMetaState(); |
| 1039 | } |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame^] | 1040 | if (event.getFlags() != 0) { |
| 1041 | out << ", flags=0x" << std::hex << event.getFlags() << std::dec; |
| 1042 | } |
Siarhei Vishniakou | 4ded0b0 | 2022-05-26 00:36:48 +0000 | [diff] [blame] | 1043 | if (event.getEdgeFlags() != 0) { |
| 1044 | out << ", edgeFlags=" << event.getEdgeFlags(); |
| 1045 | } |
| 1046 | if (pointerCount != 1) { |
| 1047 | out << ", pointerCount=" << pointerCount; |
| 1048 | } |
| 1049 | if (event.getHistorySize() != 0) { |
| 1050 | out << ", historySize=" << event.getHistorySize(); |
| 1051 | } |
| 1052 | out << ", eventTime=" << event.getEventTime(); |
| 1053 | out << ", downTime=" << event.getDownTime(); |
| 1054 | out << ", deviceId=" << event.getDeviceId(); |
| 1055 | out << ", source=" << inputEventSourceToString(event.getSource()); |
| 1056 | out << ", displayId=" << event.getDisplayId(); |
Prabir Pradhan | f5abab6 | 2024-02-01 20:51:32 +0000 | [diff] [blame] | 1057 | out << ", eventId=0x" << std::hex << event.getId() << std::dec; |
Siarhei Vishniakou | 4ded0b0 | 2022-05-26 00:36:48 +0000 | [diff] [blame] | 1058 | out << "}"; |
| 1059 | return out; |
| 1060 | } |
| 1061 | |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 1062 | // --- FocusEvent --- |
| 1063 | |
Antonio Kantek | 3cfec7b | 2021-11-05 18:26:17 -0700 | [diff] [blame] | 1064 | void FocusEvent::initialize(int32_t id, bool hasFocus) { |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 1065 | InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 1066 | ADISPLAY_ID_NONE, INVALID_HMAC); |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 1067 | mHasFocus = hasFocus; |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | void FocusEvent::initialize(const FocusEvent& from) { |
| 1071 | InputEvent::initialize(from); |
| 1072 | mHasFocus = from.mHasFocus; |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 1073 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1074 | |
Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 1075 | // --- CaptureEvent --- |
| 1076 | |
| 1077 | void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) { |
| 1078 | InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN, |
| 1079 | ADISPLAY_ID_NONE, INVALID_HMAC); |
| 1080 | mPointerCaptureEnabled = pointerCaptureEnabled; |
| 1081 | } |
| 1082 | |
| 1083 | void CaptureEvent::initialize(const CaptureEvent& from) { |
| 1084 | InputEvent::initialize(from); |
| 1085 | mPointerCaptureEnabled = from.mPointerCaptureEnabled; |
| 1086 | } |
| 1087 | |
arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 1088 | // --- DragEvent --- |
| 1089 | |
| 1090 | void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) { |
| 1091 | InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN, |
| 1092 | ADISPLAY_ID_NONE, INVALID_HMAC); |
| 1093 | mIsExiting = isExiting; |
| 1094 | mX = x; |
| 1095 | mY = y; |
| 1096 | } |
| 1097 | |
| 1098 | void DragEvent::initialize(const DragEvent& from) { |
| 1099 | InputEvent::initialize(from); |
| 1100 | mIsExiting = from.mIsExiting; |
| 1101 | mX = from.mX; |
| 1102 | mY = from.mY; |
| 1103 | } |
| 1104 | |
Antonio Kantek | 7cdf8ef | 2021-07-13 18:04:53 -0700 | [diff] [blame] | 1105 | // --- TouchModeEvent --- |
| 1106 | |
| 1107 | void TouchModeEvent::initialize(int32_t id, bool isInTouchMode) { |
| 1108 | InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN, |
| 1109 | ADISPLAY_ID_NONE, INVALID_HMAC); |
| 1110 | mIsInTouchMode = isInTouchMode; |
| 1111 | } |
| 1112 | |
| 1113 | void TouchModeEvent::initialize(const TouchModeEvent& from) { |
| 1114 | InputEvent::initialize(from); |
| 1115 | mIsInTouchMode = from.mIsInTouchMode; |
| 1116 | } |
| 1117 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1118 | // --- PooledInputEventFactory --- |
| 1119 | |
| 1120 | PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) : |
| 1121 | mMaxPoolSize(maxPoolSize) { |
| 1122 | } |
| 1123 | |
| 1124 | PooledInputEventFactory::~PooledInputEventFactory() { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | KeyEvent* PooledInputEventFactory::createKeyEvent() { |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 1128 | if (mKeyEventPool.empty()) { |
| 1129 | return new KeyEvent(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1130 | } |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 1131 | KeyEvent* event = mKeyEventPool.front().release(); |
| 1132 | mKeyEventPool.pop(); |
| 1133 | return event; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | MotionEvent* PooledInputEventFactory::createMotionEvent() { |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 1137 | if (mMotionEventPool.empty()) { |
| 1138 | return new MotionEvent(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1139 | } |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 1140 | MotionEvent* event = mMotionEventPool.front().release(); |
| 1141 | mMotionEventPool.pop(); |
| 1142 | return event; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1143 | } |
| 1144 | |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 1145 | FocusEvent* PooledInputEventFactory::createFocusEvent() { |
| 1146 | if (mFocusEventPool.empty()) { |
| 1147 | return new FocusEvent(); |
| 1148 | } |
| 1149 | FocusEvent* event = mFocusEventPool.front().release(); |
| 1150 | mFocusEventPool.pop(); |
| 1151 | return event; |
| 1152 | } |
| 1153 | |
Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 1154 | CaptureEvent* PooledInputEventFactory::createCaptureEvent() { |
| 1155 | if (mCaptureEventPool.empty()) { |
| 1156 | return new CaptureEvent(); |
| 1157 | } |
| 1158 | CaptureEvent* event = mCaptureEventPool.front().release(); |
| 1159 | mCaptureEventPool.pop(); |
| 1160 | return event; |
| 1161 | } |
| 1162 | |
arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 1163 | DragEvent* PooledInputEventFactory::createDragEvent() { |
| 1164 | if (mDragEventPool.empty()) { |
| 1165 | return new DragEvent(); |
| 1166 | } |
| 1167 | DragEvent* event = mDragEventPool.front().release(); |
| 1168 | mDragEventPool.pop(); |
| 1169 | return event; |
| 1170 | } |
| 1171 | |
Antonio Kantek | 7cdf8ef | 2021-07-13 18:04:53 -0700 | [diff] [blame] | 1172 | TouchModeEvent* PooledInputEventFactory::createTouchModeEvent() { |
| 1173 | if (mTouchModeEventPool.empty()) { |
| 1174 | return new TouchModeEvent(); |
| 1175 | } |
| 1176 | TouchModeEvent* event = mTouchModeEventPool.front().release(); |
| 1177 | mTouchModeEventPool.pop(); |
| 1178 | return event; |
| 1179 | } |
| 1180 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1181 | void PooledInputEventFactory::recycle(InputEvent* event) { |
| 1182 | switch (event->getType()) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1183 | case InputEventType::KEY: { |
| 1184 | if (mKeyEventPool.size() < mMaxPoolSize) { |
| 1185 | mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event))); |
| 1186 | return; |
| 1187 | } |
| 1188 | break; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1189 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1190 | case InputEventType::MOTION: { |
| 1191 | if (mMotionEventPool.size() < mMaxPoolSize) { |
| 1192 | mMotionEventPool.push( |
| 1193 | std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event))); |
| 1194 | return; |
| 1195 | } |
| 1196 | break; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1197 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1198 | case InputEventType::FOCUS: { |
| 1199 | if (mFocusEventPool.size() < mMaxPoolSize) { |
| 1200 | mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event))); |
| 1201 | return; |
| 1202 | } |
| 1203 | break; |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 1204 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1205 | case InputEventType::CAPTURE: { |
| 1206 | if (mCaptureEventPool.size() < mMaxPoolSize) { |
| 1207 | mCaptureEventPool.push( |
| 1208 | std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event))); |
| 1209 | return; |
| 1210 | } |
| 1211 | break; |
Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 1212 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1213 | case InputEventType::DRAG: { |
| 1214 | if (mDragEventPool.size() < mMaxPoolSize) { |
| 1215 | mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event))); |
| 1216 | return; |
| 1217 | } |
| 1218 | break; |
arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 1219 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1220 | case InputEventType::TOUCH_MODE: { |
| 1221 | if (mTouchModeEventPool.size() < mMaxPoolSize) { |
| 1222 | mTouchModeEventPool.push( |
| 1223 | std::unique_ptr<TouchModeEvent>(static_cast<TouchModeEvent*>(event))); |
| 1224 | return; |
| 1225 | } |
| 1226 | break; |
Antonio Kantek | eb4a30c | 2021-09-28 17:49:49 -0700 | [diff] [blame] | 1227 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1228 | } |
| 1229 | delete event; |
| 1230 | } |
| 1231 | |
| 1232 | } // namespace android |