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