1 /*
2  * Copyright (C) 2019 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 #ifndef _UI_INPUT_INPUTDISPATCHER_INPUTDISPATCHERINTERFACE_H
18 #define _UI_INPUT_INPUTDISPATCHER_INPUTDISPATCHERINTERFACE_H
19 
20 #include <InputListener.h>
21 #include <android-base/result.h>
22 #include <android/gui/FocusRequest.h>
23 #include <android/os/BlockUntrustedTouchesMode.h>
24 #include <android/os/InputEventInjectionResult.h>
25 #include <android/os/InputEventInjectionSync.h>
26 #include <gui/InputApplication.h>
27 #include <gui/WindowInfo.h>
28 #include <input/InputDevice.h>
29 #include <input/InputTransport.h>
30 #include <unordered_map>
31 
32 namespace android {
33 
34 /* Notifies the system about input events generated by the input reader.
35  * The dispatcher is expected to be mostly asynchronous. */
36 class InputDispatcherInterface : public virtual RefBase, public InputListenerInterface {
37 protected:
InputDispatcherInterface()38     InputDispatcherInterface() {}
~InputDispatcherInterface()39     virtual ~InputDispatcherInterface() {}
40 
41 public:
42     /* Dumps the state of the input dispatcher.
43      *
44      * This method may be called on any thread (usually by the input manager). */
45     virtual void dump(std::string& dump) = 0;
46 
47     /* Called by the heatbeat to ensures that the dispatcher has not deadlocked. */
48     virtual void monitor() = 0;
49 
50     /**
51      * Wait until dispatcher is idle. That means, there are no further events to be processed,
52      * and all of the policy callbacks have been completed.
53      * Return true if the dispatcher is idle.
54      * Return false if the timeout waiting for the dispatcher to become idle has expired.
55      */
56     virtual bool waitForIdle() = 0;
57 
58     /* Make the dispatcher start processing events.
59      *
60      * The dispatcher will start consuming events from the InputListenerInterface
61      * in the order that they were received.
62      */
63     virtual status_t start() = 0;
64 
65     /* Makes the dispatcher stop processing events. */
66     virtual status_t stop() = 0;
67 
68     /* Injects an input event and optionally waits for sync.
69      * The synchronization mode determines whether the method blocks while waiting for
70      * input injection to proceed.
71      * Returns one of the INPUT_EVENT_INJECTION_XXX constants.
72      *
73      * This method may be called on any thread (usually by the input manager).
74      */
75     virtual android::os::InputEventInjectionResult injectInputEvent(
76             const InputEvent* event, int32_t injectorPid, int32_t injectorUid,
77             android::os::InputEventInjectionSync syncMode, std::chrono::milliseconds timeout,
78             uint32_t policyFlags) = 0;
79 
80     /*
81      * Check whether InputEvent actually happened by checking the signature of the event.
82      *
83      * Return nullptr if the event cannot be verified.
84      */
85     virtual std::unique_ptr<VerifiedInputEvent> verifyInputEvent(const InputEvent& event) = 0;
86 
87     /* Sets the list of input windows per display.
88      *
89      * This method may be called on any thread (usually by the input manager).
90      */
91     virtual void setInputWindows(
92             const std::unordered_map<int32_t, std::vector<sp<gui::WindowInfoHandle>>>&
93                     handlesPerDisplay) = 0;
94 
95     /* Sets the focused application on the given display.
96      *
97      * This method may be called on any thread (usually by the input manager).
98      */
99     virtual void setFocusedApplication(
100             int32_t displayId,
101             const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) = 0;
102 
103     /* Sets the focused display.
104      *
105      * This method may be called on any thread (usually by the input manager).
106      */
107     virtual void setFocusedDisplay(int32_t displayId) = 0;
108 
109     /* Sets the input dispatching mode.
110      *
111      * This method may be called on any thread (usually by the input manager).
112      */
113     virtual void setInputDispatchMode(bool enabled, bool frozen) = 0;
114 
115     /* Sets whether input event filtering is enabled.
116      * When enabled, incoming input events are sent to the policy's filterInputEvent
117      * method instead of being dispatched.  The filter is expected to use
118      * injectInputEvent to inject the events it would like to have dispatched.
119      * It should include POLICY_FLAG_FILTERED in the policy flags during injection.
120      */
121     virtual void setInputFilterEnabled(bool enabled) = 0;
122 
123     /**
124      * Set the touch mode state.
125      * Touch mode is a global state that apps may enter / exit based on specific
126      * user interactions with input devices.
127      * If true, the device is in touch mode.
128      */
129     virtual void setInTouchMode(bool inTouchMode) = 0;
130 
131     /**
132      * Sets the maximum allowed obscuring opacity by UID to propagate touches.
133      * For certain window types (eg. SAWs), the decision of honoring
134      * FLAG_NOT_TOUCHABLE or not depends on the combined obscuring opacity of
135      * the windows above the touch-consuming window.
136      */
137     virtual void setMaximumObscuringOpacityForTouch(float opacity) = 0;
138 
139     /**
140      * Sets the mode of the block untrusted touches feature.
141      *
142      * TODO(b/169067926): Clean-up feature modes.
143      */
144     virtual void setBlockUntrustedTouchesMode(android::os::BlockUntrustedTouchesMode mode) = 0;
145 
146     /* Transfers touch focus from one window to another window.
147      *
148      * Returns true on success.  False if the window did not actually have touch focus.
149      */
150     virtual bool transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken,
151                                     bool isDragDrop) = 0;
152 
153     /**
154      * Transfer touch focus to the provided channel, no matter where the current touch is.
155      *
156      * Return true on success, false if there was no on-going touch.
157      */
158     virtual bool transferTouch(const sp<IBinder>& destChannelToken) = 0;
159 
160     /**
161      * Sets focus on the specified window.
162      */
163     virtual void setFocusedWindow(const gui::FocusRequest&) = 0;
164 
165     /**
166      * Creates an input channel that may be used as targets for input events.
167      *
168      * This method may be called on any thread (usually by the input manager).
169      */
170     virtual base::Result<std::unique_ptr<InputChannel>> createInputChannel(
171             const std::string& name) = 0;
172 
173     /**
174      * Creates an input channel to be used to monitor input events.
175      *
176      * Each monitor must target a specific display and will only receive input events sent to that
177      * display. If the monitor is a gesture monitor, it will only receive pointer events on the
178      * targeted display.
179      *
180      * This method may be called on any thread (usually by the input manager).
181      */
182     virtual base::Result<std::unique_ptr<InputChannel>> createInputMonitor(int32_t displayId,
183                                                                            bool gestureMonitor,
184                                                                            const std::string& name,
185                                                                            int32_t pid) = 0;
186 
187     /* Removes input channels that will no longer receive input events.
188      *
189      * This method may be called on any thread (usually by the input manager).
190      */
191     virtual status_t removeInputChannel(const sp<IBinder>& connectionToken) = 0;
192 
193     /* Allows an input monitor steal the current pointer stream away from normal input windows.
194      *
195      * This method may be called on any thread (usually by the input manager).
196      */
197     virtual status_t pilferPointers(const sp<IBinder>& token) = 0;
198 
199     /**
200      * Enables Pointer Capture on the specified window if the window has focus.
201      *
202      * InputDispatcher is the source of truth of Pointer Capture.
203      */
204     virtual void requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) = 0;
205     /* Flush input device motion sensor.
206      *
207      * Returns true on success.
208      */
209     virtual bool flushSensor(int deviceId, InputDeviceSensorType sensorType) = 0;
210 
211     /**
212      * Called when a display has been removed from the system.
213      */
214     virtual void displayRemoved(int32_t displayId) = 0;
215 };
216 
217 } // namespace android
218 
219 #endif // _UI_INPUT_INPUTDISPATCHER_INPUTDISPATCHERINTERFACE_H
220