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 <android/binder_auto_utils.h>
21 #include <utils/Mutex.h>
22 #include <memory>
23 #include <mutex>
24 #include "InputFilterPolicyInterface.h"
25 #include "InputListener.h"
26 #include "NotifyArgs.h"
27 
28 /**
29  * The C++ component of InputFilter designed as a wrapper around the rust callback implementation.
30  */
31 namespace android {
32 
33 using IInputFilter = aidl::com::android::server::inputflinger::IInputFilter;
34 using AidlKeyEvent = aidl::com::android::server::inputflinger::KeyEvent;
35 using aidl::com::android::server::inputflinger::IInputThread;
36 using IInputThreadCallback =
37         aidl::com::android::server::inputflinger::IInputThread::IInputThreadCallback;
38 
39 class InputFilterCallbacks : public IInputFilter::BnInputFilterCallbacks {
40 public:
41     explicit InputFilterCallbacks(InputListenerInterface& listener,
42                                   InputFilterPolicyInterface& policy);
43     ~InputFilterCallbacks() override = default;
44 
45     uint32_t getModifierState();
46     uint32_t getLockedModifierState();
47 
48 private:
49     InputListenerInterface& mNextListener;
50     InputFilterPolicyInterface& mPolicy;
51     mutable std::mutex mLock;
52     struct StickyModifierState {
53         uint32_t modifierState;
54         uint32_t lockedModifierState;
55     } mStickyModifierState GUARDED_BY(mLock);
56 
57     ndk::ScopedAStatus sendKeyEvent(const AidlKeyEvent& event) override;
58     ndk::ScopedAStatus onModifierStateChanged(int32_t modifierState,
59                                               int32_t lockedModifierState) override;
60     ndk::ScopedAStatus createInputFilterThread(
61             const std::shared_ptr<IInputThreadCallback>& callback,
62             std::shared_ptr<IInputThread>* aidl_return) override;
63 };
64 
65 } // namespace android