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