blob: 0834e86505327a34b9acb34b2fed9928138a5f86 [file] [log] [blame]
Jeff Brown46b9ac02010-04-22 18:58:52 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _UI_INPUT_DISPATCHER_H
18#define _UI_INPUT_DISPATCHER_H
19
20#include <ui/Input.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070021#include <ui/InputTransport.h>
22#include <utils/KeyedVector.h>
23#include <utils/Vector.h>
24#include <utils/threads.h>
25#include <utils/Timers.h>
26#include <utils/RefBase.h>
27#include <utils/String8.h>
Jeff Brown4fe6c3e2010-09-13 23:17:30 -070028#include <utils/Looper.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070029#include <utils/Pool.h>
Jeff Brown01ce2e92010-09-26 22:20:12 -070030#include <utils/BitSet.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070031
32#include <stddef.h>
33#include <unistd.h>
Jeff Brownb88102f2010-09-08 11:49:43 -070034#include <limits.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070035
36
37namespace android {
38
Jeff Brown9c3cda02010-06-15 01:31:58 -070039/*
Jeff Brown7fbdc842010-06-17 20:52:56 -070040 * Constants used to report the outcome of input event injection.
41 */
42enum {
43 /* (INTERNAL USE ONLY) Specifies that injection is pending and its outcome is unknown. */
44 INPUT_EVENT_INJECTION_PENDING = -1,
45
46 /* Injection succeeded. */
47 INPUT_EVENT_INJECTION_SUCCEEDED = 0,
48
49 /* Injection failed because the injector did not have permission to inject
50 * into the application with input focus. */
51 INPUT_EVENT_INJECTION_PERMISSION_DENIED = 1,
52
53 /* Injection failed because there were no available input targets. */
54 INPUT_EVENT_INJECTION_FAILED = 2,
55
56 /* Injection failed due to a timeout. */
57 INPUT_EVENT_INJECTION_TIMED_OUT = 3
58};
59
Jeff Brown6ec402b2010-07-28 15:48:59 -070060/*
61 * Constants used to determine the input event injection synchronization mode.
62 */
63enum {
64 /* Injection is asynchronous and is assumed always to be successful. */
65 INPUT_EVENT_INJECTION_SYNC_NONE = 0,
66
67 /* Waits for previous events to be dispatched so that the input dispatcher can determine
68 * whether input event injection willbe permitted based on the current input focus.
69 * Does not wait for the input event to finish processing. */
70 INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT = 1,
71
72 /* Waits for the input event to be completely processed. */
73 INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISHED = 2,
74};
75
Jeff Brown7fbdc842010-06-17 20:52:56 -070076
77/*
Jeff Brown9c3cda02010-06-15 01:31:58 -070078 * An input target specifies how an input event is to be dispatched to a particular window
79 * including the window's input channel, control flags, a timeout, and an X / Y offset to
80 * be added to input event coordinates to compensate for the absolute position of the
81 * window area.
82 */
83struct InputTarget {
84 enum {
Jeff Brown519e0242010-09-15 15:18:56 -070085 /* This flag indicates that the event is being delivered to a foreground application. */
86 FLAG_FOREGROUND = 0x01,
Jeff Brown9c3cda02010-06-15 01:31:58 -070087
Jeff Brown85a31762010-09-01 17:01:00 -070088 /* This flag indicates that a MotionEvent with AMOTION_EVENT_ACTION_DOWN falls outside
89 * of the area of this target and so should instead be delivered as an
90 * AMOTION_EVENT_ACTION_OUTSIDE to this target. */
Jeff Brown9c3cda02010-06-15 01:31:58 -070091 FLAG_OUTSIDE = 0x02,
92
Jeff Brown85a31762010-09-01 17:01:00 -070093 /* This flag indicates that the target of a MotionEvent is partly or wholly
94 * obscured by another visible window above it. The motion event should be
95 * delivered with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED. */
Jeff Brown01ce2e92010-09-26 22:20:12 -070096 FLAG_WINDOW_IS_OBSCURED = 0x04,
97
98 /* This flag indicates that a motion event is being split across multiple windows. */
99 FLAG_SPLIT = 0x08,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700100 };
101
102 // The input channel to be targeted.
103 sp<InputChannel> inputChannel;
104
105 // Flags for the input target.
106 int32_t flags;
107
Jeff Brown9c3cda02010-06-15 01:31:58 -0700108 // The x and y offset to add to a MotionEvent as it is delivered.
109 // (ignored for KeyEvents)
110 float xOffset, yOffset;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700111
112 // The window type of the input target.
113 int32_t windowType;
114
115 // The subset of pointer ids to include in motion events dispatched to this input target
116 // if FLAG_SPLIT is set.
117 BitSet32 pointerIds;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700118};
119
Jeff Brown7fbdc842010-06-17 20:52:56 -0700120
Jeff Brown9c3cda02010-06-15 01:31:58 -0700121/*
Jeff Brownb88102f2010-09-08 11:49:43 -0700122 * An input window describes the bounds of a window that can receive input.
123 */
124struct InputWindow {
125 // Window flags from WindowManager.LayoutParams
126 enum {
127 FLAG_ALLOW_LOCK_WHILE_SCREEN_ON = 0x00000001,
128 FLAG_DIM_BEHIND = 0x00000002,
129 FLAG_BLUR_BEHIND = 0x00000004,
130 FLAG_NOT_FOCUSABLE = 0x00000008,
131 FLAG_NOT_TOUCHABLE = 0x00000010,
132 FLAG_NOT_TOUCH_MODAL = 0x00000020,
133 FLAG_TOUCHABLE_WHEN_WAKING = 0x00000040,
134 FLAG_KEEP_SCREEN_ON = 0x00000080,
135 FLAG_LAYOUT_IN_SCREEN = 0x00000100,
136 FLAG_LAYOUT_NO_LIMITS = 0x00000200,
137 FLAG_FULLSCREEN = 0x00000400,
138 FLAG_FORCE_NOT_FULLSCREEN = 0x00000800,
139 FLAG_DITHER = 0x00001000,
140 FLAG_SECURE = 0x00002000,
141 FLAG_SCALED = 0x00004000,
142 FLAG_IGNORE_CHEEK_PRESSES = 0x00008000,
143 FLAG_LAYOUT_INSET_DECOR = 0x00010000,
144 FLAG_ALT_FOCUSABLE_IM = 0x00020000,
145 FLAG_WATCH_OUTSIDE_TOUCH = 0x00040000,
146 FLAG_SHOW_WHEN_LOCKED = 0x00080000,
147 FLAG_SHOW_WALLPAPER = 0x00100000,
148 FLAG_TURN_SCREEN_ON = 0x00200000,
149 FLAG_DISMISS_KEYGUARD = 0x00400000,
Jeff Brown01ce2e92010-09-26 22:20:12 -0700150 FLAG_SPLIT_TOUCH = 0x00800000,
Jeff Brownb88102f2010-09-08 11:49:43 -0700151 FLAG_KEEP_SURFACE_WHILE_ANIMATING = 0x10000000,
152 FLAG_COMPATIBLE_WINDOW = 0x20000000,
153 FLAG_SYSTEM_ERROR = 0x40000000,
154 };
155
156 // Window types from WindowManager.LayoutParams
157 enum {
158 FIRST_APPLICATION_WINDOW = 1,
159 TYPE_BASE_APPLICATION = 1,
160 TYPE_APPLICATION = 2,
161 TYPE_APPLICATION_STARTING = 3,
162 LAST_APPLICATION_WINDOW = 99,
163 FIRST_SUB_WINDOW = 1000,
164 TYPE_APPLICATION_PANEL = FIRST_SUB_WINDOW,
165 TYPE_APPLICATION_MEDIA = FIRST_SUB_WINDOW+1,
166 TYPE_APPLICATION_SUB_PANEL = FIRST_SUB_WINDOW+2,
167 TYPE_APPLICATION_ATTACHED_DIALOG = FIRST_SUB_WINDOW+3,
168 TYPE_APPLICATION_MEDIA_OVERLAY = FIRST_SUB_WINDOW+4,
169 LAST_SUB_WINDOW = 1999,
170 FIRST_SYSTEM_WINDOW = 2000,
171 TYPE_STATUS_BAR = FIRST_SYSTEM_WINDOW,
172 TYPE_SEARCH_BAR = FIRST_SYSTEM_WINDOW+1,
173 TYPE_PHONE = FIRST_SYSTEM_WINDOW+2,
174 TYPE_SYSTEM_ALERT = FIRST_SYSTEM_WINDOW+3,
175 TYPE_KEYGUARD = FIRST_SYSTEM_WINDOW+4,
176 TYPE_TOAST = FIRST_SYSTEM_WINDOW+5,
177 TYPE_SYSTEM_OVERLAY = FIRST_SYSTEM_WINDOW+6,
178 TYPE_PRIORITY_PHONE = FIRST_SYSTEM_WINDOW+7,
179 TYPE_SYSTEM_DIALOG = FIRST_SYSTEM_WINDOW+8,
180 TYPE_KEYGUARD_DIALOG = FIRST_SYSTEM_WINDOW+9,
181 TYPE_SYSTEM_ERROR = FIRST_SYSTEM_WINDOW+10,
182 TYPE_INPUT_METHOD = FIRST_SYSTEM_WINDOW+11,
183 TYPE_INPUT_METHOD_DIALOG= FIRST_SYSTEM_WINDOW+12,
184 TYPE_WALLPAPER = FIRST_SYSTEM_WINDOW+13,
185 TYPE_STATUS_BAR_PANEL = FIRST_SYSTEM_WINDOW+14,
186 LAST_SYSTEM_WINDOW = 2999,
187 };
188
189 sp<InputChannel> inputChannel;
Jeff Brown519e0242010-09-15 15:18:56 -0700190 String8 name;
Jeff Brownb88102f2010-09-08 11:49:43 -0700191 int32_t layoutParamsFlags;
192 int32_t layoutParamsType;
193 nsecs_t dispatchingTimeout;
194 int32_t frameLeft;
195 int32_t frameTop;
196 int32_t frameRight;
197 int32_t frameBottom;
198 int32_t visibleFrameLeft;
199 int32_t visibleFrameTop;
200 int32_t visibleFrameRight;
201 int32_t visibleFrameBottom;
202 int32_t touchableAreaLeft;
203 int32_t touchableAreaTop;
204 int32_t touchableAreaRight;
205 int32_t touchableAreaBottom;
206 bool visible;
Jeff Brown519e0242010-09-15 15:18:56 -0700207 bool canReceiveKeys;
Jeff Brownb88102f2010-09-08 11:49:43 -0700208 bool hasFocus;
209 bool hasWallpaper;
210 bool paused;
Jeff Brown519e0242010-09-15 15:18:56 -0700211 int32_t layer;
Jeff Brownb88102f2010-09-08 11:49:43 -0700212 int32_t ownerPid;
213 int32_t ownerUid;
214
Jeff Brownb88102f2010-09-08 11:49:43 -0700215 bool touchableAreaContainsPoint(int32_t x, int32_t y) const;
Jeff Brown19dfc832010-10-05 12:26:23 -0700216 bool frameContainsPoint(int32_t x, int32_t y) const;
217
218 /* Returns true if the window is of a trusted type that is allowed to silently
219 * overlay other windows for the purpose of implementing the secure views feature.
220 * Trusted overlays, such as IME windows, can partly obscure other windows without causing
221 * motion events to be delivered to them with AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED.
222 */
223 bool isTrustedOverlay() const;
Jeff Brownb88102f2010-09-08 11:49:43 -0700224};
225
226
227/*
228 * A private handle type used by the input manager to track the window.
229 */
230class InputApplicationHandle : public RefBase {
231protected:
232 InputApplicationHandle() { }
233 virtual ~InputApplicationHandle() { }
234};
235
236
237/*
238 * An input application describes properties of an application that can receive input.
239 */
240struct InputApplication {
241 String8 name;
242 nsecs_t dispatchingTimeout;
243 sp<InputApplicationHandle> handle;
244};
245
246
247/*
Jeff Brown9c3cda02010-06-15 01:31:58 -0700248 * Input dispatcher policy interface.
249 *
250 * The input reader policy is used by the input reader to interact with the Window Manager
251 * and other system components.
252 *
253 * The actual implementation is partially supported by callbacks into the DVM
254 * via JNI. This interface is also mocked in the unit tests.
255 */
256class InputDispatcherPolicyInterface : public virtual RefBase {
257protected:
258 InputDispatcherPolicyInterface() { }
259 virtual ~InputDispatcherPolicyInterface() { }
260
261public:
262 /* Notifies the system that a configuration change has occurred. */
263 virtual void notifyConfigurationChanged(nsecs_t when) = 0;
264
Jeff Brownb88102f2010-09-08 11:49:43 -0700265 /* Notifies the system that an application is not responding.
266 * Returns a new timeout to continue waiting, or 0 to abort dispatch. */
Jeff Brown519e0242010-09-15 15:18:56 -0700267 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
268 const sp<InputChannel>& inputChannel) = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700269
Jeff Brown9c3cda02010-06-15 01:31:58 -0700270 /* Notifies the system that an input channel is unrecoverably broken. */
271 virtual void notifyInputChannelBroken(const sp<InputChannel>& inputChannel) = 0;
272
Jeff Brownb21fb102010-09-07 10:44:57 -0700273 /* Gets the key repeat initial timeout or -1 if automatic key repeating is disabled. */
Jeff Brown9c3cda02010-06-15 01:31:58 -0700274 virtual nsecs_t getKeyRepeatTimeout() = 0;
275
Jeff Brownb21fb102010-09-07 10:44:57 -0700276 /* Gets the key repeat inter-key delay. */
277 virtual nsecs_t getKeyRepeatDelay() = 0;
278
Jeff Brownae9fc032010-08-18 15:51:08 -0700279 /* Gets the maximum suggested event delivery rate per second.
280 * This value is used to throttle motion event movement actions on a per-device
281 * basis. It is not intended to be a hard limit.
282 */
283 virtual int32_t getMaxEventsPerSecond() = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700284
Jeff Brownb6997262010-10-08 22:31:17 -0700285 /* Intercepts a key event immediately before queueing it.
286 * The policy can use this method as an opportunity to perform power management functions
287 * and early event preprocessing such as updating policy flags.
288 *
289 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
290 * should be dispatched to applications.
291 */
292 virtual void interceptKeyBeforeQueueing(nsecs_t when, int32_t deviceId,
293 int32_t action, int32_t& flags, int32_t keyCode, int32_t scanCode,
294 uint32_t& policyFlags) = 0;
295
296 /* Intercepts a generic touch, trackball or other event before queueing it.
297 * The policy can use this method as an opportunity to perform power management functions
298 * and early event preprocessing such as updating policy flags.
299 *
300 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
301 * should be dispatched to applications.
302 */
303 virtual void interceptGenericBeforeQueueing(nsecs_t when, uint32_t& policyFlags) = 0;
304
Jeff Brownb88102f2010-09-08 11:49:43 -0700305 /* Allows the policy a chance to intercept a key before dispatching. */
306 virtual bool interceptKeyBeforeDispatching(const sp<InputChannel>& inputChannel,
307 const KeyEvent* keyEvent, uint32_t policyFlags) = 0;
308
Jeff Brownb6997262010-10-08 22:31:17 -0700309 /* Notifies the policy about switch events.
310 */
311 virtual void notifySwitch(nsecs_t when,
312 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) = 0;
313
Jeff Brownb88102f2010-09-08 11:49:43 -0700314 /* Poke user activity for an event dispatched to a window. */
Jeff Brown01ce2e92010-09-26 22:20:12 -0700315 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType) = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700316
317 /* Checks whether a given application pid/uid has permission to inject input events
318 * into other applications.
319 *
320 * This method is special in that its implementation promises to be non-reentrant and
321 * is safe to call while holding other locks. (Most other methods make no such guarantees!)
322 */
323 virtual bool checkInjectEventsPermissionNonReentrant(
324 int32_t injectorPid, int32_t injectorUid) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700325};
326
327
Jeff Brown46b9ac02010-04-22 18:58:52 -0700328/* Notifies the system about input events generated by the input reader.
329 * The dispatcher is expected to be mostly asynchronous. */
330class InputDispatcherInterface : public virtual RefBase {
331protected:
332 InputDispatcherInterface() { }
333 virtual ~InputDispatcherInterface() { }
334
335public:
Jeff Brownb88102f2010-09-08 11:49:43 -0700336 /* Dumps the state of the input dispatcher.
337 *
338 * This method may be called on any thread (usually by the input manager). */
339 virtual void dump(String8& dump) = 0;
340
Jeff Brown46b9ac02010-04-22 18:58:52 -0700341 /* Runs a single iteration of the dispatch loop.
342 * Nominally processes one queued event, a timeout, or a response from an input consumer.
343 *
344 * This method should only be called on the input dispatcher thread.
345 */
346 virtual void dispatchOnce() = 0;
347
348 /* Notifies the dispatcher about new events.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700349 *
350 * These methods should only be called on the input reader thread.
351 */
Jeff Brown9c3cda02010-06-15 01:31:58 -0700352 virtual void notifyConfigurationChanged(nsecs_t eventTime) = 0;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700353 virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700354 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode,
355 int32_t scanCode, int32_t metaState, nsecs_t downTime) = 0;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700356 virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Brown85a31762010-09-01 17:01:00 -0700357 uint32_t policyFlags, int32_t action, int32_t flags,
358 int32_t metaState, int32_t edgeFlags,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700359 uint32_t pointerCount, const int32_t* pointerIds, const PointerCoords* pointerCoords,
360 float xPrecision, float yPrecision, nsecs_t downTime) = 0;
Jeff Brownb6997262010-10-08 22:31:17 -0700361 virtual void notifySwitch(nsecs_t when,
362 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700363
Jeff Brown7fbdc842010-06-17 20:52:56 -0700364 /* Injects an input event and optionally waits for sync.
Jeff Brown6ec402b2010-07-28 15:48:59 -0700365 * The synchronization mode determines whether the method blocks while waiting for
366 * input injection to proceed.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700367 * Returns one of the INPUT_EVENT_INJECTION_XXX constants.
368 *
369 * This method may be called on any thread (usually by the input manager).
370 */
371 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brown6ec402b2010-07-28 15:48:59 -0700372 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis) = 0;
Jeff Brown7fbdc842010-06-17 20:52:56 -0700373
Jeff Brownb88102f2010-09-08 11:49:43 -0700374 /* Sets the list of input windows.
375 *
376 * This method may be called on any thread (usually by the input manager).
377 */
378 virtual void setInputWindows(const Vector<InputWindow>& inputWindows) = 0;
379
380 /* Sets the focused application.
381 *
382 * This method may be called on any thread (usually by the input manager).
383 */
384 virtual void setFocusedApplication(const InputApplication* inputApplication) = 0;
385
386 /* Sets the input dispatching mode.
387 *
388 * This method may be called on any thread (usually by the input manager).
389 */
390 virtual void setInputDispatchMode(bool enabled, bool frozen) = 0;
391
Jeff Brown46b9ac02010-04-22 18:58:52 -0700392 /* Registers or unregister input channels that may be used as targets for input events.
Jeff Brownb88102f2010-09-08 11:49:43 -0700393 * If monitor is true, the channel will receive a copy of all input events.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700394 *
395 * These methods may be called on any thread (usually by the input manager).
396 */
Jeff Brownb88102f2010-09-08 11:49:43 -0700397 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel, bool monitor) = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700398 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) = 0;
399};
400
Jeff Brown9c3cda02010-06-15 01:31:58 -0700401/* Dispatches events to input targets. Some functions of the input dispatcher, such as
402 * identifying input targets, are controlled by a separate policy object.
403 *
404 * IMPORTANT INVARIANT:
405 * Because the policy can potentially block or cause re-entrance into the input dispatcher,
406 * the input dispatcher never calls into the policy while holding its internal locks.
407 * The implementation is also carefully designed to recover from scenarios such as an
408 * input channel becoming unregistered while identifying input targets or processing timeouts.
409 *
410 * Methods marked 'Locked' must be called with the lock acquired.
411 *
412 * Methods marked 'LockedInterruptible' must be called with the lock acquired but
413 * may during the course of their execution release the lock, call into the policy, and
414 * then reacquire the lock. The caller is responsible for recovering gracefully.
415 *
416 * A 'LockedInterruptible' method may called a 'Locked' method, but NOT vice-versa.
417 */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700418class InputDispatcher : public InputDispatcherInterface {
419protected:
420 virtual ~InputDispatcher();
421
422public:
Jeff Brown9c3cda02010-06-15 01:31:58 -0700423 explicit InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700424
Jeff Brownb88102f2010-09-08 11:49:43 -0700425 virtual void dump(String8& dump);
426
Jeff Brown46b9ac02010-04-22 18:58:52 -0700427 virtual void dispatchOnce();
428
Jeff Brown9c3cda02010-06-15 01:31:58 -0700429 virtual void notifyConfigurationChanged(nsecs_t eventTime);
Jeff Brownc5ed5912010-07-14 18:48:53 -0700430 virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700431 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode,
432 int32_t scanCode, int32_t metaState, nsecs_t downTime);
Jeff Brownc5ed5912010-07-14 18:48:53 -0700433 virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Brown85a31762010-09-01 17:01:00 -0700434 uint32_t policyFlags, int32_t action, int32_t flags,
435 int32_t metaState, int32_t edgeFlags,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700436 uint32_t pointerCount, const int32_t* pointerIds, const PointerCoords* pointerCoords,
437 float xPrecision, float yPrecision, nsecs_t downTime);
Jeff Brownb6997262010-10-08 22:31:17 -0700438 virtual void notifySwitch(nsecs_t when,
439 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) ;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700440
Jeff Brown7fbdc842010-06-17 20:52:56 -0700441 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brown6ec402b2010-07-28 15:48:59 -0700442 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700443
Jeff Brownb88102f2010-09-08 11:49:43 -0700444 virtual void setInputWindows(const Vector<InputWindow>& inputWindows);
445 virtual void setFocusedApplication(const InputApplication* inputApplication);
446 virtual void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown349703e2010-06-22 01:27:15 -0700447
Jeff Brownb88102f2010-09-08 11:49:43 -0700448 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel, bool monitor);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700449 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel);
450
451private:
452 template <typename T>
453 struct Link {
454 T* next;
455 T* prev;
456 };
457
Jeff Brown01ce2e92010-09-26 22:20:12 -0700458 struct InjectionState {
459 mutable int32_t refCount;
460
461 int32_t injectorPid;
462 int32_t injectorUid;
463 int32_t injectionResult; // initially INPUT_EVENT_INJECTION_PENDING
464 bool injectionIsAsync; // set to true if injection is not waiting for the result
465 int32_t pendingForegroundDispatches; // the number of foreground dispatches in progress
466 };
467
Jeff Brown46b9ac02010-04-22 18:58:52 -0700468 struct EventEntry : Link<EventEntry> {
469 enum {
470 TYPE_SENTINEL,
471 TYPE_CONFIGURATION_CHANGED,
472 TYPE_KEY,
473 TYPE_MOTION
474 };
475
Jeff Brown01ce2e92010-09-26 22:20:12 -0700476 mutable int32_t refCount;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700477 int32_t type;
478 nsecs_t eventTime;
Jeff Brownb6997262010-10-08 22:31:17 -0700479 uint32_t policyFlags;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700480 InjectionState* injectionState;
Jeff Brown7fbdc842010-06-17 20:52:56 -0700481
Jeff Brown9c3cda02010-06-15 01:31:58 -0700482 bool dispatchInProgress; // initially false, set to true while dispatching
Jeff Brown7fbdc842010-06-17 20:52:56 -0700483
Jeff Brown01ce2e92010-09-26 22:20:12 -0700484 inline bool isInjected() { return injectionState != NULL; }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700485 };
486
487 struct ConfigurationChangedEntry : EventEntry {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700488 };
489
490 struct KeyEntry : EventEntry {
491 int32_t deviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700492 int32_t source;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700493 int32_t action;
494 int32_t flags;
495 int32_t keyCode;
496 int32_t scanCode;
497 int32_t metaState;
498 int32_t repeatCount;
499 nsecs_t downTime;
Jeff Brownb88102f2010-09-08 11:49:43 -0700500
501 bool syntheticRepeat; // set to true for synthetic key repeats
502
503 enum InterceptKeyResult {
504 INTERCEPT_KEY_RESULT_UNKNOWN,
505 INTERCEPT_KEY_RESULT_SKIP,
506 INTERCEPT_KEY_RESULT_CONTINUE,
507 };
508 InterceptKeyResult interceptKeyResult; // set based on the interception result
Jeff Brown46b9ac02010-04-22 18:58:52 -0700509 };
510
511 struct MotionSample {
512 MotionSample* next;
513
514 nsecs_t eventTime;
515 PointerCoords pointerCoords[MAX_POINTERS];
516 };
517
518 struct MotionEntry : EventEntry {
519 int32_t deviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700520 int32_t source;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700521 int32_t action;
Jeff Brown85a31762010-09-01 17:01:00 -0700522 int32_t flags;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700523 int32_t metaState;
524 int32_t edgeFlags;
525 float xPrecision;
526 float yPrecision;
527 nsecs_t downTime;
528 uint32_t pointerCount;
529 int32_t pointerIds[MAX_POINTERS];
530
531 // Linked list of motion samples associated with this motion event.
532 MotionSample firstSample;
533 MotionSample* lastSample;
Jeff Brownae9fc032010-08-18 15:51:08 -0700534
535 uint32_t countSamples() const;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700536 };
537
Jeff Brown9c3cda02010-06-15 01:31:58 -0700538 // Tracks the progress of dispatching a particular event to a particular connection.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700539 struct DispatchEntry : Link<DispatchEntry> {
540 EventEntry* eventEntry; // the event to dispatch
541 int32_t targetFlags;
542 float xOffset;
543 float yOffset;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700544
545 // True if dispatch has started.
546 bool inProgress;
547
548 // For motion events:
549 // Pointer to the first motion sample to dispatch in this cycle.
550 // Usually NULL to indicate that the list of motion samples begins at
551 // MotionEntry::firstSample. Otherwise, some samples were dispatched in a previous
552 // cycle and this pointer indicates the location of the first remainining sample
553 // to dispatch during the current cycle.
554 MotionSample* headMotionSample;
555 // Pointer to a motion sample to dispatch in the next cycle if the dispatcher was
556 // unable to send all motion samples during this cycle. On the next cycle,
557 // headMotionSample will be initialized to tailMotionSample and tailMotionSample
558 // will be set to NULL.
559 MotionSample* tailMotionSample;
Jeff Brown6ec402b2010-07-28 15:48:59 -0700560
Jeff Brown519e0242010-09-15 15:18:56 -0700561 inline bool hasForegroundTarget() const {
562 return targetFlags & InputTarget::FLAG_FOREGROUND;
Jeff Brownb88102f2010-09-08 11:49:43 -0700563 }
Jeff Brown01ce2e92010-09-26 22:20:12 -0700564
565 inline bool isSplit() const {
566 return targetFlags & InputTarget::FLAG_SPLIT;
567 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700568 };
569
Jeff Brown9c3cda02010-06-15 01:31:58 -0700570 // A command entry captures state and behavior for an action to be performed in the
571 // dispatch loop after the initial processing has taken place. It is essentially
572 // a kind of continuation used to postpone sensitive policy interactions to a point
573 // in the dispatch loop where it is safe to release the lock (generally after finishing
574 // the critical parts of the dispatch cycle).
575 //
576 // The special thing about commands is that they can voluntarily release and reacquire
577 // the dispatcher lock at will. Initially when the command starts running, the
578 // dispatcher lock is held. However, if the command needs to call into the policy to
579 // do some work, it can release the lock, do the work, then reacquire the lock again
580 // before returning.
581 //
582 // This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch
583 // never calls into the policy while holding its lock.
584 //
585 // Commands are implicitly 'LockedInterruptible'.
586 struct CommandEntry;
587 typedef void (InputDispatcher::*Command)(CommandEntry* commandEntry);
588
Jeff Brown7fbdc842010-06-17 20:52:56 -0700589 class Connection;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700590 struct CommandEntry : Link<CommandEntry> {
591 CommandEntry();
592 ~CommandEntry();
593
594 Command command;
595
596 // parameters for the command (usage varies by command)
Jeff Brown7fbdc842010-06-17 20:52:56 -0700597 sp<Connection> connection;
Jeff Brownb88102f2010-09-08 11:49:43 -0700598 nsecs_t eventTime;
599 KeyEntry* keyEntry;
600 sp<InputChannel> inputChannel;
601 sp<InputApplicationHandle> inputApplicationHandle;
Jeff Brownb88102f2010-09-08 11:49:43 -0700602 int32_t userActivityEventType;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700603 };
604
605 // Generic queue implementation.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700606 template <typename T>
607 struct Queue {
Jeff Brownb88102f2010-09-08 11:49:43 -0700608 T headSentinel;
609 T tailSentinel;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700610
611 inline Queue() {
Jeff Brownb88102f2010-09-08 11:49:43 -0700612 headSentinel.prev = NULL;
613 headSentinel.next = & tailSentinel;
614 tailSentinel.prev = & headSentinel;
615 tailSentinel.next = NULL;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700616 }
617
Jeff Brownb88102f2010-09-08 11:49:43 -0700618 inline bool isEmpty() const {
619 return headSentinel.next == & tailSentinel;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700620 }
621
622 inline void enqueueAtTail(T* entry) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700623 T* last = tailSentinel.prev;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700624 last->next = entry;
625 entry->prev = last;
Jeff Brownb88102f2010-09-08 11:49:43 -0700626 entry->next = & tailSentinel;
627 tailSentinel.prev = entry;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700628 }
629
630 inline void enqueueAtHead(T* entry) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700631 T* first = headSentinel.next;
632 headSentinel.next = entry;
633 entry->prev = & headSentinel;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700634 entry->next = first;
635 first->prev = entry;
636 }
637
638 inline void dequeue(T* entry) {
639 entry->prev->next = entry->next;
640 entry->next->prev = entry->prev;
641 }
642
643 inline T* dequeueAtHead() {
Jeff Brownb88102f2010-09-08 11:49:43 -0700644 T* first = headSentinel.next;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700645 dequeue(first);
646 return first;
647 }
Jeff Brown519e0242010-09-15 15:18:56 -0700648
649 uint32_t count() const;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700650 };
651
652 /* Allocates queue entries and performs reference counting as needed. */
653 class Allocator {
654 public:
655 Allocator();
656
Jeff Brown01ce2e92010-09-26 22:20:12 -0700657 InjectionState* obtainInjectionState(int32_t injectorPid, int32_t injectorUid);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700658 ConfigurationChangedEntry* obtainConfigurationChangedEntry(nsecs_t eventTime);
659 KeyEntry* obtainKeyEntry(nsecs_t eventTime,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700660 int32_t deviceId, int32_t source, uint32_t policyFlags, int32_t action,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700661 int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState,
662 int32_t repeatCount, nsecs_t downTime);
663 MotionEntry* obtainMotionEntry(nsecs_t eventTime,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700664 int32_t deviceId, int32_t source, uint32_t policyFlags, int32_t action,
Jeff Brown85a31762010-09-01 17:01:00 -0700665 int32_t flags, int32_t metaState, int32_t edgeFlags,
666 float xPrecision, float yPrecision,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700667 nsecs_t downTime, uint32_t pointerCount,
668 const int32_t* pointerIds, const PointerCoords* pointerCoords);
Jeff Brownb88102f2010-09-08 11:49:43 -0700669 DispatchEntry* obtainDispatchEntry(EventEntry* eventEntry,
Jeff Brown519e0242010-09-15 15:18:56 -0700670 int32_t targetFlags, float xOffset, float yOffset);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700671 CommandEntry* obtainCommandEntry(Command command);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700672
Jeff Brown01ce2e92010-09-26 22:20:12 -0700673 void releaseInjectionState(InjectionState* injectionState);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700674 void releaseEventEntry(EventEntry* entry);
675 void releaseConfigurationChangedEntry(ConfigurationChangedEntry* entry);
676 void releaseKeyEntry(KeyEntry* entry);
677 void releaseMotionEntry(MotionEntry* entry);
678 void releaseDispatchEntry(DispatchEntry* entry);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700679 void releaseCommandEntry(CommandEntry* entry);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700680
Jeff Brown01ce2e92010-09-26 22:20:12 -0700681 void recycleKeyEntry(KeyEntry* entry);
682
Jeff Brown46b9ac02010-04-22 18:58:52 -0700683 void appendMotionSample(MotionEntry* motionEntry,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700684 nsecs_t eventTime, const PointerCoords* pointerCoords);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700685
686 private:
Jeff Brown01ce2e92010-09-26 22:20:12 -0700687 Pool<InjectionState> mInjectionStatePool;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700688 Pool<ConfigurationChangedEntry> mConfigurationChangeEntryPool;
689 Pool<KeyEntry> mKeyEntryPool;
690 Pool<MotionEntry> mMotionEntryPool;
691 Pool<MotionSample> mMotionSamplePool;
692 Pool<DispatchEntry> mDispatchEntryPool;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700693 Pool<CommandEntry> mCommandEntryPool;
Jeff Brown7fbdc842010-06-17 20:52:56 -0700694
Jeff Brownb6997262010-10-08 22:31:17 -0700695 void initializeEventEntry(EventEntry* entry, int32_t type, nsecs_t eventTime,
696 uint32_t policyFlags);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700697 void releaseEventEntryInjectionState(EventEntry* entry);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700698 };
699
Jeff Brownb88102f2010-09-08 11:49:43 -0700700 /* Tracks dispatched key and motion event state so that cancelation events can be
701 * synthesized when events are dropped. */
702 class InputState {
703 public:
704 // Specifies whether a given event will violate input state consistency.
705 enum Consistency {
706 // The event is consistent with the current input state.
707 CONSISTENT,
708 // The event is inconsistent with the current input state but applications
709 // will tolerate it. eg. Down followed by another down.
710 TOLERABLE,
711 // The event is inconsistent with the current input state and will probably
712 // cause applications to crash. eg. Up without prior down, move with
713 // unexpected number of pointers.
714 BROKEN
715 };
716
Jeff Brownb6997262010-10-08 22:31:17 -0700717 // Specifies the sources to cancel.
718 enum CancelationOptions {
719 CANCEL_ALL_EVENTS = 0,
720 CANCEL_POINTER_EVENTS = 1,
721 CANCEL_NON_POINTER_EVENTS = 2,
722 };
723
Jeff Brownb88102f2010-09-08 11:49:43 -0700724 InputState();
725 ~InputState();
726
727 // Returns true if there is no state to be canceled.
728 bool isNeutral() const;
729
Jeff Brownb88102f2010-09-08 11:49:43 -0700730 // Records tracking information for an event that has just been published.
731 // Returns whether the event is consistent with the current input state.
732 Consistency trackEvent(const EventEntry* entry);
733
734 // Records tracking information for a key event that has just been published.
735 // Returns whether the event is consistent with the current input state.
736 Consistency trackKey(const KeyEntry* entry);
737
738 // Records tracking information for a motion event that has just been published.
739 // Returns whether the event is consistent with the current input state.
740 Consistency trackMotion(const MotionEntry* entry);
741
Jeff Brownb6997262010-10-08 22:31:17 -0700742 // Synthesizes cancelation events for the current state and resets the tracked state.
743 void synthesizeCancelationEvents(nsecs_t currentTime, Allocator* allocator,
744 Vector<EventEntry*>& outEvents, CancelationOptions options);
Jeff Brownb88102f2010-09-08 11:49:43 -0700745
746 // Clears the current state.
747 void clear();
748
749 private:
Jeff Brownb88102f2010-09-08 11:49:43 -0700750 struct KeyMemento {
751 int32_t deviceId;
752 int32_t source;
753 int32_t keyCode;
754 int32_t scanCode;
755 nsecs_t downTime;
756 };
757
758 struct MotionMemento {
759 int32_t deviceId;
760 int32_t source;
761 float xPrecision;
762 float yPrecision;
763 nsecs_t downTime;
764 uint32_t pointerCount;
765 int32_t pointerIds[MAX_POINTERS];
766 PointerCoords pointerCoords[MAX_POINTERS];
767
768 void setPointers(const MotionEntry* entry);
769 };
770
771 Vector<KeyMemento> mKeyMementos;
772 Vector<MotionMemento> mMotionMementos;
Jeff Brownb6997262010-10-08 22:31:17 -0700773
774 static bool shouldCancelEvent(int32_t eventSource, CancelationOptions options);
Jeff Brownb88102f2010-09-08 11:49:43 -0700775 };
776
Jeff Brown46b9ac02010-04-22 18:58:52 -0700777 /* Manages the dispatch state associated with a single input channel. */
778 class Connection : public RefBase {
779 protected:
780 virtual ~Connection();
781
782 public:
783 enum Status {
784 // Everything is peachy.
785 STATUS_NORMAL,
786 // An unrecoverable communication error has occurred.
787 STATUS_BROKEN,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700788 // The input channel has been unregistered.
789 STATUS_ZOMBIE
790 };
791
792 Status status;
793 sp<InputChannel> inputChannel;
794 InputPublisher inputPublisher;
Jeff Brownb88102f2010-09-08 11:49:43 -0700795 InputState inputState;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700796 Queue<DispatchEntry> outboundQueue;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700797
798 nsecs_t lastEventTime; // the time when the event was originally captured
799 nsecs_t lastDispatchTime; // the time when the last event was dispatched
Jeff Brown46b9ac02010-04-22 18:58:52 -0700800
801 explicit Connection(const sp<InputChannel>& inputChannel);
802
Jeff Brown9c3cda02010-06-15 01:31:58 -0700803 inline const char* getInputChannelName() const { return inputChannel->getName().string(); }
804
805 const char* getStatusLabel() const;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700806
807 // Finds a DispatchEntry in the outbound queue associated with the specified event.
808 // Returns NULL if not found.
809 DispatchEntry* findQueuedDispatchEntryForEvent(const EventEntry* eventEntry) const;
810
Jeff Brown46b9ac02010-04-22 18:58:52 -0700811 // Gets the time since the current event was originally obtained from the input driver.
Jeff Brownb88102f2010-09-08 11:49:43 -0700812 inline double getEventLatencyMillis(nsecs_t currentTime) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700813 return (currentTime - lastEventTime) / 1000000.0;
814 }
815
816 // Gets the time since the current event entered the outbound dispatch queue.
Jeff Brownb88102f2010-09-08 11:49:43 -0700817 inline double getDispatchLatencyMillis(nsecs_t currentTime) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700818 return (currentTime - lastDispatchTime) / 1000000.0;
819 }
820
Jeff Brown46b9ac02010-04-22 18:58:52 -0700821 status_t initialize();
822 };
823
Jeff Brownb6997262010-10-08 22:31:17 -0700824 enum DropReason {
825 DROP_REASON_NOT_DROPPED = 0,
826 DROP_REASON_POLICY = 1,
827 DROP_REASON_APP_SWITCH = 2,
828 DROP_REASON_DISABLED = 3,
829 };
830
Jeff Brown9c3cda02010-06-15 01:31:58 -0700831 sp<InputDispatcherPolicyInterface> mPolicy;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700832
833 Mutex mLock;
834
Jeff Brown46b9ac02010-04-22 18:58:52 -0700835 Allocator mAllocator;
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700836 sp<Looper> mLooper;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700837
Jeff Brownb88102f2010-09-08 11:49:43 -0700838 EventEntry* mPendingEvent;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700839 Queue<EventEntry> mInboundQueue;
840 Queue<CommandEntry> mCommandQueue;
841
Jeff Brownb88102f2010-09-08 11:49:43 -0700842 Vector<EventEntry*> mTempCancelationEvents;
843
844 void dispatchOnceInnerLocked(nsecs_t keyRepeatTimeout, nsecs_t keyRepeatDelay,
845 nsecs_t* nextWakeupTime);
846
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700847 // Enqueues an inbound event. Returns true if mLooper->wake() should be called.
Jeff Brownb88102f2010-09-08 11:49:43 -0700848 bool enqueueInboundEventLocked(EventEntry* entry);
849
Jeff Brownb6997262010-10-08 22:31:17 -0700850 // Cleans up input state when dropping an inbound event.
851 void dropInboundEventLocked(EventEntry* entry, DropReason dropReason);
852
Jeff Brownb88102f2010-09-08 11:49:43 -0700853 // App switch latency optimization.
Jeff Brownb6997262010-10-08 22:31:17 -0700854 bool mAppSwitchSawKeyDown;
Jeff Brownb88102f2010-09-08 11:49:43 -0700855 nsecs_t mAppSwitchDueTime;
856
Jeff Brownb6997262010-10-08 22:31:17 -0700857 static bool isAppSwitchKeyCode(int32_t keyCode);
858 bool isAppSwitchKeyEventLocked(KeyEntry* keyEntry);
Jeff Brownb88102f2010-09-08 11:49:43 -0700859 bool isAppSwitchPendingLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700860 void resetPendingAppSwitchLocked(bool handled);
861
Jeff Brown46b9ac02010-04-22 18:58:52 -0700862 // All registered connections mapped by receive pipe file descriptor.
863 KeyedVector<int, sp<Connection> > mConnectionsByReceiveFd;
864
Jeff Brown519e0242010-09-15 15:18:56 -0700865 ssize_t getConnectionIndexLocked(const sp<InputChannel>& inputChannel);
Jeff Brown2cbecea2010-08-17 15:59:26 -0700866
Jeff Brown46b9ac02010-04-22 18:58:52 -0700867 // Active connections are connections that have a non-empty outbound queue.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700868 // We don't use a ref-counted pointer here because we explicitly abort connections
869 // during unregistration which causes the connection's outbound queue to be cleared
870 // and the connection itself to be deactivated.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700871 Vector<Connection*> mActiveConnections;
872
Jeff Brownb88102f2010-09-08 11:49:43 -0700873 // Input channels that will receive a copy of all input events.
874 Vector<sp<InputChannel> > mMonitoringChannels;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700875
Jeff Brownb88102f2010-09-08 11:49:43 -0700876 // Preallocated key event object used for policy inquiries.
877 KeyEvent mReusableKeyEvent;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700878
Jeff Brown7fbdc842010-06-17 20:52:56 -0700879 // Event injection and synchronization.
880 Condition mInjectionResultAvailableCondition;
Jeff Brownb6997262010-10-08 22:31:17 -0700881 bool hasInjectionPermission(int32_t injectorPid, int32_t injectorUid);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700882 void setInjectionResultLocked(EventEntry* entry, int32_t injectionResult);
883
Jeff Brown6ec402b2010-07-28 15:48:59 -0700884 Condition mInjectionSyncFinishedCondition;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700885 void incrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brown519e0242010-09-15 15:18:56 -0700886 void decrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brown6ec402b2010-07-28 15:48:59 -0700887
Jeff Brownae9fc032010-08-18 15:51:08 -0700888 // Throttling state.
889 struct ThrottleState {
890 nsecs_t minTimeBetweenEvents;
891
892 nsecs_t lastEventTime;
893 int32_t lastDeviceId;
894 uint32_t lastSource;
895
896 uint32_t originalSampleCount; // only collected during debugging
897 } mThrottleState;
898
Jeff Brown46b9ac02010-04-22 18:58:52 -0700899 // Key repeat tracking.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700900 struct KeyRepeatState {
901 KeyEntry* lastKeyEntry; // or null if no repeat
902 nsecs_t nextRepeatTime;
903 } mKeyRepeatState;
904
905 void resetKeyRepeatLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700906 KeyEntry* synthesizeKeyRepeatLocked(nsecs_t currentTime, nsecs_t keyRepeatTimeout);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700907
Jeff Brown9c3cda02010-06-15 01:31:58 -0700908 // Deferred command processing.
909 bool runCommandsLockedInterruptible();
910 CommandEntry* postCommandLocked(Command command);
911
Jeff Brownb88102f2010-09-08 11:49:43 -0700912 // Inbound event processing.
913 void drainInboundQueueLocked();
Jeff Brown54a18252010-09-16 14:07:33 -0700914 void releasePendingEventLocked();
915 void releaseInboundEventLocked(EventEntry* entry);
Jeff Brownb6997262010-10-08 22:31:17 -0700916 bool isEventFromTrustedSourceLocked(EventEntry* entry);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700917
Jeff Brownb88102f2010-09-08 11:49:43 -0700918 // Dispatch state.
919 bool mDispatchEnabled;
920 bool mDispatchFrozen;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700921
Jeff Brownb88102f2010-09-08 11:49:43 -0700922 Vector<InputWindow> mWindows;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700923
924 const InputWindow* getWindowLocked(const sp<InputChannel>& inputChannel);
Jeff Brownb88102f2010-09-08 11:49:43 -0700925
926 // Focus tracking for keys, trackball, etc.
Jeff Brown01ce2e92010-09-26 22:20:12 -0700927 const InputWindow* mFocusedWindow;
Jeff Brownb88102f2010-09-08 11:49:43 -0700928
929 // Focus tracking for touch.
Jeff Brown01ce2e92010-09-26 22:20:12 -0700930 struct TouchedWindow {
931 const InputWindow* window;
932 int32_t targetFlags;
933 BitSet32 pointerIds;
934 sp<InputChannel> channel;
Jeff Brownb88102f2010-09-08 11:49:43 -0700935 };
Jeff Brown01ce2e92010-09-26 22:20:12 -0700936 struct TouchState {
937 bool down;
938 bool split;
939 Vector<TouchedWindow> windows;
940
941 TouchState();
942 ~TouchState();
943 void reset();
944 void copyFrom(const TouchState& other);
945 void addOrUpdateWindow(const InputWindow* window, int32_t targetFlags, BitSet32 pointerIds);
946 void removeOutsideTouchWindows();
947 const InputWindow* getFirstForegroundWindow();
948 };
949
950 TouchState mTouchState;
951 TouchState mTempTouchState;
Jeff Brownb88102f2010-09-08 11:49:43 -0700952
953 // Focused application.
954 InputApplication* mFocusedApplication;
955 InputApplication mFocusedApplicationStorage; // preallocated storage for mFocusedApplication
956 void releaseFocusedApplicationLocked();
957
958 // Dispatch inbound events.
959 bool dispatchConfigurationChangedLocked(
960 nsecs_t currentTime, ConfigurationChangedEntry* entry);
961 bool dispatchKeyLocked(
962 nsecs_t currentTime, KeyEntry* entry, nsecs_t keyRepeatTimeout,
Jeff Brown54a18252010-09-16 14:07:33 -0700963 bool dropEvent, nsecs_t* nextWakeupTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700964 bool dispatchMotionLocked(
965 nsecs_t currentTime, MotionEntry* entry,
Jeff Brown54a18252010-09-16 14:07:33 -0700966 bool dropEvent, nsecs_t* nextWakeupTime);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700967 void dispatchEventToCurrentInputTargetsLocked(
968 nsecs_t currentTime, EventEntry* entry, bool resumeWithAppendedMotionSample);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700969
Jeff Brownb88102f2010-09-08 11:49:43 -0700970 void logOutboundKeyDetailsLocked(const char* prefix, const KeyEntry* entry);
971 void logOutboundMotionDetailsLocked(const char* prefix, const MotionEntry* entry);
972
973 // The input targets that were most recently identified for dispatch.
Jeff Brownb88102f2010-09-08 11:49:43 -0700974 bool mCurrentInputTargetsValid; // false while targets are being recomputed
975 Vector<InputTarget> mCurrentInputTargets;
Jeff Brownb88102f2010-09-08 11:49:43 -0700976
977 enum InputTargetWaitCause {
978 INPUT_TARGET_WAIT_CAUSE_NONE,
979 INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY,
980 INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY,
981 };
982
983 InputTargetWaitCause mInputTargetWaitCause;
984 nsecs_t mInputTargetWaitStartTime;
985 nsecs_t mInputTargetWaitTimeoutTime;
986 bool mInputTargetWaitTimeoutExpired;
987
988 // Finding targets for input events.
Jeff Brown54a18252010-09-16 14:07:33 -0700989 void resetTargetsLocked();
Jeff Brown01ce2e92010-09-26 22:20:12 -0700990 void commitTargetsLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700991 int32_t handleTargetsNotReadyLocked(nsecs_t currentTime, const EventEntry* entry,
992 const InputApplication* application, const InputWindow* window,
993 nsecs_t* nextWakeupTime);
Jeff Brown519e0242010-09-15 15:18:56 -0700994 void resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout,
995 const sp<InputChannel>& inputChannel);
996 nsecs_t getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700997 void resetANRTimeoutsLocked();
998
Jeff Brown01ce2e92010-09-26 22:20:12 -0700999 int32_t findFocusedWindowTargetsLocked(nsecs_t currentTime, const EventEntry* entry,
1000 nsecs_t* nextWakeupTime);
1001 int32_t findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry* entry,
1002 nsecs_t* nextWakeupTime);
Jeff Brownb88102f2010-09-08 11:49:43 -07001003
Jeff Brown01ce2e92010-09-26 22:20:12 -07001004 void addWindowTargetLocked(const InputWindow* window, int32_t targetFlags,
1005 BitSet32 pointerIds);
Jeff Brownb88102f2010-09-08 11:49:43 -07001006 void addMonitoringTargetsLocked();
Jeff Brown01ce2e92010-09-26 22:20:12 -07001007 bool shouldPokeUserActivityForCurrentInputTargetsLocked();
1008 void pokeUserActivityLocked(nsecs_t eventTime, int32_t eventType);
1009 bool checkInjectionPermission(const InputWindow* window, const InjectionState* injectionState);
Jeff Brown19dfc832010-10-05 12:26:23 -07001010 bool isWindowObscuredAtPointLocked(const InputWindow* window, int32_t x, int32_t y) const;
Jeff Brown519e0242010-09-15 15:18:56 -07001011 bool isWindowFinishedWithPreviousInputLocked(const InputWindow* window);
Jeff Brown519e0242010-09-15 15:18:56 -07001012 String8 getApplicationWindowLabelLocked(const InputApplication* application,
1013 const InputWindow* window);
Jeff Brownb88102f2010-09-08 11:49:43 -07001014
Jeff Brown46b9ac02010-04-22 18:58:52 -07001015 // Manage the dispatch cycle for a single connection.
Jeff Brown7fbdc842010-06-17 20:52:56 -07001016 // These methods are deliberately not Interruptible because doing all of the work
1017 // with the mutex held makes it easier to ensure that connection invariants are maintained.
1018 // If needed, the methods post commands to run later once the critical bits are done.
1019 void prepareDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001020 EventEntry* eventEntry, const InputTarget* inputTarget,
1021 bool resumeWithAppendedMotionSample);
Jeff Brown519e0242010-09-15 15:18:56 -07001022 void startDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001023 void finishDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brownb88102f2010-09-08 11:49:43 -07001024 void startNextDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brownb6997262010-10-08 22:31:17 -07001025 void abortBrokenDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown519e0242010-09-15 15:18:56 -07001026 void drainOutboundQueueLocked(Connection* connection);
Jeff Brown4fe6c3e2010-09-13 23:17:30 -07001027 static int handleReceiveCallback(int receiveFd, int events, void* data);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001028
Jeff Brownb6997262010-10-08 22:31:17 -07001029 void synthesizeCancelationEventsForAllConnectionsLocked(
1030 InputState::CancelationOptions options, const char* reason);
1031 void synthesizeCancelationEventsForInputChannelLocked(const sp<InputChannel>& channel,
1032 InputState::CancelationOptions options, const char* reason);
1033 void synthesizeCancelationEventsForConnectionLocked(const sp<Connection>& connection,
1034 InputState::CancelationOptions options, const char* reason);
1035
Jeff Brown01ce2e92010-09-26 22:20:12 -07001036 // Splitting motion events across windows.
1037 MotionEntry* splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds);
1038
Jeff Brownb88102f2010-09-08 11:49:43 -07001039 // Dump state.
1040 void dumpDispatchStateLocked(String8& dump);
1041 void logDispatchStateLocked();
1042
Jeff Brown46b9ac02010-04-22 18:58:52 -07001043 // Add or remove a connection to the mActiveConnections vector.
1044 void activateConnectionLocked(Connection* connection);
1045 void deactivateConnectionLocked(Connection* connection);
1046
1047 // Interesting events that we might like to log or tell the framework about.
Jeff Brown9c3cda02010-06-15 01:31:58 -07001048 void onDispatchCycleStartedLocked(
Jeff Brown7fbdc842010-06-17 20:52:56 -07001049 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001050 void onDispatchCycleFinishedLocked(
Jeff Brown7fbdc842010-06-17 20:52:56 -07001051 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001052 void onDispatchCycleBrokenLocked(
Jeff Brown7fbdc842010-06-17 20:52:56 -07001053 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown519e0242010-09-15 15:18:56 -07001054 void onANRLocked(
1055 nsecs_t currentTime, const InputApplication* application, const InputWindow* window,
1056 nsecs_t eventTime, nsecs_t waitStartTime);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001057
Jeff Brown7fbdc842010-06-17 20:52:56 -07001058 // Outbound policy interactions.
Jeff Brownb88102f2010-09-08 11:49:43 -07001059 void doNotifyConfigurationChangedInterruptible(CommandEntry* commandEntry);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001060 void doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown519e0242010-09-15 15:18:56 -07001061 void doNotifyANRLockedInterruptible(CommandEntry* commandEntry);
Jeff Brownb88102f2010-09-08 11:49:43 -07001062 void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry);
1063 void doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown519e0242010-09-15 15:18:56 -07001064
1065 // Statistics gathering.
1066 void updateDispatchStatisticsLocked(nsecs_t currentTime, const EventEntry* entry,
1067 int32_t injectionResult, nsecs_t timeSpentWaitingForApplication);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001068};
1069
1070/* Enqueues and dispatches input events, endlessly. */
1071class InputDispatcherThread : public Thread {
1072public:
1073 explicit InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher);
1074 ~InputDispatcherThread();
1075
1076private:
1077 virtual bool threadLoop();
1078
1079 sp<InputDispatcherInterface> mDispatcher;
1080};
1081
1082} // namespace android
1083
Jeff Brownb88102f2010-09-08 11:49:43 -07001084#endif // _UI_INPUT_DISPATCHER_H