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