blob: d73c4d6dd7be61395ca77b7306b7d2aae2e3f29e [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _UI_INPUT_DISPATCHER_H
18#define _UI_INPUT_DISPATCHER_H
19
Siarhei Vishniakou443ad902019-03-06 17:25:41 -080020#include <condition_variable>
Michael Wrightd02c5b62014-02-10 15:10:22 -080021#include <input/Input.h>
Robert Carr3720ed02018-08-08 16:08:27 -070022#include <input/InputApplication.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080023#include <input/InputTransport.h>
Robert Carr3720ed02018-08-08 16:08:27 -070024#include <input/InputWindow.h>
chaviw291d88a2019-02-14 10:33:58 -080025#include <input/ISetInputWindowsListener.h>
Michael Wright3dd60e22019-03-27 22:06:44 +000026#include <optional>
27#include <ui/Region.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080028#include <utils/threads.h>
29#include <utils/Timers.h>
30#include <utils/RefBase.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080031#include <utils/Looper.h>
32#include <utils/BitSet.h>
33#include <cutils/atomic.h>
Robert Carr5c8a0262018-10-03 16:30:44 -070034#include <unordered_map>
Michael Wrightd02c5b62014-02-10 15:10:22 -080035
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -070036#include <limits.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080037#include <stddef.h>
38#include <unistd.h>
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -070039#include <deque>
Arthur Hungb92218b2018-08-14 12:00:21 +080040#include <unordered_map>
Michael Wrightd02c5b62014-02-10 15:10:22 -080041
Michael Wrightd02c5b62014-02-10 15:10:22 -080042#include "InputListener.h"
Prabir Pradhan79a4f0c2019-01-09 11:24:01 -080043#include "InputReporterInterface.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080044
Michael Wrightd02c5b62014-02-10 15:10:22 -080045namespace android {
46
47/*
48 * Constants used to report the outcome of input event injection.
49 */
50enum {
51 /* (INTERNAL USE ONLY) Specifies that injection is pending and its outcome is unknown. */
52 INPUT_EVENT_INJECTION_PENDING = -1,
53
54 /* Injection succeeded. */
55 INPUT_EVENT_INJECTION_SUCCEEDED = 0,
56
57 /* Injection failed because the injector did not have permission to inject
58 * into the application with input focus. */
59 INPUT_EVENT_INJECTION_PERMISSION_DENIED = 1,
60
61 /* Injection failed because there were no available input targets. */
62 INPUT_EVENT_INJECTION_FAILED = 2,
63
64 /* Injection failed due to a timeout. */
65 INPUT_EVENT_INJECTION_TIMED_OUT = 3
66};
67
68/*
69 * Constants used to determine the input event injection synchronization mode.
70 */
71enum {
72 /* Injection is asynchronous and is assumed always to be successful. */
73 INPUT_EVENT_INJECTION_SYNC_NONE = 0,
74
75 /* Waits for previous events to be dispatched so that the input dispatcher can determine
76 * whether input event injection willbe permitted based on the current input focus.
77 * Does not wait for the input event to finish processing. */
78 INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT = 1,
79
80 /* Waits for the input event to be completely processed. */
81 INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISHED = 2,
82};
83
84
85/*
86 * An input target specifies how an input event is to be dispatched to a particular window
87 * including the window's input channel, control flags, a timeout, and an X / Y offset to
88 * be added to input event coordinates to compensate for the absolute position of the
89 * window area.
90 */
91struct InputTarget {
92 enum {
93 /* This flag indicates that the event is being delivered to a foreground application. */
94 FLAG_FOREGROUND = 1 << 0,
95
Michael Wrightcdcd8f22016-03-22 16:52:13 -070096 /* This flag indicates that the MotionEvent falls within the area of the target
Michael Wrightd02c5b62014-02-10 15:10:22 -080097 * obscured by another visible window above it. The motion event should be
98 * delivered with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED. */
99 FLAG_WINDOW_IS_OBSCURED = 1 << 1,
100
101 /* This flag indicates that a motion event is being split across multiple windows. */
102 FLAG_SPLIT = 1 << 2,
103
104 /* This flag indicates that the pointer coordinates dispatched to the application
105 * will be zeroed out to avoid revealing information to an application. This is
106 * used in conjunction with FLAG_DISPATCH_AS_OUTSIDE to prevent apps not sharing
107 * the same UID from watching all touches. */
108 FLAG_ZERO_COORDS = 1 << 3,
109
110 /* This flag indicates that the event should be sent as is.
111 * Should always be set unless the event is to be transmuted. */
112 FLAG_DISPATCH_AS_IS = 1 << 8,
113
114 /* This flag indicates that a MotionEvent with AMOTION_EVENT_ACTION_DOWN falls outside
115 * of the area of this target and so should instead be delivered as an
116 * AMOTION_EVENT_ACTION_OUTSIDE to this target. */
117 FLAG_DISPATCH_AS_OUTSIDE = 1 << 9,
118
119 /* This flag indicates that a hover sequence is starting in the given window.
120 * The event is transmuted into ACTION_HOVER_ENTER. */
121 FLAG_DISPATCH_AS_HOVER_ENTER = 1 << 10,
122
123 /* This flag indicates that a hover event happened outside of a window which handled
124 * previous hover events, signifying the end of the current hover sequence for that
125 * window.
126 * The event is transmuted into ACTION_HOVER_ENTER. */
127 FLAG_DISPATCH_AS_HOVER_EXIT = 1 << 11,
128
129 /* This flag indicates that the event should be canceled.
130 * It is used to transmute ACTION_MOVE into ACTION_CANCEL when a touch slips
131 * outside of a window. */
132 FLAG_DISPATCH_AS_SLIPPERY_EXIT = 1 << 12,
133
134 /* This flag indicates that the event should be dispatched as an initial down.
135 * It is used to transmute ACTION_MOVE into ACTION_DOWN when a touch slips
136 * into a new window. */
137 FLAG_DISPATCH_AS_SLIPPERY_ENTER = 1 << 13,
138
139 /* Mask for all dispatch modes. */
140 FLAG_DISPATCH_MASK = FLAG_DISPATCH_AS_IS
141 | FLAG_DISPATCH_AS_OUTSIDE
142 | FLAG_DISPATCH_AS_HOVER_ENTER
143 | FLAG_DISPATCH_AS_HOVER_EXIT
144 | FLAG_DISPATCH_AS_SLIPPERY_EXIT
145 | FLAG_DISPATCH_AS_SLIPPERY_ENTER,
Michael Wrightcdcd8f22016-03-22 16:52:13 -0700146
147 /* This flag indicates that the target of a MotionEvent is partly or wholly
148 * obscured by another visible window above it. The motion event should be
149 * delivered with flag AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED. */
150 FLAG_WINDOW_IS_PARTIALLY_OBSCURED = 1 << 14,
151
Michael Wrightd02c5b62014-02-10 15:10:22 -0800152 };
153
154 // The input channel to be targeted.
155 sp<InputChannel> inputChannel;
156
157 // Flags for the input target.
158 int32_t flags;
159
160 // The x and y offset to add to a MotionEvent as it is delivered.
161 // (ignored for KeyEvents)
162 float xOffset, yOffset;
163
164 // Scaling factor to apply to MotionEvent as it is delivered.
165 // (ignored for KeyEvents)
Robert Carre07e1032018-11-26 12:55:53 -0800166 float globalScaleFactor;
167 float windowXScale = 1.0f;
168 float windowYScale = 1.0f;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800169
170 // The subset of pointer ids to include in motion events dispatched to this input target
171 // if FLAG_SPLIT is set.
172 BitSet32 pointerIds;
173};
174
175
176/*
177 * Input dispatcher configuration.
178 *
179 * Specifies various options that modify the behavior of the input dispatcher.
180 * The values provided here are merely defaults. The actual values will come from ViewConfiguration
181 * and are passed into the dispatcher during initialization.
182 */
183struct InputDispatcherConfiguration {
184 // The key repeat initial timeout.
185 nsecs_t keyRepeatTimeout;
186
187 // The key repeat inter-key delay.
188 nsecs_t keyRepeatDelay;
189
190 InputDispatcherConfiguration() :
191 keyRepeatTimeout(500 * 1000000LL),
192 keyRepeatDelay(50 * 1000000LL) { }
193};
194
195
196/*
197 * Input dispatcher policy interface.
198 *
199 * The input reader policy is used by the input reader to interact with the Window Manager
200 * and other system components.
201 *
202 * The actual implementation is partially supported by callbacks into the DVM
203 * via JNI. This interface is also mocked in the unit tests.
204 */
205class InputDispatcherPolicyInterface : public virtual RefBase {
206protected:
207 InputDispatcherPolicyInterface() { }
208 virtual ~InputDispatcherPolicyInterface() { }
209
210public:
211 /* Notifies the system that a configuration change has occurred. */
212 virtual void notifyConfigurationChanged(nsecs_t when) = 0;
213
214 /* Notifies the system that an application is not responding.
215 * Returns a new timeout to continue waiting, or 0 to abort dispatch. */
216 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Robert Carr803535b2018-08-02 16:38:15 -0700217 const sp<IBinder>& token,
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800218 const std::string& reason) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800219
220 /* Notifies the system that an input channel is unrecoverably broken. */
Robert Carr803535b2018-08-02 16:38:15 -0700221 virtual void notifyInputChannelBroken(const sp<IBinder>& token) = 0;
chaviw0c06c6e2019-01-09 13:27:07 -0800222 virtual void notifyFocusChanged(const sp<IBinder>& oldToken, const sp<IBinder>& newToken) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800223
224 /* Gets the input dispatcher configuration. */
225 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) = 0;
226
Michael Wrightd02c5b62014-02-10 15:10:22 -0800227 /* Filters an input event.
228 * Return true to dispatch the event unmodified, false to consume the event.
229 * A filter can also transform and inject events later by passing POLICY_FLAG_FILTERED
230 * to injectInputEvent.
231 */
232 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) = 0;
233
234 /* Intercepts a key event immediately before queueing it.
235 * The policy can use this method as an opportunity to perform power management functions
236 * and early event preprocessing such as updating policy flags.
237 *
238 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
239 * should be dispatched to applications.
240 */
241 virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags) = 0;
242
243 /* Intercepts a touch, trackball or other motion event before queueing it.
244 * The policy can use this method as an opportunity to perform power management functions
245 * and early event preprocessing such as updating policy flags.
246 *
247 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
248 * should be dispatched to applications.
249 */
Charles Chen3611f1f2019-01-29 17:26:18 +0800250 virtual void interceptMotionBeforeQueueing(const int32_t displayId, nsecs_t when,
251 uint32_t& policyFlags) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800252
253 /* Allows the policy a chance to intercept a key before dispatching. */
Robert Carr803535b2018-08-02 16:38:15 -0700254 virtual nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>& token,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800255 const KeyEvent* keyEvent, uint32_t policyFlags) = 0;
256
257 /* Allows the policy a chance to perform default processing for an unhandled key.
258 * Returns an alternate keycode to redispatch as a fallback, or 0 to give up. */
Robert Carr803535b2018-08-02 16:38:15 -0700259 virtual bool dispatchUnhandledKey(const sp<IBinder>& token,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800260 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) = 0;
261
262 /* Notifies the policy about switch events.
263 */
264 virtual void notifySwitch(nsecs_t when,
265 uint32_t switchValues, uint32_t switchMask, uint32_t policyFlags) = 0;
266
267 /* Poke user activity for an event dispatched to a window. */
268 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType) = 0;
269
270 /* Checks whether a given application pid/uid has permission to inject input events
271 * into other applications.
272 *
273 * This method is special in that its implementation promises to be non-reentrant and
274 * is safe to call while holding other locks. (Most other methods make no such guarantees!)
275 */
276 virtual bool checkInjectEventsPermissionNonReentrant(
277 int32_t injectorPid, int32_t injectorUid) = 0;
chaviwfd6d3512019-03-25 13:23:49 -0700278
279 /* Notifies the policy that a pointer down event has occurred outside the current focused
280 * window.
281 *
282 * The touchedToken passed as an argument is the window that received the input event.
283 */
284 virtual void onPointerDownOutsideFocus(const sp<IBinder>& touchedToken) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800285};
286
287
288/* Notifies the system about input events generated by the input reader.
289 * The dispatcher is expected to be mostly asynchronous. */
290class InputDispatcherInterface : public virtual RefBase, public InputListenerInterface {
291protected:
292 InputDispatcherInterface() { }
293 virtual ~InputDispatcherInterface() { }
294
295public:
296 /* Dumps the state of the input dispatcher.
297 *
298 * This method may be called on any thread (usually by the input manager). */
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800299 virtual void dump(std::string& dump) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800300
301 /* Called by the heatbeat to ensures that the dispatcher has not deadlocked. */
302 virtual void monitor() = 0;
303
304 /* Runs a single iteration of the dispatch loop.
305 * Nominally processes one queued event, a timeout, or a response from an input consumer.
306 *
307 * This method should only be called on the input dispatcher thread.
308 */
309 virtual void dispatchOnce() = 0;
310
311 /* Injects an input event and optionally waits for sync.
312 * The synchronization mode determines whether the method blocks while waiting for
313 * input injection to proceed.
314 * Returns one of the INPUT_EVENT_INJECTION_XXX constants.
315 *
316 * This method may be called on any thread (usually by the input manager).
317 */
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800318 virtual int32_t injectInputEvent(const InputEvent* event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800319 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,
320 uint32_t policyFlags) = 0;
321
322 /* Sets the list of input windows.
323 *
324 * This method may be called on any thread (usually by the input manager).
325 */
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800326 virtual void setInputWindows(const std::vector<sp<InputWindowHandle> >& inputWindowHandles,
chaviw291d88a2019-02-14 10:33:58 -0800327 int32_t displayId,
328 const sp<ISetInputWindowsListener>& setInputWindowsListener = nullptr) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800329
Tiger Huang721e26f2018-07-24 22:26:19 +0800330 /* Sets the focused application on the given display.
Michael Wrightd02c5b62014-02-10 15:10:22 -0800331 *
332 * This method may be called on any thread (usually by the input manager).
333 */
334 virtual void setFocusedApplication(
Tiger Huang721e26f2018-07-24 22:26:19 +0800335 int32_t displayId, const sp<InputApplicationHandle>& inputApplicationHandle) = 0;
336
337 /* Sets the focused display.
338 *
339 * This method may be called on any thread (usually by the input manager).
340 */
341 virtual void setFocusedDisplay(int32_t displayId) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800342
343 /* Sets the input dispatching mode.
344 *
345 * This method may be called on any thread (usually by the input manager).
346 */
347 virtual void setInputDispatchMode(bool enabled, bool frozen) = 0;
348
349 /* Sets whether input event filtering is enabled.
350 * When enabled, incoming input events are sent to the policy's filterInputEvent
351 * method instead of being dispatched. The filter is expected to use
352 * injectInputEvent to inject the events it would like to have dispatched.
353 * It should include POLICY_FLAG_FILTERED in the policy flags during injection.
354 */
355 virtual void setInputFilterEnabled(bool enabled) = 0;
356
chaviwfbe5d9c2018-12-26 12:23:37 -0800357 /* Transfers touch focus from one window to another window.
Michael Wrightd02c5b62014-02-10 15:10:22 -0800358 *
359 * Returns true on success. False if the window did not actually have touch focus.
360 */
chaviwfbe5d9c2018-12-26 12:23:37 -0800361 virtual bool transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken) = 0;
362
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800363 /* Registers input channels that may be used as targets for input events.
Michael Wrightd02c5b62014-02-10 15:10:22 -0800364 *
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800365 * This method may be called on any thread (usually by the input manager).
Michael Wrightd02c5b62014-02-10 15:10:22 -0800366 */
Robert Carr803535b2018-08-02 16:38:15 -0700367 virtual status_t registerInputChannel(
368 const sp<InputChannel>& inputChannel, int32_t displayId) = 0;
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800369
Michael Wright3dd60e22019-03-27 22:06:44 +0000370 /* Registers input channels to be used to monitor input events.
371 *
372 * Each monitor must target a specific display and will only receive input events sent to that
373 * display. If the monitor is a gesture monitor, it will only receive pointer events on the
374 * targeted display.
375 *
376 * This method may be called on any thread (usually by the input manager).
377 */
378 virtual status_t registerInputMonitor(
379 const sp<InputChannel>& inputChannel, int32_t displayId, bool gestureMonitor) = 0;
380
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800381 /* Unregister input channels that will no longer receive input events.
382 *
383 * This method may be called on any thread (usually by the input manager).
384 */
Michael Wrightd02c5b62014-02-10 15:10:22 -0800385 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) = 0;
Michael Wright3dd60e22019-03-27 22:06:44 +0000386
387 /* Allows an input monitor steal the current pointer stream away from normal input windows.
388 *
389 * This method may be called on any thread (usually by the input manager).
390 */
391 virtual status_t pilferPointers(const sp<IBinder>& token) = 0;
392
Michael Wrightd02c5b62014-02-10 15:10:22 -0800393};
394
395/* Dispatches events to input targets. Some functions of the input dispatcher, such as
396 * identifying input targets, are controlled by a separate policy object.
397 *
398 * IMPORTANT INVARIANT:
399 * Because the policy can potentially block or cause re-entrance into the input dispatcher,
400 * the input dispatcher never calls into the policy while holding its internal locks.
401 * The implementation is also carefully designed to recover from scenarios such as an
402 * input channel becoming unregistered while identifying input targets or processing timeouts.
403 *
404 * Methods marked 'Locked' must be called with the lock acquired.
405 *
406 * Methods marked 'LockedInterruptible' must be called with the lock acquired but
407 * may during the course of their execution release the lock, call into the policy, and
408 * then reacquire the lock. The caller is responsible for recovering gracefully.
409 *
410 * A 'LockedInterruptible' method may called a 'Locked' method, but NOT vice-versa.
411 */
412class InputDispatcher : public InputDispatcherInterface {
413protected:
414 virtual ~InputDispatcher();
415
416public:
417 explicit InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy);
418
Michael Wright3dd60e22019-03-27 22:06:44 +0000419 virtual void dump(std::string& dump) override;
420 virtual void monitor() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800421
Michael Wright3dd60e22019-03-27 22:06:44 +0000422 virtual void dispatchOnce() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800423
Michael Wright3dd60e22019-03-27 22:06:44 +0000424 virtual void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) override;
425 virtual void notifyKey(const NotifyKeyArgs* args) override;
426 virtual void notifyMotion(const NotifyMotionArgs* args) override;
427 virtual void notifySwitch(const NotifySwitchArgs* args) override;
428 virtual void notifyDeviceReset(const NotifyDeviceResetArgs* args) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800429
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800430 virtual int32_t injectInputEvent(const InputEvent* event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800431 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,
Michael Wright3dd60e22019-03-27 22:06:44 +0000432 uint32_t policyFlags) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800433
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800434 virtual void setInputWindows(const std::vector<sp<InputWindowHandle> >& inputWindowHandles,
chaviw291d88a2019-02-14 10:33:58 -0800435 int32_t displayId,
Michael Wright3dd60e22019-03-27 22:06:44 +0000436 const sp<ISetInputWindowsListener>& setInputWindowsListener = nullptr) override;
Tiger Huang721e26f2018-07-24 22:26:19 +0800437 virtual void setFocusedApplication(int32_t displayId,
Michael Wright3dd60e22019-03-27 22:06:44 +0000438 const sp<InputApplicationHandle>& inputApplicationHandle) override;
439 virtual void setFocusedDisplay(int32_t displayId) override;
440 virtual void setInputDispatchMode(bool enabled, bool frozen) override;
441 virtual void setInputFilterEnabled(bool enabled) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800442
Michael Wright3dd60e22019-03-27 22:06:44 +0000443 virtual bool transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken)
444 override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800445
446 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel,
Michael Wright3dd60e22019-03-27 22:06:44 +0000447 int32_t displayId) override;
448 virtual status_t registerInputMonitor(const sp<InputChannel>& inputChannel,
449 int32_t displayId, bool isGestureMonitor) override;
450 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) override;
451 virtual status_t pilferPointers(const sp<IBinder>& token) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800452
453private:
454 template <typename T>
455 struct Link {
456 T* next;
457 T* prev;
458
459 protected:
Yi Kong9b14ac62018-07-17 13:48:38 -0700460 inline Link() : next(nullptr), prev(nullptr) { }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800461 };
462
463 struct InjectionState {
464 mutable int32_t refCount;
465
466 int32_t injectorPid;
467 int32_t injectorUid;
468 int32_t injectionResult; // initially INPUT_EVENT_INJECTION_PENDING
469 bool injectionIsAsync; // set to true if injection is not waiting for the result
470 int32_t pendingForegroundDispatches; // the number of foreground dispatches in progress
471
472 InjectionState(int32_t injectorPid, int32_t injectorUid);
473 void release();
474
475 private:
476 ~InjectionState();
477 };
478
479 struct EventEntry : Link<EventEntry> {
480 enum {
481 TYPE_CONFIGURATION_CHANGED,
482 TYPE_DEVICE_RESET,
483 TYPE_KEY,
484 TYPE_MOTION
485 };
486
Prabir Pradhan42611e02018-11-27 14:04:02 -0800487 uint32_t sequenceNum;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800488 mutable int32_t refCount;
489 int32_t type;
490 nsecs_t eventTime;
491 uint32_t policyFlags;
492 InjectionState* injectionState;
493
494 bool dispatchInProgress; // initially false, set to true while dispatching
495
Yi Kong9b14ac62018-07-17 13:48:38 -0700496 inline bool isInjected() const { return injectionState != nullptr; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800497
498 void release();
499
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800500 virtual void appendDescription(std::string& msg) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800501
502 protected:
Prabir Pradhan42611e02018-11-27 14:04:02 -0800503 EventEntry(uint32_t sequenceNum, int32_t type, nsecs_t eventTime, uint32_t policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800504 virtual ~EventEntry();
505 void releaseInjectionState();
506 };
507
508 struct ConfigurationChangedEntry : EventEntry {
Prabir Pradhan42611e02018-11-27 14:04:02 -0800509 explicit ConfigurationChangedEntry(uint32_t sequenceNum, nsecs_t eventTime);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800510 virtual void appendDescription(std::string& msg) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800511
512 protected:
513 virtual ~ConfigurationChangedEntry();
514 };
515
516 struct DeviceResetEntry : EventEntry {
517 int32_t deviceId;
518
Prabir Pradhan42611e02018-11-27 14:04:02 -0800519 DeviceResetEntry(uint32_t sequenceNum, nsecs_t eventTime, int32_t deviceId);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800520 virtual void appendDescription(std::string& msg) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800521
522 protected:
523 virtual ~DeviceResetEntry();
524 };
525
526 struct KeyEntry : EventEntry {
527 int32_t deviceId;
528 uint32_t source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100529 int32_t displayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800530 int32_t action;
531 int32_t flags;
532 int32_t keyCode;
533 int32_t scanCode;
534 int32_t metaState;
535 int32_t repeatCount;
536 nsecs_t downTime;
537
538 bool syntheticRepeat; // set to true for synthetic key repeats
539
540 enum InterceptKeyResult {
541 INTERCEPT_KEY_RESULT_UNKNOWN,
542 INTERCEPT_KEY_RESULT_SKIP,
543 INTERCEPT_KEY_RESULT_CONTINUE,
544 INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER,
545 };
546 InterceptKeyResult interceptKeyResult; // set based on the interception result
547 nsecs_t interceptKeyWakeupTime; // used with INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER
548
Prabir Pradhan42611e02018-11-27 14:04:02 -0800549 KeyEntry(uint32_t sequenceNum, nsecs_t eventTime,
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100550 int32_t deviceId, uint32_t source, int32_t displayId, uint32_t policyFlags,
551 int32_t action, int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800552 int32_t repeatCount, nsecs_t downTime);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800553 virtual void appendDescription(std::string& msg) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800554 void recycle();
555
556 protected:
557 virtual ~KeyEntry();
558 };
559
560 struct MotionEntry : EventEntry {
561 nsecs_t eventTime;
562 int32_t deviceId;
563 uint32_t source;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800564 int32_t displayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800565 int32_t action;
Michael Wright7b159c92015-05-14 14:48:03 +0100566 int32_t actionButton;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800567 int32_t flags;
568 int32_t metaState;
569 int32_t buttonState;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800570 MotionClassification classification;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800571 int32_t edgeFlags;
572 float xPrecision;
573 float yPrecision;
Garfield Tan00f511d2019-06-12 16:55:40 -0700574 float xCursorPosition;
575 float yCursorPosition;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800576 nsecs_t downTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800577 uint32_t pointerCount;
578 PointerProperties pointerProperties[MAX_POINTERS];
579 PointerCoords pointerCoords[MAX_POINTERS];
580
Garfield Tan00f511d2019-06-12 16:55:40 -0700581 MotionEntry(uint32_t sequenceNum, nsecs_t eventTime, int32_t deviceId, uint32_t source,
582 int32_t displayId, uint32_t policyFlags, int32_t action, int32_t actionButton,
583 int32_t flags, int32_t metaState, int32_t buttonState,
584 MotionClassification classification, int32_t edgeFlags, float xPrecision,
585 float yPrecision, float xCursorPosition, float yCursorPosition,
586 nsecs_t downTime, uint32_t pointerCount,
587 const PointerProperties* pointerProperties, const PointerCoords* pointerCoords,
588 float xOffset, float yOffset);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800589 virtual void appendDescription(std::string& msg) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800590
591 protected:
592 virtual ~MotionEntry();
593 };
594
595 // Tracks the progress of dispatching a particular event to a particular connection.
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -0700596 struct DispatchEntry {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800597 const uint32_t seq; // unique sequence number, never 0
598
599 EventEntry* eventEntry; // the event to dispatch
600 int32_t targetFlags;
601 float xOffset;
602 float yOffset;
Robert Carre07e1032018-11-26 12:55:53 -0800603 float globalScaleFactor;
604 float windowXScale = 1.0f;
605 float windowYScale = 1.0f;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800606 nsecs_t deliveryTime; // time when the event was actually delivered
607
608 // Set to the resolved action and flags when the event is enqueued.
609 int32_t resolvedAction;
610 int32_t resolvedFlags;
611
612 DispatchEntry(EventEntry* eventEntry,
Robert Carre07e1032018-11-26 12:55:53 -0800613 int32_t targetFlags, float xOffset, float yOffset,
614 float globalScaleFactor, float windowXScale, float windowYScale);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800615 ~DispatchEntry();
616
617 inline bool hasForegroundTarget() const {
618 return targetFlags & InputTarget::FLAG_FOREGROUND;
619 }
620
621 inline bool isSplit() const {
622 return targetFlags & InputTarget::FLAG_SPLIT;
623 }
624
625 private:
626 static volatile int32_t sNextSeqAtomic;
627
628 static uint32_t nextSeq();
629 };
630
631 // A command entry captures state and behavior for an action to be performed in the
632 // dispatch loop after the initial processing has taken place. It is essentially
633 // a kind of continuation used to postpone sensitive policy interactions to a point
634 // in the dispatch loop where it is safe to release the lock (generally after finishing
635 // the critical parts of the dispatch cycle).
636 //
637 // The special thing about commands is that they can voluntarily release and reacquire
638 // the dispatcher lock at will. Initially when the command starts running, the
639 // dispatcher lock is held. However, if the command needs to call into the policy to
640 // do some work, it can release the lock, do the work, then reacquire the lock again
641 // before returning.
642 //
643 // This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch
644 // never calls into the policy while holding its lock.
645 //
646 // Commands are implicitly 'LockedInterruptible'.
647 struct CommandEntry;
Siarhei Vishniakou49a350a2019-07-26 18:44:23 -0700648 typedef std::function<void(InputDispatcher&, CommandEntry*)> Command;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800649
650 class Connection;
651 struct CommandEntry : Link<CommandEntry> {
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700652 explicit CommandEntry(Command command);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800653 ~CommandEntry();
654
655 Command command;
656
657 // parameters for the command (usage varies by command)
658 sp<Connection> connection;
659 nsecs_t eventTime;
660 KeyEntry* keyEntry;
661 sp<InputApplicationHandle> inputApplicationHandle;
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800662 std::string reason;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800663 int32_t userActivityEventType;
664 uint32_t seq;
665 bool handled;
Robert Carr803535b2018-08-02 16:38:15 -0700666 sp<InputChannel> inputChannel;
chaviw0c06c6e2019-01-09 13:27:07 -0800667 sp<IBinder> oldToken;
668 sp<IBinder> newToken;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800669 };
670
671 // Generic queue implementation.
672 template <typename T>
673 struct Queue {
674 T* head;
675 T* tail;
Jon McCaffrey65dbe972014-11-18 12:07:08 -0800676 uint32_t entryCount;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800677
Yi Kong9b14ac62018-07-17 13:48:38 -0700678 inline Queue() : head(nullptr), tail(nullptr), entryCount(0) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800679 }
680
681 inline bool isEmpty() const {
682 return !head;
683 }
684
685 inline void enqueueAtTail(T* entry) {
Jon McCaffrey65dbe972014-11-18 12:07:08 -0800686 entryCount++;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800687 entry->prev = tail;
688 if (tail) {
689 tail->next = entry;
690 } else {
691 head = entry;
692 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700693 entry->next = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800694 tail = entry;
695 }
696
697 inline void enqueueAtHead(T* entry) {
Jon McCaffrey65dbe972014-11-18 12:07:08 -0800698 entryCount++;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800699 entry->next = head;
700 if (head) {
701 head->prev = entry;
702 } else {
703 tail = entry;
704 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700705 entry->prev = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800706 head = entry;
707 }
708
709 inline void dequeue(T* entry) {
Jon McCaffrey65dbe972014-11-18 12:07:08 -0800710 entryCount--;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800711 if (entry->prev) {
712 entry->prev->next = entry->next;
713 } else {
714 head = entry->next;
715 }
716 if (entry->next) {
717 entry->next->prev = entry->prev;
718 } else {
719 tail = entry->prev;
720 }
721 }
722
723 inline T* dequeueAtHead() {
Jon McCaffrey65dbe972014-11-18 12:07:08 -0800724 entryCount--;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800725 T* entry = head;
726 head = entry->next;
727 if (head) {
Yi Kong9b14ac62018-07-17 13:48:38 -0700728 head->prev = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800729 } else {
Yi Kong9b14ac62018-07-17 13:48:38 -0700730 tail = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800731 }
732 return entry;
733 }
734
Jon McCaffrey65dbe972014-11-18 12:07:08 -0800735 uint32_t count() const {
736 return entryCount;
737 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800738 };
739
740 /* Specifies which events are to be canceled and why. */
741 struct CancelationOptions {
742 enum Mode {
743 CANCEL_ALL_EVENTS = 0,
744 CANCEL_POINTER_EVENTS = 1,
745 CANCEL_NON_POINTER_EVENTS = 2,
746 CANCEL_FALLBACK_EVENTS = 3,
747 };
748
749 // The criterion to use to determine which events should be canceled.
750 Mode mode;
751
752 // Descriptive reason for the cancelation.
753 const char* reason;
754
Michael Wright3dd60e22019-03-27 22:06:44 +0000755 // The specific keycode of the key event to cancel, or nullopt to cancel any key event.
756 std::optional<int32_t> keyCode = std::nullopt;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800757
Michael Wright3dd60e22019-03-27 22:06:44 +0000758 // The specific device id of events to cancel, or nullopt to cancel events from any device.
759 std::optional<int32_t> deviceId = std::nullopt;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800760
Michael Wright3dd60e22019-03-27 22:06:44 +0000761 // The specific display id of events to cancel, or nullopt to cancel events on any display.
762 std::optional<int32_t> displayId = std::nullopt;
763
764 CancelationOptions(Mode mode, const char* reason) : mode(mode), reason(reason) { }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800765 };
766
767 /* Tracks dispatched key and motion event state so that cancelation events can be
768 * synthesized when events are dropped. */
769 class InputState {
770 public:
771 InputState();
772 ~InputState();
773
774 // Returns true if there is no state to be canceled.
775 bool isNeutral() const;
776
777 // Returns true if the specified source is known to have received a hover enter
778 // motion event.
779 bool isHovering(int32_t deviceId, uint32_t source, int32_t displayId) const;
780
781 // Records tracking information for a key event that has just been published.
782 // Returns true if the event should be delivered, false if it is inconsistent
783 // and should be skipped.
784 bool trackKey(const KeyEntry* entry, int32_t action, int32_t flags);
785
786 // Records tracking information for a motion event that has just been published.
787 // Returns true if the event should be delivered, false if it is inconsistent
788 // and should be skipped.
789 bool trackMotion(const MotionEntry* entry, int32_t action, int32_t flags);
790
791 // Synthesizes cancelation events for the current state and resets the tracked state.
792 void synthesizeCancelationEvents(nsecs_t currentTime,
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800793 std::vector<EventEntry*>& outEvents, const CancelationOptions& options);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800794
795 // Clears the current state.
796 void clear();
797
798 // Copies pointer-related parts of the input state to another instance.
799 void copyPointerStateTo(InputState& other) const;
800
801 // Gets the fallback key associated with a keycode.
802 // Returns -1 if none.
803 // Returns AKEYCODE_UNKNOWN if we are only dispatching the unhandled key to the policy.
804 int32_t getFallbackKey(int32_t originalKeyCode);
805
806 // Sets the fallback key for a particular keycode.
807 void setFallbackKey(int32_t originalKeyCode, int32_t fallbackKeyCode);
808
809 // Removes the fallback key for a particular keycode.
810 void removeFallbackKey(int32_t originalKeyCode);
811
812 inline const KeyedVector<int32_t, int32_t>& getFallbackKeys() const {
813 return mFallbackKeys;
814 }
815
816 private:
817 struct KeyMemento {
818 int32_t deviceId;
819 uint32_t source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100820 int32_t displayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800821 int32_t keyCode;
822 int32_t scanCode;
823 int32_t metaState;
824 int32_t flags;
825 nsecs_t downTime;
826 uint32_t policyFlags;
827 };
828
829 struct MotionMemento {
830 int32_t deviceId;
831 uint32_t source;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800832 int32_t displayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800833 int32_t flags;
834 float xPrecision;
835 float yPrecision;
Garfield Tan00f511d2019-06-12 16:55:40 -0700836 float xCursorPosition;
837 float yCursorPosition;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800838 nsecs_t downTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800839 uint32_t pointerCount;
840 PointerProperties pointerProperties[MAX_POINTERS];
841 PointerCoords pointerCoords[MAX_POINTERS];
842 bool hovering;
843 uint32_t policyFlags;
844
845 void setPointers(const MotionEntry* entry);
846 };
847
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800848 std::vector<KeyMemento> mKeyMementos;
849 std::vector<MotionMemento> mMotionMementos;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800850 KeyedVector<int32_t, int32_t> mFallbackKeys;
851
852 ssize_t findKeyMemento(const KeyEntry* entry) const;
853 ssize_t findMotionMemento(const MotionEntry* entry, bool hovering) const;
854
855 void addKeyMemento(const KeyEntry* entry, int32_t flags);
856 void addMotionMemento(const MotionEntry* entry, int32_t flags, bool hovering);
857
858 static bool shouldCancelKey(const KeyMemento& memento,
859 const CancelationOptions& options);
860 static bool shouldCancelMotion(const MotionMemento& memento,
861 const CancelationOptions& options);
862 };
863
864 /* Manages the dispatch state associated with a single input channel. */
865 class Connection : public RefBase {
866 protected:
867 virtual ~Connection();
868
869 public:
870 enum Status {
871 // Everything is peachy.
872 STATUS_NORMAL,
873 // An unrecoverable communication error has occurred.
874 STATUS_BROKEN,
875 // The input channel has been unregistered.
876 STATUS_ZOMBIE
877 };
878
879 Status status;
880 sp<InputChannel> inputChannel; // never null
Michael Wrightd02c5b62014-02-10 15:10:22 -0800881 bool monitor;
882 InputPublisher inputPublisher;
883 InputState inputState;
884
885 // True if the socket is full and no further events can be published until
886 // the application consumes some of the input.
887 bool inputPublisherBlocked;
888
889 // Queue of events that need to be published to the connection.
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -0700890 std::deque<DispatchEntry*> outboundQueue;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800891
892 // Queue of events that have been published to the connection but that have not
893 // yet received a "finished" response from the application.
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -0700894 std::deque<DispatchEntry*> waitQueue;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800895
Robert Carr803535b2018-08-02 16:38:15 -0700896 explicit Connection(const sp<InputChannel>& inputChannel, bool monitor);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800897
Siarhei Vishniakou587c3f02018-01-04 11:46:44 -0800898 inline const std::string getInputChannelName() const { return inputChannel->getName(); }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800899
Siarhei Vishniakou587c3f02018-01-04 11:46:44 -0800900 const std::string getWindowName() const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800901 const char* getStatusLabel() const;
902
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -0700903 std::deque<DispatchEntry*>::iterator findWaitQueueEntry(uint32_t seq);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800904 };
905
Michael Wright3dd60e22019-03-27 22:06:44 +0000906 struct Monitor {
907 sp<InputChannel> inputChannel; // never null
908
909 explicit Monitor(const sp<InputChannel>& inputChannel);
910 };
911
Michael Wrightd02c5b62014-02-10 15:10:22 -0800912 enum DropReason {
913 DROP_REASON_NOT_DROPPED = 0,
914 DROP_REASON_POLICY = 1,
915 DROP_REASON_APP_SWITCH = 2,
916 DROP_REASON_DISABLED = 3,
917 DROP_REASON_BLOCKED = 4,
918 DROP_REASON_STALE = 5,
919 };
920
921 sp<InputDispatcherPolicyInterface> mPolicy;
922 InputDispatcherConfiguration mConfig;
923
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800924 std::mutex mLock;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800925
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800926 std::condition_variable mDispatcherIsAlive;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800927
928 sp<Looper> mLooper;
929
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800930 EventEntry* mPendingEvent GUARDED_BY(mLock);
931 Queue<EventEntry> mInboundQueue GUARDED_BY(mLock);
932 Queue<EventEntry> mRecentQueue GUARDED_BY(mLock);
933 Queue<CommandEntry> mCommandQueue GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800934
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800935 DropReason mLastDropReason GUARDED_BY(mLock);
Michael Wright3a981722015-06-10 15:26:13 +0100936
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800937 void dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800938
939 // Enqueues an inbound event. Returns true if mLooper->wake() should be called.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800940 bool enqueueInboundEventLocked(EventEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800941
942 // Cleans up input state when dropping an inbound event.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800943 void dropInboundEventLocked(EventEntry* entry, DropReason dropReason) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800944
945 // Adds an event to a queue of recent events for debugging purposes.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800946 void addRecentEventLocked(EventEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800947
948 // App switch latency optimization.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800949 bool mAppSwitchSawKeyDown GUARDED_BY(mLock);
950 nsecs_t mAppSwitchDueTime GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800951
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800952 bool isAppSwitchKeyEvent(KeyEntry* keyEntry);
953 bool isAppSwitchPendingLocked() REQUIRES(mLock);
954 void resetPendingAppSwitchLocked(bool handled) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800955
956 // Stale event latency optimization.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800957 static bool isStaleEvent(nsecs_t currentTime, EventEntry* entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800958
959 // Blocked event latency optimization. Drops old events when the user intends
960 // to transfer focus to a new application.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800961 EventEntry* mNextUnblockedEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800962
Tiger Huang85b8c5e2019-01-17 18:34:54 +0800963 sp<InputWindowHandle> findTouchedWindowAtLocked(int32_t displayId, int32_t x, int32_t y,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800964 bool addOutsideTargets = false, bool addPortalWindows = false) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800965
966 // All registered connections mapped by channel file descriptor.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800967 KeyedVector<int, sp<Connection> > mConnectionsByFd GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800968
Robert Carr5c8a0262018-10-03 16:30:44 -0700969 struct IBinderHash {
970 std::size_t operator()(const sp<IBinder>& b) const {
971 return std::hash<IBinder *>{}(b.get());
972 }
973 };
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800974 std::unordered_map<sp<IBinder>, sp<InputChannel>, IBinderHash> mInputChannelsByToken
975 GUARDED_BY(mLock);
Robert Carr5c8a0262018-10-03 16:30:44 -0700976
Michael Wright3dd60e22019-03-27 22:06:44 +0000977 // Finds the display ID of the gesture monitor identified by the provided token.
978 std::optional<int32_t> findGestureMonitorDisplayByTokenLocked(const sp<IBinder>& token)
979 REQUIRES(mLock);
980
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800981 ssize_t getConnectionIndexLocked(const sp<InputChannel>& inputChannel) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800982
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800983 // Input channels that will receive a copy of all input events sent to the provided display.
Michael Wright3dd60e22019-03-27 22:06:44 +0000984 std::unordered_map<int32_t, std::vector<Monitor>> mGlobalMonitorsByDisplay
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800985 GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800986
Michael Wright3dd60e22019-03-27 22:06:44 +0000987 // Input channels that will receive pointer events that start within the corresponding display.
988 // These are a bit special when compared to global monitors since they'll cause gesture streams
989 // to continue even when there isn't a touched window,and have the ability to steal the rest of
990 // the pointer stream in order to claim it for a system gesture.
991 std::unordered_map<int32_t, std::vector<Monitor>> mGestureMonitorsByDisplay
992 GUARDED_BY(mLock);
993
994
Michael Wrightd02c5b62014-02-10 15:10:22 -0800995 // Event injection and synchronization.
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800996 std::condition_variable mInjectionResultAvailable;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800997 bool hasInjectionPermission(int32_t injectorPid, int32_t injectorUid);
Siarhei Vishniakou62683e82019-03-06 17:59:56 -0800998 void setInjectionResult(EventEntry* entry, int32_t injectionResult);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800999
Siarhei Vishniakou443ad902019-03-06 17:25:41 -08001000 std::condition_variable mInjectionSyncFinished;
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001001 void incrementPendingForegroundDispatches(EventEntry* entry);
Siarhei Vishniakou62683e82019-03-06 17:59:56 -08001002 void decrementPendingForegroundDispatches(EventEntry* entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001003
1004 // Key repeat tracking.
1005 struct KeyRepeatState {
1006 KeyEntry* lastKeyEntry; // or null if no repeat
1007 nsecs_t nextRepeatTime;
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001008 } mKeyRepeatState GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001009
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001010 void resetKeyRepeatLocked() REQUIRES(mLock);
1011 KeyEntry* synthesizeKeyRepeatLocked(nsecs_t currentTime) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001012
Michael Wright78f24442014-08-06 15:55:28 -07001013 // Key replacement tracking
1014 struct KeyReplacement {
1015 int32_t keyCode;
1016 int32_t deviceId;
1017 bool operator==(const KeyReplacement& rhs) const {
1018 return keyCode == rhs.keyCode && deviceId == rhs.deviceId;
1019 }
1020 bool operator<(const KeyReplacement& rhs) const {
1021 return keyCode != rhs.keyCode ? keyCode < rhs.keyCode : deviceId < rhs.deviceId;
1022 }
1023 };
1024 // Maps the key code replaced, device id tuple to the key code it was replaced with
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001025 KeyedVector<KeyReplacement, int32_t> mReplacedKeys GUARDED_BY(mLock);
Siarhei Vishniakou61fafdd2018-04-13 11:00:58 -05001026 // Process certain Meta + Key combinations
1027 void accelerateMetaShortcuts(const int32_t deviceId, const int32_t action,
1028 int32_t& keyCode, int32_t& metaState);
Michael Wright78f24442014-08-06 15:55:28 -07001029
Michael Wrightd02c5b62014-02-10 15:10:22 -08001030 // Deferred command processing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001031 bool haveCommandsLocked() const REQUIRES(mLock);
1032 bool runCommandsLockedInterruptible() REQUIRES(mLock);
1033 CommandEntry* postCommandLocked(Command command) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001034
1035 // Input filter processing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001036 bool shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) REQUIRES(mLock);
1037 bool shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001038
1039 // Inbound event processing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001040 void drainInboundQueueLocked() REQUIRES(mLock);
1041 void releasePendingEventLocked() REQUIRES(mLock);
1042 void releaseInboundEventLocked(EventEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001043
1044 // Dispatch state.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001045 bool mDispatchEnabled GUARDED_BY(mLock);
1046 bool mDispatchFrozen GUARDED_BY(mLock);
1047 bool mInputFilterEnabled GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001048
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001049 std::unordered_map<int32_t, std::vector<sp<InputWindowHandle>>> mWindowHandlesByDisplay
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001050 GUARDED_BY(mLock);
Arthur Hungb92218b2018-08-14 12:00:21 +08001051 // Get window handles by display, return an empty vector if not found.
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001052 std::vector<sp<InputWindowHandle>> getWindowHandlesLocked(int32_t displayId) const
1053 REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001054 sp<InputWindowHandle> getWindowHandleLocked(const sp<IBinder>& windowHandleToken) const
1055 REQUIRES(mLock);
1056 sp<InputChannel> getInputChannelLocked(const sp<IBinder>& windowToken) const REQUIRES(mLock);
1057 bool hasWindowHandleLocked(const sp<InputWindowHandle>& windowHandle) const REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001058
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -07001059 /*
1060 * Validate and update InputWindowHandles for a given display.
1061 */
1062 void updateWindowHandlesForDisplayLocked(
1063 const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId)
1064 REQUIRES(mLock);
1065
Michael Wrightd02c5b62014-02-10 15:10:22 -08001066 // Focus tracking for keys, trackball, etc.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001067 std::unordered_map<int32_t, sp<InputWindowHandle>> mFocusedWindowHandlesByDisplay
1068 GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001069
1070 // Focus tracking for touch.
1071 struct TouchedWindow {
1072 sp<InputWindowHandle> windowHandle;
1073 int32_t targetFlags;
1074 BitSet32 pointerIds; // zero unless target flag FLAG_SPLIT is set
1075 };
Michael Wright3dd60e22019-03-27 22:06:44 +00001076
1077 // For tracking the offsets we need to apply when adding gesture monitor targets.
1078 struct TouchedMonitor {
1079 Monitor monitor;
1080 float xOffset = 0.f;
1081 float yOffset = 0.f;
1082
1083 explicit TouchedMonitor(const Monitor& monitor, float xOffset, float yOffset);
1084 };
1085
Michael Wrightd02c5b62014-02-10 15:10:22 -08001086 struct TouchState {
1087 bool down;
1088 bool split;
1089 int32_t deviceId; // id of the device that is currently down, others are rejected
1090 uint32_t source; // source of the device that is current down, others are rejected
1091 int32_t displayId; // id to the display that currently has a touch, others are rejected
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001092 std::vector<TouchedWindow> windows;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001093
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001094 // This collects the portal windows that the touch has gone through. Each portal window
1095 // targets a display (embedded display for most cases). With this info, we can add the
1096 // monitoring channels of the displays touched.
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001097 std::vector<sp<InputWindowHandle>> portalWindows;
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001098
Michael Wright3dd60e22019-03-27 22:06:44 +00001099 std::vector<TouchedMonitor> gestureMonitors;
1100
Michael Wrightd02c5b62014-02-10 15:10:22 -08001101 TouchState();
1102 ~TouchState();
1103 void reset();
1104 void copyFrom(const TouchState& other);
1105 void addOrUpdateWindow(const sp<InputWindowHandle>& windowHandle,
1106 int32_t targetFlags, BitSet32 pointerIds);
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001107 void addPortalWindow(const sp<InputWindowHandle>& windowHandle);
Michael Wright3dd60e22019-03-27 22:06:44 +00001108 void addGestureMonitors(const std::vector<TouchedMonitor>& monitors);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001109 void removeWindow(const sp<InputWindowHandle>& windowHandle);
Robert Carr803535b2018-08-02 16:38:15 -07001110 void removeWindowByToken(const sp<IBinder>& token);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001111 void filterNonAsIsTouchWindows();
Michael Wright3dd60e22019-03-27 22:06:44 +00001112 void filterNonMonitors();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001113 sp<InputWindowHandle> getFirstForegroundWindowHandle() const;
1114 bool isSlippery() const;
1115 };
1116
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001117 KeyedVector<int32_t, TouchState> mTouchStatesByDisplay GUARDED_BY(mLock);
1118 TouchState mTempTouchState GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001119
Tiger Huang721e26f2018-07-24 22:26:19 +08001120 // Focused applications.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001121 std::unordered_map<int32_t, sp<InputApplicationHandle>> mFocusedApplicationHandlesByDisplay
1122 GUARDED_BY(mLock);
Tiger Huang721e26f2018-07-24 22:26:19 +08001123
1124 // Top focused display.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001125 int32_t mFocusedDisplayId GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001126
1127 // Dispatcher state at time of last ANR.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001128 std::string mLastANRState GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001129
1130 // Dispatch inbound events.
1131 bool dispatchConfigurationChangedLocked(
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001132 nsecs_t currentTime, ConfigurationChangedEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001133 bool dispatchDeviceResetLocked(
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001134 nsecs_t currentTime, DeviceResetEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001135 bool dispatchKeyLocked(
1136 nsecs_t currentTime, KeyEntry* entry,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001137 DropReason* dropReason, nsecs_t* nextWakeupTime) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001138 bool dispatchMotionLocked(
1139 nsecs_t currentTime, MotionEntry* entry,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001140 DropReason* dropReason, nsecs_t* nextWakeupTime) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001141 void dispatchEventLocked(nsecs_t currentTime, EventEntry* entry,
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001142 const std::vector<InputTarget>& inputTargets) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001143
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001144 void logOutboundKeyDetails(const char* prefix, const KeyEntry* entry);
1145 void logOutboundMotionDetails(const char* prefix, const MotionEntry* entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001146
1147 // Keeping track of ANR timeouts.
1148 enum InputTargetWaitCause {
1149 INPUT_TARGET_WAIT_CAUSE_NONE,
1150 INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY,
1151 INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY,
1152 };
1153
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001154 InputTargetWaitCause mInputTargetWaitCause GUARDED_BY(mLock);
1155 nsecs_t mInputTargetWaitStartTime GUARDED_BY(mLock);
1156 nsecs_t mInputTargetWaitTimeoutTime GUARDED_BY(mLock);
1157 bool mInputTargetWaitTimeoutExpired GUARDED_BY(mLock);
1158 sp<IBinder> mInputTargetWaitApplicationToken GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001159
1160 // Contains the last window which received a hover event.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001161 sp<InputWindowHandle> mLastHoverWindowHandle GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001162
1163 // Finding targets for input events.
1164 int32_t handleTargetsNotReadyLocked(nsecs_t currentTime, const EventEntry* entry,
1165 const sp<InputApplicationHandle>& applicationHandle,
1166 const sp<InputWindowHandle>& windowHandle,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001167 nsecs_t* nextWakeupTime, const char* reason) REQUIRES(mLock);
Robert Carr803535b2018-08-02 16:38:15 -07001168
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001169 void removeWindowByTokenLocked(const sp<IBinder>& token) REQUIRES(mLock);
Robert Carr803535b2018-08-02 16:38:15 -07001170
Michael Wrightd02c5b62014-02-10 15:10:22 -08001171 void resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001172 const sp<InputChannel>& inputChannel) REQUIRES(mLock);
1173 nsecs_t getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime) REQUIRES(mLock);
1174 void resetANRTimeoutsLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001175
Tiger Huang721e26f2018-07-24 22:26:19 +08001176 int32_t getTargetDisplayId(const EventEntry* entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001177 int32_t findFocusedWindowTargetsLocked(nsecs_t currentTime, const EventEntry* entry,
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001178 std::vector<InputTarget>& inputTargets, nsecs_t* nextWakeupTime) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001179 int32_t findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry* entry,
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001180 std::vector<InputTarget>& inputTargets, nsecs_t* nextWakeupTime,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001181 bool* outConflictingPointerActions) REQUIRES(mLock);
Michael Wright3dd60e22019-03-27 22:06:44 +00001182 std::vector<TouchedMonitor> findTouchedGestureMonitorsLocked(int32_t displayId,
1183 const std::vector<sp<InputWindowHandle>>& portalWindows) REQUIRES(mLock);
1184 void addGestureMonitors(const std::vector<Monitor>& monitors,
1185 std::vector<TouchedMonitor>& outTouchedMonitors, float xOffset = 0, float yOffset = 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001186
1187 void addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle,
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001188 int32_t targetFlags, BitSet32 pointerIds, std::vector<InputTarget>& inputTargets)
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001189 REQUIRES(mLock);
Michael Wright3dd60e22019-03-27 22:06:44 +00001190 void addMonitoringTargetLocked(const Monitor& monitor, float xOffset, float yOffset,
1191 std::vector<InputTarget>& inputTargets) REQUIRES(mLock);
1192 void addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets,
1193 int32_t displayId, float xOffset = 0, float yOffset = 0) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001194
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001195 void pokeUserActivityLocked(const EventEntry* eventEntry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001196 bool checkInjectionPermission(const sp<InputWindowHandle>& windowHandle,
1197 const InjectionState* injectionState);
1198 bool isWindowObscuredAtPointLocked(const sp<InputWindowHandle>& windowHandle,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001199 int32_t x, int32_t y) const REQUIRES(mLock);
1200 bool isWindowObscuredLocked(const sp<InputWindowHandle>& windowHandle) const REQUIRES(mLock);
1201 std::string getApplicationWindowLabel(const sp<InputApplicationHandle>& applicationHandle,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001202 const sp<InputWindowHandle>& windowHandle);
1203
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001204 std::string checkWindowReadyForMoreInputLocked(nsecs_t currentTime,
Jeff Brownffb49772014-10-10 19:01:34 -07001205 const sp<InputWindowHandle>& windowHandle, const EventEntry* eventEntry,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001206 const char* targetType) REQUIRES(mLock);
Jeff Brownffb49772014-10-10 19:01:34 -07001207
Michael Wrightd02c5b62014-02-10 15:10:22 -08001208 // Manage the dispatch cycle for a single connection.
1209 // These methods are deliberately not Interruptible because doing all of the work
1210 // with the mutex held makes it easier to ensure that connection invariants are maintained.
1211 // If needed, the methods post commands to run later once the critical bits are done.
1212 void prepareDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001213 EventEntry* eventEntry, const InputTarget* inputTarget) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001214 void enqueueDispatchEntriesLocked(nsecs_t currentTime, const sp<Connection>& connection,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001215 EventEntry* eventEntry, const InputTarget* inputTarget) REQUIRES(mLock);
chaviw8c9cf542019-03-25 13:02:48 -07001216 void enqueueDispatchEntryLocked(const sp<Connection>& connection,
1217 EventEntry* eventEntry, const InputTarget* inputTarget, int32_t dispatchMode)
1218 REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001219 void startDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection)
1220 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001221 void finishDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001222 uint32_t seq, bool handled) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001223 void abortBrokenDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001224 bool notify) REQUIRES(mLock);
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -07001225 void drainDispatchQueue(std::deque<DispatchEntry*>& queue);
Siarhei Vishniakou62683e82019-03-06 17:59:56 -08001226 void releaseDispatchEntry(DispatchEntry* dispatchEntry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001227 static int handleReceiveCallback(int fd, int events, void* data);
chaviw8c9cf542019-03-25 13:02:48 -07001228 // The action sent should only be of type AMOTION_EVENT_*
chaviwfd6d3512019-03-25 13:23:49 -07001229 void dispatchPointerDownOutsideFocus(uint32_t source, int32_t action,
chaviw8c9cf542019-03-25 13:02:48 -07001230 const sp<IBinder>& newToken) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001231
1232 void synthesizeCancelationEventsForAllConnectionsLocked(
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001233 const CancelationOptions& options) REQUIRES(mLock);
1234 void synthesizeCancelationEventsForMonitorsLocked(
1235 const CancelationOptions& options) REQUIRES(mLock);
Michael Wright3dd60e22019-03-27 22:06:44 +00001236 void synthesizeCancelationEventsForMonitorsLocked(const CancelationOptions& options,
1237 std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001238 void synthesizeCancelationEventsForInputChannelLocked(const sp<InputChannel>& channel,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001239 const CancelationOptions& options) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001240 void synthesizeCancelationEventsForConnectionLocked(const sp<Connection>& connection,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001241 const CancelationOptions& options) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001242
1243 // Splitting motion events across windows.
1244 MotionEntry* splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds);
1245
1246 // Reset and drop everything the dispatcher is doing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001247 void resetAndDropEverythingLocked(const char* reason) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001248
1249 // Dump state.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001250 void dumpDispatchStateLocked(std::string& dump) REQUIRES(mLock);
Michael Wright3dd60e22019-03-27 22:06:44 +00001251 void dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001252 void logDispatchStateLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001253
1254 // Registration.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001255 void removeMonitorChannelLocked(const sp<InputChannel>& inputChannel) REQUIRES(mLock);
Michael Wright3dd60e22019-03-27 22:06:44 +00001256 void removeMonitorChannelLocked(const sp<InputChannel>& inputChannel,
1257 std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay)
1258 REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001259 status_t unregisterInputChannelLocked(const sp<InputChannel>& inputChannel, bool notify)
1260 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001261
1262 // Interesting events that we might like to log or tell the framework about.
1263 void onDispatchCycleFinishedLocked(
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001264 nsecs_t currentTime, const sp<Connection>& connection, uint32_t seq, bool handled)
1265 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001266 void onDispatchCycleBrokenLocked(
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001267 nsecs_t currentTime, const sp<Connection>& connection) REQUIRES(mLock);
chaviw0c06c6e2019-01-09 13:27:07 -08001268 void onFocusChangedLocked(const sp<InputWindowHandle>& oldFocus,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001269 const sp<InputWindowHandle>& newFocus) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001270 void onANRLocked(
1271 nsecs_t currentTime, const sp<InputApplicationHandle>& applicationHandle,
1272 const sp<InputWindowHandle>& windowHandle,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001273 nsecs_t eventTime, nsecs_t waitStartTime, const char* reason) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001274
1275 // Outbound policy interactions.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001276 void doNotifyConfigurationChangedLockedInterruptible(CommandEntry* commandEntry)
1277 REQUIRES(mLock);
1278 void doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
1279 void doNotifyFocusChangedLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
1280 void doNotifyANRLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
1281 void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry)
1282 REQUIRES(mLock);
1283 void doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001284 bool afterKeyEventLockedInterruptible(const sp<Connection>& connection,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001285 DispatchEntry* dispatchEntry, KeyEntry* keyEntry, bool handled) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001286 bool afterMotionEventLockedInterruptible(const sp<Connection>& connection,
Siarhei Vishniakou62683e82019-03-06 17:59:56 -08001287 DispatchEntry* dispatchEntry, MotionEntry* motionEntry, bool handled) REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001288 void doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001289 void initializeKeyEvent(KeyEvent* event, const KeyEntry* entry);
chaviwfd6d3512019-03-25 13:23:49 -07001290 void doOnPointerDownOutsideFocusLockedInterruptible(CommandEntry* commandEntry)
1291 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001292
1293 // Statistics gathering.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001294 void updateDispatchStatistics(nsecs_t currentTime, const EventEntry* entry,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001295 int32_t injectionResult, nsecs_t timeSpentWaitingForApplication);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001296 void traceInboundQueueLengthLocked() REQUIRES(mLock);
1297 void traceOutboundQueueLength(const sp<Connection>& connection);
1298 void traceWaitQueueLength(const sp<Connection>& connection);
Prabir Pradhanf93562f2018-11-29 12:13:37 -08001299
Prabir Pradhan79a4f0c2019-01-09 11:24:01 -08001300 sp<InputReporterInterface> mReporter;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001301};
1302
1303/* Enqueues and dispatches input events, endlessly. */
1304class InputDispatcherThread : public Thread {
1305public:
1306 explicit InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher);
1307 ~InputDispatcherThread();
1308
1309private:
1310 virtual bool threadLoop();
1311
1312 sp<InputDispatcherInterface> mDispatcher;
1313};
1314
1315} // namespace android
1316
1317#endif // _UI_INPUT_DISPATCHER_H