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