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