1 /*
2  * Copyright (C) 2010 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 /**
20  * Native input manager.
21  */
22 
23 #include "InputDeviceMetricsCollector.h"
24 #include "InputFilter.h"
25 #include "InputProcessor.h"
26 #include "InputReaderBase.h"
27 #include "PointerChoreographer.h"
28 #include "include/UnwantedInteractionBlockerInterface.h"
29 
30 #include <InputDispatcherInterface.h>
31 #include <InputDispatcherPolicyInterface.h>
32 #include <InputFilterPolicyInterface.h>
33 #include <PointerChoreographerPolicyInterface.h>
34 #include <input/Input.h>
35 #include <input/InputTransport.h>
36 
37 #include <aidl/com/android/server/inputflinger/IInputFlingerRust.h>
38 #include <android/os/BnInputFlinger.h>
39 #include <utils/Errors.h>
40 #include <utils/RefBase.h>
41 #include <utils/Timers.h>
42 
43 using android::os::BnInputFlinger;
44 
45 using aidl::com::android::server::inputflinger::IInputFilter;
46 using aidl::com::android::server::inputflinger::IInputFlingerRust;
47 
48 namespace android {
49 class InputChannel;
50 class InputDispatcherThread;
51 
52 /*
53  * The input manager is the core of the system event processing.
54  *
55  * The input manager has three components.
56  *
57  * 1. The InputReader class starts a thread that reads and preprocesses raw input events, applies
58  *    policy, and posts messages to a queue managed by the UnwantedInteractionBlocker.
59  * 2. The UnwantedInteractionBlocker is responsible for removing unwanted interactions. For example,
60  *    this could be a palm on the screen. This stage would alter the event stream to remove either
61  *    partially (some of the pointers) or fully (all touches) the unwanted interaction. The events
62  *    are processed on the InputReader thread, without any additional queue. The events are then
63  *    posted to the queue managed by the InputProcessor.
64  * 3. The InputProcessor class starts a thread to communicate with the device-specific
65  *    IInputProcessor HAL. It then waits on the queue of events from UnwantedInteractionBlocker,
66  *    processes the events (for example, applies a classification to the events), and queues them
67  *    for the InputDispatcher.
68  * 4. The InputDispatcher class starts a thread that waits for new events on the
69  *    previous queue and asynchronously dispatches them to applications.
70  *
71  * By design, none of these classes share any internal state.  Moreover, all communication is
72  * done one way from the InputReader to the InputDispatcher and never the reverse.  All
73  * classes may interact with the InputDispatchPolicy, however.
74  *
75  * The InputManager class never makes any calls into Java itself.  Instead, the
76  * InputDispatchPolicy is responsible for performing all external interactions with the
77  * system, including calling DVM services.
78  */
79 class InputManagerInterface : public virtual RefBase {
80 protected:
InputManagerInterface()81     InputManagerInterface() { }
~InputManagerInterface()82     virtual ~InputManagerInterface() { }
83 
84 public:
85     /* Starts the input threads. */
86     virtual status_t start() = 0;
87 
88     /* Stops the input threads and waits for them to exit. */
89     virtual status_t stop() = 0;
90 
91     /* Gets the input reader. */
92     virtual InputReaderInterface& getReader() = 0;
93 
94     /* Gets the PointerChoreographer. */
95     virtual PointerChoreographerInterface& getChoreographer() = 0;
96 
97     /* Gets the input processor. */
98     virtual InputProcessorInterface& getProcessor() = 0;
99 
100     /* Gets the metrics collector. */
101     virtual InputDeviceMetricsCollectorInterface& getMetricsCollector() = 0;
102 
103     /* Gets the input dispatcher. */
104     virtual InputDispatcherInterface& getDispatcher() = 0;
105 
106     /* Gets the input filter */
107     virtual InputFilterInterface& getInputFilter() = 0;
108 
109     /* Check that the input stages have not deadlocked. */
110     virtual void monitor() = 0;
111 
112     /* Dump the state of the components controlled by the input manager. */
113     virtual void dump(std::string& dump) = 0;
114 };
115 
116 class InputManager : public InputManagerInterface, public BnInputFlinger {
117 protected:
118     ~InputManager() override;
119 
120 public:
121     InputManager(const sp<InputReaderPolicyInterface>& readerPolicy,
122                  InputDispatcherPolicyInterface& dispatcherPolicy,
123                  PointerChoreographerPolicyInterface& choreographerPolicy,
124                  InputFilterPolicyInterface& inputFilterPolicy);
125 
126     status_t start() override;
127     status_t stop() override;
128 
129     InputReaderInterface& getReader() override;
130     PointerChoreographerInterface& getChoreographer() override;
131     InputProcessorInterface& getProcessor() override;
132     InputDeviceMetricsCollectorInterface& getMetricsCollector() override;
133     InputDispatcherInterface& getDispatcher() override;
134     InputFilterInterface& getInputFilter() override;
135     void monitor() override;
136     void dump(std::string& dump) override;
137 
138     status_t dump(int fd, const Vector<String16>& args) override;
139     binder::Status createInputChannel(const std::string& name,
140                                       android::os::InputChannelCore* outChannel) override;
141     binder::Status removeInputChannel(const sp<IBinder>& connectionToken) override;
142     binder::Status setFocusedWindow(const gui::FocusRequest&) override;
143 
144 private:
145     std::unique_ptr<InputReaderInterface> mReader;
146 
147     std::unique_ptr<UnwantedInteractionBlockerInterface> mBlocker;
148 
149     std::unique_ptr<InputFilterInterface> mInputFilter;
150 
151     std::unique_ptr<PointerChoreographerInterface> mChoreographer;
152 
153     std::unique_ptr<InputProcessorInterface> mProcessor;
154 
155     std::unique_ptr<InputDeviceMetricsCollectorInterface> mCollector;
156 
157     std::unique_ptr<InputDispatcherInterface> mDispatcher;
158 
159     std::shared_ptr<IInputFlingerRust> mInputFlingerRust;
160 
161     std::vector<std::unique_ptr<TracedInputListener>> mTracingStages;
162 };
163 
164 } // namespace android
165