blob: 15a39257957f5ab0b3e1e749fe46f175bbac45b0 [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
Jeff Brown01ce2e92010-09-26 22:20:12 -0700112 // The subset of pointer ids to include in motion events dispatched to this input target
113 // if FLAG_SPLIT is set.
114 BitSet32 pointerIds;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700115};
116
Jeff Brown7fbdc842010-06-17 20:52:56 -0700117
Jeff Brown9c3cda02010-06-15 01:31:58 -0700118/*
Jeff Brownb88102f2010-09-08 11:49:43 -0700119 * An input window describes the bounds of a window that can receive input.
120 */
121struct InputWindow {
122 // Window flags from WindowManager.LayoutParams
123 enum {
124 FLAG_ALLOW_LOCK_WHILE_SCREEN_ON = 0x00000001,
125 FLAG_DIM_BEHIND = 0x00000002,
126 FLAG_BLUR_BEHIND = 0x00000004,
127 FLAG_NOT_FOCUSABLE = 0x00000008,
128 FLAG_NOT_TOUCHABLE = 0x00000010,
129 FLAG_NOT_TOUCH_MODAL = 0x00000020,
130 FLAG_TOUCHABLE_WHEN_WAKING = 0x00000040,
131 FLAG_KEEP_SCREEN_ON = 0x00000080,
132 FLAG_LAYOUT_IN_SCREEN = 0x00000100,
133 FLAG_LAYOUT_NO_LIMITS = 0x00000200,
134 FLAG_FULLSCREEN = 0x00000400,
135 FLAG_FORCE_NOT_FULLSCREEN = 0x00000800,
136 FLAG_DITHER = 0x00001000,
137 FLAG_SECURE = 0x00002000,
138 FLAG_SCALED = 0x00004000,
139 FLAG_IGNORE_CHEEK_PRESSES = 0x00008000,
140 FLAG_LAYOUT_INSET_DECOR = 0x00010000,
141 FLAG_ALT_FOCUSABLE_IM = 0x00020000,
142 FLAG_WATCH_OUTSIDE_TOUCH = 0x00040000,
143 FLAG_SHOW_WHEN_LOCKED = 0x00080000,
144 FLAG_SHOW_WALLPAPER = 0x00100000,
145 FLAG_TURN_SCREEN_ON = 0x00200000,
146 FLAG_DISMISS_KEYGUARD = 0x00400000,
Jeff Brown01ce2e92010-09-26 22:20:12 -0700147 FLAG_SPLIT_TOUCH = 0x00800000,
Jeff Brownb88102f2010-09-08 11:49:43 -0700148 FLAG_KEEP_SURFACE_WHILE_ANIMATING = 0x10000000,
149 FLAG_COMPATIBLE_WINDOW = 0x20000000,
150 FLAG_SYSTEM_ERROR = 0x40000000,
151 };
152
153 // Window types from WindowManager.LayoutParams
154 enum {
155 FIRST_APPLICATION_WINDOW = 1,
156 TYPE_BASE_APPLICATION = 1,
157 TYPE_APPLICATION = 2,
158 TYPE_APPLICATION_STARTING = 3,
159 LAST_APPLICATION_WINDOW = 99,
160 FIRST_SUB_WINDOW = 1000,
161 TYPE_APPLICATION_PANEL = FIRST_SUB_WINDOW,
162 TYPE_APPLICATION_MEDIA = FIRST_SUB_WINDOW+1,
163 TYPE_APPLICATION_SUB_PANEL = FIRST_SUB_WINDOW+2,
164 TYPE_APPLICATION_ATTACHED_DIALOG = FIRST_SUB_WINDOW+3,
165 TYPE_APPLICATION_MEDIA_OVERLAY = FIRST_SUB_WINDOW+4,
166 LAST_SUB_WINDOW = 1999,
167 FIRST_SYSTEM_WINDOW = 2000,
168 TYPE_STATUS_BAR = FIRST_SYSTEM_WINDOW,
169 TYPE_SEARCH_BAR = FIRST_SYSTEM_WINDOW+1,
170 TYPE_PHONE = FIRST_SYSTEM_WINDOW+2,
171 TYPE_SYSTEM_ALERT = FIRST_SYSTEM_WINDOW+3,
172 TYPE_KEYGUARD = FIRST_SYSTEM_WINDOW+4,
173 TYPE_TOAST = FIRST_SYSTEM_WINDOW+5,
174 TYPE_SYSTEM_OVERLAY = FIRST_SYSTEM_WINDOW+6,
175 TYPE_PRIORITY_PHONE = FIRST_SYSTEM_WINDOW+7,
176 TYPE_SYSTEM_DIALOG = FIRST_SYSTEM_WINDOW+8,
177 TYPE_KEYGUARD_DIALOG = FIRST_SYSTEM_WINDOW+9,
178 TYPE_SYSTEM_ERROR = FIRST_SYSTEM_WINDOW+10,
179 TYPE_INPUT_METHOD = FIRST_SYSTEM_WINDOW+11,
180 TYPE_INPUT_METHOD_DIALOG= FIRST_SYSTEM_WINDOW+12,
181 TYPE_WALLPAPER = FIRST_SYSTEM_WINDOW+13,
182 TYPE_STATUS_BAR_PANEL = FIRST_SYSTEM_WINDOW+14,
Jeff Brown3b2b3542010-10-15 00:54:27 -0700183 TYPE_SECURE_SYSTEM_OVERLAY = FIRST_SYSTEM_WINDOW+15,
Jeff Brownb88102f2010-09-08 11:49:43 -0700184 LAST_SYSTEM_WINDOW = 2999,
185 };
186
187 sp<InputChannel> inputChannel;
Jeff Brown519e0242010-09-15 15:18:56 -0700188 String8 name;
Jeff Brownb88102f2010-09-08 11:49:43 -0700189 int32_t layoutParamsFlags;
190 int32_t layoutParamsType;
191 nsecs_t dispatchingTimeout;
192 int32_t frameLeft;
193 int32_t frameTop;
194 int32_t frameRight;
195 int32_t frameBottom;
196 int32_t visibleFrameLeft;
197 int32_t visibleFrameTop;
198 int32_t visibleFrameRight;
199 int32_t visibleFrameBottom;
200 int32_t touchableAreaLeft;
201 int32_t touchableAreaTop;
202 int32_t touchableAreaRight;
203 int32_t touchableAreaBottom;
204 bool visible;
Jeff Brown519e0242010-09-15 15:18:56 -0700205 bool canReceiveKeys;
Jeff Brownb88102f2010-09-08 11:49:43 -0700206 bool hasFocus;
207 bool hasWallpaper;
208 bool paused;
Jeff Brown519e0242010-09-15 15:18:56 -0700209 int32_t layer;
Jeff Brownb88102f2010-09-08 11:49:43 -0700210 int32_t ownerPid;
211 int32_t ownerUid;
212
Jeff Brownb88102f2010-09-08 11:49:43 -0700213 bool touchableAreaContainsPoint(int32_t x, int32_t y) const;
Jeff Brown19dfc832010-10-05 12:26:23 -0700214 bool frameContainsPoint(int32_t x, int32_t y) const;
215
216 /* Returns true if the window is of a trusted type that is allowed to silently
217 * overlay other windows for the purpose of implementing the secure views feature.
218 * Trusted overlays, such as IME windows, can partly obscure other windows without causing
219 * motion events to be delivered to them with AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED.
220 */
221 bool isTrustedOverlay() const;
Jeff Brown46e75292010-11-10 16:53:45 -0800222
223 bool supportsSplitTouch() 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 Brown3915bb82010-11-05 15:02:16 -0700309 /* Allows the policy a chance to perform default processing for an unhandled key. */
310 virtual bool dispatchUnhandledKey(const sp<InputChannel>& inputChannel,
311 const KeyEvent* keyEvent, uint32_t policyFlags) = 0;
312
Jeff Brownb6997262010-10-08 22:31:17 -0700313 /* Notifies the policy about switch events.
314 */
315 virtual void notifySwitch(nsecs_t when,
316 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) = 0;
317
Jeff Brownb88102f2010-09-08 11:49:43 -0700318 /* Poke user activity for an event dispatched to a window. */
Jeff Brown01ce2e92010-09-26 22:20:12 -0700319 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType) = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700320
321 /* Checks whether a given application pid/uid has permission to inject input events
322 * into other applications.
323 *
324 * This method is special in that its implementation promises to be non-reentrant and
325 * is safe to call while holding other locks. (Most other methods make no such guarantees!)
326 */
327 virtual bool checkInjectEventsPermissionNonReentrant(
328 int32_t injectorPid, int32_t injectorUid) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700329};
330
331
Jeff Brown46b9ac02010-04-22 18:58:52 -0700332/* Notifies the system about input events generated by the input reader.
333 * The dispatcher is expected to be mostly asynchronous. */
334class InputDispatcherInterface : public virtual RefBase {
335protected:
336 InputDispatcherInterface() { }
337 virtual ~InputDispatcherInterface() { }
338
339public:
Jeff Brownb88102f2010-09-08 11:49:43 -0700340 /* Dumps the state of the input dispatcher.
341 *
342 * This method may be called on any thread (usually by the input manager). */
343 virtual void dump(String8& dump) = 0;
344
Jeff Brown46b9ac02010-04-22 18:58:52 -0700345 /* Runs a single iteration of the dispatch loop.
346 * Nominally processes one queued event, a timeout, or a response from an input consumer.
347 *
348 * This method should only be called on the input dispatcher thread.
349 */
350 virtual void dispatchOnce() = 0;
351
352 /* Notifies the dispatcher about new events.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700353 *
354 * These methods should only be called on the input reader thread.
355 */
Jeff Brown9c3cda02010-06-15 01:31:58 -0700356 virtual void notifyConfigurationChanged(nsecs_t eventTime) = 0;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700357 virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700358 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode,
359 int32_t scanCode, int32_t metaState, nsecs_t downTime) = 0;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700360 virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Brown85a31762010-09-01 17:01:00 -0700361 uint32_t policyFlags, int32_t action, int32_t flags,
362 int32_t metaState, int32_t edgeFlags,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700363 uint32_t pointerCount, const int32_t* pointerIds, const PointerCoords* pointerCoords,
364 float xPrecision, float yPrecision, nsecs_t downTime) = 0;
Jeff Brownb6997262010-10-08 22:31:17 -0700365 virtual void notifySwitch(nsecs_t when,
366 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700367
Jeff Brown7fbdc842010-06-17 20:52:56 -0700368 /* Injects an input event and optionally waits for sync.
Jeff Brown6ec402b2010-07-28 15:48:59 -0700369 * The synchronization mode determines whether the method blocks while waiting for
370 * input injection to proceed.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700371 * Returns one of the INPUT_EVENT_INJECTION_XXX constants.
372 *
373 * This method may be called on any thread (usually by the input manager).
374 */
375 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brown6ec402b2010-07-28 15:48:59 -0700376 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis) = 0;
Jeff Brown7fbdc842010-06-17 20:52:56 -0700377
Jeff Brownb88102f2010-09-08 11:49:43 -0700378 /* Sets the list of input windows.
379 *
380 * This method may be called on any thread (usually by the input manager).
381 */
382 virtual void setInputWindows(const Vector<InputWindow>& inputWindows) = 0;
383
384 /* Sets the focused application.
385 *
386 * This method may be called on any thread (usually by the input manager).
387 */
388 virtual void setFocusedApplication(const InputApplication* inputApplication) = 0;
389
390 /* Sets the input dispatching mode.
391 *
392 * This method may be called on any thread (usually by the input manager).
393 */
394 virtual void setInputDispatchMode(bool enabled, bool frozen) = 0;
395
Jeff Browne6504122010-09-27 14:52:15 -0700396 /* Transfers touch focus from the window associated with one channel to the
397 * window associated with the other channel.
398 *
399 * Returns true on success. False if the window did not actually have touch focus.
400 */
401 virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel,
402 const sp<InputChannel>& toChannel) = 0;
403
Jeff Brown46b9ac02010-04-22 18:58:52 -0700404 /* Registers or unregister input channels that may be used as targets for input events.
Jeff Brownb88102f2010-09-08 11:49:43 -0700405 * If monitor is true, the channel will receive a copy of all input events.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700406 *
407 * These methods may be called on any thread (usually by the input manager).
408 */
Jeff Brownb88102f2010-09-08 11:49:43 -0700409 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel, bool monitor) = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700410 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) = 0;
411};
412
Jeff Brown9c3cda02010-06-15 01:31:58 -0700413/* Dispatches events to input targets. Some functions of the input dispatcher, such as
414 * identifying input targets, are controlled by a separate policy object.
415 *
416 * IMPORTANT INVARIANT:
417 * Because the policy can potentially block or cause re-entrance into the input dispatcher,
418 * the input dispatcher never calls into the policy while holding its internal locks.
419 * The implementation is also carefully designed to recover from scenarios such as an
420 * input channel becoming unregistered while identifying input targets or processing timeouts.
421 *
422 * Methods marked 'Locked' must be called with the lock acquired.
423 *
424 * Methods marked 'LockedInterruptible' must be called with the lock acquired but
425 * may during the course of their execution release the lock, call into the policy, and
426 * then reacquire the lock. The caller is responsible for recovering gracefully.
427 *
428 * A 'LockedInterruptible' method may called a 'Locked' method, but NOT vice-versa.
429 */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700430class InputDispatcher : public InputDispatcherInterface {
431protected:
432 virtual ~InputDispatcher();
433
434public:
Jeff Brown9c3cda02010-06-15 01:31:58 -0700435 explicit InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700436
Jeff Brownb88102f2010-09-08 11:49:43 -0700437 virtual void dump(String8& dump);
438
Jeff Brown46b9ac02010-04-22 18:58:52 -0700439 virtual void dispatchOnce();
440
Jeff Brown9c3cda02010-06-15 01:31:58 -0700441 virtual void notifyConfigurationChanged(nsecs_t eventTime);
Jeff Brownc5ed5912010-07-14 18:48:53 -0700442 virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700443 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode,
444 int32_t scanCode, int32_t metaState, nsecs_t downTime);
Jeff Brownc5ed5912010-07-14 18:48:53 -0700445 virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Brown85a31762010-09-01 17:01:00 -0700446 uint32_t policyFlags, int32_t action, int32_t flags,
447 int32_t metaState, int32_t edgeFlags,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700448 uint32_t pointerCount, const int32_t* pointerIds, const PointerCoords* pointerCoords,
449 float xPrecision, float yPrecision, nsecs_t downTime);
Jeff Brownb6997262010-10-08 22:31:17 -0700450 virtual void notifySwitch(nsecs_t when,
451 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) ;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700452
Jeff Brown7fbdc842010-06-17 20:52:56 -0700453 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brown6ec402b2010-07-28 15:48:59 -0700454 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700455
Jeff Brownb88102f2010-09-08 11:49:43 -0700456 virtual void setInputWindows(const Vector<InputWindow>& inputWindows);
457 virtual void setFocusedApplication(const InputApplication* inputApplication);
458 virtual void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown349703e2010-06-22 01:27:15 -0700459
Jeff Browne6504122010-09-27 14:52:15 -0700460 virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel,
461 const sp<InputChannel>& toChannel);
462
Jeff Brownb88102f2010-09-08 11:49:43 -0700463 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel, bool monitor);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700464 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel);
465
466private:
467 template <typename T>
468 struct Link {
469 T* next;
470 T* prev;
471 };
472
Jeff Brown01ce2e92010-09-26 22:20:12 -0700473 struct InjectionState {
474 mutable int32_t refCount;
475
476 int32_t injectorPid;
477 int32_t injectorUid;
478 int32_t injectionResult; // initially INPUT_EVENT_INJECTION_PENDING
479 bool injectionIsAsync; // set to true if injection is not waiting for the result
480 int32_t pendingForegroundDispatches; // the number of foreground dispatches in progress
481 };
482
Jeff Brown46b9ac02010-04-22 18:58:52 -0700483 struct EventEntry : Link<EventEntry> {
484 enum {
485 TYPE_SENTINEL,
486 TYPE_CONFIGURATION_CHANGED,
487 TYPE_KEY,
488 TYPE_MOTION
489 };
490
Jeff Brown01ce2e92010-09-26 22:20:12 -0700491 mutable int32_t refCount;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700492 int32_t type;
493 nsecs_t eventTime;
Jeff Brownb6997262010-10-08 22:31:17 -0700494 uint32_t policyFlags;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700495 InjectionState* injectionState;
Jeff Brown7fbdc842010-06-17 20:52:56 -0700496
Jeff Brown9c3cda02010-06-15 01:31:58 -0700497 bool dispatchInProgress; // initially false, set to true while dispatching
Jeff Brown7fbdc842010-06-17 20:52:56 -0700498
Jeff Brown01ce2e92010-09-26 22:20:12 -0700499 inline bool isInjected() { return injectionState != NULL; }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700500 };
501
502 struct ConfigurationChangedEntry : EventEntry {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700503 };
504
505 struct KeyEntry : EventEntry {
506 int32_t deviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700507 int32_t source;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700508 int32_t action;
509 int32_t flags;
510 int32_t keyCode;
511 int32_t scanCode;
512 int32_t metaState;
513 int32_t repeatCount;
514 nsecs_t downTime;
Jeff Brownb88102f2010-09-08 11:49:43 -0700515
516 bool syntheticRepeat; // set to true for synthetic key repeats
517
518 enum InterceptKeyResult {
519 INTERCEPT_KEY_RESULT_UNKNOWN,
520 INTERCEPT_KEY_RESULT_SKIP,
521 INTERCEPT_KEY_RESULT_CONTINUE,
522 };
523 InterceptKeyResult interceptKeyResult; // set based on the interception result
Jeff Brown46b9ac02010-04-22 18:58:52 -0700524 };
525
526 struct MotionSample {
527 MotionSample* next;
528
529 nsecs_t eventTime;
530 PointerCoords pointerCoords[MAX_POINTERS];
531 };
532
533 struct MotionEntry : EventEntry {
534 int32_t deviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700535 int32_t source;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700536 int32_t action;
Jeff Brown85a31762010-09-01 17:01:00 -0700537 int32_t flags;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700538 int32_t metaState;
539 int32_t edgeFlags;
540 float xPrecision;
541 float yPrecision;
542 nsecs_t downTime;
543 uint32_t pointerCount;
544 int32_t pointerIds[MAX_POINTERS];
545
546 // Linked list of motion samples associated with this motion event.
547 MotionSample firstSample;
548 MotionSample* lastSample;
Jeff Brownae9fc032010-08-18 15:51:08 -0700549
550 uint32_t countSamples() const;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700551 };
552
Jeff Brown9c3cda02010-06-15 01:31:58 -0700553 // Tracks the progress of dispatching a particular event to a particular connection.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700554 struct DispatchEntry : Link<DispatchEntry> {
555 EventEntry* eventEntry; // the event to dispatch
556 int32_t targetFlags;
557 float xOffset;
558 float yOffset;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700559
560 // True if dispatch has started.
561 bool inProgress;
562
563 // For motion events:
564 // Pointer to the first motion sample to dispatch in this cycle.
565 // Usually NULL to indicate that the list of motion samples begins at
566 // MotionEntry::firstSample. Otherwise, some samples were dispatched in a previous
567 // cycle and this pointer indicates the location of the first remainining sample
568 // to dispatch during the current cycle.
569 MotionSample* headMotionSample;
570 // Pointer to a motion sample to dispatch in the next cycle if the dispatcher was
571 // unable to send all motion samples during this cycle. On the next cycle,
572 // headMotionSample will be initialized to tailMotionSample and tailMotionSample
573 // will be set to NULL.
574 MotionSample* tailMotionSample;
Jeff Brown6ec402b2010-07-28 15:48:59 -0700575
Jeff Brown519e0242010-09-15 15:18:56 -0700576 inline bool hasForegroundTarget() const {
577 return targetFlags & InputTarget::FLAG_FOREGROUND;
Jeff Brownb88102f2010-09-08 11:49:43 -0700578 }
Jeff Brown01ce2e92010-09-26 22:20:12 -0700579
580 inline bool isSplit() const {
581 return targetFlags & InputTarget::FLAG_SPLIT;
582 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700583 };
584
Jeff Brown9c3cda02010-06-15 01:31:58 -0700585 // A command entry captures state and behavior for an action to be performed in the
586 // dispatch loop after the initial processing has taken place. It is essentially
587 // a kind of continuation used to postpone sensitive policy interactions to a point
588 // in the dispatch loop where it is safe to release the lock (generally after finishing
589 // the critical parts of the dispatch cycle).
590 //
591 // The special thing about commands is that they can voluntarily release and reacquire
592 // the dispatcher lock at will. Initially when the command starts running, the
593 // dispatcher lock is held. However, if the command needs to call into the policy to
594 // do some work, it can release the lock, do the work, then reacquire the lock again
595 // before returning.
596 //
597 // This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch
598 // never calls into the policy while holding its lock.
599 //
600 // Commands are implicitly 'LockedInterruptible'.
601 struct CommandEntry;
602 typedef void (InputDispatcher::*Command)(CommandEntry* commandEntry);
603
Jeff Brown7fbdc842010-06-17 20:52:56 -0700604 class Connection;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700605 struct CommandEntry : Link<CommandEntry> {
606 CommandEntry();
607 ~CommandEntry();
608
609 Command command;
610
611 // parameters for the command (usage varies by command)
Jeff Brown7fbdc842010-06-17 20:52:56 -0700612 sp<Connection> connection;
Jeff Brownb88102f2010-09-08 11:49:43 -0700613 nsecs_t eventTime;
614 KeyEntry* keyEntry;
615 sp<InputChannel> inputChannel;
616 sp<InputApplicationHandle> inputApplicationHandle;
Jeff Brownb88102f2010-09-08 11:49:43 -0700617 int32_t userActivityEventType;
Jeff Brown3915bb82010-11-05 15:02:16 -0700618 bool handled;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700619 };
620
621 // Generic queue implementation.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700622 template <typename T>
623 struct Queue {
Jeff Brownb88102f2010-09-08 11:49:43 -0700624 T headSentinel;
625 T tailSentinel;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700626
627 inline Queue() {
Jeff Brownb88102f2010-09-08 11:49:43 -0700628 headSentinel.prev = NULL;
629 headSentinel.next = & tailSentinel;
630 tailSentinel.prev = & headSentinel;
631 tailSentinel.next = NULL;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700632 }
633
Jeff Brownb88102f2010-09-08 11:49:43 -0700634 inline bool isEmpty() const {
635 return headSentinel.next == & tailSentinel;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700636 }
637
638 inline void enqueueAtTail(T* entry) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700639 T* last = tailSentinel.prev;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700640 last->next = entry;
641 entry->prev = last;
Jeff Brownb88102f2010-09-08 11:49:43 -0700642 entry->next = & tailSentinel;
643 tailSentinel.prev = entry;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700644 }
645
646 inline void enqueueAtHead(T* entry) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700647 T* first = headSentinel.next;
648 headSentinel.next = entry;
649 entry->prev = & headSentinel;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700650 entry->next = first;
651 first->prev = entry;
652 }
653
654 inline void dequeue(T* entry) {
655 entry->prev->next = entry->next;
656 entry->next->prev = entry->prev;
657 }
658
659 inline T* dequeueAtHead() {
Jeff Brownb88102f2010-09-08 11:49:43 -0700660 T* first = headSentinel.next;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700661 dequeue(first);
662 return first;
663 }
Jeff Brown519e0242010-09-15 15:18:56 -0700664
665 uint32_t count() const;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700666 };
667
668 /* Allocates queue entries and performs reference counting as needed. */
669 class Allocator {
670 public:
671 Allocator();
672
Jeff Brown01ce2e92010-09-26 22:20:12 -0700673 InjectionState* obtainInjectionState(int32_t injectorPid, int32_t injectorUid);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700674 ConfigurationChangedEntry* obtainConfigurationChangedEntry(nsecs_t eventTime);
675 KeyEntry* obtainKeyEntry(nsecs_t eventTime,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700676 int32_t deviceId, int32_t source, uint32_t policyFlags, int32_t action,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700677 int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState,
678 int32_t repeatCount, nsecs_t downTime);
679 MotionEntry* obtainMotionEntry(nsecs_t eventTime,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700680 int32_t deviceId, int32_t source, uint32_t policyFlags, int32_t action,
Jeff Brown85a31762010-09-01 17:01:00 -0700681 int32_t flags, int32_t metaState, int32_t edgeFlags,
682 float xPrecision, float yPrecision,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700683 nsecs_t downTime, uint32_t pointerCount,
684 const int32_t* pointerIds, const PointerCoords* pointerCoords);
Jeff Brownb88102f2010-09-08 11:49:43 -0700685 DispatchEntry* obtainDispatchEntry(EventEntry* eventEntry,
Jeff Brown519e0242010-09-15 15:18:56 -0700686 int32_t targetFlags, float xOffset, float yOffset);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700687 CommandEntry* obtainCommandEntry(Command command);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700688
Jeff Brown01ce2e92010-09-26 22:20:12 -0700689 void releaseInjectionState(InjectionState* injectionState);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700690 void releaseEventEntry(EventEntry* entry);
691 void releaseConfigurationChangedEntry(ConfigurationChangedEntry* entry);
692 void releaseKeyEntry(KeyEntry* entry);
693 void releaseMotionEntry(MotionEntry* entry);
694 void releaseDispatchEntry(DispatchEntry* entry);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700695 void releaseCommandEntry(CommandEntry* entry);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700696
Jeff Brown01ce2e92010-09-26 22:20:12 -0700697 void recycleKeyEntry(KeyEntry* entry);
698
Jeff Brown46b9ac02010-04-22 18:58:52 -0700699 void appendMotionSample(MotionEntry* motionEntry,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700700 nsecs_t eventTime, const PointerCoords* pointerCoords);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700701
702 private:
Jeff Brown01ce2e92010-09-26 22:20:12 -0700703 Pool<InjectionState> mInjectionStatePool;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700704 Pool<ConfigurationChangedEntry> mConfigurationChangeEntryPool;
705 Pool<KeyEntry> mKeyEntryPool;
706 Pool<MotionEntry> mMotionEntryPool;
707 Pool<MotionSample> mMotionSamplePool;
708 Pool<DispatchEntry> mDispatchEntryPool;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700709 Pool<CommandEntry> mCommandEntryPool;
Jeff Brown7fbdc842010-06-17 20:52:56 -0700710
Jeff Brownb6997262010-10-08 22:31:17 -0700711 void initializeEventEntry(EventEntry* entry, int32_t type, nsecs_t eventTime,
712 uint32_t policyFlags);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700713 void releaseEventEntryInjectionState(EventEntry* entry);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700714 };
715
Jeff Brownb88102f2010-09-08 11:49:43 -0700716 /* Tracks dispatched key and motion event state so that cancelation events can be
717 * synthesized when events are dropped. */
718 class InputState {
719 public:
720 // Specifies whether a given event will violate input state consistency.
721 enum Consistency {
722 // The event is consistent with the current input state.
723 CONSISTENT,
724 // The event is inconsistent with the current input state but applications
725 // will tolerate it. eg. Down followed by another down.
726 TOLERABLE,
727 // The event is inconsistent with the current input state and will probably
728 // cause applications to crash. eg. Up without prior down, move with
729 // unexpected number of pointers.
730 BROKEN
731 };
732
Jeff Brownb6997262010-10-08 22:31:17 -0700733 // Specifies the sources to cancel.
734 enum CancelationOptions {
735 CANCEL_ALL_EVENTS = 0,
736 CANCEL_POINTER_EVENTS = 1,
737 CANCEL_NON_POINTER_EVENTS = 2,
738 };
739
Jeff Brownb88102f2010-09-08 11:49:43 -0700740 InputState();
741 ~InputState();
742
743 // Returns true if there is no state to be canceled.
744 bool isNeutral() const;
745
Jeff Brownb88102f2010-09-08 11:49:43 -0700746 // Records tracking information for an event that has just been published.
747 // Returns whether the event is consistent with the current input state.
748 Consistency trackEvent(const EventEntry* entry);
749
750 // Records tracking information for a key event that has just been published.
751 // Returns whether the event is consistent with the current input state.
752 Consistency trackKey(const KeyEntry* entry);
753
754 // Records tracking information for a motion event that has just been published.
755 // Returns whether the event is consistent with the current input state.
756 Consistency trackMotion(const MotionEntry* entry);
757
Jeff Brownb6997262010-10-08 22:31:17 -0700758 // Synthesizes cancelation events for the current state and resets the tracked state.
759 void synthesizeCancelationEvents(nsecs_t currentTime, Allocator* allocator,
760 Vector<EventEntry*>& outEvents, CancelationOptions options);
Jeff Brownb88102f2010-09-08 11:49:43 -0700761
762 // Clears the current state.
763 void clear();
764
Jeff Brown9c9f1a32010-10-11 18:32:20 -0700765 // Copies pointer-related parts of the input state to another instance.
766 void copyPointerStateTo(InputState& other) const;
767
Jeff Brownb88102f2010-09-08 11:49:43 -0700768 private:
Jeff Brownb88102f2010-09-08 11:49:43 -0700769 struct KeyMemento {
770 int32_t deviceId;
771 int32_t source;
772 int32_t keyCode;
773 int32_t scanCode;
774 nsecs_t downTime;
775 };
776
777 struct MotionMemento {
778 int32_t deviceId;
779 int32_t source;
780 float xPrecision;
781 float yPrecision;
782 nsecs_t downTime;
783 uint32_t pointerCount;
784 int32_t pointerIds[MAX_POINTERS];
785 PointerCoords pointerCoords[MAX_POINTERS];
786
787 void setPointers(const MotionEntry* entry);
788 };
789
790 Vector<KeyMemento> mKeyMementos;
791 Vector<MotionMemento> mMotionMementos;
Jeff Brownb6997262010-10-08 22:31:17 -0700792
793 static bool shouldCancelEvent(int32_t eventSource, CancelationOptions options);
Jeff Brownb88102f2010-09-08 11:49:43 -0700794 };
795
Jeff Brown46b9ac02010-04-22 18:58:52 -0700796 /* Manages the dispatch state associated with a single input channel. */
797 class Connection : public RefBase {
798 protected:
799 virtual ~Connection();
800
801 public:
802 enum Status {
803 // Everything is peachy.
804 STATUS_NORMAL,
805 // An unrecoverable communication error has occurred.
806 STATUS_BROKEN,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700807 // The input channel has been unregistered.
808 STATUS_ZOMBIE
809 };
810
811 Status status;
812 sp<InputChannel> inputChannel;
813 InputPublisher inputPublisher;
Jeff Brownb88102f2010-09-08 11:49:43 -0700814 InputState inputState;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700815 Queue<DispatchEntry> outboundQueue;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700816
817 nsecs_t lastEventTime; // the time when the event was originally captured
818 nsecs_t lastDispatchTime; // the time when the last event was dispatched
Jeff Brown46b9ac02010-04-22 18:58:52 -0700819
820 explicit Connection(const sp<InputChannel>& inputChannel);
821
Jeff Brown9c3cda02010-06-15 01:31:58 -0700822 inline const char* getInputChannelName() const { return inputChannel->getName().string(); }
823
824 const char* getStatusLabel() const;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700825
826 // Finds a DispatchEntry in the outbound queue associated with the specified event.
827 // Returns NULL if not found.
828 DispatchEntry* findQueuedDispatchEntryForEvent(const EventEntry* eventEntry) const;
829
Jeff Brown46b9ac02010-04-22 18:58:52 -0700830 // Gets the time since the current event was originally obtained from the input driver.
Jeff Brownb88102f2010-09-08 11:49:43 -0700831 inline double getEventLatencyMillis(nsecs_t currentTime) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700832 return (currentTime - lastEventTime) / 1000000.0;
833 }
834
835 // Gets the time since the current event entered the outbound dispatch queue.
Jeff Brownb88102f2010-09-08 11:49:43 -0700836 inline double getDispatchLatencyMillis(nsecs_t currentTime) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700837 return (currentTime - lastDispatchTime) / 1000000.0;
838 }
839
Jeff Brown46b9ac02010-04-22 18:58:52 -0700840 status_t initialize();
841 };
842
Jeff Brownb6997262010-10-08 22:31:17 -0700843 enum DropReason {
844 DROP_REASON_NOT_DROPPED = 0,
845 DROP_REASON_POLICY = 1,
846 DROP_REASON_APP_SWITCH = 2,
847 DROP_REASON_DISABLED = 3,
848 };
849
Jeff Brown9c3cda02010-06-15 01:31:58 -0700850 sp<InputDispatcherPolicyInterface> mPolicy;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700851
852 Mutex mLock;
853
Jeff Brown46b9ac02010-04-22 18:58:52 -0700854 Allocator mAllocator;
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700855 sp<Looper> mLooper;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700856
Jeff Brownb88102f2010-09-08 11:49:43 -0700857 EventEntry* mPendingEvent;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700858 Queue<EventEntry> mInboundQueue;
859 Queue<CommandEntry> mCommandQueue;
860
Jeff Brownb88102f2010-09-08 11:49:43 -0700861 Vector<EventEntry*> mTempCancelationEvents;
862
863 void dispatchOnceInnerLocked(nsecs_t keyRepeatTimeout, nsecs_t keyRepeatDelay,
864 nsecs_t* nextWakeupTime);
865
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700866 // Enqueues an inbound event. Returns true if mLooper->wake() should be called.
Jeff Brownb88102f2010-09-08 11:49:43 -0700867 bool enqueueInboundEventLocked(EventEntry* entry);
868
Jeff Brownb6997262010-10-08 22:31:17 -0700869 // Cleans up input state when dropping an inbound event.
870 void dropInboundEventLocked(EventEntry* entry, DropReason dropReason);
871
Jeff Brownb88102f2010-09-08 11:49:43 -0700872 // App switch latency optimization.
Jeff Brownb6997262010-10-08 22:31:17 -0700873 bool mAppSwitchSawKeyDown;
Jeff Brownb88102f2010-09-08 11:49:43 -0700874 nsecs_t mAppSwitchDueTime;
875
Jeff Brownb6997262010-10-08 22:31:17 -0700876 static bool isAppSwitchKeyCode(int32_t keyCode);
877 bool isAppSwitchKeyEventLocked(KeyEntry* keyEntry);
Jeff Brownb88102f2010-09-08 11:49:43 -0700878 bool isAppSwitchPendingLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700879 void resetPendingAppSwitchLocked(bool handled);
880
Jeff Brown46b9ac02010-04-22 18:58:52 -0700881 // All registered connections mapped by receive pipe file descriptor.
882 KeyedVector<int, sp<Connection> > mConnectionsByReceiveFd;
883
Jeff Brown519e0242010-09-15 15:18:56 -0700884 ssize_t getConnectionIndexLocked(const sp<InputChannel>& inputChannel);
Jeff Brown2cbecea2010-08-17 15:59:26 -0700885
Jeff Brown46b9ac02010-04-22 18:58:52 -0700886 // Active connections are connections that have a non-empty outbound queue.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700887 // We don't use a ref-counted pointer here because we explicitly abort connections
888 // during unregistration which causes the connection's outbound queue to be cleared
889 // and the connection itself to be deactivated.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700890 Vector<Connection*> mActiveConnections;
891
Jeff Brownb88102f2010-09-08 11:49:43 -0700892 // Input channels that will receive a copy of all input events.
893 Vector<sp<InputChannel> > mMonitoringChannels;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700894
Jeff Brownb88102f2010-09-08 11:49:43 -0700895 // Preallocated key event object used for policy inquiries.
896 KeyEvent mReusableKeyEvent;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700897
Jeff Brown7fbdc842010-06-17 20:52:56 -0700898 // Event injection and synchronization.
899 Condition mInjectionResultAvailableCondition;
Jeff Brownb6997262010-10-08 22:31:17 -0700900 bool hasInjectionPermission(int32_t injectorPid, int32_t injectorUid);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700901 void setInjectionResultLocked(EventEntry* entry, int32_t injectionResult);
902
Jeff Brown6ec402b2010-07-28 15:48:59 -0700903 Condition mInjectionSyncFinishedCondition;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700904 void incrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brown519e0242010-09-15 15:18:56 -0700905 void decrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brown6ec402b2010-07-28 15:48:59 -0700906
Jeff Brownae9fc032010-08-18 15:51:08 -0700907 // Throttling state.
908 struct ThrottleState {
909 nsecs_t minTimeBetweenEvents;
910
911 nsecs_t lastEventTime;
912 int32_t lastDeviceId;
913 uint32_t lastSource;
914
915 uint32_t originalSampleCount; // only collected during debugging
916 } mThrottleState;
917
Jeff Brown46b9ac02010-04-22 18:58:52 -0700918 // Key repeat tracking.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700919 struct KeyRepeatState {
920 KeyEntry* lastKeyEntry; // or null if no repeat
921 nsecs_t nextRepeatTime;
922 } mKeyRepeatState;
923
924 void resetKeyRepeatLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700925 KeyEntry* synthesizeKeyRepeatLocked(nsecs_t currentTime, nsecs_t keyRepeatTimeout);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700926
Jeff Brown9c3cda02010-06-15 01:31:58 -0700927 // Deferred command processing.
928 bool runCommandsLockedInterruptible();
929 CommandEntry* postCommandLocked(Command command);
930
Jeff Brownb88102f2010-09-08 11:49:43 -0700931 // Inbound event processing.
932 void drainInboundQueueLocked();
Jeff Brown54a18252010-09-16 14:07:33 -0700933 void releasePendingEventLocked();
934 void releaseInboundEventLocked(EventEntry* entry);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700935
Jeff Brownb88102f2010-09-08 11:49:43 -0700936 // Dispatch state.
937 bool mDispatchEnabled;
938 bool mDispatchFrozen;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700939
Jeff Brownb88102f2010-09-08 11:49:43 -0700940 Vector<InputWindow> mWindows;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700941
942 const InputWindow* getWindowLocked(const sp<InputChannel>& inputChannel);
Jeff Brownb88102f2010-09-08 11:49:43 -0700943
944 // Focus tracking for keys, trackball, etc.
Jeff Brown01ce2e92010-09-26 22:20:12 -0700945 const InputWindow* mFocusedWindow;
Jeff Brownb88102f2010-09-08 11:49:43 -0700946
947 // Focus tracking for touch.
Jeff Brown01ce2e92010-09-26 22:20:12 -0700948 struct TouchedWindow {
949 const InputWindow* window;
950 int32_t targetFlags;
Jeff Brown46e75292010-11-10 16:53:45 -0800951 BitSet32 pointerIds; // zero unless target flag FLAG_SPLIT is set
Jeff Brown01ce2e92010-09-26 22:20:12 -0700952 sp<InputChannel> channel;
Jeff Brownb88102f2010-09-08 11:49:43 -0700953 };
Jeff Brown01ce2e92010-09-26 22:20:12 -0700954 struct TouchState {
955 bool down;
956 bool split;
957 Vector<TouchedWindow> windows;
958
959 TouchState();
960 ~TouchState();
961 void reset();
962 void copyFrom(const TouchState& other);
963 void addOrUpdateWindow(const InputWindow* window, int32_t targetFlags, BitSet32 pointerIds);
964 void removeOutsideTouchWindows();
965 const InputWindow* getFirstForegroundWindow();
966 };
967
968 TouchState mTouchState;
969 TouchState mTempTouchState;
Jeff Brownb88102f2010-09-08 11:49:43 -0700970
971 // Focused application.
972 InputApplication* mFocusedApplication;
973 InputApplication mFocusedApplicationStorage; // preallocated storage for mFocusedApplication
974 void releaseFocusedApplicationLocked();
975
976 // Dispatch inbound events.
977 bool dispatchConfigurationChangedLocked(
978 nsecs_t currentTime, ConfigurationChangedEntry* entry);
979 bool dispatchKeyLocked(
980 nsecs_t currentTime, KeyEntry* entry, nsecs_t keyRepeatTimeout,
Jeff Browne20c9e02010-10-11 14:20:19 -0700981 DropReason* dropReason, nsecs_t* nextWakeupTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700982 bool dispatchMotionLocked(
983 nsecs_t currentTime, MotionEntry* entry,
Jeff Browne20c9e02010-10-11 14:20:19 -0700984 DropReason* dropReason, nsecs_t* nextWakeupTime);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700985 void dispatchEventToCurrentInputTargetsLocked(
986 nsecs_t currentTime, EventEntry* entry, bool resumeWithAppendedMotionSample);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700987
Jeff Brownb88102f2010-09-08 11:49:43 -0700988 void logOutboundKeyDetailsLocked(const char* prefix, const KeyEntry* entry);
989 void logOutboundMotionDetailsLocked(const char* prefix, const MotionEntry* entry);
990
991 // The input targets that were most recently identified for dispatch.
Jeff Brownb88102f2010-09-08 11:49:43 -0700992 bool mCurrentInputTargetsValid; // false while targets are being recomputed
993 Vector<InputTarget> mCurrentInputTargets;
Jeff Brownb88102f2010-09-08 11:49:43 -0700994
995 enum InputTargetWaitCause {
996 INPUT_TARGET_WAIT_CAUSE_NONE,
997 INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY,
998 INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY,
999 };
1000
1001 InputTargetWaitCause mInputTargetWaitCause;
1002 nsecs_t mInputTargetWaitStartTime;
1003 nsecs_t mInputTargetWaitTimeoutTime;
1004 bool mInputTargetWaitTimeoutExpired;
1005
1006 // Finding targets for input events.
Jeff Brown54a18252010-09-16 14:07:33 -07001007 void resetTargetsLocked();
Jeff Brown01ce2e92010-09-26 22:20:12 -07001008 void commitTargetsLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -07001009 int32_t handleTargetsNotReadyLocked(nsecs_t currentTime, const EventEntry* entry,
1010 const InputApplication* application, const InputWindow* window,
1011 nsecs_t* nextWakeupTime);
Jeff Brown519e0242010-09-15 15:18:56 -07001012 void resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout,
1013 const sp<InputChannel>& inputChannel);
1014 nsecs_t getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime);
Jeff Brownb88102f2010-09-08 11:49:43 -07001015 void resetANRTimeoutsLocked();
1016
Jeff Brown01ce2e92010-09-26 22:20:12 -07001017 int32_t findFocusedWindowTargetsLocked(nsecs_t currentTime, const EventEntry* entry,
1018 nsecs_t* nextWakeupTime);
1019 int32_t findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry* entry,
1020 nsecs_t* nextWakeupTime);
Jeff Brownb88102f2010-09-08 11:49:43 -07001021
Jeff Brown01ce2e92010-09-26 22:20:12 -07001022 void addWindowTargetLocked(const InputWindow* window, int32_t targetFlags,
1023 BitSet32 pointerIds);
Jeff Brownb88102f2010-09-08 11:49:43 -07001024 void addMonitoringTargetsLocked();
Jeff Browne2fe69e2010-10-18 13:21:23 -07001025 void pokeUserActivityLocked(const EventEntry* eventEntry);
Jeff Brown01ce2e92010-09-26 22:20:12 -07001026 bool checkInjectionPermission(const InputWindow* window, const InjectionState* injectionState);
Jeff Brown19dfc832010-10-05 12:26:23 -07001027 bool isWindowObscuredAtPointLocked(const InputWindow* window, int32_t x, int32_t y) const;
Jeff Brown519e0242010-09-15 15:18:56 -07001028 bool isWindowFinishedWithPreviousInputLocked(const InputWindow* window);
Jeff Brown519e0242010-09-15 15:18:56 -07001029 String8 getApplicationWindowLabelLocked(const InputApplication* application,
1030 const InputWindow* window);
Jeff Brownb88102f2010-09-08 11:49:43 -07001031
Jeff Brown46b9ac02010-04-22 18:58:52 -07001032 // Manage the dispatch cycle for a single connection.
Jeff Brown7fbdc842010-06-17 20:52:56 -07001033 // These methods are deliberately not Interruptible because doing all of the work
1034 // with the mutex held makes it easier to ensure that connection invariants are maintained.
1035 // If needed, the methods post commands to run later once the critical bits are done.
1036 void prepareDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001037 EventEntry* eventEntry, const InputTarget* inputTarget,
1038 bool resumeWithAppendedMotionSample);
Jeff Brown519e0242010-09-15 15:18:56 -07001039 void startDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown3915bb82010-11-05 15:02:16 -07001040 void finishDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
1041 bool handled);
Jeff Brownb88102f2010-09-08 11:49:43 -07001042 void startNextDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brownb6997262010-10-08 22:31:17 -07001043 void abortBrokenDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown519e0242010-09-15 15:18:56 -07001044 void drainOutboundQueueLocked(Connection* connection);
Jeff Brown4fe6c3e2010-09-13 23:17:30 -07001045 static int handleReceiveCallback(int receiveFd, int events, void* data);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001046
Jeff Brownb6997262010-10-08 22:31:17 -07001047 void synthesizeCancelationEventsForAllConnectionsLocked(
1048 InputState::CancelationOptions options, const char* reason);
1049 void synthesizeCancelationEventsForInputChannelLocked(const sp<InputChannel>& channel,
1050 InputState::CancelationOptions options, const char* reason);
1051 void synthesizeCancelationEventsForConnectionLocked(const sp<Connection>& connection,
1052 InputState::CancelationOptions options, const char* reason);
1053
Jeff Brown01ce2e92010-09-26 22:20:12 -07001054 // Splitting motion events across windows.
1055 MotionEntry* splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds);
1056
Jeff Brown120a4592010-10-27 18:43:51 -07001057 // Reset and drop everything the dispatcher is doing.
1058 void resetAndDropEverythingLocked(const char* reason);
1059
Jeff Brownb88102f2010-09-08 11:49:43 -07001060 // Dump state.
1061 void dumpDispatchStateLocked(String8& dump);
1062 void logDispatchStateLocked();
1063
Jeff Brown46b9ac02010-04-22 18:58:52 -07001064 // Add or remove a connection to the mActiveConnections vector.
1065 void activateConnectionLocked(Connection* connection);
1066 void deactivateConnectionLocked(Connection* connection);
1067
1068 // Interesting events that we might like to log or tell the framework about.
Jeff Brown9c3cda02010-06-15 01:31:58 -07001069 void onDispatchCycleStartedLocked(
Jeff Brown7fbdc842010-06-17 20:52:56 -07001070 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001071 void onDispatchCycleFinishedLocked(
Jeff Brown3915bb82010-11-05 15:02:16 -07001072 nsecs_t currentTime, const sp<Connection>& connection, bool handled);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001073 void onDispatchCycleBrokenLocked(
Jeff Brown7fbdc842010-06-17 20:52:56 -07001074 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown519e0242010-09-15 15:18:56 -07001075 void onANRLocked(
1076 nsecs_t currentTime, const InputApplication* application, const InputWindow* window,
1077 nsecs_t eventTime, nsecs_t waitStartTime);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001078
Jeff Brown7fbdc842010-06-17 20:52:56 -07001079 // Outbound policy interactions.
Jeff Brownb88102f2010-09-08 11:49:43 -07001080 void doNotifyConfigurationChangedInterruptible(CommandEntry* commandEntry);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001081 void doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown519e0242010-09-15 15:18:56 -07001082 void doNotifyANRLockedInterruptible(CommandEntry* commandEntry);
Jeff Brownb88102f2010-09-08 11:49:43 -07001083 void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown3915bb82010-11-05 15:02:16 -07001084 void doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry);
Jeff Brownb88102f2010-09-08 11:49:43 -07001085 void doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown3915bb82010-11-05 15:02:16 -07001086 void initializeKeyEvent(KeyEvent* event, const KeyEntry* entry);
Jeff Brown519e0242010-09-15 15:18:56 -07001087
1088 // Statistics gathering.
1089 void updateDispatchStatisticsLocked(nsecs_t currentTime, const EventEntry* entry,
1090 int32_t injectionResult, nsecs_t timeSpentWaitingForApplication);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001091};
1092
1093/* Enqueues and dispatches input events, endlessly. */
1094class InputDispatcherThread : public Thread {
1095public:
1096 explicit InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher);
1097 ~InputDispatcherThread();
1098
1099private:
1100 virtual bool threadLoop();
1101
1102 sp<InputDispatcherInterface> mDispatcher;
1103};
1104
1105} // namespace android
1106
Jeff Brownb88102f2010-09-08 11:49:43 -07001107#endif // _UI_INPUT_DISPATCHER_H