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