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