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