blob: 63185d39314bf855419dfdc037d71425c35c7918 [file] [log] [blame]
Jeff Browne839a582010-04-22 18:58:52 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _UI_INPUT_DISPATCHER_H
18#define _UI_INPUT_DISPATCHER_H
19
20#include <ui/Input.h>
Jeff Browne839a582010-04-22 18:58:52 -070021#include <ui/InputTransport.h>
22#include <utils/KeyedVector.h>
23#include <utils/Vector.h>
24#include <utils/threads.h>
25#include <utils/Timers.h>
26#include <utils/RefBase.h>
27#include <utils/String8.h>
Jeff Brown59abe7e2010-09-13 23:17:30 -070028#include <utils/Looper.h>
Jeff Browne839a582010-04-22 18:58:52 -070029#include <utils/Pool.h>
Jeff Brownd1b0a2b2010-09-26 22:20:12 -070030#include <utils/BitSet.h>
Jeff Browne839a582010-04-22 18:58:52 -070031
32#include <stddef.h>
33#include <unistd.h>
Jeff Browna665ca82010-09-08 11:49:43 -070034#include <limits.h>
Jeff Browne839a582010-04-22 18:58:52 -070035
36
37namespace android {
38
Jeff Brown54bc2812010-06-15 01:31:58 -070039/*
Jeff Brown51d45a72010-06-17 20:52:56 -070040 * Constants used to report the outcome of input event injection.
41 */
42enum {
43 /* (INTERNAL USE ONLY) Specifies that injection is pending and its outcome is unknown. */
44 INPUT_EVENT_INJECTION_PENDING = -1,
45
46 /* Injection succeeded. */
47 INPUT_EVENT_INJECTION_SUCCEEDED = 0,
48
49 /* Injection failed because the injector did not have permission to inject
50 * into the application with input focus. */
51 INPUT_EVENT_INJECTION_PERMISSION_DENIED = 1,
52
53 /* Injection failed because there were no available input targets. */
54 INPUT_EVENT_INJECTION_FAILED = 2,
55
56 /* Injection failed due to a timeout. */
57 INPUT_EVENT_INJECTION_TIMED_OUT = 3
58};
59
Jeff Brownf67c53e2010-07-28 15:48:59 -070060/*
61 * Constants used to determine the input event injection synchronization mode.
62 */
63enum {
64 /* Injection is asynchronous and is assumed always to be successful. */
65 INPUT_EVENT_INJECTION_SYNC_NONE = 0,
66
67 /* Waits for previous events to be dispatched so that the input dispatcher can determine
68 * whether input event injection willbe permitted based on the current input focus.
69 * Does not wait for the input event to finish processing. */
70 INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT = 1,
71
72 /* Waits for the input event to be completely processed. */
73 INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISHED = 2,
74};
75
Jeff Brown51d45a72010-06-17 20:52:56 -070076
77/*
Jeff Brown54bc2812010-06-15 01:31:58 -070078 * An input target specifies how an input event is to be dispatched to a particular window
79 * including the window's input channel, control flags, a timeout, and an X / Y offset to
80 * be added to input event coordinates to compensate for the absolute position of the
81 * window area.
82 */
83struct InputTarget {
84 enum {
Jeff Brown53a415e2010-09-15 15:18:56 -070085 /* This flag indicates that the event is being delivered to a foreground application. */
86 FLAG_FOREGROUND = 0x01,
Jeff Brown54bc2812010-06-15 01:31:58 -070087
Jeff Brownaf30ff62010-09-01 17:01:00 -070088 /* This flag indicates that a MotionEvent with AMOTION_EVENT_ACTION_DOWN falls outside
89 * of the area of this target and so should instead be delivered as an
90 * AMOTION_EVENT_ACTION_OUTSIDE to this target. */
Jeff Brown54bc2812010-06-15 01:31:58 -070091 FLAG_OUTSIDE = 0x02,
92
Jeff Brownaf30ff62010-09-01 17:01:00 -070093 /* This flag indicates that the target of a MotionEvent is partly or wholly
94 * obscured by another visible window above it. The motion event should be
95 * delivered with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED. */
Jeff Brownd1b0a2b2010-09-26 22:20:12 -070096 FLAG_WINDOW_IS_OBSCURED = 0x04,
97
98 /* This flag indicates that a motion event is being split across multiple windows. */
99 FLAG_SPLIT = 0x08,
Jeff Brown54bc2812010-06-15 01:31:58 -0700100 };
101
102 // The input channel to be targeted.
103 sp<InputChannel> inputChannel;
104
105 // Flags for the input target.
106 int32_t flags;
107
Jeff Brown54bc2812010-06-15 01:31:58 -0700108 // The x and y offset to add to a MotionEvent as it is delivered.
109 // (ignored for KeyEvents)
110 float xOffset, yOffset;
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700111
112 // The window type of the input target.
113 int32_t windowType;
114
115 // The subset of pointer ids to include in motion events dispatched to this input target
116 // if FLAG_SPLIT is set.
117 BitSet32 pointerIds;
Jeff Brown54bc2812010-06-15 01:31:58 -0700118};
119
Jeff Brown51d45a72010-06-17 20:52:56 -0700120
Jeff Brown54bc2812010-06-15 01:31:58 -0700121/*
Jeff Browna665ca82010-09-08 11:49:43 -0700122 * An input window describes the bounds of a window that can receive input.
123 */
124struct InputWindow {
125 // Window flags from WindowManager.LayoutParams
126 enum {
127 FLAG_ALLOW_LOCK_WHILE_SCREEN_ON = 0x00000001,
128 FLAG_DIM_BEHIND = 0x00000002,
129 FLAG_BLUR_BEHIND = 0x00000004,
130 FLAG_NOT_FOCUSABLE = 0x00000008,
131 FLAG_NOT_TOUCHABLE = 0x00000010,
132 FLAG_NOT_TOUCH_MODAL = 0x00000020,
133 FLAG_TOUCHABLE_WHEN_WAKING = 0x00000040,
134 FLAG_KEEP_SCREEN_ON = 0x00000080,
135 FLAG_LAYOUT_IN_SCREEN = 0x00000100,
136 FLAG_LAYOUT_NO_LIMITS = 0x00000200,
137 FLAG_FULLSCREEN = 0x00000400,
138 FLAG_FORCE_NOT_FULLSCREEN = 0x00000800,
139 FLAG_DITHER = 0x00001000,
140 FLAG_SECURE = 0x00002000,
141 FLAG_SCALED = 0x00004000,
142 FLAG_IGNORE_CHEEK_PRESSES = 0x00008000,
143 FLAG_LAYOUT_INSET_DECOR = 0x00010000,
144 FLAG_ALT_FOCUSABLE_IM = 0x00020000,
145 FLAG_WATCH_OUTSIDE_TOUCH = 0x00040000,
146 FLAG_SHOW_WHEN_LOCKED = 0x00080000,
147 FLAG_SHOW_WALLPAPER = 0x00100000,
148 FLAG_TURN_SCREEN_ON = 0x00200000,
149 FLAG_DISMISS_KEYGUARD = 0x00400000,
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700150 FLAG_SPLIT_TOUCH = 0x00800000,
Jeff Browna665ca82010-09-08 11:49:43 -0700151 FLAG_KEEP_SURFACE_WHILE_ANIMATING = 0x10000000,
152 FLAG_COMPATIBLE_WINDOW = 0x20000000,
153 FLAG_SYSTEM_ERROR = 0x40000000,
154 };
155
156 // Window types from WindowManager.LayoutParams
157 enum {
158 FIRST_APPLICATION_WINDOW = 1,
159 TYPE_BASE_APPLICATION = 1,
160 TYPE_APPLICATION = 2,
161 TYPE_APPLICATION_STARTING = 3,
162 LAST_APPLICATION_WINDOW = 99,
163 FIRST_SUB_WINDOW = 1000,
164 TYPE_APPLICATION_PANEL = FIRST_SUB_WINDOW,
165 TYPE_APPLICATION_MEDIA = FIRST_SUB_WINDOW+1,
166 TYPE_APPLICATION_SUB_PANEL = FIRST_SUB_WINDOW+2,
167 TYPE_APPLICATION_ATTACHED_DIALOG = FIRST_SUB_WINDOW+3,
168 TYPE_APPLICATION_MEDIA_OVERLAY = FIRST_SUB_WINDOW+4,
169 LAST_SUB_WINDOW = 1999,
170 FIRST_SYSTEM_WINDOW = 2000,
171 TYPE_STATUS_BAR = FIRST_SYSTEM_WINDOW,
172 TYPE_SEARCH_BAR = FIRST_SYSTEM_WINDOW+1,
173 TYPE_PHONE = FIRST_SYSTEM_WINDOW+2,
174 TYPE_SYSTEM_ALERT = FIRST_SYSTEM_WINDOW+3,
175 TYPE_KEYGUARD = FIRST_SYSTEM_WINDOW+4,
176 TYPE_TOAST = FIRST_SYSTEM_WINDOW+5,
177 TYPE_SYSTEM_OVERLAY = FIRST_SYSTEM_WINDOW+6,
178 TYPE_PRIORITY_PHONE = FIRST_SYSTEM_WINDOW+7,
179 TYPE_SYSTEM_DIALOG = FIRST_SYSTEM_WINDOW+8,
180 TYPE_KEYGUARD_DIALOG = FIRST_SYSTEM_WINDOW+9,
181 TYPE_SYSTEM_ERROR = FIRST_SYSTEM_WINDOW+10,
182 TYPE_INPUT_METHOD = FIRST_SYSTEM_WINDOW+11,
183 TYPE_INPUT_METHOD_DIALOG= FIRST_SYSTEM_WINDOW+12,
184 TYPE_WALLPAPER = FIRST_SYSTEM_WINDOW+13,
185 TYPE_STATUS_BAR_PANEL = FIRST_SYSTEM_WINDOW+14,
186 LAST_SYSTEM_WINDOW = 2999,
187 };
188
189 sp<InputChannel> inputChannel;
Jeff Brown53a415e2010-09-15 15:18:56 -0700190 String8 name;
Jeff Browna665ca82010-09-08 11:49:43 -0700191 int32_t layoutParamsFlags;
192 int32_t layoutParamsType;
193 nsecs_t dispatchingTimeout;
194 int32_t frameLeft;
195 int32_t frameTop;
196 int32_t frameRight;
197 int32_t frameBottom;
198 int32_t visibleFrameLeft;
199 int32_t visibleFrameTop;
200 int32_t visibleFrameRight;
201 int32_t visibleFrameBottom;
202 int32_t touchableAreaLeft;
203 int32_t touchableAreaTop;
204 int32_t touchableAreaRight;
205 int32_t touchableAreaBottom;
206 bool visible;
Jeff Brown53a415e2010-09-15 15:18:56 -0700207 bool canReceiveKeys;
Jeff Browna665ca82010-09-08 11:49:43 -0700208 bool hasFocus;
209 bool hasWallpaper;
210 bool paused;
Jeff Brown53a415e2010-09-15 15:18:56 -0700211 int32_t layer;
Jeff Browna665ca82010-09-08 11:49:43 -0700212 int32_t ownerPid;
213 int32_t ownerUid;
214
Jeff Browna665ca82010-09-08 11:49:43 -0700215 bool touchableAreaContainsPoint(int32_t x, int32_t y) const;
Jeff Brown35cf0e92010-10-05 12:26:23 -0700216 bool frameContainsPoint(int32_t x, int32_t y) const;
217
218 /* Returns true if the window is of a trusted type that is allowed to silently
219 * overlay other windows for the purpose of implementing the secure views feature.
220 * Trusted overlays, such as IME windows, can partly obscure other windows without causing
221 * motion events to be delivered to them with AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED.
222 */
223 bool isTrustedOverlay() const;
Jeff Browna665ca82010-09-08 11:49:43 -0700224};
225
226
227/*
228 * A private handle type used by the input manager to track the window.
229 */
230class InputApplicationHandle : public RefBase {
231protected:
232 InputApplicationHandle() { }
233 virtual ~InputApplicationHandle() { }
234};
235
236
237/*
238 * An input application describes properties of an application that can receive input.
239 */
240struct InputApplication {
241 String8 name;
242 nsecs_t dispatchingTimeout;
243 sp<InputApplicationHandle> handle;
244};
245
246
247/*
Jeff Brown54bc2812010-06-15 01:31:58 -0700248 * Input dispatcher policy interface.
249 *
250 * The input reader policy is used by the input reader to interact with the Window Manager
251 * and other system components.
252 *
253 * The actual implementation is partially supported by callbacks into the DVM
254 * via JNI. This interface is also mocked in the unit tests.
255 */
256class InputDispatcherPolicyInterface : public virtual RefBase {
257protected:
258 InputDispatcherPolicyInterface() { }
259 virtual ~InputDispatcherPolicyInterface() { }
260
261public:
262 /* Notifies the system that a configuration change has occurred. */
263 virtual void notifyConfigurationChanged(nsecs_t when) = 0;
264
Jeff Browna665ca82010-09-08 11:49:43 -0700265 /* Notifies the system that an application is not responding.
266 * Returns a new timeout to continue waiting, or 0 to abort dispatch. */
Jeff Brown53a415e2010-09-15 15:18:56 -0700267 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
268 const sp<InputChannel>& inputChannel) = 0;
Jeff Browna665ca82010-09-08 11:49:43 -0700269
Jeff Brown54bc2812010-06-15 01:31:58 -0700270 /* Notifies the system that an input channel is unrecoverably broken. */
271 virtual void notifyInputChannelBroken(const sp<InputChannel>& inputChannel) = 0;
272
Jeff Brown61ce3982010-09-07 10:44:57 -0700273 /* Gets the key repeat initial timeout or -1 if automatic key repeating is disabled. */
Jeff Brown54bc2812010-06-15 01:31:58 -0700274 virtual nsecs_t getKeyRepeatTimeout() = 0;
275
Jeff Brown61ce3982010-09-07 10:44:57 -0700276 /* Gets the key repeat inter-key delay. */
277 virtual nsecs_t getKeyRepeatDelay() = 0;
278
Jeff Brown542412c2010-08-18 15:51:08 -0700279 /* Gets the maximum suggested event delivery rate per second.
280 * This value is used to throttle motion event movement actions on a per-device
281 * basis. It is not intended to be a hard limit.
282 */
283 virtual int32_t getMaxEventsPerSecond() = 0;
Jeff Browna665ca82010-09-08 11:49:43 -0700284
Jeff Brown90f0cee2010-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 Browna665ca82010-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 Brown90f0cee2010-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 Browna665ca82010-09-08 11:49:43 -0700314 /* Poke user activity for an event dispatched to a window. */
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700315 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType) = 0;
Jeff Browna665ca82010-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 Brown54bc2812010-06-15 01:31:58 -0700325};
326
327
Jeff Browne839a582010-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 Browna665ca82010-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 Browne839a582010-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 Browne839a582010-04-22 18:58:52 -0700349 *
350 * These methods should only be called on the input reader thread.
351 */
Jeff Brown54bc2812010-06-15 01:31:58 -0700352 virtual void notifyConfigurationChanged(nsecs_t eventTime) = 0;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700353 virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Browne839a582010-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 Brown5c1ed842010-07-14 18:48:53 -0700356 virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Brownaf30ff62010-09-01 17:01:00 -0700357 uint32_t policyFlags, int32_t action, int32_t flags,
358 int32_t metaState, int32_t edgeFlags,
Jeff Browne839a582010-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 Brown90f0cee2010-10-08 22:31:17 -0700361 virtual void notifySwitch(nsecs_t when,
362 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) = 0;
Jeff Browne839a582010-04-22 18:58:52 -0700363
Jeff Brown51d45a72010-06-17 20:52:56 -0700364 /* Injects an input event and optionally waits for sync.
Jeff Brownf67c53e2010-07-28 15:48:59 -0700365 * The synchronization mode determines whether the method blocks while waiting for
366 * input injection to proceed.
Jeff Brown51d45a72010-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 Brownf67c53e2010-07-28 15:48:59 -0700372 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis) = 0;
Jeff Brown51d45a72010-06-17 20:52:56 -0700373
Jeff Browna665ca82010-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 Brown744c5592010-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 Browne839a582010-04-22 18:58:52 -0700400 /* Registers or unregister input channels that may be used as targets for input events.
Jeff Browna665ca82010-09-08 11:49:43 -0700401 * If monitor is true, the channel will receive a copy of all input events.
Jeff Browne839a582010-04-22 18:58:52 -0700402 *
403 * These methods may be called on any thread (usually by the input manager).
404 */
Jeff Browna665ca82010-09-08 11:49:43 -0700405 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel, bool monitor) = 0;
Jeff Browne839a582010-04-22 18:58:52 -0700406 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) = 0;
407};
408
Jeff Brown54bc2812010-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 Browne839a582010-04-22 18:58:52 -0700426class InputDispatcher : public InputDispatcherInterface {
427protected:
428 virtual ~InputDispatcher();
429
430public:
Jeff Brown54bc2812010-06-15 01:31:58 -0700431 explicit InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy);
Jeff Browne839a582010-04-22 18:58:52 -0700432
Jeff Browna665ca82010-09-08 11:49:43 -0700433 virtual void dump(String8& dump);
434
Jeff Browne839a582010-04-22 18:58:52 -0700435 virtual void dispatchOnce();
436
Jeff Brown54bc2812010-06-15 01:31:58 -0700437 virtual void notifyConfigurationChanged(nsecs_t eventTime);
Jeff Brown5c1ed842010-07-14 18:48:53 -0700438 virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Browne839a582010-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 Brown5c1ed842010-07-14 18:48:53 -0700441 virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Brownaf30ff62010-09-01 17:01:00 -0700442 uint32_t policyFlags, int32_t action, int32_t flags,
443 int32_t metaState, int32_t edgeFlags,
Jeff Browne839a582010-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 Brown90f0cee2010-10-08 22:31:17 -0700446 virtual void notifySwitch(nsecs_t when,
447 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) ;
Jeff Browne839a582010-04-22 18:58:52 -0700448
Jeff Brown51d45a72010-06-17 20:52:56 -0700449 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brownf67c53e2010-07-28 15:48:59 -0700450 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis);
Jeff Brown51d45a72010-06-17 20:52:56 -0700451
Jeff Browna665ca82010-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 Brown50de30a2010-06-22 01:27:15 -0700455
Jeff Brown744c5592010-09-27 14:52:15 -0700456 virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel,
457 const sp<InputChannel>& toChannel);
458
Jeff Browna665ca82010-09-08 11:49:43 -0700459 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel, bool monitor);
Jeff Browne839a582010-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 Brownd1b0a2b2010-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 Browne839a582010-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 Brownd1b0a2b2010-09-26 22:20:12 -0700487 mutable int32_t refCount;
Jeff Browne839a582010-04-22 18:58:52 -0700488 int32_t type;
489 nsecs_t eventTime;
Jeff Brown90f0cee2010-10-08 22:31:17 -0700490 uint32_t policyFlags;
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700491 InjectionState* injectionState;
Jeff Brown51d45a72010-06-17 20:52:56 -0700492
Jeff Brown54bc2812010-06-15 01:31:58 -0700493 bool dispatchInProgress; // initially false, set to true while dispatching
Jeff Brown51d45a72010-06-17 20:52:56 -0700494
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700495 inline bool isInjected() { return injectionState != NULL; }
Jeff Browne839a582010-04-22 18:58:52 -0700496 };
497
498 struct ConfigurationChangedEntry : EventEntry {
Jeff Browne839a582010-04-22 18:58:52 -0700499 };
500
501 struct KeyEntry : EventEntry {
502 int32_t deviceId;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700503 int32_t source;
Jeff Browne839a582010-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 Browna665ca82010-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 Browne839a582010-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 Brown5c1ed842010-07-14 18:48:53 -0700531 int32_t source;
Jeff Browne839a582010-04-22 18:58:52 -0700532 int32_t action;
Jeff Brownaf30ff62010-09-01 17:01:00 -0700533 int32_t flags;
Jeff Browne839a582010-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 Brown542412c2010-08-18 15:51:08 -0700545
546 uint32_t countSamples() const;
Jeff Browne839a582010-04-22 18:58:52 -0700547 };
548
Jeff Brown54bc2812010-06-15 01:31:58 -0700549 // Tracks the progress of dispatching a particular event to a particular connection.
Jeff Browne839a582010-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 Browne839a582010-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 Brownf67c53e2010-07-28 15:48:59 -0700571
Jeff Brown53a415e2010-09-15 15:18:56 -0700572 inline bool hasForegroundTarget() const {
573 return targetFlags & InputTarget::FLAG_FOREGROUND;
Jeff Browna665ca82010-09-08 11:49:43 -0700574 }
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700575
576 inline bool isSplit() const {
577 return targetFlags & InputTarget::FLAG_SPLIT;
578 }
Jeff Browne839a582010-04-22 18:58:52 -0700579 };
580
Jeff Brown54bc2812010-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 Brown51d45a72010-06-17 20:52:56 -0700600 class Connection;
Jeff Brown54bc2812010-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 Brown51d45a72010-06-17 20:52:56 -0700608 sp<Connection> connection;
Jeff Browna665ca82010-09-08 11:49:43 -0700609 nsecs_t eventTime;
610 KeyEntry* keyEntry;
611 sp<InputChannel> inputChannel;
612 sp<InputApplicationHandle> inputApplicationHandle;
Jeff Browna665ca82010-09-08 11:49:43 -0700613 int32_t userActivityEventType;
Jeff Brown54bc2812010-06-15 01:31:58 -0700614 };
615
616 // Generic queue implementation.
Jeff Browne839a582010-04-22 18:58:52 -0700617 template <typename T>
618 struct Queue {
Jeff Browna665ca82010-09-08 11:49:43 -0700619 T headSentinel;
620 T tailSentinel;
Jeff Browne839a582010-04-22 18:58:52 -0700621
622 inline Queue() {
Jeff Browna665ca82010-09-08 11:49:43 -0700623 headSentinel.prev = NULL;
624 headSentinel.next = & tailSentinel;
625 tailSentinel.prev = & headSentinel;
626 tailSentinel.next = NULL;
Jeff Browne839a582010-04-22 18:58:52 -0700627 }
628
Jeff Browna665ca82010-09-08 11:49:43 -0700629 inline bool isEmpty() const {
630 return headSentinel.next == & tailSentinel;
Jeff Browne839a582010-04-22 18:58:52 -0700631 }
632
633 inline void enqueueAtTail(T* entry) {
Jeff Browna665ca82010-09-08 11:49:43 -0700634 T* last = tailSentinel.prev;
Jeff Browne839a582010-04-22 18:58:52 -0700635 last->next = entry;
636 entry->prev = last;
Jeff Browna665ca82010-09-08 11:49:43 -0700637 entry->next = & tailSentinel;
638 tailSentinel.prev = entry;
Jeff Browne839a582010-04-22 18:58:52 -0700639 }
640
641 inline void enqueueAtHead(T* entry) {
Jeff Browna665ca82010-09-08 11:49:43 -0700642 T* first = headSentinel.next;
643 headSentinel.next = entry;
644 entry->prev = & headSentinel;
Jeff Browne839a582010-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 Browna665ca82010-09-08 11:49:43 -0700655 T* first = headSentinel.next;
Jeff Browne839a582010-04-22 18:58:52 -0700656 dequeue(first);
657 return first;
658 }
Jeff Brown53a415e2010-09-15 15:18:56 -0700659
660 uint32_t count() const;
Jeff Browne839a582010-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 Brownd1b0a2b2010-09-26 22:20:12 -0700668 InjectionState* obtainInjectionState(int32_t injectorPid, int32_t injectorUid);
Jeff Brown51d45a72010-06-17 20:52:56 -0700669 ConfigurationChangedEntry* obtainConfigurationChangedEntry(nsecs_t eventTime);
670 KeyEntry* obtainKeyEntry(nsecs_t eventTime,
Jeff Brown5c1ed842010-07-14 18:48:53 -0700671 int32_t deviceId, int32_t source, uint32_t policyFlags, int32_t action,
Jeff Brown51d45a72010-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 Brown5c1ed842010-07-14 18:48:53 -0700675 int32_t deviceId, int32_t source, uint32_t policyFlags, int32_t action,
Jeff Brownaf30ff62010-09-01 17:01:00 -0700676 int32_t flags, int32_t metaState, int32_t edgeFlags,
677 float xPrecision, float yPrecision,
Jeff Brown51d45a72010-06-17 20:52:56 -0700678 nsecs_t downTime, uint32_t pointerCount,
679 const int32_t* pointerIds, const PointerCoords* pointerCoords);
Jeff Browna665ca82010-09-08 11:49:43 -0700680 DispatchEntry* obtainDispatchEntry(EventEntry* eventEntry,
Jeff Brown53a415e2010-09-15 15:18:56 -0700681 int32_t targetFlags, float xOffset, float yOffset);
Jeff Brown54bc2812010-06-15 01:31:58 -0700682 CommandEntry* obtainCommandEntry(Command command);
Jeff Browne839a582010-04-22 18:58:52 -0700683
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700684 void releaseInjectionState(InjectionState* injectionState);
Jeff Browne839a582010-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 Brown54bc2812010-06-15 01:31:58 -0700690 void releaseCommandEntry(CommandEntry* entry);
Jeff Browne839a582010-04-22 18:58:52 -0700691
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700692 void recycleKeyEntry(KeyEntry* entry);
693
Jeff Browne839a582010-04-22 18:58:52 -0700694 void appendMotionSample(MotionEntry* motionEntry,
Jeff Brown51d45a72010-06-17 20:52:56 -0700695 nsecs_t eventTime, const PointerCoords* pointerCoords);
Jeff Browne839a582010-04-22 18:58:52 -0700696
697 private:
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700698 Pool<InjectionState> mInjectionStatePool;
Jeff Browne839a582010-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 Brown54bc2812010-06-15 01:31:58 -0700704 Pool<CommandEntry> mCommandEntryPool;
Jeff Brown51d45a72010-06-17 20:52:56 -0700705
Jeff Brown90f0cee2010-10-08 22:31:17 -0700706 void initializeEventEntry(EventEntry* entry, int32_t type, nsecs_t eventTime,
707 uint32_t policyFlags);
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700708 void releaseEventEntryInjectionState(EventEntry* entry);
Jeff Browne839a582010-04-22 18:58:52 -0700709 };
710
Jeff Browna665ca82010-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 Brown90f0cee2010-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 Browna665ca82010-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 Browna665ca82010-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 Brown90f0cee2010-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 Browna665ca82010-09-08 11:49:43 -0700756
757 // Clears the current state.
758 void clear();
759
Jeff Brownb6702e52010-10-11 18:32:20 -0700760 // Copies pointer-related parts of the input state to another instance.
761 void copyPointerStateTo(InputState& other) const;
762
Jeff Browna665ca82010-09-08 11:49:43 -0700763 private:
Jeff Browna665ca82010-09-08 11:49:43 -0700764 struct KeyMemento {
765 int32_t deviceId;
766 int32_t source;
767 int32_t keyCode;
768 int32_t scanCode;
769 nsecs_t downTime;
770 };
771
772 struct MotionMemento {
773 int32_t deviceId;
774 int32_t source;
775 float xPrecision;
776 float yPrecision;
777 nsecs_t downTime;
778 uint32_t pointerCount;
779 int32_t pointerIds[MAX_POINTERS];
780 PointerCoords pointerCoords[MAX_POINTERS];
781
782 void setPointers(const MotionEntry* entry);
783 };
784
785 Vector<KeyMemento> mKeyMementos;
786 Vector<MotionMemento> mMotionMementos;
Jeff Brown90f0cee2010-10-08 22:31:17 -0700787
788 static bool shouldCancelEvent(int32_t eventSource, CancelationOptions options);
Jeff Browna665ca82010-09-08 11:49:43 -0700789 };
790
Jeff Browne839a582010-04-22 18:58:52 -0700791 /* Manages the dispatch state associated with a single input channel. */
792 class Connection : public RefBase {
793 protected:
794 virtual ~Connection();
795
796 public:
797 enum Status {
798 // Everything is peachy.
799 STATUS_NORMAL,
800 // An unrecoverable communication error has occurred.
801 STATUS_BROKEN,
Jeff Browne839a582010-04-22 18:58:52 -0700802 // The input channel has been unregistered.
803 STATUS_ZOMBIE
804 };
805
806 Status status;
807 sp<InputChannel> inputChannel;
808 InputPublisher inputPublisher;
Jeff Browna665ca82010-09-08 11:49:43 -0700809 InputState inputState;
Jeff Browne839a582010-04-22 18:58:52 -0700810 Queue<DispatchEntry> outboundQueue;
Jeff Browne839a582010-04-22 18:58:52 -0700811
812 nsecs_t lastEventTime; // the time when the event was originally captured
813 nsecs_t lastDispatchTime; // the time when the last event was dispatched
Jeff Browne839a582010-04-22 18:58:52 -0700814
815 explicit Connection(const sp<InputChannel>& inputChannel);
816
Jeff Brown54bc2812010-06-15 01:31:58 -0700817 inline const char* getInputChannelName() const { return inputChannel->getName().string(); }
818
819 const char* getStatusLabel() const;
Jeff Browne839a582010-04-22 18:58:52 -0700820
821 // Finds a DispatchEntry in the outbound queue associated with the specified event.
822 // Returns NULL if not found.
823 DispatchEntry* findQueuedDispatchEntryForEvent(const EventEntry* eventEntry) const;
824
Jeff Browne839a582010-04-22 18:58:52 -0700825 // Gets the time since the current event was originally obtained from the input driver.
Jeff Browna665ca82010-09-08 11:49:43 -0700826 inline double getEventLatencyMillis(nsecs_t currentTime) const {
Jeff Browne839a582010-04-22 18:58:52 -0700827 return (currentTime - lastEventTime) / 1000000.0;
828 }
829
830 // Gets the time since the current event entered the outbound dispatch queue.
Jeff Browna665ca82010-09-08 11:49:43 -0700831 inline double getDispatchLatencyMillis(nsecs_t currentTime) const {
Jeff Browne839a582010-04-22 18:58:52 -0700832 return (currentTime - lastDispatchTime) / 1000000.0;
833 }
834
Jeff Browne839a582010-04-22 18:58:52 -0700835 status_t initialize();
836 };
837
Jeff Brown90f0cee2010-10-08 22:31:17 -0700838 enum DropReason {
839 DROP_REASON_NOT_DROPPED = 0,
840 DROP_REASON_POLICY = 1,
841 DROP_REASON_APP_SWITCH = 2,
842 DROP_REASON_DISABLED = 3,
843 };
844
Jeff Brown54bc2812010-06-15 01:31:58 -0700845 sp<InputDispatcherPolicyInterface> mPolicy;
Jeff Browne839a582010-04-22 18:58:52 -0700846
847 Mutex mLock;
848
Jeff Browne839a582010-04-22 18:58:52 -0700849 Allocator mAllocator;
Jeff Brown59abe7e2010-09-13 23:17:30 -0700850 sp<Looper> mLooper;
Jeff Browne839a582010-04-22 18:58:52 -0700851
Jeff Browna665ca82010-09-08 11:49:43 -0700852 EventEntry* mPendingEvent;
Jeff Brown54bc2812010-06-15 01:31:58 -0700853 Queue<EventEntry> mInboundQueue;
854 Queue<CommandEntry> mCommandQueue;
855
Jeff Browna665ca82010-09-08 11:49:43 -0700856 Vector<EventEntry*> mTempCancelationEvents;
857
858 void dispatchOnceInnerLocked(nsecs_t keyRepeatTimeout, nsecs_t keyRepeatDelay,
859 nsecs_t* nextWakeupTime);
860
Jeff Brown59abe7e2010-09-13 23:17:30 -0700861 // Enqueues an inbound event. Returns true if mLooper->wake() should be called.
Jeff Browna665ca82010-09-08 11:49:43 -0700862 bool enqueueInboundEventLocked(EventEntry* entry);
863
Jeff Brown90f0cee2010-10-08 22:31:17 -0700864 // Cleans up input state when dropping an inbound event.
865 void dropInboundEventLocked(EventEntry* entry, DropReason dropReason);
866
Jeff Browna665ca82010-09-08 11:49:43 -0700867 // App switch latency optimization.
Jeff Brown90f0cee2010-10-08 22:31:17 -0700868 bool mAppSwitchSawKeyDown;
Jeff Browna665ca82010-09-08 11:49:43 -0700869 nsecs_t mAppSwitchDueTime;
870
Jeff Brown90f0cee2010-10-08 22:31:17 -0700871 static bool isAppSwitchKeyCode(int32_t keyCode);
872 bool isAppSwitchKeyEventLocked(KeyEntry* keyEntry);
Jeff Browna665ca82010-09-08 11:49:43 -0700873 bool isAppSwitchPendingLocked();
Jeff Browna665ca82010-09-08 11:49:43 -0700874 void resetPendingAppSwitchLocked(bool handled);
875
Jeff Browne839a582010-04-22 18:58:52 -0700876 // All registered connections mapped by receive pipe file descriptor.
877 KeyedVector<int, sp<Connection> > mConnectionsByReceiveFd;
878
Jeff Brown53a415e2010-09-15 15:18:56 -0700879 ssize_t getConnectionIndexLocked(const sp<InputChannel>& inputChannel);
Jeff Brown0cacb872010-08-17 15:59:26 -0700880
Jeff Browne839a582010-04-22 18:58:52 -0700881 // Active connections are connections that have a non-empty outbound queue.
Jeff Brown51d45a72010-06-17 20:52:56 -0700882 // We don't use a ref-counted pointer here because we explicitly abort connections
883 // during unregistration which causes the connection's outbound queue to be cleared
884 // and the connection itself to be deactivated.
Jeff Browne839a582010-04-22 18:58:52 -0700885 Vector<Connection*> mActiveConnections;
886
Jeff Browna665ca82010-09-08 11:49:43 -0700887 // Input channels that will receive a copy of all input events.
888 Vector<sp<InputChannel> > mMonitoringChannels;
Jeff Browne839a582010-04-22 18:58:52 -0700889
Jeff Browna665ca82010-09-08 11:49:43 -0700890 // Preallocated key event object used for policy inquiries.
891 KeyEvent mReusableKeyEvent;
Jeff Browne839a582010-04-22 18:58:52 -0700892
Jeff Brown51d45a72010-06-17 20:52:56 -0700893 // Event injection and synchronization.
894 Condition mInjectionResultAvailableCondition;
Jeff Brown90f0cee2010-10-08 22:31:17 -0700895 bool hasInjectionPermission(int32_t injectorPid, int32_t injectorUid);
Jeff Brown51d45a72010-06-17 20:52:56 -0700896 void setInjectionResultLocked(EventEntry* entry, int32_t injectionResult);
897
Jeff Brownf67c53e2010-07-28 15:48:59 -0700898 Condition mInjectionSyncFinishedCondition;
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700899 void incrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brown53a415e2010-09-15 15:18:56 -0700900 void decrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brownf67c53e2010-07-28 15:48:59 -0700901
Jeff Brown542412c2010-08-18 15:51:08 -0700902 // Throttling state.
903 struct ThrottleState {
904 nsecs_t minTimeBetweenEvents;
905
906 nsecs_t lastEventTime;
907 int32_t lastDeviceId;
908 uint32_t lastSource;
909
910 uint32_t originalSampleCount; // only collected during debugging
911 } mThrottleState;
912
Jeff Browne839a582010-04-22 18:58:52 -0700913 // Key repeat tracking.
Jeff Browne839a582010-04-22 18:58:52 -0700914 struct KeyRepeatState {
915 KeyEntry* lastKeyEntry; // or null if no repeat
916 nsecs_t nextRepeatTime;
917 } mKeyRepeatState;
918
919 void resetKeyRepeatLocked();
Jeff Browna665ca82010-09-08 11:49:43 -0700920 KeyEntry* synthesizeKeyRepeatLocked(nsecs_t currentTime, nsecs_t keyRepeatTimeout);
Jeff Browne839a582010-04-22 18:58:52 -0700921
Jeff Brown54bc2812010-06-15 01:31:58 -0700922 // Deferred command processing.
923 bool runCommandsLockedInterruptible();
924 CommandEntry* postCommandLocked(Command command);
925
Jeff Browna665ca82010-09-08 11:49:43 -0700926 // Inbound event processing.
927 void drainInboundQueueLocked();
Jeff Brownd8816c32010-09-16 14:07:33 -0700928 void releasePendingEventLocked();
929 void releaseInboundEventLocked(EventEntry* entry);
Jeff Browne839a582010-04-22 18:58:52 -0700930
Jeff Browna665ca82010-09-08 11:49:43 -0700931 // Dispatch state.
932 bool mDispatchEnabled;
933 bool mDispatchFrozen;
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700934
Jeff Browna665ca82010-09-08 11:49:43 -0700935 Vector<InputWindow> mWindows;
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700936
937 const InputWindow* getWindowLocked(const sp<InputChannel>& inputChannel);
Jeff Browna665ca82010-09-08 11:49:43 -0700938
939 // Focus tracking for keys, trackball, etc.
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700940 const InputWindow* mFocusedWindow;
Jeff Browna665ca82010-09-08 11:49:43 -0700941
942 // Focus tracking for touch.
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700943 struct TouchedWindow {
944 const InputWindow* window;
945 int32_t targetFlags;
946 BitSet32 pointerIds;
947 sp<InputChannel> channel;
Jeff Browna665ca82010-09-08 11:49:43 -0700948 };
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700949 struct TouchState {
950 bool down;
951 bool split;
952 Vector<TouchedWindow> windows;
953
954 TouchState();
955 ~TouchState();
956 void reset();
957 void copyFrom(const TouchState& other);
958 void addOrUpdateWindow(const InputWindow* window, int32_t targetFlags, BitSet32 pointerIds);
959 void removeOutsideTouchWindows();
960 const InputWindow* getFirstForegroundWindow();
961 };
962
963 TouchState mTouchState;
964 TouchState mTempTouchState;
Jeff Browna665ca82010-09-08 11:49:43 -0700965
966 // Focused application.
967 InputApplication* mFocusedApplication;
968 InputApplication mFocusedApplicationStorage; // preallocated storage for mFocusedApplication
969 void releaseFocusedApplicationLocked();
970
971 // Dispatch inbound events.
972 bool dispatchConfigurationChangedLocked(
973 nsecs_t currentTime, ConfigurationChangedEntry* entry);
974 bool dispatchKeyLocked(
975 nsecs_t currentTime, KeyEntry* entry, nsecs_t keyRepeatTimeout,
Jeff Brown33d54ce2010-10-11 14:20:19 -0700976 DropReason* dropReason, nsecs_t* nextWakeupTime);
Jeff Browna665ca82010-09-08 11:49:43 -0700977 bool dispatchMotionLocked(
978 nsecs_t currentTime, MotionEntry* entry,
Jeff Brown33d54ce2010-10-11 14:20:19 -0700979 DropReason* dropReason, nsecs_t* nextWakeupTime);
Jeff Brown54bc2812010-06-15 01:31:58 -0700980 void dispatchEventToCurrentInputTargetsLocked(
981 nsecs_t currentTime, EventEntry* entry, bool resumeWithAppendedMotionSample);
Jeff Browne839a582010-04-22 18:58:52 -0700982
Jeff Browna665ca82010-09-08 11:49:43 -0700983 void logOutboundKeyDetailsLocked(const char* prefix, const KeyEntry* entry);
984 void logOutboundMotionDetailsLocked(const char* prefix, const MotionEntry* entry);
985
986 // The input targets that were most recently identified for dispatch.
Jeff Browna665ca82010-09-08 11:49:43 -0700987 bool mCurrentInputTargetsValid; // false while targets are being recomputed
988 Vector<InputTarget> mCurrentInputTargets;
Jeff Browna665ca82010-09-08 11:49:43 -0700989
990 enum InputTargetWaitCause {
991 INPUT_TARGET_WAIT_CAUSE_NONE,
992 INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY,
993 INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY,
994 };
995
996 InputTargetWaitCause mInputTargetWaitCause;
997 nsecs_t mInputTargetWaitStartTime;
998 nsecs_t mInputTargetWaitTimeoutTime;
999 bool mInputTargetWaitTimeoutExpired;
1000
1001 // Finding targets for input events.
Jeff Brownd8816c32010-09-16 14:07:33 -07001002 void resetTargetsLocked();
Jeff Brownd1b0a2b2010-09-26 22:20:12 -07001003 void commitTargetsLocked();
Jeff Browna665ca82010-09-08 11:49:43 -07001004 int32_t handleTargetsNotReadyLocked(nsecs_t currentTime, const EventEntry* entry,
1005 const InputApplication* application, const InputWindow* window,
1006 nsecs_t* nextWakeupTime);
Jeff Brown53a415e2010-09-15 15:18:56 -07001007 void resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout,
1008 const sp<InputChannel>& inputChannel);
1009 nsecs_t getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime);
Jeff Browna665ca82010-09-08 11:49:43 -07001010 void resetANRTimeoutsLocked();
1011
Jeff Brownd1b0a2b2010-09-26 22:20:12 -07001012 int32_t findFocusedWindowTargetsLocked(nsecs_t currentTime, const EventEntry* entry,
1013 nsecs_t* nextWakeupTime);
1014 int32_t findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry* entry,
1015 nsecs_t* nextWakeupTime);
Jeff Browna665ca82010-09-08 11:49:43 -07001016
Jeff Brownd1b0a2b2010-09-26 22:20:12 -07001017 void addWindowTargetLocked(const InputWindow* window, int32_t targetFlags,
1018 BitSet32 pointerIds);
Jeff Browna665ca82010-09-08 11:49:43 -07001019 void addMonitoringTargetsLocked();
Jeff Brownd1b0a2b2010-09-26 22:20:12 -07001020 bool shouldPokeUserActivityForCurrentInputTargetsLocked();
1021 void pokeUserActivityLocked(nsecs_t eventTime, int32_t eventType);
1022 bool checkInjectionPermission(const InputWindow* window, const InjectionState* injectionState);
Jeff Brown35cf0e92010-10-05 12:26:23 -07001023 bool isWindowObscuredAtPointLocked(const InputWindow* window, int32_t x, int32_t y) const;
Jeff Brown53a415e2010-09-15 15:18:56 -07001024 bool isWindowFinishedWithPreviousInputLocked(const InputWindow* window);
Jeff Brown53a415e2010-09-15 15:18:56 -07001025 String8 getApplicationWindowLabelLocked(const InputApplication* application,
1026 const InputWindow* window);
Jeff Browna665ca82010-09-08 11:49:43 -07001027
Jeff Browne839a582010-04-22 18:58:52 -07001028 // Manage the dispatch cycle for a single connection.
Jeff Brown51d45a72010-06-17 20:52:56 -07001029 // These methods are deliberately not Interruptible because doing all of the work
1030 // with the mutex held makes it easier to ensure that connection invariants are maintained.
1031 // If needed, the methods post commands to run later once the critical bits are done.
1032 void prepareDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Jeff Browne839a582010-04-22 18:58:52 -07001033 EventEntry* eventEntry, const InputTarget* inputTarget,
1034 bool resumeWithAppendedMotionSample);
Jeff Brown53a415e2010-09-15 15:18:56 -07001035 void startDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown51d45a72010-06-17 20:52:56 -07001036 void finishDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Browna665ca82010-09-08 11:49:43 -07001037 void startNextDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown90f0cee2010-10-08 22:31:17 -07001038 void abortBrokenDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown53a415e2010-09-15 15:18:56 -07001039 void drainOutboundQueueLocked(Connection* connection);
Jeff Brown59abe7e2010-09-13 23:17:30 -07001040 static int handleReceiveCallback(int receiveFd, int events, void* data);
Jeff Browne839a582010-04-22 18:58:52 -07001041
Jeff Brown90f0cee2010-10-08 22:31:17 -07001042 void synthesizeCancelationEventsForAllConnectionsLocked(
1043 InputState::CancelationOptions options, const char* reason);
1044 void synthesizeCancelationEventsForInputChannelLocked(const sp<InputChannel>& channel,
1045 InputState::CancelationOptions options, const char* reason);
1046 void synthesizeCancelationEventsForConnectionLocked(const sp<Connection>& connection,
1047 InputState::CancelationOptions options, const char* reason);
1048
Jeff Brownd1b0a2b2010-09-26 22:20:12 -07001049 // Splitting motion events across windows.
1050 MotionEntry* splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds);
1051
Jeff Browna665ca82010-09-08 11:49:43 -07001052 // Dump state.
1053 void dumpDispatchStateLocked(String8& dump);
1054 void logDispatchStateLocked();
1055
Jeff Browne839a582010-04-22 18:58:52 -07001056 // Add or remove a connection to the mActiveConnections vector.
1057 void activateConnectionLocked(Connection* connection);
1058 void deactivateConnectionLocked(Connection* connection);
1059
1060 // Interesting events that we might like to log or tell the framework about.
Jeff Brown54bc2812010-06-15 01:31:58 -07001061 void onDispatchCycleStartedLocked(
Jeff Brown51d45a72010-06-17 20:52:56 -07001062 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown54bc2812010-06-15 01:31:58 -07001063 void onDispatchCycleFinishedLocked(
Jeff Brown51d45a72010-06-17 20:52:56 -07001064 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown54bc2812010-06-15 01:31:58 -07001065 void onDispatchCycleBrokenLocked(
Jeff Brown51d45a72010-06-17 20:52:56 -07001066 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown53a415e2010-09-15 15:18:56 -07001067 void onANRLocked(
1068 nsecs_t currentTime, const InputApplication* application, const InputWindow* window,
1069 nsecs_t eventTime, nsecs_t waitStartTime);
Jeff Brown54bc2812010-06-15 01:31:58 -07001070
Jeff Brown51d45a72010-06-17 20:52:56 -07001071 // Outbound policy interactions.
Jeff Browna665ca82010-09-08 11:49:43 -07001072 void doNotifyConfigurationChangedInterruptible(CommandEntry* commandEntry);
Jeff Brown54bc2812010-06-15 01:31:58 -07001073 void doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown53a415e2010-09-15 15:18:56 -07001074 void doNotifyANRLockedInterruptible(CommandEntry* commandEntry);
Jeff Browna665ca82010-09-08 11:49:43 -07001075 void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry);
1076 void doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown53a415e2010-09-15 15:18:56 -07001077
1078 // Statistics gathering.
1079 void updateDispatchStatisticsLocked(nsecs_t currentTime, const EventEntry* entry,
1080 int32_t injectionResult, nsecs_t timeSpentWaitingForApplication);
Jeff Browne839a582010-04-22 18:58:52 -07001081};
1082
1083/* Enqueues and dispatches input events, endlessly. */
1084class InputDispatcherThread : public Thread {
1085public:
1086 explicit InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher);
1087 ~InputDispatcherThread();
1088
1089private:
1090 virtual bool threadLoop();
1091
1092 sp<InputDispatcherInterface> mDispatcher;
1093};
1094
1095} // namespace android
1096
Jeff Browna665ca82010-09-08 11:49:43 -07001097#endif // _UI_INPUT_DISPATCHER_H