xref: /aosp_15_r20/frameworks/native/services/inputflinger/InputFilter.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1 /*
2  * Copyright 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 #pragma once
18 
19 #include <aidl/com/android/server/inputflinger/IInputFlingerRust.h>
20 #include <utils/Mutex.h>
21 #include "InputFilterCallbacks.h"
22 #include "InputFilterPolicyInterface.h"
23 #include "InputListener.h"
24 #include "NotifyArgs.h"
25 
26 namespace android {
27 
28 /**
29  * The C++ component of InputFilter designed as a wrapper around the rust implementation.
30  */
31 class InputFilterInterface : public InputListenerInterface {
32 public:
33     /**
34      * This method may be called on any thread (usually by the input manager on a binder thread).
35      */
36     virtual void dump(std::string& dump) = 0;
37     virtual void setAccessibilityBounceKeysThreshold(nsecs_t threshold) = 0;
38     virtual void setAccessibilitySlowKeysThreshold(nsecs_t threshold) = 0;
39     virtual void setAccessibilityStickyKeysEnabled(bool enabled) = 0;
40 };
41 
42 class InputFilter : public InputFilterInterface {
43 public:
44     using IInputFlingerRust = aidl::com::android::server::inputflinger::IInputFlingerRust;
45     using IInputFilter = aidl::com::android::server::inputflinger::IInputFilter;
46     using IInputFilterCallbacks =
47             aidl::com::android::server::inputflinger::IInputFilter::IInputFilterCallbacks;
48     using InputFilterConfiguration =
49             aidl::com::android::server::inputflinger::InputFilterConfiguration;
50     using AidlDeviceInfo = aidl::com::android::server::inputflinger::DeviceInfo;
51 
52     explicit InputFilter(InputListenerInterface& listener, IInputFlingerRust& rust,
53                          InputFilterPolicyInterface& policy);
54     ~InputFilter() override = default;
55     void notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) override;
56     void notifyKey(const NotifyKeyArgs& args) override;
57     void notifyMotion(const NotifyMotionArgs& args) override;
58     void notifySwitch(const NotifySwitchArgs& args) override;
59     void notifySensor(const NotifySensorArgs& args) override;
60     void notifyVibratorState(const NotifyVibratorStateArgs& args) override;
61     void notifyDeviceReset(const NotifyDeviceResetArgs& args) override;
62     void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) override;
63     void setAccessibilityBounceKeysThreshold(nsecs_t threshold) override;
64     void setAccessibilitySlowKeysThreshold(nsecs_t threshold) override;
65     void setAccessibilityStickyKeysEnabled(bool enabled) override;
66     void dump(std::string& dump) override;
67 
68 private:
69     InputListenerInterface& mNextListener;
70     std::shared_ptr<InputFilterCallbacks> mCallbacks;
71     InputFilterPolicyInterface& mPolicy;
72     std::shared_ptr<IInputFilter> mInputFilterRust;
73     // Keep track of connected peripherals, so that if filters are enabled later, we can pass that
74     // info to the filters
75     std::vector<AidlDeviceInfo> mDeviceInfos;
76     mutable std::mutex mLock;
77     InputFilterConfiguration mConfig GUARDED_BY(mLock);
78 
79     bool isFilterEnabled();
80     void notifyConfigurationChangedLocked() REQUIRES(mLock);
81 };
82 
83 } // namespace android
84