blob: 9c5a00ac87103acebfc3bf2e7f7a173cd8af0286 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _UI_INPUT_DISPATCHER_H
18#define _UI_INPUT_DISPATCHER_H
19
Siarhei Vishniakou443ad902019-03-06 17:25:41 -080020#include <condition_variable>
Michael Wrightd02c5b62014-02-10 15:10:22 -080021#include <input/Input.h>
Robert Carr3720ed02018-08-08 16:08:27 -070022#include <input/InputApplication.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080023#include <input/InputTransport.h>
Robert Carr3720ed02018-08-08 16:08:27 -070024#include <input/InputWindow.h>
chaviw291d88a2019-02-14 10:33:58 -080025#include <input/ISetInputWindowsListener.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080026#include <utils/KeyedVector.h>
27#include <utils/Vector.h>
28#include <utils/threads.h>
29#include <utils/Timers.h>
30#include <utils/RefBase.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080031#include <utils/Looper.h>
32#include <utils/BitSet.h>
33#include <cutils/atomic.h>
Robert Carr5c8a0262018-10-03 16:30:44 -070034#include <unordered_map>
Michael Wrightd02c5b62014-02-10 15:10:22 -080035
36#include <stddef.h>
37#include <unistd.h>
38#include <limits.h>
Arthur Hungb92218b2018-08-14 12:00:21 +080039#include <unordered_map>
Michael Wrightd02c5b62014-02-10 15:10:22 -080040
Michael Wrightd02c5b62014-02-10 15:10:22 -080041#include "InputListener.h"
Prabir Pradhan79a4f0c2019-01-09 11:24:01 -080042#include "InputReporterInterface.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080043
Michael Wrightd02c5b62014-02-10 15:10:22 -080044namespace android {
45
46/*
47 * Constants used to report the outcome of input event injection.
48 */
49enum {
50 /* (INTERNAL USE ONLY) Specifies that injection is pending and its outcome is unknown. */
51 INPUT_EVENT_INJECTION_PENDING = -1,
52
53 /* Injection succeeded. */
54 INPUT_EVENT_INJECTION_SUCCEEDED = 0,
55
56 /* Injection failed because the injector did not have permission to inject
57 * into the application with input focus. */
58 INPUT_EVENT_INJECTION_PERMISSION_DENIED = 1,
59
60 /* Injection failed because there were no available input targets. */
61 INPUT_EVENT_INJECTION_FAILED = 2,
62
63 /* Injection failed due to a timeout. */
64 INPUT_EVENT_INJECTION_TIMED_OUT = 3
65};
66
67/*
68 * Constants used to determine the input event injection synchronization mode.
69 */
70enum {
71 /* Injection is asynchronous and is assumed always to be successful. */
72 INPUT_EVENT_INJECTION_SYNC_NONE = 0,
73
74 /* Waits for previous events to be dispatched so that the input dispatcher can determine
75 * whether input event injection willbe permitted based on the current input focus.
76 * Does not wait for the input event to finish processing. */
77 INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT = 1,
78
79 /* Waits for the input event to be completely processed. */
80 INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISHED = 2,
81};
82
83
84/*
85 * An input target specifies how an input event is to be dispatched to a particular window
86 * including the window's input channel, control flags, a timeout, and an X / Y offset to
87 * be added to input event coordinates to compensate for the absolute position of the
88 * window area.
89 */
90struct InputTarget {
91 enum {
92 /* This flag indicates that the event is being delivered to a foreground application. */
93 FLAG_FOREGROUND = 1 << 0,
94
Michael Wrightcdcd8f22016-03-22 16:52:13 -070095 /* This flag indicates that the MotionEvent falls within the area of the target
Michael Wrightd02c5b62014-02-10 15:10:22 -080096 * obscured by another visible window above it. The motion event should be
97 * delivered with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED. */
98 FLAG_WINDOW_IS_OBSCURED = 1 << 1,
99
100 /* This flag indicates that a motion event is being split across multiple windows. */
101 FLAG_SPLIT = 1 << 2,
102
103 /* This flag indicates that the pointer coordinates dispatched to the application
104 * will be zeroed out to avoid revealing information to an application. This is
105 * used in conjunction with FLAG_DISPATCH_AS_OUTSIDE to prevent apps not sharing
106 * the same UID from watching all touches. */
107 FLAG_ZERO_COORDS = 1 << 3,
108
109 /* This flag indicates that the event should be sent as is.
110 * Should always be set unless the event is to be transmuted. */
111 FLAG_DISPATCH_AS_IS = 1 << 8,
112
113 /* This flag indicates that a MotionEvent with AMOTION_EVENT_ACTION_DOWN falls outside
114 * of the area of this target and so should instead be delivered as an
115 * AMOTION_EVENT_ACTION_OUTSIDE to this target. */
116 FLAG_DISPATCH_AS_OUTSIDE = 1 << 9,
117
118 /* This flag indicates that a hover sequence is starting in the given window.
119 * The event is transmuted into ACTION_HOVER_ENTER. */
120 FLAG_DISPATCH_AS_HOVER_ENTER = 1 << 10,
121
122 /* This flag indicates that a hover event happened outside of a window which handled
123 * previous hover events, signifying the end of the current hover sequence for that
124 * window.
125 * The event is transmuted into ACTION_HOVER_ENTER. */
126 FLAG_DISPATCH_AS_HOVER_EXIT = 1 << 11,
127
128 /* This flag indicates that the event should be canceled.
129 * It is used to transmute ACTION_MOVE into ACTION_CANCEL when a touch slips
130 * outside of a window. */
131 FLAG_DISPATCH_AS_SLIPPERY_EXIT = 1 << 12,
132
133 /* This flag indicates that the event should be dispatched as an initial down.
134 * It is used to transmute ACTION_MOVE into ACTION_DOWN when a touch slips
135 * into a new window. */
136 FLAG_DISPATCH_AS_SLIPPERY_ENTER = 1 << 13,
137
138 /* Mask for all dispatch modes. */
139 FLAG_DISPATCH_MASK = FLAG_DISPATCH_AS_IS
140 | FLAG_DISPATCH_AS_OUTSIDE
141 | FLAG_DISPATCH_AS_HOVER_ENTER
142 | FLAG_DISPATCH_AS_HOVER_EXIT
143 | FLAG_DISPATCH_AS_SLIPPERY_EXIT
144 | FLAG_DISPATCH_AS_SLIPPERY_ENTER,
Michael Wrightcdcd8f22016-03-22 16:52:13 -0700145
146 /* This flag indicates that the target of a MotionEvent is partly or wholly
147 * obscured by another visible window above it. The motion event should be
148 * delivered with flag AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED. */
149 FLAG_WINDOW_IS_PARTIALLY_OBSCURED = 1 << 14,
150
Michael Wrightd02c5b62014-02-10 15:10:22 -0800151 };
152
153 // The input channel to be targeted.
154 sp<InputChannel> inputChannel;
155
156 // Flags for the input target.
157 int32_t flags;
158
159 // The x and y offset to add to a MotionEvent as it is delivered.
160 // (ignored for KeyEvents)
161 float xOffset, yOffset;
162
163 // Scaling factor to apply to MotionEvent as it is delivered.
164 // (ignored for KeyEvents)
Robert Carre07e1032018-11-26 12:55:53 -0800165 float globalScaleFactor;
166 float windowXScale = 1.0f;
167 float windowYScale = 1.0f;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800168
169 // The subset of pointer ids to include in motion events dispatched to this input target
170 // if FLAG_SPLIT is set.
171 BitSet32 pointerIds;
172};
173
174
175/*
176 * Input dispatcher configuration.
177 *
178 * Specifies various options that modify the behavior of the input dispatcher.
179 * The values provided here are merely defaults. The actual values will come from ViewConfiguration
180 * and are passed into the dispatcher during initialization.
181 */
182struct InputDispatcherConfiguration {
183 // The key repeat initial timeout.
184 nsecs_t keyRepeatTimeout;
185
186 // The key repeat inter-key delay.
187 nsecs_t keyRepeatDelay;
188
189 InputDispatcherConfiguration() :
190 keyRepeatTimeout(500 * 1000000LL),
191 keyRepeatDelay(50 * 1000000LL) { }
192};
193
194
195/*
196 * Input dispatcher policy interface.
197 *
198 * The input reader policy is used by the input reader to interact with the Window Manager
199 * and other system components.
200 *
201 * The actual implementation is partially supported by callbacks into the DVM
202 * via JNI. This interface is also mocked in the unit tests.
203 */
204class InputDispatcherPolicyInterface : public virtual RefBase {
205protected:
206 InputDispatcherPolicyInterface() { }
207 virtual ~InputDispatcherPolicyInterface() { }
208
209public:
210 /* Notifies the system that a configuration change has occurred. */
211 virtual void notifyConfigurationChanged(nsecs_t when) = 0;
212
213 /* Notifies the system that an application is not responding.
214 * Returns a new timeout to continue waiting, or 0 to abort dispatch. */
215 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Robert Carr803535b2018-08-02 16:38:15 -0700216 const sp<IBinder>& token,
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800217 const std::string& reason) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800218
219 /* Notifies the system that an input channel is unrecoverably broken. */
Robert Carr803535b2018-08-02 16:38:15 -0700220 virtual void notifyInputChannelBroken(const sp<IBinder>& token) = 0;
chaviw0c06c6e2019-01-09 13:27:07 -0800221 virtual void notifyFocusChanged(const sp<IBinder>& oldToken, const sp<IBinder>& newToken) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800222
223 /* Gets the input dispatcher configuration. */
224 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) = 0;
225
Michael Wrightd02c5b62014-02-10 15:10:22 -0800226 /* Filters an input event.
227 * Return true to dispatch the event unmodified, false to consume the event.
228 * A filter can also transform and inject events later by passing POLICY_FLAG_FILTERED
229 * to injectInputEvent.
230 */
231 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) = 0;
232
233 /* Intercepts a key event immediately before queueing it.
234 * The policy can use this method as an opportunity to perform power management functions
235 * and early event preprocessing such as updating policy flags.
236 *
237 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
238 * should be dispatched to applications.
239 */
240 virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags) = 0;
241
242 /* Intercepts a touch, trackball or other motion event before queueing it.
243 * The policy can use this method as an opportunity to perform power management functions
244 * and early event preprocessing such as updating policy flags.
245 *
246 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
247 * should be dispatched to applications.
248 */
Charles Chen3611f1f2019-01-29 17:26:18 +0800249 virtual void interceptMotionBeforeQueueing(const int32_t displayId, nsecs_t when,
250 uint32_t& policyFlags) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800251
252 /* Allows the policy a chance to intercept a key before dispatching. */
Robert Carr803535b2018-08-02 16:38:15 -0700253 virtual nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>& token,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800254 const KeyEvent* keyEvent, uint32_t policyFlags) = 0;
255
256 /* Allows the policy a chance to perform default processing for an unhandled key.
257 * Returns an alternate keycode to redispatch as a fallback, or 0 to give up. */
Robert Carr803535b2018-08-02 16:38:15 -0700258 virtual bool dispatchUnhandledKey(const sp<IBinder>& token,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800259 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) = 0;
260
261 /* Notifies the policy about switch events.
262 */
263 virtual void notifySwitch(nsecs_t when,
264 uint32_t switchValues, uint32_t switchMask, uint32_t policyFlags) = 0;
265
266 /* Poke user activity for an event dispatched to a window. */
267 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType) = 0;
268
269 /* Checks whether a given application pid/uid has permission to inject input events
270 * into other applications.
271 *
272 * This method is special in that its implementation promises to be non-reentrant and
273 * is safe to call while holding other locks. (Most other methods make no such guarantees!)
274 */
275 virtual bool checkInjectEventsPermissionNonReentrant(
276 int32_t injectorPid, int32_t injectorUid) = 0;
277};
278
279
280/* Notifies the system about input events generated by the input reader.
281 * The dispatcher is expected to be mostly asynchronous. */
282class InputDispatcherInterface : public virtual RefBase, public InputListenerInterface {
283protected:
284 InputDispatcherInterface() { }
285 virtual ~InputDispatcherInterface() { }
286
287public:
288 /* Dumps the state of the input dispatcher.
289 *
290 * This method may be called on any thread (usually by the input manager). */
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800291 virtual void dump(std::string& dump) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800292
293 /* Called by the heatbeat to ensures that the dispatcher has not deadlocked. */
294 virtual void monitor() = 0;
295
296 /* Runs a single iteration of the dispatch loop.
297 * Nominally processes one queued event, a timeout, or a response from an input consumer.
298 *
299 * This method should only be called on the input dispatcher thread.
300 */
301 virtual void dispatchOnce() = 0;
302
303 /* Injects an input event and optionally waits for sync.
304 * The synchronization mode determines whether the method blocks while waiting for
305 * input injection to proceed.
306 * Returns one of the INPUT_EVENT_INJECTION_XXX constants.
307 *
308 * This method may be called on any thread (usually by the input manager).
309 */
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800310 virtual int32_t injectInputEvent(const InputEvent* event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800311 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,
312 uint32_t policyFlags) = 0;
313
314 /* Sets the list of input windows.
315 *
316 * This method may be called on any thread (usually by the input manager).
317 */
Arthur Hungb92218b2018-08-14 12:00:21 +0800318 virtual void setInputWindows(const Vector<sp<InputWindowHandle> >& inputWindowHandles,
chaviw291d88a2019-02-14 10:33:58 -0800319 int32_t displayId,
320 const sp<ISetInputWindowsListener>& setInputWindowsListener = nullptr) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800321
Tiger Huang721e26f2018-07-24 22:26:19 +0800322 /* Sets the focused application on the given display.
Michael Wrightd02c5b62014-02-10 15:10:22 -0800323 *
324 * This method may be called on any thread (usually by the input manager).
325 */
326 virtual void setFocusedApplication(
Tiger Huang721e26f2018-07-24 22:26:19 +0800327 int32_t displayId, const sp<InputApplicationHandle>& inputApplicationHandle) = 0;
328
329 /* Sets the focused display.
330 *
331 * This method may be called on any thread (usually by the input manager).
332 */
333 virtual void setFocusedDisplay(int32_t displayId) = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800334
335 /* Sets the input dispatching mode.
336 *
337 * This method may be called on any thread (usually by the input manager).
338 */
339 virtual void setInputDispatchMode(bool enabled, bool frozen) = 0;
340
341 /* Sets whether input event filtering is enabled.
342 * When enabled, incoming input events are sent to the policy's filterInputEvent
343 * method instead of being dispatched. The filter is expected to use
344 * injectInputEvent to inject the events it would like to have dispatched.
345 * It should include POLICY_FLAG_FILTERED in the policy flags during injection.
346 */
347 virtual void setInputFilterEnabled(bool enabled) = 0;
348
chaviwfbe5d9c2018-12-26 12:23:37 -0800349 /* Transfers touch focus from one window to another window.
Michael Wrightd02c5b62014-02-10 15:10:22 -0800350 *
351 * Returns true on success. False if the window did not actually have touch focus.
352 */
chaviwfbe5d9c2018-12-26 12:23:37 -0800353 virtual bool transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken) = 0;
354
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800355 /* Registers input channels that may be used as targets for input events.
356 * If inputWindowHandle is null, and displayId is not ADISPLAY_ID_NONE,
357 * the channel will receive a copy of all input events form the specific displayId.
Michael Wrightd02c5b62014-02-10 15:10:22 -0800358 *
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800359 * This method may be called on any thread (usually by the input manager).
Michael Wrightd02c5b62014-02-10 15:10:22 -0800360 */
Robert Carr803535b2018-08-02 16:38:15 -0700361 virtual status_t registerInputChannel(
362 const sp<InputChannel>& inputChannel, int32_t displayId) = 0;
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800363
364 /* Unregister input channels that will no longer receive input events.
365 *
366 * This method may be called on any thread (usually by the input manager).
367 */
Michael Wrightd02c5b62014-02-10 15:10:22 -0800368 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) = 0;
369};
370
371/* Dispatches events to input targets. Some functions of the input dispatcher, such as
372 * identifying input targets, are controlled by a separate policy object.
373 *
374 * IMPORTANT INVARIANT:
375 * Because the policy can potentially block or cause re-entrance into the input dispatcher,
376 * the input dispatcher never calls into the policy while holding its internal locks.
377 * The implementation is also carefully designed to recover from scenarios such as an
378 * input channel becoming unregistered while identifying input targets or processing timeouts.
379 *
380 * Methods marked 'Locked' must be called with the lock acquired.
381 *
382 * Methods marked 'LockedInterruptible' must be called with the lock acquired but
383 * may during the course of their execution release the lock, call into the policy, and
384 * then reacquire the lock. The caller is responsible for recovering gracefully.
385 *
386 * A 'LockedInterruptible' method may called a 'Locked' method, but NOT vice-versa.
387 */
388class InputDispatcher : public InputDispatcherInterface {
389protected:
390 virtual ~InputDispatcher();
391
392public:
393 explicit InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy);
394
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800395 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800396 virtual void monitor();
397
398 virtual void dispatchOnce();
399
400 virtual void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args);
401 virtual void notifyKey(const NotifyKeyArgs* args);
402 virtual void notifyMotion(const NotifyMotionArgs* args);
403 virtual void notifySwitch(const NotifySwitchArgs* args);
404 virtual void notifyDeviceReset(const NotifyDeviceResetArgs* args);
405
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800406 virtual int32_t injectInputEvent(const InputEvent* event,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800407 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,
408 uint32_t policyFlags);
409
Arthur Hungb92218b2018-08-14 12:00:21 +0800410 virtual void setInputWindows(const Vector<sp<InputWindowHandle> >& inputWindowHandles,
chaviw291d88a2019-02-14 10:33:58 -0800411 int32_t displayId,
412 const sp<ISetInputWindowsListener>& setInputWindowsListener = nullptr);
Tiger Huang721e26f2018-07-24 22:26:19 +0800413 virtual void setFocusedApplication(int32_t displayId,
414 const sp<InputApplicationHandle>& inputApplicationHandle);
415 virtual void setFocusedDisplay(int32_t displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800416 virtual void setInputDispatchMode(bool enabled, bool frozen);
417 virtual void setInputFilterEnabled(bool enabled);
418
chaviwfbe5d9c2018-12-26 12:23:37 -0800419 virtual bool transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800420
421 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel,
Robert Carr803535b2018-08-02 16:38:15 -0700422 int32_t displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800423 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel);
424
425private:
426 template <typename T>
427 struct Link {
428 T* next;
429 T* prev;
430
431 protected:
Yi Kong9b14ac62018-07-17 13:48:38 -0700432 inline Link() : next(nullptr), prev(nullptr) { }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800433 };
434
435 struct InjectionState {
436 mutable int32_t refCount;
437
438 int32_t injectorPid;
439 int32_t injectorUid;
440 int32_t injectionResult; // initially INPUT_EVENT_INJECTION_PENDING
441 bool injectionIsAsync; // set to true if injection is not waiting for the result
442 int32_t pendingForegroundDispatches; // the number of foreground dispatches in progress
443
444 InjectionState(int32_t injectorPid, int32_t injectorUid);
445 void release();
446
447 private:
448 ~InjectionState();
449 };
450
451 struct EventEntry : Link<EventEntry> {
452 enum {
453 TYPE_CONFIGURATION_CHANGED,
454 TYPE_DEVICE_RESET,
455 TYPE_KEY,
456 TYPE_MOTION
457 };
458
Prabir Pradhan42611e02018-11-27 14:04:02 -0800459 uint32_t sequenceNum;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800460 mutable int32_t refCount;
461 int32_t type;
462 nsecs_t eventTime;
463 uint32_t policyFlags;
464 InjectionState* injectionState;
465
466 bool dispatchInProgress; // initially false, set to true while dispatching
467
Yi Kong9b14ac62018-07-17 13:48:38 -0700468 inline bool isInjected() const { return injectionState != nullptr; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800469
470 void release();
471
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800472 virtual void appendDescription(std::string& msg) const = 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800473
474 protected:
Prabir Pradhan42611e02018-11-27 14:04:02 -0800475 EventEntry(uint32_t sequenceNum, int32_t type, nsecs_t eventTime, uint32_t policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800476 virtual ~EventEntry();
477 void releaseInjectionState();
478 };
479
480 struct ConfigurationChangedEntry : EventEntry {
Prabir Pradhan42611e02018-11-27 14:04:02 -0800481 explicit ConfigurationChangedEntry(uint32_t sequenceNum, nsecs_t eventTime);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800482 virtual void appendDescription(std::string& msg) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800483
484 protected:
485 virtual ~ConfigurationChangedEntry();
486 };
487
488 struct DeviceResetEntry : EventEntry {
489 int32_t deviceId;
490
Prabir Pradhan42611e02018-11-27 14:04:02 -0800491 DeviceResetEntry(uint32_t sequenceNum, nsecs_t eventTime, int32_t deviceId);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800492 virtual void appendDescription(std::string& msg) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800493
494 protected:
495 virtual ~DeviceResetEntry();
496 };
497
498 struct KeyEntry : EventEntry {
499 int32_t deviceId;
500 uint32_t source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100501 int32_t displayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800502 int32_t action;
503 int32_t flags;
504 int32_t keyCode;
505 int32_t scanCode;
506 int32_t metaState;
507 int32_t repeatCount;
508 nsecs_t downTime;
509
510 bool syntheticRepeat; // set to true for synthetic key repeats
511
512 enum InterceptKeyResult {
513 INTERCEPT_KEY_RESULT_UNKNOWN,
514 INTERCEPT_KEY_RESULT_SKIP,
515 INTERCEPT_KEY_RESULT_CONTINUE,
516 INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER,
517 };
518 InterceptKeyResult interceptKeyResult; // set based on the interception result
519 nsecs_t interceptKeyWakeupTime; // used with INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER
520
Prabir Pradhan42611e02018-11-27 14:04:02 -0800521 KeyEntry(uint32_t sequenceNum, nsecs_t eventTime,
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100522 int32_t deviceId, uint32_t source, int32_t displayId, uint32_t policyFlags,
523 int32_t action, int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800524 int32_t repeatCount, nsecs_t downTime);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800525 virtual void appendDescription(std::string& msg) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800526 void recycle();
527
528 protected:
529 virtual ~KeyEntry();
530 };
531
532 struct MotionEntry : EventEntry {
533 nsecs_t eventTime;
534 int32_t deviceId;
535 uint32_t source;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800536 int32_t displayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800537 int32_t action;
Michael Wright7b159c92015-05-14 14:48:03 +0100538 int32_t actionButton;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800539 int32_t flags;
540 int32_t metaState;
541 int32_t buttonState;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800542 MotionClassification classification;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800543 int32_t edgeFlags;
544 float xPrecision;
545 float yPrecision;
546 nsecs_t downTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800547 uint32_t pointerCount;
548 PointerProperties pointerProperties[MAX_POINTERS];
549 PointerCoords pointerCoords[MAX_POINTERS];
550
Prabir Pradhan42611e02018-11-27 14:04:02 -0800551 MotionEntry(uint32_t sequenceNum, nsecs_t eventTime,
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800552 int32_t deviceId, uint32_t source, int32_t displayId, uint32_t policyFlags,
Michael Wright7b159c92015-05-14 14:48:03 +0100553 int32_t action, int32_t actionButton, int32_t flags,
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800554 int32_t metaState, int32_t buttonState, MotionClassification classification,
555 int32_t edgeFlags, float xPrecision, float yPrecision,
556 nsecs_t downTime, uint32_t pointerCount,
Jeff Brownf086ddb2014-02-11 14:28:48 -0800557 const PointerProperties* pointerProperties, const PointerCoords* pointerCoords,
558 float xOffset, float yOffset);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800559 virtual void appendDescription(std::string& msg) const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800560
561 protected:
562 virtual ~MotionEntry();
563 };
564
565 // Tracks the progress of dispatching a particular event to a particular connection.
566 struct DispatchEntry : Link<DispatchEntry> {
567 const uint32_t seq; // unique sequence number, never 0
568
569 EventEntry* eventEntry; // the event to dispatch
570 int32_t targetFlags;
571 float xOffset;
572 float yOffset;
Robert Carre07e1032018-11-26 12:55:53 -0800573 float globalScaleFactor;
574 float windowXScale = 1.0f;
575 float windowYScale = 1.0f;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800576 nsecs_t deliveryTime; // time when the event was actually delivered
577
578 // Set to the resolved action and flags when the event is enqueued.
579 int32_t resolvedAction;
580 int32_t resolvedFlags;
581
582 DispatchEntry(EventEntry* eventEntry,
Robert Carre07e1032018-11-26 12:55:53 -0800583 int32_t targetFlags, float xOffset, float yOffset,
584 float globalScaleFactor, float windowXScale, float windowYScale);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800585 ~DispatchEntry();
586
587 inline bool hasForegroundTarget() const {
588 return targetFlags & InputTarget::FLAG_FOREGROUND;
589 }
590
591 inline bool isSplit() const {
592 return targetFlags & InputTarget::FLAG_SPLIT;
593 }
594
595 private:
596 static volatile int32_t sNextSeqAtomic;
597
598 static uint32_t nextSeq();
599 };
600
601 // A command entry captures state and behavior for an action to be performed in the
602 // dispatch loop after the initial processing has taken place. It is essentially
603 // a kind of continuation used to postpone sensitive policy interactions to a point
604 // in the dispatch loop where it is safe to release the lock (generally after finishing
605 // the critical parts of the dispatch cycle).
606 //
607 // The special thing about commands is that they can voluntarily release and reacquire
608 // the dispatcher lock at will. Initially when the command starts running, the
609 // dispatcher lock is held. However, if the command needs to call into the policy to
610 // do some work, it can release the lock, do the work, then reacquire the lock again
611 // before returning.
612 //
613 // This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch
614 // never calls into the policy while holding its lock.
615 //
616 // Commands are implicitly 'LockedInterruptible'.
617 struct CommandEntry;
618 typedef void (InputDispatcher::*Command)(CommandEntry* commandEntry);
619
620 class Connection;
621 struct CommandEntry : Link<CommandEntry> {
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700622 explicit CommandEntry(Command command);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800623 ~CommandEntry();
624
625 Command command;
626
627 // parameters for the command (usage varies by command)
628 sp<Connection> connection;
629 nsecs_t eventTime;
630 KeyEntry* keyEntry;
631 sp<InputApplicationHandle> inputApplicationHandle;
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800632 std::string reason;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800633 int32_t userActivityEventType;
634 uint32_t seq;
635 bool handled;
Robert Carr803535b2018-08-02 16:38:15 -0700636 sp<InputChannel> inputChannel;
chaviw0c06c6e2019-01-09 13:27:07 -0800637 sp<IBinder> oldToken;
638 sp<IBinder> newToken;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800639 };
640
641 // Generic queue implementation.
642 template <typename T>
643 struct Queue {
644 T* head;
645 T* tail;
Jon McCaffrey65dbe972014-11-18 12:07:08 -0800646 uint32_t entryCount;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800647
Yi Kong9b14ac62018-07-17 13:48:38 -0700648 inline Queue() : head(nullptr), tail(nullptr), entryCount(0) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800649 }
650
651 inline bool isEmpty() const {
652 return !head;
653 }
654
655 inline void enqueueAtTail(T* entry) {
Jon McCaffrey65dbe972014-11-18 12:07:08 -0800656 entryCount++;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800657 entry->prev = tail;
658 if (tail) {
659 tail->next = entry;
660 } else {
661 head = entry;
662 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700663 entry->next = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800664 tail = entry;
665 }
666
667 inline void enqueueAtHead(T* entry) {
Jon McCaffrey65dbe972014-11-18 12:07:08 -0800668 entryCount++;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800669 entry->next = head;
670 if (head) {
671 head->prev = entry;
672 } else {
673 tail = entry;
674 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700675 entry->prev = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800676 head = entry;
677 }
678
679 inline void dequeue(T* entry) {
Jon McCaffrey65dbe972014-11-18 12:07:08 -0800680 entryCount--;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800681 if (entry->prev) {
682 entry->prev->next = entry->next;
683 } else {
684 head = entry->next;
685 }
686 if (entry->next) {
687 entry->next->prev = entry->prev;
688 } else {
689 tail = entry->prev;
690 }
691 }
692
693 inline T* dequeueAtHead() {
Jon McCaffrey65dbe972014-11-18 12:07:08 -0800694 entryCount--;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800695 T* entry = head;
696 head = entry->next;
697 if (head) {
Yi Kong9b14ac62018-07-17 13:48:38 -0700698 head->prev = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800699 } else {
Yi Kong9b14ac62018-07-17 13:48:38 -0700700 tail = nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800701 }
702 return entry;
703 }
704
Jon McCaffrey65dbe972014-11-18 12:07:08 -0800705 uint32_t count() const {
706 return entryCount;
707 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800708 };
709
710 /* Specifies which events are to be canceled and why. */
711 struct CancelationOptions {
712 enum Mode {
713 CANCEL_ALL_EVENTS = 0,
714 CANCEL_POINTER_EVENTS = 1,
715 CANCEL_NON_POINTER_EVENTS = 2,
716 CANCEL_FALLBACK_EVENTS = 3,
Tiger Huang721e26f2018-07-24 22:26:19 +0800717
718 /* Cancel events where the display not specified. These events would go to the focused
719 * display. */
720 CANCEL_DISPLAY_UNSPECIFIED_EVENTS = 4,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800721 };
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
729 // The specific keycode of the key event to cancel, or -1 to cancel any key event.
730 int32_t keyCode;
731
732 // The specific device id of events to cancel, or -1 to cancel events from any device.
733 int32_t deviceId;
734
735 CancelationOptions(Mode mode, const char* reason) :
736 mode(mode), reason(reason), keyCode(-1), deviceId(-1) { }
737 };
738
739 /* Tracks dispatched key and motion event state so that cancelation events can be
740 * synthesized when events are dropped. */
741 class InputState {
742 public:
743 InputState();
744 ~InputState();
745
746 // Returns true if there is no state to be canceled.
747 bool isNeutral() const;
748
749 // Returns true if the specified source is known to have received a hover enter
750 // motion event.
751 bool isHovering(int32_t deviceId, uint32_t source, int32_t displayId) const;
752
753 // Records tracking information for a key event that has just been published.
754 // Returns true if the event should be delivered, false if it is inconsistent
755 // and should be skipped.
756 bool trackKey(const KeyEntry* entry, int32_t action, int32_t flags);
757
758 // Records tracking information for a motion event that has just been published.
759 // Returns true if the event should be delivered, false if it is inconsistent
760 // and should be skipped.
761 bool trackMotion(const MotionEntry* entry, int32_t action, int32_t flags);
762
763 // Synthesizes cancelation events for the current state and resets the tracked state.
764 void synthesizeCancelationEvents(nsecs_t currentTime,
765 Vector<EventEntry*>& outEvents, const CancelationOptions& options);
766
767 // Clears the current state.
768 void clear();
769
770 // Copies pointer-related parts of the input state to another instance.
771 void copyPointerStateTo(InputState& other) const;
772
773 // Gets the fallback key associated with a keycode.
774 // Returns -1 if none.
775 // Returns AKEYCODE_UNKNOWN if we are only dispatching the unhandled key to the policy.
776 int32_t getFallbackKey(int32_t originalKeyCode);
777
778 // Sets the fallback key for a particular keycode.
779 void setFallbackKey(int32_t originalKeyCode, int32_t fallbackKeyCode);
780
781 // Removes the fallback key for a particular keycode.
782 void removeFallbackKey(int32_t originalKeyCode);
783
784 inline const KeyedVector<int32_t, int32_t>& getFallbackKeys() const {
785 return mFallbackKeys;
786 }
787
788 private:
789 struct KeyMemento {
790 int32_t deviceId;
791 uint32_t source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100792 int32_t displayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800793 int32_t keyCode;
794 int32_t scanCode;
795 int32_t metaState;
796 int32_t flags;
797 nsecs_t downTime;
798 uint32_t policyFlags;
799 };
800
801 struct MotionMemento {
802 int32_t deviceId;
803 uint32_t source;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800804 int32_t displayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800805 int32_t flags;
806 float xPrecision;
807 float yPrecision;
808 nsecs_t downTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800809 uint32_t pointerCount;
810 PointerProperties pointerProperties[MAX_POINTERS];
811 PointerCoords pointerCoords[MAX_POINTERS];
812 bool hovering;
813 uint32_t policyFlags;
814
815 void setPointers(const MotionEntry* entry);
816 };
817
818 Vector<KeyMemento> mKeyMementos;
819 Vector<MotionMemento> mMotionMementos;
820 KeyedVector<int32_t, int32_t> mFallbackKeys;
821
822 ssize_t findKeyMemento(const KeyEntry* entry) const;
823 ssize_t findMotionMemento(const MotionEntry* entry, bool hovering) const;
824
825 void addKeyMemento(const KeyEntry* entry, int32_t flags);
826 void addMotionMemento(const MotionEntry* entry, int32_t flags, bool hovering);
827
828 static bool shouldCancelKey(const KeyMemento& memento,
829 const CancelationOptions& options);
830 static bool shouldCancelMotion(const MotionMemento& memento,
831 const CancelationOptions& options);
832 };
833
834 /* Manages the dispatch state associated with a single input channel. */
835 class Connection : public RefBase {
836 protected:
837 virtual ~Connection();
838
839 public:
840 enum Status {
841 // Everything is peachy.
842 STATUS_NORMAL,
843 // An unrecoverable communication error has occurred.
844 STATUS_BROKEN,
845 // The input channel has been unregistered.
846 STATUS_ZOMBIE
847 };
848
849 Status status;
850 sp<InputChannel> inputChannel; // never null
Michael Wrightd02c5b62014-02-10 15:10:22 -0800851 bool monitor;
852 InputPublisher inputPublisher;
853 InputState inputState;
854
855 // True if the socket is full and no further events can be published until
856 // the application consumes some of the input.
857 bool inputPublisherBlocked;
858
859 // Queue of events that need to be published to the connection.
860 Queue<DispatchEntry> outboundQueue;
861
862 // Queue of events that have been published to the connection but that have not
863 // yet received a "finished" response from the application.
864 Queue<DispatchEntry> waitQueue;
865
Robert Carr803535b2018-08-02 16:38:15 -0700866 explicit Connection(const sp<InputChannel>& inputChannel, bool monitor);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800867
Siarhei Vishniakou587c3f02018-01-04 11:46:44 -0800868 inline const std::string getInputChannelName() const { return inputChannel->getName(); }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800869
Siarhei Vishniakou587c3f02018-01-04 11:46:44 -0800870 const std::string getWindowName() const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800871 const char* getStatusLabel() const;
872
873 DispatchEntry* findWaitQueueEntry(uint32_t seq);
874 };
875
876 enum DropReason {
877 DROP_REASON_NOT_DROPPED = 0,
878 DROP_REASON_POLICY = 1,
879 DROP_REASON_APP_SWITCH = 2,
880 DROP_REASON_DISABLED = 3,
881 DROP_REASON_BLOCKED = 4,
882 DROP_REASON_STALE = 5,
883 };
884
885 sp<InputDispatcherPolicyInterface> mPolicy;
886 InputDispatcherConfiguration mConfig;
887
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800888 std::mutex mLock;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800889
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800890 std::condition_variable mDispatcherIsAlive;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800891
892 sp<Looper> mLooper;
893
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800894 EventEntry* mPendingEvent GUARDED_BY(mLock);
895 Queue<EventEntry> mInboundQueue GUARDED_BY(mLock);
896 Queue<EventEntry> mRecentQueue GUARDED_BY(mLock);
897 Queue<CommandEntry> mCommandQueue GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800898
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800899 DropReason mLastDropReason GUARDED_BY(mLock);
Michael Wright3a981722015-06-10 15:26:13 +0100900
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800901 void dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800902
903 // Enqueues an inbound event. Returns true if mLooper->wake() should be called.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800904 bool enqueueInboundEventLocked(EventEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800905
906 // Cleans up input state when dropping an inbound event.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800907 void dropInboundEventLocked(EventEntry* entry, DropReason dropReason) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800908
909 // Adds an event to a queue of recent events for debugging purposes.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800910 void addRecentEventLocked(EventEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800911
912 // App switch latency optimization.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800913 bool mAppSwitchSawKeyDown GUARDED_BY(mLock);
914 nsecs_t mAppSwitchDueTime GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800915
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800916 bool isAppSwitchKeyEvent(KeyEntry* keyEntry);
917 bool isAppSwitchPendingLocked() REQUIRES(mLock);
918 void resetPendingAppSwitchLocked(bool handled) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800919
920 // Stale event latency optimization.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800921 static bool isStaleEvent(nsecs_t currentTime, EventEntry* entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800922
923 // Blocked event latency optimization. Drops old events when the user intends
924 // to transfer focus to a new application.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800925 EventEntry* mNextUnblockedEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800926
Tiger Huang85b8c5e2019-01-17 18:34:54 +0800927 sp<InputWindowHandle> findTouchedWindowAtLocked(int32_t displayId, int32_t x, int32_t y,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800928 bool addOutsideTargets = false, bool addPortalWindows = false) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800929
930 // All registered connections mapped by channel file descriptor.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800931 KeyedVector<int, sp<Connection> > mConnectionsByFd GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800932
Robert Carr5c8a0262018-10-03 16:30:44 -0700933 struct IBinderHash {
934 std::size_t operator()(const sp<IBinder>& b) const {
935 return std::hash<IBinder *>{}(b.get());
936 }
937 };
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800938 std::unordered_map<sp<IBinder>, sp<InputChannel>, IBinderHash> mInputChannelsByToken
939 GUARDED_BY(mLock);
Robert Carr5c8a0262018-10-03 16:30:44 -0700940
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800941 ssize_t getConnectionIndexLocked(const sp<InputChannel>& inputChannel) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800942
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800943 // Input channels that will receive a copy of all input events sent to the provided display.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800944 std::unordered_map<int32_t, Vector<sp<InputChannel>>> mMonitoringChannelsByDisplay
945 GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800946
947 // Event injection and synchronization.
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800948 std::condition_variable mInjectionResultAvailable;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800949 bool hasInjectionPermission(int32_t injectorPid, int32_t injectorUid);
950 void setInjectionResultLocked(EventEntry* entry, int32_t injectionResult);
951
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800952 std::condition_variable mInjectionSyncFinished;
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800953 void incrementPendingForegroundDispatches(EventEntry* entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800954 void decrementPendingForegroundDispatchesLocked(EventEntry* entry);
955
956 // Key repeat tracking.
957 struct KeyRepeatState {
958 KeyEntry* lastKeyEntry; // or null if no repeat
959 nsecs_t nextRepeatTime;
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800960 } mKeyRepeatState GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800961
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800962 void resetKeyRepeatLocked() REQUIRES(mLock);
963 KeyEntry* synthesizeKeyRepeatLocked(nsecs_t currentTime) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800964
Michael Wright78f24442014-08-06 15:55:28 -0700965 // Key replacement tracking
966 struct KeyReplacement {
967 int32_t keyCode;
968 int32_t deviceId;
969 bool operator==(const KeyReplacement& rhs) const {
970 return keyCode == rhs.keyCode && deviceId == rhs.deviceId;
971 }
972 bool operator<(const KeyReplacement& rhs) const {
973 return keyCode != rhs.keyCode ? keyCode < rhs.keyCode : deviceId < rhs.deviceId;
974 }
975 };
976 // Maps the key code replaced, device id tuple to the key code it was replaced with
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800977 KeyedVector<KeyReplacement, int32_t> mReplacedKeys GUARDED_BY(mLock);
Siarhei Vishniakou61fafdd2018-04-13 11:00:58 -0500978 // Process certain Meta + Key combinations
979 void accelerateMetaShortcuts(const int32_t deviceId, const int32_t action,
980 int32_t& keyCode, int32_t& metaState);
Michael Wright78f24442014-08-06 15:55:28 -0700981
Michael Wrightd02c5b62014-02-10 15:10:22 -0800982 // Deferred command processing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800983 bool haveCommandsLocked() const REQUIRES(mLock);
984 bool runCommandsLockedInterruptible() REQUIRES(mLock);
985 CommandEntry* postCommandLocked(Command command) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800986
987 // Input filter processing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800988 bool shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) REQUIRES(mLock);
989 bool shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800990
991 // Inbound event processing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800992 void drainInboundQueueLocked() REQUIRES(mLock);
993 void releasePendingEventLocked() REQUIRES(mLock);
994 void releaseInboundEventLocked(EventEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800995
996 // Dispatch state.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800997 bool mDispatchEnabled GUARDED_BY(mLock);
998 bool mDispatchFrozen GUARDED_BY(mLock);
999 bool mInputFilterEnabled GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001000
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001001 std::unordered_map<int32_t, Vector<sp<InputWindowHandle>>> mWindowHandlesByDisplay
1002 GUARDED_BY(mLock);
Arthur Hungb92218b2018-08-14 12:00:21 +08001003 // Get window handles by display, return an empty vector if not found.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001004 Vector<sp<InputWindowHandle>> getWindowHandlesLocked(int32_t displayId) const REQUIRES(mLock);
1005 sp<InputWindowHandle> getWindowHandleLocked(const sp<IBinder>& windowHandleToken) const
1006 REQUIRES(mLock);
1007 sp<InputChannel> getInputChannelLocked(const sp<IBinder>& windowToken) const REQUIRES(mLock);
1008 bool hasWindowHandleLocked(const sp<InputWindowHandle>& windowHandle) const REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001009
1010 // Focus tracking for keys, trackball, etc.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001011 std::unordered_map<int32_t, sp<InputWindowHandle>> mFocusedWindowHandlesByDisplay
1012 GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001013
1014 // Focus tracking for touch.
1015 struct TouchedWindow {
1016 sp<InputWindowHandle> windowHandle;
1017 int32_t targetFlags;
1018 BitSet32 pointerIds; // zero unless target flag FLAG_SPLIT is set
1019 };
1020 struct TouchState {
1021 bool down;
1022 bool split;
1023 int32_t deviceId; // id of the device that is currently down, others are rejected
1024 uint32_t source; // source of the device that is current down, others are rejected
1025 int32_t displayId; // id to the display that currently has a touch, others are rejected
1026 Vector<TouchedWindow> windows;
1027
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001028 // This collects the portal windows that the touch has gone through. Each portal window
1029 // targets a display (embedded display for most cases). With this info, we can add the
1030 // monitoring channels of the displays touched.
1031 Vector<sp<InputWindowHandle>> portalWindows;
1032
Michael Wrightd02c5b62014-02-10 15:10:22 -08001033 TouchState();
1034 ~TouchState();
1035 void reset();
1036 void copyFrom(const TouchState& other);
1037 void addOrUpdateWindow(const sp<InputWindowHandle>& windowHandle,
1038 int32_t targetFlags, BitSet32 pointerIds);
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001039 void addPortalWindow(const sp<InputWindowHandle>& windowHandle);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001040 void removeWindow(const sp<InputWindowHandle>& windowHandle);
Robert Carr803535b2018-08-02 16:38:15 -07001041 void removeWindowByToken(const sp<IBinder>& token);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001042 void filterNonAsIsTouchWindows();
1043 sp<InputWindowHandle> getFirstForegroundWindowHandle() const;
1044 bool isSlippery() const;
1045 };
1046
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001047 KeyedVector<int32_t, TouchState> mTouchStatesByDisplay GUARDED_BY(mLock);
1048 TouchState mTempTouchState GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001049
Tiger Huang721e26f2018-07-24 22:26:19 +08001050 // Focused applications.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001051 std::unordered_map<int32_t, sp<InputApplicationHandle>> mFocusedApplicationHandlesByDisplay
1052 GUARDED_BY(mLock);
Tiger Huang721e26f2018-07-24 22:26:19 +08001053
1054 // Top focused display.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001055 int32_t mFocusedDisplayId GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001056
1057 // Dispatcher state at time of last ANR.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001058 std::string mLastANRState GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001059
1060 // Dispatch inbound events.
1061 bool dispatchConfigurationChangedLocked(
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001062 nsecs_t currentTime, ConfigurationChangedEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001063 bool dispatchDeviceResetLocked(
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001064 nsecs_t currentTime, DeviceResetEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001065 bool dispatchKeyLocked(
1066 nsecs_t currentTime, KeyEntry* entry,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001067 DropReason* dropReason, nsecs_t* nextWakeupTime) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001068 bool dispatchMotionLocked(
1069 nsecs_t currentTime, MotionEntry* entry,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001070 DropReason* dropReason, nsecs_t* nextWakeupTime) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001071 void dispatchEventLocked(nsecs_t currentTime, EventEntry* entry,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001072 const Vector<InputTarget>& inputTargets) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001073
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001074 void logOutboundKeyDetails(const char* prefix, const KeyEntry* entry);
1075 void logOutboundMotionDetails(const char* prefix, const MotionEntry* entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001076
1077 // Keeping track of ANR timeouts.
1078 enum InputTargetWaitCause {
1079 INPUT_TARGET_WAIT_CAUSE_NONE,
1080 INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY,
1081 INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY,
1082 };
1083
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001084 InputTargetWaitCause mInputTargetWaitCause GUARDED_BY(mLock);
1085 nsecs_t mInputTargetWaitStartTime GUARDED_BY(mLock);
1086 nsecs_t mInputTargetWaitTimeoutTime GUARDED_BY(mLock);
1087 bool mInputTargetWaitTimeoutExpired GUARDED_BY(mLock);
1088 sp<IBinder> mInputTargetWaitApplicationToken GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001089
1090 // Contains the last window which received a hover event.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001091 sp<InputWindowHandle> mLastHoverWindowHandle GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001092
1093 // Finding targets for input events.
1094 int32_t handleTargetsNotReadyLocked(nsecs_t currentTime, const EventEntry* entry,
1095 const sp<InputApplicationHandle>& applicationHandle,
1096 const sp<InputWindowHandle>& windowHandle,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001097 nsecs_t* nextWakeupTime, const char* reason) REQUIRES(mLock);
Robert Carr803535b2018-08-02 16:38:15 -07001098
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001099 void removeWindowByTokenLocked(const sp<IBinder>& token) REQUIRES(mLock);
Robert Carr803535b2018-08-02 16:38:15 -07001100
Michael Wrightd02c5b62014-02-10 15:10:22 -08001101 void resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001102 const sp<InputChannel>& inputChannel) REQUIRES(mLock);
1103 nsecs_t getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime) REQUIRES(mLock);
1104 void resetANRTimeoutsLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001105
Tiger Huang721e26f2018-07-24 22:26:19 +08001106 int32_t getTargetDisplayId(const EventEntry* entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001107 int32_t findFocusedWindowTargetsLocked(nsecs_t currentTime, const EventEntry* entry,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001108 Vector<InputTarget>& inputTargets, nsecs_t* nextWakeupTime) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001109 int32_t findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry* entry,
1110 Vector<InputTarget>& inputTargets, nsecs_t* nextWakeupTime,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001111 bool* outConflictingPointerActions) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001112
1113 void addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001114 int32_t targetFlags, BitSet32 pointerIds, Vector<InputTarget>& inputTargets)
1115 REQUIRES(mLock);
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001116 void addMonitoringTargetsLocked(Vector<InputTarget>& inputTargets, int32_t displayId,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001117 float xOffset = 0, float yOffset = 0) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001118
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001119 void pokeUserActivityLocked(const EventEntry* eventEntry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001120 bool checkInjectionPermission(const sp<InputWindowHandle>& windowHandle,
1121 const InjectionState* injectionState);
1122 bool isWindowObscuredAtPointLocked(const sp<InputWindowHandle>& windowHandle,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001123 int32_t x, int32_t y) const REQUIRES(mLock);
1124 bool isWindowObscuredLocked(const sp<InputWindowHandle>& windowHandle) const REQUIRES(mLock);
1125 std::string getApplicationWindowLabel(const sp<InputApplicationHandle>& applicationHandle,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001126 const sp<InputWindowHandle>& windowHandle);
1127
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -08001128 std::string checkWindowReadyForMoreInputLocked(nsecs_t currentTime,
Jeff Brownffb49772014-10-10 19:01:34 -07001129 const sp<InputWindowHandle>& windowHandle, const EventEntry* eventEntry,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001130 const char* targetType) REQUIRES(mLock);
Jeff Brownffb49772014-10-10 19:01:34 -07001131
Michael Wrightd02c5b62014-02-10 15:10:22 -08001132 // Manage the dispatch cycle for a single connection.
1133 // These methods are deliberately not Interruptible because doing all of the work
1134 // with the mutex held makes it easier to ensure that connection invariants are maintained.
1135 // If needed, the methods post commands to run later once the critical bits are done.
1136 void prepareDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001137 EventEntry* eventEntry, const InputTarget* inputTarget) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001138 void enqueueDispatchEntriesLocked(nsecs_t currentTime, const sp<Connection>& connection,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001139 EventEntry* eventEntry, const InputTarget* inputTarget) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001140 void enqueueDispatchEntryLocked(const sp<Connection>& connection,
1141 EventEntry* eventEntry, const InputTarget* inputTarget, int32_t dispatchMode);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001142 void startDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection)
1143 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001144 void finishDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001145 uint32_t seq, bool handled) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001146 void abortBrokenDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001147 bool notify) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001148 void drainDispatchQueueLocked(Queue<DispatchEntry>* queue);
1149 void releaseDispatchEntryLocked(DispatchEntry* dispatchEntry);
1150 static int handleReceiveCallback(int fd, int events, void* data);
1151
1152 void synthesizeCancelationEventsForAllConnectionsLocked(
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001153 const CancelationOptions& options) REQUIRES(mLock);
1154 void synthesizeCancelationEventsForMonitorsLocked(
1155 const CancelationOptions& options) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001156 void synthesizeCancelationEventsForInputChannelLocked(const sp<InputChannel>& channel,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001157 const CancelationOptions& options) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001158 void synthesizeCancelationEventsForConnectionLocked(const sp<Connection>& connection,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001159 const CancelationOptions& options) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001160
1161 // Splitting motion events across windows.
1162 MotionEntry* splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds);
1163
1164 // Reset and drop everything the dispatcher is doing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001165 void resetAndDropEverythingLocked(const char* reason) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001166
1167 // Dump state.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001168 void dumpDispatchStateLocked(std::string& dump) REQUIRES(mLock);
1169 void logDispatchStateLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001170
1171 // Registration.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001172 void removeMonitorChannelLocked(const sp<InputChannel>& inputChannel) REQUIRES(mLock);
1173 status_t unregisterInputChannelLocked(const sp<InputChannel>& inputChannel, bool notify)
1174 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001175
1176 // Interesting events that we might like to log or tell the framework about.
1177 void onDispatchCycleFinishedLocked(
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001178 nsecs_t currentTime, const sp<Connection>& connection, uint32_t seq, bool handled)
1179 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001180 void onDispatchCycleBrokenLocked(
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001181 nsecs_t currentTime, const sp<Connection>& connection) REQUIRES(mLock);
chaviw0c06c6e2019-01-09 13:27:07 -08001182 void onFocusChangedLocked(const sp<InputWindowHandle>& oldFocus,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001183 const sp<InputWindowHandle>& newFocus) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001184 void onANRLocked(
1185 nsecs_t currentTime, const sp<InputApplicationHandle>& applicationHandle,
1186 const sp<InputWindowHandle>& windowHandle,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001187 nsecs_t eventTime, nsecs_t waitStartTime, const char* reason) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001188
1189 // Outbound policy interactions.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001190 void doNotifyConfigurationChangedLockedInterruptible(CommandEntry* commandEntry)
1191 REQUIRES(mLock);
1192 void doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
1193 void doNotifyFocusChangedLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
1194 void doNotifyANRLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
1195 void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry)
1196 REQUIRES(mLock);
1197 void doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001198 bool afterKeyEventLockedInterruptible(const sp<Connection>& connection,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001199 DispatchEntry* dispatchEntry, KeyEntry* keyEntry, bool handled) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001200 bool afterMotionEventLockedInterruptible(const sp<Connection>& connection,
1201 DispatchEntry* dispatchEntry, MotionEntry* motionEntry, bool handled);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001202 void doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001203 void initializeKeyEvent(KeyEvent* event, const KeyEntry* entry);
1204
1205 // Statistics gathering.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001206 void updateDispatchStatistics(nsecs_t currentTime, const EventEntry* entry,
Michael Wrightd02c5b62014-02-10 15:10:22 -08001207 int32_t injectionResult, nsecs_t timeSpentWaitingForApplication);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -08001208 void traceInboundQueueLengthLocked() REQUIRES(mLock);
1209 void traceOutboundQueueLength(const sp<Connection>& connection);
1210 void traceWaitQueueLength(const sp<Connection>& connection);
Prabir Pradhanf93562f2018-11-29 12:13:37 -08001211
Prabir Pradhan79a4f0c2019-01-09 11:24:01 -08001212 sp<InputReporterInterface> mReporter;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001213};
1214
1215/* Enqueues and dispatches input events, endlessly. */
1216class InputDispatcherThread : public Thread {
1217public:
1218 explicit InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher);
1219 ~InputDispatcherThread();
1220
1221private:
1222 virtual bool threadLoop();
1223
1224 sp<InputDispatcherInterface> mDispatcher;
1225};
1226
1227} // namespace android
1228
1229#endif // _UI_INPUT_DISPATCHER_H