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 | |
Siarhei Vishniakou | 6573583 | 2022-08-09 19:18:37 +0000 | [diff] [blame] | 46 | /** |
| 47 | * Remove the data for the provided pointers from the args. The pointers are identified by their |
| 48 | * pointerId, not by the index inside the array. |
| 49 | * Return the new NotifyMotionArgs struct that has the remaining pointers. |
| 50 | * The only fields that may be different in the returned args from the provided args are: |
| 51 | * - action |
| 52 | * - pointerCount |
| 53 | * - pointerProperties |
| 54 | * - pointerCoords |
| 55 | * Action might change because it contains a pointer index. If another pointer is removed, the |
| 56 | * active pointer index would be shifted. |
| 57 | * |
| 58 | * If the active pointer id is removed (for example, for events like |
| 59 | * POINTER_UP or POINTER_DOWN), then the action is set to ACTION_UNKNOWN. It is up to the caller |
| 60 | * to set the action appropriately after the call. |
| 61 | * |
| 62 | * @param args the args from which the pointers should be removed |
| 63 | * @param pointerIds the pointer ids of the pointers that should be removed |
| 64 | */ |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 65 | NotifyMotionArgs removePointerIds(const NotifyMotionArgs& args, |
| 66 | const std::set<int32_t>& pointerIds); |
| 67 | |
| 68 | std::vector<NotifyMotionArgs> cancelSuppressedPointers( |
| 69 | const NotifyMotionArgs& args, const std::set<int32_t>& oldSuppressedPointerIds, |
| 70 | const std::set<int32_t>& newSuppressedPointerIds); |
| 71 | |
| 72 | std::string toString(const ::ui::InProgressTouchEvdev& touch); |
| 73 | |
| 74 | // --- Main classes and interfaces --- |
| 75 | |
| 76 | class PalmRejector; |
| 77 | |
| 78 | // --- Implementations --- |
| 79 | |
| 80 | /** |
| 81 | * Implementation of the UnwantedInteractionBlockerInterface. |
| 82 | * Represents a separate stage of input processing. All of the input events go through this stage. |
| 83 | * Acts as a passthrough for all input events except for motion events. |
| 84 | * |
| 85 | * The events of motion type are sent to PalmRejectors. PalmRejectors detect unwanted touches, |
| 86 | * and emit input streams with the bad pointers removed. |
| 87 | */ |
| 88 | class UnwantedInteractionBlocker : public UnwantedInteractionBlockerInterface { |
| 89 | public: |
| 90 | explicit UnwantedInteractionBlocker(InputListenerInterface& listener); |
| 91 | explicit UnwantedInteractionBlocker(InputListenerInterface& listener, bool enablePalmRejection); |
| 92 | |
Prabir Pradhan | e3da4bb | 2023-04-05 23:51:23 +0000 | [diff] [blame] | 93 | void notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) override; |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 94 | void notifyKey(const NotifyKeyArgs& args) override; |
| 95 | void notifyMotion(const NotifyMotionArgs& args) override; |
| 96 | void notifySwitch(const NotifySwitchArgs& args) override; |
| 97 | void notifySensor(const NotifySensorArgs& args) override; |
| 98 | void notifyVibratorState(const NotifyVibratorStateArgs& args) override; |
| 99 | void notifyDeviceReset(const NotifyDeviceResetArgs& args) override; |
| 100 | void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) override; |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 101 | |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 102 | void dump(std::string& dump) override; |
| 103 | void monitor() override; |
| 104 | |
| 105 | ~UnwantedInteractionBlocker(); |
| 106 | |
| 107 | private: |
Siarhei Vishniakou | a91d857 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 108 | std::mutex mLock; |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 109 | // The next stage to pass input events to |
Siarhei Vishniakou | a91d857 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 110 | |
| 111 | QueuedInputListener mQueuedListener; |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 112 | const bool mEnablePalmRejection; |
| 113 | |
Siarhei Vishniakou | 814ace3 | 2022-03-04 15:12:16 -0800 | [diff] [blame] | 114 | // When stylus is down, ignore touch |
Siarhei Vishniakou | a91d857 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 115 | PreferStylusOverTouchBlocker mPreferStylusOverTouchBlocker GUARDED_BY(mLock); |
Siarhei Vishniakou | 814ace3 | 2022-03-04 15:12:16 -0800 | [diff] [blame] | 116 | |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 117 | // Detect and reject unwanted palms on screen |
| 118 | // Use a separate palm rejector for every touch device. |
Siarhei Vishniakou | a91d857 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 119 | std::map<int32_t /*deviceId*/, PalmRejector> mPalmRejectors GUARDED_BY(mLock); |
Siarhei Vishniakou | 814ace3 | 2022-03-04 15:12:16 -0800 | [diff] [blame] | 120 | // TODO(b/210159205): delete this when simultaneous stylus and touch is supported |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 121 | void notifyMotionLocked(const NotifyMotionArgs& args) REQUIRES(mLock); |
Siarhei Vishniakou | d5fe518 | 2022-07-20 23:28:40 +0000 | [diff] [blame] | 122 | |
| 123 | // Call this function for outbound events so that they can be logged when logging is enabled. |
| 124 | void enqueueOutboundMotionLocked(const NotifyMotionArgs& args) REQUIRES(mLock); |
Prabir Pradhan | e3da4bb | 2023-04-05 23:51:23 +0000 | [diff] [blame] | 125 | |
| 126 | void onInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices); |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | class SlotState { |
| 130 | public: |
| 131 | /** |
| 132 | * Update the state using the new information provided in the NotifyMotionArgs |
| 133 | */ |
| 134 | void update(const NotifyMotionArgs& args); |
| 135 | std::optional<size_t> getSlotForPointerId(int32_t pointerId) const; |
| 136 | std::string dump() const; |
| 137 | |
| 138 | private: |
| 139 | // Process a pointer with the provided action, and return the slot associated with it |
| 140 | void processPointerId(int32_t pointerId, int32_t action); |
| 141 | // The map from tracking id to slot state. Since the PalmRejectionFilter works close to the |
| 142 | // evdev level, the only way to tell it about UP or CANCEL events is by sending tracking id = -1 |
| 143 | // to the appropriate touch slot. So we need to reconstruct the original slot. |
| 144 | // The two collections below must always be in-sync. |
| 145 | // Use std::map instead of std::unordered_map because we rely on these collections being |
| 146 | // ordered. It also has better space efficiency than unordered_map because we only have a few |
| 147 | // pointers most of the time. |
| 148 | std::map<int32_t /*pointerId*/, size_t /*slot*/> mSlotsByPointerId; |
| 149 | std::map<size_t /*slot*/, int32_t /*pointerId */> mPointerIdsBySlot; |
| 150 | |
| 151 | size_t findUnusedSlot() const; |
| 152 | }; |
| 153 | |
| 154 | /** |
| 155 | * Convert an Android event to a linux-like 'InProgressTouchEvdev'. The provided SlotState's |
| 156 | * are used to figure out which slot does each pointer belong to. |
| 157 | */ |
| 158 | std::vector<::ui::InProgressTouchEvdev> getTouches(const NotifyMotionArgs& args, |
| 159 | const AndroidPalmFilterDeviceInfo& deviceInfo, |
| 160 | const SlotState& oldSlotState, |
| 161 | const SlotState& newSlotState); |
| 162 | |
| 163 | class PalmRejector { |
| 164 | public: |
| 165 | explicit PalmRejector(const AndroidPalmFilterDeviceInfo& info, |
| 166 | std::unique_ptr<::ui::PalmDetectionFilter> filter = nullptr); |
| 167 | std::vector<NotifyMotionArgs> processMotion(const NotifyMotionArgs& args); |
| 168 | |
| 169 | // Get the device info of this device, for comparison purposes |
Siarhei Vishniakou | 88151b8 | 2022-08-11 00:53:38 +0000 | [diff] [blame] | 170 | const AndroidPalmFilterDeviceInfo& getPalmFilterDeviceInfo() const; |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 171 | std::string dump() const; |
| 172 | |
| 173 | private: |
| 174 | PalmRejector(const PalmRejector&) = delete; |
| 175 | PalmRejector& operator=(const PalmRejector&) = delete; |
| 176 | |
Siarhei Vishniakou | e491fb5 | 2022-08-11 01:51:24 +0000 | [diff] [blame] | 177 | /** |
| 178 | * Update the slot state and send this event to the palm rejection model for palm detection. |
| 179 | * Return the pointer ids that should be suppressed. |
| 180 | * |
| 181 | * This function is not const because it has side-effects. It will update the slot state using |
| 182 | * the incoming args! Also, it will call Filter(..), which has side-effects. |
| 183 | */ |
| 184 | std::set<int32_t> detectPalmPointers(const NotifyMotionArgs& args); |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 185 | std::unique_ptr<::ui::SharedPalmDetectionFilterState> mSharedPalmState; |
| 186 | AndroidPalmFilterDeviceInfo mDeviceInfo; |
| 187 | std::unique_ptr<::ui::PalmDetectionFilter> mPalmDetectionFilter; |
| 188 | std::set<int32_t> mSuppressedPointerIds; |
| 189 | |
| 190 | // Used to help convert an Android touch stream to Linux input stream. |
| 191 | SlotState mSlotState; |
| 192 | }; |
| 193 | |
| 194 | } // namespace android |