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