blob: 8d4654f95053ae28d8d413b6f400e4f1b80a4ff7 [file] [log] [blame]
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001/*
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 <ui/Input.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070021#include <ui/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>
27#include <utils/String8.h>
Jeff Brown4fe6c3e2010-09-13 23:17:30 -070028#include <utils/Looper.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070029#include <utils/Pool.h>
Jeff Brown01ce2e92010-09-26 22:20:12 -070030#include <utils/BitSet.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070031
32#include <stddef.h>
33#include <unistd.h>
Jeff Brownb88102f2010-09-08 11:49:43 -070034#include <limits.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070035
36
37namespace android {
38
Jeff Brown9c3cda02010-06-15 01:31:58 -070039/*
Jeff Brown7fbdc842010-06-17 20:52:56 -070040 * Constants used to report the outcome of input event injection.
41 */
42enum {
43 /* (INTERNAL USE ONLY) Specifies that injection is pending and its outcome is unknown. */
44 INPUT_EVENT_INJECTION_PENDING = -1,
45
46 /* Injection succeeded. */
47 INPUT_EVENT_INJECTION_SUCCEEDED = 0,
48
49 /* Injection failed because the injector did not have permission to inject
50 * into the application with input focus. */
51 INPUT_EVENT_INJECTION_PERMISSION_DENIED = 1,
52
53 /* Injection failed because there were no available input targets. */
54 INPUT_EVENT_INJECTION_FAILED = 2,
55
56 /* Injection failed due to a timeout. */
57 INPUT_EVENT_INJECTION_TIMED_OUT = 3
58};
59
Jeff Brown6ec402b2010-07-28 15:48:59 -070060/*
61 * Constants used to determine the input event injection synchronization mode.
62 */
63enum {
64 /* Injection is asynchronous and is assumed always to be successful. */
65 INPUT_EVENT_INJECTION_SYNC_NONE = 0,
66
67 /* Waits for previous events to be dispatched so that the input dispatcher can determine
68 * whether input event injection willbe permitted based on the current input focus.
69 * Does not wait for the input event to finish processing. */
70 INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT = 1,
71
72 /* Waits for the input event to be completely processed. */
73 INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISHED = 2,
74};
75
Jeff Brown7fbdc842010-06-17 20:52:56 -070076
77/*
Jeff Brown9c3cda02010-06-15 01:31:58 -070078 * An input target specifies how an input event is to be dispatched to a particular window
79 * including the window's input channel, control flags, a timeout, and an X / Y offset to
80 * be added to input event coordinates to compensate for the absolute position of the
81 * window area.
82 */
83struct InputTarget {
84 enum {
Jeff Brown519e0242010-09-15 15:18:56 -070085 /* This flag indicates that the event is being delivered to a foreground application. */
86 FLAG_FOREGROUND = 0x01,
Jeff Brown9c3cda02010-06-15 01:31:58 -070087
Jeff Brown85a31762010-09-01 17:01:00 -070088 /* This flag indicates that a MotionEvent with AMOTION_EVENT_ACTION_DOWN falls outside
89 * of the area of this target and so should instead be delivered as an
90 * AMOTION_EVENT_ACTION_OUTSIDE to this target. */
Jeff Brown9c3cda02010-06-15 01:31:58 -070091 FLAG_OUTSIDE = 0x02,
92
Jeff Brown85a31762010-09-01 17:01:00 -070093 /* This flag indicates that the target of a MotionEvent is partly or wholly
94 * obscured by another visible window above it. The motion event should be
95 * delivered with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED. */
Jeff Brown01ce2e92010-09-26 22:20:12 -070096 FLAG_WINDOW_IS_OBSCURED = 0x04,
97
98 /* This flag indicates that a motion event is being split across multiple windows. */
99 FLAG_SPLIT = 0x08,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700100 };
101
102 // The input channel to be targeted.
103 sp<InputChannel> inputChannel;
104
105 // Flags for the input target.
106 int32_t flags;
107
Jeff Brown9c3cda02010-06-15 01:31:58 -0700108 // The x and y offset to add to a MotionEvent as it is delivered.
109 // (ignored for KeyEvents)
110 float xOffset, yOffset;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700111
112 // The window type of the input target.
113 int32_t windowType;
114
115 // The subset of pointer ids to include in motion events dispatched to this input target
116 // if FLAG_SPLIT is set.
117 BitSet32 pointerIds;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700118};
119
Jeff Brown7fbdc842010-06-17 20:52:56 -0700120
Jeff Brown9c3cda02010-06-15 01:31:58 -0700121/*
Jeff Brownb88102f2010-09-08 11:49:43 -0700122 * An input window describes the bounds of a window that can receive input.
123 */
124struct InputWindow {
125 // Window flags from WindowManager.LayoutParams
126 enum {
127 FLAG_ALLOW_LOCK_WHILE_SCREEN_ON = 0x00000001,
128 FLAG_DIM_BEHIND = 0x00000002,
129 FLAG_BLUR_BEHIND = 0x00000004,
130 FLAG_NOT_FOCUSABLE = 0x00000008,
131 FLAG_NOT_TOUCHABLE = 0x00000010,
132 FLAG_NOT_TOUCH_MODAL = 0x00000020,
133 FLAG_TOUCHABLE_WHEN_WAKING = 0x00000040,
134 FLAG_KEEP_SCREEN_ON = 0x00000080,
135 FLAG_LAYOUT_IN_SCREEN = 0x00000100,
136 FLAG_LAYOUT_NO_LIMITS = 0x00000200,
137 FLAG_FULLSCREEN = 0x00000400,
138 FLAG_FORCE_NOT_FULLSCREEN = 0x00000800,
139 FLAG_DITHER = 0x00001000,
140 FLAG_SECURE = 0x00002000,
141 FLAG_SCALED = 0x00004000,
142 FLAG_IGNORE_CHEEK_PRESSES = 0x00008000,
143 FLAG_LAYOUT_INSET_DECOR = 0x00010000,
144 FLAG_ALT_FOCUSABLE_IM = 0x00020000,
145 FLAG_WATCH_OUTSIDE_TOUCH = 0x00040000,
146 FLAG_SHOW_WHEN_LOCKED = 0x00080000,
147 FLAG_SHOW_WALLPAPER = 0x00100000,
148 FLAG_TURN_SCREEN_ON = 0x00200000,
149 FLAG_DISMISS_KEYGUARD = 0x00400000,
Jeff Brown01ce2e92010-09-26 22:20:12 -0700150 FLAG_SPLIT_TOUCH = 0x00800000,
Jeff Brownb88102f2010-09-08 11:49:43 -0700151 FLAG_KEEP_SURFACE_WHILE_ANIMATING = 0x10000000,
152 FLAG_COMPATIBLE_WINDOW = 0x20000000,
153 FLAG_SYSTEM_ERROR = 0x40000000,
154 };
155
156 // Window types from WindowManager.LayoutParams
157 enum {
158 FIRST_APPLICATION_WINDOW = 1,
159 TYPE_BASE_APPLICATION = 1,
160 TYPE_APPLICATION = 2,
161 TYPE_APPLICATION_STARTING = 3,
162 LAST_APPLICATION_WINDOW = 99,
163 FIRST_SUB_WINDOW = 1000,
164 TYPE_APPLICATION_PANEL = FIRST_SUB_WINDOW,
165 TYPE_APPLICATION_MEDIA = FIRST_SUB_WINDOW+1,
166 TYPE_APPLICATION_SUB_PANEL = FIRST_SUB_WINDOW+2,
167 TYPE_APPLICATION_ATTACHED_DIALOG = FIRST_SUB_WINDOW+3,
168 TYPE_APPLICATION_MEDIA_OVERLAY = FIRST_SUB_WINDOW+4,
169 LAST_SUB_WINDOW = 1999,
170 FIRST_SYSTEM_WINDOW = 2000,
171 TYPE_STATUS_BAR = FIRST_SYSTEM_WINDOW,
172 TYPE_SEARCH_BAR = FIRST_SYSTEM_WINDOW+1,
173 TYPE_PHONE = FIRST_SYSTEM_WINDOW+2,
174 TYPE_SYSTEM_ALERT = FIRST_SYSTEM_WINDOW+3,
175 TYPE_KEYGUARD = FIRST_SYSTEM_WINDOW+4,
176 TYPE_TOAST = FIRST_SYSTEM_WINDOW+5,
177 TYPE_SYSTEM_OVERLAY = FIRST_SYSTEM_WINDOW+6,
178 TYPE_PRIORITY_PHONE = FIRST_SYSTEM_WINDOW+7,
179 TYPE_SYSTEM_DIALOG = FIRST_SYSTEM_WINDOW+8,
180 TYPE_KEYGUARD_DIALOG = FIRST_SYSTEM_WINDOW+9,
181 TYPE_SYSTEM_ERROR = FIRST_SYSTEM_WINDOW+10,
182 TYPE_INPUT_METHOD = FIRST_SYSTEM_WINDOW+11,
183 TYPE_INPUT_METHOD_DIALOG= FIRST_SYSTEM_WINDOW+12,
184 TYPE_WALLPAPER = FIRST_SYSTEM_WINDOW+13,
185 TYPE_STATUS_BAR_PANEL = FIRST_SYSTEM_WINDOW+14,
186 LAST_SYSTEM_WINDOW = 2999,
187 };
188
189 sp<InputChannel> inputChannel;
Jeff Brown519e0242010-09-15 15:18:56 -0700190 String8 name;
Jeff Brownb88102f2010-09-08 11:49:43 -0700191 int32_t layoutParamsFlags;
192 int32_t layoutParamsType;
193 nsecs_t dispatchingTimeout;
194 int32_t frameLeft;
195 int32_t frameTop;
196 int32_t frameRight;
197 int32_t frameBottom;
198 int32_t visibleFrameLeft;
199 int32_t visibleFrameTop;
200 int32_t visibleFrameRight;
201 int32_t visibleFrameBottom;
202 int32_t touchableAreaLeft;
203 int32_t touchableAreaTop;
204 int32_t touchableAreaRight;
205 int32_t touchableAreaBottom;
206 bool visible;
Jeff Brown519e0242010-09-15 15:18:56 -0700207 bool canReceiveKeys;
Jeff Brownb88102f2010-09-08 11:49:43 -0700208 bool hasFocus;
209 bool hasWallpaper;
210 bool paused;
Jeff Brown519e0242010-09-15 15:18:56 -0700211 int32_t layer;
Jeff Brownb88102f2010-09-08 11:49:43 -0700212 int32_t ownerPid;
213 int32_t ownerUid;
214
215 bool visibleFrameIntersects(const InputWindow* other) const;
216 bool touchableAreaContainsPoint(int32_t x, int32_t y) const;
217};
218
219
220/*
221 * A private handle type used by the input manager to track the window.
222 */
223class InputApplicationHandle : public RefBase {
224protected:
225 InputApplicationHandle() { }
226 virtual ~InputApplicationHandle() { }
227};
228
229
230/*
231 * An input application describes properties of an application that can receive input.
232 */
233struct InputApplication {
234 String8 name;
235 nsecs_t dispatchingTimeout;
236 sp<InputApplicationHandle> handle;
237};
238
239
240/*
Jeff Brown9c3cda02010-06-15 01:31:58 -0700241 * Input dispatcher policy interface.
242 *
243 * The input reader policy is used by the input reader to interact with the Window Manager
244 * and other system components.
245 *
246 * The actual implementation is partially supported by callbacks into the DVM
247 * via JNI. This interface is also mocked in the unit tests.
248 */
249class InputDispatcherPolicyInterface : public virtual RefBase {
250protected:
251 InputDispatcherPolicyInterface() { }
252 virtual ~InputDispatcherPolicyInterface() { }
253
254public:
255 /* Notifies the system that a configuration change has occurred. */
256 virtual void notifyConfigurationChanged(nsecs_t when) = 0;
257
Jeff Brownb88102f2010-09-08 11:49:43 -0700258 /* Notifies the system that an application is not responding.
259 * Returns a new timeout to continue waiting, or 0 to abort dispatch. */
Jeff Brown519e0242010-09-15 15:18:56 -0700260 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
261 const sp<InputChannel>& inputChannel) = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700262
Jeff Brown9c3cda02010-06-15 01:31:58 -0700263 /* Notifies the system that an input channel is unrecoverably broken. */
264 virtual void notifyInputChannelBroken(const sp<InputChannel>& inputChannel) = 0;
265
Jeff Brownb21fb102010-09-07 10:44:57 -0700266 /* Gets the key repeat initial timeout or -1 if automatic key repeating is disabled. */
Jeff Brown9c3cda02010-06-15 01:31:58 -0700267 virtual nsecs_t getKeyRepeatTimeout() = 0;
268
Jeff Brownb21fb102010-09-07 10:44:57 -0700269 /* Gets the key repeat inter-key delay. */
270 virtual nsecs_t getKeyRepeatDelay() = 0;
271
Jeff Brownae9fc032010-08-18 15:51:08 -0700272 /* Gets the maximum suggested event delivery rate per second.
273 * This value is used to throttle motion event movement actions on a per-device
274 * basis. It is not intended to be a hard limit.
275 */
276 virtual int32_t getMaxEventsPerSecond() = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700277
278 /* Allows the policy a chance to intercept a key before dispatching. */
279 virtual bool interceptKeyBeforeDispatching(const sp<InputChannel>& inputChannel,
280 const KeyEvent* keyEvent, uint32_t policyFlags) = 0;
281
282 /* Poke user activity for an event dispatched to a window. */
Jeff Brown01ce2e92010-09-26 22:20:12 -0700283 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType) = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700284
285 /* Checks whether a given application pid/uid has permission to inject input events
286 * into other applications.
287 *
288 * This method is special in that its implementation promises to be non-reentrant and
289 * is safe to call while holding other locks. (Most other methods make no such guarantees!)
290 */
291 virtual bool checkInjectEventsPermissionNonReentrant(
292 int32_t injectorPid, int32_t injectorUid) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700293};
294
295
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700296/* Notifies the system about input events generated by the input reader.
297 * The dispatcher is expected to be mostly asynchronous. */
298class InputDispatcherInterface : public virtual RefBase {
299protected:
300 InputDispatcherInterface() { }
301 virtual ~InputDispatcherInterface() { }
302
303public:
Jeff Brownb88102f2010-09-08 11:49:43 -0700304 /* Dumps the state of the input dispatcher.
305 *
306 * This method may be called on any thread (usually by the input manager). */
307 virtual void dump(String8& dump) = 0;
308
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700309 /* Runs a single iteration of the dispatch loop.
310 * Nominally processes one queued event, a timeout, or a response from an input consumer.
311 *
312 * This method should only be called on the input dispatcher thread.
313 */
314 virtual void dispatchOnce() = 0;
315
316 /* Notifies the dispatcher about new events.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700317 *
318 * These methods should only be called on the input reader thread.
319 */
Jeff Brown9c3cda02010-06-15 01:31:58 -0700320 virtual void notifyConfigurationChanged(nsecs_t eventTime) = 0;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700321 virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700322 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode,
323 int32_t scanCode, int32_t metaState, nsecs_t downTime) = 0;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700324 virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Brown85a31762010-09-01 17:01:00 -0700325 uint32_t policyFlags, int32_t action, int32_t flags,
326 int32_t metaState, int32_t edgeFlags,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700327 uint32_t pointerCount, const int32_t* pointerIds, const PointerCoords* pointerCoords,
328 float xPrecision, float yPrecision, nsecs_t downTime) = 0;
329
Jeff Brown7fbdc842010-06-17 20:52:56 -0700330 /* Injects an input event and optionally waits for sync.
Jeff Brown6ec402b2010-07-28 15:48:59 -0700331 * The synchronization mode determines whether the method blocks while waiting for
332 * input injection to proceed.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700333 * Returns one of the INPUT_EVENT_INJECTION_XXX constants.
334 *
335 * This method may be called on any thread (usually by the input manager).
336 */
337 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brown6ec402b2010-07-28 15:48:59 -0700338 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis) = 0;
Jeff Brown7fbdc842010-06-17 20:52:56 -0700339
Jeff Brownb88102f2010-09-08 11:49:43 -0700340 /* Sets the list of input windows.
341 *
342 * This method may be called on any thread (usually by the input manager).
343 */
344 virtual void setInputWindows(const Vector<InputWindow>& inputWindows) = 0;
345
346 /* Sets the focused application.
347 *
348 * This method may be called on any thread (usually by the input manager).
349 */
350 virtual void setFocusedApplication(const InputApplication* inputApplication) = 0;
351
352 /* Sets the input dispatching mode.
353 *
354 * This method may be called on any thread (usually by the input manager).
355 */
356 virtual void setInputDispatchMode(bool enabled, bool frozen) = 0;
357
Jeff Browne6504122010-09-27 14:52:15 -0700358 /* Transfers touch focus from the window associated with one channel to the
359 * window associated with the other channel.
360 *
361 * Returns true on success. False if the window did not actually have touch focus.
362 */
363 virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel,
364 const sp<InputChannel>& toChannel) = 0;
365
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700366 /* Registers or unregister input channels that may be used as targets for input events.
Jeff Brownb88102f2010-09-08 11:49:43 -0700367 * If monitor is true, the channel will receive a copy of all input events.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700368 *
369 * These methods may be called on any thread (usually by the input manager).
370 */
Jeff Brownb88102f2010-09-08 11:49:43 -0700371 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel, bool monitor) = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700372 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) = 0;
373};
374
Jeff Brown9c3cda02010-06-15 01:31:58 -0700375/* Dispatches events to input targets. Some functions of the input dispatcher, such as
376 * identifying input targets, are controlled by a separate policy object.
377 *
378 * IMPORTANT INVARIANT:
379 * Because the policy can potentially block or cause re-entrance into the input dispatcher,
380 * the input dispatcher never calls into the policy while holding its internal locks.
381 * The implementation is also carefully designed to recover from scenarios such as an
382 * input channel becoming unregistered while identifying input targets or processing timeouts.
383 *
384 * Methods marked 'Locked' must be called with the lock acquired.
385 *
386 * Methods marked 'LockedInterruptible' must be called with the lock acquired but
387 * may during the course of their execution release the lock, call into the policy, and
388 * then reacquire the lock. The caller is responsible for recovering gracefully.
389 *
390 * A 'LockedInterruptible' method may called a 'Locked' method, but NOT vice-versa.
391 */
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700392class InputDispatcher : public InputDispatcherInterface {
393protected:
394 virtual ~InputDispatcher();
395
396public:
Jeff Brown9c3cda02010-06-15 01:31:58 -0700397 explicit InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700398
Jeff Brownb88102f2010-09-08 11:49:43 -0700399 virtual void dump(String8& dump);
400
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700401 virtual void dispatchOnce();
402
Jeff Brown9c3cda02010-06-15 01:31:58 -0700403 virtual void notifyConfigurationChanged(nsecs_t eventTime);
Jeff Brownc5ed5912010-07-14 18:48:53 -0700404 virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700405 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode,
406 int32_t scanCode, int32_t metaState, nsecs_t downTime);
Jeff Brownc5ed5912010-07-14 18:48:53 -0700407 virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Brown85a31762010-09-01 17:01:00 -0700408 uint32_t policyFlags, int32_t action, int32_t flags,
409 int32_t metaState, int32_t edgeFlags,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700410 uint32_t pointerCount, const int32_t* pointerIds, const PointerCoords* pointerCoords,
411 float xPrecision, float yPrecision, nsecs_t downTime);
412
Jeff Brown7fbdc842010-06-17 20:52:56 -0700413 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brown6ec402b2010-07-28 15:48:59 -0700414 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700415
Jeff Brownb88102f2010-09-08 11:49:43 -0700416 virtual void setInputWindows(const Vector<InputWindow>& inputWindows);
417 virtual void setFocusedApplication(const InputApplication* inputApplication);
418 virtual void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown349703e2010-06-22 01:27:15 -0700419
Jeff Browne6504122010-09-27 14:52:15 -0700420 virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel,
421 const sp<InputChannel>& toChannel);
422
Jeff Brownb88102f2010-09-08 11:49:43 -0700423 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel, bool monitor);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700424 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel);
425
426private:
427 template <typename T>
428 struct Link {
429 T* next;
430 T* prev;
431 };
432
Jeff Brown01ce2e92010-09-26 22:20:12 -0700433 struct InjectionState {
434 mutable int32_t refCount;
435
436 int32_t injectorPid;
437 int32_t injectorUid;
438 int32_t injectionResult; // initially INPUT_EVENT_INJECTION_PENDING
439 bool injectionIsAsync; // set to true if injection is not waiting for the result
440 int32_t pendingForegroundDispatches; // the number of foreground dispatches in progress
441 };
442
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700443 struct EventEntry : Link<EventEntry> {
444 enum {
445 TYPE_SENTINEL,
446 TYPE_CONFIGURATION_CHANGED,
447 TYPE_KEY,
448 TYPE_MOTION
449 };
450
Jeff Brown01ce2e92010-09-26 22:20:12 -0700451 mutable int32_t refCount;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700452 int32_t type;
453 nsecs_t eventTime;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700454 InjectionState* injectionState;
Jeff Brown7fbdc842010-06-17 20:52:56 -0700455
Jeff Brown9c3cda02010-06-15 01:31:58 -0700456 bool dispatchInProgress; // initially false, set to true while dispatching
Jeff Brown7fbdc842010-06-17 20:52:56 -0700457
Jeff Brown01ce2e92010-09-26 22:20:12 -0700458 inline bool isInjected() { return injectionState != NULL; }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700459 };
460
461 struct ConfigurationChangedEntry : EventEntry {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700462 };
463
464 struct KeyEntry : EventEntry {
465 int32_t deviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700466 int32_t source;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700467 uint32_t policyFlags;
468 int32_t action;
469 int32_t flags;
470 int32_t keyCode;
471 int32_t scanCode;
472 int32_t metaState;
473 int32_t repeatCount;
474 nsecs_t downTime;
Jeff Brownb88102f2010-09-08 11:49:43 -0700475
476 bool syntheticRepeat; // set to true for synthetic key repeats
477
478 enum InterceptKeyResult {
479 INTERCEPT_KEY_RESULT_UNKNOWN,
480 INTERCEPT_KEY_RESULT_SKIP,
481 INTERCEPT_KEY_RESULT_CONTINUE,
482 };
483 InterceptKeyResult interceptKeyResult; // set based on the interception result
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700484 };
485
486 struct MotionSample {
487 MotionSample* next;
488
489 nsecs_t eventTime;
490 PointerCoords pointerCoords[MAX_POINTERS];
491 };
492
493 struct MotionEntry : EventEntry {
494 int32_t deviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700495 int32_t source;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700496 uint32_t policyFlags;
497 int32_t action;
Jeff Brown85a31762010-09-01 17:01:00 -0700498 int32_t flags;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700499 int32_t metaState;
500 int32_t edgeFlags;
501 float xPrecision;
502 float yPrecision;
503 nsecs_t downTime;
504 uint32_t pointerCount;
505 int32_t pointerIds[MAX_POINTERS];
506
507 // Linked list of motion samples associated with this motion event.
508 MotionSample firstSample;
509 MotionSample* lastSample;
Jeff Brownae9fc032010-08-18 15:51:08 -0700510
511 uint32_t countSamples() const;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700512 };
513
Jeff Brown9c3cda02010-06-15 01:31:58 -0700514 // Tracks the progress of dispatching a particular event to a particular connection.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700515 struct DispatchEntry : Link<DispatchEntry> {
516 EventEntry* eventEntry; // the event to dispatch
517 int32_t targetFlags;
518 float xOffset;
519 float yOffset;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700520
521 // True if dispatch has started.
522 bool inProgress;
523
524 // For motion events:
525 // Pointer to the first motion sample to dispatch in this cycle.
526 // Usually NULL to indicate that the list of motion samples begins at
527 // MotionEntry::firstSample. Otherwise, some samples were dispatched in a previous
528 // cycle and this pointer indicates the location of the first remainining sample
529 // to dispatch during the current cycle.
530 MotionSample* headMotionSample;
531 // Pointer to a motion sample to dispatch in the next cycle if the dispatcher was
532 // unable to send all motion samples during this cycle. On the next cycle,
533 // headMotionSample will be initialized to tailMotionSample and tailMotionSample
534 // will be set to NULL.
535 MotionSample* tailMotionSample;
Jeff Brown6ec402b2010-07-28 15:48:59 -0700536
Jeff Brown519e0242010-09-15 15:18:56 -0700537 inline bool hasForegroundTarget() const {
538 return targetFlags & InputTarget::FLAG_FOREGROUND;
Jeff Brownb88102f2010-09-08 11:49:43 -0700539 }
Jeff Brown01ce2e92010-09-26 22:20:12 -0700540
541 inline bool isSplit() const {
542 return targetFlags & InputTarget::FLAG_SPLIT;
543 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700544 };
545
Jeff Brown9c3cda02010-06-15 01:31:58 -0700546 // A command entry captures state and behavior for an action to be performed in the
547 // dispatch loop after the initial processing has taken place. It is essentially
548 // a kind of continuation used to postpone sensitive policy interactions to a point
549 // in the dispatch loop where it is safe to release the lock (generally after finishing
550 // the critical parts of the dispatch cycle).
551 //
552 // The special thing about commands is that they can voluntarily release and reacquire
553 // the dispatcher lock at will. Initially when the command starts running, the
554 // dispatcher lock is held. However, if the command needs to call into the policy to
555 // do some work, it can release the lock, do the work, then reacquire the lock again
556 // before returning.
557 //
558 // This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch
559 // never calls into the policy while holding its lock.
560 //
561 // Commands are implicitly 'LockedInterruptible'.
562 struct CommandEntry;
563 typedef void (InputDispatcher::*Command)(CommandEntry* commandEntry);
564
Jeff Brown7fbdc842010-06-17 20:52:56 -0700565 class Connection;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700566 struct CommandEntry : Link<CommandEntry> {
567 CommandEntry();
568 ~CommandEntry();
569
570 Command command;
571
572 // parameters for the command (usage varies by command)
Jeff Brown7fbdc842010-06-17 20:52:56 -0700573 sp<Connection> connection;
Jeff Brownb88102f2010-09-08 11:49:43 -0700574 nsecs_t eventTime;
575 KeyEntry* keyEntry;
576 sp<InputChannel> inputChannel;
577 sp<InputApplicationHandle> inputApplicationHandle;
Jeff Brownb88102f2010-09-08 11:49:43 -0700578 int32_t userActivityEventType;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700579 };
580
581 // Generic queue implementation.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700582 template <typename T>
583 struct Queue {
Jeff Brownb88102f2010-09-08 11:49:43 -0700584 T headSentinel;
585 T tailSentinel;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700586
587 inline Queue() {
Jeff Brownb88102f2010-09-08 11:49:43 -0700588 headSentinel.prev = NULL;
589 headSentinel.next = & tailSentinel;
590 tailSentinel.prev = & headSentinel;
591 tailSentinel.next = NULL;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700592 }
593
Jeff Brownb88102f2010-09-08 11:49:43 -0700594 inline bool isEmpty() const {
595 return headSentinel.next == & tailSentinel;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700596 }
597
598 inline void enqueueAtTail(T* entry) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700599 T* last = tailSentinel.prev;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700600 last->next = entry;
601 entry->prev = last;
Jeff Brownb88102f2010-09-08 11:49:43 -0700602 entry->next = & tailSentinel;
603 tailSentinel.prev = entry;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700604 }
605
606 inline void enqueueAtHead(T* entry) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700607 T* first = headSentinel.next;
608 headSentinel.next = entry;
609 entry->prev = & headSentinel;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700610 entry->next = first;
611 first->prev = entry;
612 }
613
614 inline void dequeue(T* entry) {
615 entry->prev->next = entry->next;
616 entry->next->prev = entry->prev;
617 }
618
619 inline T* dequeueAtHead() {
Jeff Brownb88102f2010-09-08 11:49:43 -0700620 T* first = headSentinel.next;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700621 dequeue(first);
622 return first;
623 }
Jeff Brown519e0242010-09-15 15:18:56 -0700624
625 uint32_t count() const;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700626 };
627
628 /* Allocates queue entries and performs reference counting as needed. */
629 class Allocator {
630 public:
631 Allocator();
632
Jeff Brown01ce2e92010-09-26 22:20:12 -0700633 InjectionState* obtainInjectionState(int32_t injectorPid, int32_t injectorUid);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700634 ConfigurationChangedEntry* obtainConfigurationChangedEntry(nsecs_t eventTime);
635 KeyEntry* obtainKeyEntry(nsecs_t eventTime,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700636 int32_t deviceId, int32_t source, uint32_t policyFlags, int32_t action,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700637 int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState,
638 int32_t repeatCount, nsecs_t downTime);
639 MotionEntry* obtainMotionEntry(nsecs_t eventTime,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700640 int32_t deviceId, int32_t source, uint32_t policyFlags, int32_t action,
Jeff Brown85a31762010-09-01 17:01:00 -0700641 int32_t flags, int32_t metaState, int32_t edgeFlags,
642 float xPrecision, float yPrecision,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700643 nsecs_t downTime, uint32_t pointerCount,
644 const int32_t* pointerIds, const PointerCoords* pointerCoords);
Jeff Brownb88102f2010-09-08 11:49:43 -0700645 DispatchEntry* obtainDispatchEntry(EventEntry* eventEntry,
Jeff Brown519e0242010-09-15 15:18:56 -0700646 int32_t targetFlags, float xOffset, float yOffset);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700647 CommandEntry* obtainCommandEntry(Command command);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700648
Jeff Brown01ce2e92010-09-26 22:20:12 -0700649 void releaseInjectionState(InjectionState* injectionState);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700650 void releaseEventEntry(EventEntry* entry);
651 void releaseConfigurationChangedEntry(ConfigurationChangedEntry* entry);
652 void releaseKeyEntry(KeyEntry* entry);
653 void releaseMotionEntry(MotionEntry* entry);
654 void releaseDispatchEntry(DispatchEntry* entry);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700655 void releaseCommandEntry(CommandEntry* entry);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700656
Jeff Brown01ce2e92010-09-26 22:20:12 -0700657 void recycleKeyEntry(KeyEntry* entry);
658
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700659 void appendMotionSample(MotionEntry* motionEntry,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700660 nsecs_t eventTime, const PointerCoords* pointerCoords);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700661
662 private:
Jeff Brown01ce2e92010-09-26 22:20:12 -0700663 Pool<InjectionState> mInjectionStatePool;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700664 Pool<ConfigurationChangedEntry> mConfigurationChangeEntryPool;
665 Pool<KeyEntry> mKeyEntryPool;
666 Pool<MotionEntry> mMotionEntryPool;
667 Pool<MotionSample> mMotionSamplePool;
668 Pool<DispatchEntry> mDispatchEntryPool;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700669 Pool<CommandEntry> mCommandEntryPool;
Jeff Brown7fbdc842010-06-17 20:52:56 -0700670
671 void initializeEventEntry(EventEntry* entry, int32_t type, nsecs_t eventTime);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700672 void releaseEventEntryInjectionState(EventEntry* entry);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700673 };
674
Jeff Brownb88102f2010-09-08 11:49:43 -0700675 /* Tracks dispatched key and motion event state so that cancelation events can be
676 * synthesized when events are dropped. */
677 class InputState {
678 public:
679 // Specifies whether a given event will violate input state consistency.
680 enum Consistency {
681 // The event is consistent with the current input state.
682 CONSISTENT,
683 // The event is inconsistent with the current input state but applications
684 // will tolerate it. eg. Down followed by another down.
685 TOLERABLE,
686 // The event is inconsistent with the current input state and will probably
687 // cause applications to crash. eg. Up without prior down, move with
688 // unexpected number of pointers.
689 BROKEN
690 };
691
692 InputState();
693 ~InputState();
694
695 // Returns true if there is no state to be canceled.
696 bool isNeutral() const;
697
698 // Returns true if the input state believes it is out of sync.
699 bool isOutOfSync() const;
700
701 // Sets the input state to be out of sync if it is not neutral.
702 void setOutOfSync();
703
704 // Resets the input state out of sync flag.
705 void resetOutOfSync();
706
707 // Records tracking information for an event that has just been published.
708 // Returns whether the event is consistent with the current input state.
709 Consistency trackEvent(const EventEntry* entry);
710
711 // Records tracking information for a key event that has just been published.
712 // Returns whether the event is consistent with the current input state.
713 Consistency trackKey(const KeyEntry* entry);
714
715 // Records tracking information for a motion event that has just been published.
716 // Returns whether the event is consistent with the current input state.
717 Consistency trackMotion(const MotionEntry* entry);
718
719 // Synthesizes cancelation events for the current state.
720 void synthesizeCancelationEvents(Allocator* allocator,
721 Vector<EventEntry*>& outEvents) const;
722
723 // Clears the current state.
724 void clear();
725
726 private:
727 bool mIsOutOfSync;
728
729 struct KeyMemento {
730 int32_t deviceId;
731 int32_t source;
732 int32_t keyCode;
733 int32_t scanCode;
734 nsecs_t downTime;
735 };
736
737 struct MotionMemento {
738 int32_t deviceId;
739 int32_t source;
740 float xPrecision;
741 float yPrecision;
742 nsecs_t downTime;
743 uint32_t pointerCount;
744 int32_t pointerIds[MAX_POINTERS];
745 PointerCoords pointerCoords[MAX_POINTERS];
746
747 void setPointers(const MotionEntry* entry);
748 };
749
750 Vector<KeyMemento> mKeyMementos;
751 Vector<MotionMemento> mMotionMementos;
752 };
753
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700754 /* Manages the dispatch state associated with a single input channel. */
755 class Connection : public RefBase {
756 protected:
757 virtual ~Connection();
758
759 public:
760 enum Status {
761 // Everything is peachy.
762 STATUS_NORMAL,
763 // An unrecoverable communication error has occurred.
764 STATUS_BROKEN,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700765 // The input channel has been unregistered.
766 STATUS_ZOMBIE
767 };
768
769 Status status;
770 sp<InputChannel> inputChannel;
771 InputPublisher inputPublisher;
Jeff Brownb88102f2010-09-08 11:49:43 -0700772 InputState inputState;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700773 Queue<DispatchEntry> outboundQueue;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700774
775 nsecs_t lastEventTime; // the time when the event was originally captured
776 nsecs_t lastDispatchTime; // the time when the last event was dispatched
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700777
778 explicit Connection(const sp<InputChannel>& inputChannel);
779
Jeff Brown9c3cda02010-06-15 01:31:58 -0700780 inline const char* getInputChannelName() const { return inputChannel->getName().string(); }
781
782 const char* getStatusLabel() const;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700783
784 // Finds a DispatchEntry in the outbound queue associated with the specified event.
785 // Returns NULL if not found.
786 DispatchEntry* findQueuedDispatchEntryForEvent(const EventEntry* eventEntry) const;
787
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700788 // Gets the time since the current event was originally obtained from the input driver.
Jeff Brownb88102f2010-09-08 11:49:43 -0700789 inline double getEventLatencyMillis(nsecs_t currentTime) const {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700790 return (currentTime - lastEventTime) / 1000000.0;
791 }
792
793 // Gets the time since the current event entered the outbound dispatch queue.
Jeff Brownb88102f2010-09-08 11:49:43 -0700794 inline double getDispatchLatencyMillis(nsecs_t currentTime) const {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700795 return (currentTime - lastDispatchTime) / 1000000.0;
796 }
797
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700798 status_t initialize();
799 };
800
Jeff Brown9c3cda02010-06-15 01:31:58 -0700801 sp<InputDispatcherPolicyInterface> mPolicy;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700802
803 Mutex mLock;
804
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700805 Allocator mAllocator;
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700806 sp<Looper> mLooper;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700807
Jeff Brownb88102f2010-09-08 11:49:43 -0700808 EventEntry* mPendingEvent;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700809 Queue<EventEntry> mInboundQueue;
810 Queue<CommandEntry> mCommandQueue;
811
Jeff Brownb88102f2010-09-08 11:49:43 -0700812 Vector<EventEntry*> mTempCancelationEvents;
813
814 void dispatchOnceInnerLocked(nsecs_t keyRepeatTimeout, nsecs_t keyRepeatDelay,
815 nsecs_t* nextWakeupTime);
816
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700817 // Enqueues an inbound event. Returns true if mLooper->wake() should be called.
Jeff Brownb88102f2010-09-08 11:49:43 -0700818 bool enqueueInboundEventLocked(EventEntry* entry);
819
820 // App switch latency optimization.
821 nsecs_t mAppSwitchDueTime;
822
823 static bool isAppSwitchKey(int32_t keyCode);
824 bool isAppSwitchPendingLocked();
825 bool detectPendingAppSwitchLocked(KeyEntry* inboundKeyEntry);
826 void resetPendingAppSwitchLocked(bool handled);
827
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700828 // All registered connections mapped by receive pipe file descriptor.
829 KeyedVector<int, sp<Connection> > mConnectionsByReceiveFd;
830
Jeff Brown519e0242010-09-15 15:18:56 -0700831 ssize_t getConnectionIndexLocked(const sp<InputChannel>& inputChannel);
Jeff Brown2cbecea2010-08-17 15:59:26 -0700832
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700833 // Active connections are connections that have a non-empty outbound queue.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700834 // We don't use a ref-counted pointer here because we explicitly abort connections
835 // during unregistration which causes the connection's outbound queue to be cleared
836 // and the connection itself to be deactivated.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700837 Vector<Connection*> mActiveConnections;
838
Jeff Brownb88102f2010-09-08 11:49:43 -0700839 // Input channels that will receive a copy of all input events.
840 Vector<sp<InputChannel> > mMonitoringChannels;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700841
Jeff Brownb88102f2010-09-08 11:49:43 -0700842 // Preallocated key event object used for policy inquiries.
843 KeyEvent mReusableKeyEvent;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700844
Jeff Brown7fbdc842010-06-17 20:52:56 -0700845 // Event injection and synchronization.
846 Condition mInjectionResultAvailableCondition;
Jeff Brownb88102f2010-09-08 11:49:43 -0700847 EventEntry* createEntryFromInjectedInputEventLocked(const InputEvent* event);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700848 void setInjectionResultLocked(EventEntry* entry, int32_t injectionResult);
849
Jeff Brown6ec402b2010-07-28 15:48:59 -0700850 Condition mInjectionSyncFinishedCondition;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700851 void incrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brown519e0242010-09-15 15:18:56 -0700852 void decrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brown6ec402b2010-07-28 15:48:59 -0700853
Jeff Brownae9fc032010-08-18 15:51:08 -0700854 // Throttling state.
855 struct ThrottleState {
856 nsecs_t minTimeBetweenEvents;
857
858 nsecs_t lastEventTime;
859 int32_t lastDeviceId;
860 uint32_t lastSource;
861
862 uint32_t originalSampleCount; // only collected during debugging
863 } mThrottleState;
864
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700865 // Key repeat tracking.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700866 struct KeyRepeatState {
867 KeyEntry* lastKeyEntry; // or null if no repeat
868 nsecs_t nextRepeatTime;
869 } mKeyRepeatState;
870
871 void resetKeyRepeatLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700872 KeyEntry* synthesizeKeyRepeatLocked(nsecs_t currentTime, nsecs_t keyRepeatTimeout);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700873
Jeff Brown9c3cda02010-06-15 01:31:58 -0700874 // Deferred command processing.
875 bool runCommandsLockedInterruptible();
876 CommandEntry* postCommandLocked(Command command);
877
Jeff Brownb88102f2010-09-08 11:49:43 -0700878 // Inbound event processing.
879 void drainInboundQueueLocked();
Jeff Brown54a18252010-09-16 14:07:33 -0700880 void releasePendingEventLocked();
881 void releaseInboundEventLocked(EventEntry* entry);
Jeff Brownb88102f2010-09-08 11:49:43 -0700882 bool isEventFromReliableSourceLocked(EventEntry* entry);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700883
Jeff Brownb88102f2010-09-08 11:49:43 -0700884 // Dispatch state.
885 bool mDispatchEnabled;
886 bool mDispatchFrozen;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700887
Jeff Brownb88102f2010-09-08 11:49:43 -0700888 Vector<InputWindow> mWindows;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700889
890 const InputWindow* getWindowLocked(const sp<InputChannel>& inputChannel);
Jeff Brownb88102f2010-09-08 11:49:43 -0700891
892 // Focus tracking for keys, trackball, etc.
Jeff Brown01ce2e92010-09-26 22:20:12 -0700893 const InputWindow* mFocusedWindow;
Jeff Brownb88102f2010-09-08 11:49:43 -0700894
895 // Focus tracking for touch.
Jeff Brown01ce2e92010-09-26 22:20:12 -0700896 struct TouchedWindow {
897 const InputWindow* window;
898 int32_t targetFlags;
899 BitSet32 pointerIds;
900 sp<InputChannel> channel;
Jeff Brownb88102f2010-09-08 11:49:43 -0700901 };
Jeff Brown01ce2e92010-09-26 22:20:12 -0700902 struct TouchState {
903 bool down;
904 bool split;
905 Vector<TouchedWindow> windows;
906
907 TouchState();
908 ~TouchState();
909 void reset();
910 void copyFrom(const TouchState& other);
911 void addOrUpdateWindow(const InputWindow* window, int32_t targetFlags, BitSet32 pointerIds);
912 void removeOutsideTouchWindows();
913 const InputWindow* getFirstForegroundWindow();
914 };
915
916 TouchState mTouchState;
917 TouchState mTempTouchState;
Jeff Brownb88102f2010-09-08 11:49:43 -0700918
919 // Focused application.
920 InputApplication* mFocusedApplication;
921 InputApplication mFocusedApplicationStorage; // preallocated storage for mFocusedApplication
922 void releaseFocusedApplicationLocked();
923
924 // Dispatch inbound events.
925 bool dispatchConfigurationChangedLocked(
926 nsecs_t currentTime, ConfigurationChangedEntry* entry);
927 bool dispatchKeyLocked(
928 nsecs_t currentTime, KeyEntry* entry, nsecs_t keyRepeatTimeout,
Jeff Brown54a18252010-09-16 14:07:33 -0700929 bool dropEvent, nsecs_t* nextWakeupTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700930 bool dispatchMotionLocked(
931 nsecs_t currentTime, MotionEntry* entry,
Jeff Brown54a18252010-09-16 14:07:33 -0700932 bool dropEvent, nsecs_t* nextWakeupTime);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700933 void dispatchEventToCurrentInputTargetsLocked(
934 nsecs_t currentTime, EventEntry* entry, bool resumeWithAppendedMotionSample);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700935
Jeff Brownb88102f2010-09-08 11:49:43 -0700936 void logOutboundKeyDetailsLocked(const char* prefix, const KeyEntry* entry);
937 void logOutboundMotionDetailsLocked(const char* prefix, const MotionEntry* entry);
938
939 // The input targets that were most recently identified for dispatch.
Jeff Brownb88102f2010-09-08 11:49:43 -0700940 bool mCurrentInputTargetsValid; // false while targets are being recomputed
941 Vector<InputTarget> mCurrentInputTargets;
Jeff Brownb88102f2010-09-08 11:49:43 -0700942
943 enum InputTargetWaitCause {
944 INPUT_TARGET_WAIT_CAUSE_NONE,
945 INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY,
946 INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY,
947 };
948
949 InputTargetWaitCause mInputTargetWaitCause;
950 nsecs_t mInputTargetWaitStartTime;
951 nsecs_t mInputTargetWaitTimeoutTime;
952 bool mInputTargetWaitTimeoutExpired;
953
954 // Finding targets for input events.
Jeff Brown54a18252010-09-16 14:07:33 -0700955 void resetTargetsLocked();
Jeff Brown01ce2e92010-09-26 22:20:12 -0700956 void commitTargetsLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700957 int32_t handleTargetsNotReadyLocked(nsecs_t currentTime, const EventEntry* entry,
958 const InputApplication* application, const InputWindow* window,
959 nsecs_t* nextWakeupTime);
Jeff Brown519e0242010-09-15 15:18:56 -0700960 void resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout,
961 const sp<InputChannel>& inputChannel);
962 nsecs_t getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700963 void resetANRTimeoutsLocked();
964
Jeff Brown01ce2e92010-09-26 22:20:12 -0700965 int32_t findFocusedWindowTargetsLocked(nsecs_t currentTime, const EventEntry* entry,
966 nsecs_t* nextWakeupTime);
967 int32_t findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry* entry,
968 nsecs_t* nextWakeupTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700969
Jeff Brown01ce2e92010-09-26 22:20:12 -0700970 void addWindowTargetLocked(const InputWindow* window, int32_t targetFlags,
971 BitSet32 pointerIds);
Jeff Brownb88102f2010-09-08 11:49:43 -0700972 void addMonitoringTargetsLocked();
Jeff Brown01ce2e92010-09-26 22:20:12 -0700973 bool shouldPokeUserActivityForCurrentInputTargetsLocked();
974 void pokeUserActivityLocked(nsecs_t eventTime, int32_t eventType);
975 bool checkInjectionPermission(const InputWindow* window, const InjectionState* injectionState);
Jeff Brownb88102f2010-09-08 11:49:43 -0700976 bool isWindowObscuredLocked(const InputWindow* window);
Jeff Brown519e0242010-09-15 15:18:56 -0700977 bool isWindowFinishedWithPreviousInputLocked(const InputWindow* window);
Jeff Brown519e0242010-09-15 15:18:56 -0700978 String8 getApplicationWindowLabelLocked(const InputApplication* application,
979 const InputWindow* window);
Jeff Brownb88102f2010-09-08 11:49:43 -0700980
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700981 // Manage the dispatch cycle for a single connection.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700982 // These methods are deliberately not Interruptible because doing all of the work
983 // with the mutex held makes it easier to ensure that connection invariants are maintained.
984 // If needed, the methods post commands to run later once the critical bits are done.
985 void prepareDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700986 EventEntry* eventEntry, const InputTarget* inputTarget,
987 bool resumeWithAppendedMotionSample);
Jeff Brown519e0242010-09-15 15:18:56 -0700988 void startDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700989 void finishDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brownb88102f2010-09-08 11:49:43 -0700990 void startNextDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700991 void abortDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700992 bool broken);
Jeff Brown519e0242010-09-15 15:18:56 -0700993 void drainOutboundQueueLocked(Connection* connection);
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700994 static int handleReceiveCallback(int receiveFd, int events, void* data);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700995
Jeff Brown01ce2e92010-09-26 22:20:12 -0700996 // Splitting motion events across windows.
997 MotionEntry* splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds);
998
Jeff Brownb88102f2010-09-08 11:49:43 -0700999 // Dump state.
1000 void dumpDispatchStateLocked(String8& dump);
1001 void logDispatchStateLocked();
1002
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001003 // Add or remove a connection to the mActiveConnections vector.
1004 void activateConnectionLocked(Connection* connection);
1005 void deactivateConnectionLocked(Connection* connection);
1006
1007 // Interesting events that we might like to log or tell the framework about.
Jeff Brown9c3cda02010-06-15 01:31:58 -07001008 void onDispatchCycleStartedLocked(
Jeff Brown7fbdc842010-06-17 20:52:56 -07001009 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001010 void onDispatchCycleFinishedLocked(
Jeff Brown7fbdc842010-06-17 20:52:56 -07001011 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001012 void onDispatchCycleBrokenLocked(
Jeff Brown7fbdc842010-06-17 20:52:56 -07001013 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown519e0242010-09-15 15:18:56 -07001014 void onANRLocked(
1015 nsecs_t currentTime, const InputApplication* application, const InputWindow* window,
1016 nsecs_t eventTime, nsecs_t waitStartTime);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001017
Jeff Brown7fbdc842010-06-17 20:52:56 -07001018 // Outbound policy interactions.
Jeff Brownb88102f2010-09-08 11:49:43 -07001019 void doNotifyConfigurationChangedInterruptible(CommandEntry* commandEntry);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001020 void doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown519e0242010-09-15 15:18:56 -07001021 void doNotifyANRLockedInterruptible(CommandEntry* commandEntry);
Jeff Brownb88102f2010-09-08 11:49:43 -07001022 void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry);
1023 void doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown519e0242010-09-15 15:18:56 -07001024
1025 // Statistics gathering.
1026 void updateDispatchStatisticsLocked(nsecs_t currentTime, const EventEntry* entry,
1027 int32_t injectionResult, nsecs_t timeSpentWaitingForApplication);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001028};
1029
1030/* Enqueues and dispatches input events, endlessly. */
1031class InputDispatcherThread : public Thread {
1032public:
1033 explicit InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher);
1034 ~InputDispatcherThread();
1035
1036private:
1037 virtual bool threadLoop();
1038
1039 sp<InputDispatcherInterface> mDispatcher;
1040};
1041
1042} // namespace android
1043
Jeff Brownb88102f2010-09-08 11:49:43 -07001044#endif // _UI_INPUT_DISPATCHER_H