blob: d0812deb9876f9ba1684b405e45d5cbfd469dd84 [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
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700112 // The subset of pointer ids to include in motion events dispatched to this input target
113 // if FLAG_SPLIT is set.
114 BitSet32 pointerIds;
Jeff Brown54bc2812010-06-15 01:31:58 -0700115};
116
Jeff Brown51d45a72010-06-17 20:52:56 -0700117
Jeff Brown54bc2812010-06-15 01:31:58 -0700118/*
Jeff Browna665ca82010-09-08 11:49:43 -0700119 * An input window describes the bounds of a window that can receive input.
120 */
121struct InputWindow {
122 // Window flags from WindowManager.LayoutParams
123 enum {
124 FLAG_ALLOW_LOCK_WHILE_SCREEN_ON = 0x00000001,
125 FLAG_DIM_BEHIND = 0x00000002,
126 FLAG_BLUR_BEHIND = 0x00000004,
127 FLAG_NOT_FOCUSABLE = 0x00000008,
128 FLAG_NOT_TOUCHABLE = 0x00000010,
129 FLAG_NOT_TOUCH_MODAL = 0x00000020,
130 FLAG_TOUCHABLE_WHEN_WAKING = 0x00000040,
131 FLAG_KEEP_SCREEN_ON = 0x00000080,
132 FLAG_LAYOUT_IN_SCREEN = 0x00000100,
133 FLAG_LAYOUT_NO_LIMITS = 0x00000200,
134 FLAG_FULLSCREEN = 0x00000400,
135 FLAG_FORCE_NOT_FULLSCREEN = 0x00000800,
136 FLAG_DITHER = 0x00001000,
137 FLAG_SECURE = 0x00002000,
138 FLAG_SCALED = 0x00004000,
139 FLAG_IGNORE_CHEEK_PRESSES = 0x00008000,
140 FLAG_LAYOUT_INSET_DECOR = 0x00010000,
141 FLAG_ALT_FOCUSABLE_IM = 0x00020000,
142 FLAG_WATCH_OUTSIDE_TOUCH = 0x00040000,
143 FLAG_SHOW_WHEN_LOCKED = 0x00080000,
144 FLAG_SHOW_WALLPAPER = 0x00100000,
145 FLAG_TURN_SCREEN_ON = 0x00200000,
146 FLAG_DISMISS_KEYGUARD = 0x00400000,
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700147 FLAG_SPLIT_TOUCH = 0x00800000,
Jeff Browna665ca82010-09-08 11:49:43 -0700148 FLAG_KEEP_SURFACE_WHILE_ANIMATING = 0x10000000,
149 FLAG_COMPATIBLE_WINDOW = 0x20000000,
150 FLAG_SYSTEM_ERROR = 0x40000000,
151 };
152
153 // Window types from WindowManager.LayoutParams
154 enum {
155 FIRST_APPLICATION_WINDOW = 1,
156 TYPE_BASE_APPLICATION = 1,
157 TYPE_APPLICATION = 2,
158 TYPE_APPLICATION_STARTING = 3,
159 LAST_APPLICATION_WINDOW = 99,
160 FIRST_SUB_WINDOW = 1000,
161 TYPE_APPLICATION_PANEL = FIRST_SUB_WINDOW,
162 TYPE_APPLICATION_MEDIA = FIRST_SUB_WINDOW+1,
163 TYPE_APPLICATION_SUB_PANEL = FIRST_SUB_WINDOW+2,
164 TYPE_APPLICATION_ATTACHED_DIALOG = FIRST_SUB_WINDOW+3,
165 TYPE_APPLICATION_MEDIA_OVERLAY = FIRST_SUB_WINDOW+4,
166 LAST_SUB_WINDOW = 1999,
167 FIRST_SYSTEM_WINDOW = 2000,
168 TYPE_STATUS_BAR = FIRST_SYSTEM_WINDOW,
169 TYPE_SEARCH_BAR = FIRST_SYSTEM_WINDOW+1,
170 TYPE_PHONE = FIRST_SYSTEM_WINDOW+2,
171 TYPE_SYSTEM_ALERT = FIRST_SYSTEM_WINDOW+3,
172 TYPE_KEYGUARD = FIRST_SYSTEM_WINDOW+4,
173 TYPE_TOAST = FIRST_SYSTEM_WINDOW+5,
174 TYPE_SYSTEM_OVERLAY = FIRST_SYSTEM_WINDOW+6,
175 TYPE_PRIORITY_PHONE = FIRST_SYSTEM_WINDOW+7,
176 TYPE_SYSTEM_DIALOG = FIRST_SYSTEM_WINDOW+8,
177 TYPE_KEYGUARD_DIALOG = FIRST_SYSTEM_WINDOW+9,
178 TYPE_SYSTEM_ERROR = FIRST_SYSTEM_WINDOW+10,
179 TYPE_INPUT_METHOD = FIRST_SYSTEM_WINDOW+11,
180 TYPE_INPUT_METHOD_DIALOG= FIRST_SYSTEM_WINDOW+12,
181 TYPE_WALLPAPER = FIRST_SYSTEM_WINDOW+13,
Joe Onoratoad1894d2010-11-24 10:26:50 -0800182 TYPE_STATUS_BAR_SUB_PANEL = FIRST_SYSTEM_WINDOW+14,
Jeff Browne68d9e02010-10-15 00:54:27 -0700183 TYPE_SECURE_SYSTEM_OVERLAY = FIRST_SYSTEM_WINDOW+15,
Joe Onoratoad1894d2010-11-24 10:26:50 -0800184 TYPE_DRAG = FIRST_SYSTEM_WINDOW+16,
185 TYPE_STATUS_BAR_PANEL = FIRST_SYSTEM_WINDOW+17,
Jeff Browna665ca82010-09-08 11:49:43 -0700186 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 Browne33a9ec2010-11-10 16:53:45 -0800224
225 bool supportsSplitTouch() const;
Jeff Browna665ca82010-09-08 11:49:43 -0700226};
227
228
229/*
230 * A private handle type used by the input manager to track the window.
231 */
232class InputApplicationHandle : public RefBase {
233protected:
234 InputApplicationHandle() { }
235 virtual ~InputApplicationHandle() { }
236};
237
238
239/*
240 * An input application describes properties of an application that can receive input.
241 */
242struct InputApplication {
243 String8 name;
244 nsecs_t dispatchingTimeout;
245 sp<InputApplicationHandle> handle;
246};
247
248
249/*
Jeff Brown54bc2812010-06-15 01:31:58 -0700250 * Input dispatcher policy interface.
251 *
252 * The input reader policy is used by the input reader to interact with the Window Manager
253 * and other system components.
254 *
255 * The actual implementation is partially supported by callbacks into the DVM
256 * via JNI. This interface is also mocked in the unit tests.
257 */
258class InputDispatcherPolicyInterface : public virtual RefBase {
259protected:
260 InputDispatcherPolicyInterface() { }
261 virtual ~InputDispatcherPolicyInterface() { }
262
263public:
264 /* Notifies the system that a configuration change has occurred. */
265 virtual void notifyConfigurationChanged(nsecs_t when) = 0;
266
Jeff Browna665ca82010-09-08 11:49:43 -0700267 /* Notifies the system that an application is not responding.
268 * Returns a new timeout to continue waiting, or 0 to abort dispatch. */
Jeff Brown53a415e2010-09-15 15:18:56 -0700269 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
270 const sp<InputChannel>& inputChannel) = 0;
Jeff Browna665ca82010-09-08 11:49:43 -0700271
Jeff Brown54bc2812010-06-15 01:31:58 -0700272 /* Notifies the system that an input channel is unrecoverably broken. */
273 virtual void notifyInputChannelBroken(const sp<InputChannel>& inputChannel) = 0;
274
Jeff Brown61ce3982010-09-07 10:44:57 -0700275 /* Gets the key repeat initial timeout or -1 if automatic key repeating is disabled. */
Jeff Brown54bc2812010-06-15 01:31:58 -0700276 virtual nsecs_t getKeyRepeatTimeout() = 0;
277
Jeff Brown61ce3982010-09-07 10:44:57 -0700278 /* Gets the key repeat inter-key delay. */
279 virtual nsecs_t getKeyRepeatDelay() = 0;
280
Jeff Brown542412c2010-08-18 15:51:08 -0700281 /* Gets the maximum suggested event delivery rate per second.
282 * This value is used to throttle motion event movement actions on a per-device
283 * basis. It is not intended to be a hard limit.
284 */
285 virtual int32_t getMaxEventsPerSecond() = 0;
Jeff Browna665ca82010-09-08 11:49:43 -0700286
Jeff Brown90f0cee2010-10-08 22:31:17 -0700287 /* Intercepts a key event immediately before queueing it.
288 * The policy can use this method as an opportunity to perform power management functions
289 * and early event preprocessing such as updating policy flags.
290 *
291 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
292 * should be dispatched to applications.
293 */
294 virtual void interceptKeyBeforeQueueing(nsecs_t when, int32_t deviceId,
295 int32_t action, int32_t& flags, int32_t keyCode, int32_t scanCode,
296 uint32_t& policyFlags) = 0;
297
298 /* Intercepts a generic touch, trackball or other event before queueing it.
299 * The policy can use this method as an opportunity to perform power management functions
300 * and early event preprocessing such as updating policy flags.
301 *
302 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
303 * should be dispatched to applications.
304 */
305 virtual void interceptGenericBeforeQueueing(nsecs_t when, uint32_t& policyFlags) = 0;
306
Jeff Browna665ca82010-09-08 11:49:43 -0700307 /* Allows the policy a chance to intercept a key before dispatching. */
308 virtual bool interceptKeyBeforeDispatching(const sp<InputChannel>& inputChannel,
309 const KeyEvent* keyEvent, uint32_t policyFlags) = 0;
310
Jeff Brown81499912010-11-05 15:02:16 -0700311 /* Allows the policy a chance to perform default processing for an unhandled key. */
312 virtual bool dispatchUnhandledKey(const sp<InputChannel>& inputChannel,
313 const KeyEvent* keyEvent, uint32_t policyFlags) = 0;
314
Jeff Brown90f0cee2010-10-08 22:31:17 -0700315 /* Notifies the policy about switch events.
316 */
317 virtual void notifySwitch(nsecs_t when,
318 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) = 0;
319
Jeff Browna665ca82010-09-08 11:49:43 -0700320 /* Poke user activity for an event dispatched to a window. */
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700321 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType) = 0;
Jeff Browna665ca82010-09-08 11:49:43 -0700322
323 /* Checks whether a given application pid/uid has permission to inject input events
324 * into other applications.
325 *
326 * This method is special in that its implementation promises to be non-reentrant and
327 * is safe to call while holding other locks. (Most other methods make no such guarantees!)
328 */
329 virtual bool checkInjectEventsPermissionNonReentrant(
330 int32_t injectorPid, int32_t injectorUid) = 0;
Jeff Brown54bc2812010-06-15 01:31:58 -0700331};
332
333
Jeff Browne839a582010-04-22 18:58:52 -0700334/* Notifies the system about input events generated by the input reader.
335 * The dispatcher is expected to be mostly asynchronous. */
336class InputDispatcherInterface : public virtual RefBase {
337protected:
338 InputDispatcherInterface() { }
339 virtual ~InputDispatcherInterface() { }
340
341public:
Jeff Browna665ca82010-09-08 11:49:43 -0700342 /* Dumps the state of the input dispatcher.
343 *
344 * This method may be called on any thread (usually by the input manager). */
345 virtual void dump(String8& dump) = 0;
346
Jeff Browne839a582010-04-22 18:58:52 -0700347 /* Runs a single iteration of the dispatch loop.
348 * Nominally processes one queued event, a timeout, or a response from an input consumer.
349 *
350 * This method should only be called on the input dispatcher thread.
351 */
352 virtual void dispatchOnce() = 0;
353
354 /* Notifies the dispatcher about new events.
Jeff Browne839a582010-04-22 18:58:52 -0700355 *
356 * These methods should only be called on the input reader thread.
357 */
Jeff Brown54bc2812010-06-15 01:31:58 -0700358 virtual void notifyConfigurationChanged(nsecs_t eventTime) = 0;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700359 virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Browne839a582010-04-22 18:58:52 -0700360 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode,
361 int32_t scanCode, int32_t metaState, nsecs_t downTime) = 0;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700362 virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Brownaf30ff62010-09-01 17:01:00 -0700363 uint32_t policyFlags, int32_t action, int32_t flags,
364 int32_t metaState, int32_t edgeFlags,
Jeff Browne839a582010-04-22 18:58:52 -0700365 uint32_t pointerCount, const int32_t* pointerIds, const PointerCoords* pointerCoords,
366 float xPrecision, float yPrecision, nsecs_t downTime) = 0;
Jeff Brown90f0cee2010-10-08 22:31:17 -0700367 virtual void notifySwitch(nsecs_t when,
368 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) = 0;
Jeff Browne839a582010-04-22 18:58:52 -0700369
Jeff Brown51d45a72010-06-17 20:52:56 -0700370 /* Injects an input event and optionally waits for sync.
Jeff Brownf67c53e2010-07-28 15:48:59 -0700371 * The synchronization mode determines whether the method blocks while waiting for
372 * input injection to proceed.
Jeff Brown51d45a72010-06-17 20:52:56 -0700373 * Returns one of the INPUT_EVENT_INJECTION_XXX constants.
374 *
375 * This method may be called on any thread (usually by the input manager).
376 */
377 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brownf67c53e2010-07-28 15:48:59 -0700378 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis) = 0;
Jeff Brown51d45a72010-06-17 20:52:56 -0700379
Jeff Browna665ca82010-09-08 11:49:43 -0700380 /* Sets the list of input windows.
381 *
382 * This method may be called on any thread (usually by the input manager).
383 */
384 virtual void setInputWindows(const Vector<InputWindow>& inputWindows) = 0;
385
386 /* Sets the focused application.
387 *
388 * This method may be called on any thread (usually by the input manager).
389 */
390 virtual void setFocusedApplication(const InputApplication* inputApplication) = 0;
391
392 /* Sets the input dispatching mode.
393 *
394 * This method may be called on any thread (usually by the input manager).
395 */
396 virtual void setInputDispatchMode(bool enabled, bool frozen) = 0;
397
Jeff Brown744c5592010-09-27 14:52:15 -0700398 /* Transfers touch focus from the window associated with one channel to the
399 * window associated with the other channel.
400 *
401 * Returns true on success. False if the window did not actually have touch focus.
402 */
403 virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel,
404 const sp<InputChannel>& toChannel) = 0;
405
Jeff Browne839a582010-04-22 18:58:52 -0700406 /* Registers or unregister input channels that may be used as targets for input events.
Jeff Browna665ca82010-09-08 11:49:43 -0700407 * If monitor is true, the channel will receive a copy of all input events.
Jeff Browne839a582010-04-22 18:58:52 -0700408 *
409 * These methods may be called on any thread (usually by the input manager).
410 */
Jeff Browna665ca82010-09-08 11:49:43 -0700411 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel, bool monitor) = 0;
Jeff Browne839a582010-04-22 18:58:52 -0700412 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) = 0;
413};
414
Jeff Brown54bc2812010-06-15 01:31:58 -0700415/* Dispatches events to input targets. Some functions of the input dispatcher, such as
416 * identifying input targets, are controlled by a separate policy object.
417 *
418 * IMPORTANT INVARIANT:
419 * Because the policy can potentially block or cause re-entrance into the input dispatcher,
420 * the input dispatcher never calls into the policy while holding its internal locks.
421 * The implementation is also carefully designed to recover from scenarios such as an
422 * input channel becoming unregistered while identifying input targets or processing timeouts.
423 *
424 * Methods marked 'Locked' must be called with the lock acquired.
425 *
426 * Methods marked 'LockedInterruptible' must be called with the lock acquired but
427 * may during the course of their execution release the lock, call into the policy, and
428 * then reacquire the lock. The caller is responsible for recovering gracefully.
429 *
430 * A 'LockedInterruptible' method may called a 'Locked' method, but NOT vice-versa.
431 */
Jeff Browne839a582010-04-22 18:58:52 -0700432class InputDispatcher : public InputDispatcherInterface {
433protected:
434 virtual ~InputDispatcher();
435
436public:
Jeff Brown54bc2812010-06-15 01:31:58 -0700437 explicit InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy);
Jeff Browne839a582010-04-22 18:58:52 -0700438
Jeff Browna665ca82010-09-08 11:49:43 -0700439 virtual void dump(String8& dump);
440
Jeff Browne839a582010-04-22 18:58:52 -0700441 virtual void dispatchOnce();
442
Jeff Brown54bc2812010-06-15 01:31:58 -0700443 virtual void notifyConfigurationChanged(nsecs_t eventTime);
Jeff Brown5c1ed842010-07-14 18:48:53 -0700444 virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Browne839a582010-04-22 18:58:52 -0700445 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode,
446 int32_t scanCode, int32_t metaState, nsecs_t downTime);
Jeff Brown5c1ed842010-07-14 18:48:53 -0700447 virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Brownaf30ff62010-09-01 17:01:00 -0700448 uint32_t policyFlags, int32_t action, int32_t flags,
449 int32_t metaState, int32_t edgeFlags,
Jeff Browne839a582010-04-22 18:58:52 -0700450 uint32_t pointerCount, const int32_t* pointerIds, const PointerCoords* pointerCoords,
451 float xPrecision, float yPrecision, nsecs_t downTime);
Jeff Brown90f0cee2010-10-08 22:31:17 -0700452 virtual void notifySwitch(nsecs_t when,
453 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) ;
Jeff Browne839a582010-04-22 18:58:52 -0700454
Jeff Brown51d45a72010-06-17 20:52:56 -0700455 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brownf67c53e2010-07-28 15:48:59 -0700456 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis);
Jeff Brown51d45a72010-06-17 20:52:56 -0700457
Jeff Browna665ca82010-09-08 11:49:43 -0700458 virtual void setInputWindows(const Vector<InputWindow>& inputWindows);
459 virtual void setFocusedApplication(const InputApplication* inputApplication);
460 virtual void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown50de30a2010-06-22 01:27:15 -0700461
Jeff Brown744c5592010-09-27 14:52:15 -0700462 virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel,
463 const sp<InputChannel>& toChannel);
464
Jeff Browna665ca82010-09-08 11:49:43 -0700465 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel, bool monitor);
Jeff Browne839a582010-04-22 18:58:52 -0700466 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel);
467
468private:
469 template <typename T>
470 struct Link {
471 T* next;
472 T* prev;
473 };
474
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700475 struct InjectionState {
476 mutable int32_t refCount;
477
478 int32_t injectorPid;
479 int32_t injectorUid;
480 int32_t injectionResult; // initially INPUT_EVENT_INJECTION_PENDING
481 bool injectionIsAsync; // set to true if injection is not waiting for the result
482 int32_t pendingForegroundDispatches; // the number of foreground dispatches in progress
483 };
484
Jeff Browne839a582010-04-22 18:58:52 -0700485 struct EventEntry : Link<EventEntry> {
486 enum {
487 TYPE_SENTINEL,
488 TYPE_CONFIGURATION_CHANGED,
489 TYPE_KEY,
490 TYPE_MOTION
491 };
492
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700493 mutable int32_t refCount;
Jeff Browne839a582010-04-22 18:58:52 -0700494 int32_t type;
495 nsecs_t eventTime;
Jeff Brown90f0cee2010-10-08 22:31:17 -0700496 uint32_t policyFlags;
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700497 InjectionState* injectionState;
Jeff Brown51d45a72010-06-17 20:52:56 -0700498
Jeff Brown54bc2812010-06-15 01:31:58 -0700499 bool dispatchInProgress; // initially false, set to true while dispatching
Jeff Brown51d45a72010-06-17 20:52:56 -0700500
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700501 inline bool isInjected() { return injectionState != NULL; }
Jeff Browne839a582010-04-22 18:58:52 -0700502 };
503
504 struct ConfigurationChangedEntry : EventEntry {
Jeff Browne839a582010-04-22 18:58:52 -0700505 };
506
507 struct KeyEntry : EventEntry {
508 int32_t deviceId;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700509 int32_t source;
Jeff Browne839a582010-04-22 18:58:52 -0700510 int32_t action;
511 int32_t flags;
512 int32_t keyCode;
513 int32_t scanCode;
514 int32_t metaState;
515 int32_t repeatCount;
516 nsecs_t downTime;
Jeff Browna665ca82010-09-08 11:49:43 -0700517
518 bool syntheticRepeat; // set to true for synthetic key repeats
519
520 enum InterceptKeyResult {
521 INTERCEPT_KEY_RESULT_UNKNOWN,
522 INTERCEPT_KEY_RESULT_SKIP,
523 INTERCEPT_KEY_RESULT_CONTINUE,
524 };
525 InterceptKeyResult interceptKeyResult; // set based on the interception result
Jeff Browne839a582010-04-22 18:58:52 -0700526 };
527
528 struct MotionSample {
529 MotionSample* next;
530
531 nsecs_t eventTime;
532 PointerCoords pointerCoords[MAX_POINTERS];
533 };
534
535 struct MotionEntry : EventEntry {
536 int32_t deviceId;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700537 int32_t source;
Jeff Browne839a582010-04-22 18:58:52 -0700538 int32_t action;
Jeff Brownaf30ff62010-09-01 17:01:00 -0700539 int32_t flags;
Jeff Browne839a582010-04-22 18:58:52 -0700540 int32_t metaState;
541 int32_t edgeFlags;
542 float xPrecision;
543 float yPrecision;
544 nsecs_t downTime;
545 uint32_t pointerCount;
546 int32_t pointerIds[MAX_POINTERS];
547
548 // Linked list of motion samples associated with this motion event.
549 MotionSample firstSample;
550 MotionSample* lastSample;
Jeff Brown542412c2010-08-18 15:51:08 -0700551
552 uint32_t countSamples() const;
Jeff Browne839a582010-04-22 18:58:52 -0700553 };
554
Jeff Brown54bc2812010-06-15 01:31:58 -0700555 // Tracks the progress of dispatching a particular event to a particular connection.
Jeff Browne839a582010-04-22 18:58:52 -0700556 struct DispatchEntry : Link<DispatchEntry> {
557 EventEntry* eventEntry; // the event to dispatch
558 int32_t targetFlags;
559 float xOffset;
560 float yOffset;
Jeff Browne839a582010-04-22 18:58:52 -0700561
562 // True if dispatch has started.
563 bool inProgress;
564
565 // For motion events:
566 // Pointer to the first motion sample to dispatch in this cycle.
567 // Usually NULL to indicate that the list of motion samples begins at
568 // MotionEntry::firstSample. Otherwise, some samples were dispatched in a previous
569 // cycle and this pointer indicates the location of the first remainining sample
570 // to dispatch during the current cycle.
571 MotionSample* headMotionSample;
572 // Pointer to a motion sample to dispatch in the next cycle if the dispatcher was
573 // unable to send all motion samples during this cycle. On the next cycle,
574 // headMotionSample will be initialized to tailMotionSample and tailMotionSample
575 // will be set to NULL.
576 MotionSample* tailMotionSample;
Jeff Brownf67c53e2010-07-28 15:48:59 -0700577
Jeff Brown53a415e2010-09-15 15:18:56 -0700578 inline bool hasForegroundTarget() const {
579 return targetFlags & InputTarget::FLAG_FOREGROUND;
Jeff Browna665ca82010-09-08 11:49:43 -0700580 }
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700581
582 inline bool isSplit() const {
583 return targetFlags & InputTarget::FLAG_SPLIT;
584 }
Jeff Browne839a582010-04-22 18:58:52 -0700585 };
586
Jeff Brown54bc2812010-06-15 01:31:58 -0700587 // A command entry captures state and behavior for an action to be performed in the
588 // dispatch loop after the initial processing has taken place. It is essentially
589 // a kind of continuation used to postpone sensitive policy interactions to a point
590 // in the dispatch loop where it is safe to release the lock (generally after finishing
591 // the critical parts of the dispatch cycle).
592 //
593 // The special thing about commands is that they can voluntarily release and reacquire
594 // the dispatcher lock at will. Initially when the command starts running, the
595 // dispatcher lock is held. However, if the command needs to call into the policy to
596 // do some work, it can release the lock, do the work, then reacquire the lock again
597 // before returning.
598 //
599 // This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch
600 // never calls into the policy while holding its lock.
601 //
602 // Commands are implicitly 'LockedInterruptible'.
603 struct CommandEntry;
604 typedef void (InputDispatcher::*Command)(CommandEntry* commandEntry);
605
Jeff Brown51d45a72010-06-17 20:52:56 -0700606 class Connection;
Jeff Brown54bc2812010-06-15 01:31:58 -0700607 struct CommandEntry : Link<CommandEntry> {
608 CommandEntry();
609 ~CommandEntry();
610
611 Command command;
612
613 // parameters for the command (usage varies by command)
Jeff Brown51d45a72010-06-17 20:52:56 -0700614 sp<Connection> connection;
Jeff Browna665ca82010-09-08 11:49:43 -0700615 nsecs_t eventTime;
616 KeyEntry* keyEntry;
617 sp<InputChannel> inputChannel;
618 sp<InputApplicationHandle> inputApplicationHandle;
Jeff Browna665ca82010-09-08 11:49:43 -0700619 int32_t userActivityEventType;
Jeff Brown81499912010-11-05 15:02:16 -0700620 bool handled;
Jeff Brown54bc2812010-06-15 01:31:58 -0700621 };
622
623 // Generic queue implementation.
Jeff Browne839a582010-04-22 18:58:52 -0700624 template <typename T>
625 struct Queue {
Jeff Browna665ca82010-09-08 11:49:43 -0700626 T headSentinel;
627 T tailSentinel;
Jeff Browne839a582010-04-22 18:58:52 -0700628
629 inline Queue() {
Jeff Browna665ca82010-09-08 11:49:43 -0700630 headSentinel.prev = NULL;
631 headSentinel.next = & tailSentinel;
632 tailSentinel.prev = & headSentinel;
633 tailSentinel.next = NULL;
Jeff Browne839a582010-04-22 18:58:52 -0700634 }
635
Jeff Browna665ca82010-09-08 11:49:43 -0700636 inline bool isEmpty() const {
637 return headSentinel.next == & tailSentinel;
Jeff Browne839a582010-04-22 18:58:52 -0700638 }
639
640 inline void enqueueAtTail(T* entry) {
Jeff Browna665ca82010-09-08 11:49:43 -0700641 T* last = tailSentinel.prev;
Jeff Browne839a582010-04-22 18:58:52 -0700642 last->next = entry;
643 entry->prev = last;
Jeff Browna665ca82010-09-08 11:49:43 -0700644 entry->next = & tailSentinel;
645 tailSentinel.prev = entry;
Jeff Browne839a582010-04-22 18:58:52 -0700646 }
647
648 inline void enqueueAtHead(T* entry) {
Jeff Browna665ca82010-09-08 11:49:43 -0700649 T* first = headSentinel.next;
650 headSentinel.next = entry;
651 entry->prev = & headSentinel;
Jeff Browne839a582010-04-22 18:58:52 -0700652 entry->next = first;
653 first->prev = entry;
654 }
655
656 inline void dequeue(T* entry) {
657 entry->prev->next = entry->next;
658 entry->next->prev = entry->prev;
659 }
660
661 inline T* dequeueAtHead() {
Jeff Browna665ca82010-09-08 11:49:43 -0700662 T* first = headSentinel.next;
Jeff Browne839a582010-04-22 18:58:52 -0700663 dequeue(first);
664 return first;
665 }
Jeff Brown53a415e2010-09-15 15:18:56 -0700666
667 uint32_t count() const;
Jeff Browne839a582010-04-22 18:58:52 -0700668 };
669
670 /* Allocates queue entries and performs reference counting as needed. */
671 class Allocator {
672 public:
673 Allocator();
674
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700675 InjectionState* obtainInjectionState(int32_t injectorPid, int32_t injectorUid);
Jeff Brown51d45a72010-06-17 20:52:56 -0700676 ConfigurationChangedEntry* obtainConfigurationChangedEntry(nsecs_t eventTime);
677 KeyEntry* obtainKeyEntry(nsecs_t eventTime,
Jeff Brown5c1ed842010-07-14 18:48:53 -0700678 int32_t deviceId, int32_t source, uint32_t policyFlags, int32_t action,
Jeff Brown51d45a72010-06-17 20:52:56 -0700679 int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState,
680 int32_t repeatCount, nsecs_t downTime);
681 MotionEntry* obtainMotionEntry(nsecs_t eventTime,
Jeff Brown5c1ed842010-07-14 18:48:53 -0700682 int32_t deviceId, int32_t source, uint32_t policyFlags, int32_t action,
Jeff Brownaf30ff62010-09-01 17:01:00 -0700683 int32_t flags, int32_t metaState, int32_t edgeFlags,
684 float xPrecision, float yPrecision,
Jeff Brown51d45a72010-06-17 20:52:56 -0700685 nsecs_t downTime, uint32_t pointerCount,
686 const int32_t* pointerIds, const PointerCoords* pointerCoords);
Jeff Browna665ca82010-09-08 11:49:43 -0700687 DispatchEntry* obtainDispatchEntry(EventEntry* eventEntry,
Jeff Brown53a415e2010-09-15 15:18:56 -0700688 int32_t targetFlags, float xOffset, float yOffset);
Jeff Brown54bc2812010-06-15 01:31:58 -0700689 CommandEntry* obtainCommandEntry(Command command);
Jeff Browne839a582010-04-22 18:58:52 -0700690
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700691 void releaseInjectionState(InjectionState* injectionState);
Jeff Browne839a582010-04-22 18:58:52 -0700692 void releaseEventEntry(EventEntry* entry);
693 void releaseConfigurationChangedEntry(ConfigurationChangedEntry* entry);
694 void releaseKeyEntry(KeyEntry* entry);
695 void releaseMotionEntry(MotionEntry* entry);
696 void releaseDispatchEntry(DispatchEntry* entry);
Jeff Brown54bc2812010-06-15 01:31:58 -0700697 void releaseCommandEntry(CommandEntry* entry);
Jeff Browne839a582010-04-22 18:58:52 -0700698
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700699 void recycleKeyEntry(KeyEntry* entry);
700
Jeff Browne839a582010-04-22 18:58:52 -0700701 void appendMotionSample(MotionEntry* motionEntry,
Jeff Brown51d45a72010-06-17 20:52:56 -0700702 nsecs_t eventTime, const PointerCoords* pointerCoords);
Jeff Browne839a582010-04-22 18:58:52 -0700703
704 private:
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700705 Pool<InjectionState> mInjectionStatePool;
Jeff Browne839a582010-04-22 18:58:52 -0700706 Pool<ConfigurationChangedEntry> mConfigurationChangeEntryPool;
707 Pool<KeyEntry> mKeyEntryPool;
708 Pool<MotionEntry> mMotionEntryPool;
709 Pool<MotionSample> mMotionSamplePool;
710 Pool<DispatchEntry> mDispatchEntryPool;
Jeff Brown54bc2812010-06-15 01:31:58 -0700711 Pool<CommandEntry> mCommandEntryPool;
Jeff Brown51d45a72010-06-17 20:52:56 -0700712
Jeff Brown90f0cee2010-10-08 22:31:17 -0700713 void initializeEventEntry(EventEntry* entry, int32_t type, nsecs_t eventTime,
714 uint32_t policyFlags);
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700715 void releaseEventEntryInjectionState(EventEntry* entry);
Jeff Browne839a582010-04-22 18:58:52 -0700716 };
717
Jeff Browna665ca82010-09-08 11:49:43 -0700718 /* Tracks dispatched key and motion event state so that cancelation events can be
719 * synthesized when events are dropped. */
720 class InputState {
721 public:
722 // Specifies whether a given event will violate input state consistency.
723 enum Consistency {
724 // The event is consistent with the current input state.
725 CONSISTENT,
726 // The event is inconsistent with the current input state but applications
727 // will tolerate it. eg. Down followed by another down.
728 TOLERABLE,
729 // The event is inconsistent with the current input state and will probably
730 // cause applications to crash. eg. Up without prior down, move with
731 // unexpected number of pointers.
732 BROKEN
733 };
734
Jeff Brown90f0cee2010-10-08 22:31:17 -0700735 // Specifies the sources to cancel.
736 enum CancelationOptions {
737 CANCEL_ALL_EVENTS = 0,
738 CANCEL_POINTER_EVENTS = 1,
739 CANCEL_NON_POINTER_EVENTS = 2,
740 };
741
Jeff Browna665ca82010-09-08 11:49:43 -0700742 InputState();
743 ~InputState();
744
745 // Returns true if there is no state to be canceled.
746 bool isNeutral() const;
747
Jeff Browna665ca82010-09-08 11:49:43 -0700748 // Records tracking information for an event that has just been published.
749 // Returns whether the event is consistent with the current input state.
750 Consistency trackEvent(const EventEntry* entry);
751
752 // Records tracking information for a key event that has just been published.
753 // Returns whether the event is consistent with the current input state.
754 Consistency trackKey(const KeyEntry* entry);
755
756 // Records tracking information for a motion event that has just been published.
757 // Returns whether the event is consistent with the current input state.
758 Consistency trackMotion(const MotionEntry* entry);
759
Jeff Brown90f0cee2010-10-08 22:31:17 -0700760 // Synthesizes cancelation events for the current state and resets the tracked state.
761 void synthesizeCancelationEvents(nsecs_t currentTime, Allocator* allocator,
762 Vector<EventEntry*>& outEvents, CancelationOptions options);
Jeff Browna665ca82010-09-08 11:49:43 -0700763
764 // Clears the current state.
765 void clear();
766
Jeff Brownb6702e52010-10-11 18:32:20 -0700767 // Copies pointer-related parts of the input state to another instance.
768 void copyPointerStateTo(InputState& other) const;
769
Jeff Browna665ca82010-09-08 11:49:43 -0700770 private:
Jeff Browna665ca82010-09-08 11:49:43 -0700771 struct KeyMemento {
772 int32_t deviceId;
773 int32_t source;
774 int32_t keyCode;
775 int32_t scanCode;
776 nsecs_t downTime;
777 };
778
779 struct MotionMemento {
780 int32_t deviceId;
781 int32_t source;
782 float xPrecision;
783 float yPrecision;
784 nsecs_t downTime;
785 uint32_t pointerCount;
786 int32_t pointerIds[MAX_POINTERS];
787 PointerCoords pointerCoords[MAX_POINTERS];
788
789 void setPointers(const MotionEntry* entry);
790 };
791
792 Vector<KeyMemento> mKeyMementos;
793 Vector<MotionMemento> mMotionMementos;
Jeff Brown90f0cee2010-10-08 22:31:17 -0700794
795 static bool shouldCancelEvent(int32_t eventSource, CancelationOptions options);
Jeff Browna665ca82010-09-08 11:49:43 -0700796 };
797
Jeff Browne839a582010-04-22 18:58:52 -0700798 /* Manages the dispatch state associated with a single input channel. */
799 class Connection : public RefBase {
800 protected:
801 virtual ~Connection();
802
803 public:
804 enum Status {
805 // Everything is peachy.
806 STATUS_NORMAL,
807 // An unrecoverable communication error has occurred.
808 STATUS_BROKEN,
Jeff Browne839a582010-04-22 18:58:52 -0700809 // The input channel has been unregistered.
810 STATUS_ZOMBIE
811 };
812
813 Status status;
814 sp<InputChannel> inputChannel;
815 InputPublisher inputPublisher;
Jeff Browna665ca82010-09-08 11:49:43 -0700816 InputState inputState;
Jeff Browne839a582010-04-22 18:58:52 -0700817 Queue<DispatchEntry> outboundQueue;
Jeff Browne839a582010-04-22 18:58:52 -0700818
819 nsecs_t lastEventTime; // the time when the event was originally captured
820 nsecs_t lastDispatchTime; // the time when the last event was dispatched
Jeff Browne839a582010-04-22 18:58:52 -0700821
822 explicit Connection(const sp<InputChannel>& inputChannel);
823
Jeff Brown54bc2812010-06-15 01:31:58 -0700824 inline const char* getInputChannelName() const { return inputChannel->getName().string(); }
825
826 const char* getStatusLabel() const;
Jeff Browne839a582010-04-22 18:58:52 -0700827
828 // Finds a DispatchEntry in the outbound queue associated with the specified event.
829 // Returns NULL if not found.
830 DispatchEntry* findQueuedDispatchEntryForEvent(const EventEntry* eventEntry) const;
831
Jeff Browne839a582010-04-22 18:58:52 -0700832 // Gets the time since the current event was originally obtained from the input driver.
Jeff Browna665ca82010-09-08 11:49:43 -0700833 inline double getEventLatencyMillis(nsecs_t currentTime) const {
Jeff Browne839a582010-04-22 18:58:52 -0700834 return (currentTime - lastEventTime) / 1000000.0;
835 }
836
837 // Gets the time since the current event entered the outbound dispatch queue.
Jeff Browna665ca82010-09-08 11:49:43 -0700838 inline double getDispatchLatencyMillis(nsecs_t currentTime) const {
Jeff Browne839a582010-04-22 18:58:52 -0700839 return (currentTime - lastDispatchTime) / 1000000.0;
840 }
841
Jeff Browne839a582010-04-22 18:58:52 -0700842 status_t initialize();
843 };
844
Jeff Brown90f0cee2010-10-08 22:31:17 -0700845 enum DropReason {
846 DROP_REASON_NOT_DROPPED = 0,
847 DROP_REASON_POLICY = 1,
848 DROP_REASON_APP_SWITCH = 2,
849 DROP_REASON_DISABLED = 3,
850 };
851
Jeff Brown54bc2812010-06-15 01:31:58 -0700852 sp<InputDispatcherPolicyInterface> mPolicy;
Jeff Browne839a582010-04-22 18:58:52 -0700853
854 Mutex mLock;
855
Jeff Browne839a582010-04-22 18:58:52 -0700856 Allocator mAllocator;
Jeff Brown59abe7e2010-09-13 23:17:30 -0700857 sp<Looper> mLooper;
Jeff Browne839a582010-04-22 18:58:52 -0700858
Jeff Browna665ca82010-09-08 11:49:43 -0700859 EventEntry* mPendingEvent;
Jeff Brown54bc2812010-06-15 01:31:58 -0700860 Queue<EventEntry> mInboundQueue;
861 Queue<CommandEntry> mCommandQueue;
862
Jeff Browna665ca82010-09-08 11:49:43 -0700863 Vector<EventEntry*> mTempCancelationEvents;
864
865 void dispatchOnceInnerLocked(nsecs_t keyRepeatTimeout, nsecs_t keyRepeatDelay,
866 nsecs_t* nextWakeupTime);
867
Jeff Brown59abe7e2010-09-13 23:17:30 -0700868 // Enqueues an inbound event. Returns true if mLooper->wake() should be called.
Jeff Browna665ca82010-09-08 11:49:43 -0700869 bool enqueueInboundEventLocked(EventEntry* entry);
870
Jeff Brown90f0cee2010-10-08 22:31:17 -0700871 // Cleans up input state when dropping an inbound event.
872 void dropInboundEventLocked(EventEntry* entry, DropReason dropReason);
873
Jeff Browna665ca82010-09-08 11:49:43 -0700874 // App switch latency optimization.
Jeff Brown90f0cee2010-10-08 22:31:17 -0700875 bool mAppSwitchSawKeyDown;
Jeff Browna665ca82010-09-08 11:49:43 -0700876 nsecs_t mAppSwitchDueTime;
877
Jeff Brown90f0cee2010-10-08 22:31:17 -0700878 static bool isAppSwitchKeyCode(int32_t keyCode);
879 bool isAppSwitchKeyEventLocked(KeyEntry* keyEntry);
Jeff Browna665ca82010-09-08 11:49:43 -0700880 bool isAppSwitchPendingLocked();
Jeff Browna665ca82010-09-08 11:49:43 -0700881 void resetPendingAppSwitchLocked(bool handled);
882
Jeff Browne839a582010-04-22 18:58:52 -0700883 // All registered connections mapped by receive pipe file descriptor.
884 KeyedVector<int, sp<Connection> > mConnectionsByReceiveFd;
885
Jeff Brown53a415e2010-09-15 15:18:56 -0700886 ssize_t getConnectionIndexLocked(const sp<InputChannel>& inputChannel);
Jeff Brown0cacb872010-08-17 15:59:26 -0700887
Jeff Browne839a582010-04-22 18:58:52 -0700888 // Active connections are connections that have a non-empty outbound queue.
Jeff Brown51d45a72010-06-17 20:52:56 -0700889 // We don't use a ref-counted pointer here because we explicitly abort connections
890 // during unregistration which causes the connection's outbound queue to be cleared
891 // and the connection itself to be deactivated.
Jeff Browne839a582010-04-22 18:58:52 -0700892 Vector<Connection*> mActiveConnections;
893
Jeff Browna665ca82010-09-08 11:49:43 -0700894 // Input channels that will receive a copy of all input events.
895 Vector<sp<InputChannel> > mMonitoringChannels;
Jeff Browne839a582010-04-22 18:58:52 -0700896
Jeff Browna665ca82010-09-08 11:49:43 -0700897 // Preallocated key event object used for policy inquiries.
898 KeyEvent mReusableKeyEvent;
Jeff Browne839a582010-04-22 18:58:52 -0700899
Jeff Brown51d45a72010-06-17 20:52:56 -0700900 // Event injection and synchronization.
901 Condition mInjectionResultAvailableCondition;
Jeff Brown90f0cee2010-10-08 22:31:17 -0700902 bool hasInjectionPermission(int32_t injectorPid, int32_t injectorUid);
Jeff Brown51d45a72010-06-17 20:52:56 -0700903 void setInjectionResultLocked(EventEntry* entry, int32_t injectionResult);
904
Jeff Brownf67c53e2010-07-28 15:48:59 -0700905 Condition mInjectionSyncFinishedCondition;
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700906 void incrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brown53a415e2010-09-15 15:18:56 -0700907 void decrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brownf67c53e2010-07-28 15:48:59 -0700908
Jeff Brown542412c2010-08-18 15:51:08 -0700909 // Throttling state.
910 struct ThrottleState {
911 nsecs_t minTimeBetweenEvents;
912
913 nsecs_t lastEventTime;
914 int32_t lastDeviceId;
915 uint32_t lastSource;
916
917 uint32_t originalSampleCount; // only collected during debugging
918 } mThrottleState;
919
Jeff Browne839a582010-04-22 18:58:52 -0700920 // Key repeat tracking.
Jeff Browne839a582010-04-22 18:58:52 -0700921 struct KeyRepeatState {
922 KeyEntry* lastKeyEntry; // or null if no repeat
923 nsecs_t nextRepeatTime;
924 } mKeyRepeatState;
925
926 void resetKeyRepeatLocked();
Jeff Browna665ca82010-09-08 11:49:43 -0700927 KeyEntry* synthesizeKeyRepeatLocked(nsecs_t currentTime, nsecs_t keyRepeatTimeout);
Jeff Browne839a582010-04-22 18:58:52 -0700928
Jeff Brown54bc2812010-06-15 01:31:58 -0700929 // Deferred command processing.
930 bool runCommandsLockedInterruptible();
931 CommandEntry* postCommandLocked(Command command);
932
Jeff Browna665ca82010-09-08 11:49:43 -0700933 // Inbound event processing.
934 void drainInboundQueueLocked();
Jeff Brownd8816c32010-09-16 14:07:33 -0700935 void releasePendingEventLocked();
936 void releaseInboundEventLocked(EventEntry* entry);
Jeff Browne839a582010-04-22 18:58:52 -0700937
Jeff Browna665ca82010-09-08 11:49:43 -0700938 // Dispatch state.
939 bool mDispatchEnabled;
940 bool mDispatchFrozen;
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700941
Jeff Browna665ca82010-09-08 11:49:43 -0700942 Vector<InputWindow> mWindows;
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700943
944 const InputWindow* getWindowLocked(const sp<InputChannel>& inputChannel);
Jeff Browna665ca82010-09-08 11:49:43 -0700945
946 // Focus tracking for keys, trackball, etc.
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700947 const InputWindow* mFocusedWindow;
Jeff Browna665ca82010-09-08 11:49:43 -0700948
949 // Focus tracking for touch.
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700950 struct TouchedWindow {
951 const InputWindow* window;
952 int32_t targetFlags;
Jeff Browne33a9ec2010-11-10 16:53:45 -0800953 BitSet32 pointerIds; // zero unless target flag FLAG_SPLIT is set
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700954 sp<InputChannel> channel;
Jeff Browna665ca82010-09-08 11:49:43 -0700955 };
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700956 struct TouchState {
957 bool down;
958 bool split;
959 Vector<TouchedWindow> windows;
960
961 TouchState();
962 ~TouchState();
963 void reset();
964 void copyFrom(const TouchState& other);
965 void addOrUpdateWindow(const InputWindow* window, int32_t targetFlags, BitSet32 pointerIds);
966 void removeOutsideTouchWindows();
967 const InputWindow* getFirstForegroundWindow();
968 };
969
970 TouchState mTouchState;
971 TouchState mTempTouchState;
Jeff Browna665ca82010-09-08 11:49:43 -0700972
973 // Focused application.
974 InputApplication* mFocusedApplication;
975 InputApplication mFocusedApplicationStorage; // preallocated storage for mFocusedApplication
976 void releaseFocusedApplicationLocked();
977
978 // Dispatch inbound events.
979 bool dispatchConfigurationChangedLocked(
980 nsecs_t currentTime, ConfigurationChangedEntry* entry);
981 bool dispatchKeyLocked(
982 nsecs_t currentTime, KeyEntry* entry, nsecs_t keyRepeatTimeout,
Jeff Brown33d54ce2010-10-11 14:20:19 -0700983 DropReason* dropReason, nsecs_t* nextWakeupTime);
Jeff Browna665ca82010-09-08 11:49:43 -0700984 bool dispatchMotionLocked(
985 nsecs_t currentTime, MotionEntry* entry,
Jeff Brown33d54ce2010-10-11 14:20:19 -0700986 DropReason* dropReason, nsecs_t* nextWakeupTime);
Jeff Brown54bc2812010-06-15 01:31:58 -0700987 void dispatchEventToCurrentInputTargetsLocked(
988 nsecs_t currentTime, EventEntry* entry, bool resumeWithAppendedMotionSample);
Jeff Browne839a582010-04-22 18:58:52 -0700989
Jeff Browna665ca82010-09-08 11:49:43 -0700990 void logOutboundKeyDetailsLocked(const char* prefix, const KeyEntry* entry);
991 void logOutboundMotionDetailsLocked(const char* prefix, const MotionEntry* entry);
992
993 // The input targets that were most recently identified for dispatch.
Jeff Browna665ca82010-09-08 11:49:43 -0700994 bool mCurrentInputTargetsValid; // false while targets are being recomputed
995 Vector<InputTarget> mCurrentInputTargets;
Jeff Browna665ca82010-09-08 11:49:43 -0700996
997 enum InputTargetWaitCause {
998 INPUT_TARGET_WAIT_CAUSE_NONE,
999 INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY,
1000 INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY,
1001 };
1002
1003 InputTargetWaitCause mInputTargetWaitCause;
1004 nsecs_t mInputTargetWaitStartTime;
1005 nsecs_t mInputTargetWaitTimeoutTime;
1006 bool mInputTargetWaitTimeoutExpired;
1007
1008 // Finding targets for input events.
Jeff Brownd8816c32010-09-16 14:07:33 -07001009 void resetTargetsLocked();
Jeff Brownd1b0a2b2010-09-26 22:20:12 -07001010 void commitTargetsLocked();
Jeff Browna665ca82010-09-08 11:49:43 -07001011 int32_t handleTargetsNotReadyLocked(nsecs_t currentTime, const EventEntry* entry,
1012 const InputApplication* application, const InputWindow* window,
1013 nsecs_t* nextWakeupTime);
Jeff Brown53a415e2010-09-15 15:18:56 -07001014 void resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout,
1015 const sp<InputChannel>& inputChannel);
1016 nsecs_t getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime);
Jeff Browna665ca82010-09-08 11:49:43 -07001017 void resetANRTimeoutsLocked();
1018
Jeff Brownd1b0a2b2010-09-26 22:20:12 -07001019 int32_t findFocusedWindowTargetsLocked(nsecs_t currentTime, const EventEntry* entry,
1020 nsecs_t* nextWakeupTime);
1021 int32_t findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry* entry,
1022 nsecs_t* nextWakeupTime);
Jeff Browna665ca82010-09-08 11:49:43 -07001023
Jeff Brownd1b0a2b2010-09-26 22:20:12 -07001024 void addWindowTargetLocked(const InputWindow* window, int32_t targetFlags,
1025 BitSet32 pointerIds);
Jeff Browna665ca82010-09-08 11:49:43 -07001026 void addMonitoringTargetsLocked();
Jeff Brownef3a8232010-10-18 13:21:23 -07001027 void pokeUserActivityLocked(const EventEntry* eventEntry);
Jeff Brownd1b0a2b2010-09-26 22:20:12 -07001028 bool checkInjectionPermission(const InputWindow* window, const InjectionState* injectionState);
Jeff Brown35cf0e92010-10-05 12:26:23 -07001029 bool isWindowObscuredAtPointLocked(const InputWindow* window, int32_t x, int32_t y) const;
Jeff Brown53a415e2010-09-15 15:18:56 -07001030 bool isWindowFinishedWithPreviousInputLocked(const InputWindow* window);
Jeff Brown53a415e2010-09-15 15:18:56 -07001031 String8 getApplicationWindowLabelLocked(const InputApplication* application,
1032 const InputWindow* window);
Jeff Browna665ca82010-09-08 11:49:43 -07001033
Jeff Browne839a582010-04-22 18:58:52 -07001034 // Manage the dispatch cycle for a single connection.
Jeff Brown51d45a72010-06-17 20:52:56 -07001035 // These methods are deliberately not Interruptible because doing all of the work
1036 // with the mutex held makes it easier to ensure that connection invariants are maintained.
1037 // If needed, the methods post commands to run later once the critical bits are done.
1038 void prepareDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Jeff Browne839a582010-04-22 18:58:52 -07001039 EventEntry* eventEntry, const InputTarget* inputTarget,
1040 bool resumeWithAppendedMotionSample);
Jeff Brown53a415e2010-09-15 15:18:56 -07001041 void startDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown81499912010-11-05 15:02:16 -07001042 void finishDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
1043 bool handled);
Jeff Browna665ca82010-09-08 11:49:43 -07001044 void startNextDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown90f0cee2010-10-08 22:31:17 -07001045 void abortBrokenDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown53a415e2010-09-15 15:18:56 -07001046 void drainOutboundQueueLocked(Connection* connection);
Jeff Brown59abe7e2010-09-13 23:17:30 -07001047 static int handleReceiveCallback(int receiveFd, int events, void* data);
Jeff Browne839a582010-04-22 18:58:52 -07001048
Jeff Brown90f0cee2010-10-08 22:31:17 -07001049 void synthesizeCancelationEventsForAllConnectionsLocked(
1050 InputState::CancelationOptions options, const char* reason);
1051 void synthesizeCancelationEventsForInputChannelLocked(const sp<InputChannel>& channel,
1052 InputState::CancelationOptions options, const char* reason);
1053 void synthesizeCancelationEventsForConnectionLocked(const sp<Connection>& connection,
1054 InputState::CancelationOptions options, const char* reason);
1055
Jeff Brownd1b0a2b2010-09-26 22:20:12 -07001056 // Splitting motion events across windows.
1057 MotionEntry* splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds);
1058
Jeff Brownfef5b042010-10-27 18:43:51 -07001059 // Reset and drop everything the dispatcher is doing.
1060 void resetAndDropEverythingLocked(const char* reason);
1061
Jeff Browna665ca82010-09-08 11:49:43 -07001062 // Dump state.
1063 void dumpDispatchStateLocked(String8& dump);
1064 void logDispatchStateLocked();
1065
Jeff Browne839a582010-04-22 18:58:52 -07001066 // Add or remove a connection to the mActiveConnections vector.
1067 void activateConnectionLocked(Connection* connection);
1068 void deactivateConnectionLocked(Connection* connection);
1069
1070 // Interesting events that we might like to log or tell the framework about.
Jeff Brown54bc2812010-06-15 01:31:58 -07001071 void onDispatchCycleStartedLocked(
Jeff Brown51d45a72010-06-17 20:52:56 -07001072 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown54bc2812010-06-15 01:31:58 -07001073 void onDispatchCycleFinishedLocked(
Jeff Brown81499912010-11-05 15:02:16 -07001074 nsecs_t currentTime, const sp<Connection>& connection, bool handled);
Jeff Brown54bc2812010-06-15 01:31:58 -07001075 void onDispatchCycleBrokenLocked(
Jeff Brown51d45a72010-06-17 20:52:56 -07001076 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown53a415e2010-09-15 15:18:56 -07001077 void onANRLocked(
1078 nsecs_t currentTime, const InputApplication* application, const InputWindow* window,
1079 nsecs_t eventTime, nsecs_t waitStartTime);
Jeff Brown54bc2812010-06-15 01:31:58 -07001080
Jeff Brown51d45a72010-06-17 20:52:56 -07001081 // Outbound policy interactions.
Jeff Browna665ca82010-09-08 11:49:43 -07001082 void doNotifyConfigurationChangedInterruptible(CommandEntry* commandEntry);
Jeff Brown54bc2812010-06-15 01:31:58 -07001083 void doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown53a415e2010-09-15 15:18:56 -07001084 void doNotifyANRLockedInterruptible(CommandEntry* commandEntry);
Jeff Browna665ca82010-09-08 11:49:43 -07001085 void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown81499912010-11-05 15:02:16 -07001086 void doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry);
Jeff Browna665ca82010-09-08 11:49:43 -07001087 void doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown81499912010-11-05 15:02:16 -07001088 void initializeKeyEvent(KeyEvent* event, const KeyEntry* entry);
Jeff Brown53a415e2010-09-15 15:18:56 -07001089
1090 // Statistics gathering.
1091 void updateDispatchStatisticsLocked(nsecs_t currentTime, const EventEntry* entry,
1092 int32_t injectionResult, nsecs_t timeSpentWaitingForApplication);
Jeff Browne839a582010-04-22 18:58:52 -07001093};
1094
1095/* Enqueues and dispatches input events, endlessly. */
1096class InputDispatcherThread : public Thread {
1097public:
1098 explicit InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher);
1099 ~InputDispatcherThread();
1100
1101private:
1102 virtual bool threadLoop();
1103
1104 sp<InputDispatcherInterface> mDispatcher;
1105};
1106
1107} // namespace android
1108
Jeff Browna665ca82010-09-08 11:49:43 -07001109#endif // _UI_INPUT_DISPATCHER_H