blob: a5591ba921e34ca7801b12a6967ed6f7d1b2e83a [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 Brown90f0cee2010-10-08 22:31:17 -0700307 /* Notifies the policy about switch events.
308 */
309 virtual void notifySwitch(nsecs_t when,
310 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) = 0;
311
Jeff Browna665ca82010-09-08 11:49:43 -0700312 /* Poke user activity for an event dispatched to a window. */
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700313 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType) = 0;
Jeff Browna665ca82010-09-08 11:49:43 -0700314
315 /* Checks whether a given application pid/uid has permission to inject input events
316 * into other applications.
317 *
318 * This method is special in that its implementation promises to be non-reentrant and
319 * is safe to call while holding other locks. (Most other methods make no such guarantees!)
320 */
321 virtual bool checkInjectEventsPermissionNonReentrant(
322 int32_t injectorPid, int32_t injectorUid) = 0;
Jeff Brown54bc2812010-06-15 01:31:58 -0700323};
324
325
Jeff Browne839a582010-04-22 18:58:52 -0700326/* Notifies the system about input events generated by the input reader.
327 * The dispatcher is expected to be mostly asynchronous. */
328class InputDispatcherInterface : public virtual RefBase {
329protected:
330 InputDispatcherInterface() { }
331 virtual ~InputDispatcherInterface() { }
332
333public:
Jeff Browna665ca82010-09-08 11:49:43 -0700334 /* Dumps the state of the input dispatcher.
335 *
336 * This method may be called on any thread (usually by the input manager). */
337 virtual void dump(String8& dump) = 0;
338
Jeff Browne839a582010-04-22 18:58:52 -0700339 /* Runs a single iteration of the dispatch loop.
340 * Nominally processes one queued event, a timeout, or a response from an input consumer.
341 *
342 * This method should only be called on the input dispatcher thread.
343 */
344 virtual void dispatchOnce() = 0;
345
346 /* Notifies the dispatcher about new events.
Jeff Browne839a582010-04-22 18:58:52 -0700347 *
348 * These methods should only be called on the input reader thread.
349 */
Jeff Brown54bc2812010-06-15 01:31:58 -0700350 virtual void notifyConfigurationChanged(nsecs_t eventTime) = 0;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700351 virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Browne839a582010-04-22 18:58:52 -0700352 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode,
353 int32_t scanCode, int32_t metaState, nsecs_t downTime) = 0;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700354 virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Brownaf30ff62010-09-01 17:01:00 -0700355 uint32_t policyFlags, int32_t action, int32_t flags,
356 int32_t metaState, int32_t edgeFlags,
Jeff Browne839a582010-04-22 18:58:52 -0700357 uint32_t pointerCount, const int32_t* pointerIds, const PointerCoords* pointerCoords,
358 float xPrecision, float yPrecision, nsecs_t downTime) = 0;
Jeff Brown90f0cee2010-10-08 22:31:17 -0700359 virtual void notifySwitch(nsecs_t when,
360 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) = 0;
Jeff Browne839a582010-04-22 18:58:52 -0700361
Jeff Brown51d45a72010-06-17 20:52:56 -0700362 /* Injects an input event and optionally waits for sync.
Jeff Brownf67c53e2010-07-28 15:48:59 -0700363 * The synchronization mode determines whether the method blocks while waiting for
364 * input injection to proceed.
Jeff Brown51d45a72010-06-17 20:52:56 -0700365 * Returns one of the INPUT_EVENT_INJECTION_XXX constants.
366 *
367 * This method may be called on any thread (usually by the input manager).
368 */
369 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brownf67c53e2010-07-28 15:48:59 -0700370 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis) = 0;
Jeff Brown51d45a72010-06-17 20:52:56 -0700371
Jeff Browna665ca82010-09-08 11:49:43 -0700372 /* Sets the list of input windows.
373 *
374 * This method may be called on any thread (usually by the input manager).
375 */
376 virtual void setInputWindows(const Vector<InputWindow>& inputWindows) = 0;
377
378 /* Sets the focused application.
379 *
380 * This method may be called on any thread (usually by the input manager).
381 */
382 virtual void setFocusedApplication(const InputApplication* inputApplication) = 0;
383
384 /* Sets the input dispatching mode.
385 *
386 * This method may be called on any thread (usually by the input manager).
387 */
388 virtual void setInputDispatchMode(bool enabled, bool frozen) = 0;
389
Jeff Brown744c5592010-09-27 14:52:15 -0700390 /* Transfers touch focus from the window associated with one channel to the
391 * window associated with the other channel.
392 *
393 * Returns true on success. False if the window did not actually have touch focus.
394 */
395 virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel,
396 const sp<InputChannel>& toChannel) = 0;
397
Jeff Browne839a582010-04-22 18:58:52 -0700398 /* Registers or unregister input channels that may be used as targets for input events.
Jeff Browna665ca82010-09-08 11:49:43 -0700399 * If monitor is true, the channel will receive a copy of all input events.
Jeff Browne839a582010-04-22 18:58:52 -0700400 *
401 * These methods may be called on any thread (usually by the input manager).
402 */
Jeff Browna665ca82010-09-08 11:49:43 -0700403 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel, bool monitor) = 0;
Jeff Browne839a582010-04-22 18:58:52 -0700404 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) = 0;
405};
406
Jeff Brown54bc2812010-06-15 01:31:58 -0700407/* Dispatches events to input targets. Some functions of the input dispatcher, such as
408 * identifying input targets, are controlled by a separate policy object.
409 *
410 * IMPORTANT INVARIANT:
411 * Because the policy can potentially block or cause re-entrance into the input dispatcher,
412 * the input dispatcher never calls into the policy while holding its internal locks.
413 * The implementation is also carefully designed to recover from scenarios such as an
414 * input channel becoming unregistered while identifying input targets or processing timeouts.
415 *
416 * Methods marked 'Locked' must be called with the lock acquired.
417 *
418 * Methods marked 'LockedInterruptible' must be called with the lock acquired but
419 * may during the course of their execution release the lock, call into the policy, and
420 * then reacquire the lock. The caller is responsible for recovering gracefully.
421 *
422 * A 'LockedInterruptible' method may called a 'Locked' method, but NOT vice-versa.
423 */
Jeff Browne839a582010-04-22 18:58:52 -0700424class InputDispatcher : public InputDispatcherInterface {
425protected:
426 virtual ~InputDispatcher();
427
428public:
Jeff Brown54bc2812010-06-15 01:31:58 -0700429 explicit InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy);
Jeff Browne839a582010-04-22 18:58:52 -0700430
Jeff Browna665ca82010-09-08 11:49:43 -0700431 virtual void dump(String8& dump);
432
Jeff Browne839a582010-04-22 18:58:52 -0700433 virtual void dispatchOnce();
434
Jeff Brown54bc2812010-06-15 01:31:58 -0700435 virtual void notifyConfigurationChanged(nsecs_t eventTime);
Jeff Brown5c1ed842010-07-14 18:48:53 -0700436 virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Browne839a582010-04-22 18:58:52 -0700437 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode,
438 int32_t scanCode, int32_t metaState, nsecs_t downTime);
Jeff Brown5c1ed842010-07-14 18:48:53 -0700439 virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Brownaf30ff62010-09-01 17:01:00 -0700440 uint32_t policyFlags, int32_t action, int32_t flags,
441 int32_t metaState, int32_t edgeFlags,
Jeff Browne839a582010-04-22 18:58:52 -0700442 uint32_t pointerCount, const int32_t* pointerIds, const PointerCoords* pointerCoords,
443 float xPrecision, float yPrecision, nsecs_t downTime);
Jeff Brown90f0cee2010-10-08 22:31:17 -0700444 virtual void notifySwitch(nsecs_t when,
445 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) ;
Jeff Browne839a582010-04-22 18:58:52 -0700446
Jeff Brown51d45a72010-06-17 20:52:56 -0700447 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brownf67c53e2010-07-28 15:48:59 -0700448 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis);
Jeff Brown51d45a72010-06-17 20:52:56 -0700449
Jeff Browna665ca82010-09-08 11:49:43 -0700450 virtual void setInputWindows(const Vector<InputWindow>& inputWindows);
451 virtual void setFocusedApplication(const InputApplication* inputApplication);
452 virtual void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown50de30a2010-06-22 01:27:15 -0700453
Jeff Brown744c5592010-09-27 14:52:15 -0700454 virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel,
455 const sp<InputChannel>& toChannel);
456
Jeff Browna665ca82010-09-08 11:49:43 -0700457 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel, bool monitor);
Jeff Browne839a582010-04-22 18:58:52 -0700458 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel);
459
460private:
461 template <typename T>
462 struct Link {
463 T* next;
464 T* prev;
465 };
466
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700467 struct InjectionState {
468 mutable int32_t refCount;
469
470 int32_t injectorPid;
471 int32_t injectorUid;
472 int32_t injectionResult; // initially INPUT_EVENT_INJECTION_PENDING
473 bool injectionIsAsync; // set to true if injection is not waiting for the result
474 int32_t pendingForegroundDispatches; // the number of foreground dispatches in progress
475 };
476
Jeff Browne839a582010-04-22 18:58:52 -0700477 struct EventEntry : Link<EventEntry> {
478 enum {
479 TYPE_SENTINEL,
480 TYPE_CONFIGURATION_CHANGED,
481 TYPE_KEY,
482 TYPE_MOTION
483 };
484
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700485 mutable int32_t refCount;
Jeff Browne839a582010-04-22 18:58:52 -0700486 int32_t type;
487 nsecs_t eventTime;
Jeff Brown90f0cee2010-10-08 22:31:17 -0700488 uint32_t policyFlags;
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700489 InjectionState* injectionState;
Jeff Brown51d45a72010-06-17 20:52:56 -0700490
Jeff Brown54bc2812010-06-15 01:31:58 -0700491 bool dispatchInProgress; // initially false, set to true while dispatching
Jeff Brown51d45a72010-06-17 20:52:56 -0700492
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700493 inline bool isInjected() { return injectionState != NULL; }
Jeff Browne839a582010-04-22 18:58:52 -0700494 };
495
496 struct ConfigurationChangedEntry : EventEntry {
Jeff Browne839a582010-04-22 18:58:52 -0700497 };
498
499 struct KeyEntry : EventEntry {
500 int32_t deviceId;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700501 int32_t source;
Jeff Browne839a582010-04-22 18:58:52 -0700502 int32_t action;
503 int32_t flags;
504 int32_t keyCode;
505 int32_t scanCode;
506 int32_t metaState;
507 int32_t repeatCount;
508 nsecs_t downTime;
Jeff Browna665ca82010-09-08 11:49:43 -0700509
510 bool syntheticRepeat; // set to true for synthetic key repeats
511
512 enum InterceptKeyResult {
513 INTERCEPT_KEY_RESULT_UNKNOWN,
514 INTERCEPT_KEY_RESULT_SKIP,
515 INTERCEPT_KEY_RESULT_CONTINUE,
516 };
517 InterceptKeyResult interceptKeyResult; // set based on the interception result
Jeff Browne839a582010-04-22 18:58:52 -0700518 };
519
520 struct MotionSample {
521 MotionSample* next;
522
523 nsecs_t eventTime;
524 PointerCoords pointerCoords[MAX_POINTERS];
525 };
526
527 struct MotionEntry : EventEntry {
528 int32_t deviceId;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700529 int32_t source;
Jeff Browne839a582010-04-22 18:58:52 -0700530 int32_t action;
Jeff Brownaf30ff62010-09-01 17:01:00 -0700531 int32_t flags;
Jeff Browne839a582010-04-22 18:58:52 -0700532 int32_t metaState;
533 int32_t edgeFlags;
534 float xPrecision;
535 float yPrecision;
536 nsecs_t downTime;
537 uint32_t pointerCount;
538 int32_t pointerIds[MAX_POINTERS];
539
540 // Linked list of motion samples associated with this motion event.
541 MotionSample firstSample;
542 MotionSample* lastSample;
Jeff Brown542412c2010-08-18 15:51:08 -0700543
544 uint32_t countSamples() const;
Jeff Browne839a582010-04-22 18:58:52 -0700545 };
546
Jeff Brown54bc2812010-06-15 01:31:58 -0700547 // Tracks the progress of dispatching a particular event to a particular connection.
Jeff Browne839a582010-04-22 18:58:52 -0700548 struct DispatchEntry : Link<DispatchEntry> {
549 EventEntry* eventEntry; // the event to dispatch
550 int32_t targetFlags;
551 float xOffset;
552 float yOffset;
Jeff Browne839a582010-04-22 18:58:52 -0700553
554 // True if dispatch has started.
555 bool inProgress;
556
557 // For motion events:
558 // Pointer to the first motion sample to dispatch in this cycle.
559 // Usually NULL to indicate that the list of motion samples begins at
560 // MotionEntry::firstSample. Otherwise, some samples were dispatched in a previous
561 // cycle and this pointer indicates the location of the first remainining sample
562 // to dispatch during the current cycle.
563 MotionSample* headMotionSample;
564 // Pointer to a motion sample to dispatch in the next cycle if the dispatcher was
565 // unable to send all motion samples during this cycle. On the next cycle,
566 // headMotionSample will be initialized to tailMotionSample and tailMotionSample
567 // will be set to NULL.
568 MotionSample* tailMotionSample;
Jeff Brownf67c53e2010-07-28 15:48:59 -0700569
Jeff Brown53a415e2010-09-15 15:18:56 -0700570 inline bool hasForegroundTarget() const {
571 return targetFlags & InputTarget::FLAG_FOREGROUND;
Jeff Browna665ca82010-09-08 11:49:43 -0700572 }
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700573
574 inline bool isSplit() const {
575 return targetFlags & InputTarget::FLAG_SPLIT;
576 }
Jeff Browne839a582010-04-22 18:58:52 -0700577 };
578
Jeff Brown54bc2812010-06-15 01:31:58 -0700579 // A command entry captures state and behavior for an action to be performed in the
580 // dispatch loop after the initial processing has taken place. It is essentially
581 // a kind of continuation used to postpone sensitive policy interactions to a point
582 // in the dispatch loop where it is safe to release the lock (generally after finishing
583 // the critical parts of the dispatch cycle).
584 //
585 // The special thing about commands is that they can voluntarily release and reacquire
586 // the dispatcher lock at will. Initially when the command starts running, the
587 // dispatcher lock is held. However, if the command needs to call into the policy to
588 // do some work, it can release the lock, do the work, then reacquire the lock again
589 // before returning.
590 //
591 // This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch
592 // never calls into the policy while holding its lock.
593 //
594 // Commands are implicitly 'LockedInterruptible'.
595 struct CommandEntry;
596 typedef void (InputDispatcher::*Command)(CommandEntry* commandEntry);
597
Jeff Brown51d45a72010-06-17 20:52:56 -0700598 class Connection;
Jeff Brown54bc2812010-06-15 01:31:58 -0700599 struct CommandEntry : Link<CommandEntry> {
600 CommandEntry();
601 ~CommandEntry();
602
603 Command command;
604
605 // parameters for the command (usage varies by command)
Jeff Brown51d45a72010-06-17 20:52:56 -0700606 sp<Connection> connection;
Jeff Browna665ca82010-09-08 11:49:43 -0700607 nsecs_t eventTime;
608 KeyEntry* keyEntry;
609 sp<InputChannel> inputChannel;
610 sp<InputApplicationHandle> inputApplicationHandle;
Jeff Browna665ca82010-09-08 11:49:43 -0700611 int32_t userActivityEventType;
Jeff Brown54bc2812010-06-15 01:31:58 -0700612 };
613
614 // Generic queue implementation.
Jeff Browne839a582010-04-22 18:58:52 -0700615 template <typename T>
616 struct Queue {
Jeff Browna665ca82010-09-08 11:49:43 -0700617 T headSentinel;
618 T tailSentinel;
Jeff Browne839a582010-04-22 18:58:52 -0700619
620 inline Queue() {
Jeff Browna665ca82010-09-08 11:49:43 -0700621 headSentinel.prev = NULL;
622 headSentinel.next = & tailSentinel;
623 tailSentinel.prev = & headSentinel;
624 tailSentinel.next = NULL;
Jeff Browne839a582010-04-22 18:58:52 -0700625 }
626
Jeff Browna665ca82010-09-08 11:49:43 -0700627 inline bool isEmpty() const {
628 return headSentinel.next == & tailSentinel;
Jeff Browne839a582010-04-22 18:58:52 -0700629 }
630
631 inline void enqueueAtTail(T* entry) {
Jeff Browna665ca82010-09-08 11:49:43 -0700632 T* last = tailSentinel.prev;
Jeff Browne839a582010-04-22 18:58:52 -0700633 last->next = entry;
634 entry->prev = last;
Jeff Browna665ca82010-09-08 11:49:43 -0700635 entry->next = & tailSentinel;
636 tailSentinel.prev = entry;
Jeff Browne839a582010-04-22 18:58:52 -0700637 }
638
639 inline void enqueueAtHead(T* entry) {
Jeff Browna665ca82010-09-08 11:49:43 -0700640 T* first = headSentinel.next;
641 headSentinel.next = entry;
642 entry->prev = & headSentinel;
Jeff Browne839a582010-04-22 18:58:52 -0700643 entry->next = first;
644 first->prev = entry;
645 }
646
647 inline void dequeue(T* entry) {
648 entry->prev->next = entry->next;
649 entry->next->prev = entry->prev;
650 }
651
652 inline T* dequeueAtHead() {
Jeff Browna665ca82010-09-08 11:49:43 -0700653 T* first = headSentinel.next;
Jeff Browne839a582010-04-22 18:58:52 -0700654 dequeue(first);
655 return first;
656 }
Jeff Brown53a415e2010-09-15 15:18:56 -0700657
658 uint32_t count() const;
Jeff Browne839a582010-04-22 18:58:52 -0700659 };
660
661 /* Allocates queue entries and performs reference counting as needed. */
662 class Allocator {
663 public:
664 Allocator();
665
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700666 InjectionState* obtainInjectionState(int32_t injectorPid, int32_t injectorUid);
Jeff Brown51d45a72010-06-17 20:52:56 -0700667 ConfigurationChangedEntry* obtainConfigurationChangedEntry(nsecs_t eventTime);
668 KeyEntry* obtainKeyEntry(nsecs_t eventTime,
Jeff Brown5c1ed842010-07-14 18:48:53 -0700669 int32_t deviceId, int32_t source, uint32_t policyFlags, int32_t action,
Jeff Brown51d45a72010-06-17 20:52:56 -0700670 int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState,
671 int32_t repeatCount, nsecs_t downTime);
672 MotionEntry* obtainMotionEntry(nsecs_t eventTime,
Jeff Brown5c1ed842010-07-14 18:48:53 -0700673 int32_t deviceId, int32_t source, uint32_t policyFlags, int32_t action,
Jeff Brownaf30ff62010-09-01 17:01:00 -0700674 int32_t flags, int32_t metaState, int32_t edgeFlags,
675 float xPrecision, float yPrecision,
Jeff Brown51d45a72010-06-17 20:52:56 -0700676 nsecs_t downTime, uint32_t pointerCount,
677 const int32_t* pointerIds, const PointerCoords* pointerCoords);
Jeff Browna665ca82010-09-08 11:49:43 -0700678 DispatchEntry* obtainDispatchEntry(EventEntry* eventEntry,
Jeff Brown53a415e2010-09-15 15:18:56 -0700679 int32_t targetFlags, float xOffset, float yOffset);
Jeff Brown54bc2812010-06-15 01:31:58 -0700680 CommandEntry* obtainCommandEntry(Command command);
Jeff Browne839a582010-04-22 18:58:52 -0700681
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700682 void releaseInjectionState(InjectionState* injectionState);
Jeff Browne839a582010-04-22 18:58:52 -0700683 void releaseEventEntry(EventEntry* entry);
684 void releaseConfigurationChangedEntry(ConfigurationChangedEntry* entry);
685 void releaseKeyEntry(KeyEntry* entry);
686 void releaseMotionEntry(MotionEntry* entry);
687 void releaseDispatchEntry(DispatchEntry* entry);
Jeff Brown54bc2812010-06-15 01:31:58 -0700688 void releaseCommandEntry(CommandEntry* entry);
Jeff Browne839a582010-04-22 18:58:52 -0700689
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700690 void recycleKeyEntry(KeyEntry* entry);
691
Jeff Browne839a582010-04-22 18:58:52 -0700692 void appendMotionSample(MotionEntry* motionEntry,
Jeff Brown51d45a72010-06-17 20:52:56 -0700693 nsecs_t eventTime, const PointerCoords* pointerCoords);
Jeff Browne839a582010-04-22 18:58:52 -0700694
695 private:
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700696 Pool<InjectionState> mInjectionStatePool;
Jeff Browne839a582010-04-22 18:58:52 -0700697 Pool<ConfigurationChangedEntry> mConfigurationChangeEntryPool;
698 Pool<KeyEntry> mKeyEntryPool;
699 Pool<MotionEntry> mMotionEntryPool;
700 Pool<MotionSample> mMotionSamplePool;
701 Pool<DispatchEntry> mDispatchEntryPool;
Jeff Brown54bc2812010-06-15 01:31:58 -0700702 Pool<CommandEntry> mCommandEntryPool;
Jeff Brown51d45a72010-06-17 20:52:56 -0700703
Jeff Brown90f0cee2010-10-08 22:31:17 -0700704 void initializeEventEntry(EventEntry* entry, int32_t type, nsecs_t eventTime,
705 uint32_t policyFlags);
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700706 void releaseEventEntryInjectionState(EventEntry* entry);
Jeff Browne839a582010-04-22 18:58:52 -0700707 };
708
Jeff Browna665ca82010-09-08 11:49:43 -0700709 /* Tracks dispatched key and motion event state so that cancelation events can be
710 * synthesized when events are dropped. */
711 class InputState {
712 public:
713 // Specifies whether a given event will violate input state consistency.
714 enum Consistency {
715 // The event is consistent with the current input state.
716 CONSISTENT,
717 // The event is inconsistent with the current input state but applications
718 // will tolerate it. eg. Down followed by another down.
719 TOLERABLE,
720 // The event is inconsistent with the current input state and will probably
721 // cause applications to crash. eg. Up without prior down, move with
722 // unexpected number of pointers.
723 BROKEN
724 };
725
Jeff Brown90f0cee2010-10-08 22:31:17 -0700726 // Specifies the sources to cancel.
727 enum CancelationOptions {
728 CANCEL_ALL_EVENTS = 0,
729 CANCEL_POINTER_EVENTS = 1,
730 CANCEL_NON_POINTER_EVENTS = 2,
731 };
732
Jeff Browna665ca82010-09-08 11:49:43 -0700733 InputState();
734 ~InputState();
735
736 // Returns true if there is no state to be canceled.
737 bool isNeutral() const;
738
Jeff Browna665ca82010-09-08 11:49:43 -0700739 // Records tracking information for an event that has just been published.
740 // Returns whether the event is consistent with the current input state.
741 Consistency trackEvent(const EventEntry* entry);
742
743 // Records tracking information for a key event that has just been published.
744 // Returns whether the event is consistent with the current input state.
745 Consistency trackKey(const KeyEntry* entry);
746
747 // Records tracking information for a motion event that has just been published.
748 // Returns whether the event is consistent with the current input state.
749 Consistency trackMotion(const MotionEntry* entry);
750
Jeff Brown90f0cee2010-10-08 22:31:17 -0700751 // Synthesizes cancelation events for the current state and resets the tracked state.
752 void synthesizeCancelationEvents(nsecs_t currentTime, Allocator* allocator,
753 Vector<EventEntry*>& outEvents, CancelationOptions options);
Jeff Browna665ca82010-09-08 11:49:43 -0700754
755 // Clears the current state.
756 void clear();
757
Jeff Brownb6702e52010-10-11 18:32:20 -0700758 // Copies pointer-related parts of the input state to another instance.
759 void copyPointerStateTo(InputState& other) const;
760
Jeff Browna665ca82010-09-08 11:49:43 -0700761 private:
Jeff Browna665ca82010-09-08 11:49:43 -0700762 struct KeyMemento {
763 int32_t deviceId;
764 int32_t source;
765 int32_t keyCode;
766 int32_t scanCode;
767 nsecs_t downTime;
768 };
769
770 struct MotionMemento {
771 int32_t deviceId;
772 int32_t source;
773 float xPrecision;
774 float yPrecision;
775 nsecs_t downTime;
776 uint32_t pointerCount;
777 int32_t pointerIds[MAX_POINTERS];
778 PointerCoords pointerCoords[MAX_POINTERS];
779
780 void setPointers(const MotionEntry* entry);
781 };
782
783 Vector<KeyMemento> mKeyMementos;
784 Vector<MotionMemento> mMotionMementos;
Jeff Brown90f0cee2010-10-08 22:31:17 -0700785
786 static bool shouldCancelEvent(int32_t eventSource, CancelationOptions options);
Jeff Browna665ca82010-09-08 11:49:43 -0700787 };
788
Jeff Browne839a582010-04-22 18:58:52 -0700789 /* Manages the dispatch state associated with a single input channel. */
790 class Connection : public RefBase {
791 protected:
792 virtual ~Connection();
793
794 public:
795 enum Status {
796 // Everything is peachy.
797 STATUS_NORMAL,
798 // An unrecoverable communication error has occurred.
799 STATUS_BROKEN,
Jeff Browne839a582010-04-22 18:58:52 -0700800 // The input channel has been unregistered.
801 STATUS_ZOMBIE
802 };
803
804 Status status;
805 sp<InputChannel> inputChannel;
806 InputPublisher inputPublisher;
Jeff Browna665ca82010-09-08 11:49:43 -0700807 InputState inputState;
Jeff Browne839a582010-04-22 18:58:52 -0700808 Queue<DispatchEntry> outboundQueue;
Jeff Browne839a582010-04-22 18:58:52 -0700809
810 nsecs_t lastEventTime; // the time when the event was originally captured
811 nsecs_t lastDispatchTime; // the time when the last event was dispatched
Jeff Browne839a582010-04-22 18:58:52 -0700812
813 explicit Connection(const sp<InputChannel>& inputChannel);
814
Jeff Brown54bc2812010-06-15 01:31:58 -0700815 inline const char* getInputChannelName() const { return inputChannel->getName().string(); }
816
817 const char* getStatusLabel() const;
Jeff Browne839a582010-04-22 18:58:52 -0700818
819 // Finds a DispatchEntry in the outbound queue associated with the specified event.
820 // Returns NULL if not found.
821 DispatchEntry* findQueuedDispatchEntryForEvent(const EventEntry* eventEntry) const;
822
Jeff Browne839a582010-04-22 18:58:52 -0700823 // Gets the time since the current event was originally obtained from the input driver.
Jeff Browna665ca82010-09-08 11:49:43 -0700824 inline double getEventLatencyMillis(nsecs_t currentTime) const {
Jeff Browne839a582010-04-22 18:58:52 -0700825 return (currentTime - lastEventTime) / 1000000.0;
826 }
827
828 // Gets the time since the current event entered the outbound dispatch queue.
Jeff Browna665ca82010-09-08 11:49:43 -0700829 inline double getDispatchLatencyMillis(nsecs_t currentTime) const {
Jeff Browne839a582010-04-22 18:58:52 -0700830 return (currentTime - lastDispatchTime) / 1000000.0;
831 }
832
Jeff Browne839a582010-04-22 18:58:52 -0700833 status_t initialize();
834 };
835
Jeff Brown90f0cee2010-10-08 22:31:17 -0700836 enum DropReason {
837 DROP_REASON_NOT_DROPPED = 0,
838 DROP_REASON_POLICY = 1,
839 DROP_REASON_APP_SWITCH = 2,
840 DROP_REASON_DISABLED = 3,
841 };
842
Jeff Brown54bc2812010-06-15 01:31:58 -0700843 sp<InputDispatcherPolicyInterface> mPolicy;
Jeff Browne839a582010-04-22 18:58:52 -0700844
845 Mutex mLock;
846
Jeff Browne839a582010-04-22 18:58:52 -0700847 Allocator mAllocator;
Jeff Brown59abe7e2010-09-13 23:17:30 -0700848 sp<Looper> mLooper;
Jeff Browne839a582010-04-22 18:58:52 -0700849
Jeff Browna665ca82010-09-08 11:49:43 -0700850 EventEntry* mPendingEvent;
Jeff Brown54bc2812010-06-15 01:31:58 -0700851 Queue<EventEntry> mInboundQueue;
852 Queue<CommandEntry> mCommandQueue;
853
Jeff Browna665ca82010-09-08 11:49:43 -0700854 Vector<EventEntry*> mTempCancelationEvents;
855
856 void dispatchOnceInnerLocked(nsecs_t keyRepeatTimeout, nsecs_t keyRepeatDelay,
857 nsecs_t* nextWakeupTime);
858
Jeff Brown59abe7e2010-09-13 23:17:30 -0700859 // Enqueues an inbound event. Returns true if mLooper->wake() should be called.
Jeff Browna665ca82010-09-08 11:49:43 -0700860 bool enqueueInboundEventLocked(EventEntry* entry);
861
Jeff Brown90f0cee2010-10-08 22:31:17 -0700862 // Cleans up input state when dropping an inbound event.
863 void dropInboundEventLocked(EventEntry* entry, DropReason dropReason);
864
Jeff Browna665ca82010-09-08 11:49:43 -0700865 // App switch latency optimization.
Jeff Brown90f0cee2010-10-08 22:31:17 -0700866 bool mAppSwitchSawKeyDown;
Jeff Browna665ca82010-09-08 11:49:43 -0700867 nsecs_t mAppSwitchDueTime;
868
Jeff Brown90f0cee2010-10-08 22:31:17 -0700869 static bool isAppSwitchKeyCode(int32_t keyCode);
870 bool isAppSwitchKeyEventLocked(KeyEntry* keyEntry);
Jeff Browna665ca82010-09-08 11:49:43 -0700871 bool isAppSwitchPendingLocked();
Jeff Browna665ca82010-09-08 11:49:43 -0700872 void resetPendingAppSwitchLocked(bool handled);
873
Jeff Browne839a582010-04-22 18:58:52 -0700874 // All registered connections mapped by receive pipe file descriptor.
875 KeyedVector<int, sp<Connection> > mConnectionsByReceiveFd;
876
Jeff Brown53a415e2010-09-15 15:18:56 -0700877 ssize_t getConnectionIndexLocked(const sp<InputChannel>& inputChannel);
Jeff Brown0cacb872010-08-17 15:59:26 -0700878
Jeff Browne839a582010-04-22 18:58:52 -0700879 // Active connections are connections that have a non-empty outbound queue.
Jeff Brown51d45a72010-06-17 20:52:56 -0700880 // We don't use a ref-counted pointer here because we explicitly abort connections
881 // during unregistration which causes the connection's outbound queue to be cleared
882 // and the connection itself to be deactivated.
Jeff Browne839a582010-04-22 18:58:52 -0700883 Vector<Connection*> mActiveConnections;
884
Jeff Browna665ca82010-09-08 11:49:43 -0700885 // Input channels that will receive a copy of all input events.
886 Vector<sp<InputChannel> > mMonitoringChannels;
Jeff Browne839a582010-04-22 18:58:52 -0700887
Jeff Browna665ca82010-09-08 11:49:43 -0700888 // Preallocated key event object used for policy inquiries.
889 KeyEvent mReusableKeyEvent;
Jeff Browne839a582010-04-22 18:58:52 -0700890
Jeff Brown51d45a72010-06-17 20:52:56 -0700891 // Event injection and synchronization.
892 Condition mInjectionResultAvailableCondition;
Jeff Brown90f0cee2010-10-08 22:31:17 -0700893 bool hasInjectionPermission(int32_t injectorPid, int32_t injectorUid);
Jeff Brown51d45a72010-06-17 20:52:56 -0700894 void setInjectionResultLocked(EventEntry* entry, int32_t injectionResult);
895
Jeff Brownf67c53e2010-07-28 15:48:59 -0700896 Condition mInjectionSyncFinishedCondition;
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700897 void incrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brown53a415e2010-09-15 15:18:56 -0700898 void decrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brownf67c53e2010-07-28 15:48:59 -0700899
Jeff Brown542412c2010-08-18 15:51:08 -0700900 // Throttling state.
901 struct ThrottleState {
902 nsecs_t minTimeBetweenEvents;
903
904 nsecs_t lastEventTime;
905 int32_t lastDeviceId;
906 uint32_t lastSource;
907
908 uint32_t originalSampleCount; // only collected during debugging
909 } mThrottleState;
910
Jeff Browne839a582010-04-22 18:58:52 -0700911 // Key repeat tracking.
Jeff Browne839a582010-04-22 18:58:52 -0700912 struct KeyRepeatState {
913 KeyEntry* lastKeyEntry; // or null if no repeat
914 nsecs_t nextRepeatTime;
915 } mKeyRepeatState;
916
917 void resetKeyRepeatLocked();
Jeff Browna665ca82010-09-08 11:49:43 -0700918 KeyEntry* synthesizeKeyRepeatLocked(nsecs_t currentTime, nsecs_t keyRepeatTimeout);
Jeff Browne839a582010-04-22 18:58:52 -0700919
Jeff Brown54bc2812010-06-15 01:31:58 -0700920 // Deferred command processing.
921 bool runCommandsLockedInterruptible();
922 CommandEntry* postCommandLocked(Command command);
923
Jeff Browna665ca82010-09-08 11:49:43 -0700924 // Inbound event processing.
925 void drainInboundQueueLocked();
Jeff Brownd8816c32010-09-16 14:07:33 -0700926 void releasePendingEventLocked();
927 void releaseInboundEventLocked(EventEntry* entry);
Jeff Browne839a582010-04-22 18:58:52 -0700928
Jeff Browna665ca82010-09-08 11:49:43 -0700929 // Dispatch state.
930 bool mDispatchEnabled;
931 bool mDispatchFrozen;
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700932
Jeff Browna665ca82010-09-08 11:49:43 -0700933 Vector<InputWindow> mWindows;
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700934
935 const InputWindow* getWindowLocked(const sp<InputChannel>& inputChannel);
Jeff Browna665ca82010-09-08 11:49:43 -0700936
937 // Focus tracking for keys, trackball, etc.
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700938 const InputWindow* mFocusedWindow;
Jeff Browna665ca82010-09-08 11:49:43 -0700939
940 // Focus tracking for touch.
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700941 struct TouchedWindow {
942 const InputWindow* window;
943 int32_t targetFlags;
944 BitSet32 pointerIds;
945 sp<InputChannel> channel;
Jeff Browna665ca82010-09-08 11:49:43 -0700946 };
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700947 struct TouchState {
948 bool down;
949 bool split;
950 Vector<TouchedWindow> windows;
951
952 TouchState();
953 ~TouchState();
954 void reset();
955 void copyFrom(const TouchState& other);
956 void addOrUpdateWindow(const InputWindow* window, int32_t targetFlags, BitSet32 pointerIds);
957 void removeOutsideTouchWindows();
958 const InputWindow* getFirstForegroundWindow();
959 };
960
961 TouchState mTouchState;
962 TouchState mTempTouchState;
Jeff Browna665ca82010-09-08 11:49:43 -0700963
964 // Focused application.
965 InputApplication* mFocusedApplication;
966 InputApplication mFocusedApplicationStorage; // preallocated storage for mFocusedApplication
967 void releaseFocusedApplicationLocked();
968
969 // Dispatch inbound events.
970 bool dispatchConfigurationChangedLocked(
971 nsecs_t currentTime, ConfigurationChangedEntry* entry);
972 bool dispatchKeyLocked(
973 nsecs_t currentTime, KeyEntry* entry, nsecs_t keyRepeatTimeout,
Jeff Brown33d54ce2010-10-11 14:20:19 -0700974 DropReason* dropReason, nsecs_t* nextWakeupTime);
Jeff Browna665ca82010-09-08 11:49:43 -0700975 bool dispatchMotionLocked(
976 nsecs_t currentTime, MotionEntry* entry,
Jeff Brown33d54ce2010-10-11 14:20:19 -0700977 DropReason* dropReason, nsecs_t* nextWakeupTime);
Jeff Brown54bc2812010-06-15 01:31:58 -0700978 void dispatchEventToCurrentInputTargetsLocked(
979 nsecs_t currentTime, EventEntry* entry, bool resumeWithAppendedMotionSample);
Jeff Browne839a582010-04-22 18:58:52 -0700980
Jeff Browna665ca82010-09-08 11:49:43 -0700981 void logOutboundKeyDetailsLocked(const char* prefix, const KeyEntry* entry);
982 void logOutboundMotionDetailsLocked(const char* prefix, const MotionEntry* entry);
983
984 // The input targets that were most recently identified for dispatch.
Jeff Browna665ca82010-09-08 11:49:43 -0700985 bool mCurrentInputTargetsValid; // false while targets are being recomputed
986 Vector<InputTarget> mCurrentInputTargets;
Jeff Browna665ca82010-09-08 11:49:43 -0700987
988 enum InputTargetWaitCause {
989 INPUT_TARGET_WAIT_CAUSE_NONE,
990 INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY,
991 INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY,
992 };
993
994 InputTargetWaitCause mInputTargetWaitCause;
995 nsecs_t mInputTargetWaitStartTime;
996 nsecs_t mInputTargetWaitTimeoutTime;
997 bool mInputTargetWaitTimeoutExpired;
998
999 // Finding targets for input events.
Jeff Brownd8816c32010-09-16 14:07:33 -07001000 void resetTargetsLocked();
Jeff Brownd1b0a2b2010-09-26 22:20:12 -07001001 void commitTargetsLocked();
Jeff Browna665ca82010-09-08 11:49:43 -07001002 int32_t handleTargetsNotReadyLocked(nsecs_t currentTime, const EventEntry* entry,
1003 const InputApplication* application, const InputWindow* window,
1004 nsecs_t* nextWakeupTime);
Jeff Brown53a415e2010-09-15 15:18:56 -07001005 void resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout,
1006 const sp<InputChannel>& inputChannel);
1007 nsecs_t getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime);
Jeff Browna665ca82010-09-08 11:49:43 -07001008 void resetANRTimeoutsLocked();
1009
Jeff Brownd1b0a2b2010-09-26 22:20:12 -07001010 int32_t findFocusedWindowTargetsLocked(nsecs_t currentTime, const EventEntry* entry,
1011 nsecs_t* nextWakeupTime);
1012 int32_t findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry* entry,
1013 nsecs_t* nextWakeupTime);
Jeff Browna665ca82010-09-08 11:49:43 -07001014
Jeff Brownd1b0a2b2010-09-26 22:20:12 -07001015 void addWindowTargetLocked(const InputWindow* window, int32_t targetFlags,
1016 BitSet32 pointerIds);
Jeff Browna665ca82010-09-08 11:49:43 -07001017 void addMonitoringTargetsLocked();
Jeff Brownef3a8232010-10-18 13:21:23 -07001018 void pokeUserActivityLocked(const EventEntry* eventEntry);
Jeff Brownd1b0a2b2010-09-26 22:20:12 -07001019 bool checkInjectionPermission(const InputWindow* window, const InjectionState* injectionState);
Jeff Brown35cf0e92010-10-05 12:26:23 -07001020 bool isWindowObscuredAtPointLocked(const InputWindow* window, int32_t x, int32_t y) const;
Jeff Brown53a415e2010-09-15 15:18:56 -07001021 bool isWindowFinishedWithPreviousInputLocked(const InputWindow* window);
Jeff Brown53a415e2010-09-15 15:18:56 -07001022 String8 getApplicationWindowLabelLocked(const InputApplication* application,
1023 const InputWindow* window);
Jeff Browna665ca82010-09-08 11:49:43 -07001024
Jeff Browne839a582010-04-22 18:58:52 -07001025 // Manage the dispatch cycle for a single connection.
Jeff Brown51d45a72010-06-17 20:52:56 -07001026 // These methods are deliberately not Interruptible because doing all of the work
1027 // with the mutex held makes it easier to ensure that connection invariants are maintained.
1028 // If needed, the methods post commands to run later once the critical bits are done.
1029 void prepareDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Jeff Browne839a582010-04-22 18:58:52 -07001030 EventEntry* eventEntry, const InputTarget* inputTarget,
1031 bool resumeWithAppendedMotionSample);
Jeff Brown53a415e2010-09-15 15:18:56 -07001032 void startDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown51d45a72010-06-17 20:52:56 -07001033 void finishDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Browna665ca82010-09-08 11:49:43 -07001034 void startNextDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown90f0cee2010-10-08 22:31:17 -07001035 void abortBrokenDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown53a415e2010-09-15 15:18:56 -07001036 void drainOutboundQueueLocked(Connection* connection);
Jeff Brown59abe7e2010-09-13 23:17:30 -07001037 static int handleReceiveCallback(int receiveFd, int events, void* data);
Jeff Browne839a582010-04-22 18:58:52 -07001038
Jeff Brown90f0cee2010-10-08 22:31:17 -07001039 void synthesizeCancelationEventsForAllConnectionsLocked(
1040 InputState::CancelationOptions options, const char* reason);
1041 void synthesizeCancelationEventsForInputChannelLocked(const sp<InputChannel>& channel,
1042 InputState::CancelationOptions options, const char* reason);
1043 void synthesizeCancelationEventsForConnectionLocked(const sp<Connection>& connection,
1044 InputState::CancelationOptions options, const char* reason);
1045
Jeff Brownd1b0a2b2010-09-26 22:20:12 -07001046 // Splitting motion events across windows.
1047 MotionEntry* splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds);
1048
Jeff Brownfef5b042010-10-27 18:43:51 -07001049 // Reset and drop everything the dispatcher is doing.
1050 void resetAndDropEverythingLocked(const char* reason);
1051
Jeff Browna665ca82010-09-08 11:49:43 -07001052 // Dump state.
1053 void dumpDispatchStateLocked(String8& dump);
1054 void logDispatchStateLocked();
1055
Jeff Browne839a582010-04-22 18:58:52 -07001056 // Add or remove a connection to the mActiveConnections vector.
1057 void activateConnectionLocked(Connection* connection);
1058 void deactivateConnectionLocked(Connection* connection);
1059
1060 // Interesting events that we might like to log or tell the framework about.
Jeff Brown54bc2812010-06-15 01:31:58 -07001061 void onDispatchCycleStartedLocked(
Jeff Brown51d45a72010-06-17 20:52:56 -07001062 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown54bc2812010-06-15 01:31:58 -07001063 void onDispatchCycleFinishedLocked(
Jeff Brown51d45a72010-06-17 20:52:56 -07001064 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown54bc2812010-06-15 01:31:58 -07001065 void onDispatchCycleBrokenLocked(
Jeff Brown51d45a72010-06-17 20:52:56 -07001066 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown53a415e2010-09-15 15:18:56 -07001067 void onANRLocked(
1068 nsecs_t currentTime, const InputApplication* application, const InputWindow* window,
1069 nsecs_t eventTime, nsecs_t waitStartTime);
Jeff Brown54bc2812010-06-15 01:31:58 -07001070
Jeff Brown51d45a72010-06-17 20:52:56 -07001071 // Outbound policy interactions.
Jeff Browna665ca82010-09-08 11:49:43 -07001072 void doNotifyConfigurationChangedInterruptible(CommandEntry* commandEntry);
Jeff Brown54bc2812010-06-15 01:31:58 -07001073 void doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown53a415e2010-09-15 15:18:56 -07001074 void doNotifyANRLockedInterruptible(CommandEntry* commandEntry);
Jeff Browna665ca82010-09-08 11:49:43 -07001075 void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry);
1076 void doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown53a415e2010-09-15 15:18:56 -07001077
1078 // Statistics gathering.
1079 void updateDispatchStatisticsLocked(nsecs_t currentTime, const EventEntry* entry,
1080 int32_t injectionResult, nsecs_t timeSpentWaitingForApplication);
Jeff Browne839a582010-04-22 18:58:52 -07001081};
1082
1083/* Enqueues and dispatches input events, endlessly. */
1084class InputDispatcherThread : public Thread {
1085public:
1086 explicit InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher);
1087 ~InputDispatcherThread();
1088
1089private:
1090 virtual bool threadLoop();
1091
1092 sp<InputDispatcherInterface> mDispatcher;
1093};
1094
1095} // namespace android
1096
Jeff Browna665ca82010-09-08 11:49:43 -07001097#endif // _UI_INPUT_DISPATCHER_H