blob: 753b7488844d5a2b48cc7b875499bf7a12613054 [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;
chaviwfd6d3512019-03-25 13:23:49 -0700277
278 /* Notifies the policy that a pointer down event has occurred outside the current focused
279 * window.
280 *
281 * The touchedToken passed as an argument is the window that received the input event.
282 */
283 virtual void onPointerDownOutsideFocus(const sp<IBinder>& touchedToken) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800284};
285
286
287/* Notifies the system about input events generated by the input reader.
288 * The dispatcher is expected to be mostly asynchronous. */
289class InputDispatcherInterface : public virtual RefBase, public InputListenerInterface {
290protected:
291 InputDispatcherInterface() { }
292 virtual ~InputDispatcherInterface() { }
293
294public:
295 /* Dumps the state of the input dispatcher.
296 *
297 * This method may be called on any thread (usually by the input manager). */
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800298 virtual void dump(std::string& dump) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800299
300 /* Called by the heatbeat to ensures that the dispatcher has not deadlocked. */
301 virtual void monitor() = 0;
302
303 /* Runs a single iteration of the dispatch loop.
304 * Nominally processes one queued event, a timeout, or a response from an input consumer.
305 *
306 * This method should only be called on the input dispatcher thread.
307 */
308 virtual void dispatchOnce() = 0;
309
310 /* Injects an input event and optionally waits for sync.
311 * The synchronization mode determines whether the method blocks while waiting for
312 * input injection to proceed.
313 * Returns one of the INPUT_EVENT_INJECTION_XXX constants.
314 *
315 * This method may be called on any thread (usually by the input manager).
316 */
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800317 virtual int32_t injectInputEvent(const InputEvent* event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800318 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,
319 uint32_t policyFlags) = 0;
320
321 /* Sets the list of input windows.
322 *
323 * This method may be called on any thread (usually by the input manager).
324 */
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800325 virtual void setInputWindows(const std::vector<sp<InputWindowHandle> >& inputWindowHandles,
chaviw291d88a2019-02-14 10:33:58 -0800326 int32_t displayId,
327 const sp<ISetInputWindowsListener>& setInputWindowsListener = nullptr) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800328
Tiger Huang721e26f2018-07-24 22:26:19 +0800329 /* Sets the focused application on the given display.
Michael Wrightd02c5b62014-02-10 15:10:22 -0800330 *
331 * This method may be called on any thread (usually by the input manager).
332 */
333 virtual void setFocusedApplication(
Tiger Huang721e26f2018-07-24 22:26:19 +0800334 int32_t displayId, const sp<InputApplicationHandle>& inputApplicationHandle) = 0;
335
336 /* Sets the focused display.
337 *
338 * This method may be called on any thread (usually by the input manager).
339 */
340 virtual void setFocusedDisplay(int32_t displayId) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800341
342 /* Sets the input dispatching mode.
343 *
344 * This method may be called on any thread (usually by the input manager).
345 */
346 virtual void setInputDispatchMode(bool enabled, bool frozen) = 0;
347
348 /* Sets whether input event filtering is enabled.
349 * When enabled, incoming input events are sent to the policy's filterInputEvent
350 * method instead of being dispatched. The filter is expected to use
351 * injectInputEvent to inject the events it would like to have dispatched.
352 * It should include POLICY_FLAG_FILTERED in the policy flags during injection.
353 */
354 virtual void setInputFilterEnabled(bool enabled) = 0;
355
chaviwfbe5d9c2018-12-26 12:23:37 -0800356 /* Transfers touch focus from one window to another window.
Michael Wrightd02c5b62014-02-10 15:10:22 -0800357 *
358 * Returns true on success. False if the window did not actually have touch focus.
359 */
chaviwfbe5d9c2018-12-26 12:23:37 -0800360 virtual bool transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken) = 0;
361
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800362 /* Registers input channels that may be used as targets for input events.
Michael Wrightd02c5b62014-02-10 15:10:22 -0800363 *
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800364 * This method may be called on any thread (usually by the input manager).
Michael Wrightd02c5b62014-02-10 15:10:22 -0800365 */
Robert Carr803535b2018-08-02 16:38:15 -0700366 virtual status_t registerInputChannel(
367 const sp<InputChannel>& inputChannel, int32_t displayId) = 0;
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800368
Michael Wright3dd60e22019-03-27 22:06:44 +0000369 /* Registers input channels to be used to monitor input events.
370 *
371 * Each monitor must target a specific display and will only receive input events sent to that
372 * display. If the monitor is a gesture monitor, it will only receive pointer events on the
373 * targeted display.
374 *
375 * This method may be called on any thread (usually by the input manager).
376 */
377 virtual status_t registerInputMonitor(
378 const sp<InputChannel>& inputChannel, int32_t displayId, bool gestureMonitor) = 0;
379
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800380 /* Unregister input channels that will no longer receive input events.
381 *
382 * This method may be called on any thread (usually by the input manager).
383 */
Michael Wrightd02c5b62014-02-10 15:10:22 -0800384 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) = 0;
Michael Wright3dd60e22019-03-27 22:06:44 +0000385
386 /* Allows an input monitor steal the current pointer stream away from normal input windows.
387 *
388 * This method may be called on any thread (usually by the input manager).
389 */
390 virtual status_t pilferPointers(const sp<IBinder>& token) = 0;
391
Michael Wrightd02c5b62014-02-10 15:10:22 -0800392};
393
394/* Dispatches events to input targets. Some functions of the input dispatcher, such as
395 * identifying input targets, are controlled by a separate policy object.
396 *
397 * IMPORTANT INVARIANT:
398 * Because the policy can potentially block or cause re-entrance into the input dispatcher,
399 * the input dispatcher never calls into the policy while holding its internal locks.
400 * The implementation is also carefully designed to recover from scenarios such as an
401 * input channel becoming unregistered while identifying input targets or processing timeouts.
402 *
403 * Methods marked 'Locked' must be called with the lock acquired.
404 *
405 * Methods marked 'LockedInterruptible' must be called with the lock acquired but
406 * may during the course of their execution release the lock, call into the policy, and
407 * then reacquire the lock. The caller is responsible for recovering gracefully.
408 *
409 * A 'LockedInterruptible' method may called a 'Locked' method, but NOT vice-versa.
410 */
411class InputDispatcher : public InputDispatcherInterface {
412protected:
413 virtual ~InputDispatcher();
414
415public:
416 explicit InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy);
417
Michael Wright3dd60e22019-03-27 22:06:44 +0000418 virtual void dump(std::string& dump) override;
419 virtual void monitor() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800420
Michael Wright3dd60e22019-03-27 22:06:44 +0000421 virtual void dispatchOnce() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800422
Michael Wright3dd60e22019-03-27 22:06:44 +0000423 virtual void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) override;
424 virtual void notifyKey(const NotifyKeyArgs* args) override;
425 virtual void notifyMotion(const NotifyMotionArgs* args) override;
426 virtual void notifySwitch(const NotifySwitchArgs* args) override;
427 virtual void notifyDeviceReset(const NotifyDeviceResetArgs* args) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800428
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800429 virtual int32_t injectInputEvent(const InputEvent* event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800430 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,
Michael Wright3dd60e22019-03-27 22:06:44 +0000431 uint32_t policyFlags) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800432
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800433 virtual void setInputWindows(const std::vector<sp<InputWindowHandle> >& inputWindowHandles,
chaviw291d88a2019-02-14 10:33:58 -0800434 int32_t displayId,
Michael Wright3dd60e22019-03-27 22:06:44 +0000435 const sp<ISetInputWindowsListener>& setInputWindowsListener = nullptr) override;
Tiger Huang721e26f2018-07-24 22:26:19 +0800436 virtual void setFocusedApplication(int32_t displayId,
Michael Wright3dd60e22019-03-27 22:06:44 +0000437 const sp<InputApplicationHandle>& inputApplicationHandle) override;
438 virtual void setFocusedDisplay(int32_t displayId) override;
439 virtual void setInputDispatchMode(bool enabled, bool frozen) override;
440 virtual void setInputFilterEnabled(bool enabled) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800441
Michael Wright3dd60e22019-03-27 22:06:44 +0000442 virtual bool transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken)
443 override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800444
445 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel,
Michael Wright3dd60e22019-03-27 22:06:44 +0000446 int32_t displayId) override;
447 virtual status_t registerInputMonitor(const sp<InputChannel>& inputChannel,
448 int32_t displayId, bool isGestureMonitor) override;
449 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) override;
450 virtual status_t pilferPointers(const sp<IBinder>& token) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800451
452private:
453 template <typename T>
454 struct Link {
455 T* next;
456 T* prev;
457
458 protected:
Yi Kong9b14ac62018-07-17 13:48:38 -0700459 inline Link() : next(nullptr), prev(nullptr) { }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800460 };
461
462 struct InjectionState {
463 mutable int32_t refCount;
464
465 int32_t injectorPid;
466 int32_t injectorUid;
467 int32_t injectionResult; // initially INPUT_EVENT_INJECTION_PENDING
468 bool injectionIsAsync; // set to true if injection is not waiting for the result
469 int32_t pendingForegroundDispatches; // the number of foreground dispatches in progress
470
471 InjectionState(int32_t injectorPid, int32_t injectorUid);
472 void release();
473
474 private:
475 ~InjectionState();
476 };
477
478 struct EventEntry : Link<EventEntry> {
479 enum {
480 TYPE_CONFIGURATION_CHANGED,
481 TYPE_DEVICE_RESET,
482 TYPE_KEY,
483 TYPE_MOTION
484 };
485
Prabir Pradhan42611e02018-11-27 14:04:02 -0800486 uint32_t sequenceNum;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800487 mutable int32_t refCount;
488 int32_t type;
489 nsecs_t eventTime;
490 uint32_t policyFlags;
491 InjectionState* injectionState;
492
493 bool dispatchInProgress; // initially false, set to true while dispatching
494
Yi Kong9b14ac62018-07-17 13:48:38 -0700495 inline bool isInjected() const { return injectionState != nullptr; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800496
497 void release();
498
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800499 virtual void appendDescription(std::string& msg) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800500
501 protected:
Prabir Pradhan42611e02018-11-27 14:04:02 -0800502 EventEntry(uint32_t sequenceNum, int32_t type, nsecs_t eventTime, uint32_t policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800503 virtual ~EventEntry();
504 void releaseInjectionState();
505 };
506
507 struct ConfigurationChangedEntry : EventEntry {
Prabir Pradhan42611e02018-11-27 14:04:02 -0800508 explicit ConfigurationChangedEntry(uint32_t sequenceNum, nsecs_t eventTime);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800509 virtual void appendDescription(std::string& msg) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800510
511 protected:
512 virtual ~ConfigurationChangedEntry();
513 };
514
515 struct DeviceResetEntry : EventEntry {
516 int32_t deviceId;
517
Prabir Pradhan42611e02018-11-27 14:04:02 -0800518 DeviceResetEntry(uint32_t sequenceNum, nsecs_t eventTime, int32_t deviceId);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800519 virtual void appendDescription(std::string& msg) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800520
521 protected:
522 virtual ~DeviceResetEntry();
523 };
524
525 struct KeyEntry : EventEntry {
526 int32_t deviceId;
527 uint32_t source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100528 int32_t displayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800529 int32_t action;
530 int32_t flags;
531 int32_t keyCode;
532 int32_t scanCode;
533 int32_t metaState;
534 int32_t repeatCount;
535 nsecs_t downTime;
536
537 bool syntheticRepeat; // set to true for synthetic key repeats
538
539 enum InterceptKeyResult {
540 INTERCEPT_KEY_RESULT_UNKNOWN,
541 INTERCEPT_KEY_RESULT_SKIP,
542 INTERCEPT_KEY_RESULT_CONTINUE,
543 INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER,
544 };
545 InterceptKeyResult interceptKeyResult; // set based on the interception result
546 nsecs_t interceptKeyWakeupTime; // used with INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER
547
Prabir Pradhan42611e02018-11-27 14:04:02 -0800548 KeyEntry(uint32_t sequenceNum, nsecs_t eventTime,
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100549 int32_t deviceId, uint32_t source, int32_t displayId, uint32_t policyFlags,
550 int32_t action, int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800551 int32_t repeatCount, nsecs_t downTime);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800552 virtual void appendDescription(std::string& msg) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800553 void recycle();
554
555 protected:
556 virtual ~KeyEntry();
557 };
558
559 struct MotionEntry : EventEntry {
560 nsecs_t eventTime;
561 int32_t deviceId;
562 uint32_t source;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800563 int32_t displayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800564 int32_t action;
Michael Wright7b159c92015-05-14 14:48:03 +0100565 int32_t actionButton;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800566 int32_t flags;
567 int32_t metaState;
568 int32_t buttonState;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800569 MotionClassification classification;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800570 int32_t edgeFlags;
571 float xPrecision;
572 float yPrecision;
573 nsecs_t downTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800574 uint32_t pointerCount;
575 PointerProperties pointerProperties[MAX_POINTERS];
576 PointerCoords pointerCoords[MAX_POINTERS];
577
Prabir Pradhan42611e02018-11-27 14:04:02 -0800578 MotionEntry(uint32_t sequenceNum, nsecs_t eventTime,
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800579 int32_t deviceId, uint32_t source, int32_t displayId, uint32_t policyFlags,
Michael Wright7b159c92015-05-14 14:48:03 +0100580 int32_t action, int32_t actionButton, int32_t flags,
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800581 int32_t metaState, int32_t buttonState, MotionClassification classification,
582 int32_t edgeFlags, float xPrecision, float yPrecision,
583 nsecs_t downTime, uint32_t pointerCount,
Jeff Brownf086ddb2014-02-11 14:28:48 -0800584 const PointerProperties* pointerProperties, const PointerCoords* pointerCoords,
585 float xOffset, float yOffset);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800586 virtual void appendDescription(std::string& msg) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800587
588 protected:
589 virtual ~MotionEntry();
590 };
591
592 // Tracks the progress of dispatching a particular event to a particular connection.
593 struct DispatchEntry : Link<DispatchEntry> {
594 const uint32_t seq; // unique sequence number, never 0
595
596 EventEntry* eventEntry; // the event to dispatch
597 int32_t targetFlags;
598 float xOffset;
599 float yOffset;
Robert Carre07e1032018-11-26 12:55:53 -0800600 float globalScaleFactor;
601 float windowXScale = 1.0f;
602 float windowYScale = 1.0f;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800603 nsecs_t deliveryTime; // time when the event was actually delivered
604
605 // Set to the resolved action and flags when the event is enqueued.
606 int32_t resolvedAction;
607 int32_t resolvedFlags;
608
609 DispatchEntry(EventEntry* eventEntry,
Robert Carre07e1032018-11-26 12:55:53 -0800610 int32_t targetFlags, float xOffset, float yOffset,
611 float globalScaleFactor, float windowXScale, float windowYScale);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800612 ~DispatchEntry();
613
614 inline bool hasForegroundTarget() const {
615 return targetFlags & InputTarget::FLAG_FOREGROUND;
616 }
617
618 inline bool isSplit() const {
619 return targetFlags & InputTarget::FLAG_SPLIT;
620 }
621
622 private:
623 static volatile int32_t sNextSeqAtomic;
624
625 static uint32_t nextSeq();
626 };
627
628 // A command entry captures state and behavior for an action to be performed in the
629 // dispatch loop after the initial processing has taken place. It is essentially
630 // a kind of continuation used to postpone sensitive policy interactions to a point
631 // in the dispatch loop where it is safe to release the lock (generally after finishing
632 // the critical parts of the dispatch cycle).
633 //
634 // The special thing about commands is that they can voluntarily release and reacquire
635 // the dispatcher lock at will. Initially when the command starts running, the
636 // dispatcher lock is held. However, if the command needs to call into the policy to
637 // do some work, it can release the lock, do the work, then reacquire the lock again
638 // before returning.
639 //
640 // This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch
641 // never calls into the policy while holding its lock.
642 //
643 // Commands are implicitly 'LockedInterruptible'.
644 struct CommandEntry;
645 typedef void (InputDispatcher::*Command)(CommandEntry* commandEntry);
646
647 class Connection;
648 struct CommandEntry : Link<CommandEntry> {
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700649 explicit CommandEntry(Command command);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800650 ~CommandEntry();
651
652 Command command;
653
654 // parameters for the command (usage varies by command)
655 sp<Connection> connection;
656 nsecs_t eventTime;
657 KeyEntry* keyEntry;
658 sp<InputApplicationHandle> inputApplicationHandle;
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800659 std::string reason;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800660 int32_t userActivityEventType;
661 uint32_t seq;
662 bool handled;
Robert Carr803535b2018-08-02 16:38:15 -0700663 sp<InputChannel> inputChannel;
chaviw0c06c6e2019-01-09 13:27:07 -0800664 sp<IBinder> oldToken;
665 sp<IBinder> newToken;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800666 };
667
668 // Generic queue implementation.
669 template <typename T>
670 struct Queue {
671 T* head;
672 T* tail;
Jon McCaffrey65dbe972014-11-18 12:07:08 -0800673 uint32_t entryCount;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800674
Yi Kong9b14ac62018-07-17 13:48:38 -0700675 inline Queue() : head(nullptr), tail(nullptr), entryCount(0) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800676 }
677
678 inline bool isEmpty() const {
679 return !head;
680 }
681
682 inline void enqueueAtTail(T* entry) {
Jon McCaffrey65dbe972014-11-18 12:07:08 -0800683 entryCount++;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800684 entry->prev = tail;
685 if (tail) {
686 tail->next = entry;
687 } else {
688 head = entry;
689 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700690 entry->next = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800691 tail = entry;
692 }
693
694 inline void enqueueAtHead(T* entry) {
Jon McCaffrey65dbe972014-11-18 12:07:08 -0800695 entryCount++;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800696 entry->next = head;
697 if (head) {
698 head->prev = entry;
699 } else {
700 tail = entry;
701 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700702 entry->prev = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800703 head = entry;
704 }
705
706 inline void dequeue(T* entry) {
Jon McCaffrey65dbe972014-11-18 12:07:08 -0800707 entryCount--;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800708 if (entry->prev) {
709 entry->prev->next = entry->next;
710 } else {
711 head = entry->next;
712 }
713 if (entry->next) {
714 entry->next->prev = entry->prev;
715 } else {
716 tail = entry->prev;
717 }
718 }
719
720 inline T* dequeueAtHead() {
Jon McCaffrey65dbe972014-11-18 12:07:08 -0800721 entryCount--;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800722 T* entry = head;
723 head = entry->next;
724 if (head) {
Yi Kong9b14ac62018-07-17 13:48:38 -0700725 head->prev = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800726 } else {
Yi Kong9b14ac62018-07-17 13:48:38 -0700727 tail = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800728 }
729 return entry;
730 }
731
Jon McCaffrey65dbe972014-11-18 12:07:08 -0800732 uint32_t count() const {
733 return entryCount;
734 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800735 };
736
737 /* Specifies which events are to be canceled and why. */
738 struct CancelationOptions {
739 enum Mode {
740 CANCEL_ALL_EVENTS = 0,
741 CANCEL_POINTER_EVENTS = 1,
742 CANCEL_NON_POINTER_EVENTS = 2,
743 CANCEL_FALLBACK_EVENTS = 3,
744 };
745
746 // The criterion to use to determine which events should be canceled.
747 Mode mode;
748
749 // Descriptive reason for the cancelation.
750 const char* reason;
751
Michael Wright3dd60e22019-03-27 22:06:44 +0000752 // The specific keycode of the key event to cancel, or nullopt to cancel any key event.
753 std::optional<int32_t> keyCode = std::nullopt;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800754
Michael Wright3dd60e22019-03-27 22:06:44 +0000755 // The specific device id of events to cancel, or nullopt to cancel events from any device.
756 std::optional<int32_t> deviceId = std::nullopt;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800757
Michael Wright3dd60e22019-03-27 22:06:44 +0000758 // The specific display id of events to cancel, or nullopt to cancel events on any display.
759 std::optional<int32_t> displayId = std::nullopt;
760
761 CancelationOptions(Mode mode, const char* reason) : mode(mode), reason(reason) { }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800762 };
763
764 /* Tracks dispatched key and motion event state so that cancelation events can be
765 * synthesized when events are dropped. */
766 class InputState {
767 public:
768 InputState();
769 ~InputState();
770
771 // Returns true if there is no state to be canceled.
772 bool isNeutral() const;
773
774 // Returns true if the specified source is known to have received a hover enter
775 // motion event.
776 bool isHovering(int32_t deviceId, uint32_t source, int32_t displayId) const;
777
778 // Records tracking information for a key event that has just been published.
779 // Returns true if the event should be delivered, false if it is inconsistent
780 // and should be skipped.
781 bool trackKey(const KeyEntry* entry, int32_t action, int32_t flags);
782
783 // Records tracking information for a motion event that has just been published.
784 // Returns true if the event should be delivered, false if it is inconsistent
785 // and should be skipped.
786 bool trackMotion(const MotionEntry* entry, int32_t action, int32_t flags);
787
788 // Synthesizes cancelation events for the current state and resets the tracked state.
789 void synthesizeCancelationEvents(nsecs_t currentTime,
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800790 std::vector<EventEntry*>& outEvents, const CancelationOptions& options);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800791
792 // Clears the current state.
793 void clear();
794
795 // Copies pointer-related parts of the input state to another instance.
796 void copyPointerStateTo(InputState& other) const;
797
798 // Gets the fallback key associated with a keycode.
799 // Returns -1 if none.
800 // Returns AKEYCODE_UNKNOWN if we are only dispatching the unhandled key to the policy.
801 int32_t getFallbackKey(int32_t originalKeyCode);
802
803 // Sets the fallback key for a particular keycode.
804 void setFallbackKey(int32_t originalKeyCode, int32_t fallbackKeyCode);
805
806 // Removes the fallback key for a particular keycode.
807 void removeFallbackKey(int32_t originalKeyCode);
808
809 inline const KeyedVector<int32_t, int32_t>& getFallbackKeys() const {
810 return mFallbackKeys;
811 }
812
813 private:
814 struct KeyMemento {
815 int32_t deviceId;
816 uint32_t source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100817 int32_t displayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800818 int32_t keyCode;
819 int32_t scanCode;
820 int32_t metaState;
821 int32_t flags;
822 nsecs_t downTime;
823 uint32_t policyFlags;
824 };
825
826 struct MotionMemento {
827 int32_t deviceId;
828 uint32_t source;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800829 int32_t displayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800830 int32_t flags;
831 float xPrecision;
832 float yPrecision;
833 nsecs_t downTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800834 uint32_t pointerCount;
835 PointerProperties pointerProperties[MAX_POINTERS];
836 PointerCoords pointerCoords[MAX_POINTERS];
837 bool hovering;
838 uint32_t policyFlags;
839
840 void setPointers(const MotionEntry* entry);
841 };
842
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800843 std::vector<KeyMemento> mKeyMementos;
844 std::vector<MotionMemento> mMotionMementos;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800845 KeyedVector<int32_t, int32_t> mFallbackKeys;
846
847 ssize_t findKeyMemento(const KeyEntry* entry) const;
848 ssize_t findMotionMemento(const MotionEntry* entry, bool hovering) const;
849
850 void addKeyMemento(const KeyEntry* entry, int32_t flags);
851 void addMotionMemento(const MotionEntry* entry, int32_t flags, bool hovering);
852
853 static bool shouldCancelKey(const KeyMemento& memento,
854 const CancelationOptions& options);
855 static bool shouldCancelMotion(const MotionMemento& memento,
856 const CancelationOptions& options);
857 };
858
859 /* Manages the dispatch state associated with a single input channel. */
860 class Connection : public RefBase {
861 protected:
862 virtual ~Connection();
863
864 public:
865 enum Status {
866 // Everything is peachy.
867 STATUS_NORMAL,
868 // An unrecoverable communication error has occurred.
869 STATUS_BROKEN,
870 // The input channel has been unregistered.
871 STATUS_ZOMBIE
872 };
873
874 Status status;
875 sp<InputChannel> inputChannel; // never null
Michael Wrightd02c5b62014-02-10 15:10:22 -0800876 bool monitor;
877 InputPublisher inputPublisher;
878 InputState inputState;
879
880 // True if the socket is full and no further events can be published until
881 // the application consumes some of the input.
882 bool inputPublisherBlocked;
883
884 // Queue of events that need to be published to the connection.
885 Queue<DispatchEntry> outboundQueue;
886
887 // Queue of events that have been published to the connection but that have not
888 // yet received a "finished" response from the application.
889 Queue<DispatchEntry> waitQueue;
890
Robert Carr803535b2018-08-02 16:38:15 -0700891 explicit Connection(const sp<InputChannel>& inputChannel, bool monitor);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800892
Siarhei Vishniakou587c3f02018-01-04 11:46:44 -0800893 inline const std::string getInputChannelName() const { return inputChannel->getName(); }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800894
Siarhei Vishniakou587c3f02018-01-04 11:46:44 -0800895 const std::string getWindowName() const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800896 const char* getStatusLabel() const;
897
898 DispatchEntry* findWaitQueueEntry(uint32_t seq);
899 };
900
Michael Wright3dd60e22019-03-27 22:06:44 +0000901 struct Monitor {
902 sp<InputChannel> inputChannel; // never null
903
904 explicit Monitor(const sp<InputChannel>& inputChannel);
905 };
906
Michael Wrightd02c5b62014-02-10 15:10:22 -0800907 enum DropReason {
908 DROP_REASON_NOT_DROPPED = 0,
909 DROP_REASON_POLICY = 1,
910 DROP_REASON_APP_SWITCH = 2,
911 DROP_REASON_DISABLED = 3,
912 DROP_REASON_BLOCKED = 4,
913 DROP_REASON_STALE = 5,
914 };
915
916 sp<InputDispatcherPolicyInterface> mPolicy;
917 InputDispatcherConfiguration mConfig;
918
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800919 std::mutex mLock;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800920
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800921 std::condition_variable mDispatcherIsAlive;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800922
923 sp<Looper> mLooper;
924
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800925 EventEntry* mPendingEvent GUARDED_BY(mLock);
926 Queue<EventEntry> mInboundQueue GUARDED_BY(mLock);
927 Queue<EventEntry> mRecentQueue GUARDED_BY(mLock);
928 Queue<CommandEntry> mCommandQueue GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800929
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800930 DropReason mLastDropReason GUARDED_BY(mLock);
Michael Wright3a981722015-06-10 15:26:13 +0100931
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800932 void dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800933
934 // Enqueues an inbound event. Returns true if mLooper->wake() should be called.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800935 bool enqueueInboundEventLocked(EventEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800936
937 // Cleans up input state when dropping an inbound event.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800938 void dropInboundEventLocked(EventEntry* entry, DropReason dropReason) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800939
940 // Adds an event to a queue of recent events for debugging purposes.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800941 void addRecentEventLocked(EventEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800942
943 // App switch latency optimization.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800944 bool mAppSwitchSawKeyDown GUARDED_BY(mLock);
945 nsecs_t mAppSwitchDueTime GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800946
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800947 bool isAppSwitchKeyEvent(KeyEntry* keyEntry);
948 bool isAppSwitchPendingLocked() REQUIRES(mLock);
949 void resetPendingAppSwitchLocked(bool handled) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800950
951 // Stale event latency optimization.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800952 static bool isStaleEvent(nsecs_t currentTime, EventEntry* entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800953
954 // Blocked event latency optimization. Drops old events when the user intends
955 // to transfer focus to a new application.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800956 EventEntry* mNextUnblockedEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800957
Tiger Huang85b8c5e2019-01-17 18:34:54 +0800958 sp<InputWindowHandle> findTouchedWindowAtLocked(int32_t displayId, int32_t x, int32_t y,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800959 bool addOutsideTargets = false, bool addPortalWindows = false) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800960
961 // All registered connections mapped by channel file descriptor.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800962 KeyedVector<int, sp<Connection> > mConnectionsByFd GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800963
Robert Carr5c8a0262018-10-03 16:30:44 -0700964 struct IBinderHash {
965 std::size_t operator()(const sp<IBinder>& b) const {
966 return std::hash<IBinder *>{}(b.get());
967 }
968 };
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800969 std::unordered_map<sp<IBinder>, sp<InputChannel>, IBinderHash> mInputChannelsByToken
970 GUARDED_BY(mLock);
Robert Carr5c8a0262018-10-03 16:30:44 -0700971
Michael Wright3dd60e22019-03-27 22:06:44 +0000972 // Finds the display ID of the gesture monitor identified by the provided token.
973 std::optional<int32_t> findGestureMonitorDisplayByTokenLocked(const sp<IBinder>& token)
974 REQUIRES(mLock);
975
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800976 ssize_t getConnectionIndexLocked(const sp<InputChannel>& inputChannel) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800977
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800978 // Input channels that will receive a copy of all input events sent to the provided display.
Michael Wright3dd60e22019-03-27 22:06:44 +0000979 std::unordered_map<int32_t, std::vector<Monitor>> mGlobalMonitorsByDisplay
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800980 GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800981
Michael Wright3dd60e22019-03-27 22:06:44 +0000982 // Input channels that will receive pointer events that start within the corresponding display.
983 // These are a bit special when compared to global monitors since they'll cause gesture streams
984 // to continue even when there isn't a touched window,and have the ability to steal the rest of
985 // the pointer stream in order to claim it for a system gesture.
986 std::unordered_map<int32_t, std::vector<Monitor>> mGestureMonitorsByDisplay
987 GUARDED_BY(mLock);
988
989
Michael Wrightd02c5b62014-02-10 15:10:22 -0800990 // Event injection and synchronization.
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800991 std::condition_variable mInjectionResultAvailable;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800992 bool hasInjectionPermission(int32_t injectorPid, int32_t injectorUid);
Siarhei Vishniakou62683e82019-03-06 17:59:56 -0800993 void setInjectionResult(EventEntry* entry, int32_t injectionResult);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800994
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800995 std::condition_variable mInjectionSyncFinished;
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800996 void incrementPendingForegroundDispatches(EventEntry* entry);
Siarhei Vishniakou62683e82019-03-06 17:59:56 -0800997 void decrementPendingForegroundDispatches(EventEntry* entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800998
999 // Key repeat tracking.
1000 struct KeyRepeatState {
1001 KeyEntry* lastKeyEntry; // or null if no repeat
1002 nsecs_t nextRepeatTime;
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001003 } mKeyRepeatState GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001004
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001005 void resetKeyRepeatLocked() REQUIRES(mLock);
1006 KeyEntry* synthesizeKeyRepeatLocked(nsecs_t currentTime) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001007
Michael Wright78f24442014-08-06 15:55:28 -07001008 // Key replacement tracking
1009 struct KeyReplacement {
1010 int32_t keyCode;
1011 int32_t deviceId;
1012 bool operator==(const KeyReplacement& rhs) const {
1013 return keyCode == rhs.keyCode && deviceId == rhs.deviceId;
1014 }
1015 bool operator<(const KeyReplacement& rhs) const {
1016 return keyCode != rhs.keyCode ? keyCode < rhs.keyCode : deviceId < rhs.deviceId;
1017 }
1018 };
1019 // Maps the key code replaced, device id tuple to the key code it was replaced with
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001020 KeyedVector<KeyReplacement, int32_t> mReplacedKeys GUARDED_BY(mLock);
Siarhei Vishniakou61fafdd2018-04-13 11:00:58 -05001021 // Process certain Meta + Key combinations
1022 void accelerateMetaShortcuts(const int32_t deviceId, const int32_t action,
1023 int32_t& keyCode, int32_t& metaState);
Michael Wright78f24442014-08-06 15:55:28 -07001024
Michael Wrightd02c5b62014-02-10 15:10:22 -08001025 // Deferred command processing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001026 bool haveCommandsLocked() const REQUIRES(mLock);
1027 bool runCommandsLockedInterruptible() REQUIRES(mLock);
1028 CommandEntry* postCommandLocked(Command command) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001029
1030 // Input filter processing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001031 bool shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) REQUIRES(mLock);
1032 bool shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001033
1034 // Inbound event processing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001035 void drainInboundQueueLocked() REQUIRES(mLock);
1036 void releasePendingEventLocked() REQUIRES(mLock);
1037 void releaseInboundEventLocked(EventEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001038
1039 // Dispatch state.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001040 bool mDispatchEnabled GUARDED_BY(mLock);
1041 bool mDispatchFrozen GUARDED_BY(mLock);
1042 bool mInputFilterEnabled GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001043
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001044 std::unordered_map<int32_t, std::vector<sp<InputWindowHandle>>> mWindowHandlesByDisplay
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001045 GUARDED_BY(mLock);
Arthur Hungb92218b2018-08-14 12:00:21 +08001046 // Get window handles by display, return an empty vector if not found.
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001047 std::vector<sp<InputWindowHandle>> getWindowHandlesLocked(int32_t displayId) const
1048 REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001049 sp<InputWindowHandle> getWindowHandleLocked(const sp<IBinder>& windowHandleToken) const
1050 REQUIRES(mLock);
1051 sp<InputChannel> getInputChannelLocked(const sp<IBinder>& windowToken) const REQUIRES(mLock);
1052 bool hasWindowHandleLocked(const sp<InputWindowHandle>& windowHandle) const REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001053
1054 // Focus tracking for keys, trackball, etc.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001055 std::unordered_map<int32_t, sp<InputWindowHandle>> mFocusedWindowHandlesByDisplay
1056 GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001057
1058 // Focus tracking for touch.
1059 struct TouchedWindow {
1060 sp<InputWindowHandle> windowHandle;
1061 int32_t targetFlags;
1062 BitSet32 pointerIds; // zero unless target flag FLAG_SPLIT is set
1063 };
Michael Wright3dd60e22019-03-27 22:06:44 +00001064
1065 // For tracking the offsets we need to apply when adding gesture monitor targets.
1066 struct TouchedMonitor {
1067 Monitor monitor;
1068 float xOffset = 0.f;
1069 float yOffset = 0.f;
1070
1071 explicit TouchedMonitor(const Monitor& monitor, float xOffset, float yOffset);
1072 };
1073
Michael Wrightd02c5b62014-02-10 15:10:22 -08001074 struct TouchState {
1075 bool down;
1076 bool split;
1077 int32_t deviceId; // id of the device that is currently down, others are rejected
1078 uint32_t source; // source of the device that is current down, others are rejected
1079 int32_t displayId; // id to the display that currently has a touch, others are rejected
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001080 std::vector<TouchedWindow> windows;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001081
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001082 // This collects the portal windows that the touch has gone through. Each portal window
1083 // targets a display (embedded display for most cases). With this info, we can add the
1084 // monitoring channels of the displays touched.
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001085 std::vector<sp<InputWindowHandle>> portalWindows;
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001086
Michael Wright3dd60e22019-03-27 22:06:44 +00001087 std::vector<TouchedMonitor> gestureMonitors;
1088
Michael Wrightd02c5b62014-02-10 15:10:22 -08001089 TouchState();
1090 ~TouchState();
1091 void reset();
1092 void copyFrom(const TouchState& other);
1093 void addOrUpdateWindow(const sp<InputWindowHandle>& windowHandle,
1094 int32_t targetFlags, BitSet32 pointerIds);
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001095 void addPortalWindow(const sp<InputWindowHandle>& windowHandle);
Michael Wright3dd60e22019-03-27 22:06:44 +00001096 void addGestureMonitors(const std::vector<TouchedMonitor>& monitors);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001097 void removeWindow(const sp<InputWindowHandle>& windowHandle);
Robert Carr803535b2018-08-02 16:38:15 -07001098 void removeWindowByToken(const sp<IBinder>& token);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001099 void filterNonAsIsTouchWindows();
Michael Wright3dd60e22019-03-27 22:06:44 +00001100 void filterNonMonitors();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001101 sp<InputWindowHandle> getFirstForegroundWindowHandle() const;
1102 bool isSlippery() const;
1103 };
1104
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001105 KeyedVector<int32_t, TouchState> mTouchStatesByDisplay GUARDED_BY(mLock);
1106 TouchState mTempTouchState GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001107
Tiger Huang721e26f2018-07-24 22:26:19 +08001108 // Focused applications.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001109 std::unordered_map<int32_t, sp<InputApplicationHandle>> mFocusedApplicationHandlesByDisplay
1110 GUARDED_BY(mLock);
Tiger Huang721e26f2018-07-24 22:26:19 +08001111
1112 // Top focused display.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001113 int32_t mFocusedDisplayId GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001114
1115 // Dispatcher state at time of last ANR.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001116 std::string mLastANRState GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001117
1118 // Dispatch inbound events.
1119 bool dispatchConfigurationChangedLocked(
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001120 nsecs_t currentTime, ConfigurationChangedEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001121 bool dispatchDeviceResetLocked(
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001122 nsecs_t currentTime, DeviceResetEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001123 bool dispatchKeyLocked(
1124 nsecs_t currentTime, KeyEntry* entry,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001125 DropReason* dropReason, nsecs_t* nextWakeupTime) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001126 bool dispatchMotionLocked(
1127 nsecs_t currentTime, MotionEntry* entry,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001128 DropReason* dropReason, nsecs_t* nextWakeupTime) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001129 void dispatchEventLocked(nsecs_t currentTime, EventEntry* entry,
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001130 const std::vector<InputTarget>& inputTargets) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001131
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001132 void logOutboundKeyDetails(const char* prefix, const KeyEntry* entry);
1133 void logOutboundMotionDetails(const char* prefix, const MotionEntry* entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001134
1135 // Keeping track of ANR timeouts.
1136 enum InputTargetWaitCause {
1137 INPUT_TARGET_WAIT_CAUSE_NONE,
1138 INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY,
1139 INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY,
1140 };
1141
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001142 InputTargetWaitCause mInputTargetWaitCause GUARDED_BY(mLock);
1143 nsecs_t mInputTargetWaitStartTime GUARDED_BY(mLock);
1144 nsecs_t mInputTargetWaitTimeoutTime GUARDED_BY(mLock);
1145 bool mInputTargetWaitTimeoutExpired GUARDED_BY(mLock);
1146 sp<IBinder> mInputTargetWaitApplicationToken GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001147
1148 // Contains the last window which received a hover event.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001149 sp<InputWindowHandle> mLastHoverWindowHandle GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001150
1151 // Finding targets for input events.
1152 int32_t handleTargetsNotReadyLocked(nsecs_t currentTime, const EventEntry* entry,
1153 const sp<InputApplicationHandle>& applicationHandle,
1154 const sp<InputWindowHandle>& windowHandle,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001155 nsecs_t* nextWakeupTime, const char* reason) REQUIRES(mLock);
Robert Carr803535b2018-08-02 16:38:15 -07001156
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001157 void removeWindowByTokenLocked(const sp<IBinder>& token) REQUIRES(mLock);
Robert Carr803535b2018-08-02 16:38:15 -07001158
Michael Wrightd02c5b62014-02-10 15:10:22 -08001159 void resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001160 const sp<InputChannel>& inputChannel) REQUIRES(mLock);
1161 nsecs_t getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime) REQUIRES(mLock);
1162 void resetANRTimeoutsLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001163
Tiger Huang721e26f2018-07-24 22:26:19 +08001164 int32_t getTargetDisplayId(const EventEntry* entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001165 int32_t findFocusedWindowTargetsLocked(nsecs_t currentTime, const EventEntry* entry,
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001166 std::vector<InputTarget>& inputTargets, nsecs_t* nextWakeupTime) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001167 int32_t findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry* entry,
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001168 std::vector<InputTarget>& inputTargets, nsecs_t* nextWakeupTime,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001169 bool* outConflictingPointerActions) REQUIRES(mLock);
Michael Wright3dd60e22019-03-27 22:06:44 +00001170 std::vector<TouchedMonitor> findTouchedGestureMonitorsLocked(int32_t displayId,
1171 const std::vector<sp<InputWindowHandle>>& portalWindows) REQUIRES(mLock);
1172 void addGestureMonitors(const std::vector<Monitor>& monitors,
1173 std::vector<TouchedMonitor>& outTouchedMonitors, float xOffset = 0, float yOffset = 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001174
1175 void addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle,
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001176 int32_t targetFlags, BitSet32 pointerIds, std::vector<InputTarget>& inputTargets)
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001177 REQUIRES(mLock);
Michael Wright3dd60e22019-03-27 22:06:44 +00001178 void addMonitoringTargetLocked(const Monitor& monitor, float xOffset, float yOffset,
1179 std::vector<InputTarget>& inputTargets) REQUIRES(mLock);
1180 void addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets,
1181 int32_t displayId, float xOffset = 0, float yOffset = 0) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001182
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001183 void pokeUserActivityLocked(const EventEntry* eventEntry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001184 bool checkInjectionPermission(const sp<InputWindowHandle>& windowHandle,
1185 const InjectionState* injectionState);
1186 bool isWindowObscuredAtPointLocked(const sp<InputWindowHandle>& windowHandle,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001187 int32_t x, int32_t y) const REQUIRES(mLock);
1188 bool isWindowObscuredLocked(const sp<InputWindowHandle>& windowHandle) const REQUIRES(mLock);
1189 std::string getApplicationWindowLabel(const sp<InputApplicationHandle>& applicationHandle,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001190 const sp<InputWindowHandle>& windowHandle);
1191
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001192 std::string checkWindowReadyForMoreInputLocked(nsecs_t currentTime,
Jeff Brownffb49772014-10-10 19:01:34 -07001193 const sp<InputWindowHandle>& windowHandle, const EventEntry* eventEntry,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001194 const char* targetType) REQUIRES(mLock);
Jeff Brownffb49772014-10-10 19:01:34 -07001195
Michael Wrightd02c5b62014-02-10 15:10:22 -08001196 // Manage the dispatch cycle for a single connection.
1197 // These methods are deliberately not Interruptible because doing all of the work
1198 // with the mutex held makes it easier to ensure that connection invariants are maintained.
1199 // If needed, the methods post commands to run later once the critical bits are done.
1200 void prepareDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001201 EventEntry* eventEntry, const InputTarget* inputTarget) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001202 void enqueueDispatchEntriesLocked(nsecs_t currentTime, const sp<Connection>& connection,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001203 EventEntry* eventEntry, const InputTarget* inputTarget) REQUIRES(mLock);
chaviw8c9cf542019-03-25 13:02:48 -07001204 void enqueueDispatchEntryLocked(const sp<Connection>& connection,
1205 EventEntry* eventEntry, const InputTarget* inputTarget, int32_t dispatchMode)
1206 REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001207 void startDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection)
1208 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001209 void finishDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001210 uint32_t seq, bool handled) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001211 void abortBrokenDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001212 bool notify) REQUIRES(mLock);
Siarhei Vishniakou62683e82019-03-06 17:59:56 -08001213 void drainDispatchQueue(Queue<DispatchEntry>* queue);
1214 void releaseDispatchEntry(DispatchEntry* dispatchEntry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001215 static int handleReceiveCallback(int fd, int events, void* data);
chaviw8c9cf542019-03-25 13:02:48 -07001216 // The action sent should only be of type AMOTION_EVENT_*
chaviwfd6d3512019-03-25 13:23:49 -07001217 void dispatchPointerDownOutsideFocus(uint32_t source, int32_t action,
chaviw8c9cf542019-03-25 13:02:48 -07001218 const sp<IBinder>& newToken) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001219
1220 void synthesizeCancelationEventsForAllConnectionsLocked(
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001221 const CancelationOptions& options) REQUIRES(mLock);
1222 void synthesizeCancelationEventsForMonitorsLocked(
1223 const CancelationOptions& options) REQUIRES(mLock);
Michael Wright3dd60e22019-03-27 22:06:44 +00001224 void synthesizeCancelationEventsForMonitorsLocked(const CancelationOptions& options,
1225 std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001226 void synthesizeCancelationEventsForInputChannelLocked(const sp<InputChannel>& channel,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001227 const CancelationOptions& options) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001228 void synthesizeCancelationEventsForConnectionLocked(const sp<Connection>& connection,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001229 const CancelationOptions& options) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001230
1231 // Splitting motion events across windows.
1232 MotionEntry* splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds);
1233
1234 // Reset and drop everything the dispatcher is doing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001235 void resetAndDropEverythingLocked(const char* reason) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001236
1237 // Dump state.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001238 void dumpDispatchStateLocked(std::string& dump) REQUIRES(mLock);
Michael Wright3dd60e22019-03-27 22:06:44 +00001239 void dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001240 void logDispatchStateLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001241
1242 // Registration.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001243 void removeMonitorChannelLocked(const sp<InputChannel>& inputChannel) REQUIRES(mLock);
Michael Wright3dd60e22019-03-27 22:06:44 +00001244 void removeMonitorChannelLocked(const sp<InputChannel>& inputChannel,
1245 std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay)
1246 REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001247 status_t unregisterInputChannelLocked(const sp<InputChannel>& inputChannel, bool notify)
1248 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001249
1250 // Interesting events that we might like to log or tell the framework about.
1251 void onDispatchCycleFinishedLocked(
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001252 nsecs_t currentTime, const sp<Connection>& connection, uint32_t seq, bool handled)
1253 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001254 void onDispatchCycleBrokenLocked(
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001255 nsecs_t currentTime, const sp<Connection>& connection) REQUIRES(mLock);
chaviw0c06c6e2019-01-09 13:27:07 -08001256 void onFocusChangedLocked(const sp<InputWindowHandle>& oldFocus,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001257 const sp<InputWindowHandle>& newFocus) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001258 void onANRLocked(
1259 nsecs_t currentTime, const sp<InputApplicationHandle>& applicationHandle,
1260 const sp<InputWindowHandle>& windowHandle,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001261 nsecs_t eventTime, nsecs_t waitStartTime, const char* reason) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001262
1263 // Outbound policy interactions.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001264 void doNotifyConfigurationChangedLockedInterruptible(CommandEntry* commandEntry)
1265 REQUIRES(mLock);
1266 void doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
1267 void doNotifyFocusChangedLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
1268 void doNotifyANRLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
1269 void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry)
1270 REQUIRES(mLock);
1271 void doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001272 bool afterKeyEventLockedInterruptible(const sp<Connection>& connection,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001273 DispatchEntry* dispatchEntry, KeyEntry* keyEntry, bool handled) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001274 bool afterMotionEventLockedInterruptible(const sp<Connection>& connection,
Siarhei Vishniakou62683e82019-03-06 17:59:56 -08001275 DispatchEntry* dispatchEntry, MotionEntry* motionEntry, bool handled) REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001276 void doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001277 void initializeKeyEvent(KeyEvent* event, const KeyEntry* entry);
chaviwfd6d3512019-03-25 13:23:49 -07001278 void doOnPointerDownOutsideFocusLockedInterruptible(CommandEntry* commandEntry)
1279 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001280
1281 // Statistics gathering.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001282 void updateDispatchStatistics(nsecs_t currentTime, const EventEntry* entry,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001283 int32_t injectionResult, nsecs_t timeSpentWaitingForApplication);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001284 void traceInboundQueueLengthLocked() REQUIRES(mLock);
1285 void traceOutboundQueueLength(const sp<Connection>& connection);
1286 void traceWaitQueueLength(const sp<Connection>& connection);
Prabir Pradhanf93562f2018-11-29 12:13:37 -08001287
Prabir Pradhan79a4f0c2019-01-09 11:24:01 -08001288 sp<InputReporterInterface> mReporter;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001289};
1290
1291/* Enqueues and dispatches input events, endlessly. */
1292class InputDispatcherThread : public Thread {
1293public:
1294 explicit InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher);
1295 ~InputDispatcherThread();
1296
1297private:
1298 virtual bool threadLoop();
1299
1300 sp<InputDispatcherInterface> mDispatcher;
1301};
1302
1303} // namespace android
1304
1305#endif // _UI_INPUT_DISPATCHER_H