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