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 notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) override;
57     void notifyKey(const NotifyKeyArgs& args) override;
58     void notifyMotion(const NotifyMotionArgs& args) override;
59     void notifySwitch(const NotifySwitchArgs& args) override;
60     void notifySensor(const NotifySensorArgs& args) override;
61     void notifyVibratorState(const NotifyVibratorStateArgs& args) override;
62     void notifyDeviceReset(const NotifyDeviceResetArgs& args) override;
63     void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) override;
64     void setAccessibilityBounceKeysThreshold(nsecs_t threshold) override;
65     void setAccessibilitySlowKeysThreshold(nsecs_t threshold) override;
66     void setAccessibilityStickyKeysEnabled(bool enabled) override;
67     void dump(std::string& dump) override;
68 
69 private:
70     InputListenerInterface& mNextListener;
71     std::shared_ptr<InputFilterCallbacks> mCallbacks;
72     InputFilterPolicyInterface& mPolicy;
73     std::shared_ptr<IInputFilter> mInputFilterRust;
74     // Keep track of connected peripherals, so that if filters are enabled later, we can pass that
75     // info to the filters
76     std::vector<AidlDeviceInfo> mDeviceInfos;
77     mutable std::mutex mLock;
78     InputFilterConfiguration mConfig GUARDED_BY(mLock);
79 
80     bool isFilterEnabled();
81     void notifyConfigurationChangedLocked() REQUIRES(mLock);
82 };
83 
84 } // namespace android
85