blob: d09ff412dea3afd32413bfb78dc1f87b0db6ffc6 [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,
182 TYPE_STATUS_BAR_PANEL = FIRST_SYSTEM_WINDOW+14,
Jeff Browne68d9e02010-10-15 00:54:27 -0700183 TYPE_SECURE_SYSTEM_OVERLAY = FIRST_SYSTEM_WINDOW+15,
Jeff Browna665ca82010-09-08 11:49:43 -0700184 LAST_SYSTEM_WINDOW = 2999,
185 };
186
187 sp<InputChannel> inputChannel;
Jeff Brown53a415e2010-09-15 15:18:56 -0700188 String8 name;
Jeff Browna665ca82010-09-08 11:49:43 -0700189 int32_t layoutParamsFlags;
190 int32_t layoutParamsType;
191 nsecs_t dispatchingTimeout;
192 int32_t frameLeft;
193 int32_t frameTop;
194 int32_t frameRight;
195 int32_t frameBottom;
196 int32_t visibleFrameLeft;
197 int32_t visibleFrameTop;
198 int32_t visibleFrameRight;
199 int32_t visibleFrameBottom;
200 int32_t touchableAreaLeft;
201 int32_t touchableAreaTop;
202 int32_t touchableAreaRight;
203 int32_t touchableAreaBottom;
204 bool visible;
Jeff Brown53a415e2010-09-15 15:18:56 -0700205 bool canReceiveKeys;
Jeff Browna665ca82010-09-08 11:49:43 -0700206 bool hasFocus;
207 bool hasWallpaper;
208 bool paused;
Jeff Brown53a415e2010-09-15 15:18:56 -0700209 int32_t layer;
Jeff Browna665ca82010-09-08 11:49:43 -0700210 int32_t ownerPid;
211 int32_t ownerUid;
212
Jeff Browna665ca82010-09-08 11:49:43 -0700213 bool touchableAreaContainsPoint(int32_t x, int32_t y) const;
Jeff Brown35cf0e92010-10-05 12:26:23 -0700214 bool frameContainsPoint(int32_t x, int32_t y) const;
215
216 /* Returns true if the window is of a trusted type that is allowed to silently
217 * overlay other windows for the purpose of implementing the secure views feature.
218 * Trusted overlays, such as IME windows, can partly obscure other windows without causing
219 * motion events to be delivered to them with AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED.
220 */
221 bool isTrustedOverlay() const;
Jeff Browna665ca82010-09-08 11:49:43 -0700222};
223
224
225/*
226 * A private handle type used by the input manager to track the window.
227 */
228class InputApplicationHandle : public RefBase {
229protected:
230 InputApplicationHandle() { }
231 virtual ~InputApplicationHandle() { }
232};
233
234
235/*
236 * An input application describes properties of an application that can receive input.
237 */
238struct InputApplication {
239 String8 name;
240 nsecs_t dispatchingTimeout;
241 sp<InputApplicationHandle> handle;
242};
243
244
245/*
Jeff Brown54bc2812010-06-15 01:31:58 -0700246 * Input dispatcher policy interface.
247 *
248 * The input reader policy is used by the input reader to interact with the Window Manager
249 * and other system components.
250 *
251 * The actual implementation is partially supported by callbacks into the DVM
252 * via JNI. This interface is also mocked in the unit tests.
253 */
254class InputDispatcherPolicyInterface : public virtual RefBase {
255protected:
256 InputDispatcherPolicyInterface() { }
257 virtual ~InputDispatcherPolicyInterface() { }
258
259public:
260 /* Notifies the system that a configuration change has occurred. */
261 virtual void notifyConfigurationChanged(nsecs_t when) = 0;
262
Jeff Browna665ca82010-09-08 11:49:43 -0700263 /* Notifies the system that an application is not responding.
264 * Returns a new timeout to continue waiting, or 0 to abort dispatch. */
Jeff Brown53a415e2010-09-15 15:18:56 -0700265 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
266 const sp<InputChannel>& inputChannel) = 0;
Jeff Browna665ca82010-09-08 11:49:43 -0700267
Jeff Brown54bc2812010-06-15 01:31:58 -0700268 /* Notifies the system that an input channel is unrecoverably broken. */
269 virtual void notifyInputChannelBroken(const sp<InputChannel>& inputChannel) = 0;
270
Jeff Brown61ce3982010-09-07 10:44:57 -0700271 /* Gets the key repeat initial timeout or -1 if automatic key repeating is disabled. */
Jeff Brown54bc2812010-06-15 01:31:58 -0700272 virtual nsecs_t getKeyRepeatTimeout() = 0;
273
Jeff Brown61ce3982010-09-07 10:44:57 -0700274 /* Gets the key repeat inter-key delay. */
275 virtual nsecs_t getKeyRepeatDelay() = 0;
276
Jeff Brown542412c2010-08-18 15:51:08 -0700277 /* Gets the maximum suggested event delivery rate per second.
278 * This value is used to throttle motion event movement actions on a per-device
279 * basis. It is not intended to be a hard limit.
280 */
281 virtual int32_t getMaxEventsPerSecond() = 0;
Jeff Browna665ca82010-09-08 11:49:43 -0700282
Jeff Brown90f0cee2010-10-08 22:31:17 -0700283 /* Intercepts a key event immediately before queueing it.
284 * The policy can use this method as an opportunity to perform power management functions
285 * and early event preprocessing such as updating policy flags.
286 *
287 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
288 * should be dispatched to applications.
289 */
290 virtual void interceptKeyBeforeQueueing(nsecs_t when, int32_t deviceId,
291 int32_t action, int32_t& flags, int32_t keyCode, int32_t scanCode,
292 uint32_t& policyFlags) = 0;
293
294 /* Intercepts a generic touch, trackball or other event before queueing it.
295 * The policy can use this method as an opportunity to perform power management functions
296 * and early event preprocessing such as updating policy flags.
297 *
298 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
299 * should be dispatched to applications.
300 */
301 virtual void interceptGenericBeforeQueueing(nsecs_t when, uint32_t& policyFlags) = 0;
302
Jeff Browna665ca82010-09-08 11:49:43 -0700303 /* Allows the policy a chance to intercept a key before dispatching. */
304 virtual bool interceptKeyBeforeDispatching(const sp<InputChannel>& inputChannel,
305 const KeyEvent* keyEvent, uint32_t policyFlags) = 0;
306
Jeff Brown81499912010-11-05 15:02:16 -0700307 /* Allows the policy a chance to perform default processing for an unhandled key. */
308 virtual bool dispatchUnhandledKey(const sp<InputChannel>& inputChannel,
309 const KeyEvent* keyEvent, uint32_t policyFlags) = 0;
310
Jeff Brown90f0cee2010-10-08 22:31:17 -0700311 /* Notifies the policy about switch events.
312 */
313 virtual void notifySwitch(nsecs_t when,
314 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) = 0;
315
Jeff Browna665ca82010-09-08 11:49:43 -0700316 /* Poke user activity for an event dispatched to a window. */
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700317 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType) = 0;
Jeff Browna665ca82010-09-08 11:49:43 -0700318
319 /* Checks whether a given application pid/uid has permission to inject input events
320 * into other applications.
321 *
322 * This method is special in that its implementation promises to be non-reentrant and
323 * is safe to call while holding other locks. (Most other methods make no such guarantees!)
324 */
325 virtual bool checkInjectEventsPermissionNonReentrant(
326 int32_t injectorPid, int32_t injectorUid) = 0;
Jeff Brown54bc2812010-06-15 01:31:58 -0700327};
328
329
Jeff Browne839a582010-04-22 18:58:52 -0700330/* Notifies the system about input events generated by the input reader.
331 * The dispatcher is expected to be mostly asynchronous. */
332class InputDispatcherInterface : public virtual RefBase {
333protected:
334 InputDispatcherInterface() { }
335 virtual ~InputDispatcherInterface() { }
336
337public:
Jeff Browna665ca82010-09-08 11:49:43 -0700338 /* Dumps the state of the input dispatcher.
339 *
340 * This method may be called on any thread (usually by the input manager). */
341 virtual void dump(String8& dump) = 0;
342
Jeff Browne839a582010-04-22 18:58:52 -0700343 /* Runs a single iteration of the dispatch loop.
344 * Nominally processes one queued event, a timeout, or a response from an input consumer.
345 *
346 * This method should only be called on the input dispatcher thread.
347 */
348 virtual void dispatchOnce() = 0;
349
350 /* Notifies the dispatcher about new events.
Jeff Browne839a582010-04-22 18:58:52 -0700351 *
352 * These methods should only be called on the input reader thread.
353 */
Jeff Brown54bc2812010-06-15 01:31:58 -0700354 virtual void notifyConfigurationChanged(nsecs_t eventTime) = 0;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700355 virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Browne839a582010-04-22 18:58:52 -0700356 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode,
357 int32_t scanCode, int32_t metaState, nsecs_t downTime) = 0;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700358 virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Brownaf30ff62010-09-01 17:01:00 -0700359 uint32_t policyFlags, int32_t action, int32_t flags,
360 int32_t metaState, int32_t edgeFlags,
Jeff Browne839a582010-04-22 18:58:52 -0700361 uint32_t pointerCount, const int32_t* pointerIds, const PointerCoords* pointerCoords,
362 float xPrecision, float yPrecision, nsecs_t downTime) = 0;
Jeff Brown90f0cee2010-10-08 22:31:17 -0700363 virtual void notifySwitch(nsecs_t when,
364 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) = 0;
Jeff Browne839a582010-04-22 18:58:52 -0700365
Jeff Brown51d45a72010-06-17 20:52:56 -0700366 /* Injects an input event and optionally waits for sync.
Jeff Brownf67c53e2010-07-28 15:48:59 -0700367 * The synchronization mode determines whether the method blocks while waiting for
368 * input injection to proceed.
Jeff Brown51d45a72010-06-17 20:52:56 -0700369 * Returns one of the INPUT_EVENT_INJECTION_XXX constants.
370 *
371 * This method may be called on any thread (usually by the input manager).
372 */
373 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brownf67c53e2010-07-28 15:48:59 -0700374 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis) = 0;
Jeff Brown51d45a72010-06-17 20:52:56 -0700375
Jeff Browna665ca82010-09-08 11:49:43 -0700376 /* Sets the list of input windows.
377 *
378 * This method may be called on any thread (usually by the input manager).
379 */
380 virtual void setInputWindows(const Vector<InputWindow>& inputWindows) = 0;
381
382 /* Sets the focused application.
383 *
384 * This method may be called on any thread (usually by the input manager).
385 */
386 virtual void setFocusedApplication(const InputApplication* inputApplication) = 0;
387
388 /* Sets the input dispatching mode.
389 *
390 * This method may be called on any thread (usually by the input manager).
391 */
392 virtual void setInputDispatchMode(bool enabled, bool frozen) = 0;
393
Jeff Brown744c5592010-09-27 14:52:15 -0700394 /* Transfers touch focus from the window associated with one channel to the
395 * window associated with the other channel.
396 *
397 * Returns true on success. False if the window did not actually have touch focus.
398 */
399 virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel,
400 const sp<InputChannel>& toChannel) = 0;
401
Jeff Browne839a582010-04-22 18:58:52 -0700402 /* Registers or unregister input channels that may be used as targets for input events.
Jeff Browna665ca82010-09-08 11:49:43 -0700403 * If monitor is true, the channel will receive a copy of all input events.
Jeff Browne839a582010-04-22 18:58:52 -0700404 *
405 * These methods may be called on any thread (usually by the input manager).
406 */
Jeff Browna665ca82010-09-08 11:49:43 -0700407 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel, bool monitor) = 0;
Jeff Browne839a582010-04-22 18:58:52 -0700408 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) = 0;
409};
410
Jeff Brown54bc2812010-06-15 01:31:58 -0700411/* Dispatches events to input targets. Some functions of the input dispatcher, such as
412 * identifying input targets, are controlled by a separate policy object.
413 *
414 * IMPORTANT INVARIANT:
415 * Because the policy can potentially block or cause re-entrance into the input dispatcher,
416 * the input dispatcher never calls into the policy while holding its internal locks.
417 * The implementation is also carefully designed to recover from scenarios such as an
418 * input channel becoming unregistered while identifying input targets or processing timeouts.
419 *
420 * Methods marked 'Locked' must be called with the lock acquired.
421 *
422 * Methods marked 'LockedInterruptible' must be called with the lock acquired but
423 * may during the course of their execution release the lock, call into the policy, and
424 * then reacquire the lock. The caller is responsible for recovering gracefully.
425 *
426 * A 'LockedInterruptible' method may called a 'Locked' method, but NOT vice-versa.
427 */
Jeff Browne839a582010-04-22 18:58:52 -0700428class InputDispatcher : public InputDispatcherInterface {
429protected:
430 virtual ~InputDispatcher();
431
432public:
Jeff Brown54bc2812010-06-15 01:31:58 -0700433 explicit InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy);
Jeff Browne839a582010-04-22 18:58:52 -0700434
Jeff Browna665ca82010-09-08 11:49:43 -0700435 virtual void dump(String8& dump);
436
Jeff Browne839a582010-04-22 18:58:52 -0700437 virtual void dispatchOnce();
438
Jeff Brown54bc2812010-06-15 01:31:58 -0700439 virtual void notifyConfigurationChanged(nsecs_t eventTime);
Jeff Brown5c1ed842010-07-14 18:48:53 -0700440 virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Browne839a582010-04-22 18:58:52 -0700441 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode,
442 int32_t scanCode, int32_t metaState, nsecs_t downTime);
Jeff Brown5c1ed842010-07-14 18:48:53 -0700443 virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Brownaf30ff62010-09-01 17:01:00 -0700444 uint32_t policyFlags, int32_t action, int32_t flags,
445 int32_t metaState, int32_t edgeFlags,
Jeff Browne839a582010-04-22 18:58:52 -0700446 uint32_t pointerCount, const int32_t* pointerIds, const PointerCoords* pointerCoords,
447 float xPrecision, float yPrecision, nsecs_t downTime);
Jeff Brown90f0cee2010-10-08 22:31:17 -0700448 virtual void notifySwitch(nsecs_t when,
449 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) ;
Jeff Browne839a582010-04-22 18:58:52 -0700450
Jeff Brown51d45a72010-06-17 20:52:56 -0700451 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brownf67c53e2010-07-28 15:48:59 -0700452 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis);
Jeff Brown51d45a72010-06-17 20:52:56 -0700453
Jeff Browna665ca82010-09-08 11:49:43 -0700454 virtual void setInputWindows(const Vector<InputWindow>& inputWindows);
455 virtual void setFocusedApplication(const InputApplication* inputApplication);
456 virtual void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown50de30a2010-06-22 01:27:15 -0700457
Jeff Brown744c5592010-09-27 14:52:15 -0700458 virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel,
459 const sp<InputChannel>& toChannel);
460
Jeff Browna665ca82010-09-08 11:49:43 -0700461 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel, bool monitor);
Jeff Browne839a582010-04-22 18:58:52 -0700462 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel);
463
464private:
465 template <typename T>
466 struct Link {
467 T* next;
468 T* prev;
469 };
470
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700471 struct InjectionState {
472 mutable int32_t refCount;
473
474 int32_t injectorPid;
475 int32_t injectorUid;
476 int32_t injectionResult; // initially INPUT_EVENT_INJECTION_PENDING
477 bool injectionIsAsync; // set to true if injection is not waiting for the result
478 int32_t pendingForegroundDispatches; // the number of foreground dispatches in progress
479 };
480
Jeff Browne839a582010-04-22 18:58:52 -0700481 struct EventEntry : Link<EventEntry> {
482 enum {
483 TYPE_SENTINEL,
484 TYPE_CONFIGURATION_CHANGED,
485 TYPE_KEY,
486 TYPE_MOTION
487 };
488
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700489 mutable int32_t refCount;
Jeff Browne839a582010-04-22 18:58:52 -0700490 int32_t type;
491 nsecs_t eventTime;
Jeff Brown90f0cee2010-10-08 22:31:17 -0700492 uint32_t policyFlags;
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700493 InjectionState* injectionState;
Jeff Brown51d45a72010-06-17 20:52:56 -0700494
Jeff Brown54bc2812010-06-15 01:31:58 -0700495 bool dispatchInProgress; // initially false, set to true while dispatching
Jeff Brown51d45a72010-06-17 20:52:56 -0700496
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700497 inline bool isInjected() { return injectionState != NULL; }
Jeff Browne839a582010-04-22 18:58:52 -0700498 };
499
500 struct ConfigurationChangedEntry : EventEntry {
Jeff Browne839a582010-04-22 18:58:52 -0700501 };
502
503 struct KeyEntry : EventEntry {
504 int32_t deviceId;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700505 int32_t source;
Jeff Browne839a582010-04-22 18:58:52 -0700506 int32_t action;
507 int32_t flags;
508 int32_t keyCode;
509 int32_t scanCode;
510 int32_t metaState;
511 int32_t repeatCount;
512 nsecs_t downTime;
Jeff Browna665ca82010-09-08 11:49:43 -0700513
514 bool syntheticRepeat; // set to true for synthetic key repeats
515
516 enum InterceptKeyResult {
517 INTERCEPT_KEY_RESULT_UNKNOWN,
518 INTERCEPT_KEY_RESULT_SKIP,
519 INTERCEPT_KEY_RESULT_CONTINUE,
520 };
521 InterceptKeyResult interceptKeyResult; // set based on the interception result
Jeff Browne839a582010-04-22 18:58:52 -0700522 };
523
524 struct MotionSample {
525 MotionSample* next;
526
527 nsecs_t eventTime;
528 PointerCoords pointerCoords[MAX_POINTERS];
529 };
530
531 struct MotionEntry : EventEntry {
532 int32_t deviceId;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700533 int32_t source;
Jeff Browne839a582010-04-22 18:58:52 -0700534 int32_t action;
Jeff Brownaf30ff62010-09-01 17:01:00 -0700535 int32_t flags;
Jeff Browne839a582010-04-22 18:58:52 -0700536 int32_t metaState;
537 int32_t edgeFlags;
538 float xPrecision;
539 float yPrecision;
540 nsecs_t downTime;
541 uint32_t pointerCount;
542 int32_t pointerIds[MAX_POINTERS];
543
544 // Linked list of motion samples associated with this motion event.
545 MotionSample firstSample;
546 MotionSample* lastSample;
Jeff Brown542412c2010-08-18 15:51:08 -0700547
548 uint32_t countSamples() const;
Jeff Browne839a582010-04-22 18:58:52 -0700549 };
550
Jeff Brown54bc2812010-06-15 01:31:58 -0700551 // Tracks the progress of dispatching a particular event to a particular connection.
Jeff Browne839a582010-04-22 18:58:52 -0700552 struct DispatchEntry : Link<DispatchEntry> {
553 EventEntry* eventEntry; // the event to dispatch
554 int32_t targetFlags;
555 float xOffset;
556 float yOffset;
Jeff Browne839a582010-04-22 18:58:52 -0700557
558 // True if dispatch has started.
559 bool inProgress;
560
561 // For motion events:
562 // Pointer to the first motion sample to dispatch in this cycle.
563 // Usually NULL to indicate that the list of motion samples begins at
564 // MotionEntry::firstSample. Otherwise, some samples were dispatched in a previous
565 // cycle and this pointer indicates the location of the first remainining sample
566 // to dispatch during the current cycle.
567 MotionSample* headMotionSample;
568 // Pointer to a motion sample to dispatch in the next cycle if the dispatcher was
569 // unable to send all motion samples during this cycle. On the next cycle,
570 // headMotionSample will be initialized to tailMotionSample and tailMotionSample
571 // will be set to NULL.
572 MotionSample* tailMotionSample;
Jeff Brownf67c53e2010-07-28 15:48:59 -0700573
Jeff Brown53a415e2010-09-15 15:18:56 -0700574 inline bool hasForegroundTarget() const {
575 return targetFlags & InputTarget::FLAG_FOREGROUND;
Jeff Browna665ca82010-09-08 11:49:43 -0700576 }
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700577
578 inline bool isSplit() const {
579 return targetFlags & InputTarget::FLAG_SPLIT;
580 }
Jeff Browne839a582010-04-22 18:58:52 -0700581 };
582
Jeff Brown54bc2812010-06-15 01:31:58 -0700583 // A command entry captures state and behavior for an action to be performed in the
584 // dispatch loop after the initial processing has taken place. It is essentially
585 // a kind of continuation used to postpone sensitive policy interactions to a point
586 // in the dispatch loop where it is safe to release the lock (generally after finishing
587 // the critical parts of the dispatch cycle).
588 //
589 // The special thing about commands is that they can voluntarily release and reacquire
590 // the dispatcher lock at will. Initially when the command starts running, the
591 // dispatcher lock is held. However, if the command needs to call into the policy to
592 // do some work, it can release the lock, do the work, then reacquire the lock again
593 // before returning.
594 //
595 // This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch
596 // never calls into the policy while holding its lock.
597 //
598 // Commands are implicitly 'LockedInterruptible'.
599 struct CommandEntry;
600 typedef void (InputDispatcher::*Command)(CommandEntry* commandEntry);
601
Jeff Brown51d45a72010-06-17 20:52:56 -0700602 class Connection;
Jeff Brown54bc2812010-06-15 01:31:58 -0700603 struct CommandEntry : Link<CommandEntry> {
604 CommandEntry();
605 ~CommandEntry();
606
607 Command command;
608
609 // parameters for the command (usage varies by command)
Jeff Brown51d45a72010-06-17 20:52:56 -0700610 sp<Connection> connection;
Jeff Browna665ca82010-09-08 11:49:43 -0700611 nsecs_t eventTime;
612 KeyEntry* keyEntry;
613 sp<InputChannel> inputChannel;
614 sp<InputApplicationHandle> inputApplicationHandle;
Jeff Browna665ca82010-09-08 11:49:43 -0700615 int32_t userActivityEventType;
Jeff Brown81499912010-11-05 15:02:16 -0700616 bool handled;
Jeff Brown54bc2812010-06-15 01:31:58 -0700617 };
618
619 // Generic queue implementation.
Jeff Browne839a582010-04-22 18:58:52 -0700620 template <typename T>
621 struct Queue {
Jeff Browna665ca82010-09-08 11:49:43 -0700622 T headSentinel;
623 T tailSentinel;
Jeff Browne839a582010-04-22 18:58:52 -0700624
625 inline Queue() {
Jeff Browna665ca82010-09-08 11:49:43 -0700626 headSentinel.prev = NULL;
627 headSentinel.next = & tailSentinel;
628 tailSentinel.prev = & headSentinel;
629 tailSentinel.next = NULL;
Jeff Browne839a582010-04-22 18:58:52 -0700630 }
631
Jeff Browna665ca82010-09-08 11:49:43 -0700632 inline bool isEmpty() const {
633 return headSentinel.next == & tailSentinel;
Jeff Browne839a582010-04-22 18:58:52 -0700634 }
635
636 inline void enqueueAtTail(T* entry) {
Jeff Browna665ca82010-09-08 11:49:43 -0700637 T* last = tailSentinel.prev;
Jeff Browne839a582010-04-22 18:58:52 -0700638 last->next = entry;
639 entry->prev = last;
Jeff Browna665ca82010-09-08 11:49:43 -0700640 entry->next = & tailSentinel;
641 tailSentinel.prev = entry;
Jeff Browne839a582010-04-22 18:58:52 -0700642 }
643
644 inline void enqueueAtHead(T* entry) {
Jeff Browna665ca82010-09-08 11:49:43 -0700645 T* first = headSentinel.next;
646 headSentinel.next = entry;
647 entry->prev = & headSentinel;
Jeff Browne839a582010-04-22 18:58:52 -0700648 entry->next = first;
649 first->prev = entry;
650 }
651
652 inline void dequeue(T* entry) {
653 entry->prev->next = entry->next;
654 entry->next->prev = entry->prev;
655 }
656
657 inline T* dequeueAtHead() {
Jeff Browna665ca82010-09-08 11:49:43 -0700658 T* first = headSentinel.next;
Jeff Browne839a582010-04-22 18:58:52 -0700659 dequeue(first);
660 return first;
661 }
Jeff Brown53a415e2010-09-15 15:18:56 -0700662
663 uint32_t count() const;
Jeff Browne839a582010-04-22 18:58:52 -0700664 };
665
666 /* Allocates queue entries and performs reference counting as needed. */
667 class Allocator {
668 public:
669 Allocator();
670
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700671 InjectionState* obtainInjectionState(int32_t injectorPid, int32_t injectorUid);
Jeff Brown51d45a72010-06-17 20:52:56 -0700672 ConfigurationChangedEntry* obtainConfigurationChangedEntry(nsecs_t eventTime);
673 KeyEntry* obtainKeyEntry(nsecs_t eventTime,
Jeff Brown5c1ed842010-07-14 18:48:53 -0700674 int32_t deviceId, int32_t source, uint32_t policyFlags, int32_t action,
Jeff Brown51d45a72010-06-17 20:52:56 -0700675 int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState,
676 int32_t repeatCount, nsecs_t downTime);
677 MotionEntry* obtainMotionEntry(nsecs_t eventTime,
Jeff Brown5c1ed842010-07-14 18:48:53 -0700678 int32_t deviceId, int32_t source, uint32_t policyFlags, int32_t action,
Jeff Brownaf30ff62010-09-01 17:01:00 -0700679 int32_t flags, int32_t metaState, int32_t edgeFlags,
680 float xPrecision, float yPrecision,
Jeff Brown51d45a72010-06-17 20:52:56 -0700681 nsecs_t downTime, uint32_t pointerCount,
682 const int32_t* pointerIds, const PointerCoords* pointerCoords);
Jeff Browna665ca82010-09-08 11:49:43 -0700683 DispatchEntry* obtainDispatchEntry(EventEntry* eventEntry,
Jeff Brown53a415e2010-09-15 15:18:56 -0700684 int32_t targetFlags, float xOffset, float yOffset);
Jeff Brown54bc2812010-06-15 01:31:58 -0700685 CommandEntry* obtainCommandEntry(Command command);
Jeff Browne839a582010-04-22 18:58:52 -0700686
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700687 void releaseInjectionState(InjectionState* injectionState);
Jeff Browne839a582010-04-22 18:58:52 -0700688 void releaseEventEntry(EventEntry* entry);
689 void releaseConfigurationChangedEntry(ConfigurationChangedEntry* entry);
690 void releaseKeyEntry(KeyEntry* entry);
691 void releaseMotionEntry(MotionEntry* entry);
692 void releaseDispatchEntry(DispatchEntry* entry);
Jeff Brown54bc2812010-06-15 01:31:58 -0700693 void releaseCommandEntry(CommandEntry* entry);
Jeff Browne839a582010-04-22 18:58:52 -0700694
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700695 void recycleKeyEntry(KeyEntry* entry);
696
Jeff Browne839a582010-04-22 18:58:52 -0700697 void appendMotionSample(MotionEntry* motionEntry,
Jeff Brown51d45a72010-06-17 20:52:56 -0700698 nsecs_t eventTime, const PointerCoords* pointerCoords);
Jeff Browne839a582010-04-22 18:58:52 -0700699
700 private:
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700701 Pool<InjectionState> mInjectionStatePool;
Jeff Browne839a582010-04-22 18:58:52 -0700702 Pool<ConfigurationChangedEntry> mConfigurationChangeEntryPool;
703 Pool<KeyEntry> mKeyEntryPool;
704 Pool<MotionEntry> mMotionEntryPool;
705 Pool<MotionSample> mMotionSamplePool;
706 Pool<DispatchEntry> mDispatchEntryPool;
Jeff Brown54bc2812010-06-15 01:31:58 -0700707 Pool<CommandEntry> mCommandEntryPool;
Jeff Brown51d45a72010-06-17 20:52:56 -0700708
Jeff Brown90f0cee2010-10-08 22:31:17 -0700709 void initializeEventEntry(EventEntry* entry, int32_t type, nsecs_t eventTime,
710 uint32_t policyFlags);
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700711 void releaseEventEntryInjectionState(EventEntry* entry);
Jeff Browne839a582010-04-22 18:58:52 -0700712 };
713
Jeff Browna665ca82010-09-08 11:49:43 -0700714 /* Tracks dispatched key and motion event state so that cancelation events can be
715 * synthesized when events are dropped. */
716 class InputState {
717 public:
718 // Specifies whether a given event will violate input state consistency.
719 enum Consistency {
720 // The event is consistent with the current input state.
721 CONSISTENT,
722 // The event is inconsistent with the current input state but applications
723 // will tolerate it. eg. Down followed by another down.
724 TOLERABLE,
725 // The event is inconsistent with the current input state and will probably
726 // cause applications to crash. eg. Up without prior down, move with
727 // unexpected number of pointers.
728 BROKEN
729 };
730
Jeff Brown90f0cee2010-10-08 22:31:17 -0700731 // Specifies the sources to cancel.
732 enum CancelationOptions {
733 CANCEL_ALL_EVENTS = 0,
734 CANCEL_POINTER_EVENTS = 1,
735 CANCEL_NON_POINTER_EVENTS = 2,
736 };
737
Jeff Browna665ca82010-09-08 11:49:43 -0700738 InputState();
739 ~InputState();
740
741 // Returns true if there is no state to be canceled.
742 bool isNeutral() const;
743
Jeff Browna665ca82010-09-08 11:49:43 -0700744 // Records tracking information for an event that has just been published.
745 // Returns whether the event is consistent with the current input state.
746 Consistency trackEvent(const EventEntry* entry);
747
748 // Records tracking information for a key event that has just been published.
749 // Returns whether the event is consistent with the current input state.
750 Consistency trackKey(const KeyEntry* entry);
751
752 // Records tracking information for a motion event that has just been published.
753 // Returns whether the event is consistent with the current input state.
754 Consistency trackMotion(const MotionEntry* entry);
755
Jeff Brown90f0cee2010-10-08 22:31:17 -0700756 // Synthesizes cancelation events for the current state and resets the tracked state.
757 void synthesizeCancelationEvents(nsecs_t currentTime, Allocator* allocator,
758 Vector<EventEntry*>& outEvents, CancelationOptions options);
Jeff Browna665ca82010-09-08 11:49:43 -0700759
760 // Clears the current state.
761 void clear();
762
Jeff Brownb6702e52010-10-11 18:32:20 -0700763 // Copies pointer-related parts of the input state to another instance.
764 void copyPointerStateTo(InputState& other) const;
765
Jeff Browna665ca82010-09-08 11:49:43 -0700766 private:
Jeff Browna665ca82010-09-08 11:49:43 -0700767 struct KeyMemento {
768 int32_t deviceId;
769 int32_t source;
770 int32_t keyCode;
771 int32_t scanCode;
772 nsecs_t downTime;
773 };
774
775 struct MotionMemento {
776 int32_t deviceId;
777 int32_t source;
778 float xPrecision;
779 float yPrecision;
780 nsecs_t downTime;
781 uint32_t pointerCount;
782 int32_t pointerIds[MAX_POINTERS];
783 PointerCoords pointerCoords[MAX_POINTERS];
784
785 void setPointers(const MotionEntry* entry);
786 };
787
788 Vector<KeyMemento> mKeyMementos;
789 Vector<MotionMemento> mMotionMementos;
Jeff Brown90f0cee2010-10-08 22:31:17 -0700790
791 static bool shouldCancelEvent(int32_t eventSource, CancelationOptions options);
Jeff Browna665ca82010-09-08 11:49:43 -0700792 };
793
Jeff Browne839a582010-04-22 18:58:52 -0700794 /* Manages the dispatch state associated with a single input channel. */
795 class Connection : public RefBase {
796 protected:
797 virtual ~Connection();
798
799 public:
800 enum Status {
801 // Everything is peachy.
802 STATUS_NORMAL,
803 // An unrecoverable communication error has occurred.
804 STATUS_BROKEN,
Jeff Browne839a582010-04-22 18:58:52 -0700805 // The input channel has been unregistered.
806 STATUS_ZOMBIE
807 };
808
809 Status status;
810 sp<InputChannel> inputChannel;
811 InputPublisher inputPublisher;
Jeff Browna665ca82010-09-08 11:49:43 -0700812 InputState inputState;
Jeff Browne839a582010-04-22 18:58:52 -0700813 Queue<DispatchEntry> outboundQueue;
Jeff Browne839a582010-04-22 18:58:52 -0700814
815 nsecs_t lastEventTime; // the time when the event was originally captured
816 nsecs_t lastDispatchTime; // the time when the last event was dispatched
Jeff Browne839a582010-04-22 18:58:52 -0700817
818 explicit Connection(const sp<InputChannel>& inputChannel);
819
Jeff Brown54bc2812010-06-15 01:31:58 -0700820 inline const char* getInputChannelName() const { return inputChannel->getName().string(); }
821
822 const char* getStatusLabel() const;
Jeff Browne839a582010-04-22 18:58:52 -0700823
824 // Finds a DispatchEntry in the outbound queue associated with the specified event.
825 // Returns NULL if not found.
826 DispatchEntry* findQueuedDispatchEntryForEvent(const EventEntry* eventEntry) const;
827
Jeff Browne839a582010-04-22 18:58:52 -0700828 // Gets the time since the current event was originally obtained from the input driver.
Jeff Browna665ca82010-09-08 11:49:43 -0700829 inline double getEventLatencyMillis(nsecs_t currentTime) const {
Jeff Browne839a582010-04-22 18:58:52 -0700830 return (currentTime - lastEventTime) / 1000000.0;
831 }
832
833 // Gets the time since the current event entered the outbound dispatch queue.
Jeff Browna665ca82010-09-08 11:49:43 -0700834 inline double getDispatchLatencyMillis(nsecs_t currentTime) const {
Jeff Browne839a582010-04-22 18:58:52 -0700835 return (currentTime - lastDispatchTime) / 1000000.0;
836 }
837
Jeff Browne839a582010-04-22 18:58:52 -0700838 status_t initialize();
839 };
840
Jeff Brown90f0cee2010-10-08 22:31:17 -0700841 enum DropReason {
842 DROP_REASON_NOT_DROPPED = 0,
843 DROP_REASON_POLICY = 1,
844 DROP_REASON_APP_SWITCH = 2,
845 DROP_REASON_DISABLED = 3,
846 };
847
Jeff Brown54bc2812010-06-15 01:31:58 -0700848 sp<InputDispatcherPolicyInterface> mPolicy;
Jeff Browne839a582010-04-22 18:58:52 -0700849
850 Mutex mLock;
851
Jeff Browne839a582010-04-22 18:58:52 -0700852 Allocator mAllocator;
Jeff Brown59abe7e2010-09-13 23:17:30 -0700853 sp<Looper> mLooper;
Jeff Browne839a582010-04-22 18:58:52 -0700854
Jeff Browna665ca82010-09-08 11:49:43 -0700855 EventEntry* mPendingEvent;
Jeff Brown54bc2812010-06-15 01:31:58 -0700856 Queue<EventEntry> mInboundQueue;
857 Queue<CommandEntry> mCommandQueue;
858
Jeff Browna665ca82010-09-08 11:49:43 -0700859 Vector<EventEntry*> mTempCancelationEvents;
860
861 void dispatchOnceInnerLocked(nsecs_t keyRepeatTimeout, nsecs_t keyRepeatDelay,
862 nsecs_t* nextWakeupTime);
863
Jeff Brown59abe7e2010-09-13 23:17:30 -0700864 // Enqueues an inbound event. Returns true if mLooper->wake() should be called.
Jeff Browna665ca82010-09-08 11:49:43 -0700865 bool enqueueInboundEventLocked(EventEntry* entry);
866
Jeff Brown90f0cee2010-10-08 22:31:17 -0700867 // Cleans up input state when dropping an inbound event.
868 void dropInboundEventLocked(EventEntry* entry, DropReason dropReason);
869
Jeff Browna665ca82010-09-08 11:49:43 -0700870 // App switch latency optimization.
Jeff Brown90f0cee2010-10-08 22:31:17 -0700871 bool mAppSwitchSawKeyDown;
Jeff Browna665ca82010-09-08 11:49:43 -0700872 nsecs_t mAppSwitchDueTime;
873
Jeff Brown90f0cee2010-10-08 22:31:17 -0700874 static bool isAppSwitchKeyCode(int32_t keyCode);
875 bool isAppSwitchKeyEventLocked(KeyEntry* keyEntry);
Jeff Browna665ca82010-09-08 11:49:43 -0700876 bool isAppSwitchPendingLocked();
Jeff Browna665ca82010-09-08 11:49:43 -0700877 void resetPendingAppSwitchLocked(bool handled);
878
Jeff Browne839a582010-04-22 18:58:52 -0700879 // All registered connections mapped by receive pipe file descriptor.
880 KeyedVector<int, sp<Connection> > mConnectionsByReceiveFd;
881
Jeff Brown53a415e2010-09-15 15:18:56 -0700882 ssize_t getConnectionIndexLocked(const sp<InputChannel>& inputChannel);
Jeff Brown0cacb872010-08-17 15:59:26 -0700883
Jeff Browne839a582010-04-22 18:58:52 -0700884 // Active connections are connections that have a non-empty outbound queue.
Jeff Brown51d45a72010-06-17 20:52:56 -0700885 // We don't use a ref-counted pointer here because we explicitly abort connections
886 // during unregistration which causes the connection's outbound queue to be cleared
887 // and the connection itself to be deactivated.
Jeff Browne839a582010-04-22 18:58:52 -0700888 Vector<Connection*> mActiveConnections;
889
Jeff Browna665ca82010-09-08 11:49:43 -0700890 // Input channels that will receive a copy of all input events.
891 Vector<sp<InputChannel> > mMonitoringChannels;
Jeff Browne839a582010-04-22 18:58:52 -0700892
Jeff Browna665ca82010-09-08 11:49:43 -0700893 // Preallocated key event object used for policy inquiries.
894 KeyEvent mReusableKeyEvent;
Jeff Browne839a582010-04-22 18:58:52 -0700895
Jeff Brown51d45a72010-06-17 20:52:56 -0700896 // Event injection and synchronization.
897 Condition mInjectionResultAvailableCondition;
Jeff Brown90f0cee2010-10-08 22:31:17 -0700898 bool hasInjectionPermission(int32_t injectorPid, int32_t injectorUid);
Jeff Brown51d45a72010-06-17 20:52:56 -0700899 void setInjectionResultLocked(EventEntry* entry, int32_t injectionResult);
900
Jeff Brownf67c53e2010-07-28 15:48:59 -0700901 Condition mInjectionSyncFinishedCondition;
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700902 void incrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brown53a415e2010-09-15 15:18:56 -0700903 void decrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brownf67c53e2010-07-28 15:48:59 -0700904
Jeff Brown542412c2010-08-18 15:51:08 -0700905 // Throttling state.
906 struct ThrottleState {
907 nsecs_t minTimeBetweenEvents;
908
909 nsecs_t lastEventTime;
910 int32_t lastDeviceId;
911 uint32_t lastSource;
912
913 uint32_t originalSampleCount; // only collected during debugging
914 } mThrottleState;
915
Jeff Browne839a582010-04-22 18:58:52 -0700916 // Key repeat tracking.
Jeff Browne839a582010-04-22 18:58:52 -0700917 struct KeyRepeatState {
918 KeyEntry* lastKeyEntry; // or null if no repeat
919 nsecs_t nextRepeatTime;
920 } mKeyRepeatState;
921
922 void resetKeyRepeatLocked();
Jeff Browna665ca82010-09-08 11:49:43 -0700923 KeyEntry* synthesizeKeyRepeatLocked(nsecs_t currentTime, nsecs_t keyRepeatTimeout);
Jeff Browne839a582010-04-22 18:58:52 -0700924
Jeff Brown54bc2812010-06-15 01:31:58 -0700925 // Deferred command processing.
926 bool runCommandsLockedInterruptible();
927 CommandEntry* postCommandLocked(Command command);
928
Jeff Browna665ca82010-09-08 11:49:43 -0700929 // Inbound event processing.
930 void drainInboundQueueLocked();
Jeff Brownd8816c32010-09-16 14:07:33 -0700931 void releasePendingEventLocked();
932 void releaseInboundEventLocked(EventEntry* entry);
Jeff Browne839a582010-04-22 18:58:52 -0700933
Jeff Browna665ca82010-09-08 11:49:43 -0700934 // Dispatch state.
935 bool mDispatchEnabled;
936 bool mDispatchFrozen;
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700937
Jeff Browna665ca82010-09-08 11:49:43 -0700938 Vector<InputWindow> mWindows;
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700939
940 const InputWindow* getWindowLocked(const sp<InputChannel>& inputChannel);
Jeff Browna665ca82010-09-08 11:49:43 -0700941
942 // Focus tracking for keys, trackball, etc.
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700943 const InputWindow* mFocusedWindow;
Jeff Browna665ca82010-09-08 11:49:43 -0700944
945 // Focus tracking for touch.
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700946 struct TouchedWindow {
947 const InputWindow* window;
948 int32_t targetFlags;
949 BitSet32 pointerIds;
950 sp<InputChannel> channel;
Jeff Browna665ca82010-09-08 11:49:43 -0700951 };
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700952 struct TouchState {
953 bool down;
954 bool split;
955 Vector<TouchedWindow> windows;
956
957 TouchState();
958 ~TouchState();
959 void reset();
960 void copyFrom(const TouchState& other);
961 void addOrUpdateWindow(const InputWindow* window, int32_t targetFlags, BitSet32 pointerIds);
962 void removeOutsideTouchWindows();
963 const InputWindow* getFirstForegroundWindow();
964 };
965
966 TouchState mTouchState;
967 TouchState mTempTouchState;
Jeff Browna665ca82010-09-08 11:49:43 -0700968
969 // Focused application.
970 InputApplication* mFocusedApplication;
971 InputApplication mFocusedApplicationStorage; // preallocated storage for mFocusedApplication
972 void releaseFocusedApplicationLocked();
973
974 // Dispatch inbound events.
975 bool dispatchConfigurationChangedLocked(
976 nsecs_t currentTime, ConfigurationChangedEntry* entry);
977 bool dispatchKeyLocked(
978 nsecs_t currentTime, KeyEntry* entry, nsecs_t keyRepeatTimeout,
Jeff Brown33d54ce2010-10-11 14:20:19 -0700979 DropReason* dropReason, nsecs_t* nextWakeupTime);
Jeff Browna665ca82010-09-08 11:49:43 -0700980 bool dispatchMotionLocked(
981 nsecs_t currentTime, MotionEntry* entry,
Jeff Brown33d54ce2010-10-11 14:20:19 -0700982 DropReason* dropReason, nsecs_t* nextWakeupTime);
Jeff Brown54bc2812010-06-15 01:31:58 -0700983 void dispatchEventToCurrentInputTargetsLocked(
984 nsecs_t currentTime, EventEntry* entry, bool resumeWithAppendedMotionSample);
Jeff Browne839a582010-04-22 18:58:52 -0700985
Jeff Browna665ca82010-09-08 11:49:43 -0700986 void logOutboundKeyDetailsLocked(const char* prefix, const KeyEntry* entry);
987 void logOutboundMotionDetailsLocked(const char* prefix, const MotionEntry* entry);
988
989 // The input targets that were most recently identified for dispatch.
Jeff Browna665ca82010-09-08 11:49:43 -0700990 bool mCurrentInputTargetsValid; // false while targets are being recomputed
991 Vector<InputTarget> mCurrentInputTargets;
Jeff Browna665ca82010-09-08 11:49:43 -0700992
993 enum InputTargetWaitCause {
994 INPUT_TARGET_WAIT_CAUSE_NONE,
995 INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY,
996 INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY,
997 };
998
999 InputTargetWaitCause mInputTargetWaitCause;
1000 nsecs_t mInputTargetWaitStartTime;
1001 nsecs_t mInputTargetWaitTimeoutTime;
1002 bool mInputTargetWaitTimeoutExpired;
1003
1004 // Finding targets for input events.
Jeff Brownd8816c32010-09-16 14:07:33 -07001005 void resetTargetsLocked();
Jeff Brownd1b0a2b2010-09-26 22:20:12 -07001006 void commitTargetsLocked();
Jeff Browna665ca82010-09-08 11:49:43 -07001007 int32_t handleTargetsNotReadyLocked(nsecs_t currentTime, const EventEntry* entry,
1008 const InputApplication* application, const InputWindow* window,
1009 nsecs_t* nextWakeupTime);
Jeff Brown53a415e2010-09-15 15:18:56 -07001010 void resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout,
1011 const sp<InputChannel>& inputChannel);
1012 nsecs_t getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime);
Jeff Browna665ca82010-09-08 11:49:43 -07001013 void resetANRTimeoutsLocked();
1014
Jeff Brownd1b0a2b2010-09-26 22:20:12 -07001015 int32_t findFocusedWindowTargetsLocked(nsecs_t currentTime, const EventEntry* entry,
1016 nsecs_t* nextWakeupTime);
1017 int32_t findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry* entry,
1018 nsecs_t* nextWakeupTime);
Jeff Browna665ca82010-09-08 11:49:43 -07001019
Jeff Brownd1b0a2b2010-09-26 22:20:12 -07001020 void addWindowTargetLocked(const InputWindow* window, int32_t targetFlags,
1021 BitSet32 pointerIds);
Jeff Browna665ca82010-09-08 11:49:43 -07001022 void addMonitoringTargetsLocked();
Jeff Brownef3a8232010-10-18 13:21:23 -07001023 void pokeUserActivityLocked(const EventEntry* eventEntry);
Jeff Brownd1b0a2b2010-09-26 22:20:12 -07001024 bool checkInjectionPermission(const InputWindow* window, const InjectionState* injectionState);
Jeff Brown35cf0e92010-10-05 12:26:23 -07001025 bool isWindowObscuredAtPointLocked(const InputWindow* window, int32_t x, int32_t y) const;
Jeff Brown53a415e2010-09-15 15:18:56 -07001026 bool isWindowFinishedWithPreviousInputLocked(const InputWindow* window);
Jeff Brown53a415e2010-09-15 15:18:56 -07001027 String8 getApplicationWindowLabelLocked(const InputApplication* application,
1028 const InputWindow* window);
Jeff Browna665ca82010-09-08 11:49:43 -07001029
Jeff Browne839a582010-04-22 18:58:52 -07001030 // Manage the dispatch cycle for a single connection.
Jeff Brown51d45a72010-06-17 20:52:56 -07001031 // These methods are deliberately not Interruptible because doing all of the work
1032 // with the mutex held makes it easier to ensure that connection invariants are maintained.
1033 // If needed, the methods post commands to run later once the critical bits are done.
1034 void prepareDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Jeff Browne839a582010-04-22 18:58:52 -07001035 EventEntry* eventEntry, const InputTarget* inputTarget,
1036 bool resumeWithAppendedMotionSample);
Jeff Brown53a415e2010-09-15 15:18:56 -07001037 void startDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown81499912010-11-05 15:02:16 -07001038 void finishDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
1039 bool handled);
Jeff Browna665ca82010-09-08 11:49:43 -07001040 void startNextDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown90f0cee2010-10-08 22:31:17 -07001041 void abortBrokenDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown53a415e2010-09-15 15:18:56 -07001042 void drainOutboundQueueLocked(Connection* connection);
Jeff Brown59abe7e2010-09-13 23:17:30 -07001043 static int handleReceiveCallback(int receiveFd, int events, void* data);
Jeff Browne839a582010-04-22 18:58:52 -07001044
Jeff Brown90f0cee2010-10-08 22:31:17 -07001045 void synthesizeCancelationEventsForAllConnectionsLocked(
1046 InputState::CancelationOptions options, const char* reason);
1047 void synthesizeCancelationEventsForInputChannelLocked(const sp<InputChannel>& channel,
1048 InputState::CancelationOptions options, const char* reason);
1049 void synthesizeCancelationEventsForConnectionLocked(const sp<Connection>& connection,
1050 InputState::CancelationOptions options, const char* reason);
1051
Jeff Brownd1b0a2b2010-09-26 22:20:12 -07001052 // Splitting motion events across windows.
1053 MotionEntry* splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds);
1054
Jeff Brownfef5b042010-10-27 18:43:51 -07001055 // Reset and drop everything the dispatcher is doing.
1056 void resetAndDropEverythingLocked(const char* reason);
1057
Jeff Browna665ca82010-09-08 11:49:43 -07001058 // Dump state.
1059 void dumpDispatchStateLocked(String8& dump);
1060 void logDispatchStateLocked();
1061
Jeff Browne839a582010-04-22 18:58:52 -07001062 // Add or remove a connection to the mActiveConnections vector.
1063 void activateConnectionLocked(Connection* connection);
1064 void deactivateConnectionLocked(Connection* connection);
1065
1066 // Interesting events that we might like to log or tell the framework about.
Jeff Brown54bc2812010-06-15 01:31:58 -07001067 void onDispatchCycleStartedLocked(
Jeff Brown51d45a72010-06-17 20:52:56 -07001068 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown54bc2812010-06-15 01:31:58 -07001069 void onDispatchCycleFinishedLocked(
Jeff Brown81499912010-11-05 15:02:16 -07001070 nsecs_t currentTime, const sp<Connection>& connection, bool handled);
Jeff Brown54bc2812010-06-15 01:31:58 -07001071 void onDispatchCycleBrokenLocked(
Jeff Brown51d45a72010-06-17 20:52:56 -07001072 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown53a415e2010-09-15 15:18:56 -07001073 void onANRLocked(
1074 nsecs_t currentTime, const InputApplication* application, const InputWindow* window,
1075 nsecs_t eventTime, nsecs_t waitStartTime);
Jeff Brown54bc2812010-06-15 01:31:58 -07001076
Jeff Brown51d45a72010-06-17 20:52:56 -07001077 // Outbound policy interactions.
Jeff Browna665ca82010-09-08 11:49:43 -07001078 void doNotifyConfigurationChangedInterruptible(CommandEntry* commandEntry);
Jeff Brown54bc2812010-06-15 01:31:58 -07001079 void doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown53a415e2010-09-15 15:18:56 -07001080 void doNotifyANRLockedInterruptible(CommandEntry* commandEntry);
Jeff Browna665ca82010-09-08 11:49:43 -07001081 void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown81499912010-11-05 15:02:16 -07001082 void doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry);
Jeff Browna665ca82010-09-08 11:49:43 -07001083 void doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown81499912010-11-05 15:02:16 -07001084 void initializeKeyEvent(KeyEvent* event, const KeyEntry* entry);
Jeff Brown53a415e2010-09-15 15:18:56 -07001085
1086 // Statistics gathering.
1087 void updateDispatchStatisticsLocked(nsecs_t currentTime, const EventEntry* entry,
1088 int32_t injectionResult, nsecs_t timeSpentWaitingForApplication);
Jeff Browne839a582010-04-22 18:58:52 -07001089};
1090
1091/* Enqueues and dispatches input events, endlessly. */
1092class InputDispatcherThread : public Thread {
1093public:
1094 explicit InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher);
1095 ~InputDispatcherThread();
1096
1097private:
1098 virtual bool threadLoop();
1099
1100 sp<InputDispatcherInterface> mDispatcher;
1101};
1102
1103} // namespace android
1104
Jeff Browna665ca82010-09-08 11:49:43 -07001105#endif // _UI_INPUT_DISPATCHER_H