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