Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 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 | #pragma once |
| 18 | |
| 19 | #include <map> |
| 20 | #include <set> |
| 21 | |
Siarhei Vishniakou | a91d857 | 2022-05-17 05:03:42 -0700 | [diff] [blame^] | 22 | #include <android-base/thread_annotations.h> |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 23 | #include "include/UnwantedInteractionBlockerInterface.h" |
| 24 | #include "ui/events/ozone/evdev/touch_filter/neural_stylus_palm_detection_filter_util.h" |
| 25 | #include "ui/events/ozone/evdev/touch_filter/palm_detection_filter.h" |
| 26 | |
Siarhei Vishniakou | 814ace3 | 2022-03-04 15:12:16 -0800 | [diff] [blame] | 27 | #include "PreferStylusOverTouchBlocker.h" |
| 28 | |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 29 | namespace android { |
| 30 | |
| 31 | // --- Functions for manipulation of event streams |
| 32 | |
| 33 | struct AndroidPalmFilterDeviceInfo : ::ui::PalmFilterDeviceInfo { |
| 34 | // Additional fields from 'TouchEventConverterEvdev', added here for convenience |
| 35 | int32_t touch_major_res = 1; // info.GetAbsInfoByCode(ABS_MT_TOUCH_MAJOR).resolution; |
| 36 | int32_t touch_minor_res = 1; // info.GetAbsInfoByCode(ABS_MT_TOUCH_MINOR).resolution; |
| 37 | |
| 38 | auto operator<=>(const AndroidPalmFilterDeviceInfo&) const = default; |
| 39 | }; |
| 40 | |
| 41 | std::optional<AndroidPalmFilterDeviceInfo> createPalmFilterDeviceInfo( |
| 42 | const InputDeviceInfo& deviceInfo); |
| 43 | |
| 44 | static constexpr int32_t ACTION_UNKNOWN = -1; |
| 45 | |
| 46 | NotifyMotionArgs removePointerIds(const NotifyMotionArgs& args, |
| 47 | const std::set<int32_t>& pointerIds); |
| 48 | |
| 49 | std::vector<NotifyMotionArgs> cancelSuppressedPointers( |
| 50 | const NotifyMotionArgs& args, const std::set<int32_t>& oldSuppressedPointerIds, |
| 51 | const std::set<int32_t>& newSuppressedPointerIds); |
| 52 | |
| 53 | std::string toString(const ::ui::InProgressTouchEvdev& touch); |
| 54 | |
| 55 | // --- Main classes and interfaces --- |
| 56 | |
| 57 | class PalmRejector; |
| 58 | |
| 59 | // --- Implementations --- |
| 60 | |
| 61 | /** |
| 62 | * Implementation of the UnwantedInteractionBlockerInterface. |
| 63 | * Represents a separate stage of input processing. All of the input events go through this stage. |
| 64 | * Acts as a passthrough for all input events except for motion events. |
| 65 | * |
| 66 | * The events of motion type are sent to PalmRejectors. PalmRejectors detect unwanted touches, |
| 67 | * and emit input streams with the bad pointers removed. |
| 68 | */ |
| 69 | class UnwantedInteractionBlocker : public UnwantedInteractionBlockerInterface { |
| 70 | public: |
| 71 | explicit UnwantedInteractionBlocker(InputListenerInterface& listener); |
| 72 | explicit UnwantedInteractionBlocker(InputListenerInterface& listener, bool enablePalmRejection); |
| 73 | |
| 74 | void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) override; |
| 75 | void notifyKey(const NotifyKeyArgs* args) override; |
| 76 | void notifyMotion(const NotifyMotionArgs* args) override; |
| 77 | void notifySwitch(const NotifySwitchArgs* args) override; |
| 78 | void notifySensor(const NotifySensorArgs* args) override; |
| 79 | void notifyVibratorState(const NotifyVibratorStateArgs* args) override; |
| 80 | void notifyDeviceReset(const NotifyDeviceResetArgs* args) override; |
| 81 | void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) override; |
| 82 | |
| 83 | void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override; |
| 84 | void dump(std::string& dump) override; |
| 85 | void monitor() override; |
| 86 | |
| 87 | ~UnwantedInteractionBlocker(); |
| 88 | |
| 89 | private: |
Siarhei Vishniakou | a91d857 | 2022-05-17 05:03:42 -0700 | [diff] [blame^] | 90 | std::mutex mLock; |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 91 | // The next stage to pass input events to |
Siarhei Vishniakou | a91d857 | 2022-05-17 05:03:42 -0700 | [diff] [blame^] | 92 | |
| 93 | QueuedInputListener mQueuedListener; |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 94 | const bool mEnablePalmRejection; |
| 95 | |
Siarhei Vishniakou | 814ace3 | 2022-03-04 15:12:16 -0800 | [diff] [blame] | 96 | // When stylus is down, ignore touch |
Siarhei Vishniakou | a91d857 | 2022-05-17 05:03:42 -0700 | [diff] [blame^] | 97 | PreferStylusOverTouchBlocker mPreferStylusOverTouchBlocker GUARDED_BY(mLock); |
Siarhei Vishniakou | 814ace3 | 2022-03-04 15:12:16 -0800 | [diff] [blame] | 98 | |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 99 | // Detect and reject unwanted palms on screen |
| 100 | // Use a separate palm rejector for every touch device. |
Siarhei Vishniakou | a91d857 | 2022-05-17 05:03:42 -0700 | [diff] [blame^] | 101 | std::map<int32_t /*deviceId*/, PalmRejector> mPalmRejectors GUARDED_BY(mLock); |
Siarhei Vishniakou | 814ace3 | 2022-03-04 15:12:16 -0800 | [diff] [blame] | 102 | // TODO(b/210159205): delete this when simultaneous stylus and touch is supported |
Siarhei Vishniakou | a91d857 | 2022-05-17 05:03:42 -0700 | [diff] [blame^] | 103 | void notifyMotionLocked(const NotifyMotionArgs* args) REQUIRES(mLock); |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | class SlotState { |
| 107 | public: |
| 108 | /** |
| 109 | * Update the state using the new information provided in the NotifyMotionArgs |
| 110 | */ |
| 111 | void update(const NotifyMotionArgs& args); |
| 112 | std::optional<size_t> getSlotForPointerId(int32_t pointerId) const; |
| 113 | std::string dump() const; |
| 114 | |
| 115 | private: |
| 116 | // Process a pointer with the provided action, and return the slot associated with it |
| 117 | void processPointerId(int32_t pointerId, int32_t action); |
| 118 | // The map from tracking id to slot state. Since the PalmRejectionFilter works close to the |
| 119 | // evdev level, the only way to tell it about UP or CANCEL events is by sending tracking id = -1 |
| 120 | // to the appropriate touch slot. So we need to reconstruct the original slot. |
| 121 | // The two collections below must always be in-sync. |
| 122 | // Use std::map instead of std::unordered_map because we rely on these collections being |
| 123 | // ordered. It also has better space efficiency than unordered_map because we only have a few |
| 124 | // pointers most of the time. |
| 125 | std::map<int32_t /*pointerId*/, size_t /*slot*/> mSlotsByPointerId; |
| 126 | std::map<size_t /*slot*/, int32_t /*pointerId */> mPointerIdsBySlot; |
| 127 | |
| 128 | size_t findUnusedSlot() const; |
| 129 | }; |
| 130 | |
| 131 | /** |
| 132 | * Convert an Android event to a linux-like 'InProgressTouchEvdev'. The provided SlotState's |
| 133 | * are used to figure out which slot does each pointer belong to. |
| 134 | */ |
| 135 | std::vector<::ui::InProgressTouchEvdev> getTouches(const NotifyMotionArgs& args, |
| 136 | const AndroidPalmFilterDeviceInfo& deviceInfo, |
| 137 | const SlotState& oldSlotState, |
| 138 | const SlotState& newSlotState); |
| 139 | |
| 140 | class PalmRejector { |
| 141 | public: |
| 142 | explicit PalmRejector(const AndroidPalmFilterDeviceInfo& info, |
| 143 | std::unique_ptr<::ui::PalmDetectionFilter> filter = nullptr); |
| 144 | std::vector<NotifyMotionArgs> processMotion(const NotifyMotionArgs& args); |
| 145 | |
| 146 | // Get the device info of this device, for comparison purposes |
| 147 | const AndroidPalmFilterDeviceInfo& getPalmFilterDeviceInfo(); |
| 148 | std::string dump() const; |
| 149 | |
| 150 | private: |
| 151 | PalmRejector(const PalmRejector&) = delete; |
| 152 | PalmRejector& operator=(const PalmRejector&) = delete; |
| 153 | |
| 154 | std::unique_ptr<::ui::SharedPalmDetectionFilterState> mSharedPalmState; |
| 155 | AndroidPalmFilterDeviceInfo mDeviceInfo; |
| 156 | std::unique_ptr<::ui::PalmDetectionFilter> mPalmDetectionFilter; |
| 157 | std::set<int32_t> mSuppressedPointerIds; |
| 158 | |
| 159 | // Used to help convert an Android touch stream to Linux input stream. |
| 160 | SlotState mSlotState; |
| 161 | }; |
| 162 | |
| 163 | } // namespace android |