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