blob: cc160129863d9a6f243f38ad442f198d3dd5566c [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
215 bool visibleFrameIntersects(const InputWindow* other) const;
216 bool touchableAreaContainsPoint(int32_t x, int32_t y) const;
217};
218
219
220/*
221 * A private handle type used by the input manager to track the window.
222 */
223class InputApplicationHandle : public RefBase {
224protected:
225 InputApplicationHandle() { }
226 virtual ~InputApplicationHandle() { }
227};
228
229
230/*
231 * An input application describes properties of an application that can receive input.
232 */
233struct InputApplication {
234 String8 name;
235 nsecs_t dispatchingTimeout;
236 sp<InputApplicationHandle> handle;
237};
238
239
240/*
Jeff Brown54bc2812010-06-15 01:31:58 -0700241 * Input dispatcher policy interface.
242 *
243 * The input reader policy is used by the input reader to interact with the Window Manager
244 * and other system components.
245 *
246 * The actual implementation is partially supported by callbacks into the DVM
247 * via JNI. This interface is also mocked in the unit tests.
248 */
249class InputDispatcherPolicyInterface : public virtual RefBase {
250protected:
251 InputDispatcherPolicyInterface() { }
252 virtual ~InputDispatcherPolicyInterface() { }
253
254public:
255 /* Notifies the system that a configuration change has occurred. */
256 virtual void notifyConfigurationChanged(nsecs_t when) = 0;
257
Jeff Browna665ca82010-09-08 11:49:43 -0700258 /* Notifies the system that an application is not responding.
259 * Returns a new timeout to continue waiting, or 0 to abort dispatch. */
Jeff Brown53a415e2010-09-15 15:18:56 -0700260 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
261 const sp<InputChannel>& inputChannel) = 0;
Jeff Browna665ca82010-09-08 11:49:43 -0700262
Jeff Brown54bc2812010-06-15 01:31:58 -0700263 /* Notifies the system that an input channel is unrecoverably broken. */
264 virtual void notifyInputChannelBroken(const sp<InputChannel>& inputChannel) = 0;
265
Jeff Brown61ce3982010-09-07 10:44:57 -0700266 /* Gets the key repeat initial timeout or -1 if automatic key repeating is disabled. */
Jeff Brown54bc2812010-06-15 01:31:58 -0700267 virtual nsecs_t getKeyRepeatTimeout() = 0;
268
Jeff Brown61ce3982010-09-07 10:44:57 -0700269 /* Gets the key repeat inter-key delay. */
270 virtual nsecs_t getKeyRepeatDelay() = 0;
271
Jeff Brown542412c2010-08-18 15:51:08 -0700272 /* Gets the maximum suggested event delivery rate per second.
273 * This value is used to throttle motion event movement actions on a per-device
274 * basis. It is not intended to be a hard limit.
275 */
276 virtual int32_t getMaxEventsPerSecond() = 0;
Jeff Browna665ca82010-09-08 11:49:43 -0700277
278 /* Allows the policy a chance to intercept a key before dispatching. */
279 virtual bool interceptKeyBeforeDispatching(const sp<InputChannel>& inputChannel,
280 const KeyEvent* keyEvent, uint32_t policyFlags) = 0;
281
282 /* Poke user activity for an event dispatched to a window. */
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700283 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType) = 0;
Jeff Browna665ca82010-09-08 11:49:43 -0700284
285 /* Checks whether a given application pid/uid has permission to inject input events
286 * into other applications.
287 *
288 * This method is special in that its implementation promises to be non-reentrant and
289 * is safe to call while holding other locks. (Most other methods make no such guarantees!)
290 */
291 virtual bool checkInjectEventsPermissionNonReentrant(
292 int32_t injectorPid, int32_t injectorUid) = 0;
Jeff Brown54bc2812010-06-15 01:31:58 -0700293};
294
295
Jeff Browne839a582010-04-22 18:58:52 -0700296/* Notifies the system about input events generated by the input reader.
297 * The dispatcher is expected to be mostly asynchronous. */
298class InputDispatcherInterface : public virtual RefBase {
299protected:
300 InputDispatcherInterface() { }
301 virtual ~InputDispatcherInterface() { }
302
303public:
Jeff Browna665ca82010-09-08 11:49:43 -0700304 /* Dumps the state of the input dispatcher.
305 *
306 * This method may be called on any thread (usually by the input manager). */
307 virtual void dump(String8& dump) = 0;
308
Jeff Browne839a582010-04-22 18:58:52 -0700309 /* Runs a single iteration of the dispatch loop.
310 * Nominally processes one queued event, a timeout, or a response from an input consumer.
311 *
312 * This method should only be called on the input dispatcher thread.
313 */
314 virtual void dispatchOnce() = 0;
315
316 /* Notifies the dispatcher about new events.
Jeff Browne839a582010-04-22 18:58:52 -0700317 *
318 * These methods should only be called on the input reader thread.
319 */
Jeff Brown54bc2812010-06-15 01:31:58 -0700320 virtual void notifyConfigurationChanged(nsecs_t eventTime) = 0;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700321 virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Browne839a582010-04-22 18:58:52 -0700322 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode,
323 int32_t scanCode, int32_t metaState, nsecs_t downTime) = 0;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700324 virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Brownaf30ff62010-09-01 17:01:00 -0700325 uint32_t policyFlags, int32_t action, int32_t flags,
326 int32_t metaState, int32_t edgeFlags,
Jeff Browne839a582010-04-22 18:58:52 -0700327 uint32_t pointerCount, const int32_t* pointerIds, const PointerCoords* pointerCoords,
328 float xPrecision, float yPrecision, nsecs_t downTime) = 0;
329
Jeff Brown51d45a72010-06-17 20:52:56 -0700330 /* Injects an input event and optionally waits for sync.
Jeff Brownf67c53e2010-07-28 15:48:59 -0700331 * The synchronization mode determines whether the method blocks while waiting for
332 * input injection to proceed.
Jeff Brown51d45a72010-06-17 20:52:56 -0700333 * Returns one of the INPUT_EVENT_INJECTION_XXX constants.
334 *
335 * This method may be called on any thread (usually by the input manager).
336 */
337 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brownf67c53e2010-07-28 15:48:59 -0700338 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis) = 0;
Jeff Brown51d45a72010-06-17 20:52:56 -0700339
Jeff Browna665ca82010-09-08 11:49:43 -0700340 /* Sets the list of input windows.
341 *
342 * This method may be called on any thread (usually by the input manager).
343 */
344 virtual void setInputWindows(const Vector<InputWindow>& inputWindows) = 0;
345
346 /* Sets the focused application.
347 *
348 * This method may be called on any thread (usually by the input manager).
349 */
350 virtual void setFocusedApplication(const InputApplication* inputApplication) = 0;
351
352 /* Sets the input dispatching mode.
353 *
354 * This method may be called on any thread (usually by the input manager).
355 */
356 virtual void setInputDispatchMode(bool enabled, bool frozen) = 0;
357
Jeff Browne839a582010-04-22 18:58:52 -0700358 /* Registers or unregister input channels that may be used as targets for input events.
Jeff Browna665ca82010-09-08 11:49:43 -0700359 * If monitor is true, the channel will receive a copy of all input events.
Jeff Browne839a582010-04-22 18:58:52 -0700360 *
361 * These methods may be called on any thread (usually by the input manager).
362 */
Jeff Browna665ca82010-09-08 11:49:43 -0700363 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel, bool monitor) = 0;
Jeff Browne839a582010-04-22 18:58:52 -0700364 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) = 0;
365};
366
Jeff Brown54bc2812010-06-15 01:31:58 -0700367/* Dispatches events to input targets. Some functions of the input dispatcher, such as
368 * identifying input targets, are controlled by a separate policy object.
369 *
370 * IMPORTANT INVARIANT:
371 * Because the policy can potentially block or cause re-entrance into the input dispatcher,
372 * the input dispatcher never calls into the policy while holding its internal locks.
373 * The implementation is also carefully designed to recover from scenarios such as an
374 * input channel becoming unregistered while identifying input targets or processing timeouts.
375 *
376 * Methods marked 'Locked' must be called with the lock acquired.
377 *
378 * Methods marked 'LockedInterruptible' must be called with the lock acquired but
379 * may during the course of their execution release the lock, call into the policy, and
380 * then reacquire the lock. The caller is responsible for recovering gracefully.
381 *
382 * A 'LockedInterruptible' method may called a 'Locked' method, but NOT vice-versa.
383 */
Jeff Browne839a582010-04-22 18:58:52 -0700384class InputDispatcher : public InputDispatcherInterface {
385protected:
386 virtual ~InputDispatcher();
387
388public:
Jeff Brown54bc2812010-06-15 01:31:58 -0700389 explicit InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy);
Jeff Browne839a582010-04-22 18:58:52 -0700390
Jeff Browna665ca82010-09-08 11:49:43 -0700391 virtual void dump(String8& dump);
392
Jeff Browne839a582010-04-22 18:58:52 -0700393 virtual void dispatchOnce();
394
Jeff Brown54bc2812010-06-15 01:31:58 -0700395 virtual void notifyConfigurationChanged(nsecs_t eventTime);
Jeff Brown5c1ed842010-07-14 18:48:53 -0700396 virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Browne839a582010-04-22 18:58:52 -0700397 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode,
398 int32_t scanCode, int32_t metaState, nsecs_t downTime);
Jeff Brown5c1ed842010-07-14 18:48:53 -0700399 virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Brownaf30ff62010-09-01 17:01:00 -0700400 uint32_t policyFlags, int32_t action, int32_t flags,
401 int32_t metaState, int32_t edgeFlags,
Jeff Browne839a582010-04-22 18:58:52 -0700402 uint32_t pointerCount, const int32_t* pointerIds, const PointerCoords* pointerCoords,
403 float xPrecision, float yPrecision, nsecs_t downTime);
404
Jeff Brown51d45a72010-06-17 20:52:56 -0700405 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brownf67c53e2010-07-28 15:48:59 -0700406 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis);
Jeff Brown51d45a72010-06-17 20:52:56 -0700407
Jeff Browna665ca82010-09-08 11:49:43 -0700408 virtual void setInputWindows(const Vector<InputWindow>& inputWindows);
409 virtual void setFocusedApplication(const InputApplication* inputApplication);
410 virtual void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown50de30a2010-06-22 01:27:15 -0700411
Jeff Browna665ca82010-09-08 11:49:43 -0700412 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel, bool monitor);
Jeff Browne839a582010-04-22 18:58:52 -0700413 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel);
414
415private:
416 template <typename T>
417 struct Link {
418 T* next;
419 T* prev;
420 };
421
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700422 struct InjectionState {
423 mutable int32_t refCount;
424
425 int32_t injectorPid;
426 int32_t injectorUid;
427 int32_t injectionResult; // initially INPUT_EVENT_INJECTION_PENDING
428 bool injectionIsAsync; // set to true if injection is not waiting for the result
429 int32_t pendingForegroundDispatches; // the number of foreground dispatches in progress
430 };
431
Jeff Browne839a582010-04-22 18:58:52 -0700432 struct EventEntry : Link<EventEntry> {
433 enum {
434 TYPE_SENTINEL,
435 TYPE_CONFIGURATION_CHANGED,
436 TYPE_KEY,
437 TYPE_MOTION
438 };
439
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700440 mutable int32_t refCount;
Jeff Browne839a582010-04-22 18:58:52 -0700441 int32_t type;
442 nsecs_t eventTime;
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700443 InjectionState* injectionState;
Jeff Brown51d45a72010-06-17 20:52:56 -0700444
Jeff Brown54bc2812010-06-15 01:31:58 -0700445 bool dispatchInProgress; // initially false, set to true while dispatching
Jeff Brown51d45a72010-06-17 20:52:56 -0700446
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700447 inline bool isInjected() { return injectionState != NULL; }
Jeff Browne839a582010-04-22 18:58:52 -0700448 };
449
450 struct ConfigurationChangedEntry : EventEntry {
Jeff Browne839a582010-04-22 18:58:52 -0700451 };
452
453 struct KeyEntry : EventEntry {
454 int32_t deviceId;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700455 int32_t source;
Jeff Browne839a582010-04-22 18:58:52 -0700456 uint32_t policyFlags;
457 int32_t action;
458 int32_t flags;
459 int32_t keyCode;
460 int32_t scanCode;
461 int32_t metaState;
462 int32_t repeatCount;
463 nsecs_t downTime;
Jeff Browna665ca82010-09-08 11:49:43 -0700464
465 bool syntheticRepeat; // set to true for synthetic key repeats
466
467 enum InterceptKeyResult {
468 INTERCEPT_KEY_RESULT_UNKNOWN,
469 INTERCEPT_KEY_RESULT_SKIP,
470 INTERCEPT_KEY_RESULT_CONTINUE,
471 };
472 InterceptKeyResult interceptKeyResult; // set based on the interception result
Jeff Browne839a582010-04-22 18:58:52 -0700473 };
474
475 struct MotionSample {
476 MotionSample* next;
477
478 nsecs_t eventTime;
479 PointerCoords pointerCoords[MAX_POINTERS];
480 };
481
482 struct MotionEntry : EventEntry {
483 int32_t deviceId;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700484 int32_t source;
Jeff Browne839a582010-04-22 18:58:52 -0700485 uint32_t policyFlags;
486 int32_t action;
Jeff Brownaf30ff62010-09-01 17:01:00 -0700487 int32_t flags;
Jeff Browne839a582010-04-22 18:58:52 -0700488 int32_t metaState;
489 int32_t edgeFlags;
490 float xPrecision;
491 float yPrecision;
492 nsecs_t downTime;
493 uint32_t pointerCount;
494 int32_t pointerIds[MAX_POINTERS];
495
496 // Linked list of motion samples associated with this motion event.
497 MotionSample firstSample;
498 MotionSample* lastSample;
Jeff Brown542412c2010-08-18 15:51:08 -0700499
500 uint32_t countSamples() const;
Jeff Browne839a582010-04-22 18:58:52 -0700501 };
502
Jeff Brown54bc2812010-06-15 01:31:58 -0700503 // Tracks the progress of dispatching a particular event to a particular connection.
Jeff Browne839a582010-04-22 18:58:52 -0700504 struct DispatchEntry : Link<DispatchEntry> {
505 EventEntry* eventEntry; // the event to dispatch
506 int32_t targetFlags;
507 float xOffset;
508 float yOffset;
Jeff Browne839a582010-04-22 18:58:52 -0700509
510 // True if dispatch has started.
511 bool inProgress;
512
513 // For motion events:
514 // Pointer to the first motion sample to dispatch in this cycle.
515 // Usually NULL to indicate that the list of motion samples begins at
516 // MotionEntry::firstSample. Otherwise, some samples were dispatched in a previous
517 // cycle and this pointer indicates the location of the first remainining sample
518 // to dispatch during the current cycle.
519 MotionSample* headMotionSample;
520 // Pointer to a motion sample to dispatch in the next cycle if the dispatcher was
521 // unable to send all motion samples during this cycle. On the next cycle,
522 // headMotionSample will be initialized to tailMotionSample and tailMotionSample
523 // will be set to NULL.
524 MotionSample* tailMotionSample;
Jeff Brownf67c53e2010-07-28 15:48:59 -0700525
Jeff Brown53a415e2010-09-15 15:18:56 -0700526 inline bool hasForegroundTarget() const {
527 return targetFlags & InputTarget::FLAG_FOREGROUND;
Jeff Browna665ca82010-09-08 11:49:43 -0700528 }
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700529
530 inline bool isSplit() const {
531 return targetFlags & InputTarget::FLAG_SPLIT;
532 }
Jeff Browne839a582010-04-22 18:58:52 -0700533 };
534
Jeff Brown54bc2812010-06-15 01:31:58 -0700535 // A command entry captures state and behavior for an action to be performed in the
536 // dispatch loop after the initial processing has taken place. It is essentially
537 // a kind of continuation used to postpone sensitive policy interactions to a point
538 // in the dispatch loop where it is safe to release the lock (generally after finishing
539 // the critical parts of the dispatch cycle).
540 //
541 // The special thing about commands is that they can voluntarily release and reacquire
542 // the dispatcher lock at will. Initially when the command starts running, the
543 // dispatcher lock is held. However, if the command needs to call into the policy to
544 // do some work, it can release the lock, do the work, then reacquire the lock again
545 // before returning.
546 //
547 // This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch
548 // never calls into the policy while holding its lock.
549 //
550 // Commands are implicitly 'LockedInterruptible'.
551 struct CommandEntry;
552 typedef void (InputDispatcher::*Command)(CommandEntry* commandEntry);
553
Jeff Brown51d45a72010-06-17 20:52:56 -0700554 class Connection;
Jeff Brown54bc2812010-06-15 01:31:58 -0700555 struct CommandEntry : Link<CommandEntry> {
556 CommandEntry();
557 ~CommandEntry();
558
559 Command command;
560
561 // parameters for the command (usage varies by command)
Jeff Brown51d45a72010-06-17 20:52:56 -0700562 sp<Connection> connection;
Jeff Browna665ca82010-09-08 11:49:43 -0700563 nsecs_t eventTime;
564 KeyEntry* keyEntry;
565 sp<InputChannel> inputChannel;
566 sp<InputApplicationHandle> inputApplicationHandle;
Jeff Browna665ca82010-09-08 11:49:43 -0700567 int32_t userActivityEventType;
Jeff Brown54bc2812010-06-15 01:31:58 -0700568 };
569
570 // Generic queue implementation.
Jeff Browne839a582010-04-22 18:58:52 -0700571 template <typename T>
572 struct Queue {
Jeff Browna665ca82010-09-08 11:49:43 -0700573 T headSentinel;
574 T tailSentinel;
Jeff Browne839a582010-04-22 18:58:52 -0700575
576 inline Queue() {
Jeff Browna665ca82010-09-08 11:49:43 -0700577 headSentinel.prev = NULL;
578 headSentinel.next = & tailSentinel;
579 tailSentinel.prev = & headSentinel;
580 tailSentinel.next = NULL;
Jeff Browne839a582010-04-22 18:58:52 -0700581 }
582
Jeff Browna665ca82010-09-08 11:49:43 -0700583 inline bool isEmpty() const {
584 return headSentinel.next == & tailSentinel;
Jeff Browne839a582010-04-22 18:58:52 -0700585 }
586
587 inline void enqueueAtTail(T* entry) {
Jeff Browna665ca82010-09-08 11:49:43 -0700588 T* last = tailSentinel.prev;
Jeff Browne839a582010-04-22 18:58:52 -0700589 last->next = entry;
590 entry->prev = last;
Jeff Browna665ca82010-09-08 11:49:43 -0700591 entry->next = & tailSentinel;
592 tailSentinel.prev = entry;
Jeff Browne839a582010-04-22 18:58:52 -0700593 }
594
595 inline void enqueueAtHead(T* entry) {
Jeff Browna665ca82010-09-08 11:49:43 -0700596 T* first = headSentinel.next;
597 headSentinel.next = entry;
598 entry->prev = & headSentinel;
Jeff Browne839a582010-04-22 18:58:52 -0700599 entry->next = first;
600 first->prev = entry;
601 }
602
603 inline void dequeue(T* entry) {
604 entry->prev->next = entry->next;
605 entry->next->prev = entry->prev;
606 }
607
608 inline T* dequeueAtHead() {
Jeff Browna665ca82010-09-08 11:49:43 -0700609 T* first = headSentinel.next;
Jeff Browne839a582010-04-22 18:58:52 -0700610 dequeue(first);
611 return first;
612 }
Jeff Brown53a415e2010-09-15 15:18:56 -0700613
614 uint32_t count() const;
Jeff Browne839a582010-04-22 18:58:52 -0700615 };
616
617 /* Allocates queue entries and performs reference counting as needed. */
618 class Allocator {
619 public:
620 Allocator();
621
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700622 InjectionState* obtainInjectionState(int32_t injectorPid, int32_t injectorUid);
Jeff Brown51d45a72010-06-17 20:52:56 -0700623 ConfigurationChangedEntry* obtainConfigurationChangedEntry(nsecs_t eventTime);
624 KeyEntry* obtainKeyEntry(nsecs_t eventTime,
Jeff Brown5c1ed842010-07-14 18:48:53 -0700625 int32_t deviceId, int32_t source, uint32_t policyFlags, int32_t action,
Jeff Brown51d45a72010-06-17 20:52:56 -0700626 int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState,
627 int32_t repeatCount, nsecs_t downTime);
628 MotionEntry* obtainMotionEntry(nsecs_t eventTime,
Jeff Brown5c1ed842010-07-14 18:48:53 -0700629 int32_t deviceId, int32_t source, uint32_t policyFlags, int32_t action,
Jeff Brownaf30ff62010-09-01 17:01:00 -0700630 int32_t flags, int32_t metaState, int32_t edgeFlags,
631 float xPrecision, float yPrecision,
Jeff Brown51d45a72010-06-17 20:52:56 -0700632 nsecs_t downTime, uint32_t pointerCount,
633 const int32_t* pointerIds, const PointerCoords* pointerCoords);
Jeff Browna665ca82010-09-08 11:49:43 -0700634 DispatchEntry* obtainDispatchEntry(EventEntry* eventEntry,
Jeff Brown53a415e2010-09-15 15:18:56 -0700635 int32_t targetFlags, float xOffset, float yOffset);
Jeff Brown54bc2812010-06-15 01:31:58 -0700636 CommandEntry* obtainCommandEntry(Command command);
Jeff Browne839a582010-04-22 18:58:52 -0700637
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700638 void releaseInjectionState(InjectionState* injectionState);
Jeff Browne839a582010-04-22 18:58:52 -0700639 void releaseEventEntry(EventEntry* entry);
640 void releaseConfigurationChangedEntry(ConfigurationChangedEntry* entry);
641 void releaseKeyEntry(KeyEntry* entry);
642 void releaseMotionEntry(MotionEntry* entry);
643 void releaseDispatchEntry(DispatchEntry* entry);
Jeff Brown54bc2812010-06-15 01:31:58 -0700644 void releaseCommandEntry(CommandEntry* entry);
Jeff Browne839a582010-04-22 18:58:52 -0700645
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700646 void recycleKeyEntry(KeyEntry* entry);
647
Jeff Browne839a582010-04-22 18:58:52 -0700648 void appendMotionSample(MotionEntry* motionEntry,
Jeff Brown51d45a72010-06-17 20:52:56 -0700649 nsecs_t eventTime, const PointerCoords* pointerCoords);
Jeff Browne839a582010-04-22 18:58:52 -0700650
651 private:
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700652 Pool<InjectionState> mInjectionStatePool;
Jeff Browne839a582010-04-22 18:58:52 -0700653 Pool<ConfigurationChangedEntry> mConfigurationChangeEntryPool;
654 Pool<KeyEntry> mKeyEntryPool;
655 Pool<MotionEntry> mMotionEntryPool;
656 Pool<MotionSample> mMotionSamplePool;
657 Pool<DispatchEntry> mDispatchEntryPool;
Jeff Brown54bc2812010-06-15 01:31:58 -0700658 Pool<CommandEntry> mCommandEntryPool;
Jeff Brown51d45a72010-06-17 20:52:56 -0700659
660 void initializeEventEntry(EventEntry* entry, int32_t type, nsecs_t eventTime);
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700661 void releaseEventEntryInjectionState(EventEntry* entry);
Jeff Browne839a582010-04-22 18:58:52 -0700662 };
663
Jeff Browna665ca82010-09-08 11:49:43 -0700664 /* Tracks dispatched key and motion event state so that cancelation events can be
665 * synthesized when events are dropped. */
666 class InputState {
667 public:
668 // Specifies whether a given event will violate input state consistency.
669 enum Consistency {
670 // The event is consistent with the current input state.
671 CONSISTENT,
672 // The event is inconsistent with the current input state but applications
673 // will tolerate it. eg. Down followed by another down.
674 TOLERABLE,
675 // The event is inconsistent with the current input state and will probably
676 // cause applications to crash. eg. Up without prior down, move with
677 // unexpected number of pointers.
678 BROKEN
679 };
680
681 InputState();
682 ~InputState();
683
684 // Returns true if there is no state to be canceled.
685 bool isNeutral() const;
686
687 // Returns true if the input state believes it is out of sync.
688 bool isOutOfSync() const;
689
690 // Sets the input state to be out of sync if it is not neutral.
691 void setOutOfSync();
692
693 // Resets the input state out of sync flag.
694 void resetOutOfSync();
695
696 // Records tracking information for an event that has just been published.
697 // Returns whether the event is consistent with the current input state.
698 Consistency trackEvent(const EventEntry* entry);
699
700 // Records tracking information for a key event that has just been published.
701 // Returns whether the event is consistent with the current input state.
702 Consistency trackKey(const KeyEntry* entry);
703
704 // Records tracking information for a motion event that has just been published.
705 // Returns whether the event is consistent with the current input state.
706 Consistency trackMotion(const MotionEntry* entry);
707
708 // Synthesizes cancelation events for the current state.
709 void synthesizeCancelationEvents(Allocator* allocator,
710 Vector<EventEntry*>& outEvents) const;
711
712 // Clears the current state.
713 void clear();
714
715 private:
716 bool mIsOutOfSync;
717
718 struct KeyMemento {
719 int32_t deviceId;
720 int32_t source;
721 int32_t keyCode;
722 int32_t scanCode;
723 nsecs_t downTime;
724 };
725
726 struct MotionMemento {
727 int32_t deviceId;
728 int32_t source;
729 float xPrecision;
730 float yPrecision;
731 nsecs_t downTime;
732 uint32_t pointerCount;
733 int32_t pointerIds[MAX_POINTERS];
734 PointerCoords pointerCoords[MAX_POINTERS];
735
736 void setPointers(const MotionEntry* entry);
737 };
738
739 Vector<KeyMemento> mKeyMementos;
740 Vector<MotionMemento> mMotionMementos;
741 };
742
Jeff Browne839a582010-04-22 18:58:52 -0700743 /* Manages the dispatch state associated with a single input channel. */
744 class Connection : public RefBase {
745 protected:
746 virtual ~Connection();
747
748 public:
749 enum Status {
750 // Everything is peachy.
751 STATUS_NORMAL,
752 // An unrecoverable communication error has occurred.
753 STATUS_BROKEN,
Jeff Browne839a582010-04-22 18:58:52 -0700754 // The input channel has been unregistered.
755 STATUS_ZOMBIE
756 };
757
758 Status status;
759 sp<InputChannel> inputChannel;
760 InputPublisher inputPublisher;
Jeff Browna665ca82010-09-08 11:49:43 -0700761 InputState inputState;
Jeff Browne839a582010-04-22 18:58:52 -0700762 Queue<DispatchEntry> outboundQueue;
Jeff Browne839a582010-04-22 18:58:52 -0700763
764 nsecs_t lastEventTime; // the time when the event was originally captured
765 nsecs_t lastDispatchTime; // the time when the last event was dispatched
Jeff Browne839a582010-04-22 18:58:52 -0700766
767 explicit Connection(const sp<InputChannel>& inputChannel);
768
Jeff Brown54bc2812010-06-15 01:31:58 -0700769 inline const char* getInputChannelName() const { return inputChannel->getName().string(); }
770
771 const char* getStatusLabel() const;
Jeff Browne839a582010-04-22 18:58:52 -0700772
773 // Finds a DispatchEntry in the outbound queue associated with the specified event.
774 // Returns NULL if not found.
775 DispatchEntry* findQueuedDispatchEntryForEvent(const EventEntry* eventEntry) const;
776
Jeff Browne839a582010-04-22 18:58:52 -0700777 // Gets the time since the current event was originally obtained from the input driver.
Jeff Browna665ca82010-09-08 11:49:43 -0700778 inline double getEventLatencyMillis(nsecs_t currentTime) const {
Jeff Browne839a582010-04-22 18:58:52 -0700779 return (currentTime - lastEventTime) / 1000000.0;
780 }
781
782 // Gets the time since the current event entered the outbound dispatch queue.
Jeff Browna665ca82010-09-08 11:49:43 -0700783 inline double getDispatchLatencyMillis(nsecs_t currentTime) const {
Jeff Browne839a582010-04-22 18:58:52 -0700784 return (currentTime - lastDispatchTime) / 1000000.0;
785 }
786
Jeff Browne839a582010-04-22 18:58:52 -0700787 status_t initialize();
788 };
789
Jeff Brown54bc2812010-06-15 01:31:58 -0700790 sp<InputDispatcherPolicyInterface> mPolicy;
Jeff Browne839a582010-04-22 18:58:52 -0700791
792 Mutex mLock;
793
Jeff Browne839a582010-04-22 18:58:52 -0700794 Allocator mAllocator;
Jeff Brown59abe7e2010-09-13 23:17:30 -0700795 sp<Looper> mLooper;
Jeff Browne839a582010-04-22 18:58:52 -0700796
Jeff Browna665ca82010-09-08 11:49:43 -0700797 EventEntry* mPendingEvent;
Jeff Brown54bc2812010-06-15 01:31:58 -0700798 Queue<EventEntry> mInboundQueue;
799 Queue<CommandEntry> mCommandQueue;
800
Jeff Browna665ca82010-09-08 11:49:43 -0700801 Vector<EventEntry*> mTempCancelationEvents;
802
803 void dispatchOnceInnerLocked(nsecs_t keyRepeatTimeout, nsecs_t keyRepeatDelay,
804 nsecs_t* nextWakeupTime);
805
Jeff Brown59abe7e2010-09-13 23:17:30 -0700806 // Enqueues an inbound event. Returns true if mLooper->wake() should be called.
Jeff Browna665ca82010-09-08 11:49:43 -0700807 bool enqueueInboundEventLocked(EventEntry* entry);
808
809 // App switch latency optimization.
810 nsecs_t mAppSwitchDueTime;
811
812 static bool isAppSwitchKey(int32_t keyCode);
813 bool isAppSwitchPendingLocked();
814 bool detectPendingAppSwitchLocked(KeyEntry* inboundKeyEntry);
815 void resetPendingAppSwitchLocked(bool handled);
816
Jeff Browne839a582010-04-22 18:58:52 -0700817 // All registered connections mapped by receive pipe file descriptor.
818 KeyedVector<int, sp<Connection> > mConnectionsByReceiveFd;
819
Jeff Brown53a415e2010-09-15 15:18:56 -0700820 ssize_t getConnectionIndexLocked(const sp<InputChannel>& inputChannel);
Jeff Brown0cacb872010-08-17 15:59:26 -0700821
Jeff Browne839a582010-04-22 18:58:52 -0700822 // Active connections are connections that have a non-empty outbound queue.
Jeff Brown51d45a72010-06-17 20:52:56 -0700823 // We don't use a ref-counted pointer here because we explicitly abort connections
824 // during unregistration which causes the connection's outbound queue to be cleared
825 // and the connection itself to be deactivated.
Jeff Browne839a582010-04-22 18:58:52 -0700826 Vector<Connection*> mActiveConnections;
827
Jeff Browna665ca82010-09-08 11:49:43 -0700828 // Input channels that will receive a copy of all input events.
829 Vector<sp<InputChannel> > mMonitoringChannels;
Jeff Browne839a582010-04-22 18:58:52 -0700830
Jeff Browna665ca82010-09-08 11:49:43 -0700831 // Preallocated key event object used for policy inquiries.
832 KeyEvent mReusableKeyEvent;
Jeff Browne839a582010-04-22 18:58:52 -0700833
Jeff Brown51d45a72010-06-17 20:52:56 -0700834 // Event injection and synchronization.
835 Condition mInjectionResultAvailableCondition;
Jeff Browna665ca82010-09-08 11:49:43 -0700836 EventEntry* createEntryFromInjectedInputEventLocked(const InputEvent* event);
Jeff Brown51d45a72010-06-17 20:52:56 -0700837 void setInjectionResultLocked(EventEntry* entry, int32_t injectionResult);
838
Jeff Brownf67c53e2010-07-28 15:48:59 -0700839 Condition mInjectionSyncFinishedCondition;
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700840 void incrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brown53a415e2010-09-15 15:18:56 -0700841 void decrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brownf67c53e2010-07-28 15:48:59 -0700842
Jeff Brown542412c2010-08-18 15:51:08 -0700843 // Throttling state.
844 struct ThrottleState {
845 nsecs_t minTimeBetweenEvents;
846
847 nsecs_t lastEventTime;
848 int32_t lastDeviceId;
849 uint32_t lastSource;
850
851 uint32_t originalSampleCount; // only collected during debugging
852 } mThrottleState;
853
Jeff Browne839a582010-04-22 18:58:52 -0700854 // Key repeat tracking.
Jeff Browne839a582010-04-22 18:58:52 -0700855 struct KeyRepeatState {
856 KeyEntry* lastKeyEntry; // or null if no repeat
857 nsecs_t nextRepeatTime;
858 } mKeyRepeatState;
859
860 void resetKeyRepeatLocked();
Jeff Browna665ca82010-09-08 11:49:43 -0700861 KeyEntry* synthesizeKeyRepeatLocked(nsecs_t currentTime, nsecs_t keyRepeatTimeout);
Jeff Browne839a582010-04-22 18:58:52 -0700862
Jeff Brown54bc2812010-06-15 01:31:58 -0700863 // Deferred command processing.
864 bool runCommandsLockedInterruptible();
865 CommandEntry* postCommandLocked(Command command);
866
Jeff Browna665ca82010-09-08 11:49:43 -0700867 // Inbound event processing.
868 void drainInboundQueueLocked();
Jeff Brownd8816c32010-09-16 14:07:33 -0700869 void releasePendingEventLocked();
870 void releaseInboundEventLocked(EventEntry* entry);
Jeff Browna665ca82010-09-08 11:49:43 -0700871 bool isEventFromReliableSourceLocked(EventEntry* entry);
Jeff Browne839a582010-04-22 18:58:52 -0700872
Jeff Browna665ca82010-09-08 11:49:43 -0700873 // Dispatch state.
874 bool mDispatchEnabled;
875 bool mDispatchFrozen;
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700876
Jeff Browna665ca82010-09-08 11:49:43 -0700877 Vector<InputWindow> mWindows;
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700878
879 const InputWindow* getWindowLocked(const sp<InputChannel>& inputChannel);
Jeff Browna665ca82010-09-08 11:49:43 -0700880
881 // Focus tracking for keys, trackball, etc.
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700882 const InputWindow* mFocusedWindow;
Jeff Browna665ca82010-09-08 11:49:43 -0700883
884 // Focus tracking for touch.
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700885 struct TouchedWindow {
886 const InputWindow* window;
887 int32_t targetFlags;
888 BitSet32 pointerIds;
889 sp<InputChannel> channel;
Jeff Browna665ca82010-09-08 11:49:43 -0700890 };
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700891 struct TouchState {
892 bool down;
893 bool split;
894 Vector<TouchedWindow> windows;
895
896 TouchState();
897 ~TouchState();
898 void reset();
899 void copyFrom(const TouchState& other);
900 void addOrUpdateWindow(const InputWindow* window, int32_t targetFlags, BitSet32 pointerIds);
901 void removeOutsideTouchWindows();
902 const InputWindow* getFirstForegroundWindow();
903 };
904
905 TouchState mTouchState;
906 TouchState mTempTouchState;
Jeff Browna665ca82010-09-08 11:49:43 -0700907
908 // Focused application.
909 InputApplication* mFocusedApplication;
910 InputApplication mFocusedApplicationStorage; // preallocated storage for mFocusedApplication
911 void releaseFocusedApplicationLocked();
912
913 // Dispatch inbound events.
914 bool dispatchConfigurationChangedLocked(
915 nsecs_t currentTime, ConfigurationChangedEntry* entry);
916 bool dispatchKeyLocked(
917 nsecs_t currentTime, KeyEntry* entry, nsecs_t keyRepeatTimeout,
Jeff Brownd8816c32010-09-16 14:07:33 -0700918 bool dropEvent, nsecs_t* nextWakeupTime);
Jeff Browna665ca82010-09-08 11:49:43 -0700919 bool dispatchMotionLocked(
920 nsecs_t currentTime, MotionEntry* entry,
Jeff Brownd8816c32010-09-16 14:07:33 -0700921 bool dropEvent, nsecs_t* nextWakeupTime);
Jeff Brown54bc2812010-06-15 01:31:58 -0700922 void dispatchEventToCurrentInputTargetsLocked(
923 nsecs_t currentTime, EventEntry* entry, bool resumeWithAppendedMotionSample);
Jeff Browne839a582010-04-22 18:58:52 -0700924
Jeff Browna665ca82010-09-08 11:49:43 -0700925 void logOutboundKeyDetailsLocked(const char* prefix, const KeyEntry* entry);
926 void logOutboundMotionDetailsLocked(const char* prefix, const MotionEntry* entry);
927
928 // The input targets that were most recently identified for dispatch.
Jeff Browna665ca82010-09-08 11:49:43 -0700929 bool mCurrentInputTargetsValid; // false while targets are being recomputed
930 Vector<InputTarget> mCurrentInputTargets;
Jeff Browna665ca82010-09-08 11:49:43 -0700931
932 enum InputTargetWaitCause {
933 INPUT_TARGET_WAIT_CAUSE_NONE,
934 INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY,
935 INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY,
936 };
937
938 InputTargetWaitCause mInputTargetWaitCause;
939 nsecs_t mInputTargetWaitStartTime;
940 nsecs_t mInputTargetWaitTimeoutTime;
941 bool mInputTargetWaitTimeoutExpired;
942
943 // Finding targets for input events.
Jeff Brownd8816c32010-09-16 14:07:33 -0700944 void resetTargetsLocked();
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700945 void commitTargetsLocked();
Jeff Browna665ca82010-09-08 11:49:43 -0700946 int32_t handleTargetsNotReadyLocked(nsecs_t currentTime, const EventEntry* entry,
947 const InputApplication* application, const InputWindow* window,
948 nsecs_t* nextWakeupTime);
Jeff Brown53a415e2010-09-15 15:18:56 -0700949 void resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout,
950 const sp<InputChannel>& inputChannel);
951 nsecs_t getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime);
Jeff Browna665ca82010-09-08 11:49:43 -0700952 void resetANRTimeoutsLocked();
953
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700954 int32_t findFocusedWindowTargetsLocked(nsecs_t currentTime, const EventEntry* entry,
955 nsecs_t* nextWakeupTime);
956 int32_t findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry* entry,
957 nsecs_t* nextWakeupTime);
Jeff Browna665ca82010-09-08 11:49:43 -0700958
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700959 void addWindowTargetLocked(const InputWindow* window, int32_t targetFlags,
960 BitSet32 pointerIds);
Jeff Browna665ca82010-09-08 11:49:43 -0700961 void addMonitoringTargetsLocked();
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700962 bool shouldPokeUserActivityForCurrentInputTargetsLocked();
963 void pokeUserActivityLocked(nsecs_t eventTime, int32_t eventType);
964 bool checkInjectionPermission(const InputWindow* window, const InjectionState* injectionState);
Jeff Browna665ca82010-09-08 11:49:43 -0700965 bool isWindowObscuredLocked(const InputWindow* window);
Jeff Brown53a415e2010-09-15 15:18:56 -0700966 bool isWindowFinishedWithPreviousInputLocked(const InputWindow* window);
Jeff Brown53a415e2010-09-15 15:18:56 -0700967 String8 getApplicationWindowLabelLocked(const InputApplication* application,
968 const InputWindow* window);
Jeff Browna665ca82010-09-08 11:49:43 -0700969
Jeff Browne839a582010-04-22 18:58:52 -0700970 // Manage the dispatch cycle for a single connection.
Jeff Brown51d45a72010-06-17 20:52:56 -0700971 // These methods are deliberately not Interruptible because doing all of the work
972 // with the mutex held makes it easier to ensure that connection invariants are maintained.
973 // If needed, the methods post commands to run later once the critical bits are done.
974 void prepareDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Jeff Browne839a582010-04-22 18:58:52 -0700975 EventEntry* eventEntry, const InputTarget* inputTarget,
976 bool resumeWithAppendedMotionSample);
Jeff Brown53a415e2010-09-15 15:18:56 -0700977 void startDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown51d45a72010-06-17 20:52:56 -0700978 void finishDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Browna665ca82010-09-08 11:49:43 -0700979 void startNextDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown51d45a72010-06-17 20:52:56 -0700980 void abortDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Jeff Browne839a582010-04-22 18:58:52 -0700981 bool broken);
Jeff Brown53a415e2010-09-15 15:18:56 -0700982 void drainOutboundQueueLocked(Connection* connection);
Jeff Brown59abe7e2010-09-13 23:17:30 -0700983 static int handleReceiveCallback(int receiveFd, int events, void* data);
Jeff Browne839a582010-04-22 18:58:52 -0700984
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700985 // Splitting motion events across windows.
986 MotionEntry* splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds);
987
Jeff Browna665ca82010-09-08 11:49:43 -0700988 // Dump state.
989 void dumpDispatchStateLocked(String8& dump);
990 void logDispatchStateLocked();
991
Jeff Browne839a582010-04-22 18:58:52 -0700992 // Add or remove a connection to the mActiveConnections vector.
993 void activateConnectionLocked(Connection* connection);
994 void deactivateConnectionLocked(Connection* connection);
995
996 // Interesting events that we might like to log or tell the framework about.
Jeff Brown54bc2812010-06-15 01:31:58 -0700997 void onDispatchCycleStartedLocked(
Jeff Brown51d45a72010-06-17 20:52:56 -0700998 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown54bc2812010-06-15 01:31:58 -0700999 void onDispatchCycleFinishedLocked(
Jeff Brown51d45a72010-06-17 20:52:56 -07001000 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown54bc2812010-06-15 01:31:58 -07001001 void onDispatchCycleBrokenLocked(
Jeff Brown51d45a72010-06-17 20:52:56 -07001002 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown53a415e2010-09-15 15:18:56 -07001003 void onANRLocked(
1004 nsecs_t currentTime, const InputApplication* application, const InputWindow* window,
1005 nsecs_t eventTime, nsecs_t waitStartTime);
Jeff Brown54bc2812010-06-15 01:31:58 -07001006
Jeff Brown51d45a72010-06-17 20:52:56 -07001007 // Outbound policy interactions.
Jeff Browna665ca82010-09-08 11:49:43 -07001008 void doNotifyConfigurationChangedInterruptible(CommandEntry* commandEntry);
Jeff Brown54bc2812010-06-15 01:31:58 -07001009 void doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown53a415e2010-09-15 15:18:56 -07001010 void doNotifyANRLockedInterruptible(CommandEntry* commandEntry);
Jeff Browna665ca82010-09-08 11:49:43 -07001011 void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry);
1012 void doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown53a415e2010-09-15 15:18:56 -07001013
1014 // Statistics gathering.
1015 void updateDispatchStatisticsLocked(nsecs_t currentTime, const EventEntry* entry,
1016 int32_t injectionResult, nsecs_t timeSpentWaitingForApplication);
Jeff Browne839a582010-04-22 18:58:52 -07001017};
1018
1019/* Enqueues and dispatches input events, endlessly. */
1020class InputDispatcherThread : public Thread {
1021public:
1022 explicit InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher);
1023 ~InputDispatcherThread();
1024
1025private:
1026 virtual bool threadLoop();
1027
1028 sp<InputDispatcherInterface> mDispatcher;
1029};
1030
1031} // namespace android
1032
Jeff Browna665ca82010-09-08 11:49:43 -07001033#endif // _UI_INPUT_DISPATCHER_H