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