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