blob: 246df8f89d127493b827f8b1f1df6b48f8bac3da [file] [log] [blame]
Jeff Browne839a582010-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 Browne839a582010-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 Brown59abe7e2010-09-13 23:17:30 -070028#include <utils/Looper.h>
Jeff Browne839a582010-04-22 18:58:52 -070029#include <utils/Pool.h>
Jeff Brownd1b0a2b2010-09-26 22:20:12 -070030#include <utils/BitSet.h>
Jeff Browne839a582010-04-22 18:58:52 -070031
32#include <stddef.h>
33#include <unistd.h>
Jeff Browna665ca82010-09-08 11:49:43 -070034#include <limits.h>
Jeff Browne839a582010-04-22 18:58:52 -070035
36
37namespace android {
38
Jeff Brown54bc2812010-06-15 01:31:58 -070039/*
Jeff Brown51d45a72010-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 Brownf67c53e2010-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 Brown51d45a72010-06-17 20:52:56 -070076
77/*
Jeff Brown54bc2812010-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 Brown53a415e2010-09-15 15:18:56 -070085 /* This flag indicates that the event is being delivered to a foreground application. */
86 FLAG_FOREGROUND = 0x01,
Jeff Brown54bc2812010-06-15 01:31:58 -070087
Jeff Brownaf30ff62010-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 Brown54bc2812010-06-15 01:31:58 -070091 FLAG_OUTSIDE = 0x02,
92
Jeff Brownaf30ff62010-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 Brownd1b0a2b2010-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 Brown54bc2812010-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 Brown54bc2812010-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 Brownd1b0a2b2010-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 Brown54bc2812010-06-15 01:31:58 -0700118};
119
Jeff Brown51d45a72010-06-17 20:52:56 -0700120
Jeff Brown54bc2812010-06-15 01:31:58 -0700121/*
Jeff Browna665ca82010-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 Brownd1b0a2b2010-09-26 22:20:12 -0700150 FLAG_SPLIT_TOUCH = 0x00800000,
Jeff Browna665ca82010-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 Brown53a415e2010-09-15 15:18:56 -0700190 String8 name;
Jeff Browna665ca82010-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 Brown53a415e2010-09-15 15:18:56 -0700207 bool canReceiveKeys;
Jeff Browna665ca82010-09-08 11:49:43 -0700208 bool hasFocus;
209 bool hasWallpaper;
210 bool paused;
Jeff Brown53a415e2010-09-15 15:18:56 -0700211 int32_t layer;
Jeff Browna665ca82010-09-08 11:49:43 -0700212 int32_t ownerPid;
213 int32_t ownerUid;
214
Jeff Browna665ca82010-09-08 11:49:43 -0700215 bool touchableAreaContainsPoint(int32_t x, int32_t y) const;
Jeff Brown35cf0e92010-10-05 12:26:23 -0700216 bool frameContainsPoint(int32_t x, int32_t y) const;
217
218 /* Returns true if the window is of a trusted type that is allowed to silently
219 * overlay other windows for the purpose of implementing the secure views feature.
220 * Trusted overlays, such as IME windows, can partly obscure other windows without causing
221 * motion events to be delivered to them with AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED.
222 */
223 bool isTrustedOverlay() const;
Jeff Browna665ca82010-09-08 11:49:43 -0700224};
225
226
227/*
228 * A private handle type used by the input manager to track the window.
229 */
230class InputApplicationHandle : public RefBase {
231protected:
232 InputApplicationHandle() { }
233 virtual ~InputApplicationHandle() { }
234};
235
236
237/*
238 * An input application describes properties of an application that can receive input.
239 */
240struct InputApplication {
241 String8 name;
242 nsecs_t dispatchingTimeout;
243 sp<InputApplicationHandle> handle;
244};
245
246
247/*
Jeff Brown54bc2812010-06-15 01:31:58 -0700248 * Input dispatcher policy interface.
249 *
250 * The input reader policy is used by the input reader to interact with the Window Manager
251 * and other system components.
252 *
253 * The actual implementation is partially supported by callbacks into the DVM
254 * via JNI. This interface is also mocked in the unit tests.
255 */
256class InputDispatcherPolicyInterface : public virtual RefBase {
257protected:
258 InputDispatcherPolicyInterface() { }
259 virtual ~InputDispatcherPolicyInterface() { }
260
261public:
262 /* Notifies the system that a configuration change has occurred. */
263 virtual void notifyConfigurationChanged(nsecs_t when) = 0;
264
Jeff Browna665ca82010-09-08 11:49:43 -0700265 /* Notifies the system that an application is not responding.
266 * Returns a new timeout to continue waiting, or 0 to abort dispatch. */
Jeff Brown53a415e2010-09-15 15:18:56 -0700267 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
268 const sp<InputChannel>& inputChannel) = 0;
Jeff Browna665ca82010-09-08 11:49:43 -0700269
Jeff Brown54bc2812010-06-15 01:31:58 -0700270 /* Notifies the system that an input channel is unrecoverably broken. */
271 virtual void notifyInputChannelBroken(const sp<InputChannel>& inputChannel) = 0;
272
Jeff Brown61ce3982010-09-07 10:44:57 -0700273 /* Gets the key repeat initial timeout or -1 if automatic key repeating is disabled. */
Jeff Brown54bc2812010-06-15 01:31:58 -0700274 virtual nsecs_t getKeyRepeatTimeout() = 0;
275
Jeff Brown61ce3982010-09-07 10:44:57 -0700276 /* Gets the key repeat inter-key delay. */
277 virtual nsecs_t getKeyRepeatDelay() = 0;
278
Jeff Brown542412c2010-08-18 15:51:08 -0700279 /* Gets the maximum suggested event delivery rate per second.
280 * This value is used to throttle motion event movement actions on a per-device
281 * basis. It is not intended to be a hard limit.
282 */
283 virtual int32_t getMaxEventsPerSecond() = 0;
Jeff Browna665ca82010-09-08 11:49:43 -0700284
285 /* Allows the policy a chance to intercept a key before dispatching. */
286 virtual bool interceptKeyBeforeDispatching(const sp<InputChannel>& inputChannel,
287 const KeyEvent* keyEvent, uint32_t policyFlags) = 0;
288
289 /* Poke user activity for an event dispatched to a window. */
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700290 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType) = 0;
Jeff Browna665ca82010-09-08 11:49:43 -0700291
292 /* Checks whether a given application pid/uid has permission to inject input events
293 * into other applications.
294 *
295 * This method is special in that its implementation promises to be non-reentrant and
296 * is safe to call while holding other locks. (Most other methods make no such guarantees!)
297 */
298 virtual bool checkInjectEventsPermissionNonReentrant(
299 int32_t injectorPid, int32_t injectorUid) = 0;
Jeff Brown54bc2812010-06-15 01:31:58 -0700300};
301
302
Jeff Browne839a582010-04-22 18:58:52 -0700303/* Notifies the system about input events generated by the input reader.
304 * The dispatcher is expected to be mostly asynchronous. */
305class InputDispatcherInterface : public virtual RefBase {
306protected:
307 InputDispatcherInterface() { }
308 virtual ~InputDispatcherInterface() { }
309
310public:
Jeff Browna665ca82010-09-08 11:49:43 -0700311 /* Dumps the state of the input dispatcher.
312 *
313 * This method may be called on any thread (usually by the input manager). */
314 virtual void dump(String8& dump) = 0;
315
Jeff Browne839a582010-04-22 18:58:52 -0700316 /* Runs a single iteration of the dispatch loop.
317 * Nominally processes one queued event, a timeout, or a response from an input consumer.
318 *
319 * This method should only be called on the input dispatcher thread.
320 */
321 virtual void dispatchOnce() = 0;
322
323 /* Notifies the dispatcher about new events.
Jeff Browne839a582010-04-22 18:58:52 -0700324 *
325 * These methods should only be called on the input reader thread.
326 */
Jeff Brown54bc2812010-06-15 01:31:58 -0700327 virtual void notifyConfigurationChanged(nsecs_t eventTime) = 0;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700328 virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Browne839a582010-04-22 18:58:52 -0700329 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode,
330 int32_t scanCode, int32_t metaState, nsecs_t downTime) = 0;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700331 virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Brownaf30ff62010-09-01 17:01:00 -0700332 uint32_t policyFlags, int32_t action, int32_t flags,
333 int32_t metaState, int32_t edgeFlags,
Jeff Browne839a582010-04-22 18:58:52 -0700334 uint32_t pointerCount, const int32_t* pointerIds, const PointerCoords* pointerCoords,
335 float xPrecision, float yPrecision, nsecs_t downTime) = 0;
336
Jeff Brown51d45a72010-06-17 20:52:56 -0700337 /* Injects an input event and optionally waits for sync.
Jeff Brownf67c53e2010-07-28 15:48:59 -0700338 * The synchronization mode determines whether the method blocks while waiting for
339 * input injection to proceed.
Jeff Brown51d45a72010-06-17 20:52:56 -0700340 * Returns one of the INPUT_EVENT_INJECTION_XXX constants.
341 *
342 * This method may be called on any thread (usually by the input manager).
343 */
344 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brownf67c53e2010-07-28 15:48:59 -0700345 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis) = 0;
Jeff Brown51d45a72010-06-17 20:52:56 -0700346
Jeff Browna665ca82010-09-08 11:49:43 -0700347 /* Sets the list of input windows.
348 *
349 * This method may be called on any thread (usually by the input manager).
350 */
351 virtual void setInputWindows(const Vector<InputWindow>& inputWindows) = 0;
352
353 /* Sets the focused application.
354 *
355 * This method may be called on any thread (usually by the input manager).
356 */
357 virtual void setFocusedApplication(const InputApplication* inputApplication) = 0;
358
359 /* Sets the input dispatching mode.
360 *
361 * This method may be called on any thread (usually by the input manager).
362 */
363 virtual void setInputDispatchMode(bool enabled, bool frozen) = 0;
364
Jeff Brown744c5592010-09-27 14:52:15 -0700365 /* Transfers touch focus from the window associated with one channel to the
366 * window associated with the other channel.
367 *
368 * Returns true on success. False if the window did not actually have touch focus.
369 */
370 virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel,
371 const sp<InputChannel>& toChannel) = 0;
372
Jeff Browne839a582010-04-22 18:58:52 -0700373 /* Registers or unregister input channels that may be used as targets for input events.
Jeff Browna665ca82010-09-08 11:49:43 -0700374 * If monitor is true, the channel will receive a copy of all input events.
Jeff Browne839a582010-04-22 18:58:52 -0700375 *
376 * These methods may be called on any thread (usually by the input manager).
377 */
Jeff Browna665ca82010-09-08 11:49:43 -0700378 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel, bool monitor) = 0;
Jeff Browne839a582010-04-22 18:58:52 -0700379 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) = 0;
380};
381
Jeff Brown54bc2812010-06-15 01:31:58 -0700382/* Dispatches events to input targets. Some functions of the input dispatcher, such as
383 * identifying input targets, are controlled by a separate policy object.
384 *
385 * IMPORTANT INVARIANT:
386 * Because the policy can potentially block or cause re-entrance into the input dispatcher,
387 * the input dispatcher never calls into the policy while holding its internal locks.
388 * The implementation is also carefully designed to recover from scenarios such as an
389 * input channel becoming unregistered while identifying input targets or processing timeouts.
390 *
391 * Methods marked 'Locked' must be called with the lock acquired.
392 *
393 * Methods marked 'LockedInterruptible' must be called with the lock acquired but
394 * may during the course of their execution release the lock, call into the policy, and
395 * then reacquire the lock. The caller is responsible for recovering gracefully.
396 *
397 * A 'LockedInterruptible' method may called a 'Locked' method, but NOT vice-versa.
398 */
Jeff Browne839a582010-04-22 18:58:52 -0700399class InputDispatcher : public InputDispatcherInterface {
400protected:
401 virtual ~InputDispatcher();
402
403public:
Jeff Brown54bc2812010-06-15 01:31:58 -0700404 explicit InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy);
Jeff Browne839a582010-04-22 18:58:52 -0700405
Jeff Browna665ca82010-09-08 11:49:43 -0700406 virtual void dump(String8& dump);
407
Jeff Browne839a582010-04-22 18:58:52 -0700408 virtual void dispatchOnce();
409
Jeff Brown54bc2812010-06-15 01:31:58 -0700410 virtual void notifyConfigurationChanged(nsecs_t eventTime);
Jeff Brown5c1ed842010-07-14 18:48:53 -0700411 virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Browne839a582010-04-22 18:58:52 -0700412 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode,
413 int32_t scanCode, int32_t metaState, nsecs_t downTime);
Jeff Brown5c1ed842010-07-14 18:48:53 -0700414 virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Brownaf30ff62010-09-01 17:01:00 -0700415 uint32_t policyFlags, int32_t action, int32_t flags,
416 int32_t metaState, int32_t edgeFlags,
Jeff Browne839a582010-04-22 18:58:52 -0700417 uint32_t pointerCount, const int32_t* pointerIds, const PointerCoords* pointerCoords,
418 float xPrecision, float yPrecision, nsecs_t downTime);
419
Jeff Brown51d45a72010-06-17 20:52:56 -0700420 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brownf67c53e2010-07-28 15:48:59 -0700421 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis);
Jeff Brown51d45a72010-06-17 20:52:56 -0700422
Jeff Browna665ca82010-09-08 11:49:43 -0700423 virtual void setInputWindows(const Vector<InputWindow>& inputWindows);
424 virtual void setFocusedApplication(const InputApplication* inputApplication);
425 virtual void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown50de30a2010-06-22 01:27:15 -0700426
Jeff Brown744c5592010-09-27 14:52:15 -0700427 virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel,
428 const sp<InputChannel>& toChannel);
429
Jeff Browna665ca82010-09-08 11:49:43 -0700430 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel, bool monitor);
Jeff Browne839a582010-04-22 18:58:52 -0700431 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel);
432
433private:
434 template <typename T>
435 struct Link {
436 T* next;
437 T* prev;
438 };
439
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700440 struct InjectionState {
441 mutable int32_t refCount;
442
443 int32_t injectorPid;
444 int32_t injectorUid;
445 int32_t injectionResult; // initially INPUT_EVENT_INJECTION_PENDING
446 bool injectionIsAsync; // set to true if injection is not waiting for the result
447 int32_t pendingForegroundDispatches; // the number of foreground dispatches in progress
448 };
449
Jeff Browne839a582010-04-22 18:58:52 -0700450 struct EventEntry : Link<EventEntry> {
451 enum {
452 TYPE_SENTINEL,
453 TYPE_CONFIGURATION_CHANGED,
454 TYPE_KEY,
455 TYPE_MOTION
456 };
457
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700458 mutable int32_t refCount;
Jeff Browne839a582010-04-22 18:58:52 -0700459 int32_t type;
460 nsecs_t eventTime;
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700461 InjectionState* injectionState;
Jeff Brown51d45a72010-06-17 20:52:56 -0700462
Jeff Brown54bc2812010-06-15 01:31:58 -0700463 bool dispatchInProgress; // initially false, set to true while dispatching
Jeff Brown51d45a72010-06-17 20:52:56 -0700464
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700465 inline bool isInjected() { return injectionState != NULL; }
Jeff Browne839a582010-04-22 18:58:52 -0700466 };
467
468 struct ConfigurationChangedEntry : EventEntry {
Jeff Browne839a582010-04-22 18:58:52 -0700469 };
470
471 struct KeyEntry : EventEntry {
472 int32_t deviceId;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700473 int32_t source;
Jeff Browne839a582010-04-22 18:58:52 -0700474 uint32_t policyFlags;
475 int32_t action;
476 int32_t flags;
477 int32_t keyCode;
478 int32_t scanCode;
479 int32_t metaState;
480 int32_t repeatCount;
481 nsecs_t downTime;
Jeff Browna665ca82010-09-08 11:49:43 -0700482
483 bool syntheticRepeat; // set to true for synthetic key repeats
484
485 enum InterceptKeyResult {
486 INTERCEPT_KEY_RESULT_UNKNOWN,
487 INTERCEPT_KEY_RESULT_SKIP,
488 INTERCEPT_KEY_RESULT_CONTINUE,
489 };
490 InterceptKeyResult interceptKeyResult; // set based on the interception result
Jeff Browne839a582010-04-22 18:58:52 -0700491 };
492
493 struct MotionSample {
494 MotionSample* next;
495
496 nsecs_t eventTime;
497 PointerCoords pointerCoords[MAX_POINTERS];
498 };
499
500 struct MotionEntry : EventEntry {
501 int32_t deviceId;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700502 int32_t source;
Jeff Browne839a582010-04-22 18:58:52 -0700503 uint32_t policyFlags;
504 int32_t action;
Jeff Brownaf30ff62010-09-01 17:01:00 -0700505 int32_t flags;
Jeff Browne839a582010-04-22 18:58:52 -0700506 int32_t metaState;
507 int32_t edgeFlags;
508 float xPrecision;
509 float yPrecision;
510 nsecs_t downTime;
511 uint32_t pointerCount;
512 int32_t pointerIds[MAX_POINTERS];
513
514 // Linked list of motion samples associated with this motion event.
515 MotionSample firstSample;
516 MotionSample* lastSample;
Jeff Brown542412c2010-08-18 15:51:08 -0700517
518 uint32_t countSamples() const;
Jeff Browne839a582010-04-22 18:58:52 -0700519 };
520
Jeff Brown54bc2812010-06-15 01:31:58 -0700521 // Tracks the progress of dispatching a particular event to a particular connection.
Jeff Browne839a582010-04-22 18:58:52 -0700522 struct DispatchEntry : Link<DispatchEntry> {
523 EventEntry* eventEntry; // the event to dispatch
524 int32_t targetFlags;
525 float xOffset;
526 float yOffset;
Jeff Browne839a582010-04-22 18:58:52 -0700527
528 // True if dispatch has started.
529 bool inProgress;
530
531 // For motion events:
532 // Pointer to the first motion sample to dispatch in this cycle.
533 // Usually NULL to indicate that the list of motion samples begins at
534 // MotionEntry::firstSample. Otherwise, some samples were dispatched in a previous
535 // cycle and this pointer indicates the location of the first remainining sample
536 // to dispatch during the current cycle.
537 MotionSample* headMotionSample;
538 // Pointer to a motion sample to dispatch in the next cycle if the dispatcher was
539 // unable to send all motion samples during this cycle. On the next cycle,
540 // headMotionSample will be initialized to tailMotionSample and tailMotionSample
541 // will be set to NULL.
542 MotionSample* tailMotionSample;
Jeff Brownf67c53e2010-07-28 15:48:59 -0700543
Jeff Brown53a415e2010-09-15 15:18:56 -0700544 inline bool hasForegroundTarget() const {
545 return targetFlags & InputTarget::FLAG_FOREGROUND;
Jeff Browna665ca82010-09-08 11:49:43 -0700546 }
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700547
548 inline bool isSplit() const {
549 return targetFlags & InputTarget::FLAG_SPLIT;
550 }
Jeff Browne839a582010-04-22 18:58:52 -0700551 };
552
Jeff Brown54bc2812010-06-15 01:31:58 -0700553 // A command entry captures state and behavior for an action to be performed in the
554 // dispatch loop after the initial processing has taken place. It is essentially
555 // a kind of continuation used to postpone sensitive policy interactions to a point
556 // in the dispatch loop where it is safe to release the lock (generally after finishing
557 // the critical parts of the dispatch cycle).
558 //
559 // The special thing about commands is that they can voluntarily release and reacquire
560 // the dispatcher lock at will. Initially when the command starts running, the
561 // dispatcher lock is held. However, if the command needs to call into the policy to
562 // do some work, it can release the lock, do the work, then reacquire the lock again
563 // before returning.
564 //
565 // This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch
566 // never calls into the policy while holding its lock.
567 //
568 // Commands are implicitly 'LockedInterruptible'.
569 struct CommandEntry;
570 typedef void (InputDispatcher::*Command)(CommandEntry* commandEntry);
571
Jeff Brown51d45a72010-06-17 20:52:56 -0700572 class Connection;
Jeff Brown54bc2812010-06-15 01:31:58 -0700573 struct CommandEntry : Link<CommandEntry> {
574 CommandEntry();
575 ~CommandEntry();
576
577 Command command;
578
579 // parameters for the command (usage varies by command)
Jeff Brown51d45a72010-06-17 20:52:56 -0700580 sp<Connection> connection;
Jeff Browna665ca82010-09-08 11:49:43 -0700581 nsecs_t eventTime;
582 KeyEntry* keyEntry;
583 sp<InputChannel> inputChannel;
584 sp<InputApplicationHandle> inputApplicationHandle;
Jeff Browna665ca82010-09-08 11:49:43 -0700585 int32_t userActivityEventType;
Jeff Brown54bc2812010-06-15 01:31:58 -0700586 };
587
588 // Generic queue implementation.
Jeff Browne839a582010-04-22 18:58:52 -0700589 template <typename T>
590 struct Queue {
Jeff Browna665ca82010-09-08 11:49:43 -0700591 T headSentinel;
592 T tailSentinel;
Jeff Browne839a582010-04-22 18:58:52 -0700593
594 inline Queue() {
Jeff Browna665ca82010-09-08 11:49:43 -0700595 headSentinel.prev = NULL;
596 headSentinel.next = & tailSentinel;
597 tailSentinel.prev = & headSentinel;
598 tailSentinel.next = NULL;
Jeff Browne839a582010-04-22 18:58:52 -0700599 }
600
Jeff Browna665ca82010-09-08 11:49:43 -0700601 inline bool isEmpty() const {
602 return headSentinel.next == & tailSentinel;
Jeff Browne839a582010-04-22 18:58:52 -0700603 }
604
605 inline void enqueueAtTail(T* entry) {
Jeff Browna665ca82010-09-08 11:49:43 -0700606 T* last = tailSentinel.prev;
Jeff Browne839a582010-04-22 18:58:52 -0700607 last->next = entry;
608 entry->prev = last;
Jeff Browna665ca82010-09-08 11:49:43 -0700609 entry->next = & tailSentinel;
610 tailSentinel.prev = entry;
Jeff Browne839a582010-04-22 18:58:52 -0700611 }
612
613 inline void enqueueAtHead(T* entry) {
Jeff Browna665ca82010-09-08 11:49:43 -0700614 T* first = headSentinel.next;
615 headSentinel.next = entry;
616 entry->prev = & headSentinel;
Jeff Browne839a582010-04-22 18:58:52 -0700617 entry->next = first;
618 first->prev = entry;
619 }
620
621 inline void dequeue(T* entry) {
622 entry->prev->next = entry->next;
623 entry->next->prev = entry->prev;
624 }
625
626 inline T* dequeueAtHead() {
Jeff Browna665ca82010-09-08 11:49:43 -0700627 T* first = headSentinel.next;
Jeff Browne839a582010-04-22 18:58:52 -0700628 dequeue(first);
629 return first;
630 }
Jeff Brown53a415e2010-09-15 15:18:56 -0700631
632 uint32_t count() const;
Jeff Browne839a582010-04-22 18:58:52 -0700633 };
634
635 /* Allocates queue entries and performs reference counting as needed. */
636 class Allocator {
637 public:
638 Allocator();
639
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700640 InjectionState* obtainInjectionState(int32_t injectorPid, int32_t injectorUid);
Jeff Brown51d45a72010-06-17 20:52:56 -0700641 ConfigurationChangedEntry* obtainConfigurationChangedEntry(nsecs_t eventTime);
642 KeyEntry* obtainKeyEntry(nsecs_t eventTime,
Jeff Brown5c1ed842010-07-14 18:48:53 -0700643 int32_t deviceId, int32_t source, uint32_t policyFlags, int32_t action,
Jeff Brown51d45a72010-06-17 20:52:56 -0700644 int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState,
645 int32_t repeatCount, nsecs_t downTime);
646 MotionEntry* obtainMotionEntry(nsecs_t eventTime,
Jeff Brown5c1ed842010-07-14 18:48:53 -0700647 int32_t deviceId, int32_t source, uint32_t policyFlags, int32_t action,
Jeff Brownaf30ff62010-09-01 17:01:00 -0700648 int32_t flags, int32_t metaState, int32_t edgeFlags,
649 float xPrecision, float yPrecision,
Jeff Brown51d45a72010-06-17 20:52:56 -0700650 nsecs_t downTime, uint32_t pointerCount,
651 const int32_t* pointerIds, const PointerCoords* pointerCoords);
Jeff Browna665ca82010-09-08 11:49:43 -0700652 DispatchEntry* obtainDispatchEntry(EventEntry* eventEntry,
Jeff Brown53a415e2010-09-15 15:18:56 -0700653 int32_t targetFlags, float xOffset, float yOffset);
Jeff Brown54bc2812010-06-15 01:31:58 -0700654 CommandEntry* obtainCommandEntry(Command command);
Jeff Browne839a582010-04-22 18:58:52 -0700655
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700656 void releaseInjectionState(InjectionState* injectionState);
Jeff Browne839a582010-04-22 18:58:52 -0700657 void releaseEventEntry(EventEntry* entry);
658 void releaseConfigurationChangedEntry(ConfigurationChangedEntry* entry);
659 void releaseKeyEntry(KeyEntry* entry);
660 void releaseMotionEntry(MotionEntry* entry);
661 void releaseDispatchEntry(DispatchEntry* entry);
Jeff Brown54bc2812010-06-15 01:31:58 -0700662 void releaseCommandEntry(CommandEntry* entry);
Jeff Browne839a582010-04-22 18:58:52 -0700663
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700664 void recycleKeyEntry(KeyEntry* entry);
665
Jeff Browne839a582010-04-22 18:58:52 -0700666 void appendMotionSample(MotionEntry* motionEntry,
Jeff Brown51d45a72010-06-17 20:52:56 -0700667 nsecs_t eventTime, const PointerCoords* pointerCoords);
Jeff Browne839a582010-04-22 18:58:52 -0700668
669 private:
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700670 Pool<InjectionState> mInjectionStatePool;
Jeff Browne839a582010-04-22 18:58:52 -0700671 Pool<ConfigurationChangedEntry> mConfigurationChangeEntryPool;
672 Pool<KeyEntry> mKeyEntryPool;
673 Pool<MotionEntry> mMotionEntryPool;
674 Pool<MotionSample> mMotionSamplePool;
675 Pool<DispatchEntry> mDispatchEntryPool;
Jeff Brown54bc2812010-06-15 01:31:58 -0700676 Pool<CommandEntry> mCommandEntryPool;
Jeff Brown51d45a72010-06-17 20:52:56 -0700677
678 void initializeEventEntry(EventEntry* entry, int32_t type, nsecs_t eventTime);
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700679 void releaseEventEntryInjectionState(EventEntry* entry);
Jeff Browne839a582010-04-22 18:58:52 -0700680 };
681
Jeff Browna665ca82010-09-08 11:49:43 -0700682 /* Tracks dispatched key and motion event state so that cancelation events can be
683 * synthesized when events are dropped. */
684 class InputState {
685 public:
686 // Specifies whether a given event will violate input state consistency.
687 enum Consistency {
688 // The event is consistent with the current input state.
689 CONSISTENT,
690 // The event is inconsistent with the current input state but applications
691 // will tolerate it. eg. Down followed by another down.
692 TOLERABLE,
693 // The event is inconsistent with the current input state and will probably
694 // cause applications to crash. eg. Up without prior down, move with
695 // unexpected number of pointers.
696 BROKEN
697 };
698
699 InputState();
700 ~InputState();
701
702 // Returns true if there is no state to be canceled.
703 bool isNeutral() const;
704
705 // Returns true if the input state believes it is out of sync.
706 bool isOutOfSync() const;
707
708 // Sets the input state to be out of sync if it is not neutral.
709 void setOutOfSync();
710
711 // Resets the input state out of sync flag.
712 void resetOutOfSync();
713
714 // Records tracking information for an event that has just been published.
715 // Returns whether the event is consistent with the current input state.
716 Consistency trackEvent(const EventEntry* entry);
717
718 // Records tracking information for a key event that has just been published.
719 // Returns whether the event is consistent with the current input state.
720 Consistency trackKey(const KeyEntry* entry);
721
722 // Records tracking information for a motion event that has just been published.
723 // Returns whether the event is consistent with the current input state.
724 Consistency trackMotion(const MotionEntry* entry);
725
726 // Synthesizes cancelation events for the current state.
727 void synthesizeCancelationEvents(Allocator* allocator,
728 Vector<EventEntry*>& outEvents) const;
729
730 // Clears the current state.
731 void clear();
732
733 private:
734 bool mIsOutOfSync;
735
736 struct KeyMemento {
737 int32_t deviceId;
738 int32_t source;
739 int32_t keyCode;
740 int32_t scanCode;
741 nsecs_t downTime;
742 };
743
744 struct MotionMemento {
745 int32_t deviceId;
746 int32_t source;
747 float xPrecision;
748 float yPrecision;
749 nsecs_t downTime;
750 uint32_t pointerCount;
751 int32_t pointerIds[MAX_POINTERS];
752 PointerCoords pointerCoords[MAX_POINTERS];
753
754 void setPointers(const MotionEntry* entry);
755 };
756
757 Vector<KeyMemento> mKeyMementos;
758 Vector<MotionMemento> mMotionMementos;
759 };
760
Jeff Browne839a582010-04-22 18:58:52 -0700761 /* Manages the dispatch state associated with a single input channel. */
762 class Connection : public RefBase {
763 protected:
764 virtual ~Connection();
765
766 public:
767 enum Status {
768 // Everything is peachy.
769 STATUS_NORMAL,
770 // An unrecoverable communication error has occurred.
771 STATUS_BROKEN,
Jeff Browne839a582010-04-22 18:58:52 -0700772 // The input channel has been unregistered.
773 STATUS_ZOMBIE
774 };
775
776 Status status;
777 sp<InputChannel> inputChannel;
778 InputPublisher inputPublisher;
Jeff Browna665ca82010-09-08 11:49:43 -0700779 InputState inputState;
Jeff Browne839a582010-04-22 18:58:52 -0700780 Queue<DispatchEntry> outboundQueue;
Jeff Browne839a582010-04-22 18:58:52 -0700781
782 nsecs_t lastEventTime; // the time when the event was originally captured
783 nsecs_t lastDispatchTime; // the time when the last event was dispatched
Jeff Browne839a582010-04-22 18:58:52 -0700784
785 explicit Connection(const sp<InputChannel>& inputChannel);
786
Jeff Brown54bc2812010-06-15 01:31:58 -0700787 inline const char* getInputChannelName() const { return inputChannel->getName().string(); }
788
789 const char* getStatusLabel() const;
Jeff Browne839a582010-04-22 18:58:52 -0700790
791 // Finds a DispatchEntry in the outbound queue associated with the specified event.
792 // Returns NULL if not found.
793 DispatchEntry* findQueuedDispatchEntryForEvent(const EventEntry* eventEntry) const;
794
Jeff Browne839a582010-04-22 18:58:52 -0700795 // Gets the time since the current event was originally obtained from the input driver.
Jeff Browna665ca82010-09-08 11:49:43 -0700796 inline double getEventLatencyMillis(nsecs_t currentTime) const {
Jeff Browne839a582010-04-22 18:58:52 -0700797 return (currentTime - lastEventTime) / 1000000.0;
798 }
799
800 // Gets the time since the current event entered the outbound dispatch queue.
Jeff Browna665ca82010-09-08 11:49:43 -0700801 inline double getDispatchLatencyMillis(nsecs_t currentTime) const {
Jeff Browne839a582010-04-22 18:58:52 -0700802 return (currentTime - lastDispatchTime) / 1000000.0;
803 }
804
Jeff Browne839a582010-04-22 18:58:52 -0700805 status_t initialize();
806 };
807
Jeff Brown54bc2812010-06-15 01:31:58 -0700808 sp<InputDispatcherPolicyInterface> mPolicy;
Jeff Browne839a582010-04-22 18:58:52 -0700809
810 Mutex mLock;
811
Jeff Browne839a582010-04-22 18:58:52 -0700812 Allocator mAllocator;
Jeff Brown59abe7e2010-09-13 23:17:30 -0700813 sp<Looper> mLooper;
Jeff Browne839a582010-04-22 18:58:52 -0700814
Jeff Browna665ca82010-09-08 11:49:43 -0700815 EventEntry* mPendingEvent;
Jeff Brown54bc2812010-06-15 01:31:58 -0700816 Queue<EventEntry> mInboundQueue;
817 Queue<CommandEntry> mCommandQueue;
818
Jeff Browna665ca82010-09-08 11:49:43 -0700819 Vector<EventEntry*> mTempCancelationEvents;
820
821 void dispatchOnceInnerLocked(nsecs_t keyRepeatTimeout, nsecs_t keyRepeatDelay,
822 nsecs_t* nextWakeupTime);
823
Jeff Brown59abe7e2010-09-13 23:17:30 -0700824 // Enqueues an inbound event. Returns true if mLooper->wake() should be called.
Jeff Browna665ca82010-09-08 11:49:43 -0700825 bool enqueueInboundEventLocked(EventEntry* entry);
826
827 // App switch latency optimization.
828 nsecs_t mAppSwitchDueTime;
829
830 static bool isAppSwitchKey(int32_t keyCode);
831 bool isAppSwitchPendingLocked();
832 bool detectPendingAppSwitchLocked(KeyEntry* inboundKeyEntry);
833 void resetPendingAppSwitchLocked(bool handled);
834
Jeff Browne839a582010-04-22 18:58:52 -0700835 // All registered connections mapped by receive pipe file descriptor.
836 KeyedVector<int, sp<Connection> > mConnectionsByReceiveFd;
837
Jeff Brown53a415e2010-09-15 15:18:56 -0700838 ssize_t getConnectionIndexLocked(const sp<InputChannel>& inputChannel);
Jeff Brown0cacb872010-08-17 15:59:26 -0700839
Jeff Browne839a582010-04-22 18:58:52 -0700840 // Active connections are connections that have a non-empty outbound queue.
Jeff Brown51d45a72010-06-17 20:52:56 -0700841 // We don't use a ref-counted pointer here because we explicitly abort connections
842 // during unregistration which causes the connection's outbound queue to be cleared
843 // and the connection itself to be deactivated.
Jeff Browne839a582010-04-22 18:58:52 -0700844 Vector<Connection*> mActiveConnections;
845
Jeff Browna665ca82010-09-08 11:49:43 -0700846 // Input channels that will receive a copy of all input events.
847 Vector<sp<InputChannel> > mMonitoringChannels;
Jeff Browne839a582010-04-22 18:58:52 -0700848
Jeff Browna665ca82010-09-08 11:49:43 -0700849 // Preallocated key event object used for policy inquiries.
850 KeyEvent mReusableKeyEvent;
Jeff Browne839a582010-04-22 18:58:52 -0700851
Jeff Brown51d45a72010-06-17 20:52:56 -0700852 // Event injection and synchronization.
853 Condition mInjectionResultAvailableCondition;
Jeff Browna665ca82010-09-08 11:49:43 -0700854 EventEntry* createEntryFromInjectedInputEventLocked(const InputEvent* event);
Jeff Brown51d45a72010-06-17 20:52:56 -0700855 void setInjectionResultLocked(EventEntry* entry, int32_t injectionResult);
856
Jeff Brownf67c53e2010-07-28 15:48:59 -0700857 Condition mInjectionSyncFinishedCondition;
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700858 void incrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brown53a415e2010-09-15 15:18:56 -0700859 void decrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brownf67c53e2010-07-28 15:48:59 -0700860
Jeff Brown542412c2010-08-18 15:51:08 -0700861 // Throttling state.
862 struct ThrottleState {
863 nsecs_t minTimeBetweenEvents;
864
865 nsecs_t lastEventTime;
866 int32_t lastDeviceId;
867 uint32_t lastSource;
868
869 uint32_t originalSampleCount; // only collected during debugging
870 } mThrottleState;
871
Jeff Browne839a582010-04-22 18:58:52 -0700872 // Key repeat tracking.
Jeff Browne839a582010-04-22 18:58:52 -0700873 struct KeyRepeatState {
874 KeyEntry* lastKeyEntry; // or null if no repeat
875 nsecs_t nextRepeatTime;
876 } mKeyRepeatState;
877
878 void resetKeyRepeatLocked();
Jeff Browna665ca82010-09-08 11:49:43 -0700879 KeyEntry* synthesizeKeyRepeatLocked(nsecs_t currentTime, nsecs_t keyRepeatTimeout);
Jeff Browne839a582010-04-22 18:58:52 -0700880
Jeff Brown54bc2812010-06-15 01:31:58 -0700881 // Deferred command processing.
882 bool runCommandsLockedInterruptible();
883 CommandEntry* postCommandLocked(Command command);
884
Jeff Browna665ca82010-09-08 11:49:43 -0700885 // Inbound event processing.
886 void drainInboundQueueLocked();
Jeff Brownd8816c32010-09-16 14:07:33 -0700887 void releasePendingEventLocked();
888 void releaseInboundEventLocked(EventEntry* entry);
Jeff Browna665ca82010-09-08 11:49:43 -0700889 bool isEventFromReliableSourceLocked(EventEntry* entry);
Jeff Browne839a582010-04-22 18:58:52 -0700890
Jeff Browna665ca82010-09-08 11:49:43 -0700891 // Dispatch state.
892 bool mDispatchEnabled;
893 bool mDispatchFrozen;
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700894
Jeff Browna665ca82010-09-08 11:49:43 -0700895 Vector<InputWindow> mWindows;
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700896
897 const InputWindow* getWindowLocked(const sp<InputChannel>& inputChannel);
Jeff Browna665ca82010-09-08 11:49:43 -0700898
899 // Focus tracking for keys, trackball, etc.
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700900 const InputWindow* mFocusedWindow;
Jeff Browna665ca82010-09-08 11:49:43 -0700901
902 // Focus tracking for touch.
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700903 struct TouchedWindow {
904 const InputWindow* window;
905 int32_t targetFlags;
906 BitSet32 pointerIds;
907 sp<InputChannel> channel;
Jeff Browna665ca82010-09-08 11:49:43 -0700908 };
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700909 struct TouchState {
910 bool down;
911 bool split;
912 Vector<TouchedWindow> windows;
913
914 TouchState();
915 ~TouchState();
916 void reset();
917 void copyFrom(const TouchState& other);
918 void addOrUpdateWindow(const InputWindow* window, int32_t targetFlags, BitSet32 pointerIds);
919 void removeOutsideTouchWindows();
920 const InputWindow* getFirstForegroundWindow();
921 };
922
923 TouchState mTouchState;
924 TouchState mTempTouchState;
Jeff Browna665ca82010-09-08 11:49:43 -0700925
926 // Focused application.
927 InputApplication* mFocusedApplication;
928 InputApplication mFocusedApplicationStorage; // preallocated storage for mFocusedApplication
929 void releaseFocusedApplicationLocked();
930
931 // Dispatch inbound events.
932 bool dispatchConfigurationChangedLocked(
933 nsecs_t currentTime, ConfigurationChangedEntry* entry);
934 bool dispatchKeyLocked(
935 nsecs_t currentTime, KeyEntry* entry, nsecs_t keyRepeatTimeout,
Jeff Brownd8816c32010-09-16 14:07:33 -0700936 bool dropEvent, nsecs_t* nextWakeupTime);
Jeff Browna665ca82010-09-08 11:49:43 -0700937 bool dispatchMotionLocked(
938 nsecs_t currentTime, MotionEntry* entry,
Jeff Brownd8816c32010-09-16 14:07:33 -0700939 bool dropEvent, nsecs_t* nextWakeupTime);
Jeff Brown54bc2812010-06-15 01:31:58 -0700940 void dispatchEventToCurrentInputTargetsLocked(
941 nsecs_t currentTime, EventEntry* entry, bool resumeWithAppendedMotionSample);
Jeff Browne839a582010-04-22 18:58:52 -0700942
Jeff Browna665ca82010-09-08 11:49:43 -0700943 void logOutboundKeyDetailsLocked(const char* prefix, const KeyEntry* entry);
944 void logOutboundMotionDetailsLocked(const char* prefix, const MotionEntry* entry);
945
946 // The input targets that were most recently identified for dispatch.
Jeff Browna665ca82010-09-08 11:49:43 -0700947 bool mCurrentInputTargetsValid; // false while targets are being recomputed
948 Vector<InputTarget> mCurrentInputTargets;
Jeff Browna665ca82010-09-08 11:49:43 -0700949
950 enum InputTargetWaitCause {
951 INPUT_TARGET_WAIT_CAUSE_NONE,
952 INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY,
953 INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY,
954 };
955
956 InputTargetWaitCause mInputTargetWaitCause;
957 nsecs_t mInputTargetWaitStartTime;
958 nsecs_t mInputTargetWaitTimeoutTime;
959 bool mInputTargetWaitTimeoutExpired;
960
961 // Finding targets for input events.
Jeff Brownd8816c32010-09-16 14:07:33 -0700962 void resetTargetsLocked();
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700963 void commitTargetsLocked();
Jeff Browna665ca82010-09-08 11:49:43 -0700964 int32_t handleTargetsNotReadyLocked(nsecs_t currentTime, const EventEntry* entry,
965 const InputApplication* application, const InputWindow* window,
966 nsecs_t* nextWakeupTime);
Jeff Brown53a415e2010-09-15 15:18:56 -0700967 void resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout,
968 const sp<InputChannel>& inputChannel);
969 nsecs_t getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime);
Jeff Browna665ca82010-09-08 11:49:43 -0700970 void resetANRTimeoutsLocked();
971
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700972 int32_t findFocusedWindowTargetsLocked(nsecs_t currentTime, const EventEntry* entry,
973 nsecs_t* nextWakeupTime);
974 int32_t findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry* entry,
975 nsecs_t* nextWakeupTime);
Jeff Browna665ca82010-09-08 11:49:43 -0700976
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700977 void addWindowTargetLocked(const InputWindow* window, int32_t targetFlags,
978 BitSet32 pointerIds);
Jeff Browna665ca82010-09-08 11:49:43 -0700979 void addMonitoringTargetsLocked();
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700980 bool shouldPokeUserActivityForCurrentInputTargetsLocked();
981 void pokeUserActivityLocked(nsecs_t eventTime, int32_t eventType);
982 bool checkInjectionPermission(const InputWindow* window, const InjectionState* injectionState);
Jeff Brown35cf0e92010-10-05 12:26:23 -0700983 bool isWindowObscuredAtPointLocked(const InputWindow* window, int32_t x, int32_t y) const;
Jeff Brown53a415e2010-09-15 15:18:56 -0700984 bool isWindowFinishedWithPreviousInputLocked(const InputWindow* window);
Jeff Brown53a415e2010-09-15 15:18:56 -0700985 String8 getApplicationWindowLabelLocked(const InputApplication* application,
986 const InputWindow* window);
Jeff Browna665ca82010-09-08 11:49:43 -0700987
Jeff Browne839a582010-04-22 18:58:52 -0700988 // Manage the dispatch cycle for a single connection.
Jeff Brown51d45a72010-06-17 20:52:56 -0700989 // These methods are deliberately not Interruptible because doing all of the work
990 // with the mutex held makes it easier to ensure that connection invariants are maintained.
991 // If needed, the methods post commands to run later once the critical bits are done.
992 void prepareDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Jeff Browne839a582010-04-22 18:58:52 -0700993 EventEntry* eventEntry, const InputTarget* inputTarget,
994 bool resumeWithAppendedMotionSample);
Jeff Brown53a415e2010-09-15 15:18:56 -0700995 void startDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown51d45a72010-06-17 20:52:56 -0700996 void finishDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Browna665ca82010-09-08 11:49:43 -0700997 void startNextDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown51d45a72010-06-17 20:52:56 -0700998 void abortDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Jeff Browne839a582010-04-22 18:58:52 -0700999 bool broken);
Jeff Brown53a415e2010-09-15 15:18:56 -07001000 void drainOutboundQueueLocked(Connection* connection);
Jeff Brown59abe7e2010-09-13 23:17:30 -07001001 static int handleReceiveCallback(int receiveFd, int events, void* data);
Jeff Browne839a582010-04-22 18:58:52 -07001002
Jeff Brownd1b0a2b2010-09-26 22:20:12 -07001003 // Splitting motion events across windows.
1004 MotionEntry* splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds);
1005
Jeff Browna665ca82010-09-08 11:49:43 -07001006 // Dump state.
1007 void dumpDispatchStateLocked(String8& dump);
1008 void logDispatchStateLocked();
1009
Jeff Browne839a582010-04-22 18:58:52 -07001010 // Add or remove a connection to the mActiveConnections vector.
1011 void activateConnectionLocked(Connection* connection);
1012 void deactivateConnectionLocked(Connection* connection);
1013
1014 // Interesting events that we might like to log or tell the framework about.
Jeff Brown54bc2812010-06-15 01:31:58 -07001015 void onDispatchCycleStartedLocked(
Jeff Brown51d45a72010-06-17 20:52:56 -07001016 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown54bc2812010-06-15 01:31:58 -07001017 void onDispatchCycleFinishedLocked(
Jeff Brown51d45a72010-06-17 20:52:56 -07001018 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown54bc2812010-06-15 01:31:58 -07001019 void onDispatchCycleBrokenLocked(
Jeff Brown51d45a72010-06-17 20:52:56 -07001020 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown53a415e2010-09-15 15:18:56 -07001021 void onANRLocked(
1022 nsecs_t currentTime, const InputApplication* application, const InputWindow* window,
1023 nsecs_t eventTime, nsecs_t waitStartTime);
Jeff Brown54bc2812010-06-15 01:31:58 -07001024
Jeff Brown51d45a72010-06-17 20:52:56 -07001025 // Outbound policy interactions.
Jeff Browna665ca82010-09-08 11:49:43 -07001026 void doNotifyConfigurationChangedInterruptible(CommandEntry* commandEntry);
Jeff Brown54bc2812010-06-15 01:31:58 -07001027 void doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown53a415e2010-09-15 15:18:56 -07001028 void doNotifyANRLockedInterruptible(CommandEntry* commandEntry);
Jeff Browna665ca82010-09-08 11:49:43 -07001029 void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry);
1030 void doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown53a415e2010-09-15 15:18:56 -07001031
1032 // Statistics gathering.
1033 void updateDispatchStatisticsLocked(nsecs_t currentTime, const EventEntry* entry,
1034 int32_t injectionResult, nsecs_t timeSpentWaitingForApplication);
Jeff Browne839a582010-04-22 18:58:52 -07001035};
1036
1037/* Enqueues and dispatches input events, endlessly. */
1038class InputDispatcherThread : public Thread {
1039public:
1040 explicit InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher);
1041 ~InputDispatcherThread();
1042
1043private:
1044 virtual bool threadLoop();
1045
1046 sp<InputDispatcherInterface> mDispatcher;
1047};
1048
1049} // namespace android
1050
Jeff Browna665ca82010-09-08 11:49:43 -07001051#endif // _UI_INPUT_DISPATCHER_H