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