blob: a90f958e79c90e60f4829a5e18104cf4e895075c [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
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 Tan0fc2fa72019-08-29 17:22:15 -070020#include <cutils/atomic.h>
21#include <input/ISetInputWindowsListener.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080022#include <input/Input.h>
Robert Carr3720ed02018-08-08 16:08:27 -070023#include <input/InputApplication.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080024#include <input/InputTransport.h>
Robert Carr3720ed02018-08-08 16:08:27 -070025#include <input/InputWindow.h>
Michael Wright3dd60e22019-03-27 22:06:44 +000026#include <ui/Region.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080027#include <utils/BitSet.h>
Garfield Tan0fc2fa72019-08-29 17:22:15 -070028#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 Carr5c8a0262018-10-03 16:30:44 -070034#include <unordered_map>
Michael Wrightd02c5b62014-02-10 15:10:22 -080035
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -070036#include <limits.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080037#include <stddef.h>
38#include <unistd.h>
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -070039#include <deque>
Arthur Hungb92218b2018-08-14 12:00:21 +080040#include <unordered_map>
Michael Wrightd02c5b62014-02-10 15:10:22 -080041
Michael Wrightd02c5b62014-02-10 15:10:22 -080042#include "InputListener.h"
Prabir Pradhan79a4f0c2019-01-09 11:24:01 -080043#include "InputReporterInterface.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080044
Michael Wrightd02c5b62014-02-10 15:10:22 -080045namespace android {
46
47/*
48 * Constants used to report the outcome of input event injection.
49 */
50enum {
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 */
71enum {
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
Michael Wrightd02c5b62014-02-10 15:10:22 -080084/*
85 * An input target specifies how an input event is to be dispatched to a particular window
86 * including the window's input channel, control flags, a timeout, and an X / Y offset to
87 * be added to input event coordinates to compensate for the absolute position of the
88 * window area.
89 */
90struct InputTarget {
91 enum {
92 /* This flag indicates that the event is being delivered to a foreground application. */
93 FLAG_FOREGROUND = 1 << 0,
94
Michael Wrightcdcd8f22016-03-22 16:52:13 -070095 /* This flag indicates that the MotionEvent falls within the area of the target
Michael Wrightd02c5b62014-02-10 15:10:22 -080096 * obscured by another visible window above it. The motion event should be
97 * delivered with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED. */
98 FLAG_WINDOW_IS_OBSCURED = 1 << 1,
99
100 /* This flag indicates that a motion event is being split across multiple windows. */
101 FLAG_SPLIT = 1 << 2,
102
103 /* This flag indicates that the pointer coordinates dispatched to the application
104 * will be zeroed out to avoid revealing information to an application. This is
105 * used in conjunction with FLAG_DISPATCH_AS_OUTSIDE to prevent apps not sharing
106 * the same UID from watching all touches. */
107 FLAG_ZERO_COORDS = 1 << 3,
108
109 /* This flag indicates that the event should be sent as is.
110 * Should always be set unless the event is to be transmuted. */
111 FLAG_DISPATCH_AS_IS = 1 << 8,
112
113 /* This flag indicates that a MotionEvent with AMOTION_EVENT_ACTION_DOWN falls outside
114 * of the area of this target and so should instead be delivered as an
115 * AMOTION_EVENT_ACTION_OUTSIDE to this target. */
116 FLAG_DISPATCH_AS_OUTSIDE = 1 << 9,
117
118 /* This flag indicates that a hover sequence is starting in the given window.
119 * The event is transmuted into ACTION_HOVER_ENTER. */
120 FLAG_DISPATCH_AS_HOVER_ENTER = 1 << 10,
121
122 /* This flag indicates that a hover event happened outside of a window which handled
123 * previous hover events, signifying the end of the current hover sequence for that
124 * window.
125 * The event is transmuted into ACTION_HOVER_ENTER. */
126 FLAG_DISPATCH_AS_HOVER_EXIT = 1 << 11,
127
128 /* This flag indicates that the event should be canceled.
129 * It is used to transmute ACTION_MOVE into ACTION_CANCEL when a touch slips
130 * outside of a window. */
131 FLAG_DISPATCH_AS_SLIPPERY_EXIT = 1 << 12,
132
133 /* This flag indicates that the event should be dispatched as an initial down.
134 * It is used to transmute ACTION_MOVE into ACTION_DOWN when a touch slips
135 * into a new window. */
136 FLAG_DISPATCH_AS_SLIPPERY_ENTER = 1 << 13,
137
138 /* Mask for all dispatch modes. */
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700139 FLAG_DISPATCH_MASK = FLAG_DISPATCH_AS_IS | FLAG_DISPATCH_AS_OUTSIDE |
140 FLAG_DISPATCH_AS_HOVER_ENTER | FLAG_DISPATCH_AS_HOVER_EXIT |
141 FLAG_DISPATCH_AS_SLIPPERY_EXIT | FLAG_DISPATCH_AS_SLIPPERY_ENTER,
Michael Wrightcdcd8f22016-03-22 16:52:13 -0700142
143 /* This flag indicates that the target of a MotionEvent is partly or wholly
144 * obscured by another visible window above it. The motion event should be
145 * delivered with flag AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED. */
146 FLAG_WINDOW_IS_PARTIALLY_OBSCURED = 1 << 14,
147
Michael Wrightd02c5b62014-02-10 15:10:22 -0800148 };
149
150 // The input channel to be targeted.
151 sp<InputChannel> inputChannel;
152
153 // Flags for the input target.
154 int32_t flags;
155
156 // The x and y offset to add to a MotionEvent as it is delivered.
157 // (ignored for KeyEvents)
158 float xOffset, yOffset;
159
160 // Scaling factor to apply to MotionEvent as it is delivered.
161 // (ignored for KeyEvents)
Robert Carre07e1032018-11-26 12:55:53 -0800162 float globalScaleFactor;
163 float windowXScale = 1.0f;
164 float windowYScale = 1.0f;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800165
166 // The subset of pointer ids to include in motion events dispatched to this input target
167 // if FLAG_SPLIT is set.
168 BitSet32 pointerIds;
169};
170
Michael Wrightd02c5b62014-02-10 15:10:22 -0800171/*
172 * Input dispatcher configuration.
173 *
174 * Specifies various options that modify the behavior of the input dispatcher.
175 * The values provided here are merely defaults. The actual values will come from ViewConfiguration
176 * and are passed into the dispatcher during initialization.
177 */
178struct InputDispatcherConfiguration {
179 // The key repeat initial timeout.
180 nsecs_t keyRepeatTimeout;
181
182 // The key repeat inter-key delay.
183 nsecs_t keyRepeatDelay;
184
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700185 InputDispatcherConfiguration()
186 : keyRepeatTimeout(500 * 1000000LL), keyRepeatDelay(50 * 1000000LL) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800187};
188
Michael Wrightd02c5b62014-02-10 15:10:22 -0800189/*
190 * Input dispatcher policy interface.
191 *
192 * The input reader policy is used by the input reader to interact with the Window Manager
193 * and other system components.
194 *
195 * The actual implementation is partially supported by callbacks into the DVM
196 * via JNI. This interface is also mocked in the unit tests.
197 */
198class InputDispatcherPolicyInterface : public virtual RefBase {
199protected:
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700200 InputDispatcherPolicyInterface() {}
201 virtual ~InputDispatcherPolicyInterface() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800202
203public:
204 /* Notifies the system that a configuration change has occurred. */
205 virtual void notifyConfigurationChanged(nsecs_t when) = 0;
206
207 /* Notifies the system that an application is not responding.
208 * Returns a new timeout to continue waiting, or 0 to abort dispatch. */
209 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700210 const sp<IBinder>& token, const std::string& reason) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800211
212 /* Notifies the system that an input channel is unrecoverably broken. */
Robert Carr803535b2018-08-02 16:38:15 -0700213 virtual void notifyInputChannelBroken(const sp<IBinder>& token) = 0;
chaviw0c06c6e2019-01-09 13:27:07 -0800214 virtual void notifyFocusChanged(const sp<IBinder>& oldToken, const sp<IBinder>& newToken) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800215
216 /* Gets the input dispatcher configuration. */
217 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) = 0;
218
Michael Wrightd02c5b62014-02-10 15:10:22 -0800219 /* Filters an input event.
220 * Return true to dispatch the event unmodified, false to consume the event.
221 * A filter can also transform and inject events later by passing POLICY_FLAG_FILTERED
222 * to injectInputEvent.
223 */
224 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) = 0;
225
226 /* Intercepts a key event immediately before queueing it.
227 * The policy can use this method as an opportunity to perform power management functions
228 * and early event preprocessing such as updating policy flags.
229 *
230 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
231 * should be dispatched to applications.
232 */
233 virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags) = 0;
234
235 /* Intercepts a touch, trackball or other motion event before queueing it.
236 * The policy can use this method as an opportunity to perform power management functions
237 * and early event preprocessing such as updating policy flags.
238 *
239 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
240 * should be dispatched to applications.
241 */
Charles Chen3611f1f2019-01-29 17:26:18 +0800242 virtual void interceptMotionBeforeQueueing(const int32_t displayId, nsecs_t when,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700243 uint32_t& policyFlags) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800244
245 /* Allows the policy a chance to intercept a key before dispatching. */
Robert Carr803535b2018-08-02 16:38:15 -0700246 virtual nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>& token,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700247 const KeyEvent* keyEvent,
248 uint32_t policyFlags) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800249
250 /* Allows the policy a chance to perform default processing for an unhandled key.
251 * Returns an alternate keycode to redispatch as a fallback, or 0 to give up. */
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700252 virtual bool dispatchUnhandledKey(const sp<IBinder>& token, const KeyEvent* keyEvent,
253 uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800254
255 /* Notifies the policy about switch events.
256 */
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700257 virtual void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
258 uint32_t policyFlags) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800259
260 /* Poke user activity for an event dispatched to a window. */
261 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType) = 0;
262
263 /* Checks whether a given application pid/uid has permission to inject input events
264 * into other applications.
265 *
266 * This method is special in that its implementation promises to be non-reentrant and
267 * is safe to call while holding other locks. (Most other methods make no such guarantees!)
268 */
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700269 virtual bool checkInjectEventsPermissionNonReentrant(int32_t injectorPid,
270 int32_t injectorUid) = 0;
chaviwfd6d3512019-03-25 13:23:49 -0700271
272 /* Notifies the policy that a pointer down event has occurred outside the current focused
273 * window.
274 *
275 * The touchedToken passed as an argument is the window that received the input event.
276 */
277 virtual void onPointerDownOutsideFocus(const sp<IBinder>& touchedToken) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800278};
279
Michael Wrightd02c5b62014-02-10 15:10:22 -0800280/* Notifies the system about input events generated by the input reader.
281 * The dispatcher is expected to be mostly asynchronous. */
282class InputDispatcherInterface : public virtual RefBase, public InputListenerInterface {
283protected:
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700284 InputDispatcherInterface() {}
285 virtual ~InputDispatcherInterface() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800286
287public:
288 /* Dumps the state of the input dispatcher.
289 *
290 * This method may be called on any thread (usually by the input manager). */
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800291 virtual void dump(std::string& dump) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800292
293 /* Called by the heatbeat to ensures that the dispatcher has not deadlocked. */
294 virtual void monitor() = 0;
295
296 /* Runs a single iteration of the dispatch loop.
297 * Nominally processes one queued event, a timeout, or a response from an input consumer.
298 *
299 * This method should only be called on the input dispatcher thread.
300 */
301 virtual void dispatchOnce() = 0;
302
303 /* Injects an input event and optionally waits for sync.
304 * The synchronization mode determines whether the method blocks while waiting for
305 * input injection to proceed.
306 * Returns one of the INPUT_EVENT_INJECTION_XXX constants.
307 *
308 * This method may be called on any thread (usually by the input manager).
309 */
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700310 virtual int32_t injectInputEvent(const InputEvent* event, int32_t injectorPid,
311 int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,
312 uint32_t policyFlags) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800313
314 /* Sets the list of input windows.
315 *
316 * This method may be called on any thread (usually by the input manager).
317 */
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700318 virtual void setInputWindows(
319 const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId,
chaviw291d88a2019-02-14 10:33:58 -0800320 const sp<ISetInputWindowsListener>& setInputWindowsListener = nullptr) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800321
Tiger Huang721e26f2018-07-24 22:26:19 +0800322 /* Sets the focused application on the given display.
Michael Wrightd02c5b62014-02-10 15:10:22 -0800323 *
324 * This method may be called on any thread (usually by the input manager).
325 */
326 virtual void setFocusedApplication(
Tiger Huang721e26f2018-07-24 22:26:19 +0800327 int32_t displayId, const sp<InputApplicationHandle>& inputApplicationHandle) = 0;
328
329 /* Sets the focused display.
330 *
331 * This method may be called on any thread (usually by the input manager).
332 */
333 virtual void setFocusedDisplay(int32_t displayId) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800334
335 /* Sets the input dispatching mode.
336 *
337 * This method may be called on any thread (usually by the input manager).
338 */
339 virtual void setInputDispatchMode(bool enabled, bool frozen) = 0;
340
341 /* Sets whether input event filtering is enabled.
342 * When enabled, incoming input events are sent to the policy's filterInputEvent
343 * method instead of being dispatched. The filter is expected to use
344 * injectInputEvent to inject the events it would like to have dispatched.
345 * It should include POLICY_FLAG_FILTERED in the policy flags during injection.
346 */
347 virtual void setInputFilterEnabled(bool enabled) = 0;
348
chaviwfbe5d9c2018-12-26 12:23:37 -0800349 /* Transfers touch focus from one window to another window.
Michael Wrightd02c5b62014-02-10 15:10:22 -0800350 *
351 * Returns true on success. False if the window did not actually have touch focus.
352 */
chaviwfbe5d9c2018-12-26 12:23:37 -0800353 virtual bool transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken) = 0;
354
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800355 /* Registers input channels that may be used as targets for input events.
Michael Wrightd02c5b62014-02-10 15:10:22 -0800356 *
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800357 * This method may be called on any thread (usually by the input manager).
Michael Wrightd02c5b62014-02-10 15:10:22 -0800358 */
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700359 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel,
360 int32_t displayId) = 0;
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800361
Michael Wright3dd60e22019-03-27 22:06:44 +0000362 /* Registers input channels to be used to monitor input events.
363 *
364 * Each monitor must target a specific display and will only receive input events sent to that
365 * display. If the monitor is a gesture monitor, it will only receive pointer events on the
366 * targeted display.
367 *
368 * This method may be called on any thread (usually by the input manager).
369 */
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700370 virtual status_t registerInputMonitor(const sp<InputChannel>& inputChannel, int32_t displayId,
371 bool gestureMonitor) = 0;
Michael Wright3dd60e22019-03-27 22:06:44 +0000372
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800373 /* Unregister input channels that will no longer receive input events.
374 *
375 * This method may be called on any thread (usually by the input manager).
376 */
Michael Wrightd02c5b62014-02-10 15:10:22 -0800377 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) = 0;
Michael Wright3dd60e22019-03-27 22:06:44 +0000378
379 /* Allows an input monitor steal the current pointer stream away from normal input windows.
380 *
381 * This method may be called on any thread (usually by the input manager).
382 */
383 virtual status_t pilferPointers(const sp<IBinder>& token) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800384};
385
386/* Dispatches events to input targets. Some functions of the input dispatcher, such as
387 * identifying input targets, are controlled by a separate policy object.
388 *
389 * IMPORTANT INVARIANT:
390 * Because the policy can potentially block or cause re-entrance into the input dispatcher,
391 * the input dispatcher never calls into the policy while holding its internal locks.
392 * The implementation is also carefully designed to recover from scenarios such as an
393 * input channel becoming unregistered while identifying input targets or processing timeouts.
394 *
395 * Methods marked 'Locked' must be called with the lock acquired.
396 *
397 * Methods marked 'LockedInterruptible' must be called with the lock acquired but
398 * may during the course of their execution release the lock, call into the policy, and
399 * then reacquire the lock. The caller is responsible for recovering gracefully.
400 *
401 * A 'LockedInterruptible' method may called a 'Locked' method, but NOT vice-versa.
402 */
403class InputDispatcher : public InputDispatcherInterface {
404protected:
405 virtual ~InputDispatcher();
406
407public:
408 explicit InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy);
409
Michael Wright3dd60e22019-03-27 22:06:44 +0000410 virtual void dump(std::string& dump) override;
411 virtual void monitor() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800412
Michael Wright3dd60e22019-03-27 22:06:44 +0000413 virtual void dispatchOnce() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800414
Michael Wright3dd60e22019-03-27 22:06:44 +0000415 virtual void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) override;
416 virtual void notifyKey(const NotifyKeyArgs* args) override;
417 virtual void notifyMotion(const NotifyMotionArgs* args) override;
418 virtual void notifySwitch(const NotifySwitchArgs* args) override;
419 virtual void notifyDeviceReset(const NotifyDeviceResetArgs* args) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800420
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700421 virtual int32_t injectInputEvent(const InputEvent* event, int32_t injectorPid,
422 int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,
423 uint32_t policyFlags) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800424
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700425 virtual void setInputWindows(
426 const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId,
Michael Wright3dd60e22019-03-27 22:06:44 +0000427 const sp<ISetInputWindowsListener>& setInputWindowsListener = nullptr) override;
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700428 virtual void setFocusedApplication(
429 int32_t displayId, const sp<InputApplicationHandle>& inputApplicationHandle) override;
Michael Wright3dd60e22019-03-27 22:06:44 +0000430 virtual void setFocusedDisplay(int32_t displayId) override;
431 virtual void setInputDispatchMode(bool enabled, bool frozen) override;
432 virtual void setInputFilterEnabled(bool enabled) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800433
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700434 virtual bool transferTouchFocus(const sp<IBinder>& fromToken,
435 const sp<IBinder>& toToken) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800436
437 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700438 int32_t displayId) override;
439 virtual status_t registerInputMonitor(const sp<InputChannel>& inputChannel, int32_t displayId,
440 bool isGestureMonitor) override;
Michael Wright3dd60e22019-03-27 22:06:44 +0000441 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) override;
442 virtual status_t pilferPointers(const sp<IBinder>& token) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800443
444private:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800445 struct InjectionState {
446 mutable int32_t refCount;
447
448 int32_t injectorPid;
449 int32_t injectorUid;
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700450 int32_t injectionResult; // initially INPUT_EVENT_INJECTION_PENDING
451 bool injectionIsAsync; // set to true if injection is not waiting for the result
Michael Wrightd02c5b62014-02-10 15:10:22 -0800452 int32_t pendingForegroundDispatches; // the number of foreground dispatches in progress
453
454 InjectionState(int32_t injectorPid, int32_t injectorUid);
455 void release();
456
457 private:
458 ~InjectionState();
459 };
460
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -0700461 struct EventEntry {
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700462 enum { TYPE_CONFIGURATION_CHANGED, TYPE_DEVICE_RESET, TYPE_KEY, TYPE_MOTION };
Michael Wrightd02c5b62014-02-10 15:10:22 -0800463
Prabir Pradhan42611e02018-11-27 14:04:02 -0800464 uint32_t sequenceNum;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800465 mutable int32_t refCount;
466 int32_t type;
467 nsecs_t eventTime;
468 uint32_t policyFlags;
469 InjectionState* injectionState;
470
471 bool dispatchInProgress; // initially false, set to true while dispatching
472
Yi Kong9b14ac62018-07-17 13:48:38 -0700473 inline bool isInjected() const { return injectionState != nullptr; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800474
475 void release();
476
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800477 virtual void appendDescription(std::string& msg) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800478
479 protected:
Prabir Pradhan42611e02018-11-27 14:04:02 -0800480 EventEntry(uint32_t sequenceNum, int32_t type, nsecs_t eventTime, uint32_t policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800481 virtual ~EventEntry();
482 void releaseInjectionState();
483 };
484
485 struct ConfigurationChangedEntry : EventEntry {
Prabir Pradhan42611e02018-11-27 14:04:02 -0800486 explicit ConfigurationChangedEntry(uint32_t sequenceNum, nsecs_t eventTime);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800487 virtual void appendDescription(std::string& msg) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800488
489 protected:
490 virtual ~ConfigurationChangedEntry();
491 };
492
493 struct DeviceResetEntry : EventEntry {
494 int32_t deviceId;
495
Prabir Pradhan42611e02018-11-27 14:04:02 -0800496 DeviceResetEntry(uint32_t sequenceNum, nsecs_t eventTime, int32_t deviceId);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800497 virtual void appendDescription(std::string& msg) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800498
499 protected:
500 virtual ~DeviceResetEntry();
501 };
502
503 struct KeyEntry : EventEntry {
504 int32_t deviceId;
505 uint32_t source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100506 int32_t displayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800507 int32_t action;
508 int32_t flags;
509 int32_t keyCode;
510 int32_t scanCode;
511 int32_t metaState;
512 int32_t repeatCount;
513 nsecs_t downTime;
514
515 bool syntheticRepeat; // set to true for synthetic key repeats
516
517 enum InterceptKeyResult {
518 INTERCEPT_KEY_RESULT_UNKNOWN,
519 INTERCEPT_KEY_RESULT_SKIP,
520 INTERCEPT_KEY_RESULT_CONTINUE,
521 INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER,
522 };
523 InterceptKeyResult interceptKeyResult; // set based on the interception result
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700524 nsecs_t interceptKeyWakeupTime; // used with INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER
Michael Wrightd02c5b62014-02-10 15:10:22 -0800525
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700526 KeyEntry(uint32_t sequenceNum, nsecs_t eventTime, int32_t deviceId, uint32_t source,
527 int32_t displayId, uint32_t policyFlags, int32_t action, int32_t flags,
528 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
529 nsecs_t downTime);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800530 virtual void appendDescription(std::string& msg) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800531 void recycle();
532
533 protected:
534 virtual ~KeyEntry();
535 };
536
537 struct MotionEntry : EventEntry {
538 nsecs_t eventTime;
539 int32_t deviceId;
540 uint32_t source;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800541 int32_t displayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800542 int32_t action;
Michael Wright7b159c92015-05-14 14:48:03 +0100543 int32_t actionButton;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800544 int32_t flags;
545 int32_t metaState;
546 int32_t buttonState;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800547 MotionClassification classification;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800548 int32_t edgeFlags;
549 float xPrecision;
550 float yPrecision;
Garfield Tan00f511d2019-06-12 16:55:40 -0700551 float xCursorPosition;
552 float yCursorPosition;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800553 nsecs_t downTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800554 uint32_t pointerCount;
555 PointerProperties pointerProperties[MAX_POINTERS];
556 PointerCoords pointerCoords[MAX_POINTERS];
557
Garfield Tan00f511d2019-06-12 16:55:40 -0700558 MotionEntry(uint32_t sequenceNum, nsecs_t eventTime, int32_t deviceId, uint32_t source,
559 int32_t displayId, uint32_t policyFlags, int32_t action, int32_t actionButton,
560 int32_t flags, int32_t metaState, int32_t buttonState,
561 MotionClassification classification, int32_t edgeFlags, float xPrecision,
562 float yPrecision, float xCursorPosition, float yCursorPosition,
563 nsecs_t downTime, uint32_t pointerCount,
564 const PointerProperties* pointerProperties, const PointerCoords* pointerCoords,
565 float xOffset, float yOffset);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800566 virtual void appendDescription(std::string& msg) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800567
568 protected:
569 virtual ~MotionEntry();
570 };
571
572 // Tracks the progress of dispatching a particular event to a particular connection.
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -0700573 struct DispatchEntry {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800574 const uint32_t seq; // unique sequence number, never 0
575
576 EventEntry* eventEntry; // the event to dispatch
577 int32_t targetFlags;
578 float xOffset;
579 float yOffset;
Robert Carre07e1032018-11-26 12:55:53 -0800580 float globalScaleFactor;
581 float windowXScale = 1.0f;
582 float windowYScale = 1.0f;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800583 nsecs_t deliveryTime; // time when the event was actually delivered
584
585 // Set to the resolved action and flags when the event is enqueued.
586 int32_t resolvedAction;
587 int32_t resolvedFlags;
588
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700589 DispatchEntry(EventEntry* eventEntry, int32_t targetFlags, float xOffset, float yOffset,
590 float globalScaleFactor, float windowXScale, float windowYScale);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800591 ~DispatchEntry();
592
593 inline bool hasForegroundTarget() const {
594 return targetFlags & InputTarget::FLAG_FOREGROUND;
595 }
596
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700597 inline bool isSplit() const { return targetFlags & InputTarget::FLAG_SPLIT; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800598
599 private:
600 static volatile int32_t sNextSeqAtomic;
601
602 static uint32_t nextSeq();
603 };
604
605 // A command entry captures state and behavior for an action to be performed in the
606 // dispatch loop after the initial processing has taken place. It is essentially
607 // a kind of continuation used to postpone sensitive policy interactions to a point
608 // in the dispatch loop where it is safe to release the lock (generally after finishing
609 // the critical parts of the dispatch cycle).
610 //
611 // The special thing about commands is that they can voluntarily release and reacquire
612 // the dispatcher lock at will. Initially when the command starts running, the
613 // dispatcher lock is held. However, if the command needs to call into the policy to
614 // do some work, it can release the lock, do the work, then reacquire the lock again
615 // before returning.
616 //
617 // This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch
618 // never calls into the policy while holding its lock.
619 //
620 // Commands are implicitly 'LockedInterruptible'.
621 struct CommandEntry;
Siarhei Vishniakou49a350a2019-07-26 18:44:23 -0700622 typedef std::function<void(InputDispatcher&, CommandEntry*)> Command;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800623
624 class Connection;
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -0700625 struct CommandEntry {
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700626 explicit CommandEntry(Command command);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800627 ~CommandEntry();
628
629 Command command;
630
631 // parameters for the command (usage varies by command)
632 sp<Connection> connection;
633 nsecs_t eventTime;
634 KeyEntry* keyEntry;
635 sp<InputApplicationHandle> inputApplicationHandle;
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800636 std::string reason;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800637 int32_t userActivityEventType;
638 uint32_t seq;
639 bool handled;
Robert Carr803535b2018-08-02 16:38:15 -0700640 sp<InputChannel> inputChannel;
chaviw0c06c6e2019-01-09 13:27:07 -0800641 sp<IBinder> oldToken;
642 sp<IBinder> newToken;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800643 };
644
Michael Wrightd02c5b62014-02-10 15:10:22 -0800645 /* Specifies which events are to be canceled and why. */
646 struct CancelationOptions {
647 enum Mode {
648 CANCEL_ALL_EVENTS = 0,
649 CANCEL_POINTER_EVENTS = 1,
650 CANCEL_NON_POINTER_EVENTS = 2,
651 CANCEL_FALLBACK_EVENTS = 3,
652 };
653
654 // The criterion to use to determine which events should be canceled.
655 Mode mode;
656
657 // Descriptive reason for the cancelation.
658 const char* reason;
659
Michael Wright3dd60e22019-03-27 22:06:44 +0000660 // The specific keycode of the key event to cancel, or nullopt to cancel any key event.
661 std::optional<int32_t> keyCode = std::nullopt;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800662
Michael Wright3dd60e22019-03-27 22:06:44 +0000663 // The specific device id of events to cancel, or nullopt to cancel events from any device.
664 std::optional<int32_t> deviceId = std::nullopt;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800665
Michael Wright3dd60e22019-03-27 22:06:44 +0000666 // The specific display id of events to cancel, or nullopt to cancel events on any display.
667 std::optional<int32_t> displayId = std::nullopt;
668
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700669 CancelationOptions(Mode mode, const char* reason) : mode(mode), reason(reason) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800670 };
671
672 /* Tracks dispatched key and motion event state so that cancelation events can be
673 * synthesized when events are dropped. */
674 class InputState {
675 public:
676 InputState();
677 ~InputState();
678
679 // Returns true if there is no state to be canceled.
680 bool isNeutral() const;
681
682 // Returns true if the specified source is known to have received a hover enter
683 // motion event.
684 bool isHovering(int32_t deviceId, uint32_t source, int32_t displayId) const;
685
686 // Records tracking information for a key event that has just been published.
687 // Returns true if the event should be delivered, false if it is inconsistent
688 // and should be skipped.
689 bool trackKey(const KeyEntry* entry, int32_t action, int32_t flags);
690
691 // Records tracking information for a motion event that has just been published.
692 // Returns true if the event should be delivered, false if it is inconsistent
693 // and should be skipped.
694 bool trackMotion(const MotionEntry* entry, int32_t action, int32_t flags);
695
696 // Synthesizes cancelation events for the current state and resets the tracked state.
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700697 void synthesizeCancelationEvents(nsecs_t currentTime, std::vector<EventEntry*>& outEvents,
698 const CancelationOptions& options);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800699
700 // Clears the current state.
701 void clear();
702
703 // Copies pointer-related parts of the input state to another instance.
704 void copyPointerStateTo(InputState& other) const;
705
706 // Gets the fallback key associated with a keycode.
707 // Returns -1 if none.
708 // Returns AKEYCODE_UNKNOWN if we are only dispatching the unhandled key to the policy.
709 int32_t getFallbackKey(int32_t originalKeyCode);
710
711 // Sets the fallback key for a particular keycode.
712 void setFallbackKey(int32_t originalKeyCode, int32_t fallbackKeyCode);
713
714 // Removes the fallback key for a particular keycode.
715 void removeFallbackKey(int32_t originalKeyCode);
716
717 inline const KeyedVector<int32_t, int32_t>& getFallbackKeys() const {
718 return mFallbackKeys;
719 }
720
721 private:
722 struct KeyMemento {
723 int32_t deviceId;
724 uint32_t source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100725 int32_t displayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800726 int32_t keyCode;
727 int32_t scanCode;
728 int32_t metaState;
729 int32_t flags;
730 nsecs_t downTime;
731 uint32_t policyFlags;
732 };
733
734 struct MotionMemento {
735 int32_t deviceId;
736 uint32_t source;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800737 int32_t displayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800738 int32_t flags;
739 float xPrecision;
740 float yPrecision;
Garfield Tan00f511d2019-06-12 16:55:40 -0700741 float xCursorPosition;
742 float yCursorPosition;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800743 nsecs_t downTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800744 uint32_t pointerCount;
745 PointerProperties pointerProperties[MAX_POINTERS];
746 PointerCoords pointerCoords[MAX_POINTERS];
747 bool hovering;
748 uint32_t policyFlags;
749
750 void setPointers(const MotionEntry* entry);
751 };
752
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800753 std::vector<KeyMemento> mKeyMementos;
754 std::vector<MotionMemento> mMotionMementos;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800755 KeyedVector<int32_t, int32_t> mFallbackKeys;
756
757 ssize_t findKeyMemento(const KeyEntry* entry) const;
758 ssize_t findMotionMemento(const MotionEntry* entry, bool hovering) const;
759
760 void addKeyMemento(const KeyEntry* entry, int32_t flags);
761 void addMotionMemento(const MotionEntry* entry, int32_t flags, bool hovering);
762
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700763 static bool shouldCancelKey(const KeyMemento& memento, const CancelationOptions& options);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800764 static bool shouldCancelMotion(const MotionMemento& memento,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700765 const CancelationOptions& options);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800766 };
767
768 /* Manages the dispatch state associated with a single input channel. */
769 class Connection : public RefBase {
770 protected:
771 virtual ~Connection();
772
773 public:
774 enum Status {
775 // Everything is peachy.
776 STATUS_NORMAL,
777 // An unrecoverable communication error has occurred.
778 STATUS_BROKEN,
779 // The input channel has been unregistered.
780 STATUS_ZOMBIE
781 };
782
783 Status status;
784 sp<InputChannel> inputChannel; // never null
Michael Wrightd02c5b62014-02-10 15:10:22 -0800785 bool monitor;
786 InputPublisher inputPublisher;
787 InputState inputState;
788
789 // True if the socket is full and no further events can be published until
790 // the application consumes some of the input.
791 bool inputPublisherBlocked;
792
793 // Queue of events that need to be published to the connection.
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -0700794 std::deque<DispatchEntry*> outboundQueue;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800795
796 // Queue of events that have been published to the connection but that have not
797 // yet received a "finished" response from the application.
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -0700798 std::deque<DispatchEntry*> waitQueue;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800799
Robert Carr803535b2018-08-02 16:38:15 -0700800 explicit Connection(const sp<InputChannel>& inputChannel, bool monitor);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800801
Siarhei Vishniakou587c3f02018-01-04 11:46:44 -0800802 inline const std::string getInputChannelName() const { return inputChannel->getName(); }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800803
Siarhei Vishniakou587c3f02018-01-04 11:46:44 -0800804 const std::string getWindowName() const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800805 const char* getStatusLabel() const;
806
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -0700807 std::deque<DispatchEntry*>::iterator findWaitQueueEntry(uint32_t seq);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800808 };
809
Michael Wright3dd60e22019-03-27 22:06:44 +0000810 struct Monitor {
811 sp<InputChannel> inputChannel; // never null
812
813 explicit Monitor(const sp<InputChannel>& inputChannel);
814 };
815
Michael Wrightd02c5b62014-02-10 15:10:22 -0800816 enum DropReason {
817 DROP_REASON_NOT_DROPPED = 0,
818 DROP_REASON_POLICY = 1,
819 DROP_REASON_APP_SWITCH = 2,
820 DROP_REASON_DISABLED = 3,
821 DROP_REASON_BLOCKED = 4,
822 DROP_REASON_STALE = 5,
823 };
824
825 sp<InputDispatcherPolicyInterface> mPolicy;
826 InputDispatcherConfiguration mConfig;
827
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800828 std::mutex mLock;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800829
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800830 std::condition_variable mDispatcherIsAlive;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800831
832 sp<Looper> mLooper;
833
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800834 EventEntry* mPendingEvent GUARDED_BY(mLock);
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -0700835 std::deque<EventEntry*> mInboundQueue GUARDED_BY(mLock);
836 std::deque<EventEntry*> mRecentQueue GUARDED_BY(mLock);
Siarhei Vishniakoue7c94b92019-07-29 09:17:54 -0700837 std::deque<std::unique_ptr<CommandEntry>> mCommandQueue GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800838
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800839 DropReason mLastDropReason GUARDED_BY(mLock);
Michael Wright3a981722015-06-10 15:26:13 +0100840
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800841 void dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800842
843 // Enqueues an inbound event. Returns true if mLooper->wake() should be called.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800844 bool enqueueInboundEventLocked(EventEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800845
846 // Cleans up input state when dropping an inbound event.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800847 void dropInboundEventLocked(EventEntry* entry, DropReason dropReason) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800848
849 // Adds an event to a queue of recent events for debugging purposes.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800850 void addRecentEventLocked(EventEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800851
852 // App switch latency optimization.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800853 bool mAppSwitchSawKeyDown GUARDED_BY(mLock);
854 nsecs_t mAppSwitchDueTime GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800855
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800856 bool isAppSwitchKeyEvent(KeyEntry* keyEntry);
857 bool isAppSwitchPendingLocked() REQUIRES(mLock);
858 void resetPendingAppSwitchLocked(bool handled) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800859
860 // Stale event latency optimization.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800861 static bool isStaleEvent(nsecs_t currentTime, EventEntry* entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800862
863 // Blocked event latency optimization. Drops old events when the user intends
864 // to transfer focus to a new application.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800865 EventEntry* mNextUnblockedEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800866
Tiger Huang85b8c5e2019-01-17 18:34:54 +0800867 sp<InputWindowHandle> findTouchedWindowAtLocked(int32_t displayId, int32_t x, int32_t y,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700868 bool addOutsideTargets = false,
869 bool addPortalWindows = false) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800870
871 // All registered connections mapped by channel file descriptor.
Siarhei Vishniakou146ecfd2019-07-29 16:04:31 -0700872 std::unordered_map<int, sp<Connection>> mConnectionsByFd GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800873
Robert Carr5c8a0262018-10-03 16:30:44 -0700874 struct IBinderHash {
875 std::size_t operator()(const sp<IBinder>& b) const {
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700876 return std::hash<IBinder*>{}(b.get());
Robert Carr5c8a0262018-10-03 16:30:44 -0700877 }
878 };
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800879 std::unordered_map<sp<IBinder>, sp<InputChannel>, IBinderHash> mInputChannelsByToken
880 GUARDED_BY(mLock);
Robert Carr5c8a0262018-10-03 16:30:44 -0700881
Michael Wright3dd60e22019-03-27 22:06:44 +0000882 // Finds the display ID of the gesture monitor identified by the provided token.
883 std::optional<int32_t> findGestureMonitorDisplayByTokenLocked(const sp<IBinder>& token)
884 REQUIRES(mLock);
885
Siarhei Vishniakou146ecfd2019-07-29 16:04:31 -0700886 sp<Connection> getConnectionLocked(const sp<InputChannel>& inputChannel) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800887
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800888 // Input channels that will receive a copy of all input events sent to the provided display.
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700889 std::unordered_map<int32_t, std::vector<Monitor>> mGlobalMonitorsByDisplay GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800890
Michael Wright3dd60e22019-03-27 22:06:44 +0000891 // Input channels that will receive pointer events that start within the corresponding display.
892 // These are a bit special when compared to global monitors since they'll cause gesture streams
893 // to continue even when there isn't a touched window,and have the ability to steal the rest of
894 // the pointer stream in order to claim it for a system gesture.
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700895 std::unordered_map<int32_t, std::vector<Monitor>> mGestureMonitorsByDisplay GUARDED_BY(mLock);
Michael Wright3dd60e22019-03-27 22:06:44 +0000896
Michael Wrightd02c5b62014-02-10 15:10:22 -0800897 // Event injection and synchronization.
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800898 std::condition_variable mInjectionResultAvailable;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800899 bool hasInjectionPermission(int32_t injectorPid, int32_t injectorUid);
Siarhei Vishniakou62683e82019-03-06 17:59:56 -0800900 void setInjectionResult(EventEntry* entry, int32_t injectionResult);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800901
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800902 std::condition_variable mInjectionSyncFinished;
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800903 void incrementPendingForegroundDispatches(EventEntry* entry);
Siarhei Vishniakou62683e82019-03-06 17:59:56 -0800904 void decrementPendingForegroundDispatches(EventEntry* entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800905
906 // Key repeat tracking.
907 struct KeyRepeatState {
908 KeyEntry* lastKeyEntry; // or null if no repeat
909 nsecs_t nextRepeatTime;
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800910 } mKeyRepeatState GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800911
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800912 void resetKeyRepeatLocked() REQUIRES(mLock);
913 KeyEntry* synthesizeKeyRepeatLocked(nsecs_t currentTime) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800914
Michael Wright78f24442014-08-06 15:55:28 -0700915 // Key replacement tracking
916 struct KeyReplacement {
917 int32_t keyCode;
918 int32_t deviceId;
919 bool operator==(const KeyReplacement& rhs) const {
920 return keyCode == rhs.keyCode && deviceId == rhs.deviceId;
921 }
922 bool operator<(const KeyReplacement& rhs) const {
923 return keyCode != rhs.keyCode ? keyCode < rhs.keyCode : deviceId < rhs.deviceId;
924 }
925 };
926 // Maps the key code replaced, device id tuple to the key code it was replaced with
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800927 KeyedVector<KeyReplacement, int32_t> mReplacedKeys GUARDED_BY(mLock);
Siarhei Vishniakou61fafdd2018-04-13 11:00:58 -0500928 // Process certain Meta + Key combinations
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700929 void accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, int32_t& keyCode,
930 int32_t& metaState);
Michael Wright78f24442014-08-06 15:55:28 -0700931
Michael Wrightd02c5b62014-02-10 15:10:22 -0800932 // Deferred command processing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800933 bool haveCommandsLocked() const REQUIRES(mLock);
934 bool runCommandsLockedInterruptible() REQUIRES(mLock);
Siarhei Vishniakoue7c94b92019-07-29 09:17:54 -0700935 void postCommandLocked(std::unique_ptr<CommandEntry> commandEntry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800936
937 // Input filter processing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800938 bool shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) REQUIRES(mLock);
939 bool shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800940
941 // Inbound event processing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800942 void drainInboundQueueLocked() REQUIRES(mLock);
943 void releasePendingEventLocked() REQUIRES(mLock);
944 void releaseInboundEventLocked(EventEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800945
946 // Dispatch state.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800947 bool mDispatchEnabled GUARDED_BY(mLock);
948 bool mDispatchFrozen GUARDED_BY(mLock);
949 bool mInputFilterEnabled GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800950
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800951 std::unordered_map<int32_t, std::vector<sp<InputWindowHandle>>> mWindowHandlesByDisplay
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800952 GUARDED_BY(mLock);
Arthur Hungb92218b2018-08-14 12:00:21 +0800953 // Get window handles by display, return an empty vector if not found.
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800954 std::vector<sp<InputWindowHandle>> getWindowHandlesLocked(int32_t displayId) const
955 REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800956 sp<InputWindowHandle> getWindowHandleLocked(const sp<IBinder>& windowHandleToken) const
957 REQUIRES(mLock);
958 sp<InputChannel> getInputChannelLocked(const sp<IBinder>& windowToken) const REQUIRES(mLock);
959 bool hasWindowHandleLocked(const sp<InputWindowHandle>& windowHandle) const REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800960
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -0700961 /*
962 * Validate and update InputWindowHandles for a given display.
963 */
964 void updateWindowHandlesForDisplayLocked(
965 const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId)
966 REQUIRES(mLock);
967
Michael Wrightd02c5b62014-02-10 15:10:22 -0800968 // Focus tracking for keys, trackball, etc.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800969 std::unordered_map<int32_t, sp<InputWindowHandle>> mFocusedWindowHandlesByDisplay
970 GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800971
972 // Focus tracking for touch.
973 struct TouchedWindow {
974 sp<InputWindowHandle> windowHandle;
975 int32_t targetFlags;
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700976 BitSet32 pointerIds; // zero unless target flag FLAG_SPLIT is set
Michael Wrightd02c5b62014-02-10 15:10:22 -0800977 };
Michael Wright3dd60e22019-03-27 22:06:44 +0000978
979 // For tracking the offsets we need to apply when adding gesture monitor targets.
980 struct TouchedMonitor {
981 Monitor monitor;
982 float xOffset = 0.f;
983 float yOffset = 0.f;
984
985 explicit TouchedMonitor(const Monitor& monitor, float xOffset, float yOffset);
986 };
987
Michael Wrightd02c5b62014-02-10 15:10:22 -0800988 struct TouchState {
989 bool down;
990 bool split;
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700991 int32_t deviceId; // id of the device that is currently down, others are rejected
992 uint32_t source; // source of the device that is current down, others are rejected
Michael Wrightd02c5b62014-02-10 15:10:22 -0800993 int32_t displayId; // id to the display that currently has a touch, others are rejected
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800994 std::vector<TouchedWindow> windows;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800995
Tiger Huang85b8c5e2019-01-17 18:34:54 +0800996 // This collects the portal windows that the touch has gone through. Each portal window
997 // targets a display (embedded display for most cases). With this info, we can add the
998 // monitoring channels of the displays touched.
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800999 std::vector<sp<InputWindowHandle>> portalWindows;
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001000
Michael Wright3dd60e22019-03-27 22:06:44 +00001001 std::vector<TouchedMonitor> gestureMonitors;
1002
Michael Wrightd02c5b62014-02-10 15:10:22 -08001003 TouchState();
1004 ~TouchState();
1005 void reset();
1006 void copyFrom(const TouchState& other);
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001007 void addOrUpdateWindow(const sp<InputWindowHandle>& windowHandle, int32_t targetFlags,
1008 BitSet32 pointerIds);
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001009 void addPortalWindow(const sp<InputWindowHandle>& windowHandle);
Michael Wright3dd60e22019-03-27 22:06:44 +00001010 void addGestureMonitors(const std::vector<TouchedMonitor>& monitors);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001011 void removeWindow(const sp<InputWindowHandle>& windowHandle);
Robert Carr803535b2018-08-02 16:38:15 -07001012 void removeWindowByToken(const sp<IBinder>& token);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001013 void filterNonAsIsTouchWindows();
Michael Wright3dd60e22019-03-27 22:06:44 +00001014 void filterNonMonitors();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001015 sp<InputWindowHandle> getFirstForegroundWindowHandle() const;
1016 bool isSlippery() const;
1017 };
1018
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001019 KeyedVector<int32_t, TouchState> mTouchStatesByDisplay GUARDED_BY(mLock);
1020 TouchState mTempTouchState GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001021
Tiger Huang721e26f2018-07-24 22:26:19 +08001022 // Focused applications.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001023 std::unordered_map<int32_t, sp<InputApplicationHandle>> mFocusedApplicationHandlesByDisplay
1024 GUARDED_BY(mLock);
Tiger Huang721e26f2018-07-24 22:26:19 +08001025
1026 // Top focused display.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001027 int32_t mFocusedDisplayId GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001028
1029 // Dispatcher state at time of last ANR.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001030 std::string mLastANRState GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001031
1032 // Dispatch inbound events.
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001033 bool dispatchConfigurationChangedLocked(nsecs_t currentTime, ConfigurationChangedEntry* entry)
1034 REQUIRES(mLock);
1035 bool dispatchDeviceResetLocked(nsecs_t currentTime, DeviceResetEntry* entry) REQUIRES(mLock);
1036 bool dispatchKeyLocked(nsecs_t currentTime, KeyEntry* entry, DropReason* dropReason,
1037 nsecs_t* nextWakeupTime) REQUIRES(mLock);
1038 bool dispatchMotionLocked(nsecs_t currentTime, MotionEntry* entry, DropReason* dropReason,
1039 nsecs_t* nextWakeupTime) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001040 void dispatchEventLocked(nsecs_t currentTime, EventEntry* entry,
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001041 const std::vector<InputTarget>& inputTargets) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001042
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001043 void logOutboundKeyDetails(const char* prefix, const KeyEntry* entry);
1044 void logOutboundMotionDetails(const char* prefix, const MotionEntry* entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001045
1046 // Keeping track of ANR timeouts.
1047 enum InputTargetWaitCause {
1048 INPUT_TARGET_WAIT_CAUSE_NONE,
1049 INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY,
1050 INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY,
1051 };
1052
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001053 InputTargetWaitCause mInputTargetWaitCause GUARDED_BY(mLock);
1054 nsecs_t mInputTargetWaitStartTime GUARDED_BY(mLock);
1055 nsecs_t mInputTargetWaitTimeoutTime GUARDED_BY(mLock);
1056 bool mInputTargetWaitTimeoutExpired GUARDED_BY(mLock);
1057 sp<IBinder> mInputTargetWaitApplicationToken GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001058
1059 // Contains the last window which received a hover event.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001060 sp<InputWindowHandle> mLastHoverWindowHandle GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001061
1062 // Finding targets for input events.
1063 int32_t handleTargetsNotReadyLocked(nsecs_t currentTime, const EventEntry* entry,
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001064 const sp<InputApplicationHandle>& applicationHandle,
1065 const sp<InputWindowHandle>& windowHandle,
1066 nsecs_t* nextWakeupTime, const char* reason)
1067 REQUIRES(mLock);
Robert Carr803535b2018-08-02 16:38:15 -07001068
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001069 void removeWindowByTokenLocked(const sp<IBinder>& token) REQUIRES(mLock);
Robert Carr803535b2018-08-02 16:38:15 -07001070
Michael Wrightd02c5b62014-02-10 15:10:22 -08001071 void resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout,
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001072 const sp<InputChannel>& inputChannel)
1073 REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001074 nsecs_t getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime) REQUIRES(mLock);
1075 void resetANRTimeoutsLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001076
Tiger Huang721e26f2018-07-24 22:26:19 +08001077 int32_t getTargetDisplayId(const EventEntry* entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001078 int32_t findFocusedWindowTargetsLocked(nsecs_t currentTime, const EventEntry* entry,
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001079 std::vector<InputTarget>& inputTargets,
1080 nsecs_t* nextWakeupTime) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001081 int32_t findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry* entry,
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001082 std::vector<InputTarget>& inputTargets,
1083 nsecs_t* nextWakeupTime,
1084 bool* outConflictingPointerActions) REQUIRES(mLock);
1085 std::vector<TouchedMonitor> findTouchedGestureMonitorsLocked(
1086 int32_t displayId, const std::vector<sp<InputWindowHandle>>& portalWindows)
1087 REQUIRES(mLock);
Michael Wright3dd60e22019-03-27 22:06:44 +00001088 void addGestureMonitors(const std::vector<Monitor>& monitors,
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001089 std::vector<TouchedMonitor>& outTouchedMonitors, float xOffset = 0,
1090 float yOffset = 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001091
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001092 void addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle, int32_t targetFlags,
1093 BitSet32 pointerIds, std::vector<InputTarget>& inputTargets)
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001094 REQUIRES(mLock);
Michael Wright3dd60e22019-03-27 22:06:44 +00001095 void addMonitoringTargetLocked(const Monitor& monitor, float xOffset, float yOffset,
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001096 std::vector<InputTarget>& inputTargets) REQUIRES(mLock);
1097 void addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, int32_t displayId,
1098 float xOffset = 0, float yOffset = 0) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001099
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001100 void pokeUserActivityLocked(const EventEntry* eventEntry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001101 bool checkInjectionPermission(const sp<InputWindowHandle>& windowHandle,
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001102 const InjectionState* injectionState);
1103 bool isWindowObscuredAtPointLocked(const sp<InputWindowHandle>& windowHandle, int32_t x,
1104 int32_t y) const REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001105 bool isWindowObscuredLocked(const sp<InputWindowHandle>& windowHandle) const REQUIRES(mLock);
1106 std::string getApplicationWindowLabel(const sp<InputApplicationHandle>& applicationHandle,
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001107 const sp<InputWindowHandle>& windowHandle);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001108
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001109 std::string checkWindowReadyForMoreInputLocked(nsecs_t currentTime,
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001110 const sp<InputWindowHandle>& windowHandle,
1111 const EventEntry* eventEntry,
1112 const char* targetType) REQUIRES(mLock);
Jeff Brownffb49772014-10-10 19:01:34 -07001113
Michael Wrightd02c5b62014-02-10 15:10:22 -08001114 // Manage the dispatch cycle for a single connection.
1115 // These methods are deliberately not Interruptible because doing all of the work
1116 // with the mutex held makes it easier to ensure that connection invariants are maintained.
1117 // If needed, the methods post commands to run later once the critical bits are done.
1118 void prepareDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001119 EventEntry* eventEntry, const InputTarget* inputTarget)
1120 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001121 void enqueueDispatchEntriesLocked(nsecs_t currentTime, const sp<Connection>& connection,
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001122 EventEntry* eventEntry, const InputTarget* inputTarget)
1123 REQUIRES(mLock);
1124 void enqueueDispatchEntryLocked(const sp<Connection>& connection, EventEntry* eventEntry,
1125 const InputTarget* inputTarget, int32_t dispatchMode)
chaviw8c9cf542019-03-25 13:02:48 -07001126 REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001127 void startDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection)
1128 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001129 void finishDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001130 uint32_t seq, bool handled) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001131 void abortBrokenDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001132 bool notify) REQUIRES(mLock);
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -07001133 void drainDispatchQueue(std::deque<DispatchEntry*>& queue);
Siarhei Vishniakou62683e82019-03-06 17:59:56 -08001134 void releaseDispatchEntry(DispatchEntry* dispatchEntry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001135 static int handleReceiveCallback(int fd, int events, void* data);
chaviw8c9cf542019-03-25 13:02:48 -07001136 // The action sent should only be of type AMOTION_EVENT_*
chaviwfd6d3512019-03-25 13:23:49 -07001137 void dispatchPointerDownOutsideFocus(uint32_t source, int32_t action,
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001138 const sp<IBinder>& newToken) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001139
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001140 void synthesizeCancelationEventsForAllConnectionsLocked(const CancelationOptions& options)
1141 REQUIRES(mLock);
1142 void synthesizeCancelationEventsForMonitorsLocked(const CancelationOptions& options)
1143 REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001144 void synthesizeCancelationEventsForMonitorsLocked(
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001145 const CancelationOptions& options,
Michael Wright3dd60e22019-03-27 22:06:44 +00001146 std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001147 void synthesizeCancelationEventsForInputChannelLocked(const sp<InputChannel>& channel,
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001148 const CancelationOptions& options)
1149 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001150 void synthesizeCancelationEventsForConnectionLocked(const sp<Connection>& connection,
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001151 const CancelationOptions& options)
1152 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001153
1154 // Splitting motion events across windows.
1155 MotionEntry* splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds);
1156
1157 // Reset and drop everything the dispatcher is doing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001158 void resetAndDropEverythingLocked(const char* reason) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001159
1160 // Dump state.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001161 void dumpDispatchStateLocked(std::string& dump) REQUIRES(mLock);
Michael Wright3dd60e22019-03-27 22:06:44 +00001162 void dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001163 void logDispatchStateLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001164
1165 // Registration.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001166 void removeMonitorChannelLocked(const sp<InputChannel>& inputChannel) REQUIRES(mLock);
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001167 void removeMonitorChannelLocked(
1168 const sp<InputChannel>& inputChannel,
1169 std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001170 status_t unregisterInputChannelLocked(const sp<InputChannel>& inputChannel, bool notify)
1171 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001172
1173 // Interesting events that we might like to log or tell the framework about.
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001174 void onDispatchCycleFinishedLocked(nsecs_t currentTime, const sp<Connection>& connection,
1175 uint32_t seq, bool handled) REQUIRES(mLock);
1176 void onDispatchCycleBrokenLocked(nsecs_t currentTime, const sp<Connection>& connection)
1177 REQUIRES(mLock);
chaviw0c06c6e2019-01-09 13:27:07 -08001178 void onFocusChangedLocked(const sp<InputWindowHandle>& oldFocus,
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001179 const sp<InputWindowHandle>& newFocus) REQUIRES(mLock);
1180 void onANRLocked(nsecs_t currentTime, const sp<InputApplicationHandle>& applicationHandle,
1181 const sp<InputWindowHandle>& windowHandle, nsecs_t eventTime,
1182 nsecs_t waitStartTime, const char* reason) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001183
1184 // Outbound policy interactions.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001185 void doNotifyConfigurationChangedLockedInterruptible(CommandEntry* commandEntry)
1186 REQUIRES(mLock);
1187 void doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
1188 void doNotifyFocusChangedLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
1189 void doNotifyANRLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
1190 void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry)
1191 REQUIRES(mLock);
1192 void doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001193 bool afterKeyEventLockedInterruptible(const sp<Connection>& connection,
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001194 DispatchEntry* dispatchEntry, KeyEntry* keyEntry,
1195 bool handled) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001196 bool afterMotionEventLockedInterruptible(const sp<Connection>& connection,
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001197 DispatchEntry* dispatchEntry, MotionEntry* motionEntry,
1198 bool handled) REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001199 void doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001200 void initializeKeyEvent(KeyEvent* event, const KeyEntry* entry);
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001201 void doOnPointerDownOutsideFocusLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001202
1203 // Statistics gathering.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001204 void updateDispatchStatistics(nsecs_t currentTime, const EventEntry* entry,
Garfield Tan0fc2fa72019-08-29 17:22:15 -07001205 int32_t injectionResult, nsecs_t timeSpentWaitingForApplication);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001206 void traceInboundQueueLengthLocked() REQUIRES(mLock);
1207 void traceOutboundQueueLength(const sp<Connection>& connection);
1208 void traceWaitQueueLength(const sp<Connection>& connection);
Prabir Pradhanf93562f2018-11-29 12:13:37 -08001209
Prabir Pradhan79a4f0c2019-01-09 11:24:01 -08001210 sp<InputReporterInterface> mReporter;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001211};
1212
1213/* Enqueues and dispatches input events, endlessly. */
1214class InputDispatcherThread : public Thread {
1215public:
1216 explicit InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher);
1217 ~InputDispatcherThread();
1218
1219private:
1220 virtual bool threadLoop();
1221
1222 sp<InputDispatcherInterface> mDispatcher;
1223};
1224
1225} // namespace android
1226
1227#endif // _UI_INPUT_DISPATCHER_H