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/DisplayTopologyGraph.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) const = 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() const = 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<gui::Uid> 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 focused application on the given display. 92 * 93 * This method may be called on any thread (usually by the input manager). 94 */ 95 virtual void setFocusedApplication( 96 ui::LogicalDisplayId displayId, 97 const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) = 0; 98 99 /* Sets the focused display. 100 * 101 * This method may be called on any thread (usually by the input manager). 102 */ 103 virtual void setFocusedDisplay(ui::LogicalDisplayId displayId) = 0; 104 105 /** Sets the minimum time between user activity pokes. */ 106 virtual void setMinTimeBetweenUserActivityPokes(std::chrono::milliseconds interval) = 0; 107 108 /* Sets the input dispatching mode. 109 * 110 * This method may be called on any thread (usually by the input manager). 111 */ 112 virtual void setInputDispatchMode(bool enabled, bool frozen) = 0; 113 114 /* Sets whether input event filtering is enabled. 115 * When enabled, incoming input events are sent to the policy's filterInputEvent 116 * method instead of being dispatched. The filter is expected to use 117 * injectInputEvent to inject the events it would like to have dispatched. 118 * It should include POLICY_FLAG_FILTERED in the policy flags during injection. 119 */ 120 virtual void setInputFilterEnabled(bool enabled) = 0; 121 122 /** 123 * Set the touch mode state. 124 * Touch mode is a per display state that apps may enter / exit based on specific user 125 * interactions with input devices. If <code>inTouchMode</code> is set to true, the display 126 * identified by <code>displayId</code> will be changed to touch mode. Performs a permission 127 * check if hasPermission is set to false. 128 * 129 * This method also enqueues a a TouchModeEntry message for dispatching. 130 * 131 * Returns true when changing touch mode state. 132 */ 133 virtual bool setInTouchMode(bool inTouchMode, gui::Pid pid, gui::Uid uid, bool hasPermission, 134 ui::LogicalDisplayId displayId) = 0; 135 136 /** 137 * Sets the maximum allowed obscuring opacity by UID to propagate touches. 138 * For certain window types (eg. SAWs), the decision of honoring 139 * FLAG_NOT_TOUCHABLE or not depends on the combined obscuring opacity of 140 * the windows above the touch-consuming window. 141 */ 142 virtual void setMaximumObscuringOpacityForTouch(float opacity) = 0; 143 144 /** 145 * Transfers a touch gesture from one window to another window. Transferring touch will not 146 * have any effect on the focused window. 147 * 148 * Returns true on success. False if the window did not actually have an active touch gesture. 149 */ 150 virtual bool transferTouchGesture(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, 151 bool isDragDrop, bool transferEntireGesture) = 0; 152 153 /** 154 * Transfer a touch gesture to the provided channel, no matter where the current touch is. 155 * Transferring touch will not have any effect on the focused window. 156 * 157 * Returns true on success, false if there was no on-going touch on the display. 158 * @deprecated 159 */ 160 virtual bool transferTouchOnDisplay(const sp<IBinder>& destChannelToken, 161 ui::LogicalDisplayId displayId) = 0; 162 163 /** 164 * Sets focus on the specified window. 165 */ 166 virtual void setFocusedWindow(const gui::FocusRequest&) = 0; 167 168 /** 169 * Creates an input channel that may be used as targets for input events. 170 * 171 * This method may be called on any thread (usually by the input manager). 172 */ 173 virtual base::Result<std::unique_ptr<InputChannel>> createInputChannel( 174 const std::string& name) = 0; 175 176 /** 177 * Creates an input channel to be used to monitor all input events on a display. 178 * 179 * Each monitor must target a specific display and will only receive input events sent to that 180 * display. 181 * 182 * This method may be called on any thread (usually by the input manager). 183 */ 184 virtual base::Result<std::unique_ptr<InputChannel>> createInputMonitor( 185 ui::LogicalDisplayId displayId, const std::string& name, 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(ui::LogicalDisplayId displayId, 212 bool isEligible) = 0; 213 214 /* Flush input device motion sensor. 215 * 216 * Returns true on success. 217 */ 218 virtual bool flushSensor(int deviceId, InputDeviceSensorType sensorType) = 0; 219 220 /** 221 * Called when a display has been removed from the system. 222 */ 223 virtual void displayRemoved(ui::LogicalDisplayId displayId) = 0; 224 225 /* 226 * Abort the current touch stream. 227 */ 228 virtual void cancelCurrentTouch() = 0; 229 230 /* 231 * Updates whether key repeat is enabled and key repeat configuration timeout and delay. 232 */ 233 virtual void setKeyRepeatConfiguration(std::chrono::nanoseconds timeout, 234 std::chrono::nanoseconds delay, 235 bool keyRepeatEnabled) = 0; 236 237 /* 238 * Determine if a pointer from a device is being dispatched to the given window. 239 */ 240 virtual bool isPointerInWindow(const sp<IBinder>& token, ui::LogicalDisplayId displayId, 241 DeviceId deviceId, int32_t pointerId) = 0; 242 243 /* 244 * Notify the dispatcher that the state of the input method connection changed. 245 */ 246 virtual void setInputMethodConnectionIsActive(bool isActive) = 0; 247 248 /* 249 * Notify the dispatcher of the latest DisplayTopology. 250 */ 251 virtual void setDisplayTopology(const DisplayTopologyGraph& displayTopologyGraph) = 0; 252 }; 253 254 } // namespace android 255