blob: 47c5326f98c34361b5b10d9bc5c0442c680aa587 [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 Browne839a582010-04-22 18:58:52 -0700365 /* Registers or unregister input channels that may be used as targets for input events.
Jeff Browna665ca82010-09-08 11:49:43 -0700366 * If monitor is true, the channel will receive a copy of all input events.
Jeff Browne839a582010-04-22 18:58:52 -0700367 *
368 * These methods may be called on any thread (usually by the input manager).
369 */
Jeff Browna665ca82010-09-08 11:49:43 -0700370 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel, bool monitor) = 0;
Jeff Browne839a582010-04-22 18:58:52 -0700371 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) = 0;
372};
373
Jeff Brown54bc2812010-06-15 01:31:58 -0700374/* Dispatches events to input targets. Some functions of the input dispatcher, such as
375 * identifying input targets, are controlled by a separate policy object.
376 *
377 * IMPORTANT INVARIANT:
378 * Because the policy can potentially block or cause re-entrance into the input dispatcher,
379 * the input dispatcher never calls into the policy while holding its internal locks.
380 * The implementation is also carefully designed to recover from scenarios such as an
381 * input channel becoming unregistered while identifying input targets or processing timeouts.
382 *
383 * Methods marked 'Locked' must be called with the lock acquired.
384 *
385 * Methods marked 'LockedInterruptible' must be called with the lock acquired but
386 * may during the course of their execution release the lock, call into the policy, and
387 * then reacquire the lock. The caller is responsible for recovering gracefully.
388 *
389 * A 'LockedInterruptible' method may called a 'Locked' method, but NOT vice-versa.
390 */
Jeff Browne839a582010-04-22 18:58:52 -0700391class InputDispatcher : public InputDispatcherInterface {
392protected:
393 virtual ~InputDispatcher();
394
395public:
Jeff Brown54bc2812010-06-15 01:31:58 -0700396 explicit InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy);
Jeff Browne839a582010-04-22 18:58:52 -0700397
Jeff Browna665ca82010-09-08 11:49:43 -0700398 virtual void dump(String8& dump);
399
Jeff Browne839a582010-04-22 18:58:52 -0700400 virtual void dispatchOnce();
401
Jeff Brown54bc2812010-06-15 01:31:58 -0700402 virtual void notifyConfigurationChanged(nsecs_t eventTime);
Jeff Brown5c1ed842010-07-14 18:48:53 -0700403 virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Browne839a582010-04-22 18:58:52 -0700404 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode,
405 int32_t scanCode, int32_t metaState, nsecs_t downTime);
Jeff Brown5c1ed842010-07-14 18:48:53 -0700406 virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Brownaf30ff62010-09-01 17:01:00 -0700407 uint32_t policyFlags, int32_t action, int32_t flags,
408 int32_t metaState, int32_t edgeFlags,
Jeff Browne839a582010-04-22 18:58:52 -0700409 uint32_t pointerCount, const int32_t* pointerIds, const PointerCoords* pointerCoords,
410 float xPrecision, float yPrecision, nsecs_t downTime);
411
Jeff Brown51d45a72010-06-17 20:52:56 -0700412 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brownf67c53e2010-07-28 15:48:59 -0700413 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis);
Jeff Brown51d45a72010-06-17 20:52:56 -0700414
Jeff Browna665ca82010-09-08 11:49:43 -0700415 virtual void setInputWindows(const Vector<InputWindow>& inputWindows);
416 virtual void setFocusedApplication(const InputApplication* inputApplication);
417 virtual void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown50de30a2010-06-22 01:27:15 -0700418
Jeff Browna665ca82010-09-08 11:49:43 -0700419 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel, bool monitor);
Jeff Browne839a582010-04-22 18:58:52 -0700420 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel);
421
422private:
423 template <typename T>
424 struct Link {
425 T* next;
426 T* prev;
427 };
428
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700429 struct InjectionState {
430 mutable int32_t refCount;
431
432 int32_t injectorPid;
433 int32_t injectorUid;
434 int32_t injectionResult; // initially INPUT_EVENT_INJECTION_PENDING
435 bool injectionIsAsync; // set to true if injection is not waiting for the result
436 int32_t pendingForegroundDispatches; // the number of foreground dispatches in progress
437 };
438
Jeff Browne839a582010-04-22 18:58:52 -0700439 struct EventEntry : Link<EventEntry> {
440 enum {
441 TYPE_SENTINEL,
442 TYPE_CONFIGURATION_CHANGED,
443 TYPE_KEY,
444 TYPE_MOTION
445 };
446
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700447 mutable int32_t refCount;
Jeff Browne839a582010-04-22 18:58:52 -0700448 int32_t type;
449 nsecs_t eventTime;
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700450 InjectionState* injectionState;
Jeff Brown51d45a72010-06-17 20:52:56 -0700451
Jeff Brown54bc2812010-06-15 01:31:58 -0700452 bool dispatchInProgress; // initially false, set to true while dispatching
Jeff Brown51d45a72010-06-17 20:52:56 -0700453
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700454 inline bool isInjected() { return injectionState != NULL; }
Jeff Browne839a582010-04-22 18:58:52 -0700455 };
456
457 struct ConfigurationChangedEntry : EventEntry {
Jeff Browne839a582010-04-22 18:58:52 -0700458 };
459
460 struct KeyEntry : EventEntry {
461 int32_t deviceId;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700462 int32_t source;
Jeff Browne839a582010-04-22 18:58:52 -0700463 uint32_t policyFlags;
464 int32_t action;
465 int32_t flags;
466 int32_t keyCode;
467 int32_t scanCode;
468 int32_t metaState;
469 int32_t repeatCount;
470 nsecs_t downTime;
Jeff Browna665ca82010-09-08 11:49:43 -0700471
472 bool syntheticRepeat; // set to true for synthetic key repeats
473
474 enum InterceptKeyResult {
475 INTERCEPT_KEY_RESULT_UNKNOWN,
476 INTERCEPT_KEY_RESULT_SKIP,
477 INTERCEPT_KEY_RESULT_CONTINUE,
478 };
479 InterceptKeyResult interceptKeyResult; // set based on the interception result
Jeff Browne839a582010-04-22 18:58:52 -0700480 };
481
482 struct MotionSample {
483 MotionSample* next;
484
485 nsecs_t eventTime;
486 PointerCoords pointerCoords[MAX_POINTERS];
487 };
488
489 struct MotionEntry : EventEntry {
490 int32_t deviceId;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700491 int32_t source;
Jeff Browne839a582010-04-22 18:58:52 -0700492 uint32_t policyFlags;
493 int32_t action;
Jeff Brownaf30ff62010-09-01 17:01:00 -0700494 int32_t flags;
Jeff Browne839a582010-04-22 18:58:52 -0700495 int32_t metaState;
496 int32_t edgeFlags;
497 float xPrecision;
498 float yPrecision;
499 nsecs_t downTime;
500 uint32_t pointerCount;
501 int32_t pointerIds[MAX_POINTERS];
502
503 // Linked list of motion samples associated with this motion event.
504 MotionSample firstSample;
505 MotionSample* lastSample;
Jeff Brown542412c2010-08-18 15:51:08 -0700506
507 uint32_t countSamples() const;
Jeff Browne839a582010-04-22 18:58:52 -0700508 };
509
Jeff Brown54bc2812010-06-15 01:31:58 -0700510 // Tracks the progress of dispatching a particular event to a particular connection.
Jeff Browne839a582010-04-22 18:58:52 -0700511 struct DispatchEntry : Link<DispatchEntry> {
512 EventEntry* eventEntry; // the event to dispatch
513 int32_t targetFlags;
514 float xOffset;
515 float yOffset;
Jeff Browne839a582010-04-22 18:58:52 -0700516
517 // True if dispatch has started.
518 bool inProgress;
519
520 // For motion events:
521 // Pointer to the first motion sample to dispatch in this cycle.
522 // Usually NULL to indicate that the list of motion samples begins at
523 // MotionEntry::firstSample. Otherwise, some samples were dispatched in a previous
524 // cycle and this pointer indicates the location of the first remainining sample
525 // to dispatch during the current cycle.
526 MotionSample* headMotionSample;
527 // Pointer to a motion sample to dispatch in the next cycle if the dispatcher was
528 // unable to send all motion samples during this cycle. On the next cycle,
529 // headMotionSample will be initialized to tailMotionSample and tailMotionSample
530 // will be set to NULL.
531 MotionSample* tailMotionSample;
Jeff Brownf67c53e2010-07-28 15:48:59 -0700532
Jeff Brown53a415e2010-09-15 15:18:56 -0700533 inline bool hasForegroundTarget() const {
534 return targetFlags & InputTarget::FLAG_FOREGROUND;
Jeff Browna665ca82010-09-08 11:49:43 -0700535 }
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700536
537 inline bool isSplit() const {
538 return targetFlags & InputTarget::FLAG_SPLIT;
539 }
Jeff Browne839a582010-04-22 18:58:52 -0700540 };
541
Jeff Brown54bc2812010-06-15 01:31:58 -0700542 // A command entry captures state and behavior for an action to be performed in the
543 // dispatch loop after the initial processing has taken place. It is essentially
544 // a kind of continuation used to postpone sensitive policy interactions to a point
545 // in the dispatch loop where it is safe to release the lock (generally after finishing
546 // the critical parts of the dispatch cycle).
547 //
548 // The special thing about commands is that they can voluntarily release and reacquire
549 // the dispatcher lock at will. Initially when the command starts running, the
550 // dispatcher lock is held. However, if the command needs to call into the policy to
551 // do some work, it can release the lock, do the work, then reacquire the lock again
552 // before returning.
553 //
554 // This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch
555 // never calls into the policy while holding its lock.
556 //
557 // Commands are implicitly 'LockedInterruptible'.
558 struct CommandEntry;
559 typedef void (InputDispatcher::*Command)(CommandEntry* commandEntry);
560
Jeff Brown51d45a72010-06-17 20:52:56 -0700561 class Connection;
Jeff Brown54bc2812010-06-15 01:31:58 -0700562 struct CommandEntry : Link<CommandEntry> {
563 CommandEntry();
564 ~CommandEntry();
565
566 Command command;
567
568 // parameters for the command (usage varies by command)
Jeff Brown51d45a72010-06-17 20:52:56 -0700569 sp<Connection> connection;
Jeff Browna665ca82010-09-08 11:49:43 -0700570 nsecs_t eventTime;
571 KeyEntry* keyEntry;
572 sp<InputChannel> inputChannel;
573 sp<InputApplicationHandle> inputApplicationHandle;
Jeff Browna665ca82010-09-08 11:49:43 -0700574 int32_t userActivityEventType;
Jeff Brown54bc2812010-06-15 01:31:58 -0700575 };
576
577 // Generic queue implementation.
Jeff Browne839a582010-04-22 18:58:52 -0700578 template <typename T>
579 struct Queue {
Jeff Browna665ca82010-09-08 11:49:43 -0700580 T headSentinel;
581 T tailSentinel;
Jeff Browne839a582010-04-22 18:58:52 -0700582
583 inline Queue() {
Jeff Browna665ca82010-09-08 11:49:43 -0700584 headSentinel.prev = NULL;
585 headSentinel.next = & tailSentinel;
586 tailSentinel.prev = & headSentinel;
587 tailSentinel.next = NULL;
Jeff Browne839a582010-04-22 18:58:52 -0700588 }
589
Jeff Browna665ca82010-09-08 11:49:43 -0700590 inline bool isEmpty() const {
591 return headSentinel.next == & tailSentinel;
Jeff Browne839a582010-04-22 18:58:52 -0700592 }
593
594 inline void enqueueAtTail(T* entry) {
Jeff Browna665ca82010-09-08 11:49:43 -0700595 T* last = tailSentinel.prev;
Jeff Browne839a582010-04-22 18:58:52 -0700596 last->next = entry;
597 entry->prev = last;
Jeff Browna665ca82010-09-08 11:49:43 -0700598 entry->next = & tailSentinel;
599 tailSentinel.prev = entry;
Jeff Browne839a582010-04-22 18:58:52 -0700600 }
601
602 inline void enqueueAtHead(T* entry) {
Jeff Browna665ca82010-09-08 11:49:43 -0700603 T* first = headSentinel.next;
604 headSentinel.next = entry;
605 entry->prev = & headSentinel;
Jeff Browne839a582010-04-22 18:58:52 -0700606 entry->next = first;
607 first->prev = entry;
608 }
609
610 inline void dequeue(T* entry) {
611 entry->prev->next = entry->next;
612 entry->next->prev = entry->prev;
613 }
614
615 inline T* dequeueAtHead() {
Jeff Browna665ca82010-09-08 11:49:43 -0700616 T* first = headSentinel.next;
Jeff Browne839a582010-04-22 18:58:52 -0700617 dequeue(first);
618 return first;
619 }
Jeff Brown53a415e2010-09-15 15:18:56 -0700620
621 uint32_t count() const;
Jeff Browne839a582010-04-22 18:58:52 -0700622 };
623
624 /* Allocates queue entries and performs reference counting as needed. */
625 class Allocator {
626 public:
627 Allocator();
628
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700629 InjectionState* obtainInjectionState(int32_t injectorPid, int32_t injectorUid);
Jeff Brown51d45a72010-06-17 20:52:56 -0700630 ConfigurationChangedEntry* obtainConfigurationChangedEntry(nsecs_t eventTime);
631 KeyEntry* obtainKeyEntry(nsecs_t eventTime,
Jeff Brown5c1ed842010-07-14 18:48:53 -0700632 int32_t deviceId, int32_t source, uint32_t policyFlags, int32_t action,
Jeff Brown51d45a72010-06-17 20:52:56 -0700633 int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState,
634 int32_t repeatCount, nsecs_t downTime);
635 MotionEntry* obtainMotionEntry(nsecs_t eventTime,
Jeff Brown5c1ed842010-07-14 18:48:53 -0700636 int32_t deviceId, int32_t source, uint32_t policyFlags, int32_t action,
Jeff Brownaf30ff62010-09-01 17:01:00 -0700637 int32_t flags, int32_t metaState, int32_t edgeFlags,
638 float xPrecision, float yPrecision,
Jeff Brown51d45a72010-06-17 20:52:56 -0700639 nsecs_t downTime, uint32_t pointerCount,
640 const int32_t* pointerIds, const PointerCoords* pointerCoords);
Jeff Browna665ca82010-09-08 11:49:43 -0700641 DispatchEntry* obtainDispatchEntry(EventEntry* eventEntry,
Jeff Brown53a415e2010-09-15 15:18:56 -0700642 int32_t targetFlags, float xOffset, float yOffset);
Jeff Brown54bc2812010-06-15 01:31:58 -0700643 CommandEntry* obtainCommandEntry(Command command);
Jeff Browne839a582010-04-22 18:58:52 -0700644
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700645 void releaseInjectionState(InjectionState* injectionState);
Jeff Browne839a582010-04-22 18:58:52 -0700646 void releaseEventEntry(EventEntry* entry);
647 void releaseConfigurationChangedEntry(ConfigurationChangedEntry* entry);
648 void releaseKeyEntry(KeyEntry* entry);
649 void releaseMotionEntry(MotionEntry* entry);
650 void releaseDispatchEntry(DispatchEntry* entry);
Jeff Brown54bc2812010-06-15 01:31:58 -0700651 void releaseCommandEntry(CommandEntry* entry);
Jeff Browne839a582010-04-22 18:58:52 -0700652
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700653 void recycleKeyEntry(KeyEntry* entry);
654
Jeff Browne839a582010-04-22 18:58:52 -0700655 void appendMotionSample(MotionEntry* motionEntry,
Jeff Brown51d45a72010-06-17 20:52:56 -0700656 nsecs_t eventTime, const PointerCoords* pointerCoords);
Jeff Browne839a582010-04-22 18:58:52 -0700657
658 private:
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700659 Pool<InjectionState> mInjectionStatePool;
Jeff Browne839a582010-04-22 18:58:52 -0700660 Pool<ConfigurationChangedEntry> mConfigurationChangeEntryPool;
661 Pool<KeyEntry> mKeyEntryPool;
662 Pool<MotionEntry> mMotionEntryPool;
663 Pool<MotionSample> mMotionSamplePool;
664 Pool<DispatchEntry> mDispatchEntryPool;
Jeff Brown54bc2812010-06-15 01:31:58 -0700665 Pool<CommandEntry> mCommandEntryPool;
Jeff Brown51d45a72010-06-17 20:52:56 -0700666
667 void initializeEventEntry(EventEntry* entry, int32_t type, nsecs_t eventTime);
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700668 void releaseEventEntryInjectionState(EventEntry* entry);
Jeff Browne839a582010-04-22 18:58:52 -0700669 };
670
Jeff Browna665ca82010-09-08 11:49:43 -0700671 /* Tracks dispatched key and motion event state so that cancelation events can be
672 * synthesized when events are dropped. */
673 class InputState {
674 public:
675 // Specifies whether a given event will violate input state consistency.
676 enum Consistency {
677 // The event is consistent with the current input state.
678 CONSISTENT,
679 // The event is inconsistent with the current input state but applications
680 // will tolerate it. eg. Down followed by another down.
681 TOLERABLE,
682 // The event is inconsistent with the current input state and will probably
683 // cause applications to crash. eg. Up without prior down, move with
684 // unexpected number of pointers.
685 BROKEN
686 };
687
688 InputState();
689 ~InputState();
690
691 // Returns true if there is no state to be canceled.
692 bool isNeutral() const;
693
694 // Returns true if the input state believes it is out of sync.
695 bool isOutOfSync() const;
696
697 // Sets the input state to be out of sync if it is not neutral.
698 void setOutOfSync();
699
700 // Resets the input state out of sync flag.
701 void resetOutOfSync();
702
703 // Records tracking information for an event that has just been published.
704 // Returns whether the event is consistent with the current input state.
705 Consistency trackEvent(const EventEntry* entry);
706
707 // Records tracking information for a key event that has just been published.
708 // Returns whether the event is consistent with the current input state.
709 Consistency trackKey(const KeyEntry* entry);
710
711 // Records tracking information for a motion event that has just been published.
712 // Returns whether the event is consistent with the current input state.
713 Consistency trackMotion(const MotionEntry* entry);
714
715 // Synthesizes cancelation events for the current state.
716 void synthesizeCancelationEvents(Allocator* allocator,
717 Vector<EventEntry*>& outEvents) const;
718
719 // Clears the current state.
720 void clear();
721
722 private:
723 bool mIsOutOfSync;
724
725 struct KeyMemento {
726 int32_t deviceId;
727 int32_t source;
728 int32_t keyCode;
729 int32_t scanCode;
730 nsecs_t downTime;
731 };
732
733 struct MotionMemento {
734 int32_t deviceId;
735 int32_t source;
736 float xPrecision;
737 float yPrecision;
738 nsecs_t downTime;
739 uint32_t pointerCount;
740 int32_t pointerIds[MAX_POINTERS];
741 PointerCoords pointerCoords[MAX_POINTERS];
742
743 void setPointers(const MotionEntry* entry);
744 };
745
746 Vector<KeyMemento> mKeyMementos;
747 Vector<MotionMemento> mMotionMementos;
748 };
749
Jeff Browne839a582010-04-22 18:58:52 -0700750 /* Manages the dispatch state associated with a single input channel. */
751 class Connection : public RefBase {
752 protected:
753 virtual ~Connection();
754
755 public:
756 enum Status {
757 // Everything is peachy.
758 STATUS_NORMAL,
759 // An unrecoverable communication error has occurred.
760 STATUS_BROKEN,
Jeff Browne839a582010-04-22 18:58:52 -0700761 // The input channel has been unregistered.
762 STATUS_ZOMBIE
763 };
764
765 Status status;
766 sp<InputChannel> inputChannel;
767 InputPublisher inputPublisher;
Jeff Browna665ca82010-09-08 11:49:43 -0700768 InputState inputState;
Jeff Browne839a582010-04-22 18:58:52 -0700769 Queue<DispatchEntry> outboundQueue;
Jeff Browne839a582010-04-22 18:58:52 -0700770
771 nsecs_t lastEventTime; // the time when the event was originally captured
772 nsecs_t lastDispatchTime; // the time when the last event was dispatched
Jeff Browne839a582010-04-22 18:58:52 -0700773
774 explicit Connection(const sp<InputChannel>& inputChannel);
775
Jeff Brown54bc2812010-06-15 01:31:58 -0700776 inline const char* getInputChannelName() const { return inputChannel->getName().string(); }
777
778 const char* getStatusLabel() const;
Jeff Browne839a582010-04-22 18:58:52 -0700779
780 // Finds a DispatchEntry in the outbound queue associated with the specified event.
781 // Returns NULL if not found.
782 DispatchEntry* findQueuedDispatchEntryForEvent(const EventEntry* eventEntry) const;
783
Jeff Browne839a582010-04-22 18:58:52 -0700784 // Gets the time since the current event was originally obtained from the input driver.
Jeff Browna665ca82010-09-08 11:49:43 -0700785 inline double getEventLatencyMillis(nsecs_t currentTime) const {
Jeff Browne839a582010-04-22 18:58:52 -0700786 return (currentTime - lastEventTime) / 1000000.0;
787 }
788
789 // Gets the time since the current event entered the outbound dispatch queue.
Jeff Browna665ca82010-09-08 11:49:43 -0700790 inline double getDispatchLatencyMillis(nsecs_t currentTime) const {
Jeff Browne839a582010-04-22 18:58:52 -0700791 return (currentTime - lastDispatchTime) / 1000000.0;
792 }
793
Jeff Browne839a582010-04-22 18:58:52 -0700794 status_t initialize();
795 };
796
Jeff Brown54bc2812010-06-15 01:31:58 -0700797 sp<InputDispatcherPolicyInterface> mPolicy;
Jeff Browne839a582010-04-22 18:58:52 -0700798
799 Mutex mLock;
800
Jeff Browne839a582010-04-22 18:58:52 -0700801 Allocator mAllocator;
Jeff Brown59abe7e2010-09-13 23:17:30 -0700802 sp<Looper> mLooper;
Jeff Browne839a582010-04-22 18:58:52 -0700803
Jeff Browna665ca82010-09-08 11:49:43 -0700804 EventEntry* mPendingEvent;
Jeff Brown54bc2812010-06-15 01:31:58 -0700805 Queue<EventEntry> mInboundQueue;
806 Queue<CommandEntry> mCommandQueue;
807
Jeff Browna665ca82010-09-08 11:49:43 -0700808 Vector<EventEntry*> mTempCancelationEvents;
809
810 void dispatchOnceInnerLocked(nsecs_t keyRepeatTimeout, nsecs_t keyRepeatDelay,
811 nsecs_t* nextWakeupTime);
812
Jeff Brown59abe7e2010-09-13 23:17:30 -0700813 // Enqueues an inbound event. Returns true if mLooper->wake() should be called.
Jeff Browna665ca82010-09-08 11:49:43 -0700814 bool enqueueInboundEventLocked(EventEntry* entry);
815
816 // App switch latency optimization.
817 nsecs_t mAppSwitchDueTime;
818
819 static bool isAppSwitchKey(int32_t keyCode);
820 bool isAppSwitchPendingLocked();
821 bool detectPendingAppSwitchLocked(KeyEntry* inboundKeyEntry);
822 void resetPendingAppSwitchLocked(bool handled);
823
Jeff Browne839a582010-04-22 18:58:52 -0700824 // All registered connections mapped by receive pipe file descriptor.
825 KeyedVector<int, sp<Connection> > mConnectionsByReceiveFd;
826
Jeff Brown53a415e2010-09-15 15:18:56 -0700827 ssize_t getConnectionIndexLocked(const sp<InputChannel>& inputChannel);
Jeff Brown0cacb872010-08-17 15:59:26 -0700828
Jeff Browne839a582010-04-22 18:58:52 -0700829 // Active connections are connections that have a non-empty outbound queue.
Jeff Brown51d45a72010-06-17 20:52:56 -0700830 // We don't use a ref-counted pointer here because we explicitly abort connections
831 // during unregistration which causes the connection's outbound queue to be cleared
832 // and the connection itself to be deactivated.
Jeff Browne839a582010-04-22 18:58:52 -0700833 Vector<Connection*> mActiveConnections;
834
Jeff Browna665ca82010-09-08 11:49:43 -0700835 // Input channels that will receive a copy of all input events.
836 Vector<sp<InputChannel> > mMonitoringChannels;
Jeff Browne839a582010-04-22 18:58:52 -0700837
Jeff Browna665ca82010-09-08 11:49:43 -0700838 // Preallocated key event object used for policy inquiries.
839 KeyEvent mReusableKeyEvent;
Jeff Browne839a582010-04-22 18:58:52 -0700840
Jeff Brown51d45a72010-06-17 20:52:56 -0700841 // Event injection and synchronization.
842 Condition mInjectionResultAvailableCondition;
Jeff Browna665ca82010-09-08 11:49:43 -0700843 EventEntry* createEntryFromInjectedInputEventLocked(const InputEvent* event);
Jeff Brown51d45a72010-06-17 20:52:56 -0700844 void setInjectionResultLocked(EventEntry* entry, int32_t injectionResult);
845
Jeff Brownf67c53e2010-07-28 15:48:59 -0700846 Condition mInjectionSyncFinishedCondition;
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700847 void incrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brown53a415e2010-09-15 15:18:56 -0700848 void decrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brownf67c53e2010-07-28 15:48:59 -0700849
Jeff Brown542412c2010-08-18 15:51:08 -0700850 // Throttling state.
851 struct ThrottleState {
852 nsecs_t minTimeBetweenEvents;
853
854 nsecs_t lastEventTime;
855 int32_t lastDeviceId;
856 uint32_t lastSource;
857
858 uint32_t originalSampleCount; // only collected during debugging
859 } mThrottleState;
860
Jeff Browne839a582010-04-22 18:58:52 -0700861 // Key repeat tracking.
Jeff Browne839a582010-04-22 18:58:52 -0700862 struct KeyRepeatState {
863 KeyEntry* lastKeyEntry; // or null if no repeat
864 nsecs_t nextRepeatTime;
865 } mKeyRepeatState;
866
867 void resetKeyRepeatLocked();
Jeff Browna665ca82010-09-08 11:49:43 -0700868 KeyEntry* synthesizeKeyRepeatLocked(nsecs_t currentTime, nsecs_t keyRepeatTimeout);
Jeff Browne839a582010-04-22 18:58:52 -0700869
Jeff Brown54bc2812010-06-15 01:31:58 -0700870 // Deferred command processing.
871 bool runCommandsLockedInterruptible();
872 CommandEntry* postCommandLocked(Command command);
873
Jeff Browna665ca82010-09-08 11:49:43 -0700874 // Inbound event processing.
875 void drainInboundQueueLocked();
Jeff Brownd8816c32010-09-16 14:07:33 -0700876 void releasePendingEventLocked();
877 void releaseInboundEventLocked(EventEntry* entry);
Jeff Browna665ca82010-09-08 11:49:43 -0700878 bool isEventFromReliableSourceLocked(EventEntry* entry);
Jeff Browne839a582010-04-22 18:58:52 -0700879
Jeff Browna665ca82010-09-08 11:49:43 -0700880 // Dispatch state.
881 bool mDispatchEnabled;
882 bool mDispatchFrozen;
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700883
Jeff Browna665ca82010-09-08 11:49:43 -0700884 Vector<InputWindow> mWindows;
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700885
886 const InputWindow* getWindowLocked(const sp<InputChannel>& inputChannel);
Jeff Browna665ca82010-09-08 11:49:43 -0700887
888 // Focus tracking for keys, trackball, etc.
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700889 const InputWindow* mFocusedWindow;
Jeff Browna665ca82010-09-08 11:49:43 -0700890
891 // Focus tracking for touch.
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700892 struct TouchedWindow {
893 const InputWindow* window;
894 int32_t targetFlags;
895 BitSet32 pointerIds;
896 sp<InputChannel> channel;
Jeff Browna665ca82010-09-08 11:49:43 -0700897 };
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700898 struct TouchState {
899 bool down;
900 bool split;
901 Vector<TouchedWindow> windows;
902
903 TouchState();
904 ~TouchState();
905 void reset();
906 void copyFrom(const TouchState& other);
907 void addOrUpdateWindow(const InputWindow* window, int32_t targetFlags, BitSet32 pointerIds);
908 void removeOutsideTouchWindows();
909 const InputWindow* getFirstForegroundWindow();
910 };
911
912 TouchState mTouchState;
913 TouchState mTempTouchState;
Jeff Browna665ca82010-09-08 11:49:43 -0700914
915 // Focused application.
916 InputApplication* mFocusedApplication;
917 InputApplication mFocusedApplicationStorage; // preallocated storage for mFocusedApplication
918 void releaseFocusedApplicationLocked();
919
920 // Dispatch inbound events.
921 bool dispatchConfigurationChangedLocked(
922 nsecs_t currentTime, ConfigurationChangedEntry* entry);
923 bool dispatchKeyLocked(
924 nsecs_t currentTime, KeyEntry* entry, nsecs_t keyRepeatTimeout,
Jeff Brownd8816c32010-09-16 14:07:33 -0700925 bool dropEvent, nsecs_t* nextWakeupTime);
Jeff Browna665ca82010-09-08 11:49:43 -0700926 bool dispatchMotionLocked(
927 nsecs_t currentTime, MotionEntry* entry,
Jeff Brownd8816c32010-09-16 14:07:33 -0700928 bool dropEvent, nsecs_t* nextWakeupTime);
Jeff Brown54bc2812010-06-15 01:31:58 -0700929 void dispatchEventToCurrentInputTargetsLocked(
930 nsecs_t currentTime, EventEntry* entry, bool resumeWithAppendedMotionSample);
Jeff Browne839a582010-04-22 18:58:52 -0700931
Jeff Browna665ca82010-09-08 11:49:43 -0700932 void logOutboundKeyDetailsLocked(const char* prefix, const KeyEntry* entry);
933 void logOutboundMotionDetailsLocked(const char* prefix, const MotionEntry* entry);
934
935 // The input targets that were most recently identified for dispatch.
Jeff Browna665ca82010-09-08 11:49:43 -0700936 bool mCurrentInputTargetsValid; // false while targets are being recomputed
937 Vector<InputTarget> mCurrentInputTargets;
Jeff Browna665ca82010-09-08 11:49:43 -0700938
939 enum InputTargetWaitCause {
940 INPUT_TARGET_WAIT_CAUSE_NONE,
941 INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY,
942 INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY,
943 };
944
945 InputTargetWaitCause mInputTargetWaitCause;
946 nsecs_t mInputTargetWaitStartTime;
947 nsecs_t mInputTargetWaitTimeoutTime;
948 bool mInputTargetWaitTimeoutExpired;
949
950 // Finding targets for input events.
Jeff Brownd8816c32010-09-16 14:07:33 -0700951 void resetTargetsLocked();
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700952 void commitTargetsLocked();
Jeff Browna665ca82010-09-08 11:49:43 -0700953 int32_t handleTargetsNotReadyLocked(nsecs_t currentTime, const EventEntry* entry,
954 const InputApplication* application, const InputWindow* window,
955 nsecs_t* nextWakeupTime);
Jeff Brown53a415e2010-09-15 15:18:56 -0700956 void resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout,
957 const sp<InputChannel>& inputChannel);
958 nsecs_t getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime);
Jeff Browna665ca82010-09-08 11:49:43 -0700959 void resetANRTimeoutsLocked();
960
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700961 int32_t findFocusedWindowTargetsLocked(nsecs_t currentTime, const EventEntry* entry,
962 nsecs_t* nextWakeupTime);
963 int32_t findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry* entry,
964 nsecs_t* nextWakeupTime);
Jeff Browna665ca82010-09-08 11:49:43 -0700965
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700966 void addWindowTargetLocked(const InputWindow* window, int32_t targetFlags,
967 BitSet32 pointerIds);
Jeff Browna665ca82010-09-08 11:49:43 -0700968 void addMonitoringTargetsLocked();
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700969 bool shouldPokeUserActivityForCurrentInputTargetsLocked();
970 void pokeUserActivityLocked(nsecs_t eventTime, int32_t eventType);
971 bool checkInjectionPermission(const InputWindow* window, const InjectionState* injectionState);
Jeff Brown35cf0e92010-10-05 12:26:23 -0700972 bool isWindowObscuredAtPointLocked(const InputWindow* window, int32_t x, int32_t y) const;
Jeff Brown53a415e2010-09-15 15:18:56 -0700973 bool isWindowFinishedWithPreviousInputLocked(const InputWindow* window);
Jeff Brown53a415e2010-09-15 15:18:56 -0700974 String8 getApplicationWindowLabelLocked(const InputApplication* application,
975 const InputWindow* window);
Jeff Browna665ca82010-09-08 11:49:43 -0700976
Jeff Browne839a582010-04-22 18:58:52 -0700977 // Manage the dispatch cycle for a single connection.
Jeff Brown51d45a72010-06-17 20:52:56 -0700978 // These methods are deliberately not Interruptible because doing all of the work
979 // with the mutex held makes it easier to ensure that connection invariants are maintained.
980 // If needed, the methods post commands to run later once the critical bits are done.
981 void prepareDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Jeff Browne839a582010-04-22 18:58:52 -0700982 EventEntry* eventEntry, const InputTarget* inputTarget,
983 bool resumeWithAppendedMotionSample);
Jeff Brown53a415e2010-09-15 15:18:56 -0700984 void startDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown51d45a72010-06-17 20:52:56 -0700985 void finishDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Browna665ca82010-09-08 11:49:43 -0700986 void startNextDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown51d45a72010-06-17 20:52:56 -0700987 void abortDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Jeff Browne839a582010-04-22 18:58:52 -0700988 bool broken);
Jeff Brown53a415e2010-09-15 15:18:56 -0700989 void drainOutboundQueueLocked(Connection* connection);
Jeff Brown59abe7e2010-09-13 23:17:30 -0700990 static int handleReceiveCallback(int receiveFd, int events, void* data);
Jeff Browne839a582010-04-22 18:58:52 -0700991
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700992 // Splitting motion events across windows.
993 MotionEntry* splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds);
994
Jeff Browna665ca82010-09-08 11:49:43 -0700995 // Dump state.
996 void dumpDispatchStateLocked(String8& dump);
997 void logDispatchStateLocked();
998
Jeff Browne839a582010-04-22 18:58:52 -0700999 // Add or remove a connection to the mActiveConnections vector.
1000 void activateConnectionLocked(Connection* connection);
1001 void deactivateConnectionLocked(Connection* connection);
1002
1003 // Interesting events that we might like to log or tell the framework about.
Jeff Brown54bc2812010-06-15 01:31:58 -07001004 void onDispatchCycleStartedLocked(
Jeff Brown51d45a72010-06-17 20:52:56 -07001005 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown54bc2812010-06-15 01:31:58 -07001006 void onDispatchCycleFinishedLocked(
Jeff Brown51d45a72010-06-17 20:52:56 -07001007 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown54bc2812010-06-15 01:31:58 -07001008 void onDispatchCycleBrokenLocked(
Jeff Brown51d45a72010-06-17 20:52:56 -07001009 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown53a415e2010-09-15 15:18:56 -07001010 void onANRLocked(
1011 nsecs_t currentTime, const InputApplication* application, const InputWindow* window,
1012 nsecs_t eventTime, nsecs_t waitStartTime);
Jeff Brown54bc2812010-06-15 01:31:58 -07001013
Jeff Brown51d45a72010-06-17 20:52:56 -07001014 // Outbound policy interactions.
Jeff Browna665ca82010-09-08 11:49:43 -07001015 void doNotifyConfigurationChangedInterruptible(CommandEntry* commandEntry);
Jeff Brown54bc2812010-06-15 01:31:58 -07001016 void doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown53a415e2010-09-15 15:18:56 -07001017 void doNotifyANRLockedInterruptible(CommandEntry* commandEntry);
Jeff Browna665ca82010-09-08 11:49:43 -07001018 void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry);
1019 void doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown53a415e2010-09-15 15:18:56 -07001020
1021 // Statistics gathering.
1022 void updateDispatchStatisticsLocked(nsecs_t currentTime, const EventEntry* entry,
1023 int32_t injectionResult, nsecs_t timeSpentWaitingForApplication);
Jeff Browne839a582010-04-22 18:58:52 -07001024};
1025
1026/* Enqueues and dispatches input events, endlessly. */
1027class InputDispatcherThread : public Thread {
1028public:
1029 explicit InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher);
1030 ~InputDispatcherThread();
1031
1032private:
1033 virtual bool threadLoop();
1034
1035 sp<InputDispatcherInterface> mDispatcher;
1036};
1037
1038} // namespace android
1039
Jeff Browna665ca82010-09-08 11:49:43 -07001040#endif // _UI_INPUT_DISPATCHER_H