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