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