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