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