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