blob: 96b4faedb03d9035f9be9c7888ae7cd749c3fa07 [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>
30
31#include <stddef.h>
32#include <unistd.h>
Jeff Browna665ca82010-09-08 11:49:43 -070033#include <limits.h>
Jeff Browne839a582010-04-22 18:58:52 -070034
35
36namespace android {
37
Jeff Brown54bc2812010-06-15 01:31:58 -070038/*
Jeff Brown51d45a72010-06-17 20:52:56 -070039 * Constants used to report the outcome of input event injection.
40 */
41enum {
42 /* (INTERNAL USE ONLY) Specifies that injection is pending and its outcome is unknown. */
43 INPUT_EVENT_INJECTION_PENDING = -1,
44
45 /* Injection succeeded. */
46 INPUT_EVENT_INJECTION_SUCCEEDED = 0,
47
48 /* Injection failed because the injector did not have permission to inject
49 * into the application with input focus. */
50 INPUT_EVENT_INJECTION_PERMISSION_DENIED = 1,
51
52 /* Injection failed because there were no available input targets. */
53 INPUT_EVENT_INJECTION_FAILED = 2,
54
55 /* Injection failed due to a timeout. */
56 INPUT_EVENT_INJECTION_TIMED_OUT = 3
57};
58
Jeff Brownf67c53e2010-07-28 15:48:59 -070059/*
60 * Constants used to determine the input event injection synchronization mode.
61 */
62enum {
63 /* Injection is asynchronous and is assumed always to be successful. */
64 INPUT_EVENT_INJECTION_SYNC_NONE = 0,
65
66 /* Waits for previous events to be dispatched so that the input dispatcher can determine
67 * whether input event injection willbe permitted based on the current input focus.
68 * Does not wait for the input event to finish processing. */
69 INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT = 1,
70
71 /* Waits for the input event to be completely processed. */
72 INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISHED = 2,
73};
74
Jeff Brown51d45a72010-06-17 20:52:56 -070075
76/*
Jeff Brown54bc2812010-06-15 01:31:58 -070077 * An input target specifies how an input event is to be dispatched to a particular window
78 * including the window's input channel, control flags, a timeout, and an X / Y offset to
79 * be added to input event coordinates to compensate for the absolute position of the
80 * window area.
81 */
82struct InputTarget {
83 enum {
Jeff Brown53a415e2010-09-15 15:18:56 -070084 /* This flag indicates that the event is being delivered to a foreground application. */
85 FLAG_FOREGROUND = 0x01,
Jeff Brown54bc2812010-06-15 01:31:58 -070086
Jeff Brownaf30ff62010-09-01 17:01:00 -070087 /* This flag indicates that a MotionEvent with AMOTION_EVENT_ACTION_DOWN falls outside
88 * of the area of this target and so should instead be delivered as an
89 * AMOTION_EVENT_ACTION_OUTSIDE to this target. */
Jeff Brown54bc2812010-06-15 01:31:58 -070090 FLAG_OUTSIDE = 0x02,
91
92 /* This flag indicates that a KeyEvent or MotionEvent is being canceled.
Jeff Brownaf30ff62010-09-01 17:01:00 -070093 * In the case of a key event, it should be delivered with flag
94 * AKEY_EVENT_FLAG_CANCELED set.
95 * In the case of a motion event, it should be delivered with action
96 * AMOTION_EVENT_ACTION_CANCEL instead. */
97 FLAG_CANCEL = 0x04,
98
99 /* This flag indicates that the target of a MotionEvent is partly or wholly
100 * obscured by another visible window above it. The motion event should be
101 * delivered with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED. */
102 FLAG_WINDOW_IS_OBSCURED = 0x08,
Jeff Brown54bc2812010-06-15 01:31:58 -0700103 };
104
105 // The input channel to be targeted.
106 sp<InputChannel> inputChannel;
107
108 // Flags for the input target.
109 int32_t flags;
110
Jeff Brown54bc2812010-06-15 01:31:58 -0700111 // The x and y offset to add to a MotionEvent as it is delivered.
112 // (ignored for KeyEvents)
113 float xOffset, yOffset;
114};
115
Jeff Brown51d45a72010-06-17 20:52:56 -0700116
Jeff Brown54bc2812010-06-15 01:31:58 -0700117/*
Jeff Browna665ca82010-09-08 11:49:43 -0700118 * An input window describes the bounds of a window that can receive input.
119 */
120struct InputWindow {
121 // Window flags from WindowManager.LayoutParams
122 enum {
123 FLAG_ALLOW_LOCK_WHILE_SCREEN_ON = 0x00000001,
124 FLAG_DIM_BEHIND = 0x00000002,
125 FLAG_BLUR_BEHIND = 0x00000004,
126 FLAG_NOT_FOCUSABLE = 0x00000008,
127 FLAG_NOT_TOUCHABLE = 0x00000010,
128 FLAG_NOT_TOUCH_MODAL = 0x00000020,
129 FLAG_TOUCHABLE_WHEN_WAKING = 0x00000040,
130 FLAG_KEEP_SCREEN_ON = 0x00000080,
131 FLAG_LAYOUT_IN_SCREEN = 0x00000100,
132 FLAG_LAYOUT_NO_LIMITS = 0x00000200,
133 FLAG_FULLSCREEN = 0x00000400,
134 FLAG_FORCE_NOT_FULLSCREEN = 0x00000800,
135 FLAG_DITHER = 0x00001000,
136 FLAG_SECURE = 0x00002000,
137 FLAG_SCALED = 0x00004000,
138 FLAG_IGNORE_CHEEK_PRESSES = 0x00008000,
139 FLAG_LAYOUT_INSET_DECOR = 0x00010000,
140 FLAG_ALT_FOCUSABLE_IM = 0x00020000,
141 FLAG_WATCH_OUTSIDE_TOUCH = 0x00040000,
142 FLAG_SHOW_WHEN_LOCKED = 0x00080000,
143 FLAG_SHOW_WALLPAPER = 0x00100000,
144 FLAG_TURN_SCREEN_ON = 0x00200000,
145 FLAG_DISMISS_KEYGUARD = 0x00400000,
146 FLAG_IMMERSIVE = 0x00800000,
147 FLAG_KEEP_SURFACE_WHILE_ANIMATING = 0x10000000,
148 FLAG_COMPATIBLE_WINDOW = 0x20000000,
149 FLAG_SYSTEM_ERROR = 0x40000000,
150 };
151
152 // Window types from WindowManager.LayoutParams
153 enum {
154 FIRST_APPLICATION_WINDOW = 1,
155 TYPE_BASE_APPLICATION = 1,
156 TYPE_APPLICATION = 2,
157 TYPE_APPLICATION_STARTING = 3,
158 LAST_APPLICATION_WINDOW = 99,
159 FIRST_SUB_WINDOW = 1000,
160 TYPE_APPLICATION_PANEL = FIRST_SUB_WINDOW,
161 TYPE_APPLICATION_MEDIA = FIRST_SUB_WINDOW+1,
162 TYPE_APPLICATION_SUB_PANEL = FIRST_SUB_WINDOW+2,
163 TYPE_APPLICATION_ATTACHED_DIALOG = FIRST_SUB_WINDOW+3,
164 TYPE_APPLICATION_MEDIA_OVERLAY = FIRST_SUB_WINDOW+4,
165 LAST_SUB_WINDOW = 1999,
166 FIRST_SYSTEM_WINDOW = 2000,
167 TYPE_STATUS_BAR = FIRST_SYSTEM_WINDOW,
168 TYPE_SEARCH_BAR = FIRST_SYSTEM_WINDOW+1,
169 TYPE_PHONE = FIRST_SYSTEM_WINDOW+2,
170 TYPE_SYSTEM_ALERT = FIRST_SYSTEM_WINDOW+3,
171 TYPE_KEYGUARD = FIRST_SYSTEM_WINDOW+4,
172 TYPE_TOAST = FIRST_SYSTEM_WINDOW+5,
173 TYPE_SYSTEM_OVERLAY = FIRST_SYSTEM_WINDOW+6,
174 TYPE_PRIORITY_PHONE = FIRST_SYSTEM_WINDOW+7,
175 TYPE_SYSTEM_DIALOG = FIRST_SYSTEM_WINDOW+8,
176 TYPE_KEYGUARD_DIALOG = FIRST_SYSTEM_WINDOW+9,
177 TYPE_SYSTEM_ERROR = FIRST_SYSTEM_WINDOW+10,
178 TYPE_INPUT_METHOD = FIRST_SYSTEM_WINDOW+11,
179 TYPE_INPUT_METHOD_DIALOG= FIRST_SYSTEM_WINDOW+12,
180 TYPE_WALLPAPER = FIRST_SYSTEM_WINDOW+13,
181 TYPE_STATUS_BAR_PANEL = FIRST_SYSTEM_WINDOW+14,
182 LAST_SYSTEM_WINDOW = 2999,
183 };
184
185 sp<InputChannel> inputChannel;
Jeff Brown53a415e2010-09-15 15:18:56 -0700186 String8 name;
Jeff Browna665ca82010-09-08 11:49:43 -0700187 int32_t layoutParamsFlags;
188 int32_t layoutParamsType;
189 nsecs_t dispatchingTimeout;
190 int32_t frameLeft;
191 int32_t frameTop;
192 int32_t frameRight;
193 int32_t frameBottom;
194 int32_t visibleFrameLeft;
195 int32_t visibleFrameTop;
196 int32_t visibleFrameRight;
197 int32_t visibleFrameBottom;
198 int32_t touchableAreaLeft;
199 int32_t touchableAreaTop;
200 int32_t touchableAreaRight;
201 int32_t touchableAreaBottom;
202 bool visible;
Jeff Brown53a415e2010-09-15 15:18:56 -0700203 bool canReceiveKeys;
Jeff Browna665ca82010-09-08 11:49:43 -0700204 bool hasFocus;
205 bool hasWallpaper;
206 bool paused;
Jeff Brown53a415e2010-09-15 15:18:56 -0700207 int32_t layer;
Jeff Browna665ca82010-09-08 11:49:43 -0700208 int32_t ownerPid;
209 int32_t ownerUid;
210
211 bool visibleFrameIntersects(const InputWindow* other) const;
212 bool touchableAreaContainsPoint(int32_t x, int32_t y) const;
213};
214
215
216/*
217 * A private handle type used by the input manager to track the window.
218 */
219class InputApplicationHandle : public RefBase {
220protected:
221 InputApplicationHandle() { }
222 virtual ~InputApplicationHandle() { }
223};
224
225
226/*
227 * An input application describes properties of an application that can receive input.
228 */
229struct InputApplication {
230 String8 name;
231 nsecs_t dispatchingTimeout;
232 sp<InputApplicationHandle> handle;
233};
234
235
236/*
Jeff Brown54bc2812010-06-15 01:31:58 -0700237 * Input dispatcher policy interface.
238 *
239 * The input reader policy is used by the input reader to interact with the Window Manager
240 * and other system components.
241 *
242 * The actual implementation is partially supported by callbacks into the DVM
243 * via JNI. This interface is also mocked in the unit tests.
244 */
245class InputDispatcherPolicyInterface : public virtual RefBase {
246protected:
247 InputDispatcherPolicyInterface() { }
248 virtual ~InputDispatcherPolicyInterface() { }
249
250public:
251 /* Notifies the system that a configuration change has occurred. */
252 virtual void notifyConfigurationChanged(nsecs_t when) = 0;
253
Jeff Browna665ca82010-09-08 11:49:43 -0700254 /* Notifies the system that an application is not responding.
255 * Returns a new timeout to continue waiting, or 0 to abort dispatch. */
Jeff Brown53a415e2010-09-15 15:18:56 -0700256 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
257 const sp<InputChannel>& inputChannel) = 0;
Jeff Browna665ca82010-09-08 11:49:43 -0700258
Jeff Brown54bc2812010-06-15 01:31:58 -0700259 /* Notifies the system that an input channel is unrecoverably broken. */
260 virtual void notifyInputChannelBroken(const sp<InputChannel>& inputChannel) = 0;
261
Jeff Brown61ce3982010-09-07 10:44:57 -0700262 /* Gets the key repeat initial timeout or -1 if automatic key repeating is disabled. */
Jeff Brown54bc2812010-06-15 01:31:58 -0700263 virtual nsecs_t getKeyRepeatTimeout() = 0;
264
Jeff Brown61ce3982010-09-07 10:44:57 -0700265 /* Gets the key repeat inter-key delay. */
266 virtual nsecs_t getKeyRepeatDelay() = 0;
267
Jeff Brown542412c2010-08-18 15:51:08 -0700268 /* Gets the maximum suggested event delivery rate per second.
269 * This value is used to throttle motion event movement actions on a per-device
270 * basis. It is not intended to be a hard limit.
271 */
272 virtual int32_t getMaxEventsPerSecond() = 0;
Jeff Browna665ca82010-09-08 11:49:43 -0700273
274 /* Allows the policy a chance to intercept a key before dispatching. */
275 virtual bool interceptKeyBeforeDispatching(const sp<InputChannel>& inputChannel,
276 const KeyEvent* keyEvent, uint32_t policyFlags) = 0;
277
278 /* Poke user activity for an event dispatched to a window. */
279 virtual void pokeUserActivity(nsecs_t eventTime, int32_t windowType, int32_t eventType) = 0;
280
281 /* Checks whether a given application pid/uid has permission to inject input events
282 * into other applications.
283 *
284 * This method is special in that its implementation promises to be non-reentrant and
285 * is safe to call while holding other locks. (Most other methods make no such guarantees!)
286 */
287 virtual bool checkInjectEventsPermissionNonReentrant(
288 int32_t injectorPid, int32_t injectorUid) = 0;
Jeff Brown54bc2812010-06-15 01:31:58 -0700289};
290
291
Jeff Browne839a582010-04-22 18:58:52 -0700292/* Notifies the system about input events generated by the input reader.
293 * The dispatcher is expected to be mostly asynchronous. */
294class InputDispatcherInterface : public virtual RefBase {
295protected:
296 InputDispatcherInterface() { }
297 virtual ~InputDispatcherInterface() { }
298
299public:
Jeff Browna665ca82010-09-08 11:49:43 -0700300 /* Dumps the state of the input dispatcher.
301 *
302 * This method may be called on any thread (usually by the input manager). */
303 virtual void dump(String8& dump) = 0;
304
Jeff Browne839a582010-04-22 18:58:52 -0700305 /* Runs a single iteration of the dispatch loop.
306 * Nominally processes one queued event, a timeout, or a response from an input consumer.
307 *
308 * This method should only be called on the input dispatcher thread.
309 */
310 virtual void dispatchOnce() = 0;
311
312 /* Notifies the dispatcher about new events.
Jeff Browne839a582010-04-22 18:58:52 -0700313 *
314 * These methods should only be called on the input reader thread.
315 */
Jeff Brown54bc2812010-06-15 01:31:58 -0700316 virtual void notifyConfigurationChanged(nsecs_t eventTime) = 0;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700317 virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Browne839a582010-04-22 18:58:52 -0700318 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode,
319 int32_t scanCode, int32_t metaState, nsecs_t downTime) = 0;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700320 virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Brownaf30ff62010-09-01 17:01:00 -0700321 uint32_t policyFlags, int32_t action, int32_t flags,
322 int32_t metaState, int32_t edgeFlags,
Jeff Browne839a582010-04-22 18:58:52 -0700323 uint32_t pointerCount, const int32_t* pointerIds, const PointerCoords* pointerCoords,
324 float xPrecision, float yPrecision, nsecs_t downTime) = 0;
325
Jeff Brown51d45a72010-06-17 20:52:56 -0700326 /* Injects an input event and optionally waits for sync.
Jeff Brownf67c53e2010-07-28 15:48:59 -0700327 * The synchronization mode determines whether the method blocks while waiting for
328 * input injection to proceed.
Jeff Brown51d45a72010-06-17 20:52:56 -0700329 * Returns one of the INPUT_EVENT_INJECTION_XXX constants.
330 *
331 * This method may be called on any thread (usually by the input manager).
332 */
333 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brownf67c53e2010-07-28 15:48:59 -0700334 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis) = 0;
Jeff Brown51d45a72010-06-17 20:52:56 -0700335
Jeff Browna665ca82010-09-08 11:49:43 -0700336 /* Sets the list of input windows.
337 *
338 * This method may be called on any thread (usually by the input manager).
339 */
340 virtual void setInputWindows(const Vector<InputWindow>& inputWindows) = 0;
341
342 /* Sets the focused application.
343 *
344 * This method may be called on any thread (usually by the input manager).
345 */
346 virtual void setFocusedApplication(const InputApplication* inputApplication) = 0;
347
348 /* Sets the input dispatching mode.
349 *
350 * This method may be called on any thread (usually by the input manager).
351 */
352 virtual void setInputDispatchMode(bool enabled, bool frozen) = 0;
353
Jeff Browne839a582010-04-22 18:58:52 -0700354 /* Registers or unregister input channels that may be used as targets for input events.
Jeff Browna665ca82010-09-08 11:49:43 -0700355 * If monitor is true, the channel will receive a copy of all input events.
Jeff Browne839a582010-04-22 18:58:52 -0700356 *
357 * These methods may be called on any thread (usually by the input manager).
358 */
Jeff Browna665ca82010-09-08 11:49:43 -0700359 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel, bool monitor) = 0;
Jeff Browne839a582010-04-22 18:58:52 -0700360 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) = 0;
361};
362
Jeff Brown54bc2812010-06-15 01:31:58 -0700363/* Dispatches events to input targets. Some functions of the input dispatcher, such as
364 * identifying input targets, are controlled by a separate policy object.
365 *
366 * IMPORTANT INVARIANT:
367 * Because the policy can potentially block or cause re-entrance into the input dispatcher,
368 * the input dispatcher never calls into the policy while holding its internal locks.
369 * The implementation is also carefully designed to recover from scenarios such as an
370 * input channel becoming unregistered while identifying input targets or processing timeouts.
371 *
372 * Methods marked 'Locked' must be called with the lock acquired.
373 *
374 * Methods marked 'LockedInterruptible' must be called with the lock acquired but
375 * may during the course of their execution release the lock, call into the policy, and
376 * then reacquire the lock. The caller is responsible for recovering gracefully.
377 *
378 * A 'LockedInterruptible' method may called a 'Locked' method, but NOT vice-versa.
379 */
Jeff Browne839a582010-04-22 18:58:52 -0700380class InputDispatcher : public InputDispatcherInterface {
381protected:
382 virtual ~InputDispatcher();
383
384public:
Jeff Brown54bc2812010-06-15 01:31:58 -0700385 explicit InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy);
Jeff Browne839a582010-04-22 18:58:52 -0700386
Jeff Browna665ca82010-09-08 11:49:43 -0700387 virtual void dump(String8& dump);
388
Jeff Browne839a582010-04-22 18:58:52 -0700389 virtual void dispatchOnce();
390
Jeff Brown54bc2812010-06-15 01:31:58 -0700391 virtual void notifyConfigurationChanged(nsecs_t eventTime);
Jeff Brown5c1ed842010-07-14 18:48:53 -0700392 virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Browne839a582010-04-22 18:58:52 -0700393 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode,
394 int32_t scanCode, int32_t metaState, nsecs_t downTime);
Jeff Brown5c1ed842010-07-14 18:48:53 -0700395 virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Brownaf30ff62010-09-01 17:01:00 -0700396 uint32_t policyFlags, int32_t action, int32_t flags,
397 int32_t metaState, int32_t edgeFlags,
Jeff Browne839a582010-04-22 18:58:52 -0700398 uint32_t pointerCount, const int32_t* pointerIds, const PointerCoords* pointerCoords,
399 float xPrecision, float yPrecision, nsecs_t downTime);
400
Jeff Brown51d45a72010-06-17 20:52:56 -0700401 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brownf67c53e2010-07-28 15:48:59 -0700402 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis);
Jeff Brown51d45a72010-06-17 20:52:56 -0700403
Jeff Browna665ca82010-09-08 11:49:43 -0700404 virtual void setInputWindows(const Vector<InputWindow>& inputWindows);
405 virtual void setFocusedApplication(const InputApplication* inputApplication);
406 virtual void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown50de30a2010-06-22 01:27:15 -0700407
Jeff Browna665ca82010-09-08 11:49:43 -0700408 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel, bool monitor);
Jeff Browne839a582010-04-22 18:58:52 -0700409 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel);
410
411private:
412 template <typename T>
413 struct Link {
414 T* next;
415 T* prev;
416 };
417
418 struct EventEntry : Link<EventEntry> {
419 enum {
420 TYPE_SENTINEL,
421 TYPE_CONFIGURATION_CHANGED,
422 TYPE_KEY,
423 TYPE_MOTION
424 };
425
426 int32_t refCount;
427 int32_t type;
428 nsecs_t eventTime;
Jeff Brown54bc2812010-06-15 01:31:58 -0700429
Jeff Brownf67c53e2010-07-28 15:48:59 -0700430 int32_t injectionResult; // initially INPUT_EVENT_INJECTION_PENDING
431 bool injectionIsAsync; // set to true if injection is not waiting for the result
432 int32_t injectorPid; // -1 if not injected
433 int32_t injectorUid; // -1 if not injected
Jeff Brown51d45a72010-06-17 20:52:56 -0700434
Jeff Brown54bc2812010-06-15 01:31:58 -0700435 bool dispatchInProgress; // initially false, set to true while dispatching
Jeff Brown53a415e2010-09-15 15:18:56 -0700436 int32_t pendingForegroundDispatches; // the number of foreground dispatches in progress
Jeff Brown51d45a72010-06-17 20:52:56 -0700437
438 inline bool isInjected() { return injectorPid >= 0; }
Jeff Browna665ca82010-09-08 11:49:43 -0700439
440 void recycle();
Jeff Browne839a582010-04-22 18:58:52 -0700441 };
442
443 struct ConfigurationChangedEntry : EventEntry {
Jeff Browne839a582010-04-22 18:58:52 -0700444 };
445
446 struct KeyEntry : EventEntry {
447 int32_t deviceId;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700448 int32_t source;
Jeff Browne839a582010-04-22 18:58:52 -0700449 uint32_t policyFlags;
450 int32_t action;
451 int32_t flags;
452 int32_t keyCode;
453 int32_t scanCode;
454 int32_t metaState;
455 int32_t repeatCount;
456 nsecs_t downTime;
Jeff Browna665ca82010-09-08 11:49:43 -0700457
458 bool syntheticRepeat; // set to true for synthetic key repeats
459
460 enum InterceptKeyResult {
461 INTERCEPT_KEY_RESULT_UNKNOWN,
462 INTERCEPT_KEY_RESULT_SKIP,
463 INTERCEPT_KEY_RESULT_CONTINUE,
464 };
465 InterceptKeyResult interceptKeyResult; // set based on the interception result
466
467 void recycle();
Jeff Browne839a582010-04-22 18:58:52 -0700468 };
469
470 struct MotionSample {
471 MotionSample* next;
472
473 nsecs_t eventTime;
474 PointerCoords pointerCoords[MAX_POINTERS];
475 };
476
477 struct MotionEntry : EventEntry {
478 int32_t deviceId;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700479 int32_t source;
Jeff Browne839a582010-04-22 18:58:52 -0700480 uint32_t policyFlags;
481 int32_t action;
Jeff Brownaf30ff62010-09-01 17:01:00 -0700482 int32_t flags;
Jeff Browne839a582010-04-22 18:58:52 -0700483 int32_t metaState;
484 int32_t edgeFlags;
485 float xPrecision;
486 float yPrecision;
487 nsecs_t downTime;
488 uint32_t pointerCount;
489 int32_t pointerIds[MAX_POINTERS];
490
491 // Linked list of motion samples associated with this motion event.
492 MotionSample firstSample;
493 MotionSample* lastSample;
Jeff Brown542412c2010-08-18 15:51:08 -0700494
495 uint32_t countSamples() const;
Jeff Browne839a582010-04-22 18:58:52 -0700496 };
497
Jeff Brown54bc2812010-06-15 01:31:58 -0700498 // Tracks the progress of dispatching a particular event to a particular connection.
Jeff Browne839a582010-04-22 18:58:52 -0700499 struct DispatchEntry : Link<DispatchEntry> {
500 EventEntry* eventEntry; // the event to dispatch
501 int32_t targetFlags;
502 float xOffset;
503 float yOffset;
Jeff Browne839a582010-04-22 18:58:52 -0700504
505 // True if dispatch has started.
506 bool inProgress;
507
508 // For motion events:
509 // Pointer to the first motion sample to dispatch in this cycle.
510 // Usually NULL to indicate that the list of motion samples begins at
511 // MotionEntry::firstSample. Otherwise, some samples were dispatched in a previous
512 // cycle and this pointer indicates the location of the first remainining sample
513 // to dispatch during the current cycle.
514 MotionSample* headMotionSample;
515 // Pointer to a motion sample to dispatch in the next cycle if the dispatcher was
516 // unable to send all motion samples during this cycle. On the next cycle,
517 // headMotionSample will be initialized to tailMotionSample and tailMotionSample
518 // will be set to NULL.
519 MotionSample* tailMotionSample;
Jeff Brownf67c53e2010-07-28 15:48:59 -0700520
Jeff Brown53a415e2010-09-15 15:18:56 -0700521 inline bool hasForegroundTarget() const {
522 return targetFlags & InputTarget::FLAG_FOREGROUND;
Jeff Browna665ca82010-09-08 11:49:43 -0700523 }
Jeff Browne839a582010-04-22 18:58:52 -0700524 };
525
Jeff Brown54bc2812010-06-15 01:31:58 -0700526 // A command entry captures state and behavior for an action to be performed in the
527 // dispatch loop after the initial processing has taken place. It is essentially
528 // a kind of continuation used to postpone sensitive policy interactions to a point
529 // in the dispatch loop where it is safe to release the lock (generally after finishing
530 // the critical parts of the dispatch cycle).
531 //
532 // The special thing about commands is that they can voluntarily release and reacquire
533 // the dispatcher lock at will. Initially when the command starts running, the
534 // dispatcher lock is held. However, if the command needs to call into the policy to
535 // do some work, it can release the lock, do the work, then reacquire the lock again
536 // before returning.
537 //
538 // This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch
539 // never calls into the policy while holding its lock.
540 //
541 // Commands are implicitly 'LockedInterruptible'.
542 struct CommandEntry;
543 typedef void (InputDispatcher::*Command)(CommandEntry* commandEntry);
544
Jeff Brown51d45a72010-06-17 20:52:56 -0700545 class Connection;
Jeff Brown54bc2812010-06-15 01:31:58 -0700546 struct CommandEntry : Link<CommandEntry> {
547 CommandEntry();
548 ~CommandEntry();
549
550 Command command;
551
552 // parameters for the command (usage varies by command)
Jeff Brown51d45a72010-06-17 20:52:56 -0700553 sp<Connection> connection;
Jeff Browna665ca82010-09-08 11:49:43 -0700554 nsecs_t eventTime;
555 KeyEntry* keyEntry;
556 sp<InputChannel> inputChannel;
557 sp<InputApplicationHandle> inputApplicationHandle;
558 int32_t windowType;
559 int32_t userActivityEventType;
Jeff Brown54bc2812010-06-15 01:31:58 -0700560 };
561
562 // Generic queue implementation.
Jeff Browne839a582010-04-22 18:58:52 -0700563 template <typename T>
564 struct Queue {
Jeff Browna665ca82010-09-08 11:49:43 -0700565 T headSentinel;
566 T tailSentinel;
Jeff Browne839a582010-04-22 18:58:52 -0700567
568 inline Queue() {
Jeff Browna665ca82010-09-08 11:49:43 -0700569 headSentinel.prev = NULL;
570 headSentinel.next = & tailSentinel;
571 tailSentinel.prev = & headSentinel;
572 tailSentinel.next = NULL;
Jeff Browne839a582010-04-22 18:58:52 -0700573 }
574
Jeff Browna665ca82010-09-08 11:49:43 -0700575 inline bool isEmpty() const {
576 return headSentinel.next == & tailSentinel;
Jeff Browne839a582010-04-22 18:58:52 -0700577 }
578
579 inline void enqueueAtTail(T* entry) {
Jeff Browna665ca82010-09-08 11:49:43 -0700580 T* last = tailSentinel.prev;
Jeff Browne839a582010-04-22 18:58:52 -0700581 last->next = entry;
582 entry->prev = last;
Jeff Browna665ca82010-09-08 11:49:43 -0700583 entry->next = & tailSentinel;
584 tailSentinel.prev = entry;
Jeff Browne839a582010-04-22 18:58:52 -0700585 }
586
587 inline void enqueueAtHead(T* entry) {
Jeff Browna665ca82010-09-08 11:49:43 -0700588 T* first = headSentinel.next;
589 headSentinel.next = entry;
590 entry->prev = & headSentinel;
Jeff Browne839a582010-04-22 18:58:52 -0700591 entry->next = first;
592 first->prev = entry;
593 }
594
595 inline void dequeue(T* entry) {
596 entry->prev->next = entry->next;
597 entry->next->prev = entry->prev;
598 }
599
600 inline T* dequeueAtHead() {
Jeff Browna665ca82010-09-08 11:49:43 -0700601 T* first = headSentinel.next;
Jeff Browne839a582010-04-22 18:58:52 -0700602 dequeue(first);
603 return first;
604 }
Jeff Brown53a415e2010-09-15 15:18:56 -0700605
606 uint32_t count() const;
Jeff Browne839a582010-04-22 18:58:52 -0700607 };
608
609 /* Allocates queue entries and performs reference counting as needed. */
610 class Allocator {
611 public:
612 Allocator();
613
Jeff Brown51d45a72010-06-17 20:52:56 -0700614 ConfigurationChangedEntry* obtainConfigurationChangedEntry(nsecs_t eventTime);
615 KeyEntry* obtainKeyEntry(nsecs_t eventTime,
Jeff Brown5c1ed842010-07-14 18:48:53 -0700616 int32_t deviceId, int32_t source, uint32_t policyFlags, int32_t action,
Jeff Brown51d45a72010-06-17 20:52:56 -0700617 int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState,
618 int32_t repeatCount, nsecs_t downTime);
619 MotionEntry* obtainMotionEntry(nsecs_t eventTime,
Jeff Brown5c1ed842010-07-14 18:48:53 -0700620 int32_t deviceId, int32_t source, uint32_t policyFlags, int32_t action,
Jeff Brownaf30ff62010-09-01 17:01:00 -0700621 int32_t flags, int32_t metaState, int32_t edgeFlags,
622 float xPrecision, float yPrecision,
Jeff Brown51d45a72010-06-17 20:52:56 -0700623 nsecs_t downTime, uint32_t pointerCount,
624 const int32_t* pointerIds, const PointerCoords* pointerCoords);
Jeff Browna665ca82010-09-08 11:49:43 -0700625 DispatchEntry* obtainDispatchEntry(EventEntry* eventEntry,
Jeff Brown53a415e2010-09-15 15:18:56 -0700626 int32_t targetFlags, float xOffset, float yOffset);
Jeff Brown54bc2812010-06-15 01:31:58 -0700627 CommandEntry* obtainCommandEntry(Command command);
Jeff Browne839a582010-04-22 18:58:52 -0700628
629 void releaseEventEntry(EventEntry* entry);
630 void releaseConfigurationChangedEntry(ConfigurationChangedEntry* entry);
631 void releaseKeyEntry(KeyEntry* entry);
632 void releaseMotionEntry(MotionEntry* entry);
633 void releaseDispatchEntry(DispatchEntry* entry);
Jeff Brown54bc2812010-06-15 01:31:58 -0700634 void releaseCommandEntry(CommandEntry* entry);
Jeff Browne839a582010-04-22 18:58:52 -0700635
636 void appendMotionSample(MotionEntry* motionEntry,
Jeff Brown51d45a72010-06-17 20:52:56 -0700637 nsecs_t eventTime, const PointerCoords* pointerCoords);
Jeff Browne839a582010-04-22 18:58:52 -0700638
639 private:
640 Pool<ConfigurationChangedEntry> mConfigurationChangeEntryPool;
641 Pool<KeyEntry> mKeyEntryPool;
642 Pool<MotionEntry> mMotionEntryPool;
643 Pool<MotionSample> mMotionSamplePool;
644 Pool<DispatchEntry> mDispatchEntryPool;
Jeff Brown54bc2812010-06-15 01:31:58 -0700645 Pool<CommandEntry> mCommandEntryPool;
Jeff Brown51d45a72010-06-17 20:52:56 -0700646
647 void initializeEventEntry(EventEntry* entry, int32_t type, nsecs_t eventTime);
Jeff Browne839a582010-04-22 18:58:52 -0700648 };
649
Jeff Browna665ca82010-09-08 11:49:43 -0700650 /* Tracks dispatched key and motion event state so that cancelation events can be
651 * synthesized when events are dropped. */
652 class InputState {
653 public:
654 // Specifies whether a given event will violate input state consistency.
655 enum Consistency {
656 // The event is consistent with the current input state.
657 CONSISTENT,
658 // The event is inconsistent with the current input state but applications
659 // will tolerate it. eg. Down followed by another down.
660 TOLERABLE,
661 // The event is inconsistent with the current input state and will probably
662 // cause applications to crash. eg. Up without prior down, move with
663 // unexpected number of pointers.
664 BROKEN
665 };
666
667 InputState();
668 ~InputState();
669
670 // Returns true if there is no state to be canceled.
671 bool isNeutral() const;
672
673 // Returns true if the input state believes it is out of sync.
674 bool isOutOfSync() const;
675
676 // Sets the input state to be out of sync if it is not neutral.
677 void setOutOfSync();
678
679 // Resets the input state out of sync flag.
680 void resetOutOfSync();
681
682 // Records tracking information for an event that has just been published.
683 // Returns whether the event is consistent with the current input state.
684 Consistency trackEvent(const EventEntry* entry);
685
686 // Records tracking information for a key event that has just been published.
687 // Returns whether the event is consistent with the current input state.
688 Consistency trackKey(const KeyEntry* entry);
689
690 // Records tracking information for a motion event that has just been published.
691 // Returns whether the event is consistent with the current input state.
692 Consistency trackMotion(const MotionEntry* entry);
693
694 // Synthesizes cancelation events for the current state.
695 void synthesizeCancelationEvents(Allocator* allocator,
696 Vector<EventEntry*>& outEvents) const;
697
698 // Clears the current state.
699 void clear();
700
701 private:
702 bool mIsOutOfSync;
703
704 struct KeyMemento {
705 int32_t deviceId;
706 int32_t source;
707 int32_t keyCode;
708 int32_t scanCode;
709 nsecs_t downTime;
710 };
711
712 struct MotionMemento {
713 int32_t deviceId;
714 int32_t source;
715 float xPrecision;
716 float yPrecision;
717 nsecs_t downTime;
718 uint32_t pointerCount;
719 int32_t pointerIds[MAX_POINTERS];
720 PointerCoords pointerCoords[MAX_POINTERS];
721
722 void setPointers(const MotionEntry* entry);
723 };
724
725 Vector<KeyMemento> mKeyMementos;
726 Vector<MotionMemento> mMotionMementos;
727 };
728
Jeff Browne839a582010-04-22 18:58:52 -0700729 /* Manages the dispatch state associated with a single input channel. */
730 class Connection : public RefBase {
731 protected:
732 virtual ~Connection();
733
734 public:
735 enum Status {
736 // Everything is peachy.
737 STATUS_NORMAL,
738 // An unrecoverable communication error has occurred.
739 STATUS_BROKEN,
Jeff Browne839a582010-04-22 18:58:52 -0700740 // The input channel has been unregistered.
741 STATUS_ZOMBIE
742 };
743
744 Status status;
745 sp<InputChannel> inputChannel;
746 InputPublisher inputPublisher;
Jeff Browna665ca82010-09-08 11:49:43 -0700747 InputState inputState;
Jeff Browne839a582010-04-22 18:58:52 -0700748 Queue<DispatchEntry> outboundQueue;
Jeff Browne839a582010-04-22 18:58:52 -0700749
750 nsecs_t lastEventTime; // the time when the event was originally captured
751 nsecs_t lastDispatchTime; // the time when the last event was dispatched
Jeff Browne839a582010-04-22 18:58:52 -0700752
753 explicit Connection(const sp<InputChannel>& inputChannel);
754
Jeff Brown54bc2812010-06-15 01:31:58 -0700755 inline const char* getInputChannelName() const { return inputChannel->getName().string(); }
756
757 const char* getStatusLabel() const;
Jeff Browne839a582010-04-22 18:58:52 -0700758
759 // Finds a DispatchEntry in the outbound queue associated with the specified event.
760 // Returns NULL if not found.
761 DispatchEntry* findQueuedDispatchEntryForEvent(const EventEntry* eventEntry) const;
762
Jeff Browne839a582010-04-22 18:58:52 -0700763 // Gets the time since the current event was originally obtained from the input driver.
Jeff Browna665ca82010-09-08 11:49:43 -0700764 inline double getEventLatencyMillis(nsecs_t currentTime) const {
Jeff Browne839a582010-04-22 18:58:52 -0700765 return (currentTime - lastEventTime) / 1000000.0;
766 }
767
768 // Gets the time since the current event entered the outbound dispatch queue.
Jeff Browna665ca82010-09-08 11:49:43 -0700769 inline double getDispatchLatencyMillis(nsecs_t currentTime) const {
Jeff Browne839a582010-04-22 18:58:52 -0700770 return (currentTime - lastDispatchTime) / 1000000.0;
771 }
772
Jeff Browne839a582010-04-22 18:58:52 -0700773 status_t initialize();
774 };
775
Jeff Brown54bc2812010-06-15 01:31:58 -0700776 sp<InputDispatcherPolicyInterface> mPolicy;
Jeff Browne839a582010-04-22 18:58:52 -0700777
778 Mutex mLock;
779
Jeff Browne839a582010-04-22 18:58:52 -0700780 Allocator mAllocator;
Jeff Brown59abe7e2010-09-13 23:17:30 -0700781 sp<Looper> mLooper;
Jeff Browne839a582010-04-22 18:58:52 -0700782
Jeff Browna665ca82010-09-08 11:49:43 -0700783 EventEntry* mPendingEvent;
Jeff Brown54bc2812010-06-15 01:31:58 -0700784 Queue<EventEntry> mInboundQueue;
785 Queue<CommandEntry> mCommandQueue;
786
Jeff Browna665ca82010-09-08 11:49:43 -0700787 Vector<EventEntry*> mTempCancelationEvents;
788
789 void dispatchOnceInnerLocked(nsecs_t keyRepeatTimeout, nsecs_t keyRepeatDelay,
790 nsecs_t* nextWakeupTime);
791
Jeff Brown59abe7e2010-09-13 23:17:30 -0700792 // Enqueues an inbound event. Returns true if mLooper->wake() should be called.
Jeff Browna665ca82010-09-08 11:49:43 -0700793 bool enqueueInboundEventLocked(EventEntry* entry);
794
795 // App switch latency optimization.
796 nsecs_t mAppSwitchDueTime;
797
798 static bool isAppSwitchKey(int32_t keyCode);
799 bool isAppSwitchPendingLocked();
800 bool detectPendingAppSwitchLocked(KeyEntry* inboundKeyEntry);
801 void resetPendingAppSwitchLocked(bool handled);
802
Jeff Browne839a582010-04-22 18:58:52 -0700803 // All registered connections mapped by receive pipe file descriptor.
804 KeyedVector<int, sp<Connection> > mConnectionsByReceiveFd;
805
Jeff Brown53a415e2010-09-15 15:18:56 -0700806 ssize_t getConnectionIndexLocked(const sp<InputChannel>& inputChannel);
Jeff Brown0cacb872010-08-17 15:59:26 -0700807
Jeff Browne839a582010-04-22 18:58:52 -0700808 // Active connections are connections that have a non-empty outbound queue.
Jeff Brown51d45a72010-06-17 20:52:56 -0700809 // We don't use a ref-counted pointer here because we explicitly abort connections
810 // during unregistration which causes the connection's outbound queue to be cleared
811 // and the connection itself to be deactivated.
Jeff Browne839a582010-04-22 18:58:52 -0700812 Vector<Connection*> mActiveConnections;
813
Jeff Browna665ca82010-09-08 11:49:43 -0700814 // Input channels that will receive a copy of all input events.
815 Vector<sp<InputChannel> > mMonitoringChannels;
Jeff Browne839a582010-04-22 18:58:52 -0700816
Jeff Browna665ca82010-09-08 11:49:43 -0700817 // Preallocated key event object used for policy inquiries.
818 KeyEvent mReusableKeyEvent;
Jeff Browne839a582010-04-22 18:58:52 -0700819
Jeff Brown51d45a72010-06-17 20:52:56 -0700820 // Event injection and synchronization.
821 Condition mInjectionResultAvailableCondition;
Jeff Browna665ca82010-09-08 11:49:43 -0700822 EventEntry* createEntryFromInjectedInputEventLocked(const InputEvent* event);
Jeff Brown51d45a72010-06-17 20:52:56 -0700823 void setInjectionResultLocked(EventEntry* entry, int32_t injectionResult);
824
Jeff Brownf67c53e2010-07-28 15:48:59 -0700825 Condition mInjectionSyncFinishedCondition;
Jeff Brown53a415e2010-09-15 15:18:56 -0700826 void decrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brownf67c53e2010-07-28 15:48:59 -0700827
Jeff Brown542412c2010-08-18 15:51:08 -0700828 // Throttling state.
829 struct ThrottleState {
830 nsecs_t minTimeBetweenEvents;
831
832 nsecs_t lastEventTime;
833 int32_t lastDeviceId;
834 uint32_t lastSource;
835
836 uint32_t originalSampleCount; // only collected during debugging
837 } mThrottleState;
838
Jeff Browne839a582010-04-22 18:58:52 -0700839 // Key repeat tracking.
Jeff Browne839a582010-04-22 18:58:52 -0700840 struct KeyRepeatState {
841 KeyEntry* lastKeyEntry; // or null if no repeat
842 nsecs_t nextRepeatTime;
843 } mKeyRepeatState;
844
845 void resetKeyRepeatLocked();
Jeff Browna665ca82010-09-08 11:49:43 -0700846 KeyEntry* synthesizeKeyRepeatLocked(nsecs_t currentTime, nsecs_t keyRepeatTimeout);
Jeff Browne839a582010-04-22 18:58:52 -0700847
Jeff Brown54bc2812010-06-15 01:31:58 -0700848 // Deferred command processing.
849 bool runCommandsLockedInterruptible();
850 CommandEntry* postCommandLocked(Command command);
851
Jeff Browna665ca82010-09-08 11:49:43 -0700852 // Inbound event processing.
853 void drainInboundQueueLocked();
Jeff Brownd8816c32010-09-16 14:07:33 -0700854 void releasePendingEventLocked();
855 void releaseInboundEventLocked(EventEntry* entry);
Jeff Browna665ca82010-09-08 11:49:43 -0700856 bool isEventFromReliableSourceLocked(EventEntry* entry);
Jeff Browne839a582010-04-22 18:58:52 -0700857
Jeff Browna665ca82010-09-08 11:49:43 -0700858 // Dispatch state.
859 bool mDispatchEnabled;
860 bool mDispatchFrozen;
861 Vector<InputWindow> mWindows;
862 Vector<InputWindow*> mWallpaperWindows;
863
864 // Focus tracking for keys, trackball, etc.
865 InputWindow* mFocusedWindow;
866
867 // Focus tracking for touch.
868 bool mTouchDown;
869 InputWindow* mTouchedWindow; // primary target for current down
870 bool mTouchedWindowIsObscured; // true if other windows may obscure the target
871 Vector<InputWindow*> mTouchedWallpaperWindows; // wallpaper targets
872 struct OutsideTarget {
873 InputWindow* window;
874 bool obscured;
875 };
876 Vector<OutsideTarget> mTempTouchedOutsideTargets; // temporary outside touch targets
877 Vector<sp<InputChannel> > mTempTouchedWallpaperChannels; // temporary wallpaper targets
878
879 // Focused application.
880 InputApplication* mFocusedApplication;
881 InputApplication mFocusedApplicationStorage; // preallocated storage for mFocusedApplication
882 void releaseFocusedApplicationLocked();
883
884 // Dispatch inbound events.
885 bool dispatchConfigurationChangedLocked(
886 nsecs_t currentTime, ConfigurationChangedEntry* entry);
887 bool dispatchKeyLocked(
888 nsecs_t currentTime, KeyEntry* entry, nsecs_t keyRepeatTimeout,
Jeff Brownd8816c32010-09-16 14:07:33 -0700889 bool dropEvent, nsecs_t* nextWakeupTime);
Jeff Browna665ca82010-09-08 11:49:43 -0700890 bool dispatchMotionLocked(
891 nsecs_t currentTime, MotionEntry* entry,
Jeff Brownd8816c32010-09-16 14:07:33 -0700892 bool dropEvent, nsecs_t* nextWakeupTime);
Jeff Brown54bc2812010-06-15 01:31:58 -0700893 void dispatchEventToCurrentInputTargetsLocked(
894 nsecs_t currentTime, EventEntry* entry, bool resumeWithAppendedMotionSample);
Jeff Browne839a582010-04-22 18:58:52 -0700895
Jeff Browna665ca82010-09-08 11:49:43 -0700896 void logOutboundKeyDetailsLocked(const char* prefix, const KeyEntry* entry);
897 void logOutboundMotionDetailsLocked(const char* prefix, const MotionEntry* entry);
898
899 // The input targets that were most recently identified for dispatch.
Jeff Browna665ca82010-09-08 11:49:43 -0700900 bool mCurrentInputTargetsValid; // false while targets are being recomputed
901 Vector<InputTarget> mCurrentInputTargets;
902 int32_t mCurrentInputWindowType;
903 sp<InputChannel> mCurrentInputChannel;
904
905 enum InputTargetWaitCause {
906 INPUT_TARGET_WAIT_CAUSE_NONE,
907 INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY,
908 INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY,
909 };
910
911 InputTargetWaitCause mInputTargetWaitCause;
912 nsecs_t mInputTargetWaitStartTime;
913 nsecs_t mInputTargetWaitTimeoutTime;
914 bool mInputTargetWaitTimeoutExpired;
915
916 // Finding targets for input events.
Jeff Brownd8816c32010-09-16 14:07:33 -0700917 void resetTargetsLocked();
918 void commitTargetsLocked(const InputWindow* window);
Jeff Browna665ca82010-09-08 11:49:43 -0700919 int32_t handleTargetsNotReadyLocked(nsecs_t currentTime, const EventEntry* entry,
920 const InputApplication* application, const InputWindow* window,
921 nsecs_t* nextWakeupTime);
Jeff Brown53a415e2010-09-15 15:18:56 -0700922 void resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout,
923 const sp<InputChannel>& inputChannel);
924 nsecs_t getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime);
Jeff Browna665ca82010-09-08 11:49:43 -0700925 void resetANRTimeoutsLocked();
926
927 int32_t findFocusedWindowLocked(nsecs_t currentTime, const EventEntry* entry,
928 nsecs_t* nextWakeupTime, InputWindow** outWindow);
929 int32_t findTouchedWindowLocked(nsecs_t currentTime, const MotionEntry* entry,
930 nsecs_t* nextWakeupTime, InputWindow** outWindow);
931
Jeff Brown53a415e2010-09-15 15:18:56 -0700932 void addWindowTargetLocked(const InputWindow* window, int32_t targetFlags);
Jeff Browna665ca82010-09-08 11:49:43 -0700933 void addMonitoringTargetsLocked();
934 void pokeUserActivityLocked(nsecs_t eventTime, int32_t windowType, int32_t eventType);
935 bool checkInjectionPermission(const InputWindow* window,
936 int32_t injectorPid, int32_t injectorUid);
937 bool isWindowObscuredLocked(const InputWindow* window);
Jeff Brown53a415e2010-09-15 15:18:56 -0700938 bool isWindowFinishedWithPreviousInputLocked(const InputWindow* window);
Jeff Browna665ca82010-09-08 11:49:43 -0700939 void releaseTouchedWindowLocked();
Jeff Brown53a415e2010-09-15 15:18:56 -0700940 String8 getApplicationWindowLabelLocked(const InputApplication* application,
941 const InputWindow* window);
Jeff Browna665ca82010-09-08 11:49:43 -0700942
Jeff Browne839a582010-04-22 18:58:52 -0700943 // Manage the dispatch cycle for a single connection.
Jeff Brown51d45a72010-06-17 20:52:56 -0700944 // These methods are deliberately not Interruptible because doing all of the work
945 // with the mutex held makes it easier to ensure that connection invariants are maintained.
946 // If needed, the methods post commands to run later once the critical bits are done.
947 void prepareDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Jeff Browne839a582010-04-22 18:58:52 -0700948 EventEntry* eventEntry, const InputTarget* inputTarget,
949 bool resumeWithAppendedMotionSample);
Jeff Brown53a415e2010-09-15 15:18:56 -0700950 void startDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown51d45a72010-06-17 20:52:56 -0700951 void finishDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Browna665ca82010-09-08 11:49:43 -0700952 void startNextDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown51d45a72010-06-17 20:52:56 -0700953 void abortDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Jeff Browne839a582010-04-22 18:58:52 -0700954 bool broken);
Jeff Brown53a415e2010-09-15 15:18:56 -0700955 void drainOutboundQueueLocked(Connection* connection);
Jeff Brown59abe7e2010-09-13 23:17:30 -0700956 static int handleReceiveCallback(int receiveFd, int events, void* data);
Jeff Browne839a582010-04-22 18:58:52 -0700957
Jeff Browna665ca82010-09-08 11:49:43 -0700958 // Dump state.
959 void dumpDispatchStateLocked(String8& dump);
960 void logDispatchStateLocked();
961
Jeff Browne839a582010-04-22 18:58:52 -0700962 // Add or remove a connection to the mActiveConnections vector.
963 void activateConnectionLocked(Connection* connection);
964 void deactivateConnectionLocked(Connection* connection);
965
966 // Interesting events that we might like to log or tell the framework about.
Jeff Brown54bc2812010-06-15 01:31:58 -0700967 void onDispatchCycleStartedLocked(
Jeff Brown51d45a72010-06-17 20:52:56 -0700968 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown54bc2812010-06-15 01:31:58 -0700969 void onDispatchCycleFinishedLocked(
Jeff Brown51d45a72010-06-17 20:52:56 -0700970 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown54bc2812010-06-15 01:31:58 -0700971 void onDispatchCycleBrokenLocked(
Jeff Brown51d45a72010-06-17 20:52:56 -0700972 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown53a415e2010-09-15 15:18:56 -0700973 void onANRLocked(
974 nsecs_t currentTime, const InputApplication* application, const InputWindow* window,
975 nsecs_t eventTime, nsecs_t waitStartTime);
Jeff Brown54bc2812010-06-15 01:31:58 -0700976
Jeff Brown51d45a72010-06-17 20:52:56 -0700977 // Outbound policy interactions.
Jeff Browna665ca82010-09-08 11:49:43 -0700978 void doNotifyConfigurationChangedInterruptible(CommandEntry* commandEntry);
Jeff Brown54bc2812010-06-15 01:31:58 -0700979 void doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown53a415e2010-09-15 15:18:56 -0700980 void doNotifyANRLockedInterruptible(CommandEntry* commandEntry);
Jeff Browna665ca82010-09-08 11:49:43 -0700981 void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry);
982 void doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown53a415e2010-09-15 15:18:56 -0700983
984 // Statistics gathering.
985 void updateDispatchStatisticsLocked(nsecs_t currentTime, const EventEntry* entry,
986 int32_t injectionResult, nsecs_t timeSpentWaitingForApplication);
Jeff Browne839a582010-04-22 18:58:52 -0700987};
988
989/* Enqueues and dispatches input events, endlessly. */
990class InputDispatcherThread : public Thread {
991public:
992 explicit InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher);
993 ~InputDispatcherThread();
994
995private:
996 virtual bool threadLoop();
997
998 sp<InputDispatcherInterface> mDispatcher;
999};
1000
1001} // namespace android
1002
Jeff Browna665ca82010-09-08 11:49:43 -07001003#endif // _UI_INPUT_DISPATCHER_H