blob: 315a63013faf5a26ded5da07c61e42338323e3bb [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _UI_INPUT_DISPATCHER_H
18#define _UI_INPUT_DISPATCHER_H
19
Garfield Tane4fc0102019-09-11 13:16:25 -070020#include <cutils/atomic.h>
21#include <input/ISetInputWindowsListener.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080022#include <input/Input.h>
Robert Carr3720ed02018-08-08 16:08:27 -070023#include <input/InputApplication.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080024#include <input/InputTransport.h>
Robert Carr3720ed02018-08-08 16:08:27 -070025#include <input/InputWindow.h>
Michael Wright3dd60e22019-03-27 22:06:44 +000026#include <ui/Region.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080027#include <utils/BitSet.h>
Garfield Tane4fc0102019-09-11 13:16:25 -070028#include <utils/Looper.h>
29#include <utils/RefBase.h>
30#include <utils/Timers.h>
31#include <utils/threads.h>
32#include <condition_variable>
33#include <optional>
Robert Carr5c8a0262018-10-03 16:30:44 -070034#include <unordered_map>
Michael Wrightd02c5b62014-02-10 15:10:22 -080035
Garfield Tane4fc0102019-09-11 13:16:25 -070036#include <limits.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080037#include <stddef.h>
38#include <unistd.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
Michael Wrightd02c5b62014-02-10 15:10:22 -080083/*
84 * An input target specifies how an input event is to be dispatched to a particular window
85 * including the window's input channel, control flags, a timeout, and an X / Y offset to
86 * be added to input event coordinates to compensate for the absolute position of the
87 * window area.
88 */
89struct InputTarget {
90 enum {
91 /* This flag indicates that the event is being delivered to a foreground application. */
92 FLAG_FOREGROUND = 1 << 0,
93
Michael Wrightcdcd8f22016-03-22 16:52:13 -070094 /* This flag indicates that the MotionEvent falls within the area of the target
Michael Wrightd02c5b62014-02-10 15:10:22 -080095 * obscured by another visible window above it. The motion event should be
96 * delivered with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED. */
97 FLAG_WINDOW_IS_OBSCURED = 1 << 1,
98
99 /* This flag indicates that a motion event is being split across multiple windows. */
100 FLAG_SPLIT = 1 << 2,
101
102 /* This flag indicates that the pointer coordinates dispatched to the application
103 * will be zeroed out to avoid revealing information to an application. This is
104 * used in conjunction with FLAG_DISPATCH_AS_OUTSIDE to prevent apps not sharing
105 * the same UID from watching all touches. */
106 FLAG_ZERO_COORDS = 1 << 3,
107
108 /* This flag indicates that the event should be sent as is.
109 * Should always be set unless the event is to be transmuted. */
110 FLAG_DISPATCH_AS_IS = 1 << 8,
111
112 /* This flag indicates that a MotionEvent with AMOTION_EVENT_ACTION_DOWN falls outside
113 * of the area of this target and so should instead be delivered as an
114 * AMOTION_EVENT_ACTION_OUTSIDE to this target. */
115 FLAG_DISPATCH_AS_OUTSIDE = 1 << 9,
116
117 /* This flag indicates that a hover sequence is starting in the given window.
118 * The event is transmuted into ACTION_HOVER_ENTER. */
119 FLAG_DISPATCH_AS_HOVER_ENTER = 1 << 10,
120
121 /* This flag indicates that a hover event happened outside of a window which handled
122 * previous hover events, signifying the end of the current hover sequence for that
123 * window.
124 * The event is transmuted into ACTION_HOVER_ENTER. */
125 FLAG_DISPATCH_AS_HOVER_EXIT = 1 << 11,
126
127 /* This flag indicates that the event should be canceled.
128 * It is used to transmute ACTION_MOVE into ACTION_CANCEL when a touch slips
129 * outside of a window. */
130 FLAG_DISPATCH_AS_SLIPPERY_EXIT = 1 << 12,
131
132 /* This flag indicates that the event should be dispatched as an initial down.
133 * It is used to transmute ACTION_MOVE into ACTION_DOWN when a touch slips
134 * into a new window. */
135 FLAG_DISPATCH_AS_SLIPPERY_ENTER = 1 << 13,
136
137 /* Mask for all dispatch modes. */
Garfield Tane4fc0102019-09-11 13:16:25 -0700138 FLAG_DISPATCH_MASK = FLAG_DISPATCH_AS_IS | FLAG_DISPATCH_AS_OUTSIDE |
139 FLAG_DISPATCH_AS_HOVER_ENTER | FLAG_DISPATCH_AS_HOVER_EXIT |
140 FLAG_DISPATCH_AS_SLIPPERY_EXIT | FLAG_DISPATCH_AS_SLIPPERY_ENTER,
Michael Wrightcdcd8f22016-03-22 16:52:13 -0700141
142 /* This flag indicates that the target of a MotionEvent is partly or wholly
143 * obscured by another visible window above it. The motion event should be
144 * delivered with flag AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED. */
145 FLAG_WINDOW_IS_PARTIALLY_OBSCURED = 1 << 14,
146
Michael Wrightd02c5b62014-02-10 15:10:22 -0800147 };
148
149 // The input channel to be targeted.
150 sp<InputChannel> inputChannel;
151
152 // Flags for the input target.
153 int32_t flags;
154
155 // The x and y offset to add to a MotionEvent as it is delivered.
156 // (ignored for KeyEvents)
157 float xOffset, yOffset;
158
159 // Scaling factor to apply to MotionEvent as it is delivered.
160 // (ignored for KeyEvents)
Robert Carre07e1032018-11-26 12:55:53 -0800161 float globalScaleFactor;
162 float windowXScale = 1.0f;
163 float windowYScale = 1.0f;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800164
165 // The subset of pointer ids to include in motion events dispatched to this input target
166 // if FLAG_SPLIT is set.
167 BitSet32 pointerIds;
168};
169
Michael Wrightd02c5b62014-02-10 15:10:22 -0800170/*
171 * Input dispatcher configuration.
172 *
173 * Specifies various options that modify the behavior of the input dispatcher.
174 * The values provided here are merely defaults. The actual values will come from ViewConfiguration
175 * and are passed into the dispatcher during initialization.
176 */
177struct InputDispatcherConfiguration {
178 // The key repeat initial timeout.
179 nsecs_t keyRepeatTimeout;
180
181 // The key repeat inter-key delay.
182 nsecs_t keyRepeatDelay;
183
Garfield Tane4fc0102019-09-11 13:16:25 -0700184 InputDispatcherConfiguration()
185 : keyRepeatTimeout(500 * 1000000LL), keyRepeatDelay(50 * 1000000LL) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800186};
187
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188/*
189 * Input dispatcher policy interface.
190 *
191 * The input reader policy is used by the input reader to interact with the Window Manager
192 * and other system components.
193 *
194 * The actual implementation is partially supported by callbacks into the DVM
195 * via JNI. This interface is also mocked in the unit tests.
196 */
197class InputDispatcherPolicyInterface : public virtual RefBase {
198protected:
Garfield Tane4fc0102019-09-11 13:16:25 -0700199 InputDispatcherPolicyInterface() {}
200 virtual ~InputDispatcherPolicyInterface() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800201
202public:
203 /* Notifies the system that a configuration change has occurred. */
204 virtual void notifyConfigurationChanged(nsecs_t when) = 0;
205
206 /* Notifies the system that an application is not responding.
207 * Returns a new timeout to continue waiting, or 0 to abort dispatch. */
208 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Garfield Tane4fc0102019-09-11 13:16:25 -0700209 const sp<IBinder>& token, const std::string& reason) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800210
211 /* Notifies the system that an input channel is unrecoverably broken. */
Robert Carr803535b2018-08-02 16:38:15 -0700212 virtual void notifyInputChannelBroken(const sp<IBinder>& token) = 0;
chaviw0c06c6e2019-01-09 13:27:07 -0800213 virtual void notifyFocusChanged(const sp<IBinder>& oldToken, const sp<IBinder>& newToken) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800214
215 /* Gets the input dispatcher configuration. */
216 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) = 0;
217
Michael Wrightd02c5b62014-02-10 15:10:22 -0800218 /* Filters an input event.
219 * Return true to dispatch the event unmodified, false to consume the event.
220 * A filter can also transform and inject events later by passing POLICY_FLAG_FILTERED
221 * to injectInputEvent.
222 */
223 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) = 0;
224
225 /* Intercepts a key event immediately before queueing it.
226 * The policy can use this method as an opportunity to perform power management functions
227 * and early event preprocessing such as updating policy flags.
228 *
229 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
230 * should be dispatched to applications.
231 */
232 virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags) = 0;
233
234 /* Intercepts a touch, trackball or other motion event 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 */
Charles Chen3611f1f2019-01-29 17:26:18 +0800241 virtual void interceptMotionBeforeQueueing(const int32_t displayId, nsecs_t when,
Garfield Tane4fc0102019-09-11 13:16:25 -0700242 uint32_t& policyFlags) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800243
244 /* Allows the policy a chance to intercept a key before dispatching. */
Robert Carr803535b2018-08-02 16:38:15 -0700245 virtual nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>& token,
Garfield Tane4fc0102019-09-11 13:16:25 -0700246 const KeyEvent* keyEvent,
247 uint32_t policyFlags) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800248
249 /* Allows the policy a chance to perform default processing for an unhandled key.
250 * Returns an alternate keycode to redispatch as a fallback, or 0 to give up. */
Garfield Tane4fc0102019-09-11 13:16:25 -0700251 virtual bool dispatchUnhandledKey(const sp<IBinder>& token, const KeyEvent* keyEvent,
252 uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800253
254 /* Notifies the policy about switch events.
255 */
Garfield Tane4fc0102019-09-11 13:16:25 -0700256 virtual void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
257 uint32_t policyFlags) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800258
259 /* Poke user activity for an event dispatched to a window. */
260 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType) = 0;
261
262 /* Checks whether a given application pid/uid has permission to inject input events
263 * into other applications.
264 *
265 * This method is special in that its implementation promises to be non-reentrant and
266 * is safe to call while holding other locks. (Most other methods make no such guarantees!)
267 */
Garfield Tane4fc0102019-09-11 13:16:25 -0700268 virtual bool checkInjectEventsPermissionNonReentrant(int32_t injectorPid,
269 int32_t injectorUid) = 0;
chaviwfd6d3512019-03-25 13:23:49 -0700270
271 /* Notifies the policy that a pointer down event has occurred outside the current focused
272 * window.
273 *
274 * The touchedToken passed as an argument is the window that received the input event.
275 */
276 virtual void onPointerDownOutsideFocus(const sp<IBinder>& touchedToken) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800277};
278
Michael Wrightd02c5b62014-02-10 15:10:22 -0800279/* Notifies the system about input events generated by the input reader.
280 * The dispatcher is expected to be mostly asynchronous. */
281class InputDispatcherInterface : public virtual RefBase, public InputListenerInterface {
282protected:
Garfield Tane4fc0102019-09-11 13:16:25 -0700283 InputDispatcherInterface() {}
284 virtual ~InputDispatcherInterface() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800285
286public:
287 /* Dumps the state of the input dispatcher.
288 *
289 * This method may be called on any thread (usually by the input manager). */
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800290 virtual void dump(std::string& dump) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800291
292 /* Called by the heatbeat to ensures that the dispatcher has not deadlocked. */
293 virtual void monitor() = 0;
294
295 /* Runs a single iteration of the dispatch loop.
296 * Nominally processes one queued event, a timeout, or a response from an input consumer.
297 *
298 * This method should only be called on the input dispatcher thread.
299 */
300 virtual void dispatchOnce() = 0;
301
302 /* Injects an input event and optionally waits for sync.
303 * The synchronization mode determines whether the method blocks while waiting for
304 * input injection to proceed.
305 * Returns one of the INPUT_EVENT_INJECTION_XXX constants.
306 *
307 * This method may be called on any thread (usually by the input manager).
308 */
Garfield Tane4fc0102019-09-11 13:16:25 -0700309 virtual int32_t injectInputEvent(const InputEvent* event, int32_t injectorPid,
310 int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,
311 uint32_t policyFlags) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800312
313 /* Sets the list of input windows.
314 *
315 * This method may be called on any thread (usually by the input manager).
316 */
Garfield Tane4fc0102019-09-11 13:16:25 -0700317 virtual void setInputWindows(
318 const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId,
chaviw291d88a2019-02-14 10:33:58 -0800319 const sp<ISetInputWindowsListener>& setInputWindowsListener = nullptr) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800320
Tiger Huang721e26f2018-07-24 22:26:19 +0800321 /* Sets the focused application on the given display.
Michael Wrightd02c5b62014-02-10 15:10:22 -0800322 *
323 * This method may be called on any thread (usually by the input manager).
324 */
325 virtual void setFocusedApplication(
Tiger Huang721e26f2018-07-24 22:26:19 +0800326 int32_t displayId, const sp<InputApplicationHandle>& inputApplicationHandle) = 0;
327
328 /* Sets the focused display.
329 *
330 * This method may be called on any thread (usually by the input manager).
331 */
332 virtual void setFocusedDisplay(int32_t displayId) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800333
334 /* Sets the input dispatching mode.
335 *
336 * This method may be called on any thread (usually by the input manager).
337 */
338 virtual void setInputDispatchMode(bool enabled, bool frozen) = 0;
339
340 /* Sets whether input event filtering is enabled.
341 * When enabled, incoming input events are sent to the policy's filterInputEvent
342 * method instead of being dispatched. The filter is expected to use
343 * injectInputEvent to inject the events it would like to have dispatched.
344 * It should include POLICY_FLAG_FILTERED in the policy flags during injection.
345 */
346 virtual void setInputFilterEnabled(bool enabled) = 0;
347
chaviwfbe5d9c2018-12-26 12:23:37 -0800348 /* Transfers touch focus from one window to another window.
Michael Wrightd02c5b62014-02-10 15:10:22 -0800349 *
350 * Returns true on success. False if the window did not actually have touch focus.
351 */
chaviwfbe5d9c2018-12-26 12:23:37 -0800352 virtual bool transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken) = 0;
353
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800354 /* Registers input channels that may be used as targets for input events.
Michael Wrightd02c5b62014-02-10 15:10:22 -0800355 *
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800356 * This method may be called on any thread (usually by the input manager).
Michael Wrightd02c5b62014-02-10 15:10:22 -0800357 */
Garfield Tane4fc0102019-09-11 13:16:25 -0700358 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel,
359 int32_t displayId) = 0;
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800360
Michael Wright3dd60e22019-03-27 22:06:44 +0000361 /* Registers input channels to be used to monitor input events.
362 *
363 * Each monitor must target a specific display and will only receive input events sent to that
364 * display. If the monitor is a gesture monitor, it will only receive pointer events on the
365 * targeted display.
366 *
367 * This method may be called on any thread (usually by the input manager).
368 */
Garfield Tane4fc0102019-09-11 13:16:25 -0700369 virtual status_t registerInputMonitor(const sp<InputChannel>& inputChannel, int32_t displayId,
370 bool gestureMonitor) = 0;
Michael Wright3dd60e22019-03-27 22:06:44 +0000371
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800372 /* Unregister input channels that will no longer receive input events.
373 *
374 * This method may be called on any thread (usually by the input manager).
375 */
Michael Wrightd02c5b62014-02-10 15:10:22 -0800376 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) = 0;
Michael Wright3dd60e22019-03-27 22:06:44 +0000377
378 /* Allows an input monitor steal the current pointer stream away from normal input windows.
379 *
380 * This method may be called on any thread (usually by the input manager).
381 */
382 virtual status_t pilferPointers(const sp<IBinder>& token) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800383};
384
385/* Dispatches events to input targets. Some functions of the input dispatcher, such as
386 * identifying input targets, are controlled by a separate policy object.
387 *
388 * IMPORTANT INVARIANT:
389 * Because the policy can potentially block or cause re-entrance into the input dispatcher,
390 * the input dispatcher never calls into the policy while holding its internal locks.
391 * The implementation is also carefully designed to recover from scenarios such as an
392 * input channel becoming unregistered while identifying input targets or processing timeouts.
393 *
394 * Methods marked 'Locked' must be called with the lock acquired.
395 *
396 * Methods marked 'LockedInterruptible' must be called with the lock acquired but
397 * may during the course of their execution release the lock, call into the policy, and
398 * then reacquire the lock. The caller is responsible for recovering gracefully.
399 *
400 * A 'LockedInterruptible' method may called a 'Locked' method, but NOT vice-versa.
401 */
402class InputDispatcher : public InputDispatcherInterface {
403protected:
404 virtual ~InputDispatcher();
405
406public:
407 explicit InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy);
408
Michael Wright3dd60e22019-03-27 22:06:44 +0000409 virtual void dump(std::string& dump) override;
410 virtual void monitor() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800411
Michael Wright3dd60e22019-03-27 22:06:44 +0000412 virtual void dispatchOnce() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800413
Michael Wright3dd60e22019-03-27 22:06:44 +0000414 virtual void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) override;
415 virtual void notifyKey(const NotifyKeyArgs* args) override;
416 virtual void notifyMotion(const NotifyMotionArgs* args) override;
417 virtual void notifySwitch(const NotifySwitchArgs* args) override;
418 virtual void notifyDeviceReset(const NotifyDeviceResetArgs* args) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800419
Garfield Tane4fc0102019-09-11 13:16:25 -0700420 virtual int32_t injectInputEvent(const InputEvent* event, int32_t injectorPid,
421 int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,
422 uint32_t policyFlags) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800423
Garfield Tane4fc0102019-09-11 13:16:25 -0700424 virtual void setInputWindows(
425 const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId,
Michael Wright3dd60e22019-03-27 22:06:44 +0000426 const sp<ISetInputWindowsListener>& setInputWindowsListener = nullptr) override;
Garfield Tane4fc0102019-09-11 13:16:25 -0700427 virtual void setFocusedApplication(
428 int32_t displayId, const sp<InputApplicationHandle>& inputApplicationHandle) override;
Michael Wright3dd60e22019-03-27 22:06:44 +0000429 virtual void setFocusedDisplay(int32_t displayId) override;
430 virtual void setInputDispatchMode(bool enabled, bool frozen) override;
431 virtual void setInputFilterEnabled(bool enabled) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800432
Garfield Tane4fc0102019-09-11 13:16:25 -0700433 virtual bool transferTouchFocus(const sp<IBinder>& fromToken,
434 const sp<IBinder>& toToken) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800435
436 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel,
Garfield Tane4fc0102019-09-11 13:16:25 -0700437 int32_t displayId) override;
438 virtual status_t registerInputMonitor(const sp<InputChannel>& inputChannel, int32_t displayId,
439 bool isGestureMonitor) override;
Michael Wright3dd60e22019-03-27 22:06:44 +0000440 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) override;
441 virtual status_t pilferPointers(const sp<IBinder>& token) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800442
443private:
444 template <typename T>
445 struct Link {
446 T* next;
447 T* prev;
448
449 protected:
Garfield Tane4fc0102019-09-11 13:16:25 -0700450 inline Link() : next(nullptr), prev(nullptr) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800451 };
452
453 struct InjectionState {
454 mutable int32_t refCount;
455
456 int32_t injectorPid;
457 int32_t injectorUid;
Garfield Tane4fc0102019-09-11 13:16:25 -0700458 int32_t injectionResult; // initially INPUT_EVENT_INJECTION_PENDING
459 bool injectionIsAsync; // set to true if injection is not waiting for the result
Michael Wrightd02c5b62014-02-10 15:10:22 -0800460 int32_t pendingForegroundDispatches; // the number of foreground dispatches in progress
461
462 InjectionState(int32_t injectorPid, int32_t injectorUid);
463 void release();
464
465 private:
466 ~InjectionState();
467 };
468
469 struct EventEntry : Link<EventEntry> {
Garfield Tane4fc0102019-09-11 13:16:25 -0700470 enum { TYPE_CONFIGURATION_CHANGED, TYPE_DEVICE_RESET, TYPE_KEY, TYPE_MOTION };
Michael Wrightd02c5b62014-02-10 15:10:22 -0800471
Prabir Pradhan42611e02018-11-27 14:04:02 -0800472 uint32_t sequenceNum;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800473 mutable int32_t refCount;
474 int32_t type;
475 nsecs_t eventTime;
476 uint32_t policyFlags;
477 InjectionState* injectionState;
478
479 bool dispatchInProgress; // initially false, set to true while dispatching
480
Yi Kong9b14ac62018-07-17 13:48:38 -0700481 inline bool isInjected() const { return injectionState != nullptr; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800482
483 void release();
484
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800485 virtual void appendDescription(std::string& msg) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800486
487 protected:
Prabir Pradhan42611e02018-11-27 14:04:02 -0800488 EventEntry(uint32_t sequenceNum, int32_t type, nsecs_t eventTime, uint32_t policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800489 virtual ~EventEntry();
490 void releaseInjectionState();
491 };
492
493 struct ConfigurationChangedEntry : EventEntry {
Prabir Pradhan42611e02018-11-27 14:04:02 -0800494 explicit ConfigurationChangedEntry(uint32_t sequenceNum, nsecs_t eventTime);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800495 virtual void appendDescription(std::string& msg) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800496
497 protected:
498 virtual ~ConfigurationChangedEntry();
499 };
500
501 struct DeviceResetEntry : EventEntry {
502 int32_t deviceId;
503
Prabir Pradhan42611e02018-11-27 14:04:02 -0800504 DeviceResetEntry(uint32_t sequenceNum, nsecs_t eventTime, int32_t deviceId);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800505 virtual void appendDescription(std::string& msg) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800506
507 protected:
508 virtual ~DeviceResetEntry();
509 };
510
511 struct KeyEntry : EventEntry {
512 int32_t deviceId;
513 uint32_t source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100514 int32_t displayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800515 int32_t action;
516 int32_t flags;
517 int32_t keyCode;
518 int32_t scanCode;
519 int32_t metaState;
520 int32_t repeatCount;
521 nsecs_t downTime;
522
523 bool syntheticRepeat; // set to true for synthetic key repeats
524
525 enum InterceptKeyResult {
526 INTERCEPT_KEY_RESULT_UNKNOWN,
527 INTERCEPT_KEY_RESULT_SKIP,
528 INTERCEPT_KEY_RESULT_CONTINUE,
529 INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER,
530 };
531 InterceptKeyResult interceptKeyResult; // set based on the interception result
Garfield Tane4fc0102019-09-11 13:16:25 -0700532 nsecs_t interceptKeyWakeupTime; // used with INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER
Michael Wrightd02c5b62014-02-10 15:10:22 -0800533
Garfield Tane4fc0102019-09-11 13:16:25 -0700534 KeyEntry(uint32_t sequenceNum, nsecs_t eventTime, int32_t deviceId, uint32_t source,
535 int32_t displayId, uint32_t policyFlags, int32_t action, int32_t flags,
536 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
537 nsecs_t downTime);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800538 virtual void appendDescription(std::string& msg) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800539 void recycle();
540
541 protected:
542 virtual ~KeyEntry();
543 };
544
545 struct MotionEntry : EventEntry {
546 nsecs_t eventTime;
547 int32_t deviceId;
548 uint32_t source;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800549 int32_t displayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800550 int32_t action;
Michael Wright7b159c92015-05-14 14:48:03 +0100551 int32_t actionButton;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800552 int32_t flags;
553 int32_t metaState;
554 int32_t buttonState;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800555 MotionClassification classification;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800556 int32_t edgeFlags;
557 float xPrecision;
558 float yPrecision;
559 nsecs_t downTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800560 uint32_t pointerCount;
561 PointerProperties pointerProperties[MAX_POINTERS];
562 PointerCoords pointerCoords[MAX_POINTERS];
563
Garfield Tane4fc0102019-09-11 13:16:25 -0700564 MotionEntry(uint32_t sequenceNum, nsecs_t eventTime, int32_t deviceId, uint32_t source,
565 int32_t displayId, uint32_t policyFlags, int32_t action, int32_t actionButton,
566 int32_t flags, int32_t metaState, int32_t buttonState,
567 MotionClassification classification, int32_t edgeFlags, float xPrecision,
568 float yPrecision, nsecs_t downTime, uint32_t pointerCount,
569 const PointerProperties* pointerProperties, const PointerCoords* pointerCoords,
570 float xOffset, float yOffset);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800571 virtual void appendDescription(std::string& msg) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800572
573 protected:
574 virtual ~MotionEntry();
575 };
576
577 // Tracks the progress of dispatching a particular event to a particular connection.
578 struct DispatchEntry : Link<DispatchEntry> {
579 const uint32_t seq; // unique sequence number, never 0
580
581 EventEntry* eventEntry; // the event to dispatch
582 int32_t targetFlags;
583 float xOffset;
584 float yOffset;
Robert Carre07e1032018-11-26 12:55:53 -0800585 float globalScaleFactor;
586 float windowXScale = 1.0f;
587 float windowYScale = 1.0f;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800588 nsecs_t deliveryTime; // time when the event was actually delivered
589
590 // Set to the resolved action and flags when the event is enqueued.
591 int32_t resolvedAction;
592 int32_t resolvedFlags;
593
Garfield Tane4fc0102019-09-11 13:16:25 -0700594 DispatchEntry(EventEntry* eventEntry, int32_t targetFlags, float xOffset, float yOffset,
595 float globalScaleFactor, float windowXScale, float windowYScale);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800596 ~DispatchEntry();
597
598 inline bool hasForegroundTarget() const {
599 return targetFlags & InputTarget::FLAG_FOREGROUND;
600 }
601
Garfield Tane4fc0102019-09-11 13:16:25 -0700602 inline bool isSplit() const { return targetFlags & InputTarget::FLAG_SPLIT; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800603
604 private:
605 static volatile int32_t sNextSeqAtomic;
606
607 static uint32_t nextSeq();
608 };
609
610 // A command entry captures state and behavior for an action to be performed in the
611 // dispatch loop after the initial processing has taken place. It is essentially
612 // a kind of continuation used to postpone sensitive policy interactions to a point
613 // in the dispatch loop where it is safe to release the lock (generally after finishing
614 // the critical parts of the dispatch cycle).
615 //
616 // The special thing about commands is that they can voluntarily release and reacquire
617 // the dispatcher lock at will. Initially when the command starts running, the
618 // dispatcher lock is held. However, if the command needs to call into the policy to
619 // do some work, it can release the lock, do the work, then reacquire the lock again
620 // before returning.
621 //
622 // This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch
623 // never calls into the policy while holding its lock.
624 //
625 // Commands are implicitly 'LockedInterruptible'.
626 struct CommandEntry;
627 typedef void (InputDispatcher::*Command)(CommandEntry* commandEntry);
628
629 class Connection;
630 struct CommandEntry : Link<CommandEntry> {
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700631 explicit CommandEntry(Command command);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800632 ~CommandEntry();
633
634 Command command;
635
636 // parameters for the command (usage varies by command)
637 sp<Connection> connection;
638 nsecs_t eventTime;
639 KeyEntry* keyEntry;
640 sp<InputApplicationHandle> inputApplicationHandle;
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800641 std::string reason;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800642 int32_t userActivityEventType;
643 uint32_t seq;
644 bool handled;
Robert Carr803535b2018-08-02 16:38:15 -0700645 sp<InputChannel> inputChannel;
chaviw0c06c6e2019-01-09 13:27:07 -0800646 sp<IBinder> oldToken;
647 sp<IBinder> newToken;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800648 };
649
650 // Generic queue implementation.
651 template <typename T>
652 struct Queue {
653 T* head;
654 T* tail;
Jon McCaffrey65dbe972014-11-18 12:07:08 -0800655 uint32_t entryCount;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800656
Garfield Tane4fc0102019-09-11 13:16:25 -0700657 inline Queue() : head(nullptr), tail(nullptr), entryCount(0) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800658
Garfield Tane4fc0102019-09-11 13:16:25 -0700659 inline bool isEmpty() const { return !head; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800660
661 inline void enqueueAtTail(T* entry) {
Jon McCaffrey65dbe972014-11-18 12:07:08 -0800662 entryCount++;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800663 entry->prev = tail;
664 if (tail) {
665 tail->next = entry;
666 } else {
667 head = entry;
668 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700669 entry->next = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800670 tail = entry;
671 }
672
673 inline void enqueueAtHead(T* entry) {
Jon McCaffrey65dbe972014-11-18 12:07:08 -0800674 entryCount++;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800675 entry->next = head;
676 if (head) {
677 head->prev = entry;
678 } else {
679 tail = entry;
680 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700681 entry->prev = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800682 head = entry;
683 }
684
685 inline void dequeue(T* entry) {
Jon McCaffrey65dbe972014-11-18 12:07:08 -0800686 entryCount--;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800687 if (entry->prev) {
688 entry->prev->next = entry->next;
689 } else {
690 head = entry->next;
691 }
692 if (entry->next) {
693 entry->next->prev = entry->prev;
694 } else {
695 tail = entry->prev;
696 }
697 }
698
699 inline T* dequeueAtHead() {
Jon McCaffrey65dbe972014-11-18 12:07:08 -0800700 entryCount--;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800701 T* entry = head;
702 head = entry->next;
703 if (head) {
Yi Kong9b14ac62018-07-17 13:48:38 -0700704 head->prev = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800705 } else {
Yi Kong9b14ac62018-07-17 13:48:38 -0700706 tail = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800707 }
708 return entry;
709 }
710
Garfield Tane4fc0102019-09-11 13:16:25 -0700711 uint32_t count() const { return entryCount; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800712 };
713
714 /* Specifies which events are to be canceled and why. */
715 struct CancelationOptions {
716 enum Mode {
717 CANCEL_ALL_EVENTS = 0,
718 CANCEL_POINTER_EVENTS = 1,
719 CANCEL_NON_POINTER_EVENTS = 2,
720 CANCEL_FALLBACK_EVENTS = 3,
721 };
722
723 // The criterion to use to determine which events should be canceled.
724 Mode mode;
725
726 // Descriptive reason for the cancelation.
727 const char* reason;
728
Michael Wright3dd60e22019-03-27 22:06:44 +0000729 // The specific keycode of the key event to cancel, or nullopt to cancel any key event.
730 std::optional<int32_t> keyCode = std::nullopt;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800731
Michael Wright3dd60e22019-03-27 22:06:44 +0000732 // The specific device id of events to cancel, or nullopt to cancel events from any device.
733 std::optional<int32_t> deviceId = std::nullopt;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800734
Michael Wright3dd60e22019-03-27 22:06:44 +0000735 // The specific display id of events to cancel, or nullopt to cancel events on any display.
736 std::optional<int32_t> displayId = std::nullopt;
737
Garfield Tane4fc0102019-09-11 13:16:25 -0700738 CancelationOptions(Mode mode, const char* reason) : mode(mode), reason(reason) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800739 };
740
741 /* Tracks dispatched key and motion event state so that cancelation events can be
742 * synthesized when events are dropped. */
743 class InputState {
744 public:
745 InputState();
746 ~InputState();
747
748 // Returns true if there is no state to be canceled.
749 bool isNeutral() const;
750
751 // Returns true if the specified source is known to have received a hover enter
752 // motion event.
753 bool isHovering(int32_t deviceId, uint32_t source, int32_t displayId) const;
754
755 // Records tracking information for a key event that has just been published.
756 // Returns true if the event should be delivered, false if it is inconsistent
757 // and should be skipped.
758 bool trackKey(const KeyEntry* entry, int32_t action, int32_t flags);
759
760 // Records tracking information for a motion event that has just been published.
761 // Returns true if the event should be delivered, false if it is inconsistent
762 // and should be skipped.
763 bool trackMotion(const MotionEntry* entry, int32_t action, int32_t flags);
764
765 // Synthesizes cancelation events for the current state and resets the tracked state.
Garfield Tane4fc0102019-09-11 13:16:25 -0700766 void synthesizeCancelationEvents(nsecs_t currentTime, std::vector<EventEntry*>& outEvents,
767 const CancelationOptions& options);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800768
769 // Clears the current state.
770 void clear();
771
772 // Copies pointer-related parts of the input state to another instance.
773 void copyPointerStateTo(InputState& other) const;
774
775 // Gets the fallback key associated with a keycode.
776 // Returns -1 if none.
777 // Returns AKEYCODE_UNKNOWN if we are only dispatching the unhandled key to the policy.
778 int32_t getFallbackKey(int32_t originalKeyCode);
779
780 // Sets the fallback key for a particular keycode.
781 void setFallbackKey(int32_t originalKeyCode, int32_t fallbackKeyCode);
782
783 // Removes the fallback key for a particular keycode.
784 void removeFallbackKey(int32_t originalKeyCode);
785
786 inline const KeyedVector<int32_t, int32_t>& getFallbackKeys() const {
787 return mFallbackKeys;
788 }
789
790 private:
791 struct KeyMemento {
792 int32_t deviceId;
793 uint32_t source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100794 int32_t displayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800795 int32_t keyCode;
796 int32_t scanCode;
797 int32_t metaState;
798 int32_t flags;
799 nsecs_t downTime;
800 uint32_t policyFlags;
801 };
802
803 struct MotionMemento {
804 int32_t deviceId;
805 uint32_t source;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800806 int32_t displayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800807 int32_t flags;
808 float xPrecision;
809 float yPrecision;
810 nsecs_t downTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800811 uint32_t pointerCount;
812 PointerProperties pointerProperties[MAX_POINTERS];
813 PointerCoords pointerCoords[MAX_POINTERS];
814 bool hovering;
815 uint32_t policyFlags;
816
817 void setPointers(const MotionEntry* entry);
818 };
819
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800820 std::vector<KeyMemento> mKeyMementos;
821 std::vector<MotionMemento> mMotionMementos;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800822 KeyedVector<int32_t, int32_t> mFallbackKeys;
823
824 ssize_t findKeyMemento(const KeyEntry* entry) const;
825 ssize_t findMotionMemento(const MotionEntry* entry, bool hovering) const;
826
827 void addKeyMemento(const KeyEntry* entry, int32_t flags);
828 void addMotionMemento(const MotionEntry* entry, int32_t flags, bool hovering);
829
Garfield Tane4fc0102019-09-11 13:16:25 -0700830 static bool shouldCancelKey(const KeyMemento& memento, const CancelationOptions& options);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800831 static bool shouldCancelMotion(const MotionMemento& memento,
Garfield Tane4fc0102019-09-11 13:16:25 -0700832 const CancelationOptions& options);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800833 };
834
835 /* Manages the dispatch state associated with a single input channel. */
836 class Connection : public RefBase {
837 protected:
838 virtual ~Connection();
839
840 public:
841 enum Status {
842 // Everything is peachy.
843 STATUS_NORMAL,
844 // An unrecoverable communication error has occurred.
845 STATUS_BROKEN,
846 // The input channel has been unregistered.
847 STATUS_ZOMBIE
848 };
849
850 Status status;
851 sp<InputChannel> inputChannel; // never null
Michael Wrightd02c5b62014-02-10 15:10:22 -0800852 bool monitor;
853 InputPublisher inputPublisher;
854 InputState inputState;
855
856 // True if the socket is full and no further events can be published until
857 // the application consumes some of the input.
858 bool inputPublisherBlocked;
859
860 // Queue of events that need to be published to the connection.
861 Queue<DispatchEntry> outboundQueue;
862
863 // Queue of events that have been published to the connection but that have not
864 // yet received a "finished" response from the application.
865 Queue<DispatchEntry> waitQueue;
866
Robert Carr803535b2018-08-02 16:38:15 -0700867 explicit Connection(const sp<InputChannel>& inputChannel, bool monitor);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800868
Siarhei Vishniakou587c3f02018-01-04 11:46:44 -0800869 inline const std::string getInputChannelName() const { return inputChannel->getName(); }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800870
Siarhei Vishniakou587c3f02018-01-04 11:46:44 -0800871 const std::string getWindowName() const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800872 const char* getStatusLabel() const;
873
874 DispatchEntry* findWaitQueueEntry(uint32_t seq);
875 };
876
Michael Wright3dd60e22019-03-27 22:06:44 +0000877 struct Monitor {
878 sp<InputChannel> inputChannel; // never null
879
880 explicit Monitor(const sp<InputChannel>& inputChannel);
881 };
882
Michael Wrightd02c5b62014-02-10 15:10:22 -0800883 enum DropReason {
884 DROP_REASON_NOT_DROPPED = 0,
885 DROP_REASON_POLICY = 1,
886 DROP_REASON_APP_SWITCH = 2,
887 DROP_REASON_DISABLED = 3,
888 DROP_REASON_BLOCKED = 4,
889 DROP_REASON_STALE = 5,
890 };
891
892 sp<InputDispatcherPolicyInterface> mPolicy;
893 InputDispatcherConfiguration mConfig;
894
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800895 std::mutex mLock;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800896
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800897 std::condition_variable mDispatcherIsAlive;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800898
899 sp<Looper> mLooper;
900
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800901 EventEntry* mPendingEvent GUARDED_BY(mLock);
902 Queue<EventEntry> mInboundQueue GUARDED_BY(mLock);
903 Queue<EventEntry> mRecentQueue GUARDED_BY(mLock);
904 Queue<CommandEntry> mCommandQueue GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800905
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800906 DropReason mLastDropReason GUARDED_BY(mLock);
Michael Wright3a981722015-06-10 15:26:13 +0100907
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800908 void dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800909
910 // Enqueues an inbound event. Returns true if mLooper->wake() should be called.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800911 bool enqueueInboundEventLocked(EventEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800912
913 // Cleans up input state when dropping an inbound event.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800914 void dropInboundEventLocked(EventEntry* entry, DropReason dropReason) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800915
916 // Adds an event to a queue of recent events for debugging purposes.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800917 void addRecentEventLocked(EventEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800918
919 // App switch latency optimization.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800920 bool mAppSwitchSawKeyDown GUARDED_BY(mLock);
921 nsecs_t mAppSwitchDueTime GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800922
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800923 bool isAppSwitchKeyEvent(KeyEntry* keyEntry);
924 bool isAppSwitchPendingLocked() REQUIRES(mLock);
925 void resetPendingAppSwitchLocked(bool handled) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800926
927 // Stale event latency optimization.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800928 static bool isStaleEvent(nsecs_t currentTime, EventEntry* entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800929
930 // Blocked event latency optimization. Drops old events when the user intends
931 // to transfer focus to a new application.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800932 EventEntry* mNextUnblockedEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800933
Tiger Huang85b8c5e2019-01-17 18:34:54 +0800934 sp<InputWindowHandle> findTouchedWindowAtLocked(int32_t displayId, int32_t x, int32_t y,
Garfield Tane4fc0102019-09-11 13:16:25 -0700935 bool addOutsideTargets = false,
936 bool addPortalWindows = false) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800937
938 // All registered connections mapped by channel file descriptor.
Garfield Tane4fc0102019-09-11 13:16:25 -0700939 KeyedVector<int, sp<Connection>> mConnectionsByFd GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800940
Robert Carr5c8a0262018-10-03 16:30:44 -0700941 struct IBinderHash {
942 std::size_t operator()(const sp<IBinder>& b) const {
Garfield Tane4fc0102019-09-11 13:16:25 -0700943 return std::hash<IBinder*>{}(b.get());
Robert Carr5c8a0262018-10-03 16:30:44 -0700944 }
945 };
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800946 std::unordered_map<sp<IBinder>, sp<InputChannel>, IBinderHash> mInputChannelsByToken
947 GUARDED_BY(mLock);
Robert Carr5c8a0262018-10-03 16:30:44 -0700948
Michael Wright3dd60e22019-03-27 22:06:44 +0000949 // Finds the display ID of the gesture monitor identified by the provided token.
950 std::optional<int32_t> findGestureMonitorDisplayByTokenLocked(const sp<IBinder>& token)
951 REQUIRES(mLock);
952
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800953 ssize_t getConnectionIndexLocked(const sp<InputChannel>& inputChannel) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800954
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800955 // Input channels that will receive a copy of all input events sent to the provided display.
Garfield Tane4fc0102019-09-11 13:16:25 -0700956 std::unordered_map<int32_t, std::vector<Monitor>> mGlobalMonitorsByDisplay GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800957
Michael Wright3dd60e22019-03-27 22:06:44 +0000958 // Input channels that will receive pointer events that start within the corresponding display.
959 // These are a bit special when compared to global monitors since they'll cause gesture streams
960 // to continue even when there isn't a touched window,and have the ability to steal the rest of
961 // the pointer stream in order to claim it for a system gesture.
Garfield Tane4fc0102019-09-11 13:16:25 -0700962 std::unordered_map<int32_t, std::vector<Monitor>> mGestureMonitorsByDisplay GUARDED_BY(mLock);
Michael Wright3dd60e22019-03-27 22:06:44 +0000963
Michael Wrightd02c5b62014-02-10 15:10:22 -0800964 // Event injection and synchronization.
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800965 std::condition_variable mInjectionResultAvailable;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800966 bool hasInjectionPermission(int32_t injectorPid, int32_t injectorUid);
Siarhei Vishniakou62683e82019-03-06 17:59:56 -0800967 void setInjectionResult(EventEntry* entry, int32_t injectionResult);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800968
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800969 std::condition_variable mInjectionSyncFinished;
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800970 void incrementPendingForegroundDispatches(EventEntry* entry);
Siarhei Vishniakou62683e82019-03-06 17:59:56 -0800971 void decrementPendingForegroundDispatches(EventEntry* entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800972
973 // Key repeat tracking.
974 struct KeyRepeatState {
975 KeyEntry* lastKeyEntry; // or null if no repeat
976 nsecs_t nextRepeatTime;
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800977 } mKeyRepeatState GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800978
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800979 void resetKeyRepeatLocked() REQUIRES(mLock);
980 KeyEntry* synthesizeKeyRepeatLocked(nsecs_t currentTime) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800981
Michael Wright78f24442014-08-06 15:55:28 -0700982 // Key replacement tracking
983 struct KeyReplacement {
984 int32_t keyCode;
985 int32_t deviceId;
986 bool operator==(const KeyReplacement& rhs) const {
987 return keyCode == rhs.keyCode && deviceId == rhs.deviceId;
988 }
989 bool operator<(const KeyReplacement& rhs) const {
990 return keyCode != rhs.keyCode ? keyCode < rhs.keyCode : deviceId < rhs.deviceId;
991 }
992 };
993 // Maps the key code replaced, device id tuple to the key code it was replaced with
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800994 KeyedVector<KeyReplacement, int32_t> mReplacedKeys GUARDED_BY(mLock);
Siarhei Vishniakou61fafdd2018-04-13 11:00:58 -0500995 // Process certain Meta + Key combinations
Garfield Tane4fc0102019-09-11 13:16:25 -0700996 void accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, int32_t& keyCode,
997 int32_t& metaState);
Michael Wright78f24442014-08-06 15:55:28 -0700998
Michael Wrightd02c5b62014-02-10 15:10:22 -0800999 // Deferred command processing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001000 bool haveCommandsLocked() const REQUIRES(mLock);
1001 bool runCommandsLockedInterruptible() REQUIRES(mLock);
1002 CommandEntry* postCommandLocked(Command command) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001003
1004 // Input filter processing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001005 bool shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) REQUIRES(mLock);
1006 bool shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001007
1008 // Inbound event processing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001009 void drainInboundQueueLocked() REQUIRES(mLock);
1010 void releasePendingEventLocked() REQUIRES(mLock);
1011 void releaseInboundEventLocked(EventEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001012
1013 // Dispatch state.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001014 bool mDispatchEnabled GUARDED_BY(mLock);
1015 bool mDispatchFrozen GUARDED_BY(mLock);
1016 bool mInputFilterEnabled GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001017
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001018 std::unordered_map<int32_t, std::vector<sp<InputWindowHandle>>> mWindowHandlesByDisplay
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001019 GUARDED_BY(mLock);
Arthur Hungb92218b2018-08-14 12:00:21 +08001020 // Get window handles by display, return an empty vector if not found.
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001021 std::vector<sp<InputWindowHandle>> getWindowHandlesLocked(int32_t displayId) const
1022 REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001023 sp<InputWindowHandle> getWindowHandleLocked(const sp<IBinder>& windowHandleToken) const
1024 REQUIRES(mLock);
1025 sp<InputChannel> getInputChannelLocked(const sp<IBinder>& windowToken) const REQUIRES(mLock);
1026 bool hasWindowHandleLocked(const sp<InputWindowHandle>& windowHandle) const REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001027
1028 // Focus tracking for keys, trackball, etc.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001029 std::unordered_map<int32_t, sp<InputWindowHandle>> mFocusedWindowHandlesByDisplay
1030 GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001031
1032 // Focus tracking for touch.
1033 struct TouchedWindow {
1034 sp<InputWindowHandle> windowHandle;
1035 int32_t targetFlags;
Garfield Tane4fc0102019-09-11 13:16:25 -07001036 BitSet32 pointerIds; // zero unless target flag FLAG_SPLIT is set
Michael Wrightd02c5b62014-02-10 15:10:22 -08001037 };
Michael Wright3dd60e22019-03-27 22:06:44 +00001038
1039 // For tracking the offsets we need to apply when adding gesture monitor targets.
1040 struct TouchedMonitor {
1041 Monitor monitor;
1042 float xOffset = 0.f;
1043 float yOffset = 0.f;
1044
1045 explicit TouchedMonitor(const Monitor& monitor, float xOffset, float yOffset);
1046 };
1047
Michael Wrightd02c5b62014-02-10 15:10:22 -08001048 struct TouchState {
1049 bool down;
1050 bool split;
Garfield Tane4fc0102019-09-11 13:16:25 -07001051 int32_t deviceId; // id of the device that is currently down, others are rejected
1052 uint32_t source; // source of the device that is current down, others are rejected
Michael Wrightd02c5b62014-02-10 15:10:22 -08001053 int32_t displayId; // id to the display that currently has a touch, others are rejected
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001054 std::vector<TouchedWindow> windows;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001055
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001056 // This collects the portal windows that the touch has gone through. Each portal window
1057 // targets a display (embedded display for most cases). With this info, we can add the
1058 // monitoring channels of the displays touched.
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001059 std::vector<sp<InputWindowHandle>> portalWindows;
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001060
Michael Wright3dd60e22019-03-27 22:06:44 +00001061 std::vector<TouchedMonitor> gestureMonitors;
1062
Michael Wrightd02c5b62014-02-10 15:10:22 -08001063 TouchState();
1064 ~TouchState();
1065 void reset();
1066 void copyFrom(const TouchState& other);
Garfield Tane4fc0102019-09-11 13:16:25 -07001067 void addOrUpdateWindow(const sp<InputWindowHandle>& windowHandle, int32_t targetFlags,
1068 BitSet32 pointerIds);
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001069 void addPortalWindow(const sp<InputWindowHandle>& windowHandle);
Michael Wright3dd60e22019-03-27 22:06:44 +00001070 void addGestureMonitors(const std::vector<TouchedMonitor>& monitors);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001071 void removeWindow(const sp<InputWindowHandle>& windowHandle);
Robert Carr803535b2018-08-02 16:38:15 -07001072 void removeWindowByToken(const sp<IBinder>& token);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001073 void filterNonAsIsTouchWindows();
Michael Wright3dd60e22019-03-27 22:06:44 +00001074 void filterNonMonitors();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001075 sp<InputWindowHandle> getFirstForegroundWindowHandle() const;
1076 bool isSlippery() const;
1077 };
1078
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001079 KeyedVector<int32_t, TouchState> mTouchStatesByDisplay GUARDED_BY(mLock);
1080 TouchState mTempTouchState GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001081
Tiger Huang721e26f2018-07-24 22:26:19 +08001082 // Focused applications.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001083 std::unordered_map<int32_t, sp<InputApplicationHandle>> mFocusedApplicationHandlesByDisplay
1084 GUARDED_BY(mLock);
Tiger Huang721e26f2018-07-24 22:26:19 +08001085
1086 // Top focused display.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001087 int32_t mFocusedDisplayId GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001088
1089 // Dispatcher state at time of last ANR.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001090 std::string mLastANRState GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001091
1092 // Dispatch inbound events.
Garfield Tane4fc0102019-09-11 13:16:25 -07001093 bool dispatchConfigurationChangedLocked(nsecs_t currentTime, ConfigurationChangedEntry* entry)
1094 REQUIRES(mLock);
1095 bool dispatchDeviceResetLocked(nsecs_t currentTime, DeviceResetEntry* entry) REQUIRES(mLock);
1096 bool dispatchKeyLocked(nsecs_t currentTime, KeyEntry* entry, DropReason* dropReason,
1097 nsecs_t* nextWakeupTime) REQUIRES(mLock);
1098 bool dispatchMotionLocked(nsecs_t currentTime, MotionEntry* entry, DropReason* dropReason,
1099 nsecs_t* nextWakeupTime) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001100 void dispatchEventLocked(nsecs_t currentTime, EventEntry* entry,
Garfield Tane4fc0102019-09-11 13:16:25 -07001101 const std::vector<InputTarget>& inputTargets) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001102
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001103 void logOutboundKeyDetails(const char* prefix, const KeyEntry* entry);
1104 void logOutboundMotionDetails(const char* prefix, const MotionEntry* entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001105
1106 // Keeping track of ANR timeouts.
1107 enum InputTargetWaitCause {
1108 INPUT_TARGET_WAIT_CAUSE_NONE,
1109 INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY,
1110 INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY,
1111 };
1112
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001113 InputTargetWaitCause mInputTargetWaitCause GUARDED_BY(mLock);
1114 nsecs_t mInputTargetWaitStartTime GUARDED_BY(mLock);
1115 nsecs_t mInputTargetWaitTimeoutTime GUARDED_BY(mLock);
1116 bool mInputTargetWaitTimeoutExpired GUARDED_BY(mLock);
1117 sp<IBinder> mInputTargetWaitApplicationToken GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001118
1119 // Contains the last window which received a hover event.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001120 sp<InputWindowHandle> mLastHoverWindowHandle GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001121
1122 // Finding targets for input events.
1123 int32_t handleTargetsNotReadyLocked(nsecs_t currentTime, const EventEntry* entry,
Garfield Tane4fc0102019-09-11 13:16:25 -07001124 const sp<InputApplicationHandle>& applicationHandle,
1125 const sp<InputWindowHandle>& windowHandle,
1126 nsecs_t* nextWakeupTime, const char* reason)
1127 REQUIRES(mLock);
Robert Carr803535b2018-08-02 16:38:15 -07001128
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001129 void removeWindowByTokenLocked(const sp<IBinder>& token) REQUIRES(mLock);
Robert Carr803535b2018-08-02 16:38:15 -07001130
Michael Wrightd02c5b62014-02-10 15:10:22 -08001131 void resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout,
Garfield Tane4fc0102019-09-11 13:16:25 -07001132 const sp<InputChannel>& inputChannel)
1133 REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001134 nsecs_t getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime) REQUIRES(mLock);
1135 void resetANRTimeoutsLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001136
Tiger Huang721e26f2018-07-24 22:26:19 +08001137 int32_t getTargetDisplayId(const EventEntry* entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001138 int32_t findFocusedWindowTargetsLocked(nsecs_t currentTime, const EventEntry* entry,
Garfield Tane4fc0102019-09-11 13:16:25 -07001139 std::vector<InputTarget>& inputTargets,
1140 nsecs_t* nextWakeupTime) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001141 int32_t findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry* entry,
Garfield Tane4fc0102019-09-11 13:16:25 -07001142 std::vector<InputTarget>& inputTargets,
1143 nsecs_t* nextWakeupTime,
1144 bool* outConflictingPointerActions) REQUIRES(mLock);
1145 std::vector<TouchedMonitor> findTouchedGestureMonitorsLocked(
1146 int32_t displayId, const std::vector<sp<InputWindowHandle>>& portalWindows)
1147 REQUIRES(mLock);
Michael Wright3dd60e22019-03-27 22:06:44 +00001148 void addGestureMonitors(const std::vector<Monitor>& monitors,
Garfield Tane4fc0102019-09-11 13:16:25 -07001149 std::vector<TouchedMonitor>& outTouchedMonitors, float xOffset = 0,
1150 float yOffset = 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001151
Garfield Tane4fc0102019-09-11 13:16:25 -07001152 void addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle, int32_t targetFlags,
1153 BitSet32 pointerIds, std::vector<InputTarget>& inputTargets)
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001154 REQUIRES(mLock);
Michael Wright3dd60e22019-03-27 22:06:44 +00001155 void addMonitoringTargetLocked(const Monitor& monitor, float xOffset, float yOffset,
Garfield Tane4fc0102019-09-11 13:16:25 -07001156 std::vector<InputTarget>& inputTargets) REQUIRES(mLock);
1157 void addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, int32_t displayId,
1158 float xOffset = 0, float yOffset = 0) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001159
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001160 void pokeUserActivityLocked(const EventEntry* eventEntry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001161 bool checkInjectionPermission(const sp<InputWindowHandle>& windowHandle,
Garfield Tane4fc0102019-09-11 13:16:25 -07001162 const InjectionState* injectionState);
1163 bool isWindowObscuredAtPointLocked(const sp<InputWindowHandle>& windowHandle, int32_t x,
1164 int32_t y) const REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001165 bool isWindowObscuredLocked(const sp<InputWindowHandle>& windowHandle) const REQUIRES(mLock);
1166 std::string getApplicationWindowLabel(const sp<InputApplicationHandle>& applicationHandle,
Garfield Tane4fc0102019-09-11 13:16:25 -07001167 const sp<InputWindowHandle>& windowHandle);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001168
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001169 std::string checkWindowReadyForMoreInputLocked(nsecs_t currentTime,
Garfield Tane4fc0102019-09-11 13:16:25 -07001170 const sp<InputWindowHandle>& windowHandle,
1171 const EventEntry* eventEntry,
1172 const char* targetType) REQUIRES(mLock);
Jeff Brownffb49772014-10-10 19:01:34 -07001173
Michael Wrightd02c5b62014-02-10 15:10:22 -08001174 // Manage the dispatch cycle for a single connection.
1175 // These methods are deliberately not Interruptible because doing all of the work
1176 // with the mutex held makes it easier to ensure that connection invariants are maintained.
1177 // If needed, the methods post commands to run later once the critical bits are done.
1178 void prepareDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Garfield Tane4fc0102019-09-11 13:16:25 -07001179 EventEntry* eventEntry, const InputTarget* inputTarget)
1180 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001181 void enqueueDispatchEntriesLocked(nsecs_t currentTime, const sp<Connection>& connection,
Garfield Tane4fc0102019-09-11 13:16:25 -07001182 EventEntry* eventEntry, const InputTarget* inputTarget)
1183 REQUIRES(mLock);
1184 void enqueueDispatchEntryLocked(const sp<Connection>& connection, EventEntry* eventEntry,
1185 const InputTarget* inputTarget, int32_t dispatchMode)
chaviw8c9cf542019-03-25 13:02:48 -07001186 REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001187 void startDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection)
1188 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001189 void finishDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Garfield Tane4fc0102019-09-11 13:16:25 -07001190 uint32_t seq, bool handled) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001191 void abortBrokenDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Garfield Tane4fc0102019-09-11 13:16:25 -07001192 bool notify) REQUIRES(mLock);
Siarhei Vishniakou62683e82019-03-06 17:59:56 -08001193 void drainDispatchQueue(Queue<DispatchEntry>* queue);
1194 void releaseDispatchEntry(DispatchEntry* dispatchEntry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001195 static int handleReceiveCallback(int fd, int events, void* data);
chaviw8c9cf542019-03-25 13:02:48 -07001196 // The action sent should only be of type AMOTION_EVENT_*
chaviwfd6d3512019-03-25 13:23:49 -07001197 void dispatchPointerDownOutsideFocus(uint32_t source, int32_t action,
Garfield Tane4fc0102019-09-11 13:16:25 -07001198 const sp<IBinder>& newToken) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001199
Garfield Tane4fc0102019-09-11 13:16:25 -07001200 void synthesizeCancelationEventsForAllConnectionsLocked(const CancelationOptions& options)
1201 REQUIRES(mLock);
1202 void synthesizeCancelationEventsForMonitorsLocked(const CancelationOptions& options)
1203 REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001204 void synthesizeCancelationEventsForMonitorsLocked(
Garfield Tane4fc0102019-09-11 13:16:25 -07001205 const CancelationOptions& options,
Michael Wright3dd60e22019-03-27 22:06:44 +00001206 std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001207 void synthesizeCancelationEventsForInputChannelLocked(const sp<InputChannel>& channel,
Garfield Tane4fc0102019-09-11 13:16:25 -07001208 const CancelationOptions& options)
1209 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001210 void synthesizeCancelationEventsForConnectionLocked(const sp<Connection>& connection,
Garfield Tane4fc0102019-09-11 13:16:25 -07001211 const CancelationOptions& options)
1212 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001213
1214 // Splitting motion events across windows.
1215 MotionEntry* splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds);
1216
1217 // Reset and drop everything the dispatcher is doing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001218 void resetAndDropEverythingLocked(const char* reason) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001219
1220 // Dump state.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001221 void dumpDispatchStateLocked(std::string& dump) REQUIRES(mLock);
Michael Wright3dd60e22019-03-27 22:06:44 +00001222 void dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001223 void logDispatchStateLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001224
1225 // Registration.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001226 void removeMonitorChannelLocked(const sp<InputChannel>& inputChannel) REQUIRES(mLock);
Garfield Tane4fc0102019-09-11 13:16:25 -07001227 void removeMonitorChannelLocked(
1228 const sp<InputChannel>& inputChannel,
1229 std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001230 status_t unregisterInputChannelLocked(const sp<InputChannel>& inputChannel, bool notify)
1231 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001232
1233 // Interesting events that we might like to log or tell the framework about.
Garfield Tane4fc0102019-09-11 13:16:25 -07001234 void onDispatchCycleFinishedLocked(nsecs_t currentTime, const sp<Connection>& connection,
1235 uint32_t seq, bool handled) REQUIRES(mLock);
1236 void onDispatchCycleBrokenLocked(nsecs_t currentTime, const sp<Connection>& connection)
1237 REQUIRES(mLock);
chaviw0c06c6e2019-01-09 13:27:07 -08001238 void onFocusChangedLocked(const sp<InputWindowHandle>& oldFocus,
Garfield Tane4fc0102019-09-11 13:16:25 -07001239 const sp<InputWindowHandle>& newFocus) REQUIRES(mLock);
1240 void onANRLocked(nsecs_t currentTime, const sp<InputApplicationHandle>& applicationHandle,
1241 const sp<InputWindowHandle>& windowHandle, nsecs_t eventTime,
1242 nsecs_t waitStartTime, const char* reason) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001243
1244 // Outbound policy interactions.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001245 void doNotifyConfigurationChangedLockedInterruptible(CommandEntry* commandEntry)
1246 REQUIRES(mLock);
1247 void doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
1248 void doNotifyFocusChangedLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
1249 void doNotifyANRLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
1250 void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry)
1251 REQUIRES(mLock);
1252 void doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001253 bool afterKeyEventLockedInterruptible(const sp<Connection>& connection,
Garfield Tane4fc0102019-09-11 13:16:25 -07001254 DispatchEntry* dispatchEntry, KeyEntry* keyEntry,
1255 bool handled) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001256 bool afterMotionEventLockedInterruptible(const sp<Connection>& connection,
Garfield Tane4fc0102019-09-11 13:16:25 -07001257 DispatchEntry* dispatchEntry, MotionEntry* motionEntry,
1258 bool handled) REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001259 void doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001260 void initializeKeyEvent(KeyEvent* event, const KeyEntry* entry);
Garfield Tane4fc0102019-09-11 13:16:25 -07001261 void doOnPointerDownOutsideFocusLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001262
1263 // Statistics gathering.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001264 void updateDispatchStatistics(nsecs_t currentTime, const EventEntry* entry,
Garfield Tane4fc0102019-09-11 13:16:25 -07001265 int32_t injectionResult, nsecs_t timeSpentWaitingForApplication);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001266 void traceInboundQueueLengthLocked() REQUIRES(mLock);
1267 void traceOutboundQueueLength(const sp<Connection>& connection);
1268 void traceWaitQueueLength(const sp<Connection>& connection);
Prabir Pradhanf93562f2018-11-29 12:13:37 -08001269
Prabir Pradhan79a4f0c2019-01-09 11:24:01 -08001270 sp<InputReporterInterface> mReporter;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001271};
1272
1273/* Enqueues and dispatches input events, endlessly. */
1274class InputDispatcherThread : public Thread {
1275public:
1276 explicit InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher);
1277 ~InputDispatcherThread();
1278
1279private:
1280 virtual bool threadLoop();
1281
1282 sp<InputDispatcherInterface> mDispatcher;
1283};
1284
1285} // namespace android
1286
1287#endif // _UI_INPUT_DISPATCHER_H