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