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