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