blob: 1072eb655f24bef873a85cddcc8681df103a590b [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
20#include <input/Input.h>
21#include <input/InputTransport.h>
22#include <utils/KeyedVector.h>
23#include <utils/Vector.h>
24#include <utils/threads.h>
25#include <utils/Timers.h>
26#include <utils/RefBase.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080027#include <utils/Looper.h>
28#include <utils/BitSet.h>
29#include <cutils/atomic.h>
30
31#include <stddef.h>
32#include <unistd.h>
33#include <limits.h>
34
35#include "InputWindow.h"
36#include "InputApplication.h"
37#include "InputListener.h"
38
39
40namespace android {
41
42/*
43 * Constants used to report the outcome of input event injection.
44 */
45enum {
46 /* (INTERNAL USE ONLY) Specifies that injection is pending and its outcome is unknown. */
47 INPUT_EVENT_INJECTION_PENDING = -1,
48
49 /* Injection succeeded. */
50 INPUT_EVENT_INJECTION_SUCCEEDED = 0,
51
52 /* Injection failed because the injector did not have permission to inject
53 * into the application with input focus. */
54 INPUT_EVENT_INJECTION_PERMISSION_DENIED = 1,
55
56 /* Injection failed because there were no available input targets. */
57 INPUT_EVENT_INJECTION_FAILED = 2,
58
59 /* Injection failed due to a timeout. */
60 INPUT_EVENT_INJECTION_TIMED_OUT = 3
61};
62
63/*
64 * Constants used to determine the input event injection synchronization mode.
65 */
66enum {
67 /* Injection is asynchronous and is assumed always to be successful. */
68 INPUT_EVENT_INJECTION_SYNC_NONE = 0,
69
70 /* Waits for previous events to be dispatched so that the input dispatcher can determine
71 * whether input event injection willbe permitted based on the current input focus.
72 * Does not wait for the input event to finish processing. */
73 INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT = 1,
74
75 /* Waits for the input event to be completely processed. */
76 INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISHED = 2,
77};
78
79
80/*
81 * An input target specifies how an input event is to be dispatched to a particular window
82 * including the window's input channel, control flags, a timeout, and an X / Y offset to
83 * be added to input event coordinates to compensate for the absolute position of the
84 * window area.
85 */
86struct InputTarget {
87 enum {
88 /* This flag indicates that the event is being delivered to a foreground application. */
89 FLAG_FOREGROUND = 1 << 0,
90
Michael Wrightcdcd8f22016-03-22 16:52:13 -070091 /* This flag indicates that the MotionEvent falls within the area of the target
Michael Wrightd02c5b62014-02-10 15:10:22 -080092 * obscured by another visible window above it. The motion event should be
93 * delivered with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED. */
94 FLAG_WINDOW_IS_OBSCURED = 1 << 1,
95
96 /* This flag indicates that a motion event is being split across multiple windows. */
97 FLAG_SPLIT = 1 << 2,
98
99 /* This flag indicates that the pointer coordinates dispatched to the application
100 * will be zeroed out to avoid revealing information to an application. This is
101 * used in conjunction with FLAG_DISPATCH_AS_OUTSIDE to prevent apps not sharing
102 * the same UID from watching all touches. */
103 FLAG_ZERO_COORDS = 1 << 3,
104
105 /* This flag indicates that the event should be sent as is.
106 * Should always be set unless the event is to be transmuted. */
107 FLAG_DISPATCH_AS_IS = 1 << 8,
108
109 /* This flag indicates that a MotionEvent with AMOTION_EVENT_ACTION_DOWN falls outside
110 * of the area of this target and so should instead be delivered as an
111 * AMOTION_EVENT_ACTION_OUTSIDE to this target. */
112 FLAG_DISPATCH_AS_OUTSIDE = 1 << 9,
113
114 /* This flag indicates that a hover sequence is starting in the given window.
115 * The event is transmuted into ACTION_HOVER_ENTER. */
116 FLAG_DISPATCH_AS_HOVER_ENTER = 1 << 10,
117
118 /* This flag indicates that a hover event happened outside of a window which handled
119 * previous hover events, signifying the end of the current hover sequence for that
120 * window.
121 * The event is transmuted into ACTION_HOVER_ENTER. */
122 FLAG_DISPATCH_AS_HOVER_EXIT = 1 << 11,
123
124 /* This flag indicates that the event should be canceled.
125 * It is used to transmute ACTION_MOVE into ACTION_CANCEL when a touch slips
126 * outside of a window. */
127 FLAG_DISPATCH_AS_SLIPPERY_EXIT = 1 << 12,
128
129 /* This flag indicates that the event should be dispatched as an initial down.
130 * It is used to transmute ACTION_MOVE into ACTION_DOWN when a touch slips
131 * into a new window. */
132 FLAG_DISPATCH_AS_SLIPPERY_ENTER = 1 << 13,
133
134 /* Mask for all dispatch modes. */
135 FLAG_DISPATCH_MASK = FLAG_DISPATCH_AS_IS
136 | FLAG_DISPATCH_AS_OUTSIDE
137 | FLAG_DISPATCH_AS_HOVER_ENTER
138 | FLAG_DISPATCH_AS_HOVER_EXIT
139 | FLAG_DISPATCH_AS_SLIPPERY_EXIT
140 | 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)
161 float scaleFactor;
162
163 // The subset of pointer ids to include in motion events dispatched to this input target
164 // if FLAG_SPLIT is set.
165 BitSet32 pointerIds;
166};
167
168
169/*
170 * Input dispatcher configuration.
171 *
172 * Specifies various options that modify the behavior of the input dispatcher.
173 * The values provided here are merely defaults. The actual values will come from ViewConfiguration
174 * and are passed into the dispatcher during initialization.
175 */
176struct InputDispatcherConfiguration {
177 // The key repeat initial timeout.
178 nsecs_t keyRepeatTimeout;
179
180 // The key repeat inter-key delay.
181 nsecs_t keyRepeatDelay;
182
183 InputDispatcherConfiguration() :
184 keyRepeatTimeout(500 * 1000000LL),
185 keyRepeatDelay(50 * 1000000LL) { }
186};
187
188
189/*
190 * Input dispatcher policy interface.
191 *
192 * The input reader policy is used by the input reader to interact with the Window Manager
193 * and other system components.
194 *
195 * The actual implementation is partially supported by callbacks into the DVM
196 * via JNI. This interface is also mocked in the unit tests.
197 */
198class InputDispatcherPolicyInterface : public virtual RefBase {
199protected:
200 InputDispatcherPolicyInterface() { }
201 virtual ~InputDispatcherPolicyInterface() { }
202
203public:
204 /* Notifies the system that a configuration change has occurred. */
205 virtual void notifyConfigurationChanged(nsecs_t when) = 0;
206
207 /* Notifies the system that an application is not responding.
208 * Returns a new timeout to continue waiting, or 0 to abort dispatch. */
209 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
210 const sp<InputWindowHandle>& inputWindowHandle,
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800211 const std::string& reason) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800212
213 /* Notifies the system that an input channel is unrecoverably broken. */
214 virtual void notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle) = 0;
215
216 /* Gets the input dispatcher configuration. */
217 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) = 0;
218
Michael Wrightd02c5b62014-02-10 15:10:22 -0800219 /* Filters an input event.
220 * Return true to dispatch the event unmodified, false to consume the event.
221 * A filter can also transform and inject events later by passing POLICY_FLAG_FILTERED
222 * to injectInputEvent.
223 */
224 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) = 0;
225
226 /* Intercepts a key event immediately before queueing it.
227 * The policy can use this method as an opportunity to perform power management functions
228 * and early event preprocessing such as updating policy flags.
229 *
230 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
231 * should be dispatched to applications.
232 */
233 virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags) = 0;
234
235 /* Intercepts a touch, trackball or other motion event before queueing it.
236 * The policy can use this method as an opportunity to perform power management functions
237 * and early event preprocessing such as updating policy flags.
238 *
239 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
240 * should be dispatched to applications.
241 */
242 virtual void interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) = 0;
243
244 /* Allows the policy a chance to intercept a key before dispatching. */
245 virtual nsecs_t interceptKeyBeforeDispatching(const sp<InputWindowHandle>& inputWindowHandle,
246 const KeyEvent* keyEvent, uint32_t policyFlags) = 0;
247
248 /* Allows the policy a chance to perform default processing for an unhandled key.
249 * Returns an alternate keycode to redispatch as a fallback, or 0 to give up. */
250 virtual bool dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
251 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) = 0;
252
253 /* Notifies the policy about switch events.
254 */
255 virtual void notifySwitch(nsecs_t when,
256 uint32_t switchValues, uint32_t switchMask, uint32_t policyFlags) = 0;
257
258 /* Poke user activity for an event dispatched to a window. */
259 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType) = 0;
260
261 /* Checks whether a given application pid/uid has permission to inject input events
262 * into other applications.
263 *
264 * This method is special in that its implementation promises to be non-reentrant and
265 * is safe to call while holding other locks. (Most other methods make no such guarantees!)
266 */
267 virtual bool checkInjectEventsPermissionNonReentrant(
268 int32_t injectorPid, int32_t injectorUid) = 0;
269};
270
271
272/* Notifies the system about input events generated by the input reader.
273 * The dispatcher is expected to be mostly asynchronous. */
274class InputDispatcherInterface : public virtual RefBase, public InputListenerInterface {
275protected:
276 InputDispatcherInterface() { }
277 virtual ~InputDispatcherInterface() { }
278
279public:
280 /* Dumps the state of the input dispatcher.
281 *
282 * This method may be called on any thread (usually by the input manager). */
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800283 virtual void dump(std::string& dump) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800284
285 /* Called by the heatbeat to ensures that the dispatcher has not deadlocked. */
286 virtual void monitor() = 0;
287
288 /* Runs a single iteration of the dispatch loop.
289 * Nominally processes one queued event, a timeout, or a response from an input consumer.
290 *
291 * This method should only be called on the input dispatcher thread.
292 */
293 virtual void dispatchOnce() = 0;
294
295 /* Injects an input event and optionally waits for sync.
296 * The synchronization mode determines whether the method blocks while waiting for
297 * input injection to proceed.
298 * Returns one of the INPUT_EVENT_INJECTION_XXX constants.
299 *
300 * This method may be called on any thread (usually by the input manager).
301 */
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800302 virtual int32_t injectInputEvent(const InputEvent* event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800303 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,
304 uint32_t policyFlags) = 0;
305
306 /* Sets the list of input windows.
307 *
308 * This method may be called on any thread (usually by the input manager).
309 */
310 virtual void setInputWindows(const Vector<sp<InputWindowHandle> >& inputWindowHandles) = 0;
311
312 /* Sets the focused application.
313 *
314 * This method may be called on any thread (usually by the input manager).
315 */
316 virtual void setFocusedApplication(
317 const sp<InputApplicationHandle>& inputApplicationHandle) = 0;
318
319 /* Sets the input dispatching mode.
320 *
321 * This method may be called on any thread (usually by the input manager).
322 */
323 virtual void setInputDispatchMode(bool enabled, bool frozen) = 0;
324
325 /* Sets whether input event filtering is enabled.
326 * When enabled, incoming input events are sent to the policy's filterInputEvent
327 * method instead of being dispatched. The filter is expected to use
328 * injectInputEvent to inject the events it would like to have dispatched.
329 * It should include POLICY_FLAG_FILTERED in the policy flags during injection.
330 */
331 virtual void setInputFilterEnabled(bool enabled) = 0;
332
333 /* Transfers touch focus from the window associated with one channel to the
334 * window associated with the other channel.
335 *
336 * Returns true on success. False if the window did not actually have touch focus.
337 */
338 virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel,
339 const sp<InputChannel>& toChannel) = 0;
340
341 /* Registers or unregister input channels that may be used as targets for input events.
342 * If monitor is true, the channel will receive a copy of all input events.
343 *
344 * These methods may be called on any thread (usually by the input manager).
345 */
346 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel,
347 const sp<InputWindowHandle>& inputWindowHandle, bool monitor) = 0;
348 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) = 0;
349};
350
351/* Dispatches events to input targets. Some functions of the input dispatcher, such as
352 * identifying input targets, are controlled by a separate policy object.
353 *
354 * IMPORTANT INVARIANT:
355 * Because the policy can potentially block or cause re-entrance into the input dispatcher,
356 * the input dispatcher never calls into the policy while holding its internal locks.
357 * The implementation is also carefully designed to recover from scenarios such as an
358 * input channel becoming unregistered while identifying input targets or processing timeouts.
359 *
360 * Methods marked 'Locked' must be called with the lock acquired.
361 *
362 * Methods marked 'LockedInterruptible' must be called with the lock acquired but
363 * may during the course of their execution release the lock, call into the policy, and
364 * then reacquire the lock. The caller is responsible for recovering gracefully.
365 *
366 * A 'LockedInterruptible' method may called a 'Locked' method, but NOT vice-versa.
367 */
368class InputDispatcher : public InputDispatcherInterface {
369protected:
370 virtual ~InputDispatcher();
371
372public:
373 explicit InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy);
374
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800375 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800376 virtual void monitor();
377
378 virtual void dispatchOnce();
379
380 virtual void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args);
381 virtual void notifyKey(const NotifyKeyArgs* args);
382 virtual void notifyMotion(const NotifyMotionArgs* args);
383 virtual void notifySwitch(const NotifySwitchArgs* args);
384 virtual void notifyDeviceReset(const NotifyDeviceResetArgs* args);
385
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800386 virtual int32_t injectInputEvent(const InputEvent* event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800387 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,
388 uint32_t policyFlags);
389
390 virtual void setInputWindows(const Vector<sp<InputWindowHandle> >& inputWindowHandles);
391 virtual void setFocusedApplication(const sp<InputApplicationHandle>& inputApplicationHandle);
392 virtual void setInputDispatchMode(bool enabled, bool frozen);
393 virtual void setInputFilterEnabled(bool enabled);
394
395 virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel,
396 const sp<InputChannel>& toChannel);
397
398 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel,
399 const sp<InputWindowHandle>& inputWindowHandle, bool monitor);
400 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel);
401
402private:
403 template <typename T>
404 struct Link {
405 T* next;
406 T* prev;
407
408 protected:
409 inline Link() : next(NULL), prev(NULL) { }
410 };
411
412 struct InjectionState {
413 mutable int32_t refCount;
414
415 int32_t injectorPid;
416 int32_t injectorUid;
417 int32_t injectionResult; // initially INPUT_EVENT_INJECTION_PENDING
418 bool injectionIsAsync; // set to true if injection is not waiting for the result
419 int32_t pendingForegroundDispatches; // the number of foreground dispatches in progress
420
421 InjectionState(int32_t injectorPid, int32_t injectorUid);
422 void release();
423
424 private:
425 ~InjectionState();
426 };
427
428 struct EventEntry : Link<EventEntry> {
429 enum {
430 TYPE_CONFIGURATION_CHANGED,
431 TYPE_DEVICE_RESET,
432 TYPE_KEY,
433 TYPE_MOTION
434 };
435
436 mutable int32_t refCount;
437 int32_t type;
438 nsecs_t eventTime;
439 uint32_t policyFlags;
440 InjectionState* injectionState;
441
442 bool dispatchInProgress; // initially false, set to true while dispatching
443
444 inline bool isInjected() const { return injectionState != NULL; }
445
446 void release();
447
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800448 virtual void appendDescription(std::string& msg) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800449
450 protected:
451 EventEntry(int32_t type, nsecs_t eventTime, uint32_t policyFlags);
452 virtual ~EventEntry();
453 void releaseInjectionState();
454 };
455
456 struct ConfigurationChangedEntry : EventEntry {
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700457 explicit ConfigurationChangedEntry(nsecs_t eventTime);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800458 virtual void appendDescription(std::string& msg) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800459
460 protected:
461 virtual ~ConfigurationChangedEntry();
462 };
463
464 struct DeviceResetEntry : EventEntry {
465 int32_t deviceId;
466
467 DeviceResetEntry(nsecs_t eventTime, int32_t deviceId);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800468 virtual void appendDescription(std::string& msg) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800469
470 protected:
471 virtual ~DeviceResetEntry();
472 };
473
474 struct KeyEntry : EventEntry {
475 int32_t deviceId;
476 uint32_t source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100477 int32_t displayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800478 int32_t action;
479 int32_t flags;
480 int32_t keyCode;
481 int32_t scanCode;
482 int32_t metaState;
483 int32_t repeatCount;
484 nsecs_t downTime;
485
486 bool syntheticRepeat; // set to true for synthetic key repeats
487
488 enum InterceptKeyResult {
489 INTERCEPT_KEY_RESULT_UNKNOWN,
490 INTERCEPT_KEY_RESULT_SKIP,
491 INTERCEPT_KEY_RESULT_CONTINUE,
492 INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER,
493 };
494 InterceptKeyResult interceptKeyResult; // set based on the interception result
495 nsecs_t interceptKeyWakeupTime; // used with INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER
496
497 KeyEntry(nsecs_t eventTime,
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100498 int32_t deviceId, uint32_t source, int32_t displayId, uint32_t policyFlags,
499 int32_t action, int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800500 int32_t repeatCount, nsecs_t downTime);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800501 virtual void appendDescription(std::string& msg) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800502 void recycle();
503
504 protected:
505 virtual ~KeyEntry();
506 };
507
508 struct MotionEntry : EventEntry {
509 nsecs_t eventTime;
510 int32_t deviceId;
511 uint32_t source;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800512 int32_t displayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800513 int32_t action;
Michael Wright7b159c92015-05-14 14:48:03 +0100514 int32_t actionButton;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800515 int32_t flags;
516 int32_t metaState;
517 int32_t buttonState;
518 int32_t edgeFlags;
519 float xPrecision;
520 float yPrecision;
521 nsecs_t downTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800522 uint32_t pointerCount;
523 PointerProperties pointerProperties[MAX_POINTERS];
524 PointerCoords pointerCoords[MAX_POINTERS];
525
526 MotionEntry(nsecs_t eventTime,
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800527 int32_t deviceId, uint32_t source, int32_t displayId, uint32_t policyFlags,
Michael Wright7b159c92015-05-14 14:48:03 +0100528 int32_t action, int32_t actionButton, int32_t flags,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800529 int32_t metaState, int32_t buttonState, int32_t edgeFlags,
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800530 float xPrecision, float yPrecision, nsecs_t downTime, uint32_t pointerCount,
Jeff Brownf086ddb2014-02-11 14:28:48 -0800531 const PointerProperties* pointerProperties, const PointerCoords* pointerCoords,
532 float xOffset, float yOffset);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800533 virtual void appendDescription(std::string& msg) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800534
535 protected:
536 virtual ~MotionEntry();
537 };
538
539 // Tracks the progress of dispatching a particular event to a particular connection.
540 struct DispatchEntry : Link<DispatchEntry> {
541 const uint32_t seq; // unique sequence number, never 0
542
543 EventEntry* eventEntry; // the event to dispatch
544 int32_t targetFlags;
545 float xOffset;
546 float yOffset;
547 float scaleFactor;
548 nsecs_t deliveryTime; // time when the event was actually delivered
549
550 // Set to the resolved action and flags when the event is enqueued.
551 int32_t resolvedAction;
552 int32_t resolvedFlags;
553
554 DispatchEntry(EventEntry* eventEntry,
555 int32_t targetFlags, float xOffset, float yOffset, float scaleFactor);
556 ~DispatchEntry();
557
558 inline bool hasForegroundTarget() const {
559 return targetFlags & InputTarget::FLAG_FOREGROUND;
560 }
561
562 inline bool isSplit() const {
563 return targetFlags & InputTarget::FLAG_SPLIT;
564 }
565
566 private:
567 static volatile int32_t sNextSeqAtomic;
568
569 static uint32_t nextSeq();
570 };
571
572 // A command entry captures state and behavior for an action to be performed in the
573 // dispatch loop after the initial processing has taken place. It is essentially
574 // a kind of continuation used to postpone sensitive policy interactions to a point
575 // in the dispatch loop where it is safe to release the lock (generally after finishing
576 // the critical parts of the dispatch cycle).
577 //
578 // The special thing about commands is that they can voluntarily release and reacquire
579 // the dispatcher lock at will. Initially when the command starts running, the
580 // dispatcher lock is held. However, if the command needs to call into the policy to
581 // do some work, it can release the lock, do the work, then reacquire the lock again
582 // before returning.
583 //
584 // This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch
585 // never calls into the policy while holding its lock.
586 //
587 // Commands are implicitly 'LockedInterruptible'.
588 struct CommandEntry;
589 typedef void (InputDispatcher::*Command)(CommandEntry* commandEntry);
590
591 class Connection;
592 struct CommandEntry : Link<CommandEntry> {
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700593 explicit CommandEntry(Command command);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800594 ~CommandEntry();
595
596 Command command;
597
598 // parameters for the command (usage varies by command)
599 sp<Connection> connection;
600 nsecs_t eventTime;
601 KeyEntry* keyEntry;
602 sp<InputApplicationHandle> inputApplicationHandle;
603 sp<InputWindowHandle> inputWindowHandle;
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800604 std::string reason;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800605 int32_t userActivityEventType;
606 uint32_t seq;
607 bool handled;
608 };
609
610 // Generic queue implementation.
611 template <typename T>
612 struct Queue {
613 T* head;
614 T* tail;
Jon McCaffrey65dbe972014-11-18 12:07:08 -0800615 uint32_t entryCount;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800616
Jon McCaffrey65dbe972014-11-18 12:07:08 -0800617 inline Queue() : head(NULL), tail(NULL), entryCount(0) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800618 }
619
620 inline bool isEmpty() const {
621 return !head;
622 }
623
624 inline void enqueueAtTail(T* entry) {
Jon McCaffrey65dbe972014-11-18 12:07:08 -0800625 entryCount++;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800626 entry->prev = tail;
627 if (tail) {
628 tail->next = entry;
629 } else {
630 head = entry;
631 }
632 entry->next = NULL;
633 tail = entry;
634 }
635
636 inline void enqueueAtHead(T* entry) {
Jon McCaffrey65dbe972014-11-18 12:07:08 -0800637 entryCount++;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800638 entry->next = head;
639 if (head) {
640 head->prev = entry;
641 } else {
642 tail = entry;
643 }
644 entry->prev = NULL;
645 head = entry;
646 }
647
648 inline void dequeue(T* entry) {
Jon McCaffrey65dbe972014-11-18 12:07:08 -0800649 entryCount--;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800650 if (entry->prev) {
651 entry->prev->next = entry->next;
652 } else {
653 head = entry->next;
654 }
655 if (entry->next) {
656 entry->next->prev = entry->prev;
657 } else {
658 tail = entry->prev;
659 }
660 }
661
662 inline T* dequeueAtHead() {
Jon McCaffrey65dbe972014-11-18 12:07:08 -0800663 entryCount--;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800664 T* entry = head;
665 head = entry->next;
666 if (head) {
667 head->prev = NULL;
668 } else {
669 tail = NULL;
670 }
671 return entry;
672 }
673
Jon McCaffrey65dbe972014-11-18 12:07:08 -0800674 uint32_t count() const {
675 return entryCount;
676 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800677 };
678
679 /* Specifies which events are to be canceled and why. */
680 struct CancelationOptions {
681 enum Mode {
682 CANCEL_ALL_EVENTS = 0,
683 CANCEL_POINTER_EVENTS = 1,
684 CANCEL_NON_POINTER_EVENTS = 2,
685 CANCEL_FALLBACK_EVENTS = 3,
686 };
687
688 // The criterion to use to determine which events should be canceled.
689 Mode mode;
690
691 // Descriptive reason for the cancelation.
692 const char* reason;
693
694 // The specific keycode of the key event to cancel, or -1 to cancel any key event.
695 int32_t keyCode;
696
697 // The specific device id of events to cancel, or -1 to cancel events from any device.
698 int32_t deviceId;
699
700 CancelationOptions(Mode mode, const char* reason) :
701 mode(mode), reason(reason), keyCode(-1), deviceId(-1) { }
702 };
703
704 /* Tracks dispatched key and motion event state so that cancelation events can be
705 * synthesized when events are dropped. */
706 class InputState {
707 public:
708 InputState();
709 ~InputState();
710
711 // Returns true if there is no state to be canceled.
712 bool isNeutral() const;
713
714 // Returns true if the specified source is known to have received a hover enter
715 // motion event.
716 bool isHovering(int32_t deviceId, uint32_t source, int32_t displayId) const;
717
718 // Records tracking information for a key event that has just been published.
719 // Returns true if the event should be delivered, false if it is inconsistent
720 // and should be skipped.
721 bool trackKey(const KeyEntry* entry, int32_t action, int32_t flags);
722
723 // Records tracking information for a motion event that has just been published.
724 // Returns true if the event should be delivered, false if it is inconsistent
725 // and should be skipped.
726 bool trackMotion(const MotionEntry* entry, int32_t action, int32_t flags);
727
728 // Synthesizes cancelation events for the current state and resets the tracked state.
729 void synthesizeCancelationEvents(nsecs_t currentTime,
730 Vector<EventEntry*>& outEvents, const CancelationOptions& options);
731
732 // Clears the current state.
733 void clear();
734
735 // Copies pointer-related parts of the input state to another instance.
736 void copyPointerStateTo(InputState& other) const;
737
738 // Gets the fallback key associated with a keycode.
739 // Returns -1 if none.
740 // Returns AKEYCODE_UNKNOWN if we are only dispatching the unhandled key to the policy.
741 int32_t getFallbackKey(int32_t originalKeyCode);
742
743 // Sets the fallback key for a particular keycode.
744 void setFallbackKey(int32_t originalKeyCode, int32_t fallbackKeyCode);
745
746 // Removes the fallback key for a particular keycode.
747 void removeFallbackKey(int32_t originalKeyCode);
748
749 inline const KeyedVector<int32_t, int32_t>& getFallbackKeys() const {
750 return mFallbackKeys;
751 }
752
753 private:
754 struct KeyMemento {
755 int32_t deviceId;
756 uint32_t source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100757 int32_t displayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800758 int32_t keyCode;
759 int32_t scanCode;
760 int32_t metaState;
761 int32_t flags;
762 nsecs_t downTime;
763 uint32_t policyFlags;
764 };
765
766 struct MotionMemento {
767 int32_t deviceId;
768 uint32_t source;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800769 int32_t displayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800770 int32_t flags;
771 float xPrecision;
772 float yPrecision;
773 nsecs_t downTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800774 uint32_t pointerCount;
775 PointerProperties pointerProperties[MAX_POINTERS];
776 PointerCoords pointerCoords[MAX_POINTERS];
777 bool hovering;
778 uint32_t policyFlags;
779
780 void setPointers(const MotionEntry* entry);
781 };
782
783 Vector<KeyMemento> mKeyMementos;
784 Vector<MotionMemento> mMotionMementos;
785 KeyedVector<int32_t, int32_t> mFallbackKeys;
786
787 ssize_t findKeyMemento(const KeyEntry* entry) const;
788 ssize_t findMotionMemento(const MotionEntry* entry, bool hovering) const;
789
790 void addKeyMemento(const KeyEntry* entry, int32_t flags);
791 void addMotionMemento(const MotionEntry* entry, int32_t flags, bool hovering);
792
793 static bool shouldCancelKey(const KeyMemento& memento,
794 const CancelationOptions& options);
795 static bool shouldCancelMotion(const MotionMemento& memento,
796 const CancelationOptions& options);
797 };
798
799 /* Manages the dispatch state associated with a single input channel. */
800 class Connection : public RefBase {
801 protected:
802 virtual ~Connection();
803
804 public:
805 enum Status {
806 // Everything is peachy.
807 STATUS_NORMAL,
808 // An unrecoverable communication error has occurred.
809 STATUS_BROKEN,
810 // The input channel has been unregistered.
811 STATUS_ZOMBIE
812 };
813
814 Status status;
815 sp<InputChannel> inputChannel; // never null
816 sp<InputWindowHandle> inputWindowHandle; // may be null
817 bool monitor;
818 InputPublisher inputPublisher;
819 InputState inputState;
820
821 // True if the socket is full and no further events can be published until
822 // the application consumes some of the input.
823 bool inputPublisherBlocked;
824
825 // Queue of events that need to be published to the connection.
826 Queue<DispatchEntry> outboundQueue;
827
828 // Queue of events that have been published to the connection but that have not
829 // yet received a "finished" response from the application.
830 Queue<DispatchEntry> waitQueue;
831
832 explicit Connection(const sp<InputChannel>& inputChannel,
833 const sp<InputWindowHandle>& inputWindowHandle, bool monitor);
834
Siarhei Vishniakou587c3f02018-01-04 11:46:44 -0800835 inline const std::string getInputChannelName() const { return inputChannel->getName(); }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800836
Siarhei Vishniakou587c3f02018-01-04 11:46:44 -0800837 const std::string getWindowName() const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800838 const char* getStatusLabel() const;
839
840 DispatchEntry* findWaitQueueEntry(uint32_t seq);
841 };
842
843 enum DropReason {
844 DROP_REASON_NOT_DROPPED = 0,
845 DROP_REASON_POLICY = 1,
846 DROP_REASON_APP_SWITCH = 2,
847 DROP_REASON_DISABLED = 3,
848 DROP_REASON_BLOCKED = 4,
849 DROP_REASON_STALE = 5,
850 };
851
852 sp<InputDispatcherPolicyInterface> mPolicy;
853 InputDispatcherConfiguration mConfig;
854
855 Mutex mLock;
856
857 Condition mDispatcherIsAliveCondition;
858
859 sp<Looper> mLooper;
860
861 EventEntry* mPendingEvent;
862 Queue<EventEntry> mInboundQueue;
863 Queue<EventEntry> mRecentQueue;
864 Queue<CommandEntry> mCommandQueue;
865
Michael Wright3a981722015-06-10 15:26:13 +0100866 DropReason mLastDropReason;
867
Michael Wrightd02c5b62014-02-10 15:10:22 -0800868 void dispatchOnceInnerLocked(nsecs_t* nextWakeupTime);
869
870 // Enqueues an inbound event. Returns true if mLooper->wake() should be called.
871 bool enqueueInboundEventLocked(EventEntry* entry);
872
873 // Cleans up input state when dropping an inbound event.
874 void dropInboundEventLocked(EventEntry* entry, DropReason dropReason);
875
876 // Adds an event to a queue of recent events for debugging purposes.
877 void addRecentEventLocked(EventEntry* entry);
878
879 // App switch latency optimization.
880 bool mAppSwitchSawKeyDown;
881 nsecs_t mAppSwitchDueTime;
882
883 static bool isAppSwitchKeyCode(int32_t keyCode);
884 bool isAppSwitchKeyEventLocked(KeyEntry* keyEntry);
885 bool isAppSwitchPendingLocked();
886 void resetPendingAppSwitchLocked(bool handled);
887
888 // Stale event latency optimization.
889 static bool isStaleEventLocked(nsecs_t currentTime, EventEntry* entry);
890
891 // Blocked event latency optimization. Drops old events when the user intends
892 // to transfer focus to a new application.
893 EventEntry* mNextUnblockedEvent;
894
895 sp<InputWindowHandle> findTouchedWindowAtLocked(int32_t displayId, int32_t x, int32_t y);
896
897 // All registered connections mapped by channel file descriptor.
898 KeyedVector<int, sp<Connection> > mConnectionsByFd;
899
900 ssize_t getConnectionIndexLocked(const sp<InputChannel>& inputChannel);
901
902 // Input channels that will receive a copy of all input events.
903 Vector<sp<InputChannel> > mMonitoringChannels;
904
905 // Event injection and synchronization.
906 Condition mInjectionResultAvailableCondition;
907 bool hasInjectionPermission(int32_t injectorPid, int32_t injectorUid);
908 void setInjectionResultLocked(EventEntry* entry, int32_t injectionResult);
909
910 Condition mInjectionSyncFinishedCondition;
911 void incrementPendingForegroundDispatchesLocked(EventEntry* entry);
912 void decrementPendingForegroundDispatchesLocked(EventEntry* entry);
913
914 // Key repeat tracking.
915 struct KeyRepeatState {
916 KeyEntry* lastKeyEntry; // or null if no repeat
917 nsecs_t nextRepeatTime;
918 } mKeyRepeatState;
919
920 void resetKeyRepeatLocked();
921 KeyEntry* synthesizeKeyRepeatLocked(nsecs_t currentTime);
922
Michael Wright78f24442014-08-06 15:55:28 -0700923 // Key replacement tracking
924 struct KeyReplacement {
925 int32_t keyCode;
926 int32_t deviceId;
927 bool operator==(const KeyReplacement& rhs) const {
928 return keyCode == rhs.keyCode && deviceId == rhs.deviceId;
929 }
930 bool operator<(const KeyReplacement& rhs) const {
931 return keyCode != rhs.keyCode ? keyCode < rhs.keyCode : deviceId < rhs.deviceId;
932 }
933 };
934 // Maps the key code replaced, device id tuple to the key code it was replaced with
935 KeyedVector<KeyReplacement, int32_t> mReplacedKeys;
Siarhei Vishniakou61fafdd2018-04-13 11:00:58 -0500936 // Process certain Meta + Key combinations
937 void accelerateMetaShortcuts(const int32_t deviceId, const int32_t action,
938 int32_t& keyCode, int32_t& metaState);
Michael Wright78f24442014-08-06 15:55:28 -0700939
Michael Wrightd02c5b62014-02-10 15:10:22 -0800940 // Deferred command processing.
941 bool haveCommandsLocked() const;
942 bool runCommandsLockedInterruptible();
943 CommandEntry* postCommandLocked(Command command);
944
945 // Input filter processing.
946 bool shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args);
947 bool shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args);
948
949 // Inbound event processing.
950 void drainInboundQueueLocked();
951 void releasePendingEventLocked();
952 void releaseInboundEventLocked(EventEntry* entry);
953
954 // Dispatch state.
955 bool mDispatchEnabled;
956 bool mDispatchFrozen;
957 bool mInputFilterEnabled;
958
959 Vector<sp<InputWindowHandle> > mWindowHandles;
960
961 sp<InputWindowHandle> getWindowHandleLocked(const sp<InputChannel>& inputChannel) const;
962 bool hasWindowHandleLocked(const sp<InputWindowHandle>& windowHandle) const;
963
964 // Focus tracking for keys, trackball, etc.
965 sp<InputWindowHandle> mFocusedWindowHandle;
966
967 // Focus tracking for touch.
968 struct TouchedWindow {
969 sp<InputWindowHandle> windowHandle;
970 int32_t targetFlags;
971 BitSet32 pointerIds; // zero unless target flag FLAG_SPLIT is set
972 };
973 struct TouchState {
974 bool down;
975 bool split;
976 int32_t deviceId; // id of the device that is currently down, others are rejected
977 uint32_t source; // source of the device that is current down, others are rejected
978 int32_t displayId; // id to the display that currently has a touch, others are rejected
979 Vector<TouchedWindow> windows;
980
981 TouchState();
982 ~TouchState();
983 void reset();
984 void copyFrom(const TouchState& other);
985 void addOrUpdateWindow(const sp<InputWindowHandle>& windowHandle,
986 int32_t targetFlags, BitSet32 pointerIds);
987 void removeWindow(const sp<InputWindowHandle>& windowHandle);
988 void filterNonAsIsTouchWindows();
989 sp<InputWindowHandle> getFirstForegroundWindowHandle() const;
990 bool isSlippery() const;
991 };
992
Jeff Brownf086ddb2014-02-11 14:28:48 -0800993 KeyedVector<int32_t, TouchState> mTouchStatesByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800994 TouchState mTempTouchState;
995
996 // Focused application.
997 sp<InputApplicationHandle> mFocusedApplicationHandle;
998
999 // Dispatcher state at time of last ANR.
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001000 std::string mLastANRState;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001001
1002 // Dispatch inbound events.
1003 bool dispatchConfigurationChangedLocked(
1004 nsecs_t currentTime, ConfigurationChangedEntry* entry);
1005 bool dispatchDeviceResetLocked(
1006 nsecs_t currentTime, DeviceResetEntry* entry);
1007 bool dispatchKeyLocked(
1008 nsecs_t currentTime, KeyEntry* entry,
1009 DropReason* dropReason, nsecs_t* nextWakeupTime);
1010 bool dispatchMotionLocked(
1011 nsecs_t currentTime, MotionEntry* entry,
1012 DropReason* dropReason, nsecs_t* nextWakeupTime);
1013 void dispatchEventLocked(nsecs_t currentTime, EventEntry* entry,
1014 const Vector<InputTarget>& inputTargets);
1015
1016 void logOutboundKeyDetailsLocked(const char* prefix, const KeyEntry* entry);
1017 void logOutboundMotionDetailsLocked(const char* prefix, const MotionEntry* entry);
1018
1019 // Keeping track of ANR timeouts.
1020 enum InputTargetWaitCause {
1021 INPUT_TARGET_WAIT_CAUSE_NONE,
1022 INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY,
1023 INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY,
1024 };
1025
1026 InputTargetWaitCause mInputTargetWaitCause;
1027 nsecs_t mInputTargetWaitStartTime;
1028 nsecs_t mInputTargetWaitTimeoutTime;
1029 bool mInputTargetWaitTimeoutExpired;
1030 sp<InputApplicationHandle> mInputTargetWaitApplicationHandle;
1031
1032 // Contains the last window which received a hover event.
1033 sp<InputWindowHandle> mLastHoverWindowHandle;
1034
1035 // Finding targets for input events.
1036 int32_t handleTargetsNotReadyLocked(nsecs_t currentTime, const EventEntry* entry,
1037 const sp<InputApplicationHandle>& applicationHandle,
1038 const sp<InputWindowHandle>& windowHandle,
1039 nsecs_t* nextWakeupTime, const char* reason);
1040 void resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout,
1041 const sp<InputChannel>& inputChannel);
1042 nsecs_t getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime);
1043 void resetANRTimeoutsLocked();
1044
1045 int32_t findFocusedWindowTargetsLocked(nsecs_t currentTime, const EventEntry* entry,
1046 Vector<InputTarget>& inputTargets, nsecs_t* nextWakeupTime);
1047 int32_t findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry* entry,
1048 Vector<InputTarget>& inputTargets, nsecs_t* nextWakeupTime,
1049 bool* outConflictingPointerActions);
1050
1051 void addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle,
1052 int32_t targetFlags, BitSet32 pointerIds, Vector<InputTarget>& inputTargets);
1053 void addMonitoringTargetsLocked(Vector<InputTarget>& inputTargets);
1054
1055 void pokeUserActivityLocked(const EventEntry* eventEntry);
1056 bool checkInjectionPermission(const sp<InputWindowHandle>& windowHandle,
1057 const InjectionState* injectionState);
1058 bool isWindowObscuredAtPointLocked(const sp<InputWindowHandle>& windowHandle,
1059 int32_t x, int32_t y) const;
Michael Wrightcdcd8f22016-03-22 16:52:13 -07001060 bool isWindowObscuredLocked(const sp<InputWindowHandle>& windowHandle) const;
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001061 std::string getApplicationWindowLabelLocked(const sp<InputApplicationHandle>& applicationHandle,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001062 const sp<InputWindowHandle>& windowHandle);
1063
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001064 std::string checkWindowReadyForMoreInputLocked(nsecs_t currentTime,
Jeff Brownffb49772014-10-10 19:01:34 -07001065 const sp<InputWindowHandle>& windowHandle, const EventEntry* eventEntry,
1066 const char* targetType);
1067
Michael Wrightd02c5b62014-02-10 15:10:22 -08001068 // Manage the dispatch cycle for a single connection.
1069 // These methods are deliberately not Interruptible because doing all of the work
1070 // with the mutex held makes it easier to ensure that connection invariants are maintained.
1071 // If needed, the methods post commands to run later once the critical bits are done.
1072 void prepareDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
1073 EventEntry* eventEntry, const InputTarget* inputTarget);
1074 void enqueueDispatchEntriesLocked(nsecs_t currentTime, const sp<Connection>& connection,
1075 EventEntry* eventEntry, const InputTarget* inputTarget);
1076 void enqueueDispatchEntryLocked(const sp<Connection>& connection,
1077 EventEntry* eventEntry, const InputTarget* inputTarget, int32_t dispatchMode);
1078 void startDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
1079 void finishDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
1080 uint32_t seq, bool handled);
1081 void abortBrokenDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
1082 bool notify);
1083 void drainDispatchQueueLocked(Queue<DispatchEntry>* queue);
1084 void releaseDispatchEntryLocked(DispatchEntry* dispatchEntry);
1085 static int handleReceiveCallback(int fd, int events, void* data);
1086
1087 void synthesizeCancelationEventsForAllConnectionsLocked(
1088 const CancelationOptions& options);
Michael Wrightfa13dcf2015-06-12 13:25:11 +01001089 void synthesizeCancelationEventsForMonitorsLocked(const CancelationOptions& options);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001090 void synthesizeCancelationEventsForInputChannelLocked(const sp<InputChannel>& channel,
1091 const CancelationOptions& options);
1092 void synthesizeCancelationEventsForConnectionLocked(const sp<Connection>& connection,
1093 const CancelationOptions& options);
1094
1095 // Splitting motion events across windows.
1096 MotionEntry* splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds);
1097
1098 // Reset and drop everything the dispatcher is doing.
1099 void resetAndDropEverythingLocked(const char* reason);
1100
1101 // Dump state.
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001102 void dumpDispatchStateLocked(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001103 void logDispatchStateLocked();
1104
1105 // Registration.
1106 void removeMonitorChannelLocked(const sp<InputChannel>& inputChannel);
1107 status_t unregisterInputChannelLocked(const sp<InputChannel>& inputChannel, bool notify);
1108
1109 // Add or remove a connection to the mActiveConnections vector.
1110 void activateConnectionLocked(Connection* connection);
1111 void deactivateConnectionLocked(Connection* connection);
1112
1113 // Interesting events that we might like to log or tell the framework about.
1114 void onDispatchCycleFinishedLocked(
1115 nsecs_t currentTime, const sp<Connection>& connection, uint32_t seq, bool handled);
1116 void onDispatchCycleBrokenLocked(
1117 nsecs_t currentTime, const sp<Connection>& connection);
1118 void onANRLocked(
1119 nsecs_t currentTime, const sp<InputApplicationHandle>& applicationHandle,
1120 const sp<InputWindowHandle>& windowHandle,
1121 nsecs_t eventTime, nsecs_t waitStartTime, const char* reason);
1122
1123 // Outbound policy interactions.
1124 void doNotifyConfigurationChangedInterruptible(CommandEntry* commandEntry);
1125 void doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry);
1126 void doNotifyANRLockedInterruptible(CommandEntry* commandEntry);
1127 void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry);
1128 void doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry);
1129 bool afterKeyEventLockedInterruptible(const sp<Connection>& connection,
1130 DispatchEntry* dispatchEntry, KeyEntry* keyEntry, bool handled);
1131 bool afterMotionEventLockedInterruptible(const sp<Connection>& connection,
1132 DispatchEntry* dispatchEntry, MotionEntry* motionEntry, bool handled);
1133 void doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry);
1134 void initializeKeyEvent(KeyEvent* event, const KeyEntry* entry);
1135
1136 // Statistics gathering.
1137 void updateDispatchStatisticsLocked(nsecs_t currentTime, const EventEntry* entry,
1138 int32_t injectionResult, nsecs_t timeSpentWaitingForApplication);
1139 void traceInboundQueueLengthLocked();
1140 void traceOutboundQueueLengthLocked(const sp<Connection>& connection);
1141 void traceWaitQueueLengthLocked(const sp<Connection>& connection);
1142};
1143
1144/* Enqueues and dispatches input events, endlessly. */
1145class InputDispatcherThread : public Thread {
1146public:
1147 explicit InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher);
1148 ~InputDispatcherThread();
1149
1150private:
1151 virtual bool threadLoop();
1152
1153 sp<InputDispatcherInterface> mDispatcher;
1154};
1155
1156} // namespace android
1157
1158#endif // _UI_INPUT_DISPATCHER_H