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 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 20 | #include <attestation/HmacKeyManager.h> |
Garfield Tan | 84b087e | 2020-01-23 10:49:05 -0800 | [diff] [blame] | 21 | #include <cutils/compiler.h> |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame] | 22 | #include <inttypes.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 23 | #include <limits.h> |
Garfield Tan | 84b087e | 2020-01-23 10:49:05 -0800 | [diff] [blame] | 24 | #include <string.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 25 | |
Prabir Pradhan | 7e1443f | 2021-07-23 21:01:55 +0000 | [diff] [blame^] | 26 | #include <android-base/properties.h> |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame] | 27 | #include <android-base/stringprintf.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 28 | #include <input/Input.h> |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 29 | #include <input/InputDevice.h> |
Michael Wright | 872db4f | 2014-04-22 15:03:51 -0700 | [diff] [blame] | 30 | #include <input/InputEventLabels.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 31 | |
Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 32 | #ifdef __linux__ |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 33 | #include <binder/Parcel.h> |
Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 34 | #endif |
Brett Chabot | 5820852 | 2020-09-09 13:55:24 -0700 | [diff] [blame] | 35 | #ifdef __ANDROID__ |
Garfield Tan | 84b087e | 2020-01-23 10:49:05 -0800 | [diff] [blame] | 36 | #include <sys/random.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 37 | #endif |
| 38 | |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame] | 39 | using android::base::StringPrintf; |
| 40 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 41 | namespace android { |
| 42 | |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 43 | namespace { |
| 44 | |
Prabir Pradhan | 7e1443f | 2021-07-23 21:01:55 +0000 | [diff] [blame^] | 45 | // When per-window-input-rotation is enabled, InputFlinger works in the un-rotated display |
| 46 | // coordinates and SurfaceFlinger includes the display rotation in the input window transforms. |
| 47 | bool isPerWindowInputRotationEnabled() { |
| 48 | static const bool PER_WINDOW_INPUT_ROTATION = |
| 49 | base::GetBoolProperty("persist.debug.per_window_input_rotation", false); |
| 50 | |
| 51 | return PER_WINDOW_INPUT_ROTATION; |
| 52 | } |
| 53 | |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 54 | float transformAngle(const ui::Transform& transform, float angleRadians) { |
| 55 | // Construct and transform a vector oriented at the specified clockwise angle from vertical. |
| 56 | // Coordinate system: down is increasing Y, right is increasing X. |
| 57 | float x = sinf(angleRadians); |
| 58 | float y = -cosf(angleRadians); |
| 59 | vec2 transformedPoint = transform.transform(x, y); |
| 60 | |
| 61 | // Determine how the origin is transformed by the matrix so that we |
| 62 | // can transform orientation vectors. |
| 63 | const vec2 origin = transform.transform(0, 0); |
| 64 | |
| 65 | transformedPoint.x -= origin.x; |
| 66 | transformedPoint.y -= origin.y; |
| 67 | |
| 68 | // Derive the transformed vector's clockwise angle from vertical. |
| 69 | float result = atan2f(transformedPoint.x, -transformedPoint.y); |
| 70 | if (result < -M_PI_2) { |
| 71 | result += M_PI; |
| 72 | } else if (result > M_PI_2) { |
| 73 | result -= M_PI; |
| 74 | } |
| 75 | return result; |
| 76 | } |
| 77 | |
| 78 | // Rotates the given point to the transform's orientation. If the display width and height are |
| 79 | // provided, the point is rotated in the screen space. Otherwise, the point is rotated about the |
| 80 | // origin. This helper is used to avoid the extra overhead of creating new Transforms. |
| 81 | vec2 rotatePoint(const ui::Transform& transform, float x, float y, int32_t displayWidth = 0, |
| 82 | int32_t displayHeight = 0) { |
| 83 | // 0x7 encapsulates all 3 rotations (see ui::Transform::RotationFlags) |
| 84 | static const int ALL_ROTATIONS_MASK = 0x7; |
| 85 | const uint32_t orientation = (transform.getOrientation() & ALL_ROTATIONS_MASK); |
| 86 | if (orientation == ui::Transform::ROT_0) { |
| 87 | return {x, y}; |
| 88 | } |
| 89 | |
| 90 | vec2 xy(x, y); |
| 91 | if (orientation == ui::Transform::ROT_90) { |
| 92 | xy.x = displayHeight - y; |
| 93 | xy.y = x; |
| 94 | } else if (orientation == ui::Transform::ROT_180) { |
| 95 | xy.x = displayWidth - x; |
| 96 | xy.y = displayHeight - y; |
| 97 | } else if (orientation == ui::Transform::ROT_270) { |
| 98 | xy.x = y; |
| 99 | xy.y = displayWidth - x; |
| 100 | } |
| 101 | return xy; |
| 102 | } |
| 103 | |
| 104 | } // namespace |
| 105 | |
Siarhei Vishniakou | 16a2e30 | 2019-01-14 19:21:45 -0800 | [diff] [blame] | 106 | const char* motionClassificationToString(MotionClassification classification) { |
| 107 | switch (classification) { |
| 108 | case MotionClassification::NONE: |
| 109 | return "NONE"; |
| 110 | case MotionClassification::AMBIGUOUS_GESTURE: |
| 111 | return "AMBIGUOUS_GESTURE"; |
| 112 | case MotionClassification::DEEP_PRESS: |
| 113 | return "DEEP_PRESS"; |
| 114 | } |
| 115 | } |
| 116 | |
Garfield Tan | 84b087e | 2020-01-23 10:49:05 -0800 | [diff] [blame] | 117 | // --- IdGenerator --- |
| 118 | IdGenerator::IdGenerator(Source source) : mSource(source) {} |
| 119 | |
| 120 | int32_t IdGenerator::nextId() const { |
| 121 | constexpr uint32_t SEQUENCE_NUMBER_MASK = ~SOURCE_MASK; |
| 122 | int32_t id = 0; |
| 123 | |
| 124 | // Avoid building against syscall getrandom(2) on host, which will fail build on Mac. Host doesn't |
| 125 | // use sequence number so just always return mSource. |
| 126 | #ifdef __ANDROID__ |
| 127 | constexpr size_t BUF_LEN = sizeof(id); |
| 128 | size_t totalBytes = 0; |
| 129 | while (totalBytes < BUF_LEN) { |
| 130 | ssize_t bytes = TEMP_FAILURE_RETRY(getrandom(&id, BUF_LEN, GRND_NONBLOCK)); |
| 131 | if (CC_UNLIKELY(bytes < 0)) { |
| 132 | ALOGW("Failed to fill in random number for sequence number: %s.", strerror(errno)); |
| 133 | id = 0; |
| 134 | break; |
| 135 | } |
| 136 | totalBytes += bytes; |
| 137 | } |
| 138 | #endif // __ANDROID__ |
| 139 | |
| 140 | return (id & SEQUENCE_NUMBER_MASK) | static_cast<int32_t>(mSource); |
| 141 | } |
| 142 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 143 | // --- InputEvent --- |
| 144 | |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 145 | const char* inputEventTypeToString(int32_t type) { |
| 146 | switch (type) { |
| 147 | case AINPUT_EVENT_TYPE_KEY: { |
| 148 | return "KEY"; |
| 149 | } |
| 150 | case AINPUT_EVENT_TYPE_MOTION: { |
| 151 | return "MOTION"; |
| 152 | } |
| 153 | case AINPUT_EVENT_TYPE_FOCUS: { |
| 154 | return "FOCUS"; |
| 155 | } |
Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 156 | case AINPUT_EVENT_TYPE_CAPTURE: { |
| 157 | return "CAPTURE"; |
| 158 | } |
arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 159 | case AINPUT_EVENT_TYPE_DRAG: { |
| 160 | return "DRAG"; |
| 161 | } |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 162 | } |
| 163 | return "UNKNOWN"; |
| 164 | } |
| 165 | |
Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 166 | VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) { |
| 167 | return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(), |
| 168 | event.getSource(), event.getDisplayId()}, |
| 169 | event.getAction(), |
| 170 | event.getDownTime(), |
| 171 | event.getFlags() & VERIFIED_KEY_EVENT_FLAGS, |
| 172 | event.getKeyCode(), |
| 173 | event.getScanCode(), |
| 174 | event.getMetaState(), |
| 175 | event.getRepeatCount()}; |
| 176 | } |
| 177 | |
| 178 | VerifiedMotionEvent verifiedMotionEventFromMotionEvent(const MotionEvent& event) { |
| 179 | return {{VerifiedInputEvent::Type::MOTION, event.getDeviceId(), event.getEventTime(), |
| 180 | event.getSource(), event.getDisplayId()}, |
| 181 | event.getRawX(0), |
| 182 | event.getRawY(0), |
| 183 | event.getActionMasked(), |
| 184 | event.getDownTime(), |
| 185 | event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS, |
| 186 | event.getMetaState(), |
| 187 | event.getButtonState()}; |
| 188 | } |
| 189 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 190 | void InputEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 191 | std::array<uint8_t, 32> hmac) { |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 192 | mId = id; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 193 | mDeviceId = deviceId; |
| 194 | mSource = source; |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 195 | mDisplayId = displayId; |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 196 | mHmac = hmac; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void InputEvent::initialize(const InputEvent& from) { |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 200 | mId = from.mId; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 201 | mDeviceId = from.mDeviceId; |
| 202 | mSource = from.mSource; |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 203 | mDisplayId = from.mDisplayId; |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 204 | mHmac = from.mHmac; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 207 | int32_t InputEvent::nextId() { |
| 208 | static IdGenerator idGen(IdGenerator::Source::OTHER); |
| 209 | return idGen.nextId(); |
| 210 | } |
| 211 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 212 | // --- KeyEvent --- |
| 213 | |
Michael Wright | 872db4f | 2014-04-22 15:03:51 -0700 | [diff] [blame] | 214 | const char* KeyEvent::getLabel(int32_t keyCode) { |
Chris Ye | 4958d06 | 2020-08-20 13:21:10 -0700 | [diff] [blame] | 215 | return InputEventLookup::getLabelByKeyCode(keyCode); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Michael Wright | 872db4f | 2014-04-22 15:03:51 -0700 | [diff] [blame] | 218 | int32_t KeyEvent::getKeyCodeFromLabel(const char* label) { |
Chris Ye | 4958d06 | 2020-08-20 13:21:10 -0700 | [diff] [blame] | 219 | return InputEventLookup::getKeyCodeByLabel(label); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 222 | void KeyEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 223 | std::array<uint8_t, 32> hmac, int32_t action, int32_t flags, |
| 224 | int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount, |
| 225 | nsecs_t downTime, nsecs_t eventTime) { |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 226 | InputEvent::initialize(id, deviceId, source, displayId, hmac); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 227 | mAction = action; |
| 228 | mFlags = flags; |
| 229 | mKeyCode = keyCode; |
| 230 | mScanCode = scanCode; |
| 231 | mMetaState = metaState; |
| 232 | mRepeatCount = repeatCount; |
| 233 | mDownTime = downTime; |
| 234 | mEventTime = eventTime; |
| 235 | } |
| 236 | |
| 237 | void KeyEvent::initialize(const KeyEvent& from) { |
| 238 | InputEvent::initialize(from); |
| 239 | mAction = from.mAction; |
| 240 | mFlags = from.mFlags; |
| 241 | mKeyCode = from.mKeyCode; |
| 242 | mScanCode = from.mScanCode; |
| 243 | mMetaState = from.mMetaState; |
| 244 | mRepeatCount = from.mRepeatCount; |
| 245 | mDownTime = from.mDownTime; |
| 246 | mEventTime = from.mEventTime; |
| 247 | } |
| 248 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 249 | const char* KeyEvent::actionToString(int32_t action) { |
| 250 | // Convert KeyEvent action to string |
| 251 | switch (action) { |
| 252 | case AKEY_EVENT_ACTION_DOWN: |
| 253 | return "DOWN"; |
| 254 | case AKEY_EVENT_ACTION_UP: |
| 255 | return "UP"; |
| 256 | case AKEY_EVENT_ACTION_MULTIPLE: |
| 257 | return "MULTIPLE"; |
| 258 | } |
| 259 | return "UNKNOWN"; |
| 260 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 261 | |
| 262 | // --- PointerCoords --- |
| 263 | |
| 264 | float PointerCoords::getAxisValue(int32_t axis) const { |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 265 | if (axis < 0 || axis > 63 || !BitSet64::hasBit(bits, axis)){ |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 266 | return 0; |
| 267 | } |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 268 | return values[BitSet64::getIndexOfBit(bits, axis)]; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | status_t PointerCoords::setAxisValue(int32_t axis, float value) { |
| 272 | if (axis < 0 || axis > 63) { |
| 273 | return NAME_NOT_FOUND; |
| 274 | } |
| 275 | |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 276 | uint32_t index = BitSet64::getIndexOfBit(bits, axis); |
| 277 | if (!BitSet64::hasBit(bits, axis)) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 278 | if (value == 0) { |
| 279 | return OK; // axes with value 0 do not need to be stored |
| 280 | } |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 281 | |
| 282 | uint32_t count = BitSet64::count(bits); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 283 | if (count >= MAX_AXES) { |
| 284 | tooManyAxes(axis); |
| 285 | return NO_MEMORY; |
| 286 | } |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 287 | BitSet64::markBit(bits, axis); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 288 | for (uint32_t i = count; i > index; i--) { |
| 289 | values[i] = values[i - 1]; |
| 290 | } |
| 291 | } |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 292 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 293 | values[index] = value; |
| 294 | return OK; |
| 295 | } |
| 296 | |
| 297 | static inline void scaleAxisValue(PointerCoords& c, int axis, float scaleFactor) { |
| 298 | float value = c.getAxisValue(axis); |
| 299 | if (value != 0) { |
| 300 | c.setAxisValue(axis, value * scaleFactor); |
| 301 | } |
| 302 | } |
| 303 | |
Robert Carr | e07e103 | 2018-11-26 12:55:53 -0800 | [diff] [blame] | 304 | void PointerCoords::scale(float globalScaleFactor, float windowXScale, float windowYScale) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 305 | // No need to scale pressure or size since they are normalized. |
| 306 | // No need to scale orientation since it is meaningless to do so. |
Robert Carr | e07e103 | 2018-11-26 12:55:53 -0800 | [diff] [blame] | 307 | |
| 308 | // If there is a global scale factor, it is included in the windowX/YScale |
| 309 | // so we don't need to apply it twice to the X/Y axes. |
| 310 | // However we don't want to apply any windowXYScale not included in the global scale |
| 311 | // to the TOUCH_MAJOR/MINOR coordinates. |
| 312 | scaleAxisValue(*this, AMOTION_EVENT_AXIS_X, windowXScale); |
| 313 | scaleAxisValue(*this, AMOTION_EVENT_AXIS_Y, windowYScale); |
| 314 | scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MAJOR, globalScaleFactor); |
| 315 | scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOUCH_MINOR, globalScaleFactor); |
| 316 | scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MAJOR, globalScaleFactor); |
| 317 | scaleAxisValue(*this, AMOTION_EVENT_AXIS_TOOL_MINOR, globalScaleFactor); |
| 318 | } |
| 319 | |
| 320 | void PointerCoords::scale(float globalScaleFactor) { |
| 321 | scale(globalScaleFactor, globalScaleFactor, globalScaleFactor); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 324 | void PointerCoords::applyOffset(float xOffset, float yOffset) { |
| 325 | setAxisValue(AMOTION_EVENT_AXIS_X, getX() + xOffset); |
| 326 | setAxisValue(AMOTION_EVENT_AXIS_Y, getY() + yOffset); |
| 327 | } |
| 328 | |
Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 329 | #ifdef __linux__ |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 330 | status_t PointerCoords::readFromParcel(Parcel* parcel) { |
| 331 | bits = parcel->readInt64(); |
| 332 | |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 333 | uint32_t count = BitSet64::count(bits); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 334 | if (count > MAX_AXES) { |
| 335 | return BAD_VALUE; |
| 336 | } |
| 337 | |
| 338 | for (uint32_t i = 0; i < count; i++) { |
| 339 | values[i] = parcel->readFloat(); |
| 340 | } |
| 341 | return OK; |
| 342 | } |
| 343 | |
| 344 | status_t PointerCoords::writeToParcel(Parcel* parcel) const { |
| 345 | parcel->writeInt64(bits); |
| 346 | |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 347 | uint32_t count = BitSet64::count(bits); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 348 | for (uint32_t i = 0; i < count; i++) { |
| 349 | parcel->writeFloat(values[i]); |
| 350 | } |
| 351 | return OK; |
| 352 | } |
Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 353 | #endif |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 354 | |
| 355 | void PointerCoords::tooManyAxes(int axis) { |
| 356 | ALOGW("Could not set value for axis %d because the PointerCoords structure is full and " |
| 357 | "cannot contain more than %d axis values.", axis, int(MAX_AXES)); |
| 358 | } |
| 359 | |
| 360 | bool PointerCoords::operator==(const PointerCoords& other) const { |
| 361 | if (bits != other.bits) { |
| 362 | return false; |
| 363 | } |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 364 | uint32_t count = BitSet64::count(bits); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 365 | for (uint32_t i = 0; i < count; i++) { |
| 366 | if (values[i] != other.values[i]) { |
| 367 | return false; |
| 368 | } |
| 369 | } |
| 370 | return true; |
| 371 | } |
| 372 | |
| 373 | void PointerCoords::copyFrom(const PointerCoords& other) { |
| 374 | bits = other.bits; |
Michael Wright | 38dcdff | 2014-03-19 12:06:10 -0700 | [diff] [blame] | 375 | uint32_t count = BitSet64::count(bits); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 376 | for (uint32_t i = 0; i < count; i++) { |
| 377 | values[i] = other.values[i]; |
| 378 | } |
| 379 | } |
| 380 | |
chaviw | c01e137 | 2020-07-01 12:37:31 -0700 | [diff] [blame] | 381 | void PointerCoords::transform(const ui::Transform& transform) { |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 382 | const vec2 xy = transform.transform(getXYValue()); |
| 383 | setAxisValue(AMOTION_EVENT_AXIS_X, xy.x); |
| 384 | setAxisValue(AMOTION_EVENT_AXIS_Y, xy.y); |
| 385 | |
| 386 | if (BitSet64::hasBit(bits, AMOTION_EVENT_AXIS_ORIENTATION)) { |
| 387 | const float val = getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION); |
| 388 | setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(transform, val)); |
| 389 | } |
chaviw | c01e137 | 2020-07-01 12:37:31 -0700 | [diff] [blame] | 390 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 391 | |
| 392 | // --- PointerProperties --- |
| 393 | |
| 394 | bool PointerProperties::operator==(const PointerProperties& other) const { |
| 395 | return id == other.id |
| 396 | && toolType == other.toolType; |
| 397 | } |
| 398 | |
| 399 | void PointerProperties::copyFrom(const PointerProperties& other) { |
| 400 | id = other.id; |
| 401 | toolType = other.toolType; |
| 402 | } |
| 403 | |
| 404 | |
| 405 | // --- MotionEvent --- |
| 406 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 407 | void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int32_t displayId, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 408 | std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton, |
| 409 | int32_t flags, int32_t edgeFlags, int32_t metaState, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 410 | int32_t buttonState, MotionClassification classification, |
| 411 | const ui::Transform& transform, float xPrecision, float yPrecision, |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 412 | float rawXCursorPosition, float rawYCursorPosition, |
| 413 | int32_t displayWidth, int32_t displayHeight, nsecs_t downTime, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 414 | nsecs_t eventTime, size_t pointerCount, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 415 | const PointerProperties* pointerProperties, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 416 | const PointerCoords* pointerCoords) { |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 417 | InputEvent::initialize(id, deviceId, source, displayId, hmac); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 418 | mAction = action; |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 419 | mActionButton = actionButton; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 420 | mFlags = flags; |
| 421 | mEdgeFlags = edgeFlags; |
| 422 | mMetaState = metaState; |
| 423 | mButtonState = buttonState; |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 424 | mClassification = classification; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 425 | mTransform = transform; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 426 | mXPrecision = xPrecision; |
| 427 | mYPrecision = yPrecision; |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 428 | mRawXCursorPosition = rawXCursorPosition; |
| 429 | mRawYCursorPosition = rawYCursorPosition; |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 430 | mDisplayWidth = displayWidth; |
| 431 | mDisplayHeight = displayHeight; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 432 | mDownTime = downTime; |
| 433 | mPointerProperties.clear(); |
| 434 | mPointerProperties.appendArray(pointerProperties, pointerCount); |
| 435 | mSampleEventTimes.clear(); |
| 436 | mSamplePointerCoords.clear(); |
| 437 | addSample(eventTime, pointerCoords); |
| 438 | } |
| 439 | |
| 440 | void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) { |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 441 | InputEvent::initialize(other->mId, other->mDeviceId, other->mSource, other->mDisplayId, |
| 442 | other->mHmac); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 443 | mAction = other->mAction; |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 444 | mActionButton = other->mActionButton; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 445 | mFlags = other->mFlags; |
| 446 | mEdgeFlags = other->mEdgeFlags; |
| 447 | mMetaState = other->mMetaState; |
| 448 | mButtonState = other->mButtonState; |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 449 | mClassification = other->mClassification; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 450 | mTransform = other->mTransform; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 451 | mXPrecision = other->mXPrecision; |
| 452 | mYPrecision = other->mYPrecision; |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 453 | mRawXCursorPosition = other->mRawXCursorPosition; |
| 454 | mRawYCursorPosition = other->mRawYCursorPosition; |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 455 | mDisplayWidth = other->mDisplayWidth; |
| 456 | mDisplayHeight = other->mDisplayHeight; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 457 | mDownTime = other->mDownTime; |
| 458 | mPointerProperties = other->mPointerProperties; |
| 459 | |
| 460 | if (keepHistory) { |
| 461 | mSampleEventTimes = other->mSampleEventTimes; |
| 462 | mSamplePointerCoords = other->mSamplePointerCoords; |
| 463 | } else { |
| 464 | mSampleEventTimes.clear(); |
Siarhei Vishniakou | 46a2774 | 2020-09-09 13:57:28 -0500 | [diff] [blame] | 465 | mSampleEventTimes.push_back(other->getEventTime()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 466 | mSamplePointerCoords.clear(); |
| 467 | size_t pointerCount = other->getPointerCount(); |
| 468 | size_t historySize = other->getHistorySize(); |
| 469 | mSamplePointerCoords.appendArray(other->mSamplePointerCoords.array() |
| 470 | + (historySize * pointerCount), pointerCount); |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | void MotionEvent::addSample( |
| 475 | int64_t eventTime, |
| 476 | const PointerCoords* pointerCoords) { |
Siarhei Vishniakou | 46a2774 | 2020-09-09 13:57:28 -0500 | [diff] [blame] | 477 | mSampleEventTimes.push_back(eventTime); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 478 | mSamplePointerCoords.appendArray(pointerCoords, getPointerCount()); |
| 479 | } |
| 480 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 481 | float MotionEvent::getXCursorPosition() const { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 482 | vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition()); |
| 483 | return vals.x; |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | float MotionEvent::getYCursorPosition() const { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 487 | vec2 vals = mTransform.transform(getRawXCursorPosition(), getRawYCursorPosition()); |
| 488 | return vals.y; |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 489 | } |
| 490 | |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 491 | void MotionEvent::setCursorPosition(float x, float y) { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 492 | ui::Transform inverse = mTransform.inverse(); |
| 493 | vec2 vals = inverse.transform(x, y); |
| 494 | mRawXCursorPosition = vals.x; |
| 495 | mRawYCursorPosition = vals.y; |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 498 | const PointerCoords* MotionEvent::getRawPointerCoords(size_t pointerIndex) const { |
| 499 | return &mSamplePointerCoords[getHistorySize() * getPointerCount() + pointerIndex]; |
| 500 | } |
| 501 | |
| 502 | float MotionEvent::getRawAxisValue(int32_t axis, size_t pointerIndex) const { |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 503 | return getHistoricalRawAxisValue(axis, pointerIndex, getHistorySize()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | float MotionEvent::getAxisValue(int32_t axis, size_t pointerIndex) const { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 507 | return getHistoricalAxisValue(axis, pointerIndex, getHistorySize()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | const PointerCoords* MotionEvent::getHistoricalRawPointerCoords( |
| 511 | size_t pointerIndex, size_t historicalIndex) const { |
| 512 | return &mSamplePointerCoords[historicalIndex * getPointerCount() + pointerIndex]; |
| 513 | } |
| 514 | |
| 515 | float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex, |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 516 | size_t historicalIndex) const { |
| 517 | const PointerCoords* coords = getHistoricalRawPointerCoords(pointerIndex, historicalIndex); |
| 518 | |
Prabir Pradhan | 7e1443f | 2021-07-23 21:01:55 +0000 | [diff] [blame^] | 519 | if (!isPerWindowInputRotationEnabled()) return coords->getAxisValue(axis); |
| 520 | |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 521 | if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) { |
| 522 | // For compatibility, convert raw coordinates into "oriented screen space". Once app |
| 523 | // developers are educated about getRaw, we can consider removing this. |
| 524 | const vec2 xy = rotatePoint(mTransform, coords->getX(), coords->getY(), mDisplayWidth, |
| 525 | mDisplayHeight); |
| 526 | static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1); |
| 527 | return xy[axis]; |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 528 | } |
| 529 | |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 530 | return coords->getAxisValue(axis); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex, |
| 534 | size_t historicalIndex) const { |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 535 | const PointerCoords* coords = getHistoricalRawPointerCoords(pointerIndex, historicalIndex); |
| 536 | |
| 537 | if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) { |
| 538 | const vec2 xy = mTransform.transform(coords->getXYValue()); |
| 539 | static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1); |
| 540 | return xy[axis]; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 541 | } |
| 542 | |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 543 | return coords->getAxisValue(axis); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const { |
| 547 | size_t pointerCount = mPointerProperties.size(); |
| 548 | for (size_t i = 0; i < pointerCount; i++) { |
| 549 | if (mPointerProperties.itemAt(i).id == pointerId) { |
| 550 | return i; |
| 551 | } |
| 552 | } |
| 553 | return -1; |
| 554 | } |
| 555 | |
| 556 | void MotionEvent::offsetLocation(float xOffset, float yOffset) { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 557 | float currXOffset = mTransform.tx(); |
| 558 | float currYOffset = mTransform.ty(); |
| 559 | mTransform.set(currXOffset + xOffset, currYOffset + yOffset); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 560 | } |
| 561 | |
Robert Carr | e07e103 | 2018-11-26 12:55:53 -0800 | [diff] [blame] | 562 | void MotionEvent::scale(float globalScaleFactor) { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 563 | mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor); |
Robert Carr | e07e103 | 2018-11-26 12:55:53 -0800 | [diff] [blame] | 564 | mXPrecision *= globalScaleFactor; |
| 565 | mYPrecision *= globalScaleFactor; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 566 | |
| 567 | size_t numSamples = mSamplePointerCoords.size(); |
| 568 | for (size_t i = 0; i < numSamples; i++) { |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 569 | mSamplePointerCoords.editItemAt(i).scale(globalScaleFactor, globalScaleFactor, |
| 570 | globalScaleFactor); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 571 | } |
| 572 | } |
| 573 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 574 | void MotionEvent::transform(const std::array<float, 9>& matrix) { |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 575 | // We want to preserve the raw axes values stored in the PointerCoords, so we just update the |
| 576 | // transform using the values passed in. |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 577 | ui::Transform newTransform; |
| 578 | newTransform.set(matrix); |
| 579 | mTransform = newTransform * mTransform; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 580 | |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 581 | // We need to update the AXIS_ORIENTATION value here to maintain the old behavior where the |
| 582 | // orientation angle is not affected by the initial transformation set in the MotionEvent. |
| 583 | std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(), |
| 584 | [&newTransform](PointerCoords& c) { |
| 585 | float orientation = c.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION); |
| 586 | c.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, |
| 587 | transformAngle(newTransform, orientation)); |
| 588 | }); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 589 | } |
| 590 | |
Evan Rosky | d4d4d80 | 2021-05-03 20:12:21 -0700 | [diff] [blame] | 591 | void MotionEvent::applyTransform(const std::array<float, 9>& matrix) { |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 592 | ui::Transform transform; |
| 593 | transform.set(matrix); |
Evan Rosky | d4d4d80 | 2021-05-03 20:12:21 -0700 | [diff] [blame] | 594 | |
| 595 | // Apply the transformation to all samples. |
Prabir Pradhan | 6b38461 | 2021-05-14 16:56:25 -0700 | [diff] [blame] | 596 | std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(), |
| 597 | [&transform](PointerCoords& c) { c.transform(transform); }); |
Evan Rosky | d4d4d80 | 2021-05-03 20:12:21 -0700 | [diff] [blame] | 598 | } |
| 599 | |
Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 600 | #ifdef __linux__ |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 601 | static status_t readFromParcel(ui::Transform& transform, const Parcel& parcel) { |
| 602 | float dsdx, dtdx, tx, dtdy, dsdy, ty; |
| 603 | status_t status = parcel.readFloat(&dsdx); |
| 604 | status |= parcel.readFloat(&dtdx); |
| 605 | status |= parcel.readFloat(&tx); |
| 606 | status |= parcel.readFloat(&dtdy); |
| 607 | status |= parcel.readFloat(&dsdy); |
| 608 | status |= parcel.readFloat(&ty); |
| 609 | |
| 610 | transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1}); |
| 611 | return status; |
| 612 | } |
| 613 | |
| 614 | static status_t writeToParcel(const ui::Transform& transform, Parcel& parcel) { |
| 615 | status_t status = parcel.writeFloat(transform.dsdx()); |
| 616 | status |= parcel.writeFloat(transform.dtdx()); |
| 617 | status |= parcel.writeFloat(transform.tx()); |
| 618 | status |= parcel.writeFloat(transform.dtdy()); |
| 619 | status |= parcel.writeFloat(transform.dsdy()); |
| 620 | status |= parcel.writeFloat(transform.ty()); |
| 621 | return status; |
| 622 | } |
| 623 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 624 | status_t MotionEvent::readFromParcel(Parcel* parcel) { |
| 625 | size_t pointerCount = parcel->readInt32(); |
| 626 | size_t sampleCount = parcel->readInt32(); |
Flanker | 552a8a5 | 2015-09-07 15:28:58 +0800 | [diff] [blame] | 627 | if (pointerCount == 0 || pointerCount > MAX_POINTERS || |
| 628 | sampleCount == 0 || sampleCount > MAX_SAMPLES) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 629 | return BAD_VALUE; |
| 630 | } |
| 631 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 632 | mId = parcel->readInt32(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 633 | mDeviceId = parcel->readInt32(); |
Siarhei Vishniakou | 3826d47 | 2020-01-27 10:44:40 -0600 | [diff] [blame] | 634 | mSource = parcel->readUint32(); |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 635 | mDisplayId = parcel->readInt32(); |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 636 | std::vector<uint8_t> hmac; |
| 637 | status_t result = parcel->readByteVector(&hmac); |
| 638 | if (result != OK || hmac.size() != 32) { |
| 639 | return BAD_VALUE; |
| 640 | } |
| 641 | std::move(hmac.begin(), hmac.begin() + hmac.size(), mHmac.begin()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 642 | mAction = parcel->readInt32(); |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 643 | mActionButton = parcel->readInt32(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 644 | mFlags = parcel->readInt32(); |
| 645 | mEdgeFlags = parcel->readInt32(); |
| 646 | mMetaState = parcel->readInt32(); |
| 647 | mButtonState = parcel->readInt32(); |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 648 | mClassification = static_cast<MotionClassification>(parcel->readByte()); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 649 | |
| 650 | result = android::readFromParcel(mTransform, *parcel); |
| 651 | if (result != OK) { |
| 652 | return result; |
| 653 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 654 | mXPrecision = parcel->readFloat(); |
| 655 | mYPrecision = parcel->readFloat(); |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 656 | mRawXCursorPosition = parcel->readFloat(); |
| 657 | mRawYCursorPosition = parcel->readFloat(); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 658 | mDisplayWidth = parcel->readInt32(); |
| 659 | mDisplayHeight = parcel->readInt32(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 660 | mDownTime = parcel->readInt64(); |
| 661 | |
| 662 | mPointerProperties.clear(); |
| 663 | mPointerProperties.setCapacity(pointerCount); |
| 664 | mSampleEventTimes.clear(); |
Siarhei Vishniakou | 46a2774 | 2020-09-09 13:57:28 -0500 | [diff] [blame] | 665 | mSampleEventTimes.reserve(sampleCount); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 666 | mSamplePointerCoords.clear(); |
| 667 | mSamplePointerCoords.setCapacity(sampleCount * pointerCount); |
| 668 | |
| 669 | for (size_t i = 0; i < pointerCount; i++) { |
| 670 | mPointerProperties.push(); |
| 671 | PointerProperties& properties = mPointerProperties.editTop(); |
| 672 | properties.id = parcel->readInt32(); |
| 673 | properties.toolType = parcel->readInt32(); |
| 674 | } |
| 675 | |
Dan Austin | c94fc45 | 2015-09-22 14:22:41 -0700 | [diff] [blame] | 676 | while (sampleCount > 0) { |
| 677 | sampleCount--; |
Siarhei Vishniakou | 46a2774 | 2020-09-09 13:57:28 -0500 | [diff] [blame] | 678 | mSampleEventTimes.push_back(parcel->readInt64()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 679 | for (size_t i = 0; i < pointerCount; i++) { |
| 680 | mSamplePointerCoords.push(); |
| 681 | status_t status = mSamplePointerCoords.editTop().readFromParcel(parcel); |
| 682 | if (status) { |
| 683 | return status; |
| 684 | } |
| 685 | } |
| 686 | } |
| 687 | return OK; |
| 688 | } |
| 689 | |
| 690 | status_t MotionEvent::writeToParcel(Parcel* parcel) const { |
| 691 | size_t pointerCount = mPointerProperties.size(); |
| 692 | size_t sampleCount = mSampleEventTimes.size(); |
| 693 | |
| 694 | parcel->writeInt32(pointerCount); |
| 695 | parcel->writeInt32(sampleCount); |
| 696 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 697 | parcel->writeInt32(mId); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 698 | parcel->writeInt32(mDeviceId); |
Siarhei Vishniakou | 3826d47 | 2020-01-27 10:44:40 -0600 | [diff] [blame] | 699 | parcel->writeUint32(mSource); |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 700 | parcel->writeInt32(mDisplayId); |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 701 | std::vector<uint8_t> hmac(mHmac.begin(), mHmac.end()); |
| 702 | parcel->writeByteVector(hmac); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 703 | parcel->writeInt32(mAction); |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 704 | parcel->writeInt32(mActionButton); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 705 | parcel->writeInt32(mFlags); |
| 706 | parcel->writeInt32(mEdgeFlags); |
| 707 | parcel->writeInt32(mMetaState); |
| 708 | parcel->writeInt32(mButtonState); |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 709 | parcel->writeByte(static_cast<int8_t>(mClassification)); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 710 | |
| 711 | status_t result = android::writeToParcel(mTransform, *parcel); |
| 712 | if (result != OK) { |
| 713 | return result; |
| 714 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 715 | parcel->writeFloat(mXPrecision); |
| 716 | parcel->writeFloat(mYPrecision); |
Garfield Tan | 937bb83 | 2019-07-25 17:48:31 -0700 | [diff] [blame] | 717 | parcel->writeFloat(mRawXCursorPosition); |
| 718 | parcel->writeFloat(mRawYCursorPosition); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 719 | parcel->writeInt32(mDisplayWidth); |
| 720 | parcel->writeInt32(mDisplayHeight); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 721 | parcel->writeInt64(mDownTime); |
| 722 | |
| 723 | for (size_t i = 0; i < pointerCount; i++) { |
| 724 | const PointerProperties& properties = mPointerProperties.itemAt(i); |
| 725 | parcel->writeInt32(properties.id); |
| 726 | parcel->writeInt32(properties.toolType); |
| 727 | } |
| 728 | |
| 729 | const PointerCoords* pc = mSamplePointerCoords.array(); |
| 730 | for (size_t h = 0; h < sampleCount; h++) { |
Siarhei Vishniakou | 46a2774 | 2020-09-09 13:57:28 -0500 | [diff] [blame] | 731 | parcel->writeInt64(mSampleEventTimes[h]); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 732 | for (size_t i = 0; i < pointerCount; i++) { |
| 733 | status_t status = (pc++)->writeToParcel(parcel); |
| 734 | if (status) { |
| 735 | return status; |
| 736 | } |
| 737 | } |
| 738 | } |
| 739 | return OK; |
| 740 | } |
Brett Chabot | faa986c | 2020-11-04 17:39:36 -0800 | [diff] [blame] | 741 | #endif |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 742 | |
Siarhei Vishniakou | 3826d47 | 2020-01-27 10:44:40 -0600 | [diff] [blame] | 743 | bool MotionEvent::isTouchEvent(uint32_t source, int32_t action) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 744 | if (source & AINPUT_SOURCE_CLASS_POINTER) { |
| 745 | // Specifically excludes HOVER_MOVE and SCROLL. |
| 746 | switch (action & AMOTION_EVENT_ACTION_MASK) { |
| 747 | case AMOTION_EVENT_ACTION_DOWN: |
| 748 | case AMOTION_EVENT_ACTION_MOVE: |
| 749 | case AMOTION_EVENT_ACTION_UP: |
| 750 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 751 | case AMOTION_EVENT_ACTION_POINTER_UP: |
| 752 | case AMOTION_EVENT_ACTION_CANCEL: |
| 753 | case AMOTION_EVENT_ACTION_OUTSIDE: |
| 754 | return true; |
| 755 | } |
| 756 | } |
| 757 | return false; |
| 758 | } |
| 759 | |
Michael Wright | 872db4f | 2014-04-22 15:03:51 -0700 | [diff] [blame] | 760 | const char* MotionEvent::getLabel(int32_t axis) { |
Chris Ye | 4958d06 | 2020-08-20 13:21:10 -0700 | [diff] [blame] | 761 | return InputEventLookup::getAxisLabel(axis); |
Michael Wright | 872db4f | 2014-04-22 15:03:51 -0700 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | int32_t MotionEvent::getAxisFromLabel(const char* label) { |
Chris Ye | 4958d06 | 2020-08-20 13:21:10 -0700 | [diff] [blame] | 765 | return InputEventLookup::getAxisByLabel(label); |
Michael Wright | 872db4f | 2014-04-22 15:03:51 -0700 | [diff] [blame] | 766 | } |
| 767 | |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame] | 768 | std::string MotionEvent::actionToString(int32_t action) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 769 | // Convert MotionEvent action to string |
| 770 | switch (action & AMOTION_EVENT_ACTION_MASK) { |
| 771 | case AMOTION_EVENT_ACTION_DOWN: |
| 772 | return "DOWN"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 773 | case AMOTION_EVENT_ACTION_UP: |
| 774 | return "UP"; |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame] | 775 | case AMOTION_EVENT_ACTION_MOVE: |
| 776 | return "MOVE"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 777 | case AMOTION_EVENT_ACTION_CANCEL: |
| 778 | return "CANCEL"; |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame] | 779 | case AMOTION_EVENT_ACTION_OUTSIDE: |
| 780 | return "OUTSIDE"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 781 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 782 | return "POINTER_DOWN"; |
| 783 | case AMOTION_EVENT_ACTION_POINTER_UP: |
| 784 | return "POINTER_UP"; |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame] | 785 | case AMOTION_EVENT_ACTION_HOVER_MOVE: |
| 786 | return "HOVER_MOVE"; |
| 787 | case AMOTION_EVENT_ACTION_SCROLL: |
| 788 | return "SCROLL"; |
| 789 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 790 | return "HOVER_ENTER"; |
| 791 | case AMOTION_EVENT_ACTION_HOVER_EXIT: |
| 792 | return "HOVER_EXIT"; |
| 793 | case AMOTION_EVENT_ACTION_BUTTON_PRESS: |
| 794 | return "BUTTON_PRESS"; |
| 795 | case AMOTION_EVENT_ACTION_BUTTON_RELEASE: |
| 796 | return "BUTTON_RELEASE"; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 797 | } |
Siarhei Vishniakou | c68fdec | 2020-10-22 14:58:14 -0500 | [diff] [blame] | 798 | return android::base::StringPrintf("%" PRId32, action); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 799 | } |
| 800 | |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 801 | // --- FocusEvent --- |
| 802 | |
Garfield Tan | 4cc839f | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 803 | void FocusEvent::initialize(int32_t id, bool hasFocus, bool inTouchMode) { |
| 804 | InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 805 | ADISPLAY_ID_NONE, INVALID_HMAC); |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 806 | mHasFocus = hasFocus; |
| 807 | mInTouchMode = inTouchMode; |
| 808 | } |
| 809 | |
| 810 | void FocusEvent::initialize(const FocusEvent& from) { |
| 811 | InputEvent::initialize(from); |
| 812 | mHasFocus = from.mHasFocus; |
| 813 | mInTouchMode = from.mInTouchMode; |
| 814 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 815 | |
Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 816 | // --- CaptureEvent --- |
| 817 | |
| 818 | void CaptureEvent::initialize(int32_t id, bool pointerCaptureEnabled) { |
| 819 | InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN, |
| 820 | ADISPLAY_ID_NONE, INVALID_HMAC); |
| 821 | mPointerCaptureEnabled = pointerCaptureEnabled; |
| 822 | } |
| 823 | |
| 824 | void CaptureEvent::initialize(const CaptureEvent& from) { |
| 825 | InputEvent::initialize(from); |
| 826 | mPointerCaptureEnabled = from.mPointerCaptureEnabled; |
| 827 | } |
| 828 | |
arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 829 | // --- DragEvent --- |
| 830 | |
| 831 | void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) { |
| 832 | InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN, |
| 833 | ADISPLAY_ID_NONE, INVALID_HMAC); |
| 834 | mIsExiting = isExiting; |
| 835 | mX = x; |
| 836 | mY = y; |
| 837 | } |
| 838 | |
| 839 | void DragEvent::initialize(const DragEvent& from) { |
| 840 | InputEvent::initialize(from); |
| 841 | mIsExiting = from.mIsExiting; |
| 842 | mX = from.mX; |
| 843 | mY = from.mY; |
| 844 | } |
| 845 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 846 | // --- PooledInputEventFactory --- |
| 847 | |
| 848 | PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) : |
| 849 | mMaxPoolSize(maxPoolSize) { |
| 850 | } |
| 851 | |
| 852 | PooledInputEventFactory::~PooledInputEventFactory() { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | KeyEvent* PooledInputEventFactory::createKeyEvent() { |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 856 | if (mKeyEventPool.empty()) { |
| 857 | return new KeyEvent(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 858 | } |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 859 | KeyEvent* event = mKeyEventPool.front().release(); |
| 860 | mKeyEventPool.pop(); |
| 861 | return event; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | MotionEvent* PooledInputEventFactory::createMotionEvent() { |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 865 | if (mMotionEventPool.empty()) { |
| 866 | return new MotionEvent(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 867 | } |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 868 | MotionEvent* event = mMotionEventPool.front().release(); |
| 869 | mMotionEventPool.pop(); |
| 870 | return event; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 871 | } |
| 872 | |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 873 | FocusEvent* PooledInputEventFactory::createFocusEvent() { |
| 874 | if (mFocusEventPool.empty()) { |
| 875 | return new FocusEvent(); |
| 876 | } |
| 877 | FocusEvent* event = mFocusEventPool.front().release(); |
| 878 | mFocusEventPool.pop(); |
| 879 | return event; |
| 880 | } |
| 881 | |
Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 882 | CaptureEvent* PooledInputEventFactory::createCaptureEvent() { |
| 883 | if (mCaptureEventPool.empty()) { |
| 884 | return new CaptureEvent(); |
| 885 | } |
| 886 | CaptureEvent* event = mCaptureEventPool.front().release(); |
| 887 | mCaptureEventPool.pop(); |
| 888 | return event; |
| 889 | } |
| 890 | |
arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 891 | DragEvent* PooledInputEventFactory::createDragEvent() { |
| 892 | if (mDragEventPool.empty()) { |
| 893 | return new DragEvent(); |
| 894 | } |
| 895 | DragEvent* event = mDragEventPool.front().release(); |
| 896 | mDragEventPool.pop(); |
| 897 | return event; |
| 898 | } |
| 899 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 900 | void PooledInputEventFactory::recycle(InputEvent* event) { |
| 901 | switch (event->getType()) { |
| 902 | case AINPUT_EVENT_TYPE_KEY: |
| 903 | if (mKeyEventPool.size() < mMaxPoolSize) { |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 904 | mKeyEventPool.push(std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event))); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 905 | return; |
| 906 | } |
| 907 | break; |
| 908 | case AINPUT_EVENT_TYPE_MOTION: |
| 909 | if (mMotionEventPool.size() < mMaxPoolSize) { |
Siarhei Vishniakou | 727a44e | 2019-11-23 12:59:16 -0800 | [diff] [blame] | 910 | mMotionEventPool.push(std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event))); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 911 | return; |
| 912 | } |
| 913 | break; |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 914 | case AINPUT_EVENT_TYPE_FOCUS: |
| 915 | if (mFocusEventPool.size() < mMaxPoolSize) { |
| 916 | mFocusEventPool.push(std::unique_ptr<FocusEvent>(static_cast<FocusEvent*>(event))); |
| 917 | return; |
| 918 | } |
| 919 | break; |
Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 920 | case AINPUT_EVENT_TYPE_CAPTURE: |
| 921 | if (mCaptureEventPool.size() < mMaxPoolSize) { |
| 922 | mCaptureEventPool.push( |
| 923 | std::unique_ptr<CaptureEvent>(static_cast<CaptureEvent*>(event))); |
| 924 | return; |
| 925 | } |
| 926 | break; |
arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 927 | case AINPUT_EVENT_TYPE_DRAG: |
| 928 | if (mDragEventPool.size() < mMaxPoolSize) { |
| 929 | mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event))); |
| 930 | return; |
| 931 | } |
| 932 | break; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 933 | } |
| 934 | delete event; |
| 935 | } |
| 936 | |
| 937 | } // namespace android |