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