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 | } |
| 418 | return OK; |
| 419 | } |
| 420 | |
| 421 | status_t PointerCoords::writeToParcel(Parcel* parcel) const { |
| 422 | parcel->writeInt64(bits); |
| 423 | |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 424 | uint32_t count = BitSet64::count(bits); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 425 | for (uint32_t i = 0; i < count; i++) { |
| 426 | parcel->writeFloat(values[i]); |
| 427 | } |
| 428 | return OK; |
| 429 | } |
Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 430 | #endif |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 431 | |
| 432 | void PointerCoords::tooManyAxes(int axis) { |
| 433 | ALOGW("Could not set value for axis %d because the PointerCoords structure is full and " |
| 434 | "cannot contain more than %d axis values.", axis, int(MAX_AXES)); |
| 435 | } |
| 436 | |
| 437 | bool PointerCoords::operator==(const PointerCoords& other) const { |
| 438 | if (bits != other.bits) { |
| 439 | return false; |
| 440 | } |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 441 | uint32_t count = BitSet64::count(bits); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 442 | for (uint32_t i = 0; i < count; i++) { |
| 443 | if (values[i] != other.values[i]) { |
| 444 | return false; |
| 445 | } |
| 446 | } |
| 447 | return true; |
| 448 | } |
| 449 | |
chaviw | c01e137 | 2020-07-01 12:37:31 -0700 | [diff] [blame] | 450 | void PointerCoords::transform(const ui::Transform& transform) { |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 451 | const vec2 xy = transform.transform(getXYValue()); |
| 452 | setAxisValue(AMOTION_EVENT_AXIS_X, xy.x); |
| 453 | setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y); |
| 454 | |
Prabir Pradhan | c652358 | 2021-05-14 18:02:55 -0700 | [diff] [blame] | 455 | if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_X) || |
| 456 | BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_RELATIVE_Y)) { |
| 457 | const ui::Transform rotation(transform.getOrientation()); |
| 458 | const vec2 relativeXy = rotation.transform(getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X), |
| 459 | getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)); |
| 460 | setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x); |
| 461 | setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y); |
| 462 | } |
| 463 | |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 464 | if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) { |
| 465 | const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION); |
| 466 | setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val)); |
| 467 | } |
chaviw | c01e137 | 2020-07-01 12:37:31 -0700 | [diff] [blame] | 468 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 469 | |
| 470 | // --- PointerProperties --- |
| 471 | |
| 472 | bool PointerProperties::operator==(const PointerProperties& other) const { |
| 473 | return id == other.id |
| 474 | && toolType == other.toolType; |
| 475 | } |
| 476 | |
| 477 | void PointerProperties::copyFrom(const PointerProperties& other) { |
| 478 | id = other.id; |
| 479 | toolType = other.toolType; |
| 480 | } |
| 481 | |
| 482 | |
| 483 | // --- MotionEvent --- |
| 484 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 485 | 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] | 486 | std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton, |
| 487 | int32_t flags, int32_t edgeFlags, int32_t metaState, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 488 | int32_t buttonState, MotionClassification classification, |
| 489 | const ui::Transform& transform, float xPrecision, float yPrecision, |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 490 | float rawXCursorPosition, float rawYCursorPosition, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 491 | const ui::Transform& rawTransform, nsecs_t downTime, nsecs_t eventTime, |
Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 492 | size_t pointerCount, const PointerProperties* pointerProperties, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 493 | const PointerCoords* pointerCoords) { |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 494 | InputEvent::initialize(id, deviceId, source, displayId, hmac); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 495 | mAction = action; |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 496 | mActionButton = actionButton; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 497 | mFlags = flags; |
| 498 | mEdgeFlags = edgeFlags; |
| 499 | mMetaState = metaState; |
| 500 | mButtonState = buttonState; |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 501 | mClassification = classification; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 502 | mTransform = transform; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 503 | mXPrecision = xPrecision; |
| 504 | mYPrecision = yPrecision; |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 505 | mRawXCursorPosition = rawXCursorPosition; |
| 506 | mRawYCursorPosition = rawYCursorPosition; |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 507 | mRawTransform = rawTransform; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 508 | mDownTime = downTime; |
| 509 | mPointerProperties.clear(); |
Siarhei Vishniakou | 6dbd0ce | 2022-01-13 01:24:14 -0800 | [diff] [blame] | 510 | mPointerProperties.insert(mPointerProperties.end(), &pointerProperties[0], |
| 511 | &pointerProperties[pointerCount]); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 512 | mSampleEventTimes.clear(); |
| 513 | mSamplePointerCoords.clear(); |
| 514 | addSample(eventTime, pointerCoords); |
| 515 | } |
| 516 | |
| 517 | void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) { |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 518 | InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId, |
| 519 | other->mHmac); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 520 | mAction = other->mAction; |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 521 | mActionButton = other->mActionButton; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 522 | mFlags = other->mFlags; |
| 523 | mEdgeFlags = other->mEdgeFlags; |
| 524 | mMetaState = other->mMetaState; |
| 525 | mButtonState = other->mButtonState; |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 526 | mClassification = other->mClassification; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 527 | mTransform = other->mTransform; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 528 | mXPrecision = other->mXPrecision; |
| 529 | mYPrecision = other->mYPrecision; |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 530 | mRawXCursorPosition = other->mRawXCursorPosition; |
| 531 | mRawYCursorPosition = other->mRawYCursorPosition; |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 532 | mRawTransform = other->mRawTransform; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 533 | mDownTime = other->mDownTime; |
| 534 | mPointerProperties = other->mPointerProperties; |
| 535 | |
| 536 | if (keepHistory) { |
| 537 | mSampleEventTimes = other->mSampleEventTimes; |
| 538 | mSamplePointerCoords = other->mSamplePointerCoords; |
| 539 | } else { |
| 540 | mSampleEventTimes.clear(); |
Siarhei Vishniakou | 46a2774 | 2020-09-09 13:57:28 -0500 | [diff] [blame] | 541 | mSampleEventTimes.push_back(other->getEventTime()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 542 | mSamplePointerCoords.clear(); |
| 543 | size_t pointerCount = other->getPointerCount(); |
| 544 | size_t historySize = other->getHistorySize(); |
Siarhei Vishniakou | 6dbd0ce | 2022-01-13 01:24:14 -0800 | [diff] [blame] | 545 | mSamplePointerCoords |
| 546 | .insert(mSamplePointerCoords.end(), |
| 547 | &other->mSamplePointerCoords[historySize * pointerCount], |
| 548 | &other->mSamplePointerCoords[historySize * pointerCount + pointerCount]); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 549 | } |
| 550 | } |
| 551 | |
| 552 | void MotionEvent::addSample( |
| 553 | int64_t eventTime, |
| 554 | const PointerCoords* pointerCoords) { |
Siarhei Vishniakou | 46a2774 | 2020-09-09 13:57:28 -0500 | [diff] [blame] | 555 | mSampleEventTimes.push_back(eventTime); |
Siarhei Vishniakou | 6dbd0ce | 2022-01-13 01:24:14 -0800 | [diff] [blame] | 556 | mSamplePointerCoords.insert(mSamplePointerCoords.end(), &pointerCoords[0], |
| 557 | &pointerCoords[getPointerCount()]); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 558 | } |
| 559 | |
Michael Wright | 635422b | 2022-12-02 00:43:56 +0000 | [diff] [blame] | 560 | std::optional<ui::Rotation> MotionEvent::getSurfaceRotation() const { |
Prabir Pradhan | 092f3a9 | 2021-11-25 10:53:27 -0800 | [diff] [blame] | 561 | // The surface rotation is the rotation from the window's coordinate space to that of the |
| 562 | // display. Since the event's transform takes display space coordinates to window space, the |
| 563 | // returned surface rotation is the inverse of the rotation for the surface. |
| 564 | switch (mTransform.getOrientation()) { |
| 565 | case ui::Transform::ROT_0: |
Michael Wright | 635422b | 2022-12-02 00:43:56 +0000 | [diff] [blame] | 566 | return ui::ROTATION_0; |
Prabir Pradhan | 092f3a9 | 2021-11-25 10:53:27 -0800 | [diff] [blame] | 567 | case ui::Transform::ROT_90: |
Michael Wright | 635422b | 2022-12-02 00:43:56 +0000 | [diff] [blame] | 568 | return ui::ROTATION_270; |
Prabir Pradhan | 092f3a9 | 2021-11-25 10:53:27 -0800 | [diff] [blame] | 569 | case ui::Transform::ROT_180: |
Michael Wright | 635422b | 2022-12-02 00:43:56 +0000 | [diff] [blame] | 570 | return ui::ROTATION_180; |
Prabir Pradhan | 092f3a9 | 2021-11-25 10:53:27 -0800 | [diff] [blame] | 571 | case ui::Transform::ROT_270: |
Michael Wright | 635422b | 2022-12-02 00:43:56 +0000 | [diff] [blame] | 572 | return ui::ROTATION_90; |
Prabir Pradhan | 092f3a9 | 2021-11-25 10:53:27 -0800 | [diff] [blame] | 573 | default: |
Michael Wright | 635422b | 2022-12-02 00:43:56 +0000 | [diff] [blame] | 574 | return std::nullopt; |
Prabir Pradhan | 092f3a9 | 2021-11-25 10:53:27 -0800 | [diff] [blame] | 575 | } |
| 576 | } |
| 577 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 578 | float MotionEvent::getXCursorPosition() const { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 579 | vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition()); |
| 580 | return vals.x; |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | float MotionEvent::getYCursorPosition() const { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 584 | vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition()); |
| 585 | return vals.y; |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 586 | } |
| 587 | |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 588 | void MotionEvent::setCursorPosition(float x, float y) { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 589 | ui::Transform inverse = mTransform.inverse(); |
| 590 | vec2 vals = inverse.transform(x, y); |
| 591 | mRawXCursorPosition = vals.x; |
| 592 | mRawYCursorPosition = vals.y; |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 593 | } |
| 594 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 595 | const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const { |
Siarhei Vishniakou | 4ded0b0 | 2022-05-26 00:36:48 +0000 | [diff] [blame] | 596 | if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) { |
| 597 | LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this; |
| 598 | } |
| 599 | const size_t position = getHistorySize() * getPointerCount() + pointerIndex; |
| 600 | if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) { |
| 601 | LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this; |
| 602 | } |
| 603 | return &mSamplePointerCoords[position]; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const { |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 607 | return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 611 | return getHistoricalAxisValue(axis, pointerIndex, getHistorySize()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | const PointerCoords* MotionEvent::getHistoricalRawPointerCoords( |
| 615 | size_t pointerIndex, size_t historicalIndex) const { |
Siarhei Vishniakou | 4ded0b0 | 2022-05-26 00:36:48 +0000 | [diff] [blame] | 616 | if (CC_UNLIKELY(pointerIndex < 0 || pointerIndex >= getPointerCount())) { |
| 617 | LOG(FATAL) << __func__ << ": Invalid pointer index " << pointerIndex << " for " << *this; |
| 618 | } |
| 619 | if (CC_UNLIKELY(historicalIndex < 0 || historicalIndex > getHistorySize())) { |
| 620 | LOG(FATAL) << __func__ << ": Invalid historical index " << historicalIndex << " for " |
| 621 | << *this; |
| 622 | } |
| 623 | const size_t position = historicalIndex * getPointerCount() + pointerIndex; |
| 624 | if (CC_UNLIKELY(position < 0 || position >= mSamplePointerCoords.size())) { |
| 625 | LOG(FATAL) << __func__ << ": Invalid array index " << position << " for " << *this; |
| 626 | } |
| 627 | return &mSamplePointerCoords[position]; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex, |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 631 | size_t historicalIndex) const { |
Prabir Pradhan | 9eb02c0 | 2021-10-19 14:02:20 -0700 | [diff] [blame] | 632 | const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex); |
| 633 | return calculateTransformedAxisValue(axis, mSource, mRawTransform, coords); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex, |
Prabir Pradhan | 9f38881 | 2021-05-13 16:54:53 -0700 | [diff] [blame] | 637 | size_t historicalIndex) const { |
Prabir Pradhan | 9eb02c0 | 2021-10-19 14:02:20 -0700 | [diff] [blame] | 638 | const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex); |
| 639 | return calculateTransformedAxisValue(axis, mSource, mTransform, coords); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const { |
| 643 | size_t pointerCount = mPointerProperties.size(); |
| 644 | for (size_t i = 0; i < pointerCount; i++) { |
Siarhei Vishniakou | 6dbd0ce | 2022-01-13 01:24:14 -0800 | [diff] [blame] | 645 | if (mPointerProperties[i].id == pointerId) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 646 | return i; |
| 647 | } |
| 648 | } |
| 649 | return -1; |
| 650 | } |
| 651 | |
| 652 | void MotionEvent::offsetLocation(float xOffset, float yOffset) { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 653 | float currXOffset = mTransform.tx(); |
| 654 | float currYOffset = mTransform.ty(); |
| 655 | mTransform.set(currXOffset + xOffset, currYOffset + yOffset); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 656 | } |
| 657 | |
Robert Carr | e07e103 | 2018-11-26 12:55:53 -0800 | [diff] [blame] | 658 | void MotionEvent::scale(float globalScaleFactor) { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 659 | mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor); |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 660 | mRawTransform.set(mRawTransform.tx() * globalScaleFactor, |
| 661 | mRawTransform.ty() * globalScaleFactor); |
Robert Carr | e07e103 | 2018-11-26 12:55:53 -0800 | [diff] [blame] | 662 | mXPrecision *= globalScaleFactor; |
| 663 | mYPrecision *= globalScaleFactor; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 664 | |
| 665 | size_t numSamples = mSamplePointerCoords.size(); |
| 666 | for (size_t i = 0; i < numSamples; i++) { |
Siarhei Vishniakou | 6dbd0ce | 2022-01-13 01:24:14 -0800 | [diff] [blame] | 667 | mSamplePointerCoords[i].scale(globalScaleFactor, globalScaleFactor, globalScaleFactor); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 668 | } |
| 669 | } |
| 670 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 671 | void MotionEvent::transform(const std::array<float, 9>& matrix) { |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 672 | // We want to preserve the raw axes values stored in the PointerCoords, so we just update the |
| 673 | // transform using the values passed in. |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 674 | ui::Transform newTransform; |
| 675 | newTransform.set(matrix); |
| 676 | mTransform = newTransform * mTransform; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 677 | } |
| 678 | |
Evan Rosky | d4d4d80 | 2021-05-03 20:12:21 -0700 | [diff] [blame] | 679 | void MotionEvent::applyTransform(const std::array<float, 9>& matrix) { |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 680 | ui::Transform transform; |
| 681 | transform.set(matrix); |
Evan Rosky | d4d4d80 | 2021-05-03 20:12:21 -0700 | [diff] [blame] | 682 | |
| 683 | // Apply the transformation to all samples. |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 684 | std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(), |
| 685 | [&transform](PointerCoords& c) { c.transform(transform); }); |
Prabir Pradhan | 4b19bd0 | 2021-06-01 17:34:59 -0700 | [diff] [blame] | 686 | |
| 687 | if (mRawXCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION && |
| 688 | mRawYCursorPosition != AMOTION_EVENT_INVALID_CURSOR_POSITION) { |
| 689 | const vec2 cursor = transform.transform(mRawXCursorPosition, mRawYCursorPosition); |
| 690 | mRawXCursorPosition = cursor.x; |
| 691 | mRawYCursorPosition = cursor.y; |
| 692 | } |
Evan Rosky | d4d4d80 | 2021-05-03 20:12:21 -0700 | [diff] [blame] | 693 | } |
| 694 | |
Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 695 | #ifdef __linux__ |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 696 | static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) { |
| 697 | float dsdx, dtdx, tx, dtdy, dsdy, ty; |
| 698 | status_t status = parcel.readFloat(&dsdx); |
| 699 | status |= parcel.readFloat(&dtdx); |
| 700 | status |= parcel.readFloat(&tx); |
| 701 | status |= parcel.readFloat(&dtdy); |
| 702 | status |= parcel.readFloat(&dsdy); |
| 703 | status |= parcel.readFloat(&ty); |
| 704 | |
| 705 | transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1}); |
| 706 | return status; |
| 707 | } |
| 708 | |
| 709 | static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) { |
| 710 | status_t status = parcel.writeFloat(transform.dsdx()); |
| 711 | status |= parcel.writeFloat(transform.dtdx()); |
| 712 | status |= parcel.writeFloat(transform.tx()); |
| 713 | status |= parcel.writeFloat(transform.dtdy()); |
| 714 | status |= parcel.writeFloat(transform.dsdy()); |
| 715 | status |= parcel.writeFloat(transform.ty()); |
| 716 | return status; |
| 717 | } |
| 718 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 719 | status_t MotionEvent::readFromParcel(Parcel* parcel) { |
| 720 | size_t pointerCount = parcel->readInt32(); |
| 721 | size_t sampleCount = parcel->readInt32(); |
Flanker | 552a8a5 | 2015-09-07 15:28:58 +0800 | [diff] [blame] | 722 | if (pointerCount == 0 || pointerCount > MAX_POINTERS || |
| 723 | sampleCount == 0 || sampleCount > MAX_SAMPLES) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 724 | return BAD_VALUE; |
| 725 | } |
| 726 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 727 | mId = parcel->readInt32(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 728 | mDeviceId = parcel->readInt32(); |
Siarhei Vishniakou | 3826d47 | 2020-01-27 10:44:40 -0600 | [diff] [blame] | 729 | mSource = parcel->readUint32(); |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 730 | mDisplayId = parcel->readInt32(); |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 731 | std::vector<uint8_t> hmac; |
| 732 | status_t result = parcel->readByteVector(&hmac); |
| 733 | if (result != OK || hmac.size() != 32) { |
| 734 | return BAD_VALUE; |
| 735 | } |
| 736 | std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 737 | mAction = parcel->readInt32(); |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 738 | mActionButton = parcel->readInt32(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 739 | mFlags = parcel->readInt32(); |
| 740 | mEdgeFlags = parcel->readInt32(); |
| 741 | mMetaState = parcel->readInt32(); |
| 742 | mButtonState = parcel->readInt32(); |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 743 | mClassification = static_cast<MotionClassification>(parcel->readByte()); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 744 | |
| 745 | result = android::readFromParcel(mTransform, *parcel); |
| 746 | if (result != OK) { |
| 747 | return result; |
| 748 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 749 | mXPrecision = parcel->readFloat(); |
| 750 | mYPrecision = parcel->readFloat(); |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 751 | mRawXCursorPosition = parcel->readFloat(); |
| 752 | mRawYCursorPosition = parcel->readFloat(); |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 753 | |
| 754 | result = android::readFromParcel(mRawTransform, *parcel); |
| 755 | if (result != OK) { |
| 756 | return result; |
| 757 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 758 | mDownTime = parcel->readInt64(); |
| 759 | |
| 760 | mPointerProperties.clear(); |
Siarhei Vishniakou | 6dbd0ce | 2022-01-13 01:24:14 -0800 | [diff] [blame] | 761 | mPointerProperties.reserve(pointerCount); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 762 | mSampleEventTimes.clear(); |
Siarhei Vishniakou | 46a2774 | 2020-09-09 13:57:28 -0500 | [diff] [blame] | 763 | mSampleEventTimes.reserve(sampleCount); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 764 | mSamplePointerCoords.clear(); |
Siarhei Vishniakou | 6dbd0ce | 2022-01-13 01:24:14 -0800 | [diff] [blame] | 765 | mSamplePointerCoords.reserve(sampleCount * pointerCount); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 766 | |
| 767 | for (size_t i = 0; i < pointerCount; i++) { |
Siarhei Vishniakou | 6dbd0ce | 2022-01-13 01:24:14 -0800 | [diff] [blame] | 768 | mPointerProperties.push_back({}); |
| 769 | PointerProperties& properties = mPointerProperties.back(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 770 | properties.id = parcel->readInt32(); |
| 771 | properties.toolType = parcel->readInt32(); |
| 772 | } |
| 773 | |
Dan Austin | c94fc45 | 2015-09-22 14:22:41 -0700 | [diff] [blame] | 774 | while (sampleCount > 0) { |
| 775 | sampleCount--; |
Siarhei Vishniakou | 46a2774 | 2020-09-09 13:57:28 -0500 | [diff] [blame] | 776 | mSampleEventTimes.push_back(parcel->readInt64()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 777 | for (size_t i = 0; i < pointerCount; i++) { |
Siarhei Vishniakou | 6dbd0ce | 2022-01-13 01:24:14 -0800 | [diff] [blame] | 778 | mSamplePointerCoords.push_back({}); |
| 779 | status_t status = mSamplePointerCoords.back().readFromParcel(parcel); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 780 | if (status) { |
| 781 | return status; |
| 782 | } |
| 783 | } |
| 784 | } |
| 785 | return OK; |
| 786 | } |
| 787 | |
| 788 | status_t MotionEvent::writeToParcel(Parcel* parcel) const { |
| 789 | size_t pointerCount = mPointerProperties.size(); |
| 790 | size_t sampleCount = mSampleEventTimes.size(); |
| 791 | |
| 792 | parcel->writeInt32(pointerCount); |
| 793 | parcel->writeInt32(sampleCount); |
| 794 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 795 | parcel->writeInt32(mId); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 796 | parcel->writeInt32(mDeviceId); |
Siarhei Vishniakou | 3826d47 | 2020-01-27 10:44:40 -0600 | [diff] [blame] | 797 | parcel->writeUint32(mSource); |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 798 | parcel->writeInt32(mDisplayId); |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 799 | std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end()); |
| 800 | parcel->writeByteVector(hmac); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 801 | parcel->writeInt32(mAction); |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 802 | parcel->writeInt32(mActionButton); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 803 | parcel->writeInt32(mFlags); |
| 804 | parcel->writeInt32(mEdgeFlags); |
| 805 | parcel->writeInt32(mMetaState); |
| 806 | parcel->writeInt32(mButtonState); |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 807 | parcel->writeByte(static_cast<int8_t>(mClassification)); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 808 | |
| 809 | status_t result = android::writeToParcel(mTransform, *parcel); |
| 810 | if (result != OK) { |
| 811 | return result; |
| 812 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 813 | parcel->writeFloat(mXPrecision); |
| 814 | parcel->writeFloat(mYPrecision); |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 815 | parcel->writeFloat(mRawXCursorPosition); |
| 816 | parcel->writeFloat(mRawYCursorPosition); |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 817 | |
| 818 | result = android::writeToParcel(mRawTransform, *parcel); |
| 819 | if (result != OK) { |
| 820 | return result; |
| 821 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 822 | parcel->writeInt64(mDownTime); |
| 823 | |
| 824 | for (size_t i = 0; i < pointerCount; i++) { |
Siarhei Vishniakou | 6dbd0ce | 2022-01-13 01:24:14 -0800 | [diff] [blame] | 825 | const PointerProperties& properties = mPointerProperties[i]; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 826 | parcel->writeInt32(properties.id); |
| 827 | parcel->writeInt32(properties.toolType); |
| 828 | } |
| 829 | |
Siarhei Vishniakou | 6dbd0ce | 2022-01-13 01:24:14 -0800 | [diff] [blame] | 830 | const PointerCoords* pc = mSamplePointerCoords.data(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 831 | for (size_t h = 0; h < sampleCount; h++) { |
Siarhei Vishniakou | 46a2774 | 2020-09-09 13:57:28 -0500 | [diff] [blame] | 832 | parcel->writeInt64(mSampleEventTimes[h]); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 833 | for (size_t i = 0; i < pointerCount; i++) { |
| 834 | status_t status = (pc++)->writeToParcel(parcel); |
| 835 | if (status) { |
| 836 | return status; |
| 837 | } |
| 838 | } |
| 839 | } |
| 840 | return OK; |
| 841 | } |
Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 842 | #endif |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 843 | |
Siarhei Vishniakou | 3826d47 | 2020-01-27 10:44:40 -0600 | [diff] [blame] | 844 | bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) { |
Prabir Pradhan | 7e1ee56 | 2021-10-26 10:19:49 -0700 | [diff] [blame] | 845 | if (isFromSource(source, AINPUT_SOURCE_CLASS_POINTER)) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 846 | // Specifically excludes HOVER_MOVE and SCROLL. |
| 847 | switch (action & AMOTION_EVENT_ACTION_MASK) { |
| 848 | case AMOTION_EVENT_ACTION_DOWN: |
| 849 | case AMOTION_EVENT_ACTION_MOVE: |
| 850 | case AMOTION_EVENT_ACTION_UP: |
| 851 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 852 | case AMOTION_EVENT_ACTION_POINTER_UP: |
| 853 | case AMOTION_EVENT_ACTION_CANCEL: |
| 854 | case AMOTION_EVENT_ACTION_OUTSIDE: |
| 855 | return true; |
| 856 | } |
| 857 | } |
| 858 | return false; |
| 859 | } |
| 860 | |
Michael Wright | 872db4f | 2014-04-22 15:03:51 -0700 | [diff] [blame] | 861 | const char* MotionEvent::getLabel(int32_t axis) { |
Chris Ye | 4958d06 | 2020-08-20 13:21:10 -0700 | [diff] [blame] | 862 | return InputEventLookup::getAxisLabel(axis); |
Michael Wright | 872db4f | 2014-04-22 15:03:51 -0700 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | int32_t MotionEvent::getAxisFromLabel(const char* label) { |
Chris Ye | 4958d06 | 2020-08-20 13:21:10 -0700 | [diff] [blame] | 866 | return InputEventLookup::getAxisByLabel(label); |
Michael Wright | 872db4f | 2014-04-22 15:03:51 -0700 | [diff] [blame] | 867 | } |
| 868 | |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame] | 869 | std::string MotionEvent::actionToString(int32_t action) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 870 | // Convert MotionEvent action to string |
| 871 | switch (action & AMOTION_EVENT_ACTION_MASK) { |
| 872 | case AMOTION_EVENT_ACTION_DOWN: |
| 873 | return "DOWN"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 874 | case AMOTION_EVENT_ACTION_UP: |
| 875 | return "UP"; |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame] | 876 | case AMOTION_EVENT_ACTION_MOVE: |
| 877 | return "MOVE"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 878 | case AMOTION_EVENT_ACTION_CANCEL: |
| 879 | return "CANCEL"; |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame] | 880 | case AMOTION_EVENT_ACTION_OUTSIDE: |
| 881 | return "OUTSIDE"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 882 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 883 | return StringPrintf("POINTER_DOWN(%" PRId32 ")", MotionEvent::getActionIndex(action)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 884 | case AMOTION_EVENT_ACTION_POINTER_UP: |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 885 | return StringPrintf("POINTER_UP(%" PRId32 ")", MotionEvent::getActionIndex(action)); |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame] | 886 | case AMOTION_EVENT_ACTION_HOVER_MOVE: |
| 887 | return "HOVER_MOVE"; |
| 888 | case AMOTION_EVENT_ACTION_SCROLL: |
| 889 | return "SCROLL"; |
| 890 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 891 | return "HOVER_ENTER"; |
| 892 | case AMOTION_EVENT_ACTION_HOVER_EXIT: |
| 893 | return "HOVER_EXIT"; |
| 894 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: |
| 895 | return "BUTTON_PRESS"; |
| 896 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: |
| 897 | return "BUTTON_RELEASE"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 898 | } |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame] | 899 | return android::base::StringPrintf("%" PRId32, action); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 900 | } |
| 901 | |
Prabir Pradhan | 7e1ee56 | 2021-10-26 10:19:49 -0700 | [diff] [blame] | 902 | // Apply the given transformation to the point without checking whether the entire transform |
| 903 | // should be disregarded altogether for the provided source. |
| 904 | static inline vec2 calculateTransformedXYUnchecked(uint32_t source, const ui::Transform& transform, |
| 905 | const vec2& xy) { |
| 906 | return shouldDisregardOffset(source) ? transformWithoutTranslation(transform, xy) |
| 907 | : transform.transform(xy); |
| 908 | } |
| 909 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 910 | vec2 MotionEvent::calculateTransformedXY(uint32_t source, const ui::Transform& transform, |
| 911 | const vec2& xy) { |
Prabir Pradhan | 7e1ee56 | 2021-10-26 10:19:49 -0700 | [diff] [blame] | 912 | if (shouldDisregardTransformation(source)) { |
| 913 | return xy; |
| 914 | } |
| 915 | return calculateTransformedXYUnchecked(source, transform, xy); |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 916 | } |
| 917 | |
Prabir Pradhan | 8e6ce22 | 2022-02-24 09:08:54 -0800 | [diff] [blame] | 918 | // Keep in sync with calculateTransformedCoords. |
Prabir Pradhan | 9eb02c0 | 2021-10-19 14:02:20 -0700 | [diff] [blame] | 919 | float MotionEvent::calculateTransformedAxisValue(int32_t axis, uint32_t source, |
| 920 | const ui::Transform& transform, |
| 921 | const PointerCoords& coords) { |
Prabir Pradhan | 7e1ee56 | 2021-10-26 10:19:49 -0700 | [diff] [blame] | 922 | if (shouldDisregardTransformation(source)) { |
| 923 | return coords.getAxisValue(axis); |
| 924 | } |
| 925 | |
Prabir Pradhan | 9eb02c0 | 2021-10-19 14:02:20 -0700 | [diff] [blame] | 926 | if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) { |
Prabir Pradhan | 7e1ee56 | 2021-10-26 10:19:49 -0700 | [diff] [blame] | 927 | const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue()); |
Prabir Pradhan | 9eb02c0 | 2021-10-19 14:02:20 -0700 | [diff] [blame] | 928 | static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1); |
| 929 | return xy[axis]; |
| 930 | } |
| 931 | |
| 932 | if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) { |
| 933 | const vec2 relativeXy = |
| 934 | transformWithoutTranslation(transform, |
| 935 | {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X), |
| 936 | coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)}); |
| 937 | return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y; |
| 938 | } |
| 939 | |
| 940 | if (axis == AMOTION_EVENT_AXIS_ORIENTATION) { |
| 941 | return transformAngle(transform, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
| 942 | } |
| 943 | |
| 944 | return coords.getAxisValue(axis); |
| 945 | } |
| 946 | |
Prabir Pradhan | 8e6ce22 | 2022-02-24 09:08:54 -0800 | [diff] [blame] | 947 | // Keep in sync with calculateTransformedAxisValue. This is an optimization of |
| 948 | // calculateTransformedAxisValue for all PointerCoords axes. |
| 949 | PointerCoords MotionEvent::calculateTransformedCoords(uint32_t source, |
| 950 | const ui::Transform& transform, |
| 951 | const PointerCoords& coords) { |
| 952 | if (shouldDisregardTransformation(source)) { |
| 953 | return coords; |
| 954 | } |
| 955 | PointerCoords out = coords; |
| 956 | |
| 957 | const vec2 xy = calculateTransformedXYUnchecked(source, transform, coords.getXYValue()); |
| 958 | out.setAxisValue(AMOTION_EVENT_AXIS_X, xy.x); |
| 959 | out.setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y); |
| 960 | |
| 961 | const vec2 relativeXy = |
| 962 | transformWithoutTranslation(transform, |
| 963 | {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X), |
| 964 | coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)}); |
| 965 | out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, relativeXy.x); |
| 966 | out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, relativeXy.y); |
| 967 | |
| 968 | out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, |
| 969 | transformAngle(transform, |
| 970 | coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION))); |
| 971 | |
| 972 | return out; |
| 973 | } |
| 974 | |
Siarhei Vishniakou | 4ded0b0 | 2022-05-26 00:36:48 +0000 | [diff] [blame] | 975 | std::ostream& operator<<(std::ostream& out, const MotionEvent& event) { |
| 976 | out << "MotionEvent { action=" << MotionEvent::actionToString(event.getAction()); |
| 977 | if (event.getActionButton() != 0) { |
| 978 | out << ", actionButton=" << std::to_string(event.getActionButton()); |
| 979 | } |
| 980 | const size_t pointerCount = event.getPointerCount(); |
hupeng3 | aa5a51a | 2022-09-02 16:00:18 +0800 | [diff] [blame] | 981 | LOG_ALWAYS_FATAL_IF(pointerCount > MAX_POINTERS, "Too many pointers : pointerCount = %zu", |
| 982 | pointerCount); |
Siarhei Vishniakou | 4ded0b0 | 2022-05-26 00:36:48 +0000 | [diff] [blame] | 983 | for (size_t i = 0; i < pointerCount; i++) { |
| 984 | out << ", id[" << i << "]=" << event.getPointerId(i); |
| 985 | float x = event.getX(i); |
| 986 | float y = event.getY(i); |
| 987 | if (x != 0 || y != 0) { |
| 988 | out << ", x[" << i << "]=" << x; |
| 989 | out << ", y[" << i << "]=" << y; |
| 990 | } |
| 991 | int toolType = event.getToolType(i); |
| 992 | if (toolType != AMOTION_EVENT_TOOL_TYPE_FINGER) { |
| 993 | out << ", toolType[" << i << "]=" << toolType; |
| 994 | } |
| 995 | } |
| 996 | if (event.getButtonState() != 0) { |
| 997 | out << ", buttonState=" << event.getButtonState(); |
| 998 | } |
| 999 | if (event.getClassification() != MotionClassification::NONE) { |
| 1000 | out << ", classification=" << motionClassificationToString(event.getClassification()); |
| 1001 | } |
| 1002 | if (event.getMetaState() != 0) { |
| 1003 | out << ", metaState=" << event.getMetaState(); |
| 1004 | } |
| 1005 | if (event.getEdgeFlags() != 0) { |
| 1006 | out << ", edgeFlags=" << event.getEdgeFlags(); |
| 1007 | } |
| 1008 | if (pointerCount != 1) { |
| 1009 | out << ", pointerCount=" << pointerCount; |
| 1010 | } |
| 1011 | if (event.getHistorySize() != 0) { |
| 1012 | out << ", historySize=" << event.getHistorySize(); |
| 1013 | } |
| 1014 | out << ", eventTime=" << event.getEventTime(); |
| 1015 | out << ", downTime=" << event.getDownTime(); |
| 1016 | out << ", deviceId=" << event.getDeviceId(); |
| 1017 | out << ", source=" << inputEventSourceToString(event.getSource()); |
| 1018 | out << ", displayId=" << event.getDisplayId(); |
| 1019 | out << ", eventId=" << event.getId(); |
| 1020 | out << "}"; |
| 1021 | return out; |
| 1022 | } |
| 1023 | |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 1024 | // --- FocusEvent --- |
| 1025 | |
Antonio Kantek | 3cfec7b | 2021-11-05 18:26:17 -0700 | [diff] [blame] | 1026 | void FocusEvent::initialize(int32_t id, bool hasFocus) { |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 1027 | InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 1028 | ADISPLAY_ID_NONE, INVALID_HMAC); |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 1029 | mHasFocus = hasFocus; |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | void FocusEvent::initialize(const FocusEvent& from) { |
| 1033 | InputEvent::initialize(from); |
| 1034 | mHasFocus = from.mHasFocus; |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 1035 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1036 | |
Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 1037 | // --- CaptureEvent --- |
| 1038 | |
| 1039 | void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) { |
| 1040 | InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN, |
| 1041 | ADISPLAY_ID_NONE, INVALID_HMAC); |
| 1042 | mPointerCaptureEnabled = pointerCaptureEnabled; |
| 1043 | } |
| 1044 | |
| 1045 | void CaptureEvent::initialize(const CaptureEvent& from) { |
| 1046 | InputEvent::initialize(from); |
| 1047 | mPointerCaptureEnabled = from.mPointerCaptureEnabled; |
| 1048 | } |
| 1049 | |
arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 1050 | // --- DragEvent --- |
| 1051 | |
| 1052 | void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) { |
| 1053 | InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN, |
| 1054 | ADISPLAY_ID_NONE, INVALID_HMAC); |
| 1055 | mIsExiting = isExiting; |
| 1056 | mX = x; |
| 1057 | mY = y; |
| 1058 | } |
| 1059 | |
| 1060 | void DragEvent::initialize(const DragEvent& from) { |
| 1061 | InputEvent::initialize(from); |
| 1062 | mIsExiting = from.mIsExiting; |
| 1063 | mX = from.mX; |
| 1064 | mY = from.mY; |
| 1065 | } |
| 1066 | |
Antonio Kantek | 7cdf8ef | 2021-07-13 18:04:53 -0700 | [diff] [blame] | 1067 | // --- TouchModeEvent --- |
| 1068 | |
| 1069 | void TouchModeEvent::initialize(int32_t id, bool isInTouchMode) { |
| 1070 | InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN, |
| 1071 | ADISPLAY_ID_NONE, INVALID_HMAC); |
| 1072 | mIsInTouchMode = isInTouchMode; |
| 1073 | } |
| 1074 | |
| 1075 | void TouchModeEvent::initialize(const TouchModeEvent& from) { |
| 1076 | InputEvent::initialize(from); |
| 1077 | mIsInTouchMode = from.mIsInTouchMode; |
| 1078 | } |
| 1079 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1080 | // --- PooledInputEventFactory --- |
| 1081 | |
| 1082 | PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) : |
| 1083 | mMaxPoolSize(maxPoolSize) { |
| 1084 | } |
| 1085 | |
| 1086 | PooledInputEventFactory::~PooledInputEventFactory() { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | KeyEvent* PooledInputEventFactory::createKeyEvent() { |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 1090 | if (mKeyEventPool.empty()) { |
| 1091 | return new KeyEvent(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1092 | } |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 1093 | KeyEvent* event = mKeyEventPool.front().release(); |
| 1094 | mKeyEventPool.pop(); |
| 1095 | return event; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | MotionEvent* PooledInputEventFactory::createMotionEvent() { |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 1099 | if (mMotionEventPool.empty()) { |
| 1100 | return new MotionEvent(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1101 | } |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 1102 | MotionEvent* event = mMotionEventPool.front().release(); |
| 1103 | mMotionEventPool.pop(); |
| 1104 | return event; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1105 | } |
| 1106 | |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 1107 | FocusEvent* PooledInputEventFactory::createFocusEvent() { |
| 1108 | if (mFocusEventPool.empty()) { |
| 1109 | return new FocusEvent(); |
| 1110 | } |
| 1111 | FocusEvent* event = mFocusEventPool.front().release(); |
| 1112 | mFocusEventPool.pop(); |
| 1113 | return event; |
| 1114 | } |
| 1115 | |
Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 1116 | CaptureEvent* PooledInputEventFactory::createCaptureEvent() { |
| 1117 | if (mCaptureEventPool.empty()) { |
| 1118 | return new CaptureEvent(); |
| 1119 | } |
| 1120 | CaptureEvent* event = mCaptureEventPool.front().release(); |
| 1121 | mCaptureEventPool.pop(); |
| 1122 | return event; |
| 1123 | } |
| 1124 | |
arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 1125 | DragEvent* PooledInputEventFactory::createDragEvent() { |
| 1126 | if (mDragEventPool.empty()) { |
| 1127 | return new DragEvent(); |
| 1128 | } |
| 1129 | DragEvent* event = mDragEventPool.front().release(); |
| 1130 | mDragEventPool.pop(); |
| 1131 | return event; |
| 1132 | } |
| 1133 | |
Antonio Kantek | 7cdf8ef | 2021-07-13 18:04:53 -0700 | [diff] [blame] | 1134 | TouchModeEvent* PooledInputEventFactory::createTouchModeEvent() { |
| 1135 | if (mTouchModeEventPool.empty()) { |
| 1136 | return new TouchModeEvent(); |
| 1137 | } |
| 1138 | TouchModeEvent* event = mTouchModeEventPool.front().release(); |
| 1139 | mTouchModeEventPool.pop(); |
| 1140 | return event; |
| 1141 | } |
| 1142 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1143 | void PooledInputEventFactory::recycle(InputEvent* event) { |
| 1144 | switch (event->getType()) { |
| 1145 | case AINPUT_EVENT_TYPE_KEY: |
| 1146 | if (mKeyEventPool.size() < mMaxPoolSize) { |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 1147 | mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event))); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1148 | return; |
| 1149 | } |
| 1150 | break; |
| 1151 | case AINPUT_EVENT_TYPE_MOTION: |
| 1152 | if (mMotionEventPool.size() < mMaxPoolSize) { |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 1153 | mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event))); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1154 | return; |
| 1155 | } |
| 1156 | break; |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 1157 | case AINPUT_EVENT_TYPE_FOCUS: |
| 1158 | if (mFocusEventPool.size() < mMaxPoolSize) { |
| 1159 | mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event))); |
| 1160 | return; |
| 1161 | } |
| 1162 | break; |
Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 1163 | case AINPUT_EVENT_TYPE_CAPTURE: |
| 1164 | if (mCaptureEventPool.size() < mMaxPoolSize) { |
| 1165 | mCaptureEventPool.push( |
| 1166 | std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event))); |
| 1167 | return; |
| 1168 | } |
| 1169 | break; |
arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 1170 | case AINPUT_EVENT_TYPE_DRAG: |
| 1171 | if (mDragEventPool.size() < mMaxPoolSize) { |
| 1172 | mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event))); |
| 1173 | return; |
| 1174 | } |
| 1175 | break; |
Antonio Kantek | eb4a30c | 2021-09-28 17:49:49 -0700 | [diff] [blame] | 1176 | case AINPUT_EVENT_TYPE_TOUCH_MODE: |
| 1177 | if (mTouchModeEventPool.size() < mMaxPoolSize) { |
| 1178 | mTouchModeEventPool.push( |
| 1179 | std::unique_ptr<TouchModeEvent>(static_cast<TouchModeEvent*>(event))); |
| 1180 | return; |
| 1181 | } |
| 1182 | break; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1183 | } |
| 1184 | delete event; |
| 1185 | } |
| 1186 | |
| 1187 | } // namespace android |