blob: 2c22cadcfd4253a7e5ba39cad2f1b0ec1f460158 [file] [log] [blame]
Jeff Brown46b9ac02010-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 Brown46b9ac02010-04-22 18:58:52 -070021#include <ui/InputTransport.h>
22#include <utils/KeyedVector.h>
23#include <utils/Vector.h>
24#include <utils/threads.h>
25#include <utils/Timers.h>
26#include <utils/RefBase.h>
27#include <utils/String8.h>
Jeff Brown4fe6c3e2010-09-13 23:17:30 -070028#include <utils/Looper.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070029#include <utils/Pool.h>
Jeff Brown01ce2e92010-09-26 22:20:12 -070030#include <utils/BitSet.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070031
32#include <stddef.h>
33#include <unistd.h>
Jeff Brownb88102f2010-09-08 11:49:43 -070034#include <limits.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070035
36
37namespace android {
38
Jeff Brown9c3cda02010-06-15 01:31:58 -070039/*
Jeff Brown7fbdc842010-06-17 20:52:56 -070040 * Constants used to report the outcome of input event injection.
41 */
42enum {
43 /* (INTERNAL USE ONLY) Specifies that injection is pending and its outcome is unknown. */
44 INPUT_EVENT_INJECTION_PENDING = -1,
45
46 /* Injection succeeded. */
47 INPUT_EVENT_INJECTION_SUCCEEDED = 0,
48
49 /* Injection failed because the injector did not have permission to inject
50 * into the application with input focus. */
51 INPUT_EVENT_INJECTION_PERMISSION_DENIED = 1,
52
53 /* Injection failed because there were no available input targets. */
54 INPUT_EVENT_INJECTION_FAILED = 2,
55
56 /* Injection failed due to a timeout. */
57 INPUT_EVENT_INJECTION_TIMED_OUT = 3
58};
59
Jeff Brown6ec402b2010-07-28 15:48:59 -070060/*
61 * Constants used to determine the input event injection synchronization mode.
62 */
63enum {
64 /* Injection is asynchronous and is assumed always to be successful. */
65 INPUT_EVENT_INJECTION_SYNC_NONE = 0,
66
67 /* Waits for previous events to be dispatched so that the input dispatcher can determine
68 * whether input event injection willbe permitted based on the current input focus.
69 * Does not wait for the input event to finish processing. */
70 INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT = 1,
71
72 /* Waits for the input event to be completely processed. */
73 INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISHED = 2,
74};
75
Jeff Brown7fbdc842010-06-17 20:52:56 -070076
77/*
Jeff Brown9c3cda02010-06-15 01:31:58 -070078 * An input target specifies how an input event is to be dispatched to a particular window
79 * including the window's input channel, control flags, a timeout, and an X / Y offset to
80 * be added to input event coordinates to compensate for the absolute position of the
81 * window area.
82 */
83struct InputTarget {
84 enum {
Jeff Brown519e0242010-09-15 15:18:56 -070085 /* This flag indicates that the event is being delivered to a foreground application. */
86 FLAG_FOREGROUND = 0x01,
Jeff Brown9c3cda02010-06-15 01:31:58 -070087
Jeff Brown85a31762010-09-01 17:01:00 -070088 /* This flag indicates that a MotionEvent with AMOTION_EVENT_ACTION_DOWN falls outside
89 * of the area of this target and so should instead be delivered as an
90 * AMOTION_EVENT_ACTION_OUTSIDE to this target. */
Jeff Brown9c3cda02010-06-15 01:31:58 -070091 FLAG_OUTSIDE = 0x02,
92
Jeff Brown85a31762010-09-01 17:01:00 -070093 /* This flag indicates that the target of a MotionEvent is partly or wholly
94 * obscured by another visible window above it. The motion event should be
95 * delivered with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED. */
Jeff Brown01ce2e92010-09-26 22:20:12 -070096 FLAG_WINDOW_IS_OBSCURED = 0x04,
97
98 /* This flag indicates that a motion event is being split across multiple windows. */
99 FLAG_SPLIT = 0x08,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700100 };
101
102 // The input channel to be targeted.
103 sp<InputChannel> inputChannel;
104
105 // Flags for the input target.
106 int32_t flags;
107
Jeff Brown9c3cda02010-06-15 01:31:58 -0700108 // The x and y offset to add to a MotionEvent as it is delivered.
109 // (ignored for KeyEvents)
110 float xOffset, yOffset;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700111
112 // The window type of the input target.
113 int32_t windowType;
114
115 // The subset of pointer ids to include in motion events dispatched to this input target
116 // if FLAG_SPLIT is set.
117 BitSet32 pointerIds;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700118};
119
Jeff Brown7fbdc842010-06-17 20:52:56 -0700120
Jeff Brown9c3cda02010-06-15 01:31:58 -0700121/*
Jeff Brownb88102f2010-09-08 11:49:43 -0700122 * An input window describes the bounds of a window that can receive input.
123 */
124struct InputWindow {
125 // Window flags from WindowManager.LayoutParams
126 enum {
127 FLAG_ALLOW_LOCK_WHILE_SCREEN_ON = 0x00000001,
128 FLAG_DIM_BEHIND = 0x00000002,
129 FLAG_BLUR_BEHIND = 0x00000004,
130 FLAG_NOT_FOCUSABLE = 0x00000008,
131 FLAG_NOT_TOUCHABLE = 0x00000010,
132 FLAG_NOT_TOUCH_MODAL = 0x00000020,
133 FLAG_TOUCHABLE_WHEN_WAKING = 0x00000040,
134 FLAG_KEEP_SCREEN_ON = 0x00000080,
135 FLAG_LAYOUT_IN_SCREEN = 0x00000100,
136 FLAG_LAYOUT_NO_LIMITS = 0x00000200,
137 FLAG_FULLSCREEN = 0x00000400,
138 FLAG_FORCE_NOT_FULLSCREEN = 0x00000800,
139 FLAG_DITHER = 0x00001000,
140 FLAG_SECURE = 0x00002000,
141 FLAG_SCALED = 0x00004000,
142 FLAG_IGNORE_CHEEK_PRESSES = 0x00008000,
143 FLAG_LAYOUT_INSET_DECOR = 0x00010000,
144 FLAG_ALT_FOCUSABLE_IM = 0x00020000,
145 FLAG_WATCH_OUTSIDE_TOUCH = 0x00040000,
146 FLAG_SHOW_WHEN_LOCKED = 0x00080000,
147 FLAG_SHOW_WALLPAPER = 0x00100000,
148 FLAG_TURN_SCREEN_ON = 0x00200000,
149 FLAG_DISMISS_KEYGUARD = 0x00400000,
Jeff Brown01ce2e92010-09-26 22:20:12 -0700150 FLAG_SPLIT_TOUCH = 0x00800000,
Jeff Brownb88102f2010-09-08 11:49:43 -0700151 FLAG_KEEP_SURFACE_WHILE_ANIMATING = 0x10000000,
152 FLAG_COMPATIBLE_WINDOW = 0x20000000,
153 FLAG_SYSTEM_ERROR = 0x40000000,
154 };
155
156 // Window types from WindowManager.LayoutParams
157 enum {
158 FIRST_APPLICATION_WINDOW = 1,
159 TYPE_BASE_APPLICATION = 1,
160 TYPE_APPLICATION = 2,
161 TYPE_APPLICATION_STARTING = 3,
162 LAST_APPLICATION_WINDOW = 99,
163 FIRST_SUB_WINDOW = 1000,
164 TYPE_APPLICATION_PANEL = FIRST_SUB_WINDOW,
165 TYPE_APPLICATION_MEDIA = FIRST_SUB_WINDOW+1,
166 TYPE_APPLICATION_SUB_PANEL = FIRST_SUB_WINDOW+2,
167 TYPE_APPLICATION_ATTACHED_DIALOG = FIRST_SUB_WINDOW+3,
168 TYPE_APPLICATION_MEDIA_OVERLAY = FIRST_SUB_WINDOW+4,
169 LAST_SUB_WINDOW = 1999,
170 FIRST_SYSTEM_WINDOW = 2000,
171 TYPE_STATUS_BAR = FIRST_SYSTEM_WINDOW,
172 TYPE_SEARCH_BAR = FIRST_SYSTEM_WINDOW+1,
173 TYPE_PHONE = FIRST_SYSTEM_WINDOW+2,
174 TYPE_SYSTEM_ALERT = FIRST_SYSTEM_WINDOW+3,
175 TYPE_KEYGUARD = FIRST_SYSTEM_WINDOW+4,
176 TYPE_TOAST = FIRST_SYSTEM_WINDOW+5,
177 TYPE_SYSTEM_OVERLAY = FIRST_SYSTEM_WINDOW+6,
178 TYPE_PRIORITY_PHONE = FIRST_SYSTEM_WINDOW+7,
179 TYPE_SYSTEM_DIALOG = FIRST_SYSTEM_WINDOW+8,
180 TYPE_KEYGUARD_DIALOG = FIRST_SYSTEM_WINDOW+9,
181 TYPE_SYSTEM_ERROR = FIRST_SYSTEM_WINDOW+10,
182 TYPE_INPUT_METHOD = FIRST_SYSTEM_WINDOW+11,
183 TYPE_INPUT_METHOD_DIALOG= FIRST_SYSTEM_WINDOW+12,
184 TYPE_WALLPAPER = FIRST_SYSTEM_WINDOW+13,
185 TYPE_STATUS_BAR_PANEL = FIRST_SYSTEM_WINDOW+14,
186 LAST_SYSTEM_WINDOW = 2999,
187 };
188
189 sp<InputChannel> inputChannel;
Jeff Brown519e0242010-09-15 15:18:56 -0700190 String8 name;
Jeff Brownb88102f2010-09-08 11:49:43 -0700191 int32_t layoutParamsFlags;
192 int32_t layoutParamsType;
193 nsecs_t dispatchingTimeout;
194 int32_t frameLeft;
195 int32_t frameTop;
196 int32_t frameRight;
197 int32_t frameBottom;
198 int32_t visibleFrameLeft;
199 int32_t visibleFrameTop;
200 int32_t visibleFrameRight;
201 int32_t visibleFrameBottom;
202 int32_t touchableAreaLeft;
203 int32_t touchableAreaTop;
204 int32_t touchableAreaRight;
205 int32_t touchableAreaBottom;
206 bool visible;
Jeff Brown519e0242010-09-15 15:18:56 -0700207 bool canReceiveKeys;
Jeff Brownb88102f2010-09-08 11:49:43 -0700208 bool hasFocus;
209 bool hasWallpaper;
210 bool paused;
Jeff Brown519e0242010-09-15 15:18:56 -0700211 int32_t layer;
Jeff Brownb88102f2010-09-08 11:49:43 -0700212 int32_t ownerPid;
213 int32_t ownerUid;
214
Jeff Brownb88102f2010-09-08 11:49:43 -0700215 bool touchableAreaContainsPoint(int32_t x, int32_t y) const;
Jeff Brown19dfc832010-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 Brownb88102f2010-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 Brown9c3cda02010-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 Brownb88102f2010-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 Brown519e0242010-09-15 15:18:56 -0700267 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
268 const sp<InputChannel>& inputChannel) = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700269
Jeff Brown9c3cda02010-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 Brownb21fb102010-09-07 10:44:57 -0700273 /* Gets the key repeat initial timeout or -1 if automatic key repeating is disabled. */
Jeff Brown9c3cda02010-06-15 01:31:58 -0700274 virtual nsecs_t getKeyRepeatTimeout() = 0;
275
Jeff Brownb21fb102010-09-07 10:44:57 -0700276 /* Gets the key repeat inter-key delay. */
277 virtual nsecs_t getKeyRepeatDelay() = 0;
278
Jeff Brownae9fc032010-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 Brownb88102f2010-09-08 11:49:43 -0700284
Jeff Brownb6997262010-10-08 22:31:17 -0700285 /* Intercepts a key event immediately before queueing it.
286 * The policy can use this method as an opportunity to perform power management functions
287 * and early event preprocessing such as updating policy flags.
288 *
289 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
290 * should be dispatched to applications.
291 */
292 virtual void interceptKeyBeforeQueueing(nsecs_t when, int32_t deviceId,
293 int32_t action, int32_t& flags, int32_t keyCode, int32_t scanCode,
294 uint32_t& policyFlags) = 0;
295
296 /* Intercepts a generic touch, trackball or other event before queueing it.
297 * The policy can use this method as an opportunity to perform power management functions
298 * and early event preprocessing such as updating policy flags.
299 *
300 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
301 * should be dispatched to applications.
302 */
303 virtual void interceptGenericBeforeQueueing(nsecs_t when, uint32_t& policyFlags) = 0;
304
Jeff Brownb88102f2010-09-08 11:49:43 -0700305 /* Allows the policy a chance to intercept a key before dispatching. */
306 virtual bool interceptKeyBeforeDispatching(const sp<InputChannel>& inputChannel,
307 const KeyEvent* keyEvent, uint32_t policyFlags) = 0;
308
Jeff Brownb6997262010-10-08 22:31:17 -0700309 /* Notifies the policy about switch events.
310 */
311 virtual void notifySwitch(nsecs_t when,
312 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) = 0;
313
Jeff Brownb88102f2010-09-08 11:49:43 -0700314 /* Poke user activity for an event dispatched to a window. */
Jeff Brown01ce2e92010-09-26 22:20:12 -0700315 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType) = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700316
317 /* Checks whether a given application pid/uid has permission to inject input events
318 * into other applications.
319 *
320 * This method is special in that its implementation promises to be non-reentrant and
321 * is safe to call while holding other locks. (Most other methods make no such guarantees!)
322 */
323 virtual bool checkInjectEventsPermissionNonReentrant(
324 int32_t injectorPid, int32_t injectorUid) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700325};
326
327
Jeff Brown46b9ac02010-04-22 18:58:52 -0700328/* Notifies the system about input events generated by the input reader.
329 * The dispatcher is expected to be mostly asynchronous. */
330class InputDispatcherInterface : public virtual RefBase {
331protected:
332 InputDispatcherInterface() { }
333 virtual ~InputDispatcherInterface() { }
334
335public:
Jeff Brownb88102f2010-09-08 11:49:43 -0700336 /* Dumps the state of the input dispatcher.
337 *
338 * This method may be called on any thread (usually by the input manager). */
339 virtual void dump(String8& dump) = 0;
340
Jeff Brown46b9ac02010-04-22 18:58:52 -0700341 /* Runs a single iteration of the dispatch loop.
342 * Nominally processes one queued event, a timeout, or a response from an input consumer.
343 *
344 * This method should only be called on the input dispatcher thread.
345 */
346 virtual void dispatchOnce() = 0;
347
348 /* Notifies the dispatcher about new events.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700349 *
350 * These methods should only be called on the input reader thread.
351 */
Jeff Brown9c3cda02010-06-15 01:31:58 -0700352 virtual void notifyConfigurationChanged(nsecs_t eventTime) = 0;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700353 virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700354 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode,
355 int32_t scanCode, int32_t metaState, nsecs_t downTime) = 0;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700356 virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Brown85a31762010-09-01 17:01:00 -0700357 uint32_t policyFlags, int32_t action, int32_t flags,
358 int32_t metaState, int32_t edgeFlags,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700359 uint32_t pointerCount, const int32_t* pointerIds, const PointerCoords* pointerCoords,
360 float xPrecision, float yPrecision, nsecs_t downTime) = 0;
Jeff Brownb6997262010-10-08 22:31:17 -0700361 virtual void notifySwitch(nsecs_t when,
362 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700363
Jeff Brown7fbdc842010-06-17 20:52:56 -0700364 /* Injects an input event and optionally waits for sync.
Jeff Brown6ec402b2010-07-28 15:48:59 -0700365 * The synchronization mode determines whether the method blocks while waiting for
366 * input injection to proceed.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700367 * Returns one of the INPUT_EVENT_INJECTION_XXX constants.
368 *
369 * This method may be called on any thread (usually by the input manager).
370 */
371 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brown6ec402b2010-07-28 15:48:59 -0700372 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis) = 0;
Jeff Brown7fbdc842010-06-17 20:52:56 -0700373
Jeff Brownb88102f2010-09-08 11:49:43 -0700374 /* Sets the list of input windows.
375 *
376 * This method may be called on any thread (usually by the input manager).
377 */
378 virtual void setInputWindows(const Vector<InputWindow>& inputWindows) = 0;
379
380 /* Sets the focused application.
381 *
382 * This method may be called on any thread (usually by the input manager).
383 */
384 virtual void setFocusedApplication(const InputApplication* inputApplication) = 0;
385
386 /* Sets the input dispatching mode.
387 *
388 * This method may be called on any thread (usually by the input manager).
389 */
390 virtual void setInputDispatchMode(bool enabled, bool frozen) = 0;
391
Jeff Browne6504122010-09-27 14:52:15 -0700392 /* Transfers touch focus from the window associated with one channel to the
393 * window associated with the other channel.
394 *
395 * Returns true on success. False if the window did not actually have touch focus.
396 */
397 virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel,
398 const sp<InputChannel>& toChannel) = 0;
399
Jeff Brown46b9ac02010-04-22 18:58:52 -0700400 /* Registers or unregister input channels that may be used as targets for input events.
Jeff Brownb88102f2010-09-08 11:49:43 -0700401 * If monitor is true, the channel will receive a copy of all input events.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700402 *
403 * These methods may be called on any thread (usually by the input manager).
404 */
Jeff Brownb88102f2010-09-08 11:49:43 -0700405 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel, bool monitor) = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700406 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) = 0;
407};
408
Jeff Brown9c3cda02010-06-15 01:31:58 -0700409/* Dispatches events to input targets. Some functions of the input dispatcher, such as
410 * identifying input targets, are controlled by a separate policy object.
411 *
412 * IMPORTANT INVARIANT:
413 * Because the policy can potentially block or cause re-entrance into the input dispatcher,
414 * the input dispatcher never calls into the policy while holding its internal locks.
415 * The implementation is also carefully designed to recover from scenarios such as an
416 * input channel becoming unregistered while identifying input targets or processing timeouts.
417 *
418 * Methods marked 'Locked' must be called with the lock acquired.
419 *
420 * Methods marked 'LockedInterruptible' must be called with the lock acquired but
421 * may during the course of their execution release the lock, call into the policy, and
422 * then reacquire the lock. The caller is responsible for recovering gracefully.
423 *
424 * A 'LockedInterruptible' method may called a 'Locked' method, but NOT vice-versa.
425 */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700426class InputDispatcher : public InputDispatcherInterface {
427protected:
428 virtual ~InputDispatcher();
429
430public:
Jeff Brown9c3cda02010-06-15 01:31:58 -0700431 explicit InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700432
Jeff Brownb88102f2010-09-08 11:49:43 -0700433 virtual void dump(String8& dump);
434
Jeff Brown46b9ac02010-04-22 18:58:52 -0700435 virtual void dispatchOnce();
436
Jeff Brown9c3cda02010-06-15 01:31:58 -0700437 virtual void notifyConfigurationChanged(nsecs_t eventTime);
Jeff Brownc5ed5912010-07-14 18:48:53 -0700438 virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700439 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode,
440 int32_t scanCode, int32_t metaState, nsecs_t downTime);
Jeff Brownc5ed5912010-07-14 18:48:53 -0700441 virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Brown85a31762010-09-01 17:01:00 -0700442 uint32_t policyFlags, int32_t action, int32_t flags,
443 int32_t metaState, int32_t edgeFlags,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700444 uint32_t pointerCount, const int32_t* pointerIds, const PointerCoords* pointerCoords,
445 float xPrecision, float yPrecision, nsecs_t downTime);
Jeff Brownb6997262010-10-08 22:31:17 -0700446 virtual void notifySwitch(nsecs_t when,
447 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) ;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700448
Jeff Brown7fbdc842010-06-17 20:52:56 -0700449 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brown6ec402b2010-07-28 15:48:59 -0700450 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700451
Jeff Brownb88102f2010-09-08 11:49:43 -0700452 virtual void setInputWindows(const Vector<InputWindow>& inputWindows);
453 virtual void setFocusedApplication(const InputApplication* inputApplication);
454 virtual void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown349703e2010-06-22 01:27:15 -0700455
Jeff Browne6504122010-09-27 14:52:15 -0700456 virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel,
457 const sp<InputChannel>& toChannel);
458
Jeff Brownb88102f2010-09-08 11:49:43 -0700459 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel, bool monitor);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700460 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel);
461
462private:
463 template <typename T>
464 struct Link {
465 T* next;
466 T* prev;
467 };
468
Jeff Brown01ce2e92010-09-26 22:20:12 -0700469 struct InjectionState {
470 mutable int32_t refCount;
471
472 int32_t injectorPid;
473 int32_t injectorUid;
474 int32_t injectionResult; // initially INPUT_EVENT_INJECTION_PENDING
475 bool injectionIsAsync; // set to true if injection is not waiting for the result
476 int32_t pendingForegroundDispatches; // the number of foreground dispatches in progress
477 };
478
Jeff Brown46b9ac02010-04-22 18:58:52 -0700479 struct EventEntry : Link<EventEntry> {
480 enum {
481 TYPE_SENTINEL,
482 TYPE_CONFIGURATION_CHANGED,
483 TYPE_KEY,
484 TYPE_MOTION
485 };
486
Jeff Brown01ce2e92010-09-26 22:20:12 -0700487 mutable int32_t refCount;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700488 int32_t type;
489 nsecs_t eventTime;
Jeff Brownb6997262010-10-08 22:31:17 -0700490 uint32_t policyFlags;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700491 InjectionState* injectionState;
Jeff Brown7fbdc842010-06-17 20:52:56 -0700492
Jeff Brown9c3cda02010-06-15 01:31:58 -0700493 bool dispatchInProgress; // initially false, set to true while dispatching
Jeff Brown7fbdc842010-06-17 20:52:56 -0700494
Jeff Brown01ce2e92010-09-26 22:20:12 -0700495 inline bool isInjected() { return injectionState != NULL; }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700496 };
497
498 struct ConfigurationChangedEntry : EventEntry {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700499 };
500
501 struct KeyEntry : EventEntry {
502 int32_t deviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700503 int32_t source;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700504 int32_t action;
505 int32_t flags;
506 int32_t keyCode;
507 int32_t scanCode;
508 int32_t metaState;
509 int32_t repeatCount;
510 nsecs_t downTime;
Jeff Brownb88102f2010-09-08 11:49:43 -0700511
512 bool syntheticRepeat; // set to true for synthetic key repeats
513
514 enum InterceptKeyResult {
515 INTERCEPT_KEY_RESULT_UNKNOWN,
516 INTERCEPT_KEY_RESULT_SKIP,
517 INTERCEPT_KEY_RESULT_CONTINUE,
518 };
519 InterceptKeyResult interceptKeyResult; // set based on the interception result
Jeff Brown46b9ac02010-04-22 18:58:52 -0700520 };
521
522 struct MotionSample {
523 MotionSample* next;
524
525 nsecs_t eventTime;
526 PointerCoords pointerCoords[MAX_POINTERS];
527 };
528
529 struct MotionEntry : EventEntry {
530 int32_t deviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700531 int32_t source;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700532 int32_t action;
Jeff Brown85a31762010-09-01 17:01:00 -0700533 int32_t flags;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700534 int32_t metaState;
535 int32_t edgeFlags;
536 float xPrecision;
537 float yPrecision;
538 nsecs_t downTime;
539 uint32_t pointerCount;
540 int32_t pointerIds[MAX_POINTERS];
541
542 // Linked list of motion samples associated with this motion event.
543 MotionSample firstSample;
544 MotionSample* lastSample;
Jeff Brownae9fc032010-08-18 15:51:08 -0700545
546 uint32_t countSamples() const;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700547 };
548
Jeff Brown9c3cda02010-06-15 01:31:58 -0700549 // Tracks the progress of dispatching a particular event to a particular connection.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700550 struct DispatchEntry : Link<DispatchEntry> {
551 EventEntry* eventEntry; // the event to dispatch
552 int32_t targetFlags;
553 float xOffset;
554 float yOffset;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700555
556 // True if dispatch has started.
557 bool inProgress;
558
559 // For motion events:
560 // Pointer to the first motion sample to dispatch in this cycle.
561 // Usually NULL to indicate that the list of motion samples begins at
562 // MotionEntry::firstSample. Otherwise, some samples were dispatched in a previous
563 // cycle and this pointer indicates the location of the first remainining sample
564 // to dispatch during the current cycle.
565 MotionSample* headMotionSample;
566 // Pointer to a motion sample to dispatch in the next cycle if the dispatcher was
567 // unable to send all motion samples during this cycle. On the next cycle,
568 // headMotionSample will be initialized to tailMotionSample and tailMotionSample
569 // will be set to NULL.
570 MotionSample* tailMotionSample;
Jeff Brown6ec402b2010-07-28 15:48:59 -0700571
Jeff Brown519e0242010-09-15 15:18:56 -0700572 inline bool hasForegroundTarget() const {
573 return targetFlags & InputTarget::FLAG_FOREGROUND;
Jeff Brownb88102f2010-09-08 11:49:43 -0700574 }
Jeff Brown01ce2e92010-09-26 22:20:12 -0700575
576 inline bool isSplit() const {
577 return targetFlags & InputTarget::FLAG_SPLIT;
578 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700579 };
580
Jeff Brown9c3cda02010-06-15 01:31:58 -0700581 // A command entry captures state and behavior for an action to be performed in the
582 // dispatch loop after the initial processing has taken place. It is essentially
583 // a kind of continuation used to postpone sensitive policy interactions to a point
584 // in the dispatch loop where it is safe to release the lock (generally after finishing
585 // the critical parts of the dispatch cycle).
586 //
587 // The special thing about commands is that they can voluntarily release and reacquire
588 // the dispatcher lock at will. Initially when the command starts running, the
589 // dispatcher lock is held. However, if the command needs to call into the policy to
590 // do some work, it can release the lock, do the work, then reacquire the lock again
591 // before returning.
592 //
593 // This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch
594 // never calls into the policy while holding its lock.
595 //
596 // Commands are implicitly 'LockedInterruptible'.
597 struct CommandEntry;
598 typedef void (InputDispatcher::*Command)(CommandEntry* commandEntry);
599
Jeff Brown7fbdc842010-06-17 20:52:56 -0700600 class Connection;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700601 struct CommandEntry : Link<CommandEntry> {
602 CommandEntry();
603 ~CommandEntry();
604
605 Command command;
606
607 // parameters for the command (usage varies by command)
Jeff Brown7fbdc842010-06-17 20:52:56 -0700608 sp<Connection> connection;
Jeff Brownb88102f2010-09-08 11:49:43 -0700609 nsecs_t eventTime;
610 KeyEntry* keyEntry;
611 sp<InputChannel> inputChannel;
612 sp<InputApplicationHandle> inputApplicationHandle;
Jeff Brownb88102f2010-09-08 11:49:43 -0700613 int32_t userActivityEventType;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700614 };
615
616 // Generic queue implementation.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700617 template <typename T>
618 struct Queue {
Jeff Brownb88102f2010-09-08 11:49:43 -0700619 T headSentinel;
620 T tailSentinel;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700621
622 inline Queue() {
Jeff Brownb88102f2010-09-08 11:49:43 -0700623 headSentinel.prev = NULL;
624 headSentinel.next = & tailSentinel;
625 tailSentinel.prev = & headSentinel;
626 tailSentinel.next = NULL;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700627 }
628
Jeff Brownb88102f2010-09-08 11:49:43 -0700629 inline bool isEmpty() const {
630 return headSentinel.next == & tailSentinel;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700631 }
632
633 inline void enqueueAtTail(T* entry) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700634 T* last = tailSentinel.prev;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700635 last->next = entry;
636 entry->prev = last;
Jeff Brownb88102f2010-09-08 11:49:43 -0700637 entry->next = & tailSentinel;
638 tailSentinel.prev = entry;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700639 }
640
641 inline void enqueueAtHead(T* entry) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700642 T* first = headSentinel.next;
643 headSentinel.next = entry;
644 entry->prev = & headSentinel;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700645 entry->next = first;
646 first->prev = entry;
647 }
648
649 inline void dequeue(T* entry) {
650 entry->prev->next = entry->next;
651 entry->next->prev = entry->prev;
652 }
653
654 inline T* dequeueAtHead() {
Jeff Brownb88102f2010-09-08 11:49:43 -0700655 T* first = headSentinel.next;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700656 dequeue(first);
657 return first;
658 }
Jeff Brown519e0242010-09-15 15:18:56 -0700659
660 uint32_t count() const;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700661 };
662
663 /* Allocates queue entries and performs reference counting as needed. */
664 class Allocator {
665 public:
666 Allocator();
667
Jeff Brown01ce2e92010-09-26 22:20:12 -0700668 InjectionState* obtainInjectionState(int32_t injectorPid, int32_t injectorUid);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700669 ConfigurationChangedEntry* obtainConfigurationChangedEntry(nsecs_t eventTime);
670 KeyEntry* obtainKeyEntry(nsecs_t eventTime,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700671 int32_t deviceId, int32_t source, uint32_t policyFlags, int32_t action,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700672 int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState,
673 int32_t repeatCount, nsecs_t downTime);
674 MotionEntry* obtainMotionEntry(nsecs_t eventTime,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700675 int32_t deviceId, int32_t source, uint32_t policyFlags, int32_t action,
Jeff Brown85a31762010-09-01 17:01:00 -0700676 int32_t flags, int32_t metaState, int32_t edgeFlags,
677 float xPrecision, float yPrecision,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700678 nsecs_t downTime, uint32_t pointerCount,
679 const int32_t* pointerIds, const PointerCoords* pointerCoords);
Jeff Brownb88102f2010-09-08 11:49:43 -0700680 DispatchEntry* obtainDispatchEntry(EventEntry* eventEntry,
Jeff Brown519e0242010-09-15 15:18:56 -0700681 int32_t targetFlags, float xOffset, float yOffset);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700682 CommandEntry* obtainCommandEntry(Command command);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700683
Jeff Brown01ce2e92010-09-26 22:20:12 -0700684 void releaseInjectionState(InjectionState* injectionState);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700685 void releaseEventEntry(EventEntry* entry);
686 void releaseConfigurationChangedEntry(ConfigurationChangedEntry* entry);
687 void releaseKeyEntry(KeyEntry* entry);
688 void releaseMotionEntry(MotionEntry* entry);
689 void releaseDispatchEntry(DispatchEntry* entry);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700690 void releaseCommandEntry(CommandEntry* entry);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700691
Jeff Brown01ce2e92010-09-26 22:20:12 -0700692 void recycleKeyEntry(KeyEntry* entry);
693
Jeff Brown46b9ac02010-04-22 18:58:52 -0700694 void appendMotionSample(MotionEntry* motionEntry,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700695 nsecs_t eventTime, const PointerCoords* pointerCoords);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700696
697 private:
Jeff Brown01ce2e92010-09-26 22:20:12 -0700698 Pool<InjectionState> mInjectionStatePool;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700699 Pool<ConfigurationChangedEntry> mConfigurationChangeEntryPool;
700 Pool<KeyEntry> mKeyEntryPool;
701 Pool<MotionEntry> mMotionEntryPool;
702 Pool<MotionSample> mMotionSamplePool;
703 Pool<DispatchEntry> mDispatchEntryPool;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700704 Pool<CommandEntry> mCommandEntryPool;
Jeff Brown7fbdc842010-06-17 20:52:56 -0700705
Jeff Brownb6997262010-10-08 22:31:17 -0700706 void initializeEventEntry(EventEntry* entry, int32_t type, nsecs_t eventTime,
707 uint32_t policyFlags);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700708 void releaseEventEntryInjectionState(EventEntry* entry);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700709 };
710
Jeff Brownb88102f2010-09-08 11:49:43 -0700711 /* Tracks dispatched key and motion event state so that cancelation events can be
712 * synthesized when events are dropped. */
713 class InputState {
714 public:
715 // Specifies whether a given event will violate input state consistency.
716 enum Consistency {
717 // The event is consistent with the current input state.
718 CONSISTENT,
719 // The event is inconsistent with the current input state but applications
720 // will tolerate it. eg. Down followed by another down.
721 TOLERABLE,
722 // The event is inconsistent with the current input state and will probably
723 // cause applications to crash. eg. Up without prior down, move with
724 // unexpected number of pointers.
725 BROKEN
726 };
727
Jeff Brownb6997262010-10-08 22:31:17 -0700728 // Specifies the sources to cancel.
729 enum CancelationOptions {
730 CANCEL_ALL_EVENTS = 0,
731 CANCEL_POINTER_EVENTS = 1,
732 CANCEL_NON_POINTER_EVENTS = 2,
733 };
734
Jeff Brownb88102f2010-09-08 11:49:43 -0700735 InputState();
736 ~InputState();
737
738 // Returns true if there is no state to be canceled.
739 bool isNeutral() const;
740
Jeff Brownb88102f2010-09-08 11:49:43 -0700741 // Records tracking information for an event that has just been published.
742 // Returns whether the event is consistent with the current input state.
743 Consistency trackEvent(const EventEntry* entry);
744
745 // Records tracking information for a key event that has just been published.
746 // Returns whether the event is consistent with the current input state.
747 Consistency trackKey(const KeyEntry* entry);
748
749 // Records tracking information for a motion event that has just been published.
750 // Returns whether the event is consistent with the current input state.
751 Consistency trackMotion(const MotionEntry* entry);
752
Jeff Brownb6997262010-10-08 22:31:17 -0700753 // Synthesizes cancelation events for the current state and resets the tracked state.
754 void synthesizeCancelationEvents(nsecs_t currentTime, Allocator* allocator,
755 Vector<EventEntry*>& outEvents, CancelationOptions options);
Jeff Brownb88102f2010-09-08 11:49:43 -0700756
757 // Clears the current state.
758 void clear();
759
760 private:
Jeff Brownb88102f2010-09-08 11:49:43 -0700761 struct KeyMemento {
762 int32_t deviceId;
763 int32_t source;
764 int32_t keyCode;
765 int32_t scanCode;
766 nsecs_t downTime;
767 };
768
769 struct MotionMemento {
770 int32_t deviceId;
771 int32_t source;
772 float xPrecision;
773 float yPrecision;
774 nsecs_t downTime;
775 uint32_t pointerCount;
776 int32_t pointerIds[MAX_POINTERS];
777 PointerCoords pointerCoords[MAX_POINTERS];
778
779 void setPointers(const MotionEntry* entry);
780 };
781
782 Vector<KeyMemento> mKeyMementos;
783 Vector<MotionMemento> mMotionMementos;
Jeff Brownb6997262010-10-08 22:31:17 -0700784
785 static bool shouldCancelEvent(int32_t eventSource, CancelationOptions options);
Jeff Brownb88102f2010-09-08 11:49:43 -0700786 };
787
Jeff Brown46b9ac02010-04-22 18:58:52 -0700788 /* Manages the dispatch state associated with a single input channel. */
789 class Connection : public RefBase {
790 protected:
791 virtual ~Connection();
792
793 public:
794 enum Status {
795 // Everything is peachy.
796 STATUS_NORMAL,
797 // An unrecoverable communication error has occurred.
798 STATUS_BROKEN,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700799 // The input channel has been unregistered.
800 STATUS_ZOMBIE
801 };
802
803 Status status;
804 sp<InputChannel> inputChannel;
805 InputPublisher inputPublisher;
Jeff Brownb88102f2010-09-08 11:49:43 -0700806 InputState inputState;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700807 Queue<DispatchEntry> outboundQueue;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700808
809 nsecs_t lastEventTime; // the time when the event was originally captured
810 nsecs_t lastDispatchTime; // the time when the last event was dispatched
Jeff Brown46b9ac02010-04-22 18:58:52 -0700811
812 explicit Connection(const sp<InputChannel>& inputChannel);
813
Jeff Brown9c3cda02010-06-15 01:31:58 -0700814 inline const char* getInputChannelName() const { return inputChannel->getName().string(); }
815
816 const char* getStatusLabel() const;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700817
818 // Finds a DispatchEntry in the outbound queue associated with the specified event.
819 // Returns NULL if not found.
820 DispatchEntry* findQueuedDispatchEntryForEvent(const EventEntry* eventEntry) const;
821
Jeff Brown46b9ac02010-04-22 18:58:52 -0700822 // Gets the time since the current event was originally obtained from the input driver.
Jeff Brownb88102f2010-09-08 11:49:43 -0700823 inline double getEventLatencyMillis(nsecs_t currentTime) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700824 return (currentTime - lastEventTime) / 1000000.0;
825 }
826
827 // Gets the time since the current event entered the outbound dispatch queue.
Jeff Brownb88102f2010-09-08 11:49:43 -0700828 inline double getDispatchLatencyMillis(nsecs_t currentTime) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700829 return (currentTime - lastDispatchTime) / 1000000.0;
830 }
831
Jeff Brown46b9ac02010-04-22 18:58:52 -0700832 status_t initialize();
833 };
834
Jeff Brownb6997262010-10-08 22:31:17 -0700835 enum DropReason {
836 DROP_REASON_NOT_DROPPED = 0,
837 DROP_REASON_POLICY = 1,
838 DROP_REASON_APP_SWITCH = 2,
839 DROP_REASON_DISABLED = 3,
840 };
841
Jeff Brown9c3cda02010-06-15 01:31:58 -0700842 sp<InputDispatcherPolicyInterface> mPolicy;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700843
844 Mutex mLock;
845
Jeff Brown46b9ac02010-04-22 18:58:52 -0700846 Allocator mAllocator;
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700847 sp<Looper> mLooper;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700848
Jeff Brownb88102f2010-09-08 11:49:43 -0700849 EventEntry* mPendingEvent;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700850 Queue<EventEntry> mInboundQueue;
851 Queue<CommandEntry> mCommandQueue;
852
Jeff Brownb88102f2010-09-08 11:49:43 -0700853 Vector<EventEntry*> mTempCancelationEvents;
854
855 void dispatchOnceInnerLocked(nsecs_t keyRepeatTimeout, nsecs_t keyRepeatDelay,
856 nsecs_t* nextWakeupTime);
857
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700858 // Enqueues an inbound event. Returns true if mLooper->wake() should be called.
Jeff Brownb88102f2010-09-08 11:49:43 -0700859 bool enqueueInboundEventLocked(EventEntry* entry);
860
Jeff Brownb6997262010-10-08 22:31:17 -0700861 // Cleans up input state when dropping an inbound event.
862 void dropInboundEventLocked(EventEntry* entry, DropReason dropReason);
863
Jeff Brownb88102f2010-09-08 11:49:43 -0700864 // App switch latency optimization.
Jeff Brownb6997262010-10-08 22:31:17 -0700865 bool mAppSwitchSawKeyDown;
Jeff Brownb88102f2010-09-08 11:49:43 -0700866 nsecs_t mAppSwitchDueTime;
867
Jeff Brownb6997262010-10-08 22:31:17 -0700868 static bool isAppSwitchKeyCode(int32_t keyCode);
869 bool isAppSwitchKeyEventLocked(KeyEntry* keyEntry);
Jeff Brownb88102f2010-09-08 11:49:43 -0700870 bool isAppSwitchPendingLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700871 void resetPendingAppSwitchLocked(bool handled);
872
Jeff Brown46b9ac02010-04-22 18:58:52 -0700873 // All registered connections mapped by receive pipe file descriptor.
874 KeyedVector<int, sp<Connection> > mConnectionsByReceiveFd;
875
Jeff Brown519e0242010-09-15 15:18:56 -0700876 ssize_t getConnectionIndexLocked(const sp<InputChannel>& inputChannel);
Jeff Brown2cbecea2010-08-17 15:59:26 -0700877
Jeff Brown46b9ac02010-04-22 18:58:52 -0700878 // Active connections are connections that have a non-empty outbound queue.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700879 // We don't use a ref-counted pointer here because we explicitly abort connections
880 // during unregistration which causes the connection's outbound queue to be cleared
881 // and the connection itself to be deactivated.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700882 Vector<Connection*> mActiveConnections;
883
Jeff Brownb88102f2010-09-08 11:49:43 -0700884 // Input channels that will receive a copy of all input events.
885 Vector<sp<InputChannel> > mMonitoringChannels;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700886
Jeff Brownb88102f2010-09-08 11:49:43 -0700887 // Preallocated key event object used for policy inquiries.
888 KeyEvent mReusableKeyEvent;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700889
Jeff Brown7fbdc842010-06-17 20:52:56 -0700890 // Event injection and synchronization.
891 Condition mInjectionResultAvailableCondition;
Jeff Brownb6997262010-10-08 22:31:17 -0700892 bool hasInjectionPermission(int32_t injectorPid, int32_t injectorUid);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700893 void setInjectionResultLocked(EventEntry* entry, int32_t injectionResult);
894
Jeff Brown6ec402b2010-07-28 15:48:59 -0700895 Condition mInjectionSyncFinishedCondition;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700896 void incrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brown519e0242010-09-15 15:18:56 -0700897 void decrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brown6ec402b2010-07-28 15:48:59 -0700898
Jeff Brownae9fc032010-08-18 15:51:08 -0700899 // Throttling state.
900 struct ThrottleState {
901 nsecs_t minTimeBetweenEvents;
902
903 nsecs_t lastEventTime;
904 int32_t lastDeviceId;
905 uint32_t lastSource;
906
907 uint32_t originalSampleCount; // only collected during debugging
908 } mThrottleState;
909
Jeff Brown46b9ac02010-04-22 18:58:52 -0700910 // Key repeat tracking.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700911 struct KeyRepeatState {
912 KeyEntry* lastKeyEntry; // or null if no repeat
913 nsecs_t nextRepeatTime;
914 } mKeyRepeatState;
915
916 void resetKeyRepeatLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700917 KeyEntry* synthesizeKeyRepeatLocked(nsecs_t currentTime, nsecs_t keyRepeatTimeout);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700918
Jeff Brown9c3cda02010-06-15 01:31:58 -0700919 // Deferred command processing.
920 bool runCommandsLockedInterruptible();
921 CommandEntry* postCommandLocked(Command command);
922
Jeff Brownb88102f2010-09-08 11:49:43 -0700923 // Inbound event processing.
924 void drainInboundQueueLocked();
Jeff Brown54a18252010-09-16 14:07:33 -0700925 void releasePendingEventLocked();
926 void releaseInboundEventLocked(EventEntry* entry);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700927
Jeff Brownb88102f2010-09-08 11:49:43 -0700928 // Dispatch state.
929 bool mDispatchEnabled;
930 bool mDispatchFrozen;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700931
Jeff Brownb88102f2010-09-08 11:49:43 -0700932 Vector<InputWindow> mWindows;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700933
934 const InputWindow* getWindowLocked(const sp<InputChannel>& inputChannel);
Jeff Brownb88102f2010-09-08 11:49:43 -0700935
936 // Focus tracking for keys, trackball, etc.
Jeff Brown01ce2e92010-09-26 22:20:12 -0700937 const InputWindow* mFocusedWindow;
Jeff Brownb88102f2010-09-08 11:49:43 -0700938
939 // Focus tracking for touch.
Jeff Brown01ce2e92010-09-26 22:20:12 -0700940 struct TouchedWindow {
941 const InputWindow* window;
942 int32_t targetFlags;
943 BitSet32 pointerIds;
944 sp<InputChannel> channel;
Jeff Brownb88102f2010-09-08 11:49:43 -0700945 };
Jeff Brown01ce2e92010-09-26 22:20:12 -0700946 struct TouchState {
947 bool down;
948 bool split;
949 Vector<TouchedWindow> windows;
950
951 TouchState();
952 ~TouchState();
953 void reset();
954 void copyFrom(const TouchState& other);
955 void addOrUpdateWindow(const InputWindow* window, int32_t targetFlags, BitSet32 pointerIds);
956 void removeOutsideTouchWindows();
957 const InputWindow* getFirstForegroundWindow();
958 };
959
960 TouchState mTouchState;
961 TouchState mTempTouchState;
Jeff Brownb88102f2010-09-08 11:49:43 -0700962
963 // Focused application.
964 InputApplication* mFocusedApplication;
965 InputApplication mFocusedApplicationStorage; // preallocated storage for mFocusedApplication
966 void releaseFocusedApplicationLocked();
967
968 // Dispatch inbound events.
969 bool dispatchConfigurationChangedLocked(
970 nsecs_t currentTime, ConfigurationChangedEntry* entry);
971 bool dispatchKeyLocked(
972 nsecs_t currentTime, KeyEntry* entry, nsecs_t keyRepeatTimeout,
Jeff Browne20c9e02010-10-11 14:20:19 -0700973 DropReason* dropReason, nsecs_t* nextWakeupTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700974 bool dispatchMotionLocked(
975 nsecs_t currentTime, MotionEntry* entry,
Jeff Browne20c9e02010-10-11 14:20:19 -0700976 DropReason* dropReason, nsecs_t* nextWakeupTime);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700977 void dispatchEventToCurrentInputTargetsLocked(
978 nsecs_t currentTime, EventEntry* entry, bool resumeWithAppendedMotionSample);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700979
Jeff Brownb88102f2010-09-08 11:49:43 -0700980 void logOutboundKeyDetailsLocked(const char* prefix, const KeyEntry* entry);
981 void logOutboundMotionDetailsLocked(const char* prefix, const MotionEntry* entry);
982
983 // The input targets that were most recently identified for dispatch.
Jeff Brownb88102f2010-09-08 11:49:43 -0700984 bool mCurrentInputTargetsValid; // false while targets are being recomputed
985 Vector<InputTarget> mCurrentInputTargets;
Jeff Brownb88102f2010-09-08 11:49:43 -0700986
987 enum InputTargetWaitCause {
988 INPUT_TARGET_WAIT_CAUSE_NONE,
989 INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY,
990 INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY,
991 };
992
993 InputTargetWaitCause mInputTargetWaitCause;
994 nsecs_t mInputTargetWaitStartTime;
995 nsecs_t mInputTargetWaitTimeoutTime;
996 bool mInputTargetWaitTimeoutExpired;
997
998 // Finding targets for input events.
Jeff Brown54a18252010-09-16 14:07:33 -0700999 void resetTargetsLocked();
Jeff Brown01ce2e92010-09-26 22:20:12 -07001000 void commitTargetsLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -07001001 int32_t handleTargetsNotReadyLocked(nsecs_t currentTime, const EventEntry* entry,
1002 const InputApplication* application, const InputWindow* window,
1003 nsecs_t* nextWakeupTime);
Jeff Brown519e0242010-09-15 15:18:56 -07001004 void resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout,
1005 const sp<InputChannel>& inputChannel);
1006 nsecs_t getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime);
Jeff Brownb88102f2010-09-08 11:49:43 -07001007 void resetANRTimeoutsLocked();
1008
Jeff Brown01ce2e92010-09-26 22:20:12 -07001009 int32_t findFocusedWindowTargetsLocked(nsecs_t currentTime, const EventEntry* entry,
1010 nsecs_t* nextWakeupTime);
1011 int32_t findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry* entry,
1012 nsecs_t* nextWakeupTime);
Jeff Brownb88102f2010-09-08 11:49:43 -07001013
Jeff Brown01ce2e92010-09-26 22:20:12 -07001014 void addWindowTargetLocked(const InputWindow* window, int32_t targetFlags,
1015 BitSet32 pointerIds);
Jeff Brownb88102f2010-09-08 11:49:43 -07001016 void addMonitoringTargetsLocked();
Jeff Brown01ce2e92010-09-26 22:20:12 -07001017 bool shouldPokeUserActivityForCurrentInputTargetsLocked();
1018 void pokeUserActivityLocked(nsecs_t eventTime, int32_t eventType);
1019 bool checkInjectionPermission(const InputWindow* window, const InjectionState* injectionState);
Jeff Brown19dfc832010-10-05 12:26:23 -07001020 bool isWindowObscuredAtPointLocked(const InputWindow* window, int32_t x, int32_t y) const;
Jeff Brown519e0242010-09-15 15:18:56 -07001021 bool isWindowFinishedWithPreviousInputLocked(const InputWindow* window);
Jeff Brown519e0242010-09-15 15:18:56 -07001022 String8 getApplicationWindowLabelLocked(const InputApplication* application,
1023 const InputWindow* window);
Jeff Brownb88102f2010-09-08 11:49:43 -07001024
Jeff Brown46b9ac02010-04-22 18:58:52 -07001025 // Manage the dispatch cycle for a single connection.
Jeff Brown7fbdc842010-06-17 20:52:56 -07001026 // These methods are deliberately not Interruptible because doing all of the work
1027 // with the mutex held makes it easier to ensure that connection invariants are maintained.
1028 // If needed, the methods post commands to run later once the critical bits are done.
1029 void prepareDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001030 EventEntry* eventEntry, const InputTarget* inputTarget,
1031 bool resumeWithAppendedMotionSample);
Jeff Brown519e0242010-09-15 15:18:56 -07001032 void startDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001033 void finishDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brownb88102f2010-09-08 11:49:43 -07001034 void startNextDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brownb6997262010-10-08 22:31:17 -07001035 void abortBrokenDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown519e0242010-09-15 15:18:56 -07001036 void drainOutboundQueueLocked(Connection* connection);
Jeff Brown4fe6c3e2010-09-13 23:17:30 -07001037 static int handleReceiveCallback(int receiveFd, int events, void* data);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001038
Jeff Brownb6997262010-10-08 22:31:17 -07001039 void synthesizeCancelationEventsForAllConnectionsLocked(
1040 InputState::CancelationOptions options, const char* reason);
1041 void synthesizeCancelationEventsForInputChannelLocked(const sp<InputChannel>& channel,
1042 InputState::CancelationOptions options, const char* reason);
1043 void synthesizeCancelationEventsForConnectionLocked(const sp<Connection>& connection,
1044 InputState::CancelationOptions options, const char* reason);
1045
Jeff Brown01ce2e92010-09-26 22:20:12 -07001046 // Splitting motion events across windows.
1047 MotionEntry* splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds);
1048
Jeff Brownb88102f2010-09-08 11:49:43 -07001049 // Dump state.
1050 void dumpDispatchStateLocked(String8& dump);
1051 void logDispatchStateLocked();
1052
Jeff Brown46b9ac02010-04-22 18:58:52 -07001053 // Add or remove a connection to the mActiveConnections vector.
1054 void activateConnectionLocked(Connection* connection);
1055 void deactivateConnectionLocked(Connection* connection);
1056
1057 // Interesting events that we might like to log or tell the framework about.
Jeff Brown9c3cda02010-06-15 01:31:58 -07001058 void onDispatchCycleStartedLocked(
Jeff Brown7fbdc842010-06-17 20:52:56 -07001059 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001060 void onDispatchCycleFinishedLocked(
Jeff Brown7fbdc842010-06-17 20:52:56 -07001061 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001062 void onDispatchCycleBrokenLocked(
Jeff Brown7fbdc842010-06-17 20:52:56 -07001063 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown519e0242010-09-15 15:18:56 -07001064 void onANRLocked(
1065 nsecs_t currentTime, const InputApplication* application, const InputWindow* window,
1066 nsecs_t eventTime, nsecs_t waitStartTime);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001067
Jeff Brown7fbdc842010-06-17 20:52:56 -07001068 // Outbound policy interactions.
Jeff Brownb88102f2010-09-08 11:49:43 -07001069 void doNotifyConfigurationChangedInterruptible(CommandEntry* commandEntry);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001070 void doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown519e0242010-09-15 15:18:56 -07001071 void doNotifyANRLockedInterruptible(CommandEntry* commandEntry);
Jeff Brownb88102f2010-09-08 11:49:43 -07001072 void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry);
1073 void doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown519e0242010-09-15 15:18:56 -07001074
1075 // Statistics gathering.
1076 void updateDispatchStatisticsLocked(nsecs_t currentTime, const EventEntry* entry,
1077 int32_t injectionResult, nsecs_t timeSpentWaitingForApplication);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001078};
1079
1080/* Enqueues and dispatches input events, endlessly. */
1081class InputDispatcherThread : public Thread {
1082public:
1083 explicit InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher);
1084 ~InputDispatcherThread();
1085
1086private:
1087 virtual bool threadLoop();
1088
1089 sp<InputDispatcherInterface> mDispatcher;
1090};
1091
1092} // namespace android
1093
Jeff Brownb88102f2010-09-08 11:49:43 -07001094#endif // _UI_INPUT_DISPATCHER_H