xref: /aosp_15_r20/frameworks/native/services/inputflinger/InputFilterCallbacks.cpp (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 #define LOG_TAG "InputFilterCallbacks"
18 
19 #include "InputFilterCallbacks.h"
20 #include <aidl/com/android/server/inputflinger/BnInputThread.h>
21 #include <android/binder_auto_utils.h>
22 #include <utils/Looper.h>
23 #include <utils/StrongPointer.h>
24 #include <functional>
25 #include "InputThread.h"
26 
27 namespace android {
28 
29 using AidlKeyEvent = aidl::com::android::server::inputflinger::KeyEvent;
30 
keyEventToNotifyKeyArgs(const AidlKeyEvent & event)31 NotifyKeyArgs keyEventToNotifyKeyArgs(const AidlKeyEvent& event) {
32     return NotifyKeyArgs(event.id, event.eventTime, event.readTime, event.deviceId,
33                          static_cast<uint32_t>(event.source), ui::LogicalDisplayId{event.displayId},
34                          event.policyFlags, static_cast<int32_t>(event.action), event.flags,
35                          event.keyCode, event.scanCode, event.metaState, event.downTime);
36 }
37 
38 namespace {
39 
40 using namespace aidl::com::android::server::inputflinger;
41 
42 class InputFilterThread : public BnInputThread {
43 public:
InputFilterThread(std::shared_ptr<IInputThreadCallback> callback)44     InputFilterThread(std::shared_ptr<IInputThreadCallback> callback) : mCallback(callback) {
45         mLooper = sp<Looper>::make(/*allowNonCallbacks=*/false);
46         mThread = std::make_unique<InputThread>(
47                 "InputFilter", [this]() { loopOnce(); }, [this]() { mLooper->wake(); },
48                 /*isInCriticalPath=*/false);
49     }
50 
finish()51     ndk::ScopedAStatus finish() override {
52         if (mThread && mThread->isCallingThread()) {
53             ALOGE("InputFilterThread cannot be stopped on itself!");
54             return ndk::ScopedAStatus::fromStatus(INVALID_OPERATION);
55         }
56         mThread.reset();
57         return ndk::ScopedAStatus::ok();
58     }
59 
sleepUntil(nsecs_t when)60     ndk::ScopedAStatus sleepUntil(nsecs_t when) override {
61         nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
62         mLooper->pollOnce(toMillisecondTimeoutDelay(now, when));
63         return ndk::ScopedAStatus::ok();
64     }
65 
wake()66     ndk::ScopedAStatus wake() override {
67         mLooper->wake();
68         return ndk::ScopedAStatus::ok();
69     }
70 
71 private:
72     sp<Looper> mLooper;
73     std::unique_ptr<InputThread> mThread;
74     std::shared_ptr<IInputThreadCallback> mCallback;
75 
loopOnce()76     void loopOnce() { LOG_ALWAYS_FATAL_IF(!mCallback->loopOnce().isOk()); }
77 };
78 
79 } // namespace
80 
InputFilterCallbacks(InputListenerInterface & listener,InputFilterPolicyInterface & policy)81 InputFilterCallbacks::InputFilterCallbacks(InputListenerInterface& listener,
82                                            InputFilterPolicyInterface& policy)
83       : mNextListener(listener), mPolicy(policy) {}
84 
sendKeyEvent(const AidlKeyEvent & event)85 ndk::ScopedAStatus InputFilterCallbacks::sendKeyEvent(const AidlKeyEvent& event) {
86     mNextListener.notifyKey(keyEventToNotifyKeyArgs(event));
87     return ndk::ScopedAStatus::ok();
88 }
89 
onModifierStateChanged(int32_t modifierState,int32_t lockedModifierState)90 ndk::ScopedAStatus InputFilterCallbacks::onModifierStateChanged(int32_t modifierState,
91                                                                 int32_t lockedModifierState) {
92     std::scoped_lock _l(mLock);
93     mStickyModifierState.modifierState = modifierState;
94     mStickyModifierState.lockedModifierState = lockedModifierState;
95     mPolicy.notifyStickyModifierStateChanged(modifierState, lockedModifierState);
96     ALOGI("Sticky keys modifier state changed: modifierState=%d, lockedModifierState=%d",
97           modifierState, lockedModifierState);
98     return ndk::ScopedAStatus::ok();
99 }
100 
createInputFilterThread(const std::shared_ptr<IInputThreadCallback> & callback,std::shared_ptr<IInputThread> * aidl_return)101 ndk::ScopedAStatus InputFilterCallbacks::createInputFilterThread(
102         const std::shared_ptr<IInputThreadCallback>& callback,
103         std::shared_ptr<IInputThread>* aidl_return) {
104     *aidl_return = ndk::SharedRefBase::make<InputFilterThread>(callback);
105     return ndk::ScopedAStatus::ok();
106 }
107 
getModifierState()108 uint32_t InputFilterCallbacks::getModifierState() {
109     std::scoped_lock _l(mLock);
110     return mStickyModifierState.modifierState;
111 }
112 
getLockedModifierState()113 uint32_t InputFilterCallbacks::getLockedModifierState() {
114     std::scoped_lock _l(mLock);
115     return mStickyModifierState.lockedModifierState;
116 }
117 
118 } // namespace android
119