1*38e8c45fSAndroid Build Coastguard Worker /* 2*38e8c45fSAndroid Build Coastguard Worker * Copyright (C) 2022 The Android Open Source Project 3*38e8c45fSAndroid Build Coastguard Worker * 4*38e8c45fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*38e8c45fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*38e8c45fSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*38e8c45fSAndroid Build Coastguard Worker * 8*38e8c45fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*38e8c45fSAndroid Build Coastguard Worker * 10*38e8c45fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*38e8c45fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*38e8c45fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*38e8c45fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*38e8c45fSAndroid Build Coastguard Worker * limitations under the License. 15*38e8c45fSAndroid Build Coastguard Worker */ 16*38e8c45fSAndroid Build Coastguard Worker 17*38e8c45fSAndroid Build Coastguard Worker #pragma once 18*38e8c45fSAndroid Build Coastguard Worker 19*38e8c45fSAndroid Build Coastguard Worker #include <map> 20*38e8c45fSAndroid Build Coastguard Worker #include <set> 21*38e8c45fSAndroid Build Coastguard Worker 22*38e8c45fSAndroid Build Coastguard Worker #include <android-base/thread_annotations.h> 23*38e8c45fSAndroid Build Coastguard Worker #include "include/UnwantedInteractionBlockerInterface.h" 24*38e8c45fSAndroid Build Coastguard Worker #include "ui/events/ozone/evdev/touch_filter/neural_stylus_palm_detection_filter_util.h" 25*38e8c45fSAndroid Build Coastguard Worker #include "ui/events/ozone/evdev/touch_filter/palm_detection_filter.h" 26*38e8c45fSAndroid Build Coastguard Worker 27*38e8c45fSAndroid Build Coastguard Worker #include "PreferStylusOverTouchBlocker.h" 28*38e8c45fSAndroid Build Coastguard Worker 29*38e8c45fSAndroid Build Coastguard Worker namespace android { 30*38e8c45fSAndroid Build Coastguard Worker 31*38e8c45fSAndroid Build Coastguard Worker // --- Functions for manipulation of event streams 32*38e8c45fSAndroid Build Coastguard Worker 33*38e8c45fSAndroid Build Coastguard Worker struct AndroidPalmFilterDeviceInfo : ::ui::PalmFilterDeviceInfo { 34*38e8c45fSAndroid Build Coastguard Worker // Additional fields from 'TouchEventConverterEvdev', added here for convenience 35*38e8c45fSAndroid Build Coastguard Worker int32_t touch_major_res = 1; // info.GetAbsInfoByCode(ABS_MT_TOUCH_MAJOR).resolution; 36*38e8c45fSAndroid Build Coastguard Worker int32_t touch_minor_res = 1; // info.GetAbsInfoByCode(ABS_MT_TOUCH_MINOR).resolution; 37*38e8c45fSAndroid Build Coastguard Worker 38*38e8c45fSAndroid Build Coastguard Worker auto operator<=>(const AndroidPalmFilterDeviceInfo&) const = default; 39*38e8c45fSAndroid Build Coastguard Worker }; 40*38e8c45fSAndroid Build Coastguard Worker 41*38e8c45fSAndroid Build Coastguard Worker std::optional<AndroidPalmFilterDeviceInfo> createPalmFilterDeviceInfo( 42*38e8c45fSAndroid Build Coastguard Worker const InputDeviceInfo& deviceInfo); 43*38e8c45fSAndroid Build Coastguard Worker 44*38e8c45fSAndroid Build Coastguard Worker static constexpr int32_t ACTION_UNKNOWN = -1; 45*38e8c45fSAndroid Build Coastguard Worker 46*38e8c45fSAndroid Build Coastguard Worker /** 47*38e8c45fSAndroid Build Coastguard Worker * Remove the data for the provided pointers from the args. The pointers are identified by their 48*38e8c45fSAndroid Build Coastguard Worker * pointerId, not by the index inside the array. 49*38e8c45fSAndroid Build Coastguard Worker * Return the new NotifyMotionArgs struct that has the remaining pointers. 50*38e8c45fSAndroid Build Coastguard Worker * The only fields that may be different in the returned args from the provided args are: 51*38e8c45fSAndroid Build Coastguard Worker * - action 52*38e8c45fSAndroid Build Coastguard Worker * - pointerCount 53*38e8c45fSAndroid Build Coastguard Worker * - pointerProperties 54*38e8c45fSAndroid Build Coastguard Worker * - pointerCoords 55*38e8c45fSAndroid Build Coastguard Worker * Action might change because it contains a pointer index. If another pointer is removed, the 56*38e8c45fSAndroid Build Coastguard Worker * active pointer index would be shifted. 57*38e8c45fSAndroid Build Coastguard Worker * 58*38e8c45fSAndroid Build Coastguard Worker * If the active pointer id is removed (for example, for events like 59*38e8c45fSAndroid Build Coastguard Worker * POINTER_UP or POINTER_DOWN), then the action is set to ACTION_UNKNOWN. It is up to the caller 60*38e8c45fSAndroid Build Coastguard Worker * to set the action appropriately after the call. 61*38e8c45fSAndroid Build Coastguard Worker * 62*38e8c45fSAndroid Build Coastguard Worker * @param args the args from which the pointers should be removed 63*38e8c45fSAndroid Build Coastguard Worker * @param pointerIds the pointer ids of the pointers that should be removed 64*38e8c45fSAndroid Build Coastguard Worker */ 65*38e8c45fSAndroid Build Coastguard Worker NotifyMotionArgs removePointerIds(const NotifyMotionArgs& args, 66*38e8c45fSAndroid Build Coastguard Worker const std::set<int32_t>& pointerIds); 67*38e8c45fSAndroid Build Coastguard Worker 68*38e8c45fSAndroid Build Coastguard Worker std::vector<NotifyMotionArgs> cancelSuppressedPointers( 69*38e8c45fSAndroid Build Coastguard Worker const NotifyMotionArgs& args, const std::set<int32_t>& oldSuppressedPointerIds, 70*38e8c45fSAndroid Build Coastguard Worker const std::set<int32_t>& newSuppressedPointerIds); 71*38e8c45fSAndroid Build Coastguard Worker 72*38e8c45fSAndroid Build Coastguard Worker std::string toString(const ::ui::InProgressTouchEvdev& touch); 73*38e8c45fSAndroid Build Coastguard Worker 74*38e8c45fSAndroid Build Coastguard Worker // --- Main classes and interfaces --- 75*38e8c45fSAndroid Build Coastguard Worker 76*38e8c45fSAndroid Build Coastguard Worker class PalmRejector; 77*38e8c45fSAndroid Build Coastguard Worker 78*38e8c45fSAndroid Build Coastguard Worker // --- Implementations --- 79*38e8c45fSAndroid Build Coastguard Worker 80*38e8c45fSAndroid Build Coastguard Worker /** 81*38e8c45fSAndroid Build Coastguard Worker * Implementation of the UnwantedInteractionBlockerInterface. 82*38e8c45fSAndroid Build Coastguard Worker * Represents a separate stage of input processing. All of the input events go through this stage. 83*38e8c45fSAndroid Build Coastguard Worker * Acts as a passthrough for all input events except for motion events. 84*38e8c45fSAndroid Build Coastguard Worker * 85*38e8c45fSAndroid Build Coastguard Worker * The events of motion type are sent to PalmRejectors. PalmRejectors detect unwanted touches, 86*38e8c45fSAndroid Build Coastguard Worker * and emit input streams with the bad pointers removed. 87*38e8c45fSAndroid Build Coastguard Worker */ 88*38e8c45fSAndroid Build Coastguard Worker class UnwantedInteractionBlocker : public UnwantedInteractionBlockerInterface { 89*38e8c45fSAndroid Build Coastguard Worker public: 90*38e8c45fSAndroid Build Coastguard Worker explicit UnwantedInteractionBlocker(InputListenerInterface& listener); 91*38e8c45fSAndroid Build Coastguard Worker explicit UnwantedInteractionBlocker(InputListenerInterface& listener, bool enablePalmRejection); 92*38e8c45fSAndroid Build Coastguard Worker 93*38e8c45fSAndroid Build Coastguard Worker void notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) override; 94*38e8c45fSAndroid Build Coastguard Worker void notifyKey(const NotifyKeyArgs& args) override; 95*38e8c45fSAndroid Build Coastguard Worker void notifyMotion(const NotifyMotionArgs& args) override; 96*38e8c45fSAndroid Build Coastguard Worker void notifySwitch(const NotifySwitchArgs& args) override; 97*38e8c45fSAndroid Build Coastguard Worker void notifySensor(const NotifySensorArgs& args) override; 98*38e8c45fSAndroid Build Coastguard Worker void notifyVibratorState(const NotifyVibratorStateArgs& args) override; 99*38e8c45fSAndroid Build Coastguard Worker void notifyDeviceReset(const NotifyDeviceResetArgs& args) override; 100*38e8c45fSAndroid Build Coastguard Worker void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) override; 101*38e8c45fSAndroid Build Coastguard Worker 102*38e8c45fSAndroid Build Coastguard Worker void dump(std::string& dump) override; 103*38e8c45fSAndroid Build Coastguard Worker void monitor() override; 104*38e8c45fSAndroid Build Coastguard Worker 105*38e8c45fSAndroid Build Coastguard Worker ~UnwantedInteractionBlocker(); 106*38e8c45fSAndroid Build Coastguard Worker 107*38e8c45fSAndroid Build Coastguard Worker private: 108*38e8c45fSAndroid Build Coastguard Worker std::mutex mLock; 109*38e8c45fSAndroid Build Coastguard Worker // The next stage to pass input events to 110*38e8c45fSAndroid Build Coastguard Worker 111*38e8c45fSAndroid Build Coastguard Worker QueuedInputListener mQueuedListener; 112*38e8c45fSAndroid Build Coastguard Worker const bool mEnablePalmRejection; 113*38e8c45fSAndroid Build Coastguard Worker 114*38e8c45fSAndroid Build Coastguard Worker // When stylus is down, ignore touch 115*38e8c45fSAndroid Build Coastguard Worker PreferStylusOverTouchBlocker mPreferStylusOverTouchBlocker GUARDED_BY(mLock); 116*38e8c45fSAndroid Build Coastguard Worker 117*38e8c45fSAndroid Build Coastguard Worker // Detect and reject unwanted palms on screen 118*38e8c45fSAndroid Build Coastguard Worker // Use a separate palm rejector for every touch device. 119*38e8c45fSAndroid Build Coastguard Worker std::map<int32_t /*deviceId*/, PalmRejector> mPalmRejectors GUARDED_BY(mLock); 120*38e8c45fSAndroid Build Coastguard Worker // TODO(b/210159205): delete this when simultaneous stylus and touch is supported 121*38e8c45fSAndroid Build Coastguard Worker void notifyMotionLocked(const NotifyMotionArgs& args) REQUIRES(mLock); 122*38e8c45fSAndroid Build Coastguard Worker 123*38e8c45fSAndroid Build Coastguard Worker // Call this function for outbound events so that they can be logged when logging is enabled. 124*38e8c45fSAndroid Build Coastguard Worker void enqueueOutboundMotionLocked(const NotifyMotionArgs& args) REQUIRES(mLock); 125*38e8c45fSAndroid Build Coastguard Worker 126*38e8c45fSAndroid Build Coastguard Worker void onInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices); 127*38e8c45fSAndroid Build Coastguard Worker }; 128*38e8c45fSAndroid Build Coastguard Worker 129*38e8c45fSAndroid Build Coastguard Worker class SlotState { 130*38e8c45fSAndroid Build Coastguard Worker public: 131*38e8c45fSAndroid Build Coastguard Worker /** 132*38e8c45fSAndroid Build Coastguard Worker * Update the state using the new information provided in the NotifyMotionArgs 133*38e8c45fSAndroid Build Coastguard Worker */ 134*38e8c45fSAndroid Build Coastguard Worker void update(const NotifyMotionArgs& args); 135*38e8c45fSAndroid Build Coastguard Worker std::optional<size_t> getSlotForPointerId(int32_t pointerId) const; 136*38e8c45fSAndroid Build Coastguard Worker std::string dump() const; 137*38e8c45fSAndroid Build Coastguard Worker 138*38e8c45fSAndroid Build Coastguard Worker private: 139*38e8c45fSAndroid Build Coastguard Worker // Process a pointer with the provided action, and return the slot associated with it 140*38e8c45fSAndroid Build Coastguard Worker void processPointerId(int32_t pointerId, int32_t action); 141*38e8c45fSAndroid Build Coastguard Worker // The map from tracking id to slot state. Since the PalmRejectionFilter works close to the 142*38e8c45fSAndroid Build Coastguard Worker // evdev level, the only way to tell it about UP or CANCEL events is by sending tracking id = -1 143*38e8c45fSAndroid Build Coastguard Worker // to the appropriate touch slot. So we need to reconstruct the original slot. 144*38e8c45fSAndroid Build Coastguard Worker // The two collections below must always be in-sync. 145*38e8c45fSAndroid Build Coastguard Worker // Use std::map instead of std::unordered_map because we rely on these collections being 146*38e8c45fSAndroid Build Coastguard Worker // ordered. It also has better space efficiency than unordered_map because we only have a few 147*38e8c45fSAndroid Build Coastguard Worker // pointers most of the time. 148*38e8c45fSAndroid Build Coastguard Worker std::map<int32_t /*pointerId*/, size_t /*slot*/> mSlotsByPointerId; 149*38e8c45fSAndroid Build Coastguard Worker std::map<size_t /*slot*/, int32_t /*pointerId */> mPointerIdsBySlot; 150*38e8c45fSAndroid Build Coastguard Worker 151*38e8c45fSAndroid Build Coastguard Worker size_t findUnusedSlot() const; 152*38e8c45fSAndroid Build Coastguard Worker }; 153*38e8c45fSAndroid Build Coastguard Worker 154*38e8c45fSAndroid Build Coastguard Worker /** 155*38e8c45fSAndroid Build Coastguard Worker * Convert an Android event to a linux-like 'InProgressTouchEvdev'. The provided SlotState's 156*38e8c45fSAndroid Build Coastguard Worker * are used to figure out which slot does each pointer belong to. 157*38e8c45fSAndroid Build Coastguard Worker */ 158*38e8c45fSAndroid Build Coastguard Worker std::vector<::ui::InProgressTouchEvdev> getTouches(const NotifyMotionArgs& args, 159*38e8c45fSAndroid Build Coastguard Worker const AndroidPalmFilterDeviceInfo& deviceInfo, 160*38e8c45fSAndroid Build Coastguard Worker const SlotState& oldSlotState, 161*38e8c45fSAndroid Build Coastguard Worker const SlotState& newSlotState); 162*38e8c45fSAndroid Build Coastguard Worker 163*38e8c45fSAndroid Build Coastguard Worker class PalmRejector { 164*38e8c45fSAndroid Build Coastguard Worker public: 165*38e8c45fSAndroid Build Coastguard Worker explicit PalmRejector(const AndroidPalmFilterDeviceInfo& info, 166*38e8c45fSAndroid Build Coastguard Worker std::unique_ptr<::ui::PalmDetectionFilter> filter = nullptr); 167*38e8c45fSAndroid Build Coastguard Worker std::vector<NotifyMotionArgs> processMotion(const NotifyMotionArgs& args); 168*38e8c45fSAndroid Build Coastguard Worker 169*38e8c45fSAndroid Build Coastguard Worker // Get the device info of this device, for comparison purposes 170*38e8c45fSAndroid Build Coastguard Worker const AndroidPalmFilterDeviceInfo& getPalmFilterDeviceInfo() const; 171*38e8c45fSAndroid Build Coastguard Worker std::string dump() const; 172*38e8c45fSAndroid Build Coastguard Worker 173*38e8c45fSAndroid Build Coastguard Worker private: 174*38e8c45fSAndroid Build Coastguard Worker PalmRejector(const PalmRejector&) = delete; 175*38e8c45fSAndroid Build Coastguard Worker PalmRejector& operator=(const PalmRejector&) = delete; 176*38e8c45fSAndroid Build Coastguard Worker 177*38e8c45fSAndroid Build Coastguard Worker /** 178*38e8c45fSAndroid Build Coastguard Worker * Update the slot state and send this event to the palm rejection model for palm detection. 179*38e8c45fSAndroid Build Coastguard Worker * Return the pointer ids that should be suppressed. 180*38e8c45fSAndroid Build Coastguard Worker * 181*38e8c45fSAndroid Build Coastguard Worker * This function is not const because it has side-effects. It will update the slot state using 182*38e8c45fSAndroid Build Coastguard Worker * the incoming args! Also, it will call Filter(..), which has side-effects. 183*38e8c45fSAndroid Build Coastguard Worker */ 184*38e8c45fSAndroid Build Coastguard Worker std::set<int32_t> detectPalmPointers(const NotifyMotionArgs& args); 185*38e8c45fSAndroid Build Coastguard Worker std::unique_ptr<::ui::SharedPalmDetectionFilterState> mSharedPalmState; 186*38e8c45fSAndroid Build Coastguard Worker AndroidPalmFilterDeviceInfo mDeviceInfo; 187*38e8c45fSAndroid Build Coastguard Worker std::unique_ptr<::ui::PalmDetectionFilter> mPalmDetectionFilter; 188*38e8c45fSAndroid Build Coastguard Worker std::set<int32_t> mSuppressedPointerIds; 189*38e8c45fSAndroid Build Coastguard Worker 190*38e8c45fSAndroid Build Coastguard Worker // Used to help convert an Android touch stream to Linux input stream. 191*38e8c45fSAndroid Build Coastguard Worker SlotState mSlotState; 192*38e8c45fSAndroid Build Coastguard Worker }; 193*38e8c45fSAndroid Build Coastguard Worker 194*38e8c45fSAndroid Build Coastguard Worker } // namespace android 195