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