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