Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2023 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 "InputVerifier" |
| 18 | |
| 19 | #include <android-base/logging.h> |
| 20 | #include <input/InputVerifier.h> |
| 21 | |
| 22 | namespace android { |
| 23 | |
| 24 | /** |
| 25 | * Log all of the movements that are sent to this verifier. Helps to identify the streams that lead |
| 26 | * to inconsistent events. |
| 27 | * Enable this via "adb shell setprop log.tag.InputVerifierLogEvents DEBUG" |
| 28 | */ |
| 29 | static bool logEvents() { |
| 30 | return __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "LogEvents", ANDROID_LOG_INFO); |
| 31 | } |
| 32 | |
| 33 | // --- InputVerifier --- |
| 34 | |
| 35 | InputVerifier::InputVerifier(const std::string& name) : mName(name){}; |
| 36 | |
| 37 | void InputVerifier::processMovement(int32_t deviceId, int32_t action, uint32_t pointerCount, |
| 38 | const PointerProperties* pointerProperties, |
| 39 | const PointerCoords* pointerCoords, int32_t flags) { |
| 40 | if (logEvents()) { |
| 41 | LOG(ERROR) << "Processing " << MotionEvent::actionToString(action) << " for device " |
| 42 | << deviceId << " (" << pointerCount << " pointer" |
| 43 | << (pointerCount == 1 ? "" : "s") << ") on " << mName; |
| 44 | } |
| 45 | |
| 46 | switch (MotionEvent::getActionMasked(action)) { |
| 47 | case AMOTION_EVENT_ACTION_DOWN: { |
| 48 | auto [it, inserted] = mTouchingPointerIdsByDevice.insert({deviceId, {}}); |
| 49 | if (!inserted) { |
| 50 | LOG(FATAL) << "Got ACTION_DOWN, but already have touching pointers " << it->second |
| 51 | << " for device " << deviceId << " on " << mName; |
| 52 | } |
| 53 | it->second.set(pointerProperties[0].id); |
| 54 | break; |
| 55 | } |
| 56 | case AMOTION_EVENT_ACTION_POINTER_DOWN: { |
| 57 | auto it = mTouchingPointerIdsByDevice.find(deviceId); |
| 58 | if (it == mTouchingPointerIdsByDevice.end()) { |
| 59 | LOG(FATAL) << "Got POINTER_DOWN, but no touching pointers for device " << deviceId |
| 60 | << " on " << mName; |
| 61 | } |
| 62 | it->second.set(pointerProperties[MotionEvent::getActionIndex(action)].id); |
| 63 | break; |
| 64 | } |
| 65 | case AMOTION_EVENT_ACTION_MOVE: { |
| 66 | ensureTouchingPointersMatch(deviceId, pointerCount, pointerProperties, "MOVE"); |
| 67 | break; |
| 68 | } |
| 69 | case AMOTION_EVENT_ACTION_POINTER_UP: { |
| 70 | auto it = mTouchingPointerIdsByDevice.find(deviceId); |
| 71 | if (it == mTouchingPointerIdsByDevice.end()) { |
| 72 | LOG(FATAL) << "Got POINTER_UP, but no touching pointers for device " << deviceId |
| 73 | << " on " << mName; |
| 74 | } |
| 75 | it->second.reset(pointerProperties[MotionEvent::getActionIndex(action)].id); |
| 76 | break; |
| 77 | } |
| 78 | case AMOTION_EVENT_ACTION_UP: { |
| 79 | auto it = mTouchingPointerIdsByDevice.find(deviceId); |
| 80 | if (it == mTouchingPointerIdsByDevice.end()) { |
| 81 | LOG(FATAL) << "Got ACTION_UP, but no record for deviceId " << deviceId << " on " |
| 82 | << mName; |
| 83 | } |
| 84 | const auto& [_, touchingPointerIds] = *it; |
| 85 | if (touchingPointerIds.count() != 1) { |
| 86 | LOG(FATAL) << "Got ACTION_UP, but we have pointers: " << touchingPointerIds |
| 87 | << " for deviceId " << deviceId << " on " << mName; |
| 88 | } |
| 89 | const int32_t pointerId = pointerProperties[0].id; |
| 90 | if (!touchingPointerIds.test(pointerId)) { |
| 91 | LOG(FATAL) << "Got ACTION_UP, but pointerId " << pointerId |
| 92 | << " is not touching. Touching pointers: " << touchingPointerIds |
| 93 | << " for deviceId " << deviceId << " on " << mName; |
| 94 | } |
| 95 | mTouchingPointerIdsByDevice.erase(it); |
| 96 | break; |
| 97 | } |
| 98 | case AMOTION_EVENT_ACTION_CANCEL: { |
| 99 | if ((flags & AMOTION_EVENT_FLAG_CANCELED) != AMOTION_EVENT_FLAG_CANCELED) { |
| 100 | LOG(FATAL) << "For ACTION_CANCEL, must set FLAG_CANCELED"; |
| 101 | } |
| 102 | ensureTouchingPointersMatch(deviceId, pointerCount, pointerProperties, "CANCEL"); |
| 103 | mTouchingPointerIdsByDevice.erase(deviceId); |
| 104 | break; |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void InputVerifier::ensureTouchingPointersMatch(int32_t deviceId, uint32_t pointerCount, |
| 110 | const PointerProperties* pointerProperties, |
| 111 | const char* action) const { |
| 112 | auto it = mTouchingPointerIdsByDevice.find(deviceId); |
| 113 | if (it == mTouchingPointerIdsByDevice.end()) { |
| 114 | LOG(FATAL) << "Got " << action << ", but no touching pointers for device " << deviceId |
| 115 | << " on " << mName; |
| 116 | } |
| 117 | const auto& [_, touchingPointerIds] = *it; |
| 118 | for (size_t i = 0; i < pointerCount; i++) { |
| 119 | const int32_t pointerId = pointerProperties[i].id; |
| 120 | if (!touchingPointerIds.test(pointerId)) { |
| 121 | LOG(FATAL) << "Got " << action << " for pointerId " << pointerId |
| 122 | << " but the touching pointers are " << touchingPointerIds << " on " |
| 123 | << mName; |
| 124 | } |
| 125 | } |
| 126 | }; |
| 127 | |
| 128 | } // namespace android |