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