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