blob: 65d6d67a78022dcd3a866d09db283965108da9a8 [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
36#include <stddef.h>
37#include <unistd.h>
38#include <limits.h>
Arthur Hungb92218b2018-08-14 12:00:21 +080039#include <unordered_map>
Michael Wrightd02c5b62014-02-10 15:10:22 -080040
Michael Wrightd02c5b62014-02-10 15:10:22 -080041#include "InputListener.h"
Prabir Pradhan79a4f0c2019-01-09 11:24:01 -080042#include "InputReporterInterface.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080043
Michael Wrightd02c5b62014-02-10 15:10:22 -080044namespace android {
45
46/*
47 * Constants used to report the outcome of input event injection.
48 */
49enum {
50 /* (INTERNAL USE ONLY) Specifies that injection is pending and its outcome is unknown. */
51 INPUT_EVENT_INJECTION_PENDING = -1,
52
53 /* Injection succeeded. */
54 INPUT_EVENT_INJECTION_SUCCEEDED = 0,
55
56 /* Injection failed because the injector did not have permission to inject
57 * into the application with input focus. */
58 INPUT_EVENT_INJECTION_PERMISSION_DENIED = 1,
59
60 /* Injection failed because there were no available input targets. */
61 INPUT_EVENT_INJECTION_FAILED = 2,
62
63 /* Injection failed due to a timeout. */
64 INPUT_EVENT_INJECTION_TIMED_OUT = 3
65};
66
67/*
68 * Constants used to determine the input event injection synchronization mode.
69 */
70enum {
71 /* Injection is asynchronous and is assumed always to be successful. */
72 INPUT_EVENT_INJECTION_SYNC_NONE = 0,
73
74 /* Waits for previous events to be dispatched so that the input dispatcher can determine
75 * whether input event injection willbe permitted based on the current input focus.
76 * Does not wait for the input event to finish processing. */
77 INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT = 1,
78
79 /* Waits for the input event to be completely processed. */
80 INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISHED = 2,
81};
82
83
84/*
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. */
139 FLAG_DISPATCH_MASK = FLAG_DISPATCH_AS_IS
140 | FLAG_DISPATCH_AS_OUTSIDE
141 | FLAG_DISPATCH_AS_HOVER_ENTER
142 | FLAG_DISPATCH_AS_HOVER_EXIT
143 | FLAG_DISPATCH_AS_SLIPPERY_EXIT
144 | FLAG_DISPATCH_AS_SLIPPERY_ENTER,
Michael Wrightcdcd8f22016-03-22 16:52:13 -0700145
146 /* This flag indicates that the target of a MotionEvent is partly or wholly
147 * obscured by another visible window above it. The motion event should be
148 * delivered with flag AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED. */
149 FLAG_WINDOW_IS_PARTIALLY_OBSCURED = 1 << 14,
150
Michael Wrightd02c5b62014-02-10 15:10:22 -0800151 };
152
153 // The input channel to be targeted.
154 sp<InputChannel> inputChannel;
155
156 // Flags for the input target.
157 int32_t flags;
158
159 // The x and y offset to add to a MotionEvent as it is delivered.
160 // (ignored for KeyEvents)
161 float xOffset, yOffset;
162
163 // Scaling factor to apply to MotionEvent as it is delivered.
164 // (ignored for KeyEvents)
Robert Carre07e1032018-11-26 12:55:53 -0800165 float globalScaleFactor;
166 float windowXScale = 1.0f;
167 float windowYScale = 1.0f;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800168
169 // The subset of pointer ids to include in motion events dispatched to this input target
170 // if FLAG_SPLIT is set.
171 BitSet32 pointerIds;
172};
173
174
175/*
176 * Input dispatcher configuration.
177 *
178 * Specifies various options that modify the behavior of the input dispatcher.
179 * The values provided here are merely defaults. The actual values will come from ViewConfiguration
180 * and are passed into the dispatcher during initialization.
181 */
182struct InputDispatcherConfiguration {
183 // The key repeat initial timeout.
184 nsecs_t keyRepeatTimeout;
185
186 // The key repeat inter-key delay.
187 nsecs_t keyRepeatDelay;
188
189 InputDispatcherConfiguration() :
190 keyRepeatTimeout(500 * 1000000LL),
191 keyRepeatDelay(50 * 1000000LL) { }
192};
193
194
195/*
196 * Input dispatcher policy interface.
197 *
198 * The input reader policy is used by the input reader to interact with the Window Manager
199 * and other system components.
200 *
201 * The actual implementation is partially supported by callbacks into the DVM
202 * via JNI. This interface is also mocked in the unit tests.
203 */
204class InputDispatcherPolicyInterface : public virtual RefBase {
205protected:
206 InputDispatcherPolicyInterface() { }
207 virtual ~InputDispatcherPolicyInterface() { }
208
209public:
210 /* Notifies the system that a configuration change has occurred. */
211 virtual void notifyConfigurationChanged(nsecs_t when) = 0;
212
213 /* Notifies the system that an application is not responding.
214 * Returns a new timeout to continue waiting, or 0 to abort dispatch. */
215 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Robert Carr803535b2018-08-02 16:38:15 -0700216 const sp<IBinder>& token,
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800217 const std::string& reason) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800218
219 /* Notifies the system that an input channel is unrecoverably broken. */
Robert Carr803535b2018-08-02 16:38:15 -0700220 virtual void notifyInputChannelBroken(const sp<IBinder>& token) = 0;
chaviw0c06c6e2019-01-09 13:27:07 -0800221 virtual void notifyFocusChanged(const sp<IBinder>& oldToken, const sp<IBinder>& newToken) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800222
223 /* Gets the input dispatcher configuration. */
224 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) = 0;
225
Michael Wrightd02c5b62014-02-10 15:10:22 -0800226 /* Filters an input event.
227 * Return true to dispatch the event unmodified, false to consume the event.
228 * A filter can also transform and inject events later by passing POLICY_FLAG_FILTERED
229 * to injectInputEvent.
230 */
231 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) = 0;
232
233 /* Intercepts a key event immediately before queueing it.
234 * The policy can use this method as an opportunity to perform power management functions
235 * and early event preprocessing such as updating policy flags.
236 *
237 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
238 * should be dispatched to applications.
239 */
240 virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags) = 0;
241
242 /* Intercepts a touch, trackball or other motion event before queueing it.
243 * The policy can use this method as an opportunity to perform power management functions
244 * and early event preprocessing such as updating policy flags.
245 *
246 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
247 * should be dispatched to applications.
248 */
Charles Chen3611f1f2019-01-29 17:26:18 +0800249 virtual void interceptMotionBeforeQueueing(const int32_t displayId, nsecs_t when,
250 uint32_t& policyFlags) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800251
252 /* Allows the policy a chance to intercept a key before dispatching. */
Robert Carr803535b2018-08-02 16:38:15 -0700253 virtual nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>& token,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800254 const KeyEvent* keyEvent, uint32_t policyFlags) = 0;
255
256 /* Allows the policy a chance to perform default processing for an unhandled key.
257 * Returns an alternate keycode to redispatch as a fallback, or 0 to give up. */
Robert Carr803535b2018-08-02 16:38:15 -0700258 virtual bool dispatchUnhandledKey(const sp<IBinder>& token,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800259 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) = 0;
260
261 /* Notifies the policy about switch events.
262 */
263 virtual void notifySwitch(nsecs_t when,
264 uint32_t switchValues, uint32_t switchMask, uint32_t policyFlags) = 0;
265
266 /* Poke user activity for an event dispatched to a window. */
267 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType) = 0;
268
269 /* Checks whether a given application pid/uid has permission to inject input events
270 * into other applications.
271 *
272 * This method is special in that its implementation promises to be non-reentrant and
273 * is safe to call while holding other locks. (Most other methods make no such guarantees!)
274 */
275 virtual bool checkInjectEventsPermissionNonReentrant(
276 int32_t injectorPid, int32_t injectorUid) = 0;
277};
278
279
280/* 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:
284 InputDispatcherInterface() { }
285 virtual ~InputDispatcherInterface() { }
286
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 */
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800310 virtual int32_t injectInputEvent(const InputEvent* event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800311 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,
312 uint32_t policyFlags) = 0;
313
314 /* Sets the list of input windows.
315 *
316 * This method may be called on any thread (usually by the input manager).
317 */
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800318 virtual void setInputWindows(const std::vector<sp<InputWindowHandle> >& inputWindowHandles,
chaviw291d88a2019-02-14 10:33:58 -0800319 int32_t displayId,
320 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 */
Robert Carr803535b2018-08-02 16:38:15 -0700359 virtual status_t registerInputChannel(
360 const sp<InputChannel>& inputChannel, 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 */
370 virtual status_t registerInputMonitor(
371 const sp<InputChannel>& inputChannel, int32_t displayId, bool gestureMonitor) = 0;
372
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;
384
Michael Wrightd02c5b62014-02-10 15:10:22 -0800385};
386
387/* Dispatches events to input targets. Some functions of the input dispatcher, such as
388 * identifying input targets, are controlled by a separate policy object.
389 *
390 * IMPORTANT INVARIANT:
391 * Because the policy can potentially block or cause re-entrance into the input dispatcher,
392 * the input dispatcher never calls into the policy while holding its internal locks.
393 * The implementation is also carefully designed to recover from scenarios such as an
394 * input channel becoming unregistered while identifying input targets or processing timeouts.
395 *
396 * Methods marked 'Locked' must be called with the lock acquired.
397 *
398 * Methods marked 'LockedInterruptible' must be called with the lock acquired but
399 * may during the course of their execution release the lock, call into the policy, and
400 * then reacquire the lock. The caller is responsible for recovering gracefully.
401 *
402 * A 'LockedInterruptible' method may called a 'Locked' method, but NOT vice-versa.
403 */
404class InputDispatcher : public InputDispatcherInterface {
405protected:
406 virtual ~InputDispatcher();
407
408public:
409 explicit InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy);
410
Michael Wright3dd60e22019-03-27 22:06:44 +0000411 virtual void dump(std::string& dump) override;
412 virtual void monitor() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800413
Michael Wright3dd60e22019-03-27 22:06:44 +0000414 virtual void dispatchOnce() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800415
Michael Wright3dd60e22019-03-27 22:06:44 +0000416 virtual void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) override;
417 virtual void notifyKey(const NotifyKeyArgs* args) override;
418 virtual void notifyMotion(const NotifyMotionArgs* args) override;
419 virtual void notifySwitch(const NotifySwitchArgs* args) override;
420 virtual void notifyDeviceReset(const NotifyDeviceResetArgs* args) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800421
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800422 virtual int32_t injectInputEvent(const InputEvent* event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800423 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,
Michael Wright3dd60e22019-03-27 22:06:44 +0000424 uint32_t policyFlags) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800425
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800426 virtual void setInputWindows(const std::vector<sp<InputWindowHandle> >& inputWindowHandles,
chaviw291d88a2019-02-14 10:33:58 -0800427 int32_t displayId,
Michael Wright3dd60e22019-03-27 22:06:44 +0000428 const sp<ISetInputWindowsListener>& setInputWindowsListener = nullptr) override;
Tiger Huang721e26f2018-07-24 22:26:19 +0800429 virtual void setFocusedApplication(int32_t displayId,
Michael Wright3dd60e22019-03-27 22:06:44 +0000430 const sp<InputApplicationHandle>& inputApplicationHandle) override;
431 virtual void setFocusedDisplay(int32_t displayId) override;
432 virtual void setInputDispatchMode(bool enabled, bool frozen) override;
433 virtual void setInputFilterEnabled(bool enabled) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800434
Michael Wright3dd60e22019-03-27 22:06:44 +0000435 virtual bool transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken)
436 override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800437
438 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel,
Michael Wright3dd60e22019-03-27 22:06:44 +0000439 int32_t displayId) override;
440 virtual status_t registerInputMonitor(const sp<InputChannel>& inputChannel,
441 int32_t displayId, bool isGestureMonitor) override;
442 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) override;
443 virtual status_t pilferPointers(const sp<IBinder>& token) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800444
445private:
446 template <typename T>
447 struct Link {
448 T* next;
449 T* prev;
450
451 protected:
Yi Kong9b14ac62018-07-17 13:48:38 -0700452 inline Link() : next(nullptr), prev(nullptr) { }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800453 };
454
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
471 struct EventEntry : Link<EventEntry> {
472 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;
566 nsecs_t downTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800567 uint32_t pointerCount;
568 PointerProperties pointerProperties[MAX_POINTERS];
569 PointerCoords pointerCoords[MAX_POINTERS];
570
Prabir Pradhan42611e02018-11-27 14:04:02 -0800571 MotionEntry(uint32_t sequenceNum, nsecs_t eventTime,
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800572 int32_t deviceId, uint32_t source, int32_t displayId, uint32_t policyFlags,
Michael Wright7b159c92015-05-14 14:48:03 +0100573 int32_t action, int32_t actionButton, int32_t flags,
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800574 int32_t metaState, int32_t buttonState, MotionClassification classification,
575 int32_t edgeFlags, float xPrecision, float yPrecision,
576 nsecs_t downTime, uint32_t pointerCount,
Jeff Brownf086ddb2014-02-11 14:28:48 -0800577 const PointerProperties* pointerProperties, const PointerCoords* pointerCoords,
578 float xOffset, float yOffset);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800579 virtual void appendDescription(std::string& msg) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800580
581 protected:
582 virtual ~MotionEntry();
583 };
584
585 // Tracks the progress of dispatching a particular event to a particular connection.
586 struct DispatchEntry : Link<DispatchEntry> {
587 const uint32_t seq; // unique sequence number, never 0
588
589 EventEntry* eventEntry; // the event to dispatch
590 int32_t targetFlags;
591 float xOffset;
592 float yOffset;
Robert Carre07e1032018-11-26 12:55:53 -0800593 float globalScaleFactor;
594 float windowXScale = 1.0f;
595 float windowYScale = 1.0f;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800596 nsecs_t deliveryTime; // time when the event was actually delivered
597
598 // Set to the resolved action and flags when the event is enqueued.
599 int32_t resolvedAction;
600 int32_t resolvedFlags;
601
602 DispatchEntry(EventEntry* eventEntry,
Robert Carre07e1032018-11-26 12:55:53 -0800603 int32_t targetFlags, float xOffset, float yOffset,
604 float globalScaleFactor, float windowXScale, float windowYScale);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800605 ~DispatchEntry();
606
607 inline bool hasForegroundTarget() const {
608 return targetFlags & InputTarget::FLAG_FOREGROUND;
609 }
610
611 inline bool isSplit() const {
612 return targetFlags & InputTarget::FLAG_SPLIT;
613 }
614
615 private:
616 static volatile int32_t sNextSeqAtomic;
617
618 static uint32_t nextSeq();
619 };
620
621 // A command entry captures state and behavior for an action to be performed in the
622 // dispatch loop after the initial processing has taken place. It is essentially
623 // a kind of continuation used to postpone sensitive policy interactions to a point
624 // in the dispatch loop where it is safe to release the lock (generally after finishing
625 // the critical parts of the dispatch cycle).
626 //
627 // The special thing about commands is that they can voluntarily release and reacquire
628 // the dispatcher lock at will. Initially when the command starts running, the
629 // dispatcher lock is held. However, if the command needs to call into the policy to
630 // do some work, it can release the lock, do the work, then reacquire the lock again
631 // before returning.
632 //
633 // This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch
634 // never calls into the policy while holding its lock.
635 //
636 // Commands are implicitly 'LockedInterruptible'.
637 struct CommandEntry;
638 typedef void (InputDispatcher::*Command)(CommandEntry* commandEntry);
639
640 class Connection;
641 struct CommandEntry : Link<CommandEntry> {
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700642 explicit CommandEntry(Command command);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800643 ~CommandEntry();
644
645 Command command;
646
647 // parameters for the command (usage varies by command)
648 sp<Connection> connection;
649 nsecs_t eventTime;
650 KeyEntry* keyEntry;
651 sp<InputApplicationHandle> inputApplicationHandle;
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800652 std::string reason;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800653 int32_t userActivityEventType;
654 uint32_t seq;
655 bool handled;
Robert Carr803535b2018-08-02 16:38:15 -0700656 sp<InputChannel> inputChannel;
chaviw0c06c6e2019-01-09 13:27:07 -0800657 sp<IBinder> oldToken;
658 sp<IBinder> newToken;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800659 };
660
661 // Generic queue implementation.
662 template <typename T>
663 struct Queue {
664 T* head;
665 T* tail;
Jon McCaffrey65dbe972014-11-18 12:07:08 -0800666 uint32_t entryCount;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800667
Yi Kong9b14ac62018-07-17 13:48:38 -0700668 inline Queue() : head(nullptr), tail(nullptr), entryCount(0) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800669 }
670
671 inline bool isEmpty() const {
672 return !head;
673 }
674
675 inline void enqueueAtTail(T* entry) {
Jon McCaffrey65dbe972014-11-18 12:07:08 -0800676 entryCount++;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800677 entry->prev = tail;
678 if (tail) {
679 tail->next = entry;
680 } else {
681 head = entry;
682 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700683 entry->next = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800684 tail = entry;
685 }
686
687 inline void enqueueAtHead(T* entry) {
Jon McCaffrey65dbe972014-11-18 12:07:08 -0800688 entryCount++;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800689 entry->next = head;
690 if (head) {
691 head->prev = entry;
692 } else {
693 tail = entry;
694 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700695 entry->prev = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800696 head = entry;
697 }
698
699 inline void dequeue(T* entry) {
Jon McCaffrey65dbe972014-11-18 12:07:08 -0800700 entryCount--;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800701 if (entry->prev) {
702 entry->prev->next = entry->next;
703 } else {
704 head = entry->next;
705 }
706 if (entry->next) {
707 entry->next->prev = entry->prev;
708 } else {
709 tail = entry->prev;
710 }
711 }
712
713 inline T* dequeueAtHead() {
Jon McCaffrey65dbe972014-11-18 12:07:08 -0800714 entryCount--;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800715 T* entry = head;
716 head = entry->next;
717 if (head) {
Yi Kong9b14ac62018-07-17 13:48:38 -0700718 head->prev = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800719 } else {
Yi Kong9b14ac62018-07-17 13:48:38 -0700720 tail = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800721 }
722 return entry;
723 }
724
Jon McCaffrey65dbe972014-11-18 12:07:08 -0800725 uint32_t count() const {
726 return entryCount;
727 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800728 };
729
730 /* Specifies which events are to be canceled and why. */
731 struct CancelationOptions {
732 enum Mode {
733 CANCEL_ALL_EVENTS = 0,
734 CANCEL_POINTER_EVENTS = 1,
735 CANCEL_NON_POINTER_EVENTS = 2,
736 CANCEL_FALLBACK_EVENTS = 3,
737 };
738
739 // The criterion to use to determine which events should be canceled.
740 Mode mode;
741
742 // Descriptive reason for the cancelation.
743 const char* reason;
744
Michael Wright3dd60e22019-03-27 22:06:44 +0000745 // The specific keycode of the key event to cancel, or nullopt to cancel any key event.
746 std::optional<int32_t> keyCode = std::nullopt;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800747
Michael Wright3dd60e22019-03-27 22:06:44 +0000748 // The specific device id of events to cancel, or nullopt to cancel events from any device.
749 std::optional<int32_t> deviceId = std::nullopt;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800750
Michael Wright3dd60e22019-03-27 22:06:44 +0000751 // The specific display id of events to cancel, or nullopt to cancel events on any display.
752 std::optional<int32_t> displayId = std::nullopt;
753
754 CancelationOptions(Mode mode, const char* reason) : mode(mode), reason(reason) { }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800755 };
756
757 /* Tracks dispatched key and motion event state so that cancelation events can be
758 * synthesized when events are dropped. */
759 class InputState {
760 public:
761 InputState();
762 ~InputState();
763
764 // Returns true if there is no state to be canceled.
765 bool isNeutral() const;
766
767 // Returns true if the specified source is known to have received a hover enter
768 // motion event.
769 bool isHovering(int32_t deviceId, uint32_t source, int32_t displayId) const;
770
771 // Records tracking information for a key event that has just been published.
772 // Returns true if the event should be delivered, false if it is inconsistent
773 // and should be skipped.
774 bool trackKey(const KeyEntry* entry, int32_t action, int32_t flags);
775
776 // Records tracking information for a motion event that has just been published.
777 // Returns true if the event should be delivered, false if it is inconsistent
778 // and should be skipped.
779 bool trackMotion(const MotionEntry* entry, int32_t action, int32_t flags);
780
781 // Synthesizes cancelation events for the current state and resets the tracked state.
782 void synthesizeCancelationEvents(nsecs_t currentTime,
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800783 std::vector<EventEntry*>& outEvents, const CancelationOptions& options);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800784
785 // Clears the current state.
786 void clear();
787
788 // Copies pointer-related parts of the input state to another instance.
789 void copyPointerStateTo(InputState& other) const;
790
791 // Gets the fallback key associated with a keycode.
792 // Returns -1 if none.
793 // Returns AKEYCODE_UNKNOWN if we are only dispatching the unhandled key to the policy.
794 int32_t getFallbackKey(int32_t originalKeyCode);
795
796 // Sets the fallback key for a particular keycode.
797 void setFallbackKey(int32_t originalKeyCode, int32_t fallbackKeyCode);
798
799 // Removes the fallback key for a particular keycode.
800 void removeFallbackKey(int32_t originalKeyCode);
801
802 inline const KeyedVector<int32_t, int32_t>& getFallbackKeys() const {
803 return mFallbackKeys;
804 }
805
806 private:
807 struct KeyMemento {
808 int32_t deviceId;
809 uint32_t source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100810 int32_t displayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800811 int32_t keyCode;
812 int32_t scanCode;
813 int32_t metaState;
814 int32_t flags;
815 nsecs_t downTime;
816 uint32_t policyFlags;
817 };
818
819 struct MotionMemento {
820 int32_t deviceId;
821 uint32_t source;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800822 int32_t displayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800823 int32_t flags;
824 float xPrecision;
825 float yPrecision;
826 nsecs_t downTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800827 uint32_t pointerCount;
828 PointerProperties pointerProperties[MAX_POINTERS];
829 PointerCoords pointerCoords[MAX_POINTERS];
830 bool hovering;
831 uint32_t policyFlags;
832
833 void setPointers(const MotionEntry* entry);
834 };
835
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800836 std::vector<KeyMemento> mKeyMementos;
837 std::vector<MotionMemento> mMotionMementos;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800838 KeyedVector<int32_t, int32_t> mFallbackKeys;
839
840 ssize_t findKeyMemento(const KeyEntry* entry) const;
841 ssize_t findMotionMemento(const MotionEntry* entry, bool hovering) const;
842
843 void addKeyMemento(const KeyEntry* entry, int32_t flags);
844 void addMotionMemento(const MotionEntry* entry, int32_t flags, bool hovering);
845
846 static bool shouldCancelKey(const KeyMemento& memento,
847 const CancelationOptions& options);
848 static bool shouldCancelMotion(const MotionMemento& memento,
849 const CancelationOptions& options);
850 };
851
852 /* Manages the dispatch state associated with a single input channel. */
853 class Connection : public RefBase {
854 protected:
855 virtual ~Connection();
856
857 public:
858 enum Status {
859 // Everything is peachy.
860 STATUS_NORMAL,
861 // An unrecoverable communication error has occurred.
862 STATUS_BROKEN,
863 // The input channel has been unregistered.
864 STATUS_ZOMBIE
865 };
866
867 Status status;
868 sp<InputChannel> inputChannel; // never null
Michael Wrightd02c5b62014-02-10 15:10:22 -0800869 bool monitor;
870 InputPublisher inputPublisher;
871 InputState inputState;
872
873 // True if the socket is full and no further events can be published until
874 // the application consumes some of the input.
875 bool inputPublisherBlocked;
876
877 // Queue of events that need to be published to the connection.
878 Queue<DispatchEntry> outboundQueue;
879
880 // Queue of events that have been published to the connection but that have not
881 // yet received a "finished" response from the application.
882 Queue<DispatchEntry> waitQueue;
883
Robert Carr803535b2018-08-02 16:38:15 -0700884 explicit Connection(const sp<InputChannel>& inputChannel, bool monitor);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800885
Siarhei Vishniakou587c3f02018-01-04 11:46:44 -0800886 inline const std::string getInputChannelName() const { return inputChannel->getName(); }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800887
Siarhei Vishniakou587c3f02018-01-04 11:46:44 -0800888 const std::string getWindowName() const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800889 const char* getStatusLabel() const;
890
891 DispatchEntry* findWaitQueueEntry(uint32_t seq);
892 };
893
Michael Wright3dd60e22019-03-27 22:06:44 +0000894 struct Monitor {
895 sp<InputChannel> inputChannel; // never null
896
897 explicit Monitor(const sp<InputChannel>& inputChannel);
898 };
899
Michael Wrightd02c5b62014-02-10 15:10:22 -0800900 enum DropReason {
901 DROP_REASON_NOT_DROPPED = 0,
902 DROP_REASON_POLICY = 1,
903 DROP_REASON_APP_SWITCH = 2,
904 DROP_REASON_DISABLED = 3,
905 DROP_REASON_BLOCKED = 4,
906 DROP_REASON_STALE = 5,
907 };
908
909 sp<InputDispatcherPolicyInterface> mPolicy;
910 InputDispatcherConfiguration mConfig;
911
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800912 std::mutex mLock;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800913
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800914 std::condition_variable mDispatcherIsAlive;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800915
916 sp<Looper> mLooper;
917
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800918 EventEntry* mPendingEvent GUARDED_BY(mLock);
919 Queue<EventEntry> mInboundQueue GUARDED_BY(mLock);
920 Queue<EventEntry> mRecentQueue GUARDED_BY(mLock);
921 Queue<CommandEntry> mCommandQueue GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800922
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800923 DropReason mLastDropReason GUARDED_BY(mLock);
Michael Wright3a981722015-06-10 15:26:13 +0100924
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800925 void dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800926
927 // Enqueues an inbound event. Returns true if mLooper->wake() should be called.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800928 bool enqueueInboundEventLocked(EventEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800929
930 // Cleans up input state when dropping an inbound event.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800931 void dropInboundEventLocked(EventEntry* entry, DropReason dropReason) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800932
933 // Adds an event to a queue of recent events for debugging purposes.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800934 void addRecentEventLocked(EventEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800935
936 // App switch latency optimization.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800937 bool mAppSwitchSawKeyDown GUARDED_BY(mLock);
938 nsecs_t mAppSwitchDueTime GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800939
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800940 bool isAppSwitchKeyEvent(KeyEntry* keyEntry);
941 bool isAppSwitchPendingLocked() REQUIRES(mLock);
942 void resetPendingAppSwitchLocked(bool handled) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800943
944 // Stale event latency optimization.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800945 static bool isStaleEvent(nsecs_t currentTime, EventEntry* entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800946
947 // Blocked event latency optimization. Drops old events when the user intends
948 // to transfer focus to a new application.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800949 EventEntry* mNextUnblockedEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800950
Tiger Huang85b8c5e2019-01-17 18:34:54 +0800951 sp<InputWindowHandle> findTouchedWindowAtLocked(int32_t displayId, int32_t x, int32_t y,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800952 bool addOutsideTargets = false, bool addPortalWindows = false) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800953
954 // All registered connections mapped by channel file descriptor.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800955 KeyedVector<int, sp<Connection> > mConnectionsByFd GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800956
Robert Carr5c8a0262018-10-03 16:30:44 -0700957 struct IBinderHash {
958 std::size_t operator()(const sp<IBinder>& b) const {
959 return std::hash<IBinder *>{}(b.get());
960 }
961 };
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800962 std::unordered_map<sp<IBinder>, sp<InputChannel>, IBinderHash> mInputChannelsByToken
963 GUARDED_BY(mLock);
Robert Carr5c8a0262018-10-03 16:30:44 -0700964
Michael Wright3dd60e22019-03-27 22:06:44 +0000965 // Finds the display ID of the gesture monitor identified by the provided token.
966 std::optional<int32_t> findGestureMonitorDisplayByTokenLocked(const sp<IBinder>& token)
967 REQUIRES(mLock);
968
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800969 ssize_t getConnectionIndexLocked(const sp<InputChannel>& inputChannel) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800970
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800971 // Input channels that will receive a copy of all input events sent to the provided display.
Michael Wright3dd60e22019-03-27 22:06:44 +0000972 std::unordered_map<int32_t, std::vector<Monitor>> mGlobalMonitorsByDisplay
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800973 GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800974
Michael Wright3dd60e22019-03-27 22:06:44 +0000975 // Input channels that will receive pointer events that start within the corresponding display.
976 // These are a bit special when compared to global monitors since they'll cause gesture streams
977 // to continue even when there isn't a touched window,and have the ability to steal the rest of
978 // the pointer stream in order to claim it for a system gesture.
979 std::unordered_map<int32_t, std::vector<Monitor>> mGestureMonitorsByDisplay
980 GUARDED_BY(mLock);
981
982
Michael Wrightd02c5b62014-02-10 15:10:22 -0800983 // Event injection and synchronization.
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800984 std::condition_variable mInjectionResultAvailable;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800985 bool hasInjectionPermission(int32_t injectorPid, int32_t injectorUid);
Siarhei Vishniakou62683e82019-03-06 17:59:56 -0800986 void setInjectionResult(EventEntry* entry, int32_t injectionResult);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800987
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800988 std::condition_variable mInjectionSyncFinished;
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800989 void incrementPendingForegroundDispatches(EventEntry* entry);
Siarhei Vishniakou62683e82019-03-06 17:59:56 -0800990 void decrementPendingForegroundDispatches(EventEntry* entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800991
992 // Key repeat tracking.
993 struct KeyRepeatState {
994 KeyEntry* lastKeyEntry; // or null if no repeat
995 nsecs_t nextRepeatTime;
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800996 } mKeyRepeatState GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800997
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800998 void resetKeyRepeatLocked() REQUIRES(mLock);
999 KeyEntry* synthesizeKeyRepeatLocked(nsecs_t currentTime) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001000
Michael Wright78f24442014-08-06 15:55:28 -07001001 // Key replacement tracking
1002 struct KeyReplacement {
1003 int32_t keyCode;
1004 int32_t deviceId;
1005 bool operator==(const KeyReplacement& rhs) const {
1006 return keyCode == rhs.keyCode && deviceId == rhs.deviceId;
1007 }
1008 bool operator<(const KeyReplacement& rhs) const {
1009 return keyCode != rhs.keyCode ? keyCode < rhs.keyCode : deviceId < rhs.deviceId;
1010 }
1011 };
1012 // Maps the key code replaced, device id tuple to the key code it was replaced with
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001013 KeyedVector<KeyReplacement, int32_t> mReplacedKeys GUARDED_BY(mLock);
Siarhei Vishniakou61fafdd2018-04-13 11:00:58 -05001014 // Process certain Meta + Key combinations
1015 void accelerateMetaShortcuts(const int32_t deviceId, const int32_t action,
1016 int32_t& keyCode, int32_t& metaState);
Michael Wright78f24442014-08-06 15:55:28 -07001017
Michael Wrightd02c5b62014-02-10 15:10:22 -08001018 // Deferred command processing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001019 bool haveCommandsLocked() const REQUIRES(mLock);
1020 bool runCommandsLockedInterruptible() REQUIRES(mLock);
1021 CommandEntry* postCommandLocked(Command command) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001022
1023 // Input filter processing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001024 bool shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) REQUIRES(mLock);
1025 bool shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001026
1027 // Inbound event processing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001028 void drainInboundQueueLocked() REQUIRES(mLock);
1029 void releasePendingEventLocked() REQUIRES(mLock);
1030 void releaseInboundEventLocked(EventEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001031
1032 // Dispatch state.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001033 bool mDispatchEnabled GUARDED_BY(mLock);
1034 bool mDispatchFrozen GUARDED_BY(mLock);
1035 bool mInputFilterEnabled GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001036
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001037 std::unordered_map<int32_t, std::vector<sp<InputWindowHandle>>> mWindowHandlesByDisplay
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001038 GUARDED_BY(mLock);
Arthur Hungb92218b2018-08-14 12:00:21 +08001039 // Get window handles by display, return an empty vector if not found.
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001040 std::vector<sp<InputWindowHandle>> getWindowHandlesLocked(int32_t displayId) const
1041 REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001042 sp<InputWindowHandle> getWindowHandleLocked(const sp<IBinder>& windowHandleToken) const
1043 REQUIRES(mLock);
1044 sp<InputChannel> getInputChannelLocked(const sp<IBinder>& windowToken) const REQUIRES(mLock);
1045 bool hasWindowHandleLocked(const sp<InputWindowHandle>& windowHandle) const REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001046
1047 // Focus tracking for keys, trackball, etc.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001048 std::unordered_map<int32_t, sp<InputWindowHandle>> mFocusedWindowHandlesByDisplay
1049 GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001050
1051 // Focus tracking for touch.
1052 struct TouchedWindow {
1053 sp<InputWindowHandle> windowHandle;
1054 int32_t targetFlags;
1055 BitSet32 pointerIds; // zero unless target flag FLAG_SPLIT is set
1056 };
Michael Wright3dd60e22019-03-27 22:06:44 +00001057
1058 // For tracking the offsets we need to apply when adding gesture monitor targets.
1059 struct TouchedMonitor {
1060 Monitor monitor;
1061 float xOffset = 0.f;
1062 float yOffset = 0.f;
1063
1064 explicit TouchedMonitor(const Monitor& monitor, float xOffset, float yOffset);
1065 };
1066
Michael Wrightd02c5b62014-02-10 15:10:22 -08001067 struct TouchState {
1068 bool down;
1069 bool split;
1070 int32_t deviceId; // id of the device that is currently down, others are rejected
1071 uint32_t source; // source of the device that is current down, others are rejected
1072 int32_t displayId; // id to the display that currently has a touch, others are rejected
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001073 std::vector<TouchedWindow> windows;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001074
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001075 // This collects the portal windows that the touch has gone through. Each portal window
1076 // targets a display (embedded display for most cases). With this info, we can add the
1077 // monitoring channels of the displays touched.
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001078 std::vector<sp<InputWindowHandle>> portalWindows;
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001079
Michael Wright3dd60e22019-03-27 22:06:44 +00001080 std::vector<TouchedMonitor> gestureMonitors;
1081
Michael Wrightd02c5b62014-02-10 15:10:22 -08001082 TouchState();
1083 ~TouchState();
1084 void reset();
1085 void copyFrom(const TouchState& other);
1086 void addOrUpdateWindow(const sp<InputWindowHandle>& windowHandle,
1087 int32_t targetFlags, BitSet32 pointerIds);
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001088 void addPortalWindow(const sp<InputWindowHandle>& windowHandle);
Michael Wright3dd60e22019-03-27 22:06:44 +00001089 void addGestureMonitors(const std::vector<TouchedMonitor>& monitors);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001090 void removeWindow(const sp<InputWindowHandle>& windowHandle);
Robert Carr803535b2018-08-02 16:38:15 -07001091 void removeWindowByToken(const sp<IBinder>& token);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001092 void filterNonAsIsTouchWindows();
Michael Wright3dd60e22019-03-27 22:06:44 +00001093 void filterNonMonitors();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001094 sp<InputWindowHandle> getFirstForegroundWindowHandle() const;
1095 bool isSlippery() const;
1096 };
1097
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001098 KeyedVector<int32_t, TouchState> mTouchStatesByDisplay GUARDED_BY(mLock);
1099 TouchState mTempTouchState GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001100
Tiger Huang721e26f2018-07-24 22:26:19 +08001101 // Focused applications.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001102 std::unordered_map<int32_t, sp<InputApplicationHandle>> mFocusedApplicationHandlesByDisplay
1103 GUARDED_BY(mLock);
Tiger Huang721e26f2018-07-24 22:26:19 +08001104
1105 // Top focused display.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001106 int32_t mFocusedDisplayId GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001107
1108 // Dispatcher state at time of last ANR.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001109 std::string mLastANRState GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001110
1111 // Dispatch inbound events.
1112 bool dispatchConfigurationChangedLocked(
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001113 nsecs_t currentTime, ConfigurationChangedEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001114 bool dispatchDeviceResetLocked(
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001115 nsecs_t currentTime, DeviceResetEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001116 bool dispatchKeyLocked(
1117 nsecs_t currentTime, KeyEntry* entry,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001118 DropReason* dropReason, nsecs_t* nextWakeupTime) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001119 bool dispatchMotionLocked(
1120 nsecs_t currentTime, MotionEntry* entry,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001121 DropReason* dropReason, nsecs_t* nextWakeupTime) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001122 void dispatchEventLocked(nsecs_t currentTime, EventEntry* entry,
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001123 const std::vector<InputTarget>& inputTargets) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001124
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001125 void logOutboundKeyDetails(const char* prefix, const KeyEntry* entry);
1126 void logOutboundMotionDetails(const char* prefix, const MotionEntry* entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001127
1128 // Keeping track of ANR timeouts.
1129 enum InputTargetWaitCause {
1130 INPUT_TARGET_WAIT_CAUSE_NONE,
1131 INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY,
1132 INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY,
1133 };
1134
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001135 InputTargetWaitCause mInputTargetWaitCause GUARDED_BY(mLock);
1136 nsecs_t mInputTargetWaitStartTime GUARDED_BY(mLock);
1137 nsecs_t mInputTargetWaitTimeoutTime GUARDED_BY(mLock);
1138 bool mInputTargetWaitTimeoutExpired GUARDED_BY(mLock);
1139 sp<IBinder> mInputTargetWaitApplicationToken GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001140
1141 // Contains the last window which received a hover event.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001142 sp<InputWindowHandle> mLastHoverWindowHandle GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001143
1144 // Finding targets for input events.
1145 int32_t handleTargetsNotReadyLocked(nsecs_t currentTime, const EventEntry* entry,
1146 const sp<InputApplicationHandle>& applicationHandle,
1147 const sp<InputWindowHandle>& windowHandle,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001148 nsecs_t* nextWakeupTime, const char* reason) REQUIRES(mLock);
Robert Carr803535b2018-08-02 16:38:15 -07001149
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001150 void removeWindowByTokenLocked(const sp<IBinder>& token) REQUIRES(mLock);
Robert Carr803535b2018-08-02 16:38:15 -07001151
Michael Wrightd02c5b62014-02-10 15:10:22 -08001152 void resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001153 const sp<InputChannel>& inputChannel) REQUIRES(mLock);
1154 nsecs_t getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime) REQUIRES(mLock);
1155 void resetANRTimeoutsLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001156
Tiger Huang721e26f2018-07-24 22:26:19 +08001157 int32_t getTargetDisplayId(const EventEntry* entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001158 int32_t findFocusedWindowTargetsLocked(nsecs_t currentTime, const EventEntry* entry,
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001159 std::vector<InputTarget>& inputTargets, nsecs_t* nextWakeupTime) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001160 int32_t findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry* entry,
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001161 std::vector<InputTarget>& inputTargets, nsecs_t* nextWakeupTime,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001162 bool* outConflictingPointerActions) REQUIRES(mLock);
Michael Wright3dd60e22019-03-27 22:06:44 +00001163 std::vector<TouchedMonitor> findTouchedGestureMonitorsLocked(int32_t displayId,
1164 const std::vector<sp<InputWindowHandle>>& portalWindows) REQUIRES(mLock);
1165 void addGestureMonitors(const std::vector<Monitor>& monitors,
1166 std::vector<TouchedMonitor>& outTouchedMonitors, float xOffset = 0, float yOffset = 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001167
1168 void addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle,
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001169 int32_t targetFlags, BitSet32 pointerIds, std::vector<InputTarget>& inputTargets)
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001170 REQUIRES(mLock);
Michael Wright3dd60e22019-03-27 22:06:44 +00001171 void addMonitoringTargetLocked(const Monitor& monitor, float xOffset, float yOffset,
1172 std::vector<InputTarget>& inputTargets) REQUIRES(mLock);
1173 void addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets,
1174 int32_t displayId, float xOffset = 0, float yOffset = 0) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001175
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001176 void pokeUserActivityLocked(const EventEntry* eventEntry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001177 bool checkInjectionPermission(const sp<InputWindowHandle>& windowHandle,
1178 const InjectionState* injectionState);
1179 bool isWindowObscuredAtPointLocked(const sp<InputWindowHandle>& windowHandle,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001180 int32_t x, int32_t y) const REQUIRES(mLock);
1181 bool isWindowObscuredLocked(const sp<InputWindowHandle>& windowHandle) const REQUIRES(mLock);
1182 std::string getApplicationWindowLabel(const sp<InputApplicationHandle>& applicationHandle,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001183 const sp<InputWindowHandle>& windowHandle);
1184
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001185 std::string checkWindowReadyForMoreInputLocked(nsecs_t currentTime,
Jeff Brownffb49772014-10-10 19:01:34 -07001186 const sp<InputWindowHandle>& windowHandle, const EventEntry* eventEntry,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001187 const char* targetType) REQUIRES(mLock);
Jeff Brownffb49772014-10-10 19:01:34 -07001188
Michael Wrightd02c5b62014-02-10 15:10:22 -08001189 // Manage the dispatch cycle for a single connection.
1190 // These methods are deliberately not Interruptible because doing all of the work
1191 // with the mutex held makes it easier to ensure that connection invariants are maintained.
1192 // If needed, the methods post commands to run later once the critical bits are done.
1193 void prepareDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001194 EventEntry* eventEntry, const InputTarget* inputTarget) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001195 void enqueueDispatchEntriesLocked(nsecs_t currentTime, const sp<Connection>& connection,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001196 EventEntry* eventEntry, const InputTarget* inputTarget) REQUIRES(mLock);
chaviw8c9cf542019-03-25 13:02:48 -07001197 void enqueueDispatchEntryLocked(const sp<Connection>& connection,
1198 EventEntry* eventEntry, const InputTarget* inputTarget, int32_t dispatchMode)
1199 REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001200 void startDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection)
1201 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001202 void finishDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001203 uint32_t seq, bool handled) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001204 void abortBrokenDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001205 bool notify) REQUIRES(mLock);
Siarhei Vishniakou62683e82019-03-06 17:59:56 -08001206 void drainDispatchQueue(Queue<DispatchEntry>* queue);
1207 void releaseDispatchEntry(DispatchEntry* dispatchEntry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001208 static int handleReceiveCallback(int fd, int events, void* data);
chaviw8c9cf542019-03-25 13:02:48 -07001209 // The action sent should only be of type AMOTION_EVENT_*
1210 void dispatchPointerDownOutsideFocusIfNecessary(uint32_t source, int32_t action,
1211 const sp<IBinder>& newToken) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001212
1213 void synthesizeCancelationEventsForAllConnectionsLocked(
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001214 const CancelationOptions& options) REQUIRES(mLock);
1215 void synthesizeCancelationEventsForMonitorsLocked(
1216 const CancelationOptions& options) REQUIRES(mLock);
Michael Wright3dd60e22019-03-27 22:06:44 +00001217 void synthesizeCancelationEventsForMonitorsLocked(const CancelationOptions& options,
1218 std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001219 void synthesizeCancelationEventsForInputChannelLocked(const sp<InputChannel>& channel,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001220 const CancelationOptions& options) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001221 void synthesizeCancelationEventsForConnectionLocked(const sp<Connection>& connection,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001222 const CancelationOptions& options) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001223
1224 // Splitting motion events across windows.
1225 MotionEntry* splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds);
1226
1227 // Reset and drop everything the dispatcher is doing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001228 void resetAndDropEverythingLocked(const char* reason) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001229
1230 // Dump state.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001231 void dumpDispatchStateLocked(std::string& dump) REQUIRES(mLock);
Michael Wright3dd60e22019-03-27 22:06:44 +00001232 void dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001233 void logDispatchStateLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001234
1235 // Registration.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001236 void removeMonitorChannelLocked(const sp<InputChannel>& inputChannel) REQUIRES(mLock);
Michael Wright3dd60e22019-03-27 22:06:44 +00001237 void removeMonitorChannelLocked(const sp<InputChannel>& inputChannel,
1238 std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay)
1239 REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001240 status_t unregisterInputChannelLocked(const sp<InputChannel>& inputChannel, bool notify)
1241 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001242
1243 // Interesting events that we might like to log or tell the framework about.
1244 void onDispatchCycleFinishedLocked(
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001245 nsecs_t currentTime, const sp<Connection>& connection, uint32_t seq, bool handled)
1246 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001247 void onDispatchCycleBrokenLocked(
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001248 nsecs_t currentTime, const sp<Connection>& connection) REQUIRES(mLock);
chaviw0c06c6e2019-01-09 13:27:07 -08001249 void onFocusChangedLocked(const sp<InputWindowHandle>& oldFocus,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001250 const sp<InputWindowHandle>& newFocus) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001251 void onANRLocked(
1252 nsecs_t currentTime, const sp<InputApplicationHandle>& applicationHandle,
1253 const sp<InputWindowHandle>& windowHandle,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001254 nsecs_t eventTime, nsecs_t waitStartTime, const char* reason) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001255
1256 // Outbound policy interactions.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001257 void doNotifyConfigurationChangedLockedInterruptible(CommandEntry* commandEntry)
1258 REQUIRES(mLock);
1259 void doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
1260 void doNotifyFocusChangedLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
1261 void doNotifyANRLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
1262 void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry)
1263 REQUIRES(mLock);
1264 void doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001265 bool afterKeyEventLockedInterruptible(const sp<Connection>& connection,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001266 DispatchEntry* dispatchEntry, KeyEntry* keyEntry, bool handled) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001267 bool afterMotionEventLockedInterruptible(const sp<Connection>& connection,
Siarhei Vishniakou62683e82019-03-06 17:59:56 -08001268 DispatchEntry* dispatchEntry, MotionEntry* motionEntry, bool handled) REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001269 void doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001270 void initializeKeyEvent(KeyEvent* event, const KeyEntry* entry);
1271
1272 // Statistics gathering.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001273 void updateDispatchStatistics(nsecs_t currentTime, const EventEntry* entry,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001274 int32_t injectionResult, nsecs_t timeSpentWaitingForApplication);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001275 void traceInboundQueueLengthLocked() REQUIRES(mLock);
1276 void traceOutboundQueueLength(const sp<Connection>& connection);
1277 void traceWaitQueueLength(const sp<Connection>& connection);
Prabir Pradhanf93562f2018-11-29 12:13:37 -08001278
Prabir Pradhan79a4f0c2019-01-09 11:24:01 -08001279 sp<InputReporterInterface> mReporter;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001280};
1281
1282/* Enqueues and dispatches input events, endlessly. */
1283class InputDispatcherThread : public Thread {
1284public:
1285 explicit InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher);
1286 ~InputDispatcherThread();
1287
1288private:
1289 virtual bool threadLoop();
1290
1291 sp<InputDispatcherInterface> mDispatcher;
1292};
1293
1294} // namespace android
1295
1296#endif // _UI_INPUT_DISPATCHER_H