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