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