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