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