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