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 | |
Elliott Hughes | 6071da7 | 2015-08-12 15:27:47 -0700 | [diff] [blame] | 31 | #ifdef __ANDROID__ |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 32 | #include <binder/Parcel.h> |
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 | |
Elliott Hughes | 6071da7 | 2015-08-12 15:27:47 -0700 | [diff] [blame] | 257 | #ifdef __ANDROID__ |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 258 | status_t PointerCoords::readFromParcel(Parcel* parcel) { |
| 259 | bits = parcel->readInt64(); |
| 260 | |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 261 | uint32_t count = BitSet64::count(bits); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 262 | if (count > MAX_AXES) { |
| 263 | return BAD_VALUE; |
| 264 | } |
| 265 | |
| 266 | for (uint32_t i = 0; i < count; i++) { |
| 267 | values[i] = parcel->readFloat(); |
| 268 | } |
| 269 | return OK; |
| 270 | } |
| 271 | |
| 272 | status_t PointerCoords::writeToParcel(Parcel* parcel) const { |
| 273 | parcel->writeInt64(bits); |
| 274 | |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 275 | uint32_t count = BitSet64::count(bits); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 276 | for (uint32_t i = 0; i < count; i++) { |
| 277 | parcel->writeFloat(values[i]); |
| 278 | } |
| 279 | return OK; |
| 280 | } |
| 281 | #endif |
| 282 | |
| 283 | void PointerCoords::tooManyAxes(int axis) { |
| 284 | ALOGW("Could not set value for axis %d because the PointerCoords structure is full and " |
| 285 | "cannot contain more than %d axis values.", axis, int(MAX_AXES)); |
| 286 | } |
| 287 | |
| 288 | bool PointerCoords::operator==(const PointerCoords& other) const { |
| 289 | if (bits != other.bits) { |
| 290 | return false; |
| 291 | } |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 292 | uint32_t count = BitSet64::count(bits); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 293 | for (uint32_t i = 0; i < count; i++) { |
| 294 | if (values[i] != other.values[i]) { |
| 295 | return false; |
| 296 | } |
| 297 | } |
| 298 | return true; |
| 299 | } |
| 300 | |
| 301 | void PointerCoords::copyFrom(const PointerCoords& other) { |
| 302 | bits = other.bits; |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 303 | uint32_t count = BitSet64::count(bits); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 304 | for (uint32_t i = 0; i < count; i++) { |
| 305 | values[i] = other.values[i]; |
| 306 | } |
| 307 | } |
| 308 | |
chaviw | c01e137 | 2020-07-01 12:37:31 -0700 | [diff] [blame] | 309 | void PointerCoords::transform(const ui::Transform& transform) { |
| 310 | vec2 newCoords = transform.transform(getX(), getY()); |
| 311 | setAxisValue(AMOTION_EVENT_AXIS_X, newCoords.x); |
| 312 | setAxisValue(AMOTION_EVENT_AXIS_Y, newCoords.y); |
| 313 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 314 | |
| 315 | // --- PointerProperties --- |
| 316 | |
| 317 | bool PointerProperties::operator==(const PointerProperties& other) const { |
| 318 | return id == other.id |
| 319 | && toolType == other.toolType; |
| 320 | } |
| 321 | |
| 322 | void PointerProperties::copyFrom(const PointerProperties& other) { |
| 323 | id = other.id; |
| 324 | toolType = other.toolType; |
| 325 | } |
| 326 | |
| 327 | |
| 328 | // --- MotionEvent --- |
| 329 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 330 | 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] | 331 | std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton, |
| 332 | int32_t flags, int32_t edgeFlags, int32_t metaState, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 333 | int32_t buttonState, MotionClassification classification, |
| 334 | const ui::Transform& transform, float xPrecision, float yPrecision, |
| 335 | float rawXCursorPosition, float rawYCursorPosition, nsecs_t downTime, |
| 336 | nsecs_t eventTime, size_t pointerCount, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 337 | const PointerProperties* pointerProperties, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 338 | const PointerCoords* pointerCoords) { |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 339 | InputEvent::initialize(id, deviceId, source, displayId, hmac); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 340 | mAction = action; |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 341 | mActionButton = actionButton; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 342 | mFlags = flags; |
| 343 | mEdgeFlags = edgeFlags; |
| 344 | mMetaState = metaState; |
| 345 | mButtonState = buttonState; |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 346 | mClassification = classification; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 347 | mTransform = transform; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 348 | mXPrecision = xPrecision; |
| 349 | mYPrecision = yPrecision; |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 350 | mRawXCursorPosition = rawXCursorPosition; |
| 351 | mRawYCursorPosition = rawYCursorPosition; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 352 | mDownTime = downTime; |
| 353 | mPointerProperties.clear(); |
| 354 | mPointerProperties.appendArray(pointerProperties, pointerCount); |
| 355 | mSampleEventTimes.clear(); |
| 356 | mSamplePointerCoords.clear(); |
| 357 | addSample(eventTime, pointerCoords); |
| 358 | } |
| 359 | |
| 360 | void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) { |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 361 | InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId, |
| 362 | other->mHmac); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 363 | mAction = other->mAction; |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 364 | mActionButton = other->mActionButton; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 365 | mFlags = other->mFlags; |
| 366 | mEdgeFlags = other->mEdgeFlags; |
| 367 | mMetaState = other->mMetaState; |
| 368 | mButtonState = other->mButtonState; |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 369 | mClassification = other->mClassification; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 370 | mTransform = other->mTransform; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 371 | mXPrecision = other->mXPrecision; |
| 372 | mYPrecision = other->mYPrecision; |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 373 | mRawXCursorPosition = other->mRawXCursorPosition; |
| 374 | mRawYCursorPosition = other->mRawYCursorPosition; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 375 | mDownTime = other->mDownTime; |
| 376 | mPointerProperties = other->mPointerProperties; |
| 377 | |
| 378 | if (keepHistory) { |
| 379 | mSampleEventTimes = other->mSampleEventTimes; |
| 380 | mSamplePointerCoords = other->mSamplePointerCoords; |
| 381 | } else { |
| 382 | mSampleEventTimes.clear(); |
Siarhei Vishniakou | 46a2774 | 2020-09-09 13:57:28 -0500 | [diff] [blame] | 383 | mSampleEventTimes.push_back(other->getEventTime()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 384 | mSamplePointerCoords.clear(); |
| 385 | size_t pointerCount = other->getPointerCount(); |
| 386 | size_t historySize = other->getHistorySize(); |
| 387 | mSamplePointerCoords.appendArray(other->mSamplePointerCoords.array() |
| 388 | + (historySize * pointerCount), pointerCount); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | void MotionEvent::addSample( |
| 393 | int64_t eventTime, |
| 394 | const PointerCoords* pointerCoords) { |
Siarhei Vishniakou | 46a2774 | 2020-09-09 13:57:28 -0500 | [diff] [blame] | 395 | mSampleEventTimes.push_back(eventTime); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 396 | mSamplePointerCoords.appendArray(pointerCoords, getPointerCount()); |
| 397 | } |
| 398 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 399 | float MotionEvent::getXCursorPosition() const { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 400 | vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition()); |
| 401 | return vals.x; |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | float MotionEvent::getYCursorPosition() const { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 405 | vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition()); |
| 406 | return vals.y; |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 409 | void MotionEvent::setCursorPosition(float x, float y) { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 410 | ui::Transform inverse = mTransform.inverse(); |
| 411 | vec2 vals = inverse.transform(x, y); |
| 412 | mRawXCursorPosition = vals.x; |
| 413 | mRawYCursorPosition = vals.y; |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 416 | const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const { |
| 417 | return &mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex]; |
| 418 | } |
| 419 | |
| 420 | float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const { |
| 421 | return getRawPointerCoords(pointerIndex)->getAxisValue(axis); |
| 422 | } |
| 423 | |
| 424 | float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 425 | return getHistoricalAxisValue(axis, pointerIndex, getHistorySize()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | const PointerCoords* MotionEvent::getHistoricalRawPointerCoords( |
| 429 | size_t pointerIndex, size_t historicalIndex) const { |
| 430 | return &mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex]; |
| 431 | } |
| 432 | |
| 433 | float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex, |
| 434 | size_t historicalIndex) const { |
| 435 | return getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis); |
| 436 | } |
| 437 | |
| 438 | float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex, |
| 439 | size_t historicalIndex) const { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 440 | if (axis != AMOTION_EVENT_AXIS_X && axis != AMOTION_EVENT_AXIS_Y) { |
| 441 | return getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getAxisValue(axis); |
| 442 | } |
| 443 | |
| 444 | float rawX = getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getX(); |
| 445 | float rawY = getHistoricalRawPointerCoords(pointerIndex, historicalIndex)->getY(); |
| 446 | vec2 vals = mTransform.transform(rawX, rawY); |
| 447 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 448 | switch (axis) { |
| 449 | case AMOTION_EVENT_AXIS_X: |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 450 | return vals.x; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 451 | case AMOTION_EVENT_AXIS_Y: |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 452 | return vals.y; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 453 | } |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 454 | |
| 455 | // This should never happen |
| 456 | return 0; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const { |
| 460 | size_t pointerCount = mPointerProperties.size(); |
| 461 | for (size_t i = 0; i < pointerCount; i++) { |
| 462 | if (mPointerProperties.itemAt(i).id == pointerId) { |
| 463 | return i; |
| 464 | } |
| 465 | } |
| 466 | return -1; |
| 467 | } |
| 468 | |
| 469 | void MotionEvent::offsetLocation(float xOffset, float yOffset) { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 470 | float currXOffset = mTransform.tx(); |
| 471 | float currYOffset = mTransform.ty(); |
| 472 | mTransform.set(currXOffset + xOffset, currYOffset + yOffset); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 473 | } |
| 474 | |
Robert Carr | e07e103 | 2018-11-26 12:55:53 -0800 | [diff] [blame] | 475 | void MotionEvent::scale(float globalScaleFactor) { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 476 | mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor); |
Robert Carr | e07e103 | 2018-11-26 12:55:53 -0800 | [diff] [blame] | 477 | mXPrecision *= globalScaleFactor; |
| 478 | mYPrecision *= globalScaleFactor; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 479 | |
| 480 | size_t numSamples = mSamplePointerCoords.size(); |
| 481 | for (size_t i = 0; i < numSamples; i++) { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 482 | mSamplePointerCoords.editItemAt(i).scale(globalScaleFactor, globalScaleFactor, |
| 483 | globalScaleFactor); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 487 | 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] | 488 | // Apply perspective transform like Skia. |
| 489 | float newX = matrix[0] * x + matrix[1] * y + matrix[2]; |
| 490 | float newY = matrix[3] * x + matrix[4] * y + matrix[5]; |
| 491 | float newZ = matrix[6] * x + matrix[7] * y + matrix[8]; |
| 492 | if (newZ) { |
| 493 | newZ = 1.0f / newZ; |
| 494 | } |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 495 | vec2 transformedPoint; |
| 496 | transformedPoint.x = newX * newZ; |
| 497 | transformedPoint.y = newY * newZ; |
| 498 | return transformedPoint; |
Jeff Brown | 5a2f68e | 2013-07-15 17:28:19 -0700 | [diff] [blame] | 499 | } |
| 500 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 501 | static float transformAngle(const std::array<float, 9>& matrix, float angleRadians, float originX, |
| 502 | float originY) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 503 | // Construct and transform a vector oriented at the specified clockwise angle from vertical. |
| 504 | // Coordinate system: down is increasing Y, right is increasing X. |
Jeff Brown | 5a2f68e | 2013-07-15 17:28:19 -0700 | [diff] [blame] | 505 | float x = sinf(angleRadians); |
| 506 | float y = -cosf(angleRadians); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 507 | vec2 transformedPoint = transformPoint(matrix, x, y); |
| 508 | |
| 509 | transformedPoint.x -= originX; |
| 510 | transformedPoint.y -= originY; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 511 | |
| 512 | // Derive the transformed vector's clockwise angle from vertical. |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 513 | float result = atan2f(transformedPoint.x, -transformedPoint.y); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 514 | if (result < - M_PI_2) { |
| 515 | result += M_PI; |
| 516 | } else if (result > M_PI_2) { |
| 517 | result -= M_PI; |
| 518 | } |
| 519 | return result; |
| 520 | } |
| 521 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 522 | void MotionEvent::transform(const std::array<float, 9>& matrix) { |
| 523 | // We want to preserve the rawX and rawY so we just update the transform |
| 524 | // using the values of the transform passed in |
| 525 | ui::Transform newTransform; |
| 526 | newTransform.set(matrix); |
| 527 | mTransform = newTransform * mTransform; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 528 | |
Jeff Brown | 5a2f68e | 2013-07-15 17:28:19 -0700 | [diff] [blame] | 529 | // Determine how the origin is transformed by the matrix so that we |
| 530 | // can transform orientation vectors. |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 531 | vec2 origin = transformPoint(matrix, 0, 0); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 532 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 533 | // Apply the transformation to all samples. |
| 534 | size_t numSamples = mSamplePointerCoords.size(); |
| 535 | for (size_t i = 0; i < numSamples; i++) { |
| 536 | PointerCoords& c = mSamplePointerCoords.editItemAt(i); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 537 | float orientation = c.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION); |
Jeff Brown | 5a2f68e | 2013-07-15 17:28:19 -0700 | [diff] [blame] | 538 | c.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 539 | transformAngle(matrix, orientation, origin.x, origin.y)); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 540 | } |
| 541 | } |
| 542 | |
Elliott Hughes | 6071da7 | 2015-08-12 15:27:47 -0700 | [diff] [blame] | 543 | #ifdef __ANDROID__ |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 544 | static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) { |
| 545 | float dsdx, dtdx, tx, dtdy, dsdy, ty; |
| 546 | status_t status = parcel.readFloat(&dsdx); |
| 547 | status |= parcel.readFloat(&dtdx); |
| 548 | status |= parcel.readFloat(&tx); |
| 549 | status |= parcel.readFloat(&dtdy); |
| 550 | status |= parcel.readFloat(&dsdy); |
| 551 | status |= parcel.readFloat(&ty); |
| 552 | |
| 553 | transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1}); |
| 554 | return status; |
| 555 | } |
| 556 | |
| 557 | static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) { |
| 558 | status_t status = parcel.writeFloat(transform.dsdx()); |
| 559 | status |= parcel.writeFloat(transform.dtdx()); |
| 560 | status |= parcel.writeFloat(transform.tx()); |
| 561 | status |= parcel.writeFloat(transform.dtdy()); |
| 562 | status |= parcel.writeFloat(transform.dsdy()); |
| 563 | status |= parcel.writeFloat(transform.ty()); |
| 564 | return status; |
| 565 | } |
| 566 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 567 | status_t MotionEvent::readFromParcel(Parcel* parcel) { |
| 568 | size_t pointerCount = parcel->readInt32(); |
| 569 | size_t sampleCount = parcel->readInt32(); |
Flanker | 552a8a5 | 2015-09-07 15:28:58 +0800 | [diff] [blame] | 570 | if (pointerCount == 0 || pointerCount > MAX_POINTERS || |
| 571 | sampleCount == 0 || sampleCount > MAX_SAMPLES) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 572 | return BAD_VALUE; |
| 573 | } |
| 574 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 575 | mId = parcel->readInt32(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 576 | mDeviceId = parcel->readInt32(); |
Siarhei Vishniakou | 3826d47 | 2020-01-27 10:44:40 -0600 | [diff] [blame] | 577 | mSource = parcel->readUint32(); |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 578 | mDisplayId = parcel->readInt32(); |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 579 | std::vector<uint8_t> hmac; |
| 580 | status_t result = parcel->readByteVector(&hmac); |
| 581 | if (result != OK || hmac.size() != 32) { |
| 582 | return BAD_VALUE; |
| 583 | } |
| 584 | std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 585 | mAction = parcel->readInt32(); |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 586 | mActionButton = parcel->readInt32(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 587 | mFlags = parcel->readInt32(); |
| 588 | mEdgeFlags = parcel->readInt32(); |
| 589 | mMetaState = parcel->readInt32(); |
| 590 | mButtonState = parcel->readInt32(); |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 591 | mClassification = static_cast<MotionClassification>(parcel->readByte()); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 592 | |
| 593 | result = android::readFromParcel(mTransform, *parcel); |
| 594 | if (result != OK) { |
| 595 | return result; |
| 596 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 597 | mXPrecision = parcel->readFloat(); |
| 598 | mYPrecision = parcel->readFloat(); |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 599 | mRawXCursorPosition = parcel->readFloat(); |
| 600 | mRawYCursorPosition = parcel->readFloat(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 601 | mDownTime = parcel->readInt64(); |
| 602 | |
| 603 | mPointerProperties.clear(); |
| 604 | mPointerProperties.setCapacity(pointerCount); |
| 605 | mSampleEventTimes.clear(); |
Siarhei Vishniakou | 46a2774 | 2020-09-09 13:57:28 -0500 | [diff] [blame] | 606 | mSampleEventTimes.reserve(sampleCount); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 607 | mSamplePointerCoords.clear(); |
| 608 | mSamplePointerCoords.setCapacity(sampleCount * pointerCount); |
| 609 | |
| 610 | for (size_t i = 0; i < pointerCount; i++) { |
| 611 | mPointerProperties.push(); |
| 612 | PointerProperties& properties = mPointerProperties.editTop(); |
| 613 | properties.id = parcel->readInt32(); |
| 614 | properties.toolType = parcel->readInt32(); |
| 615 | } |
| 616 | |
Dan Austin | c94fc45 | 2015-09-22 14:22:41 -0700 | [diff] [blame] | 617 | while (sampleCount > 0) { |
| 618 | sampleCount--; |
Siarhei Vishniakou | 46a2774 | 2020-09-09 13:57:28 -0500 | [diff] [blame] | 619 | mSampleEventTimes.push_back(parcel->readInt64()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 620 | for (size_t i = 0; i < pointerCount; i++) { |
| 621 | mSamplePointerCoords.push(); |
| 622 | status_t status = mSamplePointerCoords.editTop().readFromParcel(parcel); |
| 623 | if (status) { |
| 624 | return status; |
| 625 | } |
| 626 | } |
| 627 | } |
| 628 | return OK; |
| 629 | } |
| 630 | |
| 631 | status_t MotionEvent::writeToParcel(Parcel* parcel) const { |
| 632 | size_t pointerCount = mPointerProperties.size(); |
| 633 | size_t sampleCount = mSampleEventTimes.size(); |
| 634 | |
| 635 | parcel->writeInt32(pointerCount); |
| 636 | parcel->writeInt32(sampleCount); |
| 637 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 638 | parcel->writeInt32(mId); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 639 | parcel->writeInt32(mDeviceId); |
Siarhei Vishniakou | 3826d47 | 2020-01-27 10:44:40 -0600 | [diff] [blame] | 640 | parcel->writeUint32(mSource); |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 641 | parcel->writeInt32(mDisplayId); |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 642 | std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end()); |
| 643 | parcel->writeByteVector(hmac); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 644 | parcel->writeInt32(mAction); |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 645 | parcel->writeInt32(mActionButton); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 646 | parcel->writeInt32(mFlags); |
| 647 | parcel->writeInt32(mEdgeFlags); |
| 648 | parcel->writeInt32(mMetaState); |
| 649 | parcel->writeInt32(mButtonState); |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 650 | parcel->writeByte(static_cast<int8_t>(mClassification)); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 651 | |
| 652 | status_t result = android::writeToParcel(mTransform, *parcel); |
| 653 | if (result != OK) { |
| 654 | return result; |
| 655 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 656 | parcel->writeFloat(mXPrecision); |
| 657 | parcel->writeFloat(mYPrecision); |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 658 | parcel->writeFloat(mRawXCursorPosition); |
| 659 | parcel->writeFloat(mRawYCursorPosition); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 660 | parcel->writeInt64(mDownTime); |
| 661 | |
| 662 | for (size_t i = 0; i < pointerCount; i++) { |
| 663 | const PointerProperties& properties = mPointerProperties.itemAt(i); |
| 664 | parcel->writeInt32(properties.id); |
| 665 | parcel->writeInt32(properties.toolType); |
| 666 | } |
| 667 | |
| 668 | const PointerCoords* pc = mSamplePointerCoords.array(); |
| 669 | for (size_t h = 0; h < sampleCount; h++) { |
Siarhei Vishniakou | 46a2774 | 2020-09-09 13:57:28 -0500 | [diff] [blame] | 670 | parcel->writeInt64(mSampleEventTimes[h]); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 671 | for (size_t i = 0; i < pointerCount; i++) { |
| 672 | status_t status = (pc++)->writeToParcel(parcel); |
| 673 | if (status) { |
| 674 | return status; |
| 675 | } |
| 676 | } |
| 677 | } |
| 678 | return OK; |
| 679 | } |
| 680 | #endif |
| 681 | |
Siarhei Vishniakou | 3826d47 | 2020-01-27 10:44:40 -0600 | [diff] [blame] | 682 | bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 683 | if (source & AINPUT_SOURCE_CLASS_POINTER) { |
| 684 | // Specifically excludes HOVER_MOVE and SCROLL. |
| 685 | switch (action & AMOTION_EVENT_ACTION_MASK) { |
| 686 | case AMOTION_EVENT_ACTION_DOWN: |
| 687 | case AMOTION_EVENT_ACTION_MOVE: |
| 688 | case AMOTION_EVENT_ACTION_UP: |
| 689 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 690 | case AMOTION_EVENT_ACTION_POINTER_UP: |
| 691 | case AMOTION_EVENT_ACTION_CANCEL: |
| 692 | case AMOTION_EVENT_ACTION_OUTSIDE: |
| 693 | return true; |
| 694 | } |
| 695 | } |
| 696 | return false; |
| 697 | } |
| 698 | |
Michael Wright | 872db4f | 2014-04-22 15:03:51 -0700 | [diff] [blame] | 699 | const char* MotionEvent::getLabel(int32_t axis) { |
Chris Ye | 4958d06 | 2020-08-20 13:21:10 -0700 | [diff] [blame] | 700 | return InputEventLookup::getAxisLabel(axis); |
Michael Wright | 872db4f | 2014-04-22 15:03:51 -0700 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | int32_t MotionEvent::getAxisFromLabel(const char* label) { |
Chris Ye | 4958d06 | 2020-08-20 13:21:10 -0700 | [diff] [blame] | 704 | return InputEventLookup::getAxisByLabel(label); |
Michael Wright | 872db4f | 2014-04-22 15:03:51 -0700 | [diff] [blame] | 705 | } |
| 706 | |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame^] | 707 | std::string MotionEvent::actionToString(int32_t action) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 708 | // Convert MotionEvent action to string |
| 709 | switch (action & AMOTION_EVENT_ACTION_MASK) { |
| 710 | case AMOTION_EVENT_ACTION_DOWN: |
| 711 | return "DOWN"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 712 | case AMOTION_EVENT_ACTION_UP: |
| 713 | return "UP"; |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame^] | 714 | case AMOTION_EVENT_ACTION_MOVE: |
| 715 | return "MOVE"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 716 | case AMOTION_EVENT_ACTION_CANCEL: |
| 717 | return "CANCEL"; |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame^] | 718 | case AMOTION_EVENT_ACTION_OUTSIDE: |
| 719 | return "OUTSIDE"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 720 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 721 | return "POINTER_DOWN"; |
| 722 | case AMOTION_EVENT_ACTION_POINTER_UP: |
| 723 | return "POINTER_UP"; |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame^] | 724 | case AMOTION_EVENT_ACTION_HOVER_MOVE: |
| 725 | return "HOVER_MOVE"; |
| 726 | case AMOTION_EVENT_ACTION_SCROLL: |
| 727 | return "SCROLL"; |
| 728 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 729 | return "HOVER_ENTER"; |
| 730 | case AMOTION_EVENT_ACTION_HOVER_EXIT: |
| 731 | return "HOVER_EXIT"; |
| 732 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: |
| 733 | return "BUTTON_PRESS"; |
| 734 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: |
| 735 | return "BUTTON_RELEASE"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 736 | } |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame^] | 737 | return android::base::StringPrintf("%" PRId32, action); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 738 | } |
| 739 | |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 740 | // --- FocusEvent --- |
| 741 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 742 | void FocusEvent::initialize(int32_t id, bool hasFocus, bool inTouchMode) { |
| 743 | InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 744 | ADISPLAY_ID_NONE, INVALID_HMAC); |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 745 | mHasFocus = hasFocus; |
| 746 | mInTouchMode = inTouchMode; |
| 747 | } |
| 748 | |
| 749 | void FocusEvent::initialize(const FocusEvent& from) { |
| 750 | InputEvent::initialize(from); |
| 751 | mHasFocus = from.mHasFocus; |
| 752 | mInTouchMode = from.mInTouchMode; |
| 753 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 754 | |
| 755 | // --- PooledInputEventFactory --- |
| 756 | |
| 757 | PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) : |
| 758 | mMaxPoolSize(maxPoolSize) { |
| 759 | } |
| 760 | |
| 761 | PooledInputEventFactory::~PooledInputEventFactory() { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | KeyEvent* PooledInputEventFactory::createKeyEvent() { |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 765 | if (mKeyEventPool.empty()) { |
| 766 | return new KeyEvent(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 767 | } |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 768 | KeyEvent* event = mKeyEventPool.front().release(); |
| 769 | mKeyEventPool.pop(); |
| 770 | return event; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | MotionEvent* PooledInputEventFactory::createMotionEvent() { |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 774 | if (mMotionEventPool.empty()) { |
| 775 | return new MotionEvent(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 776 | } |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 777 | MotionEvent* event = mMotionEventPool.front().release(); |
| 778 | mMotionEventPool.pop(); |
| 779 | return event; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 780 | } |
| 781 | |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 782 | FocusEvent* PooledInputEventFactory::createFocusEvent() { |
| 783 | if (mFocusEventPool.empty()) { |
| 784 | return new FocusEvent(); |
| 785 | } |
| 786 | FocusEvent* event = mFocusEventPool.front().release(); |
| 787 | mFocusEventPool.pop(); |
| 788 | return event; |
| 789 | } |
| 790 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 791 | void PooledInputEventFactory::recycle(InputEvent* event) { |
| 792 | switch (event->getType()) { |
| 793 | case AINPUT_EVENT_TYPE_KEY: |
| 794 | if (mKeyEventPool.size() < mMaxPoolSize) { |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 795 | mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event))); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 796 | return; |
| 797 | } |
| 798 | break; |
| 799 | case AINPUT_EVENT_TYPE_MOTION: |
| 800 | if (mMotionEventPool.size() < mMaxPoolSize) { |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 801 | mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event))); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 802 | return; |
| 803 | } |
| 804 | break; |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 805 | case AINPUT_EVENT_TYPE_FOCUS: |
| 806 | if (mFocusEventPool.size() < mMaxPoolSize) { |
| 807 | mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event))); |
| 808 | return; |
| 809 | } |
| 810 | break; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 811 | } |
| 812 | delete event; |
| 813 | } |
| 814 | |
| 815 | } // namespace android |