Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 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 | #ifndef _UI_INPUT_DISPATCHER_H |
| 18 | #define _UI_INPUT_DISPATCHER_H |
| 19 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 20 | #include <cutils/atomic.h> |
| 21 | #include <input/ISetInputWindowsListener.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 22 | #include <input/Input.h> |
Robert Carr | 3720ed0 | 2018-08-08 16:08:27 -0700 | [diff] [blame] | 23 | #include <input/InputApplication.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 24 | #include <input/InputTransport.h> |
Robert Carr | 3720ed0 | 2018-08-08 16:08:27 -0700 | [diff] [blame] | 25 | #include <input/InputWindow.h> |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 26 | #include <ui/Region.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 27 | #include <utils/BitSet.h> |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 28 | #include <utils/Looper.h> |
| 29 | #include <utils/RefBase.h> |
| 30 | #include <utils/Timers.h> |
| 31 | #include <utils/threads.h> |
| 32 | #include <condition_variable> |
| 33 | #include <optional> |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 34 | #include <unordered_map> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 35 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 36 | #include <limits.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 37 | #include <stddef.h> |
| 38 | #include <unistd.h> |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 39 | #include <deque> |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 40 | #include <unordered_map> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 41 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 42 | #include "InputListener.h" |
Prabir Pradhan | 79a4f0c | 2019-01-09 11:24:01 -0800 | [diff] [blame] | 43 | #include "InputReporterInterface.h" |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 44 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 45 | namespace android { |
| 46 | |
| 47 | /* |
| 48 | * Constants used to report the outcome of input event injection. |
| 49 | */ |
| 50 | enum { |
| 51 | /* (INTERNAL USE ONLY) Specifies that injection is pending and its outcome is unknown. */ |
| 52 | INPUT_EVENT_INJECTION_PENDING = -1, |
| 53 | |
| 54 | /* Injection succeeded. */ |
| 55 | INPUT_EVENT_INJECTION_SUCCEEDED = 0, |
| 56 | |
| 57 | /* Injection failed because the injector did not have permission to inject |
| 58 | * into the application with input focus. */ |
| 59 | INPUT_EVENT_INJECTION_PERMISSION_DENIED = 1, |
| 60 | |
| 61 | /* Injection failed because there were no available input targets. */ |
| 62 | INPUT_EVENT_INJECTION_FAILED = 2, |
| 63 | |
| 64 | /* Injection failed due to a timeout. */ |
| 65 | INPUT_EVENT_INJECTION_TIMED_OUT = 3 |
| 66 | }; |
| 67 | |
| 68 | /* |
| 69 | * Constants used to determine the input event injection synchronization mode. |
| 70 | */ |
| 71 | enum { |
| 72 | /* Injection is asynchronous and is assumed always to be successful. */ |
| 73 | INPUT_EVENT_INJECTION_SYNC_NONE = 0, |
| 74 | |
| 75 | /* Waits for previous events to be dispatched so that the input dispatcher can determine |
| 76 | * whether input event injection willbe permitted based on the current input focus. |
| 77 | * Does not wait for the input event to finish processing. */ |
| 78 | INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT = 1, |
| 79 | |
| 80 | /* Waits for the input event to be completely processed. */ |
| 81 | INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISHED = 2, |
| 82 | }; |
| 83 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 84 | /* |
| 85 | * An input target specifies how an input event is to be dispatched to a particular window |
| 86 | * including the window's input channel, control flags, a timeout, and an X / Y offset to |
| 87 | * be added to input event coordinates to compensate for the absolute position of the |
| 88 | * window area. |
| 89 | */ |
| 90 | struct InputTarget { |
| 91 | enum { |
| 92 | /* This flag indicates that the event is being delivered to a foreground application. */ |
| 93 | FLAG_FOREGROUND = 1 << 0, |
| 94 | |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 95 | /* This flag indicates that the MotionEvent falls within the area of the target |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 96 | * obscured by another visible window above it. The motion event should be |
| 97 | * delivered with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED. */ |
| 98 | FLAG_WINDOW_IS_OBSCURED = 1 << 1, |
| 99 | |
| 100 | /* This flag indicates that a motion event is being split across multiple windows. */ |
| 101 | FLAG_SPLIT = 1 << 2, |
| 102 | |
| 103 | /* This flag indicates that the pointer coordinates dispatched to the application |
| 104 | * will be zeroed out to avoid revealing information to an application. This is |
| 105 | * used in conjunction with FLAG_DISPATCH_AS_OUTSIDE to prevent apps not sharing |
| 106 | * the same UID from watching all touches. */ |
| 107 | FLAG_ZERO_COORDS = 1 << 3, |
| 108 | |
| 109 | /* This flag indicates that the event should be sent as is. |
| 110 | * Should always be set unless the event is to be transmuted. */ |
| 111 | FLAG_DISPATCH_AS_IS = 1 << 8, |
| 112 | |
| 113 | /* This flag indicates that a MotionEvent with AMOTION_EVENT_ACTION_DOWN falls outside |
| 114 | * of the area of this target and so should instead be delivered as an |
| 115 | * AMOTION_EVENT_ACTION_OUTSIDE to this target. */ |
| 116 | FLAG_DISPATCH_AS_OUTSIDE = 1 << 9, |
| 117 | |
| 118 | /* This flag indicates that a hover sequence is starting in the given window. |
| 119 | * The event is transmuted into ACTION_HOVER_ENTER. */ |
| 120 | FLAG_DISPATCH_AS_HOVER_ENTER = 1 << 10, |
| 121 | |
| 122 | /* This flag indicates that a hover event happened outside of a window which handled |
| 123 | * previous hover events, signifying the end of the current hover sequence for that |
| 124 | * window. |
| 125 | * The event is transmuted into ACTION_HOVER_ENTER. */ |
| 126 | FLAG_DISPATCH_AS_HOVER_EXIT = 1 << 11, |
| 127 | |
| 128 | /* This flag indicates that the event should be canceled. |
| 129 | * It is used to transmute ACTION_MOVE into ACTION_CANCEL when a touch slips |
| 130 | * outside of a window. */ |
| 131 | FLAG_DISPATCH_AS_SLIPPERY_EXIT = 1 << 12, |
| 132 | |
| 133 | /* This flag indicates that the event should be dispatched as an initial down. |
| 134 | * It is used to transmute ACTION_MOVE into ACTION_DOWN when a touch slips |
| 135 | * into a new window. */ |
| 136 | FLAG_DISPATCH_AS_SLIPPERY_ENTER = 1 << 13, |
| 137 | |
| 138 | /* Mask for all dispatch modes. */ |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 139 | FLAG_DISPATCH_MASK = FLAG_DISPATCH_AS_IS | FLAG_DISPATCH_AS_OUTSIDE | |
| 140 | FLAG_DISPATCH_AS_HOVER_ENTER | FLAG_DISPATCH_AS_HOVER_EXIT | |
| 141 | FLAG_DISPATCH_AS_SLIPPERY_EXIT | FLAG_DISPATCH_AS_SLIPPERY_ENTER, |
Michael Wright | cdcd8f2 | 2016-03-22 16:52:13 -0700 | [diff] [blame] | 142 | |
| 143 | /* This flag indicates that the target of a MotionEvent is partly or wholly |
| 144 | * obscured by another visible window above it. The motion event should be |
| 145 | * delivered with flag AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED. */ |
| 146 | FLAG_WINDOW_IS_PARTIALLY_OBSCURED = 1 << 14, |
| 147 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | // The input channel to be targeted. |
| 151 | sp<InputChannel> inputChannel; |
| 152 | |
| 153 | // Flags for the input target. |
| 154 | int32_t flags; |
| 155 | |
| 156 | // The x and y offset to add to a MotionEvent as it is delivered. |
| 157 | // (ignored for KeyEvents) |
| 158 | float xOffset, yOffset; |
| 159 | |
| 160 | // Scaling factor to apply to MotionEvent as it is delivered. |
| 161 | // (ignored for KeyEvents) |
Robert Carr | e07e103 | 2018-11-26 12:55:53 -0800 | [diff] [blame] | 162 | float globalScaleFactor; |
| 163 | float windowXScale = 1.0f; |
| 164 | float windowYScale = 1.0f; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 165 | |
| 166 | // The subset of pointer ids to include in motion events dispatched to this input target |
| 167 | // if FLAG_SPLIT is set. |
| 168 | BitSet32 pointerIds; |
| 169 | }; |
| 170 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 171 | /* |
| 172 | * Input dispatcher configuration. |
| 173 | * |
| 174 | * Specifies various options that modify the behavior of the input dispatcher. |
| 175 | * The values provided here are merely defaults. The actual values will come from ViewConfiguration |
| 176 | * and are passed into the dispatcher during initialization. |
| 177 | */ |
| 178 | struct InputDispatcherConfiguration { |
| 179 | // The key repeat initial timeout. |
| 180 | nsecs_t keyRepeatTimeout; |
| 181 | |
| 182 | // The key repeat inter-key delay. |
| 183 | nsecs_t keyRepeatDelay; |
| 184 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 185 | InputDispatcherConfiguration() |
| 186 | : keyRepeatTimeout(500 * 1000000LL), keyRepeatDelay(50 * 1000000LL) {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 187 | }; |
| 188 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 189 | /* |
| 190 | * Input dispatcher policy interface. |
| 191 | * |
| 192 | * The input reader policy is used by the input reader to interact with the Window Manager |
| 193 | * and other system components. |
| 194 | * |
| 195 | * The actual implementation is partially supported by callbacks into the DVM |
| 196 | * via JNI. This interface is also mocked in the unit tests. |
| 197 | */ |
| 198 | class InputDispatcherPolicyInterface : public virtual RefBase { |
| 199 | protected: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 200 | InputDispatcherPolicyInterface() {} |
| 201 | virtual ~InputDispatcherPolicyInterface() {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 202 | |
| 203 | public: |
| 204 | /* Notifies the system that a configuration change has occurred. */ |
| 205 | virtual void notifyConfigurationChanged(nsecs_t when) = 0; |
| 206 | |
| 207 | /* Notifies the system that an application is not responding. |
| 208 | * Returns a new timeout to continue waiting, or 0 to abort dispatch. */ |
| 209 | virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 210 | const sp<IBinder>& token, const std::string& reason) = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 211 | |
| 212 | /* Notifies the system that an input channel is unrecoverably broken. */ |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 213 | virtual void notifyInputChannelBroken(const sp<IBinder>& token) = 0; |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 214 | virtual void notifyFocusChanged(const sp<IBinder>& oldToken, const sp<IBinder>& newToken) = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 215 | |
| 216 | /* Gets the input dispatcher configuration. */ |
| 217 | virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) = 0; |
| 218 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 219 | /* Filters an input event. |
| 220 | * Return true to dispatch the event unmodified, false to consume the event. |
| 221 | * A filter can also transform and inject events later by passing POLICY_FLAG_FILTERED |
| 222 | * to injectInputEvent. |
| 223 | */ |
| 224 | virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) = 0; |
| 225 | |
| 226 | /* Intercepts a key event immediately before queueing it. |
| 227 | * The policy can use this method as an opportunity to perform power management functions |
| 228 | * and early event preprocessing such as updating policy flags. |
| 229 | * |
| 230 | * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event |
| 231 | * should be dispatched to applications. |
| 232 | */ |
| 233 | virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags) = 0; |
| 234 | |
| 235 | /* Intercepts a touch, trackball or other motion event before queueing it. |
| 236 | * The policy can use this method as an opportunity to perform power management functions |
| 237 | * and early event preprocessing such as updating policy flags. |
| 238 | * |
| 239 | * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event |
| 240 | * should be dispatched to applications. |
| 241 | */ |
Charles Chen | 3611f1f | 2019-01-29 17:26:18 +0800 | [diff] [blame] | 242 | virtual void interceptMotionBeforeQueueing(const int32_t displayId, nsecs_t when, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 243 | uint32_t& policyFlags) = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 244 | |
| 245 | /* Allows the policy a chance to intercept a key before dispatching. */ |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 246 | virtual nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>& token, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 247 | const KeyEvent* keyEvent, |
| 248 | uint32_t policyFlags) = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 249 | |
| 250 | /* Allows the policy a chance to perform default processing for an unhandled key. |
| 251 | * Returns an alternate keycode to redispatch as a fallback, or 0 to give up. */ |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 252 | virtual bool dispatchUnhandledKey(const sp<IBinder>& token, const KeyEvent* keyEvent, |
| 253 | uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 254 | |
| 255 | /* Notifies the policy about switch events. |
| 256 | */ |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 257 | virtual void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask, |
| 258 | uint32_t policyFlags) = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 259 | |
| 260 | /* Poke user activity for an event dispatched to a window. */ |
| 261 | virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType) = 0; |
| 262 | |
| 263 | /* Checks whether a given application pid/uid has permission to inject input events |
| 264 | * into other applications. |
| 265 | * |
| 266 | * This method is special in that its implementation promises to be non-reentrant and |
| 267 | * is safe to call while holding other locks. (Most other methods make no such guarantees!) |
| 268 | */ |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 269 | virtual bool checkInjectEventsPermissionNonReentrant(int32_t injectorPid, |
| 270 | int32_t injectorUid) = 0; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 271 | |
| 272 | /* Notifies the policy that a pointer down event has occurred outside the current focused |
| 273 | * window. |
| 274 | * |
| 275 | * The touchedToken passed as an argument is the window that received the input event. |
| 276 | */ |
| 277 | virtual void onPointerDownOutsideFocus(const sp<IBinder>& touchedToken) = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 278 | }; |
| 279 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 280 | /* Notifies the system about input events generated by the input reader. |
| 281 | * The dispatcher is expected to be mostly asynchronous. */ |
| 282 | class InputDispatcherInterface : public virtual RefBase, public InputListenerInterface { |
| 283 | protected: |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 284 | InputDispatcherInterface() {} |
| 285 | virtual ~InputDispatcherInterface() {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 286 | |
| 287 | public: |
| 288 | /* Dumps the state of the input dispatcher. |
| 289 | * |
| 290 | * This method may be called on any thread (usually by the input manager). */ |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 291 | virtual void dump(std::string& dump) = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 292 | |
| 293 | /* Called by the heatbeat to ensures that the dispatcher has not deadlocked. */ |
| 294 | virtual void monitor() = 0; |
| 295 | |
| 296 | /* Runs a single iteration of the dispatch loop. |
| 297 | * Nominally processes one queued event, a timeout, or a response from an input consumer. |
| 298 | * |
| 299 | * This method should only be called on the input dispatcher thread. |
| 300 | */ |
| 301 | virtual void dispatchOnce() = 0; |
| 302 | |
| 303 | /* Injects an input event and optionally waits for sync. |
| 304 | * The synchronization mode determines whether the method blocks while waiting for |
| 305 | * input injection to proceed. |
| 306 | * Returns one of the INPUT_EVENT_INJECTION_XXX constants. |
| 307 | * |
| 308 | * This method may be called on any thread (usually by the input manager). |
| 309 | */ |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 310 | virtual int32_t injectInputEvent(const InputEvent* event, int32_t injectorPid, |
| 311 | int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis, |
| 312 | uint32_t policyFlags) = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 313 | |
| 314 | /* Sets the list of input windows. |
| 315 | * |
| 316 | * This method may be called on any thread (usually by the input manager). |
| 317 | */ |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 318 | virtual void setInputWindows( |
| 319 | const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId, |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 320 | const sp<ISetInputWindowsListener>& setInputWindowsListener = nullptr) = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 321 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 322 | /* Sets the focused application on the given display. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 323 | * |
| 324 | * This method may be called on any thread (usually by the input manager). |
| 325 | */ |
| 326 | virtual void setFocusedApplication( |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 327 | int32_t displayId, const sp<InputApplicationHandle>& inputApplicationHandle) = 0; |
| 328 | |
| 329 | /* Sets the focused display. |
| 330 | * |
| 331 | * This method may be called on any thread (usually by the input manager). |
| 332 | */ |
| 333 | virtual void setFocusedDisplay(int32_t displayId) = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 334 | |
| 335 | /* Sets the input dispatching mode. |
| 336 | * |
| 337 | * This method may be called on any thread (usually by the input manager). |
| 338 | */ |
| 339 | virtual void setInputDispatchMode(bool enabled, bool frozen) = 0; |
| 340 | |
| 341 | /* Sets whether input event filtering is enabled. |
| 342 | * When enabled, incoming input events are sent to the policy's filterInputEvent |
| 343 | * method instead of being dispatched. The filter is expected to use |
| 344 | * injectInputEvent to inject the events it would like to have dispatched. |
| 345 | * It should include POLICY_FLAG_FILTERED in the policy flags during injection. |
| 346 | */ |
| 347 | virtual void setInputFilterEnabled(bool enabled) = 0; |
| 348 | |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 349 | /* Transfers touch focus from one window to another window. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 350 | * |
| 351 | * Returns true on success. False if the window did not actually have touch focus. |
| 352 | */ |
chaviw | fbe5d9c | 2018-12-26 12:23:37 -0800 | [diff] [blame] | 353 | virtual bool transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken) = 0; |
| 354 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 355 | /* Registers input channels that may be used as targets for input events. |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 356 | * |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 357 | * This method may be called on any thread (usually by the input manager). |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 358 | */ |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 359 | virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel, |
| 360 | int32_t displayId) = 0; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 361 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 362 | /* Registers input channels to be used to monitor input events. |
| 363 | * |
| 364 | * Each monitor must target a specific display and will only receive input events sent to that |
| 365 | * display. If the monitor is a gesture monitor, it will only receive pointer events on the |
| 366 | * targeted display. |
| 367 | * |
| 368 | * This method may be called on any thread (usually by the input manager). |
| 369 | */ |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 370 | virtual status_t registerInputMonitor(const sp<InputChannel>& inputChannel, int32_t displayId, |
| 371 | bool gestureMonitor) = 0; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 372 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 373 | /* Unregister input channels that will no longer receive input events. |
| 374 | * |
| 375 | * This method may be called on any thread (usually by the input manager). |
| 376 | */ |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 377 | virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) = 0; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 378 | |
| 379 | /* Allows an input monitor steal the current pointer stream away from normal input windows. |
| 380 | * |
| 381 | * This method may be called on any thread (usually by the input manager). |
| 382 | */ |
| 383 | virtual status_t pilferPointers(const sp<IBinder>& token) = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 384 | }; |
| 385 | |
| 386 | /* Dispatches events to input targets. Some functions of the input dispatcher, such as |
| 387 | * identifying input targets, are controlled by a separate policy object. |
| 388 | * |
| 389 | * IMPORTANT INVARIANT: |
| 390 | * Because the policy can potentially block or cause re-entrance into the input dispatcher, |
| 391 | * the input dispatcher never calls into the policy while holding its internal locks. |
| 392 | * The implementation is also carefully designed to recover from scenarios such as an |
| 393 | * input channel becoming unregistered while identifying input targets or processing timeouts. |
| 394 | * |
| 395 | * Methods marked 'Locked' must be called with the lock acquired. |
| 396 | * |
| 397 | * Methods marked 'LockedInterruptible' must be called with the lock acquired but |
| 398 | * may during the course of their execution release the lock, call into the policy, and |
| 399 | * then reacquire the lock. The caller is responsible for recovering gracefully. |
| 400 | * |
| 401 | * A 'LockedInterruptible' method may called a 'Locked' method, but NOT vice-versa. |
| 402 | */ |
| 403 | class InputDispatcher : public InputDispatcherInterface { |
| 404 | protected: |
| 405 | virtual ~InputDispatcher(); |
| 406 | |
| 407 | public: |
| 408 | explicit InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy); |
| 409 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 410 | virtual void dump(std::string& dump) override; |
| 411 | virtual void monitor() override; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 412 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 413 | virtual void dispatchOnce() override; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 414 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 415 | virtual void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) override; |
| 416 | virtual void notifyKey(const NotifyKeyArgs* args) override; |
| 417 | virtual void notifyMotion(const NotifyMotionArgs* args) override; |
| 418 | virtual void notifySwitch(const NotifySwitchArgs* args) override; |
| 419 | virtual void notifyDeviceReset(const NotifyDeviceResetArgs* args) override; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 420 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 421 | virtual int32_t injectInputEvent(const InputEvent* event, int32_t injectorPid, |
| 422 | int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis, |
| 423 | uint32_t policyFlags) override; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 424 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 425 | virtual void setInputWindows( |
| 426 | const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId, |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 427 | const sp<ISetInputWindowsListener>& setInputWindowsListener = nullptr) override; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 428 | virtual void setFocusedApplication( |
| 429 | int32_t displayId, const sp<InputApplicationHandle>& inputApplicationHandle) override; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 430 | virtual void setFocusedDisplay(int32_t displayId) override; |
| 431 | virtual void setInputDispatchMode(bool enabled, bool frozen) override; |
| 432 | virtual void setInputFilterEnabled(bool enabled) override; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 433 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 434 | virtual bool transferTouchFocus(const sp<IBinder>& fromToken, |
| 435 | const sp<IBinder>& toToken) override; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 436 | |
| 437 | virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 438 | int32_t displayId) override; |
| 439 | virtual status_t registerInputMonitor(const sp<InputChannel>& inputChannel, int32_t displayId, |
| 440 | bool isGestureMonitor) override; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 441 | virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) override; |
| 442 | virtual status_t pilferPointers(const sp<IBinder>& token) override; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 443 | |
| 444 | private: |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 445 | struct InjectionState { |
| 446 | mutable int32_t refCount; |
| 447 | |
| 448 | int32_t injectorPid; |
| 449 | int32_t injectorUid; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 450 | int32_t injectionResult; // initially INPUT_EVENT_INJECTION_PENDING |
| 451 | bool injectionIsAsync; // set to true if injection is not waiting for the result |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 452 | int32_t pendingForegroundDispatches; // the number of foreground dispatches in progress |
| 453 | |
| 454 | InjectionState(int32_t injectorPid, int32_t injectorUid); |
| 455 | void release(); |
| 456 | |
| 457 | private: |
| 458 | ~InjectionState(); |
| 459 | }; |
| 460 | |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 461 | struct EventEntry { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 462 | enum { TYPE_CONFIGURATION_CHANGED, TYPE_DEVICE_RESET, TYPE_KEY, TYPE_MOTION }; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 463 | |
Prabir Pradhan | 42611e0 | 2018-11-27 14:04:02 -0800 | [diff] [blame] | 464 | uint32_t sequenceNum; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 465 | mutable int32_t refCount; |
| 466 | int32_t type; |
| 467 | nsecs_t eventTime; |
| 468 | uint32_t policyFlags; |
| 469 | InjectionState* injectionState; |
| 470 | |
| 471 | bool dispatchInProgress; // initially false, set to true while dispatching |
| 472 | |
Yi Kong | 9b14ac6 | 2018-07-17 13:48:38 -0700 | [diff] [blame] | 473 | inline bool isInjected() const { return injectionState != nullptr; } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 474 | |
| 475 | void release(); |
| 476 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 477 | virtual void appendDescription(std::string& msg) const = 0; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 478 | |
| 479 | protected: |
Prabir Pradhan | 42611e0 | 2018-11-27 14:04:02 -0800 | [diff] [blame] | 480 | EventEntry(uint32_t sequenceNum, int32_t type, nsecs_t eventTime, uint32_t policyFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 481 | virtual ~EventEntry(); |
| 482 | void releaseInjectionState(); |
| 483 | }; |
| 484 | |
| 485 | struct ConfigurationChangedEntry : EventEntry { |
Prabir Pradhan | 42611e0 | 2018-11-27 14:04:02 -0800 | [diff] [blame] | 486 | explicit ConfigurationChangedEntry(uint32_t sequenceNum, nsecs_t eventTime); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 487 | virtual void appendDescription(std::string& msg) const; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 488 | |
| 489 | protected: |
| 490 | virtual ~ConfigurationChangedEntry(); |
| 491 | }; |
| 492 | |
| 493 | struct DeviceResetEntry : EventEntry { |
| 494 | int32_t deviceId; |
| 495 | |
Prabir Pradhan | 42611e0 | 2018-11-27 14:04:02 -0800 | [diff] [blame] | 496 | DeviceResetEntry(uint32_t sequenceNum, nsecs_t eventTime, int32_t deviceId); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 497 | virtual void appendDescription(std::string& msg) const; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 498 | |
| 499 | protected: |
| 500 | virtual ~DeviceResetEntry(); |
| 501 | }; |
| 502 | |
| 503 | struct KeyEntry : EventEntry { |
| 504 | int32_t deviceId; |
| 505 | uint32_t source; |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 506 | int32_t displayId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 507 | int32_t action; |
| 508 | int32_t flags; |
| 509 | int32_t keyCode; |
| 510 | int32_t scanCode; |
| 511 | int32_t metaState; |
| 512 | int32_t repeatCount; |
| 513 | nsecs_t downTime; |
| 514 | |
| 515 | bool syntheticRepeat; // set to true for synthetic key repeats |
| 516 | |
| 517 | enum InterceptKeyResult { |
| 518 | INTERCEPT_KEY_RESULT_UNKNOWN, |
| 519 | INTERCEPT_KEY_RESULT_SKIP, |
| 520 | INTERCEPT_KEY_RESULT_CONTINUE, |
| 521 | INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER, |
| 522 | }; |
| 523 | InterceptKeyResult interceptKeyResult; // set based on the interception result |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 524 | nsecs_t interceptKeyWakeupTime; // used with INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 525 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 526 | KeyEntry(uint32_t sequenceNum, nsecs_t eventTime, int32_t deviceId, uint32_t source, |
| 527 | int32_t displayId, uint32_t policyFlags, int32_t action, int32_t flags, |
| 528 | int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount, |
| 529 | nsecs_t downTime); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 530 | virtual void appendDescription(std::string& msg) const; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 531 | void recycle(); |
| 532 | |
| 533 | protected: |
| 534 | virtual ~KeyEntry(); |
| 535 | }; |
| 536 | |
| 537 | struct MotionEntry : EventEntry { |
| 538 | nsecs_t eventTime; |
| 539 | int32_t deviceId; |
| 540 | uint32_t source; |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 541 | int32_t displayId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 542 | int32_t action; |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 543 | int32_t actionButton; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 544 | int32_t flags; |
| 545 | int32_t metaState; |
| 546 | int32_t buttonState; |
Siarhei Vishniakou | 16a2e30 | 2019-01-14 19:21:45 -0800 | [diff] [blame] | 547 | MotionClassification classification; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 548 | int32_t edgeFlags; |
| 549 | float xPrecision; |
| 550 | float yPrecision; |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 551 | float xCursorPosition; |
| 552 | float yCursorPosition; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 553 | nsecs_t downTime; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 554 | uint32_t pointerCount; |
| 555 | PointerProperties pointerProperties[MAX_POINTERS]; |
| 556 | PointerCoords pointerCoords[MAX_POINTERS]; |
| 557 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 558 | MotionEntry(uint32_t sequenceNum, nsecs_t eventTime, int32_t deviceId, uint32_t source, |
| 559 | int32_t displayId, uint32_t policyFlags, int32_t action, int32_t actionButton, |
| 560 | int32_t flags, int32_t metaState, int32_t buttonState, |
| 561 | MotionClassification classification, int32_t edgeFlags, float xPrecision, |
| 562 | float yPrecision, float xCursorPosition, float yCursorPosition, |
| 563 | nsecs_t downTime, uint32_t pointerCount, |
| 564 | const PointerProperties* pointerProperties, const PointerCoords* pointerCoords, |
| 565 | float xOffset, float yOffset); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 566 | virtual void appendDescription(std::string& msg) const; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 567 | |
| 568 | protected: |
| 569 | virtual ~MotionEntry(); |
| 570 | }; |
| 571 | |
| 572 | // Tracks the progress of dispatching a particular event to a particular connection. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 573 | struct DispatchEntry { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 574 | const uint32_t seq; // unique sequence number, never 0 |
| 575 | |
| 576 | EventEntry* eventEntry; // the event to dispatch |
| 577 | int32_t targetFlags; |
| 578 | float xOffset; |
| 579 | float yOffset; |
Robert Carr | e07e103 | 2018-11-26 12:55:53 -0800 | [diff] [blame] | 580 | float globalScaleFactor; |
| 581 | float windowXScale = 1.0f; |
| 582 | float windowYScale = 1.0f; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 583 | nsecs_t deliveryTime; // time when the event was actually delivered |
| 584 | |
| 585 | // Set to the resolved action and flags when the event is enqueued. |
| 586 | int32_t resolvedAction; |
| 587 | int32_t resolvedFlags; |
| 588 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 589 | DispatchEntry(EventEntry* eventEntry, int32_t targetFlags, float xOffset, float yOffset, |
| 590 | float globalScaleFactor, float windowXScale, float windowYScale); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 591 | ~DispatchEntry(); |
| 592 | |
| 593 | inline bool hasForegroundTarget() const { |
| 594 | return targetFlags & InputTarget::FLAG_FOREGROUND; |
| 595 | } |
| 596 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 597 | inline bool isSplit() const { return targetFlags & InputTarget::FLAG_SPLIT; } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 598 | |
| 599 | private: |
| 600 | static volatile int32_t sNextSeqAtomic; |
| 601 | |
| 602 | static uint32_t nextSeq(); |
| 603 | }; |
| 604 | |
| 605 | // A command entry captures state and behavior for an action to be performed in the |
| 606 | // dispatch loop after the initial processing has taken place. It is essentially |
| 607 | // a kind of continuation used to postpone sensitive policy interactions to a point |
| 608 | // in the dispatch loop where it is safe to release the lock (generally after finishing |
| 609 | // the critical parts of the dispatch cycle). |
| 610 | // |
| 611 | // The special thing about commands is that they can voluntarily release and reacquire |
| 612 | // the dispatcher lock at will. Initially when the command starts running, the |
| 613 | // dispatcher lock is held. However, if the command needs to call into the policy to |
| 614 | // do some work, it can release the lock, do the work, then reacquire the lock again |
| 615 | // before returning. |
| 616 | // |
| 617 | // This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch |
| 618 | // never calls into the policy while holding its lock. |
| 619 | // |
| 620 | // Commands are implicitly 'LockedInterruptible'. |
| 621 | struct CommandEntry; |
Siarhei Vishniakou | 49a350a | 2019-07-26 18:44:23 -0700 | [diff] [blame] | 622 | typedef std::function<void(InputDispatcher&, CommandEntry*)> Command; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 623 | |
| 624 | class Connection; |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 625 | struct CommandEntry { |
Chih-Hung Hsieh | 6d2ede1 | 2016-09-01 11:28:23 -0700 | [diff] [blame] | 626 | explicit CommandEntry(Command command); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 627 | ~CommandEntry(); |
| 628 | |
| 629 | Command command; |
| 630 | |
| 631 | // parameters for the command (usage varies by command) |
| 632 | sp<Connection> connection; |
| 633 | nsecs_t eventTime; |
| 634 | KeyEntry* keyEntry; |
| 635 | sp<InputApplicationHandle> inputApplicationHandle; |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 636 | std::string reason; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 637 | int32_t userActivityEventType; |
| 638 | uint32_t seq; |
| 639 | bool handled; |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 640 | sp<InputChannel> inputChannel; |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 641 | sp<IBinder> oldToken; |
| 642 | sp<IBinder> newToken; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 643 | }; |
| 644 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 645 | /* Specifies which events are to be canceled and why. */ |
| 646 | struct CancelationOptions { |
| 647 | enum Mode { |
| 648 | CANCEL_ALL_EVENTS = 0, |
| 649 | CANCEL_POINTER_EVENTS = 1, |
| 650 | CANCEL_NON_POINTER_EVENTS = 2, |
| 651 | CANCEL_FALLBACK_EVENTS = 3, |
| 652 | }; |
| 653 | |
| 654 | // The criterion to use to determine which events should be canceled. |
| 655 | Mode mode; |
| 656 | |
| 657 | // Descriptive reason for the cancelation. |
| 658 | const char* reason; |
| 659 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 660 | // The specific keycode of the key event to cancel, or nullopt to cancel any key event. |
| 661 | std::optional<int32_t> keyCode = std::nullopt; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 662 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 663 | // The specific device id of events to cancel, or nullopt to cancel events from any device. |
| 664 | std::optional<int32_t> deviceId = std::nullopt; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 665 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 666 | // The specific display id of events to cancel, or nullopt to cancel events on any display. |
| 667 | std::optional<int32_t> displayId = std::nullopt; |
| 668 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 669 | CancelationOptions(Mode mode, const char* reason) : mode(mode), reason(reason) {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 670 | }; |
| 671 | |
| 672 | /* Tracks dispatched key and motion event state so that cancelation events can be |
| 673 | * synthesized when events are dropped. */ |
| 674 | class InputState { |
| 675 | public: |
| 676 | InputState(); |
| 677 | ~InputState(); |
| 678 | |
| 679 | // Returns true if there is no state to be canceled. |
| 680 | bool isNeutral() const; |
| 681 | |
| 682 | // Returns true if the specified source is known to have received a hover enter |
| 683 | // motion event. |
| 684 | bool isHovering(int32_t deviceId, uint32_t source, int32_t displayId) const; |
| 685 | |
| 686 | // Records tracking information for a key event that has just been published. |
| 687 | // Returns true if the event should be delivered, false if it is inconsistent |
| 688 | // and should be skipped. |
| 689 | bool trackKey(const KeyEntry* entry, int32_t action, int32_t flags); |
| 690 | |
| 691 | // Records tracking information for a motion event that has just been published. |
| 692 | // Returns true if the event should be delivered, false if it is inconsistent |
| 693 | // and should be skipped. |
| 694 | bool trackMotion(const MotionEntry* entry, int32_t action, int32_t flags); |
| 695 | |
| 696 | // Synthesizes cancelation events for the current state and resets the tracked state. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 697 | void synthesizeCancelationEvents(nsecs_t currentTime, std::vector<EventEntry*>& outEvents, |
| 698 | const CancelationOptions& options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 699 | |
| 700 | // Clears the current state. |
| 701 | void clear(); |
| 702 | |
| 703 | // Copies pointer-related parts of the input state to another instance. |
| 704 | void copyPointerStateTo(InputState& other) const; |
| 705 | |
| 706 | // Gets the fallback key associated with a keycode. |
| 707 | // Returns -1 if none. |
| 708 | // Returns AKEYCODE_UNKNOWN if we are only dispatching the unhandled key to the policy. |
| 709 | int32_t getFallbackKey(int32_t originalKeyCode); |
| 710 | |
| 711 | // Sets the fallback key for a particular keycode. |
| 712 | void setFallbackKey(int32_t originalKeyCode, int32_t fallbackKeyCode); |
| 713 | |
| 714 | // Removes the fallback key for a particular keycode. |
| 715 | void removeFallbackKey(int32_t originalKeyCode); |
| 716 | |
| 717 | inline const KeyedVector<int32_t, int32_t>& getFallbackKeys() const { |
| 718 | return mFallbackKeys; |
| 719 | } |
| 720 | |
| 721 | private: |
| 722 | struct KeyMemento { |
| 723 | int32_t deviceId; |
| 724 | uint32_t source; |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 725 | int32_t displayId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 726 | int32_t keyCode; |
| 727 | int32_t scanCode; |
| 728 | int32_t metaState; |
| 729 | int32_t flags; |
| 730 | nsecs_t downTime; |
| 731 | uint32_t policyFlags; |
| 732 | }; |
| 733 | |
| 734 | struct MotionMemento { |
| 735 | int32_t deviceId; |
| 736 | uint32_t source; |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 737 | int32_t displayId; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 738 | int32_t flags; |
| 739 | float xPrecision; |
| 740 | float yPrecision; |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 741 | float xCursorPosition; |
| 742 | float yCursorPosition; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 743 | nsecs_t downTime; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 744 | uint32_t pointerCount; |
| 745 | PointerProperties pointerProperties[MAX_POINTERS]; |
| 746 | PointerCoords pointerCoords[MAX_POINTERS]; |
| 747 | bool hovering; |
| 748 | uint32_t policyFlags; |
| 749 | |
| 750 | void setPointers(const MotionEntry* entry); |
| 751 | }; |
| 752 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 753 | std::vector<KeyMemento> mKeyMementos; |
| 754 | std::vector<MotionMemento> mMotionMementos; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 755 | KeyedVector<int32_t, int32_t> mFallbackKeys; |
| 756 | |
| 757 | ssize_t findKeyMemento(const KeyEntry* entry) const; |
| 758 | ssize_t findMotionMemento(const MotionEntry* entry, bool hovering) const; |
| 759 | |
| 760 | void addKeyMemento(const KeyEntry* entry, int32_t flags); |
| 761 | void addMotionMemento(const MotionEntry* entry, int32_t flags, bool hovering); |
| 762 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 763 | static bool shouldCancelKey(const KeyMemento& memento, const CancelationOptions& options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 764 | static bool shouldCancelMotion(const MotionMemento& memento, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 765 | const CancelationOptions& options); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 766 | }; |
| 767 | |
| 768 | /* Manages the dispatch state associated with a single input channel. */ |
| 769 | class Connection : public RefBase { |
| 770 | protected: |
| 771 | virtual ~Connection(); |
| 772 | |
| 773 | public: |
| 774 | enum Status { |
| 775 | // Everything is peachy. |
| 776 | STATUS_NORMAL, |
| 777 | // An unrecoverable communication error has occurred. |
| 778 | STATUS_BROKEN, |
| 779 | // The input channel has been unregistered. |
| 780 | STATUS_ZOMBIE |
| 781 | }; |
| 782 | |
| 783 | Status status; |
| 784 | sp<InputChannel> inputChannel; // never null |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 785 | bool monitor; |
| 786 | InputPublisher inputPublisher; |
| 787 | InputState inputState; |
| 788 | |
| 789 | // True if the socket is full and no further events can be published until |
| 790 | // the application consumes some of the input. |
| 791 | bool inputPublisherBlocked; |
| 792 | |
| 793 | // Queue of events that need to be published to the connection. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 794 | std::deque<DispatchEntry*> outboundQueue; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 795 | |
| 796 | // Queue of events that have been published to the connection but that have not |
| 797 | // yet received a "finished" response from the application. |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 798 | std::deque<DispatchEntry*> waitQueue; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 799 | |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 800 | explicit Connection(const sp<InputChannel>& inputChannel, bool monitor); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 801 | |
Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 802 | inline const std::string getInputChannelName() const { return inputChannel->getName(); } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 803 | |
Siarhei Vishniakou | 587c3f0 | 2018-01-04 11:46:44 -0800 | [diff] [blame] | 804 | const std::string getWindowName() const; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 805 | const char* getStatusLabel() const; |
| 806 | |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 807 | std::deque<DispatchEntry*>::iterator findWaitQueueEntry(uint32_t seq); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 808 | }; |
| 809 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 810 | struct Monitor { |
| 811 | sp<InputChannel> inputChannel; // never null |
| 812 | |
| 813 | explicit Monitor(const sp<InputChannel>& inputChannel); |
| 814 | }; |
| 815 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 816 | enum DropReason { |
| 817 | DROP_REASON_NOT_DROPPED = 0, |
| 818 | DROP_REASON_POLICY = 1, |
| 819 | DROP_REASON_APP_SWITCH = 2, |
| 820 | DROP_REASON_DISABLED = 3, |
| 821 | DROP_REASON_BLOCKED = 4, |
| 822 | DROP_REASON_STALE = 5, |
| 823 | }; |
| 824 | |
| 825 | sp<InputDispatcherPolicyInterface> mPolicy; |
| 826 | InputDispatcherConfiguration mConfig; |
| 827 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 828 | std::mutex mLock; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 829 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 830 | std::condition_variable mDispatcherIsAlive; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 831 | |
| 832 | sp<Looper> mLooper; |
| 833 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 834 | EventEntry* mPendingEvent GUARDED_BY(mLock); |
Siarhei Vishniakou | 44a2aed | 2019-07-29 08:59:52 -0700 | [diff] [blame] | 835 | std::deque<EventEntry*> mInboundQueue GUARDED_BY(mLock); |
| 836 | std::deque<EventEntry*> mRecentQueue GUARDED_BY(mLock); |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 837 | std::deque<std::unique_ptr<CommandEntry>> mCommandQueue GUARDED_BY(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 838 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 839 | DropReason mLastDropReason GUARDED_BY(mLock); |
Michael Wright | 3a98172 | 2015-06-10 15:26:13 +0100 | [diff] [blame] | 840 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 841 | void dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 842 | |
| 843 | // Enqueues an inbound event. Returns true if mLooper->wake() should be called. |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 844 | bool enqueueInboundEventLocked(EventEntry* entry) REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 845 | |
| 846 | // Cleans up input state when dropping an inbound event. |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 847 | void dropInboundEventLocked(EventEntry* entry, DropReason dropReason) REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 848 | |
| 849 | // Adds an event to a queue of recent events for debugging purposes. |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 850 | void addRecentEventLocked(EventEntry* entry) REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 851 | |
| 852 | // App switch latency optimization. |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 853 | bool mAppSwitchSawKeyDown GUARDED_BY(mLock); |
| 854 | nsecs_t mAppSwitchDueTime GUARDED_BY(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 855 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 856 | bool isAppSwitchKeyEvent(KeyEntry* keyEntry); |
| 857 | bool isAppSwitchPendingLocked() REQUIRES(mLock); |
| 858 | void resetPendingAppSwitchLocked(bool handled) REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 859 | |
| 860 | // Stale event latency optimization. |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 861 | static bool isStaleEvent(nsecs_t currentTime, EventEntry* entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 862 | |
| 863 | // Blocked event latency optimization. Drops old events when the user intends |
| 864 | // to transfer focus to a new application. |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 865 | EventEntry* mNextUnblockedEvent GUARDED_BY(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 866 | |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 867 | sp<InputWindowHandle> findTouchedWindowAtLocked(int32_t displayId, int32_t x, int32_t y, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 868 | bool addOutsideTargets = false, |
| 869 | bool addPortalWindows = false) REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 870 | |
| 871 | // All registered connections mapped by channel file descriptor. |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 872 | std::unordered_map<int, sp<Connection>> mConnectionsByFd GUARDED_BY(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 873 | |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 874 | struct IBinderHash { |
| 875 | std::size_t operator()(const sp<IBinder>& b) const { |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 876 | return std::hash<IBinder*>{}(b.get()); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 877 | } |
| 878 | }; |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 879 | std::unordered_map<sp<IBinder>, sp<InputChannel>, IBinderHash> mInputChannelsByToken |
| 880 | GUARDED_BY(mLock); |
Robert Carr | 5c8a026 | 2018-10-03 16:30:44 -0700 | [diff] [blame] | 881 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 882 | // Finds the display ID of the gesture monitor identified by the provided token. |
| 883 | std::optional<int32_t> findGestureMonitorDisplayByTokenLocked(const sp<IBinder>& token) |
| 884 | REQUIRES(mLock); |
| 885 | |
Siarhei Vishniakou | 146ecfd | 2019-07-29 16:04:31 -0700 | [diff] [blame] | 886 | sp<Connection> getConnectionLocked(const sp<InputChannel>& inputChannel) REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 887 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 888 | // Input channels that will receive a copy of all input events sent to the provided display. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 889 | std::unordered_map<int32_t, std::vector<Monitor>> mGlobalMonitorsByDisplay GUARDED_BY(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 890 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 891 | // Input channels that will receive pointer events that start within the corresponding display. |
| 892 | // These are a bit special when compared to global monitors since they'll cause gesture streams |
| 893 | // to continue even when there isn't a touched window,and have the ability to steal the rest of |
| 894 | // the pointer stream in order to claim it for a system gesture. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 895 | std::unordered_map<int32_t, std::vector<Monitor>> mGestureMonitorsByDisplay GUARDED_BY(mLock); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 896 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 897 | // Event injection and synchronization. |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 898 | std::condition_variable mInjectionResultAvailable; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 899 | bool hasInjectionPermission(int32_t injectorPid, int32_t injectorUid); |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 900 | void setInjectionResult(EventEntry* entry, int32_t injectionResult); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 901 | |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 902 | std::condition_variable mInjectionSyncFinished; |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 903 | void incrementPendingForegroundDispatches(EventEntry* entry); |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 904 | void decrementPendingForegroundDispatches(EventEntry* entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 905 | |
| 906 | // Key repeat tracking. |
| 907 | struct KeyRepeatState { |
| 908 | KeyEntry* lastKeyEntry; // or null if no repeat |
| 909 | nsecs_t nextRepeatTime; |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 910 | } mKeyRepeatState GUARDED_BY(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 911 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 912 | void resetKeyRepeatLocked() REQUIRES(mLock); |
| 913 | KeyEntry* synthesizeKeyRepeatLocked(nsecs_t currentTime) REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 914 | |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 915 | // Key replacement tracking |
| 916 | struct KeyReplacement { |
| 917 | int32_t keyCode; |
| 918 | int32_t deviceId; |
| 919 | bool operator==(const KeyReplacement& rhs) const { |
| 920 | return keyCode == rhs.keyCode && deviceId == rhs.deviceId; |
| 921 | } |
| 922 | bool operator<(const KeyReplacement& rhs) const { |
| 923 | return keyCode != rhs.keyCode ? keyCode < rhs.keyCode : deviceId < rhs.deviceId; |
| 924 | } |
| 925 | }; |
| 926 | // Maps the key code replaced, device id tuple to the key code it was replaced with |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 927 | KeyedVector<KeyReplacement, int32_t> mReplacedKeys GUARDED_BY(mLock); |
Siarhei Vishniakou | 61fafdd | 2018-04-13 11:00:58 -0500 | [diff] [blame] | 928 | // Process certain Meta + Key combinations |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 929 | void accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, int32_t& keyCode, |
| 930 | int32_t& metaState); |
Michael Wright | 78f2444 | 2014-08-06 15:55:28 -0700 | [diff] [blame] | 931 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 932 | // Deferred command processing. |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 933 | bool haveCommandsLocked() const REQUIRES(mLock); |
| 934 | bool runCommandsLockedInterruptible() REQUIRES(mLock); |
Siarhei Vishniakou | e7c94b9 | 2019-07-29 09:17:54 -0700 | [diff] [blame] | 935 | void postCommandLocked(std::unique_ptr<CommandEntry> commandEntry) REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 936 | |
| 937 | // Input filter processing. |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 938 | bool shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) REQUIRES(mLock); |
| 939 | bool shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 940 | |
| 941 | // Inbound event processing. |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 942 | void drainInboundQueueLocked() REQUIRES(mLock); |
| 943 | void releasePendingEventLocked() REQUIRES(mLock); |
| 944 | void releaseInboundEventLocked(EventEntry* entry) REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 945 | |
| 946 | // Dispatch state. |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 947 | bool mDispatchEnabled GUARDED_BY(mLock); |
| 948 | bool mDispatchFrozen GUARDED_BY(mLock); |
| 949 | bool mInputFilterEnabled GUARDED_BY(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 950 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 951 | std::unordered_map<int32_t, std::vector<sp<InputWindowHandle>>> mWindowHandlesByDisplay |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 952 | GUARDED_BY(mLock); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 953 | // Get window handles by display, return an empty vector if not found. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 954 | std::vector<sp<InputWindowHandle>> getWindowHandlesLocked(int32_t displayId) const |
| 955 | REQUIRES(mLock); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 956 | sp<InputWindowHandle> getWindowHandleLocked(const sp<IBinder>& windowHandleToken) const |
| 957 | REQUIRES(mLock); |
| 958 | sp<InputChannel> getInputChannelLocked(const sp<IBinder>& windowToken) const REQUIRES(mLock); |
| 959 | bool hasWindowHandleLocked(const sp<InputWindowHandle>& windowHandle) const REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 960 | |
Siarhei Vishniakou | b3ad35c | 2019-04-05 10:50:52 -0700 | [diff] [blame] | 961 | /* |
| 962 | * Validate and update InputWindowHandles for a given display. |
| 963 | */ |
| 964 | void updateWindowHandlesForDisplayLocked( |
| 965 | const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId) |
| 966 | REQUIRES(mLock); |
| 967 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 968 | // Focus tracking for keys, trackball, etc. |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 969 | std::unordered_map<int32_t, sp<InputWindowHandle>> mFocusedWindowHandlesByDisplay |
| 970 | GUARDED_BY(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 971 | |
| 972 | // Focus tracking for touch. |
| 973 | struct TouchedWindow { |
| 974 | sp<InputWindowHandle> windowHandle; |
| 975 | int32_t targetFlags; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 976 | BitSet32 pointerIds; // zero unless target flag FLAG_SPLIT is set |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 977 | }; |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 978 | |
| 979 | // For tracking the offsets we need to apply when adding gesture monitor targets. |
| 980 | struct TouchedMonitor { |
| 981 | Monitor monitor; |
| 982 | float xOffset = 0.f; |
| 983 | float yOffset = 0.f; |
| 984 | |
| 985 | explicit TouchedMonitor(const Monitor& monitor, float xOffset, float yOffset); |
| 986 | }; |
| 987 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 988 | struct TouchState { |
| 989 | bool down; |
| 990 | bool split; |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 991 | int32_t deviceId; // id of the device that is currently down, others are rejected |
| 992 | uint32_t source; // source of the device that is current down, others are rejected |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 993 | int32_t displayId; // id to the display that currently has a touch, others are rejected |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 994 | std::vector<TouchedWindow> windows; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 995 | |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 996 | // This collects the portal windows that the touch has gone through. Each portal window |
| 997 | // targets a display (embedded display for most cases). With this info, we can add the |
| 998 | // monitoring channels of the displays touched. |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 999 | std::vector<sp<InputWindowHandle>> portalWindows; |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1000 | |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1001 | std::vector<TouchedMonitor> gestureMonitors; |
| 1002 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1003 | TouchState(); |
| 1004 | ~TouchState(); |
| 1005 | void reset(); |
| 1006 | void copyFrom(const TouchState& other); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 1007 | void addOrUpdateWindow(const sp<InputWindowHandle>& windowHandle, int32_t targetFlags, |
| 1008 | BitSet32 pointerIds); |
Tiger Huang | 85b8c5e | 2019-01-17 18:34:54 +0800 | [diff] [blame] | 1009 | void addPortalWindow(const sp<InputWindowHandle>& windowHandle); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1010 | void addGestureMonitors(const std::vector<TouchedMonitor>& monitors); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1011 | void removeWindow(const sp<InputWindowHandle>& windowHandle); |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 1012 | void removeWindowByToken(const sp<IBinder>& token); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1013 | void filterNonAsIsTouchWindows(); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1014 | void filterNonMonitors(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1015 | sp<InputWindowHandle> getFirstForegroundWindowHandle() const; |
| 1016 | bool isSlippery() const; |
| 1017 | }; |
| 1018 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1019 | KeyedVector<int32_t, TouchState> mTouchStatesByDisplay GUARDED_BY(mLock); |
| 1020 | TouchState mTempTouchState GUARDED_BY(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1021 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1022 | // Focused applications. |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1023 | std::unordered_map<int32_t, sp<InputApplicationHandle>> mFocusedApplicationHandlesByDisplay |
| 1024 | GUARDED_BY(mLock); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1025 | |
| 1026 | // Top focused display. |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1027 | int32_t mFocusedDisplayId GUARDED_BY(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1028 | |
| 1029 | // Dispatcher state at time of last ANR. |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1030 | std::string mLastANRState GUARDED_BY(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1031 | |
| 1032 | // Dispatch inbound events. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 1033 | bool dispatchConfigurationChangedLocked(nsecs_t currentTime, ConfigurationChangedEntry* entry) |
| 1034 | REQUIRES(mLock); |
| 1035 | bool dispatchDeviceResetLocked(nsecs_t currentTime, DeviceResetEntry* entry) REQUIRES(mLock); |
| 1036 | bool dispatchKeyLocked(nsecs_t currentTime, KeyEntry* entry, DropReason* dropReason, |
| 1037 | nsecs_t* nextWakeupTime) REQUIRES(mLock); |
| 1038 | bool dispatchMotionLocked(nsecs_t currentTime, MotionEntry* entry, DropReason* dropReason, |
| 1039 | nsecs_t* nextWakeupTime) REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1040 | void dispatchEventLocked(nsecs_t currentTime, EventEntry* entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 1041 | const std::vector<InputTarget>& inputTargets) REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1042 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1043 | void logOutboundKeyDetails(const char* prefix, const KeyEntry* entry); |
| 1044 | void logOutboundMotionDetails(const char* prefix, const MotionEntry* entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1045 | |
| 1046 | // Keeping track of ANR timeouts. |
| 1047 | enum InputTargetWaitCause { |
| 1048 | INPUT_TARGET_WAIT_CAUSE_NONE, |
| 1049 | INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY, |
| 1050 | INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY, |
| 1051 | }; |
| 1052 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1053 | InputTargetWaitCause mInputTargetWaitCause GUARDED_BY(mLock); |
| 1054 | nsecs_t mInputTargetWaitStartTime GUARDED_BY(mLock); |
| 1055 | nsecs_t mInputTargetWaitTimeoutTime GUARDED_BY(mLock); |
| 1056 | bool mInputTargetWaitTimeoutExpired GUARDED_BY(mLock); |
| 1057 | sp<IBinder> mInputTargetWaitApplicationToken GUARDED_BY(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1058 | |
| 1059 | // Contains the last window which received a hover event. |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1060 | sp<InputWindowHandle> mLastHoverWindowHandle GUARDED_BY(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1061 | |
| 1062 | // Finding targets for input events. |
| 1063 | int32_t handleTargetsNotReadyLocked(nsecs_t currentTime, const EventEntry* entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 1064 | const sp<InputApplicationHandle>& applicationHandle, |
| 1065 | const sp<InputWindowHandle>& windowHandle, |
| 1066 | nsecs_t* nextWakeupTime, const char* reason) |
| 1067 | REQUIRES(mLock); |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 1068 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1069 | void removeWindowByTokenLocked(const sp<IBinder>& token) REQUIRES(mLock); |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 1070 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1071 | void resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 1072 | const sp<InputChannel>& inputChannel) |
| 1073 | REQUIRES(mLock); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1074 | nsecs_t getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime) REQUIRES(mLock); |
| 1075 | void resetANRTimeoutsLocked() REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1076 | |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 1077 | int32_t getTargetDisplayId(const EventEntry* entry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1078 | int32_t findFocusedWindowTargetsLocked(nsecs_t currentTime, const EventEntry* entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 1079 | std::vector<InputTarget>& inputTargets, |
| 1080 | nsecs_t* nextWakeupTime) REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1081 | int32_t findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry* entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 1082 | std::vector<InputTarget>& inputTargets, |
| 1083 | nsecs_t* nextWakeupTime, |
| 1084 | bool* outConflictingPointerActions) REQUIRES(mLock); |
| 1085 | std::vector<TouchedMonitor> findTouchedGestureMonitorsLocked( |
| 1086 | int32_t displayId, const std::vector<sp<InputWindowHandle>>& portalWindows) |
| 1087 | REQUIRES(mLock); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1088 | void addGestureMonitors(const std::vector<Monitor>& monitors, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 1089 | std::vector<TouchedMonitor>& outTouchedMonitors, float xOffset = 0, |
| 1090 | float yOffset = 0); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1091 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 1092 | void addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle, int32_t targetFlags, |
| 1093 | BitSet32 pointerIds, std::vector<InputTarget>& inputTargets) |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1094 | REQUIRES(mLock); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1095 | void addMonitoringTargetLocked(const Monitor& monitor, float xOffset, float yOffset, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 1096 | std::vector<InputTarget>& inputTargets) REQUIRES(mLock); |
| 1097 | void addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, int32_t displayId, |
| 1098 | float xOffset = 0, float yOffset = 0) REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1099 | |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1100 | void pokeUserActivityLocked(const EventEntry* eventEntry) REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1101 | bool checkInjectionPermission(const sp<InputWindowHandle>& windowHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 1102 | const InjectionState* injectionState); |
| 1103 | bool isWindowObscuredAtPointLocked(const sp<InputWindowHandle>& windowHandle, int32_t x, |
| 1104 | int32_t y) const REQUIRES(mLock); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1105 | bool isWindowObscuredLocked(const sp<InputWindowHandle>& windowHandle) const REQUIRES(mLock); |
| 1106 | std::string getApplicationWindowLabel(const sp<InputApplicationHandle>& applicationHandle, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 1107 | const sp<InputWindowHandle>& windowHandle); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1108 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 1109 | std::string checkWindowReadyForMoreInputLocked(nsecs_t currentTime, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 1110 | const sp<InputWindowHandle>& windowHandle, |
| 1111 | const EventEntry* eventEntry, |
| 1112 | const char* targetType) REQUIRES(mLock); |
Jeff Brown | ffb4977 | 2014-10-10 19:01:34 -0700 | [diff] [blame] | 1113 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1114 | // Manage the dispatch cycle for a single connection. |
| 1115 | // These methods are deliberately not Interruptible because doing all of the work |
| 1116 | // with the mutex held makes it easier to ensure that connection invariants are maintained. |
| 1117 | // If needed, the methods post commands to run later once the critical bits are done. |
| 1118 | void prepareDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 1119 | EventEntry* eventEntry, const InputTarget* inputTarget) |
| 1120 | REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1121 | void enqueueDispatchEntriesLocked(nsecs_t currentTime, const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 1122 | EventEntry* eventEntry, const InputTarget* inputTarget) |
| 1123 | REQUIRES(mLock); |
| 1124 | void enqueueDispatchEntryLocked(const sp<Connection>& connection, EventEntry* eventEntry, |
| 1125 | const InputTarget* inputTarget, int32_t dispatchMode) |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 1126 | REQUIRES(mLock); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1127 | void startDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection) |
| 1128 | REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1129 | void finishDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 1130 | uint32_t seq, bool handled) REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1131 | void abortBrokenDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 1132 | bool notify) REQUIRES(mLock); |
Siarhei Vishniakou | 13bda6c | 2019-07-29 08:34:33 -0700 | [diff] [blame] | 1133 | void drainDispatchQueue(std::deque<DispatchEntry*>& queue); |
Siarhei Vishniakou | 62683e8 | 2019-03-06 17:59:56 -0800 | [diff] [blame] | 1134 | void releaseDispatchEntry(DispatchEntry* dispatchEntry); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1135 | static int handleReceiveCallback(int fd, int events, void* data); |
chaviw | 8c9cf54 | 2019-03-25 13:02:48 -0700 | [diff] [blame] | 1136 | // The action sent should only be of type AMOTION_EVENT_* |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 1137 | void dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 1138 | const sp<IBinder>& newToken) REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1139 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 1140 | void synthesizeCancelationEventsForAllConnectionsLocked(const CancelationOptions& options) |
| 1141 | REQUIRES(mLock); |
| 1142 | void synthesizeCancelationEventsForMonitorsLocked(const CancelationOptions& options) |
| 1143 | REQUIRES(mLock); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1144 | void synthesizeCancelationEventsForMonitorsLocked( |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 1145 | const CancelationOptions& options, |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1146 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1147 | void synthesizeCancelationEventsForInputChannelLocked(const sp<InputChannel>& channel, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 1148 | const CancelationOptions& options) |
| 1149 | REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1150 | void synthesizeCancelationEventsForConnectionLocked(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 1151 | const CancelationOptions& options) |
| 1152 | REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1153 | |
| 1154 | // Splitting motion events across windows. |
| 1155 | MotionEntry* splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds); |
| 1156 | |
| 1157 | // Reset and drop everything the dispatcher is doing. |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1158 | void resetAndDropEverythingLocked(const char* reason) REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1159 | |
| 1160 | // Dump state. |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1161 | void dumpDispatchStateLocked(std::string& dump) REQUIRES(mLock); |
Michael Wright | 3dd60e2 | 2019-03-27 22:06:44 +0000 | [diff] [blame] | 1162 | void dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1163 | void logDispatchStateLocked() REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1164 | |
| 1165 | // Registration. |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1166 | void removeMonitorChannelLocked(const sp<InputChannel>& inputChannel) REQUIRES(mLock); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 1167 | void removeMonitorChannelLocked( |
| 1168 | const sp<InputChannel>& inputChannel, |
| 1169 | std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) REQUIRES(mLock); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1170 | status_t unregisterInputChannelLocked(const sp<InputChannel>& inputChannel, bool notify) |
| 1171 | REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1172 | |
| 1173 | // Interesting events that we might like to log or tell the framework about. |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 1174 | void onDispatchCycleFinishedLocked(nsecs_t currentTime, const sp<Connection>& connection, |
| 1175 | uint32_t seq, bool handled) REQUIRES(mLock); |
| 1176 | void onDispatchCycleBrokenLocked(nsecs_t currentTime, const sp<Connection>& connection) |
| 1177 | REQUIRES(mLock); |
chaviw | 0c06c6e | 2019-01-09 13:27:07 -0800 | [diff] [blame] | 1178 | void onFocusChangedLocked(const sp<InputWindowHandle>& oldFocus, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 1179 | const sp<InputWindowHandle>& newFocus) REQUIRES(mLock); |
| 1180 | void onANRLocked(nsecs_t currentTime, const sp<InputApplicationHandle>& applicationHandle, |
| 1181 | const sp<InputWindowHandle>& windowHandle, nsecs_t eventTime, |
| 1182 | nsecs_t waitStartTime, const char* reason) REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1183 | |
| 1184 | // Outbound policy interactions. |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1185 | void doNotifyConfigurationChangedLockedInterruptible(CommandEntry* commandEntry) |
| 1186 | REQUIRES(mLock); |
| 1187 | void doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock); |
| 1188 | void doNotifyFocusChangedLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock); |
| 1189 | void doNotifyANRLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock); |
| 1190 | void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry) |
| 1191 | REQUIRES(mLock); |
| 1192 | void doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1193 | bool afterKeyEventLockedInterruptible(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 1194 | DispatchEntry* dispatchEntry, KeyEntry* keyEntry, |
| 1195 | bool handled) REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1196 | bool afterMotionEventLockedInterruptible(const sp<Connection>& connection, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 1197 | DispatchEntry* dispatchEntry, MotionEntry* motionEntry, |
| 1198 | bool handled) REQUIRES(mLock); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1199 | void doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1200 | void initializeKeyEvent(KeyEvent* event, const KeyEntry* entry); |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 1201 | void doOnPointerDownOutsideFocusLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1202 | |
| 1203 | // Statistics gathering. |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1204 | void updateDispatchStatistics(nsecs_t currentTime, const EventEntry* entry, |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame^] | 1205 | int32_t injectionResult, nsecs_t timeSpentWaitingForApplication); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 1206 | void traceInboundQueueLengthLocked() REQUIRES(mLock); |
| 1207 | void traceOutboundQueueLength(const sp<Connection>& connection); |
| 1208 | void traceWaitQueueLength(const sp<Connection>& connection); |
Prabir Pradhan | f93562f | 2018-11-29 12:13:37 -0800 | [diff] [blame] | 1209 | |
Prabir Pradhan | 79a4f0c | 2019-01-09 11:24:01 -0800 | [diff] [blame] | 1210 | sp<InputReporterInterface> mReporter; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1211 | }; |
| 1212 | |
| 1213 | /* Enqueues and dispatches input events, endlessly. */ |
| 1214 | class InputDispatcherThread : public Thread { |
| 1215 | public: |
| 1216 | explicit InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher); |
| 1217 | ~InputDispatcherThread(); |
| 1218 | |
| 1219 | private: |
| 1220 | virtual bool threadLoop(); |
| 1221 | |
| 1222 | sp<InputDispatcherInterface> mDispatcher; |
| 1223 | }; |
| 1224 | |
| 1225 | } // namespace android |
| 1226 | |
| 1227 | #endif // _UI_INPUT_DISPATCHER_H |