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