blob: b6c07970459f933a2b928023f8a374f87c7c5a9e [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
Siarhei Vishniakou443ad902019-03-06 17:25:41 -080020#include <condition_variable>
Michael Wrightd02c5b62014-02-10 15:10:22 -080021#include <input/Input.h>
Robert Carr3720ed02018-08-08 16:08:27 -070022#include <input/InputApplication.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080023#include <input/InputTransport.h>
Robert Carr3720ed02018-08-08 16:08:27 -070024#include <input/InputWindow.h>
chaviw291d88a2019-02-14 10:33:58 -080025#include <input/ISetInputWindowsListener.h>
Michael Wright3dd60e22019-03-27 22:06:44 +000026#include <optional>
27#include <ui/Region.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080028#include <utils/threads.h>
29#include <utils/Timers.h>
30#include <utils/RefBase.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080031#include <utils/Looper.h>
32#include <utils/BitSet.h>
33#include <cutils/atomic.h>
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
84
85/*
86 * An input target specifies how an input event is to be dispatched to a particular window
87 * including the window's input channel, control flags, a timeout, and an X / Y offset to
88 * be added to input event coordinates to compensate for the absolute position of the
89 * window area.
90 */
91struct InputTarget {
92 enum {
93 /* This flag indicates that the event is being delivered to a foreground application. */
94 FLAG_FOREGROUND = 1 << 0,
95
Michael Wrightcdcd8f22016-03-22 16:52:13 -070096 /* This flag indicates that the MotionEvent falls within the area of the target
Michael Wrightd02c5b62014-02-10 15:10:22 -080097 * obscured by another visible window above it. The motion event should be
98 * delivered with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED. */
99 FLAG_WINDOW_IS_OBSCURED = 1 << 1,
100
101 /* This flag indicates that a motion event is being split across multiple windows. */
102 FLAG_SPLIT = 1 << 2,
103
104 /* This flag indicates that the pointer coordinates dispatched to the application
105 * will be zeroed out to avoid revealing information to an application. This is
106 * used in conjunction with FLAG_DISPATCH_AS_OUTSIDE to prevent apps not sharing
107 * the same UID from watching all touches. */
108 FLAG_ZERO_COORDS = 1 << 3,
109
110 /* This flag indicates that the event should be sent as is.
111 * Should always be set unless the event is to be transmuted. */
112 FLAG_DISPATCH_AS_IS = 1 << 8,
113
114 /* This flag indicates that a MotionEvent with AMOTION_EVENT_ACTION_DOWN falls outside
115 * of the area of this target and so should instead be delivered as an
116 * AMOTION_EVENT_ACTION_OUTSIDE to this target. */
117 FLAG_DISPATCH_AS_OUTSIDE = 1 << 9,
118
119 /* This flag indicates that a hover sequence is starting in the given window.
120 * The event is transmuted into ACTION_HOVER_ENTER. */
121 FLAG_DISPATCH_AS_HOVER_ENTER = 1 << 10,
122
123 /* This flag indicates that a hover event happened outside of a window which handled
124 * previous hover events, signifying the end of the current hover sequence for that
125 * window.
126 * The event is transmuted into ACTION_HOVER_ENTER. */
127 FLAG_DISPATCH_AS_HOVER_EXIT = 1 << 11,
128
129 /* This flag indicates that the event should be canceled.
130 * It is used to transmute ACTION_MOVE into ACTION_CANCEL when a touch slips
131 * outside of a window. */
132 FLAG_DISPATCH_AS_SLIPPERY_EXIT = 1 << 12,
133
134 /* This flag indicates that the event should be dispatched as an initial down.
135 * It is used to transmute ACTION_MOVE into ACTION_DOWN when a touch slips
136 * into a new window. */
137 FLAG_DISPATCH_AS_SLIPPERY_ENTER = 1 << 13,
138
139 /* Mask for all dispatch modes. */
140 FLAG_DISPATCH_MASK = FLAG_DISPATCH_AS_IS
141 | FLAG_DISPATCH_AS_OUTSIDE
142 | FLAG_DISPATCH_AS_HOVER_ENTER
143 | FLAG_DISPATCH_AS_HOVER_EXIT
144 | FLAG_DISPATCH_AS_SLIPPERY_EXIT
145 | FLAG_DISPATCH_AS_SLIPPERY_ENTER,
Michael Wrightcdcd8f22016-03-22 16:52:13 -0700146
147 /* This flag indicates that the target of a MotionEvent is partly or wholly
148 * obscured by another visible window above it. The motion event should be
149 * delivered with flag AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED. */
150 FLAG_WINDOW_IS_PARTIALLY_OBSCURED = 1 << 14,
151
Michael Wrightd02c5b62014-02-10 15:10:22 -0800152 };
153
154 // The input channel to be targeted.
155 sp<InputChannel> inputChannel;
156
157 // Flags for the input target.
158 int32_t flags;
159
160 // The x and y offset to add to a MotionEvent as it is delivered.
161 // (ignored for KeyEvents)
162 float xOffset, yOffset;
163
164 // Scaling factor to apply to MotionEvent as it is delivered.
165 // (ignored for KeyEvents)
Robert Carre07e1032018-11-26 12:55:53 -0800166 float globalScaleFactor;
167 float windowXScale = 1.0f;
168 float windowYScale = 1.0f;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800169
170 // The subset of pointer ids to include in motion events dispatched to this input target
171 // if FLAG_SPLIT is set.
172 BitSet32 pointerIds;
173};
174
175
176/*
177 * Input dispatcher configuration.
178 *
179 * Specifies various options that modify the behavior of the input dispatcher.
180 * The values provided here are merely defaults. The actual values will come from ViewConfiguration
181 * and are passed into the dispatcher during initialization.
182 */
183struct InputDispatcherConfiguration {
184 // The key repeat initial timeout.
185 nsecs_t keyRepeatTimeout;
186
187 // The key repeat inter-key delay.
188 nsecs_t keyRepeatDelay;
189
190 InputDispatcherConfiguration() :
191 keyRepeatTimeout(500 * 1000000LL),
192 keyRepeatDelay(50 * 1000000LL) { }
193};
194
195
196/*
197 * Input dispatcher policy interface.
198 *
199 * The input reader policy is used by the input reader to interact with the Window Manager
200 * and other system components.
201 *
202 * The actual implementation is partially supported by callbacks into the DVM
203 * via JNI. This interface is also mocked in the unit tests.
204 */
205class InputDispatcherPolicyInterface : public virtual RefBase {
206protected:
207 InputDispatcherPolicyInterface() { }
208 virtual ~InputDispatcherPolicyInterface() { }
209
210public:
211 /* Notifies the system that a configuration change has occurred. */
212 virtual void notifyConfigurationChanged(nsecs_t when) = 0;
213
214 /* Notifies the system that an application is not responding.
215 * Returns a new timeout to continue waiting, or 0 to abort dispatch. */
216 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Robert Carr803535b2018-08-02 16:38:15 -0700217 const sp<IBinder>& token,
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800218 const std::string& reason) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800219
220 /* Notifies the system that an input channel is unrecoverably broken. */
Robert Carr803535b2018-08-02 16:38:15 -0700221 virtual void notifyInputChannelBroken(const sp<IBinder>& token) = 0;
chaviw0c06c6e2019-01-09 13:27:07 -0800222 virtual void notifyFocusChanged(const sp<IBinder>& oldToken, const sp<IBinder>& newToken) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800223
224 /* Gets the input dispatcher configuration. */
225 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) = 0;
226
Michael Wrightd02c5b62014-02-10 15:10:22 -0800227 /* Filters an input event.
228 * Return true to dispatch the event unmodified, false to consume the event.
229 * A filter can also transform and inject events later by passing POLICY_FLAG_FILTERED
230 * to injectInputEvent.
231 */
232 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) = 0;
233
234 /* Intercepts a key event immediately before queueing it.
235 * The policy can use this method as an opportunity to perform power management functions
236 * and early event preprocessing such as updating policy flags.
237 *
238 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
239 * should be dispatched to applications.
240 */
241 virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags) = 0;
242
243 /* Intercepts a touch, trackball or other motion event before queueing it.
244 * The policy can use this method as an opportunity to perform power management functions
245 * and early event preprocessing such as updating policy flags.
246 *
247 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
248 * should be dispatched to applications.
249 */
Charles Chen3611f1f2019-01-29 17:26:18 +0800250 virtual void interceptMotionBeforeQueueing(const int32_t displayId, nsecs_t when,
251 uint32_t& policyFlags) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800252
253 /* Allows the policy a chance to intercept a key before dispatching. */
Robert Carr803535b2018-08-02 16:38:15 -0700254 virtual nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>& token,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800255 const KeyEvent* keyEvent, uint32_t policyFlags) = 0;
256
257 /* Allows the policy a chance to perform default processing for an unhandled key.
258 * Returns an alternate keycode to redispatch as a fallback, or 0 to give up. */
Robert Carr803535b2018-08-02 16:38:15 -0700259 virtual bool dispatchUnhandledKey(const sp<IBinder>& token,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800260 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) = 0;
261
262 /* Notifies the policy about switch events.
263 */
264 virtual void notifySwitch(nsecs_t when,
265 uint32_t switchValues, uint32_t switchMask, uint32_t policyFlags) = 0;
266
267 /* Poke user activity for an event dispatched to a window. */
268 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType) = 0;
269
270 /* Checks whether a given application pid/uid has permission to inject input events
271 * into other applications.
272 *
273 * This method is special in that its implementation promises to be non-reentrant and
274 * is safe to call while holding other locks. (Most other methods make no such guarantees!)
275 */
276 virtual bool checkInjectEventsPermissionNonReentrant(
277 int32_t injectorPid, int32_t injectorUid) = 0;
chaviwfd6d3512019-03-25 13:23:49 -0700278
279 /* Notifies the policy that a pointer down event has occurred outside the current focused
280 * window.
281 *
282 * The touchedToken passed as an argument is the window that received the input event.
283 */
284 virtual void onPointerDownOutsideFocus(const sp<IBinder>& touchedToken) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800285};
286
287
288/* Notifies the system about input events generated by the input reader.
289 * The dispatcher is expected to be mostly asynchronous. */
290class InputDispatcherInterface : public virtual RefBase, public InputListenerInterface {
291protected:
292 InputDispatcherInterface() { }
293 virtual ~InputDispatcherInterface() { }
294
295public:
296 /* Dumps the state of the input dispatcher.
297 *
298 * This method may be called on any thread (usually by the input manager). */
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800299 virtual void dump(std::string& dump) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800300
301 /* Called by the heatbeat to ensures that the dispatcher has not deadlocked. */
302 virtual void monitor() = 0;
303
304 /* Runs a single iteration of the dispatch loop.
305 * Nominally processes one queued event, a timeout, or a response from an input consumer.
306 *
307 * This method should only be called on the input dispatcher thread.
308 */
309 virtual void dispatchOnce() = 0;
310
311 /* Injects an input event and optionally waits for sync.
312 * The synchronization mode determines whether the method blocks while waiting for
313 * input injection to proceed.
314 * Returns one of the INPUT_EVENT_INJECTION_XXX constants.
315 *
316 * This method may be called on any thread (usually by the input manager).
317 */
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800318 virtual int32_t injectInputEvent(const InputEvent* event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800319 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,
320 uint32_t policyFlags) = 0;
321
322 /* Sets the list of input windows.
323 *
324 * This method may be called on any thread (usually by the input manager).
325 */
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800326 virtual void setInputWindows(const std::vector<sp<InputWindowHandle> >& inputWindowHandles,
chaviw291d88a2019-02-14 10:33:58 -0800327 int32_t displayId,
328 const sp<ISetInputWindowsListener>& setInputWindowsListener = nullptr) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800329
Tiger Huang721e26f2018-07-24 22:26:19 +0800330 /* Sets the focused application on the given display.
Michael Wrightd02c5b62014-02-10 15:10:22 -0800331 *
332 * This method may be called on any thread (usually by the input manager).
333 */
334 virtual void setFocusedApplication(
Tiger Huang721e26f2018-07-24 22:26:19 +0800335 int32_t displayId, const sp<InputApplicationHandle>& inputApplicationHandle) = 0;
336
337 /* Sets the focused display.
338 *
339 * This method may be called on any thread (usually by the input manager).
340 */
341 virtual void setFocusedDisplay(int32_t displayId) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800342
343 /* Sets the input dispatching mode.
344 *
345 * This method may be called on any thread (usually by the input manager).
346 */
347 virtual void setInputDispatchMode(bool enabled, bool frozen) = 0;
348
349 /* Sets whether input event filtering is enabled.
350 * When enabled, incoming input events are sent to the policy's filterInputEvent
351 * method instead of being dispatched. The filter is expected to use
352 * injectInputEvent to inject the events it would like to have dispatched.
353 * It should include POLICY_FLAG_FILTERED in the policy flags during injection.
354 */
355 virtual void setInputFilterEnabled(bool enabled) = 0;
356
chaviwfbe5d9c2018-12-26 12:23:37 -0800357 /* Transfers touch focus from one window to another window.
Michael Wrightd02c5b62014-02-10 15:10:22 -0800358 *
359 * Returns true on success. False if the window did not actually have touch focus.
360 */
chaviwfbe5d9c2018-12-26 12:23:37 -0800361 virtual bool transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken) = 0;
362
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800363 /* Registers input channels that may be used as targets for input events.
Michael Wrightd02c5b62014-02-10 15:10:22 -0800364 *
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800365 * This method may be called on any thread (usually by the input manager).
Michael Wrightd02c5b62014-02-10 15:10:22 -0800366 */
Robert Carr803535b2018-08-02 16:38:15 -0700367 virtual status_t registerInputChannel(
368 const sp<InputChannel>& inputChannel, int32_t displayId) = 0;
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800369
Michael Wright3dd60e22019-03-27 22:06:44 +0000370 /* Registers input channels to be used to monitor input events.
371 *
372 * Each monitor must target a specific display and will only receive input events sent to that
373 * display. If the monitor is a gesture monitor, it will only receive pointer events on the
374 * targeted display.
375 *
376 * This method may be called on any thread (usually by the input manager).
377 */
378 virtual status_t registerInputMonitor(
379 const sp<InputChannel>& inputChannel, int32_t displayId, bool gestureMonitor) = 0;
380
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800381 /* Unregister input channels that will no longer receive input events.
382 *
383 * This method may be called on any thread (usually by the input manager).
384 */
Michael Wrightd02c5b62014-02-10 15:10:22 -0800385 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) = 0;
Michael Wright3dd60e22019-03-27 22:06:44 +0000386
387 /* Allows an input monitor steal the current pointer stream away from normal input windows.
388 *
389 * This method may be called on any thread (usually by the input manager).
390 */
391 virtual status_t pilferPointers(const sp<IBinder>& token) = 0;
392
Michael Wrightd02c5b62014-02-10 15:10:22 -0800393};
394
395/* Dispatches events to input targets. Some functions of the input dispatcher, such as
396 * identifying input targets, are controlled by a separate policy object.
397 *
398 * IMPORTANT INVARIANT:
399 * Because the policy can potentially block or cause re-entrance into the input dispatcher,
400 * the input dispatcher never calls into the policy while holding its internal locks.
401 * The implementation is also carefully designed to recover from scenarios such as an
402 * input channel becoming unregistered while identifying input targets or processing timeouts.
403 *
404 * Methods marked 'Locked' must be called with the lock acquired.
405 *
406 * Methods marked 'LockedInterruptible' must be called with the lock acquired but
407 * may during the course of their execution release the lock, call into the policy, and
408 * then reacquire the lock. The caller is responsible for recovering gracefully.
409 *
410 * A 'LockedInterruptible' method may called a 'Locked' method, but NOT vice-versa.
411 */
412class InputDispatcher : public InputDispatcherInterface {
413protected:
414 virtual ~InputDispatcher();
415
416public:
417 explicit InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy);
418
Michael Wright3dd60e22019-03-27 22:06:44 +0000419 virtual void dump(std::string& dump) override;
420 virtual void monitor() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800421
Michael Wright3dd60e22019-03-27 22:06:44 +0000422 virtual void dispatchOnce() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800423
Michael Wright3dd60e22019-03-27 22:06:44 +0000424 virtual void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) override;
425 virtual void notifyKey(const NotifyKeyArgs* args) override;
426 virtual void notifyMotion(const NotifyMotionArgs* args) override;
427 virtual void notifySwitch(const NotifySwitchArgs* args) override;
428 virtual void notifyDeviceReset(const NotifyDeviceResetArgs* args) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800429
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800430 virtual int32_t injectInputEvent(const InputEvent* event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800431 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,
Michael Wright3dd60e22019-03-27 22:06:44 +0000432 uint32_t policyFlags) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800433
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800434 virtual void setInputWindows(const std::vector<sp<InputWindowHandle> >& inputWindowHandles,
chaviw291d88a2019-02-14 10:33:58 -0800435 int32_t displayId,
Michael Wright3dd60e22019-03-27 22:06:44 +0000436 const sp<ISetInputWindowsListener>& setInputWindowsListener = nullptr) override;
Tiger Huang721e26f2018-07-24 22:26:19 +0800437 virtual void setFocusedApplication(int32_t displayId,
Michael Wright3dd60e22019-03-27 22:06:44 +0000438 const sp<InputApplicationHandle>& inputApplicationHandle) override;
439 virtual void setFocusedDisplay(int32_t displayId) override;
440 virtual void setInputDispatchMode(bool enabled, bool frozen) override;
441 virtual void setInputFilterEnabled(bool enabled) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800442
Michael Wright3dd60e22019-03-27 22:06:44 +0000443 virtual bool transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken)
444 override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800445
446 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel,
Michael Wright3dd60e22019-03-27 22:06:44 +0000447 int32_t displayId) override;
448 virtual status_t registerInputMonitor(const sp<InputChannel>& inputChannel,
449 int32_t displayId, bool isGestureMonitor) override;
450 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) override;
451 virtual status_t pilferPointers(const sp<IBinder>& token) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800452
453private:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800454
455 struct InjectionState {
456 mutable int32_t refCount;
457
458 int32_t injectorPid;
459 int32_t injectorUid;
460 int32_t injectionResult; // initially INPUT_EVENT_INJECTION_PENDING
461 bool injectionIsAsync; // set to true if injection is not waiting for the result
462 int32_t pendingForegroundDispatches; // the number of foreground dispatches in progress
463
464 InjectionState(int32_t injectorPid, int32_t injectorUid);
465 void release();
466
467 private:
468 ~InjectionState();
469 };
470
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -0700471 struct EventEntry {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800472 enum {
473 TYPE_CONFIGURATION_CHANGED,
474 TYPE_DEVICE_RESET,
475 TYPE_KEY,
476 TYPE_MOTION
477 };
478
Prabir Pradhan42611e02018-11-27 14:04:02 -0800479 uint32_t sequenceNum;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800480 mutable int32_t refCount;
481 int32_t type;
482 nsecs_t eventTime;
483 uint32_t policyFlags;
484 InjectionState* injectionState;
485
486 bool dispatchInProgress; // initially false, set to true while dispatching
487
Yi Kong9b14ac62018-07-17 13:48:38 -0700488 inline bool isInjected() const { return injectionState != nullptr; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800489
490 void release();
491
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800492 virtual void appendDescription(std::string& msg) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800493
494 protected:
Prabir Pradhan42611e02018-11-27 14:04:02 -0800495 EventEntry(uint32_t sequenceNum, int32_t type, nsecs_t eventTime, uint32_t policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800496 virtual ~EventEntry();
497 void releaseInjectionState();
498 };
499
500 struct ConfigurationChangedEntry : EventEntry {
Prabir Pradhan42611e02018-11-27 14:04:02 -0800501 explicit ConfigurationChangedEntry(uint32_t sequenceNum, nsecs_t eventTime);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800502 virtual void appendDescription(std::string& msg) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800503
504 protected:
505 virtual ~ConfigurationChangedEntry();
506 };
507
508 struct DeviceResetEntry : EventEntry {
509 int32_t deviceId;
510
Prabir Pradhan42611e02018-11-27 14:04:02 -0800511 DeviceResetEntry(uint32_t sequenceNum, nsecs_t eventTime, int32_t deviceId);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800512 virtual void appendDescription(std::string& msg) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800513
514 protected:
515 virtual ~DeviceResetEntry();
516 };
517
518 struct KeyEntry : EventEntry {
519 int32_t deviceId;
520 uint32_t source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100521 int32_t displayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800522 int32_t action;
523 int32_t flags;
524 int32_t keyCode;
525 int32_t scanCode;
526 int32_t metaState;
527 int32_t repeatCount;
528 nsecs_t downTime;
529
530 bool syntheticRepeat; // set to true for synthetic key repeats
531
532 enum InterceptKeyResult {
533 INTERCEPT_KEY_RESULT_UNKNOWN,
534 INTERCEPT_KEY_RESULT_SKIP,
535 INTERCEPT_KEY_RESULT_CONTINUE,
536 INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER,
537 };
538 InterceptKeyResult interceptKeyResult; // set based on the interception result
539 nsecs_t interceptKeyWakeupTime; // used with INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER
540
Prabir Pradhan42611e02018-11-27 14:04:02 -0800541 KeyEntry(uint32_t sequenceNum, nsecs_t eventTime,
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100542 int32_t deviceId, uint32_t source, int32_t displayId, uint32_t policyFlags,
543 int32_t action, int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800544 int32_t repeatCount, nsecs_t downTime);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800545 virtual void appendDescription(std::string& msg) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800546 void recycle();
547
548 protected:
549 virtual ~KeyEntry();
550 };
551
552 struct MotionEntry : EventEntry {
553 nsecs_t eventTime;
554 int32_t deviceId;
555 uint32_t source;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800556 int32_t displayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800557 int32_t action;
Michael Wright7b159c92015-05-14 14:48:03 +0100558 int32_t actionButton;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800559 int32_t flags;
560 int32_t metaState;
561 int32_t buttonState;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800562 MotionClassification classification;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800563 int32_t edgeFlags;
564 float xPrecision;
565 float yPrecision;
Garfield Tan00f511d2019-06-12 16:55:40 -0700566 float xCursorPosition;
567 float yCursorPosition;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800568 nsecs_t downTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800569 uint32_t pointerCount;
570 PointerProperties pointerProperties[MAX_POINTERS];
571 PointerCoords pointerCoords[MAX_POINTERS];
572
Garfield Tan00f511d2019-06-12 16:55:40 -0700573 MotionEntry(uint32_t sequenceNum, nsecs_t eventTime, int32_t deviceId, uint32_t source,
574 int32_t displayId, uint32_t policyFlags, int32_t action, int32_t actionButton,
575 int32_t flags, int32_t metaState, int32_t buttonState,
576 MotionClassification classification, int32_t edgeFlags, float xPrecision,
577 float yPrecision, float xCursorPosition, float yCursorPosition,
578 nsecs_t downTime, uint32_t pointerCount,
579 const PointerProperties* pointerProperties, const PointerCoords* pointerCoords,
580 float xOffset, float yOffset);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800581 virtual void appendDescription(std::string& msg) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800582
583 protected:
584 virtual ~MotionEntry();
585 };
586
587 // Tracks the progress of dispatching a particular event to a particular connection.
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -0700588 struct DispatchEntry {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800589 const uint32_t seq; // unique sequence number, never 0
590
591 EventEntry* eventEntry; // the event to dispatch
592 int32_t targetFlags;
593 float xOffset;
594 float yOffset;
Robert Carre07e1032018-11-26 12:55:53 -0800595 float globalScaleFactor;
596 float windowXScale = 1.0f;
597 float windowYScale = 1.0f;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800598 nsecs_t deliveryTime; // time when the event was actually delivered
599
600 // Set to the resolved action and flags when the event is enqueued.
601 int32_t resolvedAction;
602 int32_t resolvedFlags;
603
604 DispatchEntry(EventEntry* eventEntry,
Robert Carre07e1032018-11-26 12:55:53 -0800605 int32_t targetFlags, float xOffset, float yOffset,
606 float globalScaleFactor, float windowXScale, float windowYScale);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800607 ~DispatchEntry();
608
609 inline bool hasForegroundTarget() const {
610 return targetFlags & InputTarget::FLAG_FOREGROUND;
611 }
612
613 inline bool isSplit() const {
614 return targetFlags & InputTarget::FLAG_SPLIT;
615 }
616
617 private:
618 static volatile int32_t sNextSeqAtomic;
619
620 static uint32_t nextSeq();
621 };
622
623 // A command entry captures state and behavior for an action to be performed in the
624 // dispatch loop after the initial processing has taken place. It is essentially
625 // a kind of continuation used to postpone sensitive policy interactions to a point
626 // in the dispatch loop where it is safe to release the lock (generally after finishing
627 // the critical parts of the dispatch cycle).
628 //
629 // The special thing about commands is that they can voluntarily release and reacquire
630 // the dispatcher lock at will. Initially when the command starts running, the
631 // dispatcher lock is held. However, if the command needs to call into the policy to
632 // do some work, it can release the lock, do the work, then reacquire the lock again
633 // before returning.
634 //
635 // This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch
636 // never calls into the policy while holding its lock.
637 //
638 // Commands are implicitly 'LockedInterruptible'.
639 struct CommandEntry;
Siarhei Vishniakou49a350a2019-07-26 18:44:23 -0700640 typedef std::function<void(InputDispatcher&, CommandEntry*)> Command;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800641
642 class Connection;
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -0700643 struct CommandEntry {
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700644 explicit CommandEntry(Command command);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800645 ~CommandEntry();
646
647 Command command;
648
649 // parameters for the command (usage varies by command)
650 sp<Connection> connection;
651 nsecs_t eventTime;
652 KeyEntry* keyEntry;
653 sp<InputApplicationHandle> inputApplicationHandle;
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800654 std::string reason;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800655 int32_t userActivityEventType;
656 uint32_t seq;
657 bool handled;
Robert Carr803535b2018-08-02 16:38:15 -0700658 sp<InputChannel> inputChannel;
chaviw0c06c6e2019-01-09 13:27:07 -0800659 sp<IBinder> oldToken;
660 sp<IBinder> newToken;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800661 };
662
Michael Wrightd02c5b62014-02-10 15:10:22 -0800663 /* Specifies which events are to be canceled and why. */
664 struct CancelationOptions {
665 enum Mode {
666 CANCEL_ALL_EVENTS = 0,
667 CANCEL_POINTER_EVENTS = 1,
668 CANCEL_NON_POINTER_EVENTS = 2,
669 CANCEL_FALLBACK_EVENTS = 3,
670 };
671
672 // The criterion to use to determine which events should be canceled.
673 Mode mode;
674
675 // Descriptive reason for the cancelation.
676 const char* reason;
677
Michael Wright3dd60e22019-03-27 22:06:44 +0000678 // The specific keycode of the key event to cancel, or nullopt to cancel any key event.
679 std::optional<int32_t> keyCode = std::nullopt;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800680
Michael Wright3dd60e22019-03-27 22:06:44 +0000681 // The specific device id of events to cancel, or nullopt to cancel events from any device.
682 std::optional<int32_t> deviceId = std::nullopt;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800683
Michael Wright3dd60e22019-03-27 22:06:44 +0000684 // The specific display id of events to cancel, or nullopt to cancel events on any display.
685 std::optional<int32_t> displayId = std::nullopt;
686
687 CancelationOptions(Mode mode, const char* reason) : mode(mode), reason(reason) { }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800688 };
689
690 /* Tracks dispatched key and motion event state so that cancelation events can be
691 * synthesized when events are dropped. */
692 class InputState {
693 public:
694 InputState();
695 ~InputState();
696
697 // Returns true if there is no state to be canceled.
698 bool isNeutral() const;
699
700 // Returns true if the specified source is known to have received a hover enter
701 // motion event.
702 bool isHovering(int32_t deviceId, uint32_t source, int32_t displayId) const;
703
704 // Records tracking information for a key event that has just been published.
705 // Returns true if the event should be delivered, false if it is inconsistent
706 // and should be skipped.
707 bool trackKey(const KeyEntry* entry, int32_t action, int32_t flags);
708
709 // Records tracking information for a motion event that has just been published.
710 // Returns true if the event should be delivered, false if it is inconsistent
711 // and should be skipped.
712 bool trackMotion(const MotionEntry* entry, int32_t action, int32_t flags);
713
714 // Synthesizes cancelation events for the current state and resets the tracked state.
715 void synthesizeCancelationEvents(nsecs_t currentTime,
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800716 std::vector<EventEntry*>& outEvents, const CancelationOptions& options);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800717
718 // Clears the current state.
719 void clear();
720
721 // Copies pointer-related parts of the input state to another instance.
722 void copyPointerStateTo(InputState& other) const;
723
724 // Gets the fallback key associated with a keycode.
725 // Returns -1 if none.
726 // Returns AKEYCODE_UNKNOWN if we are only dispatching the unhandled key to the policy.
727 int32_t getFallbackKey(int32_t originalKeyCode);
728
729 // Sets the fallback key for a particular keycode.
730 void setFallbackKey(int32_t originalKeyCode, int32_t fallbackKeyCode);
731
732 // Removes the fallback key for a particular keycode.
733 void removeFallbackKey(int32_t originalKeyCode);
734
735 inline const KeyedVector<int32_t, int32_t>& getFallbackKeys() const {
736 return mFallbackKeys;
737 }
738
739 private:
740 struct KeyMemento {
741 int32_t deviceId;
742 uint32_t source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100743 int32_t displayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800744 int32_t keyCode;
745 int32_t scanCode;
746 int32_t metaState;
747 int32_t flags;
748 nsecs_t downTime;
749 uint32_t policyFlags;
750 };
751
752 struct MotionMemento {
753 int32_t deviceId;
754 uint32_t source;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800755 int32_t displayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800756 int32_t flags;
757 float xPrecision;
758 float yPrecision;
Garfield Tan00f511d2019-06-12 16:55:40 -0700759 float xCursorPosition;
760 float yCursorPosition;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800761 nsecs_t downTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800762 uint32_t pointerCount;
763 PointerProperties pointerProperties[MAX_POINTERS];
764 PointerCoords pointerCoords[MAX_POINTERS];
765 bool hovering;
766 uint32_t policyFlags;
767
768 void setPointers(const MotionEntry* entry);
769 };
770
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800771 std::vector<KeyMemento> mKeyMementos;
772 std::vector<MotionMemento> mMotionMementos;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800773 KeyedVector<int32_t, int32_t> mFallbackKeys;
774
775 ssize_t findKeyMemento(const KeyEntry* entry) const;
776 ssize_t findMotionMemento(const MotionEntry* entry, bool hovering) const;
777
778 void addKeyMemento(const KeyEntry* entry, int32_t flags);
779 void addMotionMemento(const MotionEntry* entry, int32_t flags, bool hovering);
780
781 static bool shouldCancelKey(const KeyMemento& memento,
782 const CancelationOptions& options);
783 static bool shouldCancelMotion(const MotionMemento& memento,
784 const CancelationOptions& options);
785 };
786
787 /* Manages the dispatch state associated with a single input channel. */
788 class Connection : public RefBase {
789 protected:
790 virtual ~Connection();
791
792 public:
793 enum Status {
794 // Everything is peachy.
795 STATUS_NORMAL,
796 // An unrecoverable communication error has occurred.
797 STATUS_BROKEN,
798 // The input channel has been unregistered.
799 STATUS_ZOMBIE
800 };
801
802 Status status;
803 sp<InputChannel> inputChannel; // never null
Michael Wrightd02c5b62014-02-10 15:10:22 -0800804 bool monitor;
805 InputPublisher inputPublisher;
806 InputState inputState;
807
808 // True if the socket is full and no further events can be published until
809 // the application consumes some of the input.
810 bool inputPublisherBlocked;
811
812 // Queue of events that need to be published to the connection.
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -0700813 std::deque<DispatchEntry*> outboundQueue;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800814
815 // Queue of events that have been published to the connection but that have not
816 // yet received a "finished" response from the application.
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -0700817 std::deque<DispatchEntry*> waitQueue;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800818
Robert Carr803535b2018-08-02 16:38:15 -0700819 explicit Connection(const sp<InputChannel>& inputChannel, bool monitor);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800820
Siarhei Vishniakou587c3f02018-01-04 11:46:44 -0800821 inline const std::string getInputChannelName() const { return inputChannel->getName(); }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800822
Siarhei Vishniakou587c3f02018-01-04 11:46:44 -0800823 const std::string getWindowName() const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800824 const char* getStatusLabel() const;
825
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -0700826 std::deque<DispatchEntry*>::iterator findWaitQueueEntry(uint32_t seq);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800827 };
828
Michael Wright3dd60e22019-03-27 22:06:44 +0000829 struct Monitor {
830 sp<InputChannel> inputChannel; // never null
831
832 explicit Monitor(const sp<InputChannel>& inputChannel);
833 };
834
Michael Wrightd02c5b62014-02-10 15:10:22 -0800835 enum DropReason {
836 DROP_REASON_NOT_DROPPED = 0,
837 DROP_REASON_POLICY = 1,
838 DROP_REASON_APP_SWITCH = 2,
839 DROP_REASON_DISABLED = 3,
840 DROP_REASON_BLOCKED = 4,
841 DROP_REASON_STALE = 5,
842 };
843
844 sp<InputDispatcherPolicyInterface> mPolicy;
845 InputDispatcherConfiguration mConfig;
846
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800847 std::mutex mLock;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800848
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800849 std::condition_variable mDispatcherIsAlive;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800850
851 sp<Looper> mLooper;
852
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800853 EventEntry* mPendingEvent GUARDED_BY(mLock);
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -0700854 std::deque<EventEntry*> mInboundQueue GUARDED_BY(mLock);
855 std::deque<EventEntry*> mRecentQueue GUARDED_BY(mLock);
856 std::deque<CommandEntry*> mCommandQueue GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800857
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800858 DropReason mLastDropReason GUARDED_BY(mLock);
Michael Wright3a981722015-06-10 15:26:13 +0100859
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800860 void dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800861
862 // Enqueues an inbound event. Returns true if mLooper->wake() should be called.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800863 bool enqueueInboundEventLocked(EventEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800864
865 // Cleans up input state when dropping an inbound event.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800866 void dropInboundEventLocked(EventEntry* entry, DropReason dropReason) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800867
868 // Adds an event to a queue of recent events for debugging purposes.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800869 void addRecentEventLocked(EventEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800870
871 // App switch latency optimization.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800872 bool mAppSwitchSawKeyDown GUARDED_BY(mLock);
873 nsecs_t mAppSwitchDueTime GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800874
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800875 bool isAppSwitchKeyEvent(KeyEntry* keyEntry);
876 bool isAppSwitchPendingLocked() REQUIRES(mLock);
877 void resetPendingAppSwitchLocked(bool handled) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800878
879 // Stale event latency optimization.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800880 static bool isStaleEvent(nsecs_t currentTime, EventEntry* entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800881
882 // Blocked event latency optimization. Drops old events when the user intends
883 // to transfer focus to a new application.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800884 EventEntry* mNextUnblockedEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800885
Tiger Huang85b8c5e2019-01-17 18:34:54 +0800886 sp<InputWindowHandle> findTouchedWindowAtLocked(int32_t displayId, int32_t x, int32_t y,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800887 bool addOutsideTargets = false, bool addPortalWindows = false) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800888
889 // All registered connections mapped by channel file descriptor.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800890 KeyedVector<int, sp<Connection> > mConnectionsByFd GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800891
Robert Carr5c8a0262018-10-03 16:30:44 -0700892 struct IBinderHash {
893 std::size_t operator()(const sp<IBinder>& b) const {
894 return std::hash<IBinder *>{}(b.get());
895 }
896 };
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800897 std::unordered_map<sp<IBinder>, sp<InputChannel>, IBinderHash> mInputChannelsByToken
898 GUARDED_BY(mLock);
Robert Carr5c8a0262018-10-03 16:30:44 -0700899
Michael Wright3dd60e22019-03-27 22:06:44 +0000900 // Finds the display ID of the gesture monitor identified by the provided token.
901 std::optional<int32_t> findGestureMonitorDisplayByTokenLocked(const sp<IBinder>& token)
902 REQUIRES(mLock);
903
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800904 ssize_t getConnectionIndexLocked(const sp<InputChannel>& inputChannel) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800905
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800906 // Input channels that will receive a copy of all input events sent to the provided display.
Michael Wright3dd60e22019-03-27 22:06:44 +0000907 std::unordered_map<int32_t, std::vector<Monitor>> mGlobalMonitorsByDisplay
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800908 GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800909
Michael Wright3dd60e22019-03-27 22:06:44 +0000910 // Input channels that will receive pointer events that start within the corresponding display.
911 // These are a bit special when compared to global monitors since they'll cause gesture streams
912 // to continue even when there isn't a touched window,and have the ability to steal the rest of
913 // the pointer stream in order to claim it for a system gesture.
914 std::unordered_map<int32_t, std::vector<Monitor>> mGestureMonitorsByDisplay
915 GUARDED_BY(mLock);
916
917
Michael Wrightd02c5b62014-02-10 15:10:22 -0800918 // Event injection and synchronization.
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800919 std::condition_variable mInjectionResultAvailable;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800920 bool hasInjectionPermission(int32_t injectorPid, int32_t injectorUid);
Siarhei Vishniakou62683e82019-03-06 17:59:56 -0800921 void setInjectionResult(EventEntry* entry, int32_t injectionResult);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800922
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800923 std::condition_variable mInjectionSyncFinished;
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800924 void incrementPendingForegroundDispatches(EventEntry* entry);
Siarhei Vishniakou62683e82019-03-06 17:59:56 -0800925 void decrementPendingForegroundDispatches(EventEntry* entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800926
927 // Key repeat tracking.
928 struct KeyRepeatState {
929 KeyEntry* lastKeyEntry; // or null if no repeat
930 nsecs_t nextRepeatTime;
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800931 } mKeyRepeatState GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800932
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800933 void resetKeyRepeatLocked() REQUIRES(mLock);
934 KeyEntry* synthesizeKeyRepeatLocked(nsecs_t currentTime) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800935
Michael Wright78f24442014-08-06 15:55:28 -0700936 // Key replacement tracking
937 struct KeyReplacement {
938 int32_t keyCode;
939 int32_t deviceId;
940 bool operator==(const KeyReplacement& rhs) const {
941 return keyCode == rhs.keyCode && deviceId == rhs.deviceId;
942 }
943 bool operator<(const KeyReplacement& rhs) const {
944 return keyCode != rhs.keyCode ? keyCode < rhs.keyCode : deviceId < rhs.deviceId;
945 }
946 };
947 // Maps the key code replaced, device id tuple to the key code it was replaced with
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800948 KeyedVector<KeyReplacement, int32_t> mReplacedKeys GUARDED_BY(mLock);
Siarhei Vishniakou61fafdd2018-04-13 11:00:58 -0500949 // Process certain Meta + Key combinations
950 void accelerateMetaShortcuts(const int32_t deviceId, const int32_t action,
951 int32_t& keyCode, int32_t& metaState);
Michael Wright78f24442014-08-06 15:55:28 -0700952
Michael Wrightd02c5b62014-02-10 15:10:22 -0800953 // Deferred command processing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800954 bool haveCommandsLocked() const REQUIRES(mLock);
955 bool runCommandsLockedInterruptible() REQUIRES(mLock);
956 CommandEntry* postCommandLocked(Command command) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800957
958 // Input filter processing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800959 bool shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) REQUIRES(mLock);
960 bool shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800961
962 // Inbound event processing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800963 void drainInboundQueueLocked() REQUIRES(mLock);
964 void releasePendingEventLocked() REQUIRES(mLock);
965 void releaseInboundEventLocked(EventEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800966
967 // Dispatch state.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800968 bool mDispatchEnabled GUARDED_BY(mLock);
969 bool mDispatchFrozen GUARDED_BY(mLock);
970 bool mInputFilterEnabled GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800971
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800972 std::unordered_map<int32_t, std::vector<sp<InputWindowHandle>>> mWindowHandlesByDisplay
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800973 GUARDED_BY(mLock);
Arthur Hungb92218b2018-08-14 12:00:21 +0800974 // Get window handles by display, return an empty vector if not found.
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800975 std::vector<sp<InputWindowHandle>> getWindowHandlesLocked(int32_t displayId) const
976 REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800977 sp<InputWindowHandle> getWindowHandleLocked(const sp<IBinder>& windowHandleToken) const
978 REQUIRES(mLock);
979 sp<InputChannel> getInputChannelLocked(const sp<IBinder>& windowToken) const REQUIRES(mLock);
980 bool hasWindowHandleLocked(const sp<InputWindowHandle>& windowHandle) const REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800981
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -0700982 /*
983 * Validate and update InputWindowHandles for a given display.
984 */
985 void updateWindowHandlesForDisplayLocked(
986 const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId)
987 REQUIRES(mLock);
988
Michael Wrightd02c5b62014-02-10 15:10:22 -0800989 // Focus tracking for keys, trackball, etc.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800990 std::unordered_map<int32_t, sp<InputWindowHandle>> mFocusedWindowHandlesByDisplay
991 GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800992
993 // Focus tracking for touch.
994 struct TouchedWindow {
995 sp<InputWindowHandle> windowHandle;
996 int32_t targetFlags;
997 BitSet32 pointerIds; // zero unless target flag FLAG_SPLIT is set
998 };
Michael Wright3dd60e22019-03-27 22:06:44 +0000999
1000 // For tracking the offsets we need to apply when adding gesture monitor targets.
1001 struct TouchedMonitor {
1002 Monitor monitor;
1003 float xOffset = 0.f;
1004 float yOffset = 0.f;
1005
1006 explicit TouchedMonitor(const Monitor& monitor, float xOffset, float yOffset);
1007 };
1008
Michael Wrightd02c5b62014-02-10 15:10:22 -08001009 struct TouchState {
1010 bool down;
1011 bool split;
1012 int32_t deviceId; // id of the device that is currently down, others are rejected
1013 uint32_t source; // source of the device that is current down, others are rejected
1014 int32_t displayId; // id to the display that currently has a touch, others are rejected
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001015 std::vector<TouchedWindow> windows;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001016
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001017 // This collects the portal windows that the touch has gone through. Each portal window
1018 // targets a display (embedded display for most cases). With this info, we can add the
1019 // monitoring channels of the displays touched.
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001020 std::vector<sp<InputWindowHandle>> portalWindows;
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001021
Michael Wright3dd60e22019-03-27 22:06:44 +00001022 std::vector<TouchedMonitor> gestureMonitors;
1023
Michael Wrightd02c5b62014-02-10 15:10:22 -08001024 TouchState();
1025 ~TouchState();
1026 void reset();
1027 void copyFrom(const TouchState& other);
1028 void addOrUpdateWindow(const sp<InputWindowHandle>& windowHandle,
1029 int32_t targetFlags, BitSet32 pointerIds);
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001030 void addPortalWindow(const sp<InputWindowHandle>& windowHandle);
Michael Wright3dd60e22019-03-27 22:06:44 +00001031 void addGestureMonitors(const std::vector<TouchedMonitor>& monitors);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001032 void removeWindow(const sp<InputWindowHandle>& windowHandle);
Robert Carr803535b2018-08-02 16:38:15 -07001033 void removeWindowByToken(const sp<IBinder>& token);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001034 void filterNonAsIsTouchWindows();
Michael Wright3dd60e22019-03-27 22:06:44 +00001035 void filterNonMonitors();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001036 sp<InputWindowHandle> getFirstForegroundWindowHandle() const;
1037 bool isSlippery() const;
1038 };
1039
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001040 KeyedVector<int32_t, TouchState> mTouchStatesByDisplay GUARDED_BY(mLock);
1041 TouchState mTempTouchState GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001042
Tiger Huang721e26f2018-07-24 22:26:19 +08001043 // Focused applications.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001044 std::unordered_map<int32_t, sp<InputApplicationHandle>> mFocusedApplicationHandlesByDisplay
1045 GUARDED_BY(mLock);
Tiger Huang721e26f2018-07-24 22:26:19 +08001046
1047 // Top focused display.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001048 int32_t mFocusedDisplayId GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001049
1050 // Dispatcher state at time of last ANR.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001051 std::string mLastANRState GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001052
1053 // Dispatch inbound events.
1054 bool dispatchConfigurationChangedLocked(
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001055 nsecs_t currentTime, ConfigurationChangedEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001056 bool dispatchDeviceResetLocked(
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001057 nsecs_t currentTime, DeviceResetEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001058 bool dispatchKeyLocked(
1059 nsecs_t currentTime, KeyEntry* entry,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001060 DropReason* dropReason, nsecs_t* nextWakeupTime) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001061 bool dispatchMotionLocked(
1062 nsecs_t currentTime, MotionEntry* entry,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001063 DropReason* dropReason, nsecs_t* nextWakeupTime) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001064 void dispatchEventLocked(nsecs_t currentTime, EventEntry* entry,
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001065 const std::vector<InputTarget>& inputTargets) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001066
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001067 void logOutboundKeyDetails(const char* prefix, const KeyEntry* entry);
1068 void logOutboundMotionDetails(const char* prefix, const MotionEntry* entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001069
1070 // Keeping track of ANR timeouts.
1071 enum InputTargetWaitCause {
1072 INPUT_TARGET_WAIT_CAUSE_NONE,
1073 INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY,
1074 INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY,
1075 };
1076
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001077 InputTargetWaitCause mInputTargetWaitCause GUARDED_BY(mLock);
1078 nsecs_t mInputTargetWaitStartTime GUARDED_BY(mLock);
1079 nsecs_t mInputTargetWaitTimeoutTime GUARDED_BY(mLock);
1080 bool mInputTargetWaitTimeoutExpired GUARDED_BY(mLock);
1081 sp<IBinder> mInputTargetWaitApplicationToken GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001082
1083 // Contains the last window which received a hover event.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001084 sp<InputWindowHandle> mLastHoverWindowHandle GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001085
1086 // Finding targets for input events.
1087 int32_t handleTargetsNotReadyLocked(nsecs_t currentTime, const EventEntry* entry,
1088 const sp<InputApplicationHandle>& applicationHandle,
1089 const sp<InputWindowHandle>& windowHandle,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001090 nsecs_t* nextWakeupTime, const char* reason) REQUIRES(mLock);
Robert Carr803535b2018-08-02 16:38:15 -07001091
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001092 void removeWindowByTokenLocked(const sp<IBinder>& token) REQUIRES(mLock);
Robert Carr803535b2018-08-02 16:38:15 -07001093
Michael Wrightd02c5b62014-02-10 15:10:22 -08001094 void resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001095 const sp<InputChannel>& inputChannel) REQUIRES(mLock);
1096 nsecs_t getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime) REQUIRES(mLock);
1097 void resetANRTimeoutsLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001098
Tiger Huang721e26f2018-07-24 22:26:19 +08001099 int32_t getTargetDisplayId(const EventEntry* entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001100 int32_t findFocusedWindowTargetsLocked(nsecs_t currentTime, const EventEntry* entry,
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001101 std::vector<InputTarget>& inputTargets, nsecs_t* nextWakeupTime) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001102 int32_t findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry* entry,
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001103 std::vector<InputTarget>& inputTargets, nsecs_t* nextWakeupTime,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001104 bool* outConflictingPointerActions) REQUIRES(mLock);
Michael Wright3dd60e22019-03-27 22:06:44 +00001105 std::vector<TouchedMonitor> findTouchedGestureMonitorsLocked(int32_t displayId,
1106 const std::vector<sp<InputWindowHandle>>& portalWindows) REQUIRES(mLock);
1107 void addGestureMonitors(const std::vector<Monitor>& monitors,
1108 std::vector<TouchedMonitor>& outTouchedMonitors, float xOffset = 0, float yOffset = 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001109
1110 void addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle,
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001111 int32_t targetFlags, BitSet32 pointerIds, std::vector<InputTarget>& inputTargets)
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001112 REQUIRES(mLock);
Michael Wright3dd60e22019-03-27 22:06:44 +00001113 void addMonitoringTargetLocked(const Monitor& monitor, float xOffset, float yOffset,
1114 std::vector<InputTarget>& inputTargets) REQUIRES(mLock);
1115 void addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets,
1116 int32_t displayId, float xOffset = 0, float yOffset = 0) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001117
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001118 void pokeUserActivityLocked(const EventEntry* eventEntry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001119 bool checkInjectionPermission(const sp<InputWindowHandle>& windowHandle,
1120 const InjectionState* injectionState);
1121 bool isWindowObscuredAtPointLocked(const sp<InputWindowHandle>& windowHandle,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001122 int32_t x, int32_t y) const REQUIRES(mLock);
1123 bool isWindowObscuredLocked(const sp<InputWindowHandle>& windowHandle) const REQUIRES(mLock);
1124 std::string getApplicationWindowLabel(const sp<InputApplicationHandle>& applicationHandle,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001125 const sp<InputWindowHandle>& windowHandle);
1126
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001127 std::string checkWindowReadyForMoreInputLocked(nsecs_t currentTime,
Jeff Brownffb49772014-10-10 19:01:34 -07001128 const sp<InputWindowHandle>& windowHandle, const EventEntry* eventEntry,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001129 const char* targetType) REQUIRES(mLock);
Jeff Brownffb49772014-10-10 19:01:34 -07001130
Michael Wrightd02c5b62014-02-10 15:10:22 -08001131 // Manage the dispatch cycle for a single connection.
1132 // These methods are deliberately not Interruptible because doing all of the work
1133 // with the mutex held makes it easier to ensure that connection invariants are maintained.
1134 // If needed, the methods post commands to run later once the critical bits are done.
1135 void prepareDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001136 EventEntry* eventEntry, const InputTarget* inputTarget) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001137 void enqueueDispatchEntriesLocked(nsecs_t currentTime, const sp<Connection>& connection,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001138 EventEntry* eventEntry, const InputTarget* inputTarget) REQUIRES(mLock);
chaviw8c9cf542019-03-25 13:02:48 -07001139 void enqueueDispatchEntryLocked(const sp<Connection>& connection,
1140 EventEntry* eventEntry, const InputTarget* inputTarget, int32_t dispatchMode)
1141 REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001142 void startDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection)
1143 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001144 void finishDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001145 uint32_t seq, bool handled) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001146 void abortBrokenDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001147 bool notify) REQUIRES(mLock);
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -07001148 void drainDispatchQueue(std::deque<DispatchEntry*>& queue);
Siarhei Vishniakou62683e82019-03-06 17:59:56 -08001149 void releaseDispatchEntry(DispatchEntry* dispatchEntry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001150 static int handleReceiveCallback(int fd, int events, void* data);
chaviw8c9cf542019-03-25 13:02:48 -07001151 // The action sent should only be of type AMOTION_EVENT_*
chaviwfd6d3512019-03-25 13:23:49 -07001152 void dispatchPointerDownOutsideFocus(uint32_t source, int32_t action,
chaviw8c9cf542019-03-25 13:02:48 -07001153 const sp<IBinder>& newToken) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001154
1155 void synthesizeCancelationEventsForAllConnectionsLocked(
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001156 const CancelationOptions& options) REQUIRES(mLock);
1157 void synthesizeCancelationEventsForMonitorsLocked(
1158 const CancelationOptions& options) REQUIRES(mLock);
Michael Wright3dd60e22019-03-27 22:06:44 +00001159 void synthesizeCancelationEventsForMonitorsLocked(const CancelationOptions& options,
1160 std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001161 void synthesizeCancelationEventsForInputChannelLocked(const sp<InputChannel>& channel,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001162 const CancelationOptions& options) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001163 void synthesizeCancelationEventsForConnectionLocked(const sp<Connection>& connection,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001164 const CancelationOptions& options) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001165
1166 // Splitting motion events across windows.
1167 MotionEntry* splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds);
1168
1169 // Reset and drop everything the dispatcher is doing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001170 void resetAndDropEverythingLocked(const char* reason) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001171
1172 // Dump state.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001173 void dumpDispatchStateLocked(std::string& dump) REQUIRES(mLock);
Michael Wright3dd60e22019-03-27 22:06:44 +00001174 void dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001175 void logDispatchStateLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001176
1177 // Registration.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001178 void removeMonitorChannelLocked(const sp<InputChannel>& inputChannel) REQUIRES(mLock);
Michael Wright3dd60e22019-03-27 22:06:44 +00001179 void removeMonitorChannelLocked(const sp<InputChannel>& inputChannel,
1180 std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay)
1181 REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001182 status_t unregisterInputChannelLocked(const sp<InputChannel>& inputChannel, bool notify)
1183 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001184
1185 // Interesting events that we might like to log or tell the framework about.
1186 void onDispatchCycleFinishedLocked(
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001187 nsecs_t currentTime, const sp<Connection>& connection, uint32_t seq, bool handled)
1188 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001189 void onDispatchCycleBrokenLocked(
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001190 nsecs_t currentTime, const sp<Connection>& connection) REQUIRES(mLock);
chaviw0c06c6e2019-01-09 13:27:07 -08001191 void onFocusChangedLocked(const sp<InputWindowHandle>& oldFocus,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001192 const sp<InputWindowHandle>& newFocus) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001193 void onANRLocked(
1194 nsecs_t currentTime, const sp<InputApplicationHandle>& applicationHandle,
1195 const sp<InputWindowHandle>& windowHandle,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001196 nsecs_t eventTime, nsecs_t waitStartTime, const char* reason) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001197
1198 // Outbound policy interactions.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001199 void doNotifyConfigurationChangedLockedInterruptible(CommandEntry* commandEntry)
1200 REQUIRES(mLock);
1201 void doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
1202 void doNotifyFocusChangedLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
1203 void doNotifyANRLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
1204 void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry)
1205 REQUIRES(mLock);
1206 void doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001207 bool afterKeyEventLockedInterruptible(const sp<Connection>& connection,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001208 DispatchEntry* dispatchEntry, KeyEntry* keyEntry, bool handled) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001209 bool afterMotionEventLockedInterruptible(const sp<Connection>& connection,
Siarhei Vishniakou62683e82019-03-06 17:59:56 -08001210 DispatchEntry* dispatchEntry, MotionEntry* motionEntry, bool handled) REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001211 void doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001212 void initializeKeyEvent(KeyEvent* event, const KeyEntry* entry);
chaviwfd6d3512019-03-25 13:23:49 -07001213 void doOnPointerDownOutsideFocusLockedInterruptible(CommandEntry* commandEntry)
1214 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001215
1216 // Statistics gathering.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001217 void updateDispatchStatistics(nsecs_t currentTime, const EventEntry* entry,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001218 int32_t injectionResult, nsecs_t timeSpentWaitingForApplication);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001219 void traceInboundQueueLengthLocked() REQUIRES(mLock);
1220 void traceOutboundQueueLength(const sp<Connection>& connection);
1221 void traceWaitQueueLength(const sp<Connection>& connection);
Prabir Pradhanf93562f2018-11-29 12:13:37 -08001222
Prabir Pradhan79a4f0c2019-01-09 11:24:01 -08001223 sp<InputReporterInterface> mReporter;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001224};
1225
1226/* Enqueues and dispatches input events, endlessly. */
1227class InputDispatcherThread : public Thread {
1228public:
1229 explicit InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher);
1230 ~InputDispatcherThread();
1231
1232private:
1233 virtual bool threadLoop();
1234
1235 sp<InputDispatcherInterface> mDispatcher;
1236};
1237
1238} // namespace android
1239
1240#endif // _UI_INPUT_DISPATCHER_H