blob: b811ace2561e2300df4563b31fe5b66dd1d943e0 [file] [log] [blame]
Jeff Browne839a582010-04-22 18:58:52 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _UI_INPUT_DISPATCHER_H
18#define _UI_INPUT_DISPATCHER_H
19
20#include <ui/Input.h>
Jeff Browne839a582010-04-22 18:58:52 -070021#include <ui/InputTransport.h>
22#include <utils/KeyedVector.h>
23#include <utils/Vector.h>
24#include <utils/threads.h>
25#include <utils/Timers.h>
26#include <utils/RefBase.h>
27#include <utils/String8.h>
Jeff Brown59abe7e2010-09-13 23:17:30 -070028#include <utils/Looper.h>
Jeff Browne839a582010-04-22 18:58:52 -070029#include <utils/Pool.h>
Jeff Brownd1b0a2b2010-09-26 22:20:12 -070030#include <utils/BitSet.h>
Jeff Browne839a582010-04-22 18:58:52 -070031
32#include <stddef.h>
33#include <unistd.h>
Jeff Browna665ca82010-09-08 11:49:43 -070034#include <limits.h>
Jeff Browne839a582010-04-22 18:58:52 -070035
36
37namespace android {
38
Jeff Brown54bc2812010-06-15 01:31:58 -070039/*
Jeff Brown51d45a72010-06-17 20:52:56 -070040 * Constants used to report the outcome of input event injection.
41 */
42enum {
43 /* (INTERNAL USE ONLY) Specifies that injection is pending and its outcome is unknown. */
44 INPUT_EVENT_INJECTION_PENDING = -1,
45
46 /* Injection succeeded. */
47 INPUT_EVENT_INJECTION_SUCCEEDED = 0,
48
49 /* Injection failed because the injector did not have permission to inject
50 * into the application with input focus. */
51 INPUT_EVENT_INJECTION_PERMISSION_DENIED = 1,
52
53 /* Injection failed because there were no available input targets. */
54 INPUT_EVENT_INJECTION_FAILED = 2,
55
56 /* Injection failed due to a timeout. */
57 INPUT_EVENT_INJECTION_TIMED_OUT = 3
58};
59
Jeff Brownf67c53e2010-07-28 15:48:59 -070060/*
61 * Constants used to determine the input event injection synchronization mode.
62 */
63enum {
64 /* Injection is asynchronous and is assumed always to be successful. */
65 INPUT_EVENT_INJECTION_SYNC_NONE = 0,
66
67 /* Waits for previous events to be dispatched so that the input dispatcher can determine
68 * whether input event injection willbe permitted based on the current input focus.
69 * Does not wait for the input event to finish processing. */
70 INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT = 1,
71
72 /* Waits for the input event to be completely processed. */
73 INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISHED = 2,
74};
75
Jeff Brown51d45a72010-06-17 20:52:56 -070076
77/*
Jeff Brown54bc2812010-06-15 01:31:58 -070078 * An input target specifies how an input event is to be dispatched to a particular window
79 * including the window's input channel, control flags, a timeout, and an X / Y offset to
80 * be added to input event coordinates to compensate for the absolute position of the
81 * window area.
82 */
83struct InputTarget {
84 enum {
Jeff Brown53a415e2010-09-15 15:18:56 -070085 /* This flag indicates that the event is being delivered to a foreground application. */
86 FLAG_FOREGROUND = 0x01,
Jeff Brown54bc2812010-06-15 01:31:58 -070087
Jeff Brownaf30ff62010-09-01 17:01:00 -070088 /* This flag indicates that a MotionEvent with AMOTION_EVENT_ACTION_DOWN falls outside
89 * of the area of this target and so should instead be delivered as an
90 * AMOTION_EVENT_ACTION_OUTSIDE to this target. */
Jeff Brown54bc2812010-06-15 01:31:58 -070091 FLAG_OUTSIDE = 0x02,
92
Jeff Brownaf30ff62010-09-01 17:01:00 -070093 /* This flag indicates that the target of a MotionEvent is partly or wholly
94 * obscured by another visible window above it. The motion event should be
95 * delivered with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED. */
Jeff Brownd1b0a2b2010-09-26 22:20:12 -070096 FLAG_WINDOW_IS_OBSCURED = 0x04,
97
98 /* This flag indicates that a motion event is being split across multiple windows. */
99 FLAG_SPLIT = 0x08,
Jeff Brown54bc2812010-06-15 01:31:58 -0700100 };
101
102 // The input channel to be targeted.
103 sp<InputChannel> inputChannel;
104
105 // Flags for the input target.
106 int32_t flags;
107
Jeff Brown54bc2812010-06-15 01:31:58 -0700108 // The x and y offset to add to a MotionEvent as it is delivered.
109 // (ignored for KeyEvents)
110 float xOffset, yOffset;
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700111
112 // The window type of the input target.
113 int32_t windowType;
114
115 // The subset of pointer ids to include in motion events dispatched to this input target
116 // if FLAG_SPLIT is set.
117 BitSet32 pointerIds;
Jeff Brown54bc2812010-06-15 01:31:58 -0700118};
119
Jeff Brown51d45a72010-06-17 20:52:56 -0700120
Jeff Brown54bc2812010-06-15 01:31:58 -0700121/*
Jeff Browna665ca82010-09-08 11:49:43 -0700122 * An input window describes the bounds of a window that can receive input.
123 */
124struct InputWindow {
125 // Window flags from WindowManager.LayoutParams
126 enum {
127 FLAG_ALLOW_LOCK_WHILE_SCREEN_ON = 0x00000001,
128 FLAG_DIM_BEHIND = 0x00000002,
129 FLAG_BLUR_BEHIND = 0x00000004,
130 FLAG_NOT_FOCUSABLE = 0x00000008,
131 FLAG_NOT_TOUCHABLE = 0x00000010,
132 FLAG_NOT_TOUCH_MODAL = 0x00000020,
133 FLAG_TOUCHABLE_WHEN_WAKING = 0x00000040,
134 FLAG_KEEP_SCREEN_ON = 0x00000080,
135 FLAG_LAYOUT_IN_SCREEN = 0x00000100,
136 FLAG_LAYOUT_NO_LIMITS = 0x00000200,
137 FLAG_FULLSCREEN = 0x00000400,
138 FLAG_FORCE_NOT_FULLSCREEN = 0x00000800,
139 FLAG_DITHER = 0x00001000,
140 FLAG_SECURE = 0x00002000,
141 FLAG_SCALED = 0x00004000,
142 FLAG_IGNORE_CHEEK_PRESSES = 0x00008000,
143 FLAG_LAYOUT_INSET_DECOR = 0x00010000,
144 FLAG_ALT_FOCUSABLE_IM = 0x00020000,
145 FLAG_WATCH_OUTSIDE_TOUCH = 0x00040000,
146 FLAG_SHOW_WHEN_LOCKED = 0x00080000,
147 FLAG_SHOW_WALLPAPER = 0x00100000,
148 FLAG_TURN_SCREEN_ON = 0x00200000,
149 FLAG_DISMISS_KEYGUARD = 0x00400000,
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700150 FLAG_SPLIT_TOUCH = 0x00800000,
Jeff Browna665ca82010-09-08 11:49:43 -0700151 FLAG_KEEP_SURFACE_WHILE_ANIMATING = 0x10000000,
152 FLAG_COMPATIBLE_WINDOW = 0x20000000,
153 FLAG_SYSTEM_ERROR = 0x40000000,
154 };
155
156 // Window types from WindowManager.LayoutParams
157 enum {
158 FIRST_APPLICATION_WINDOW = 1,
159 TYPE_BASE_APPLICATION = 1,
160 TYPE_APPLICATION = 2,
161 TYPE_APPLICATION_STARTING = 3,
162 LAST_APPLICATION_WINDOW = 99,
163 FIRST_SUB_WINDOW = 1000,
164 TYPE_APPLICATION_PANEL = FIRST_SUB_WINDOW,
165 TYPE_APPLICATION_MEDIA = FIRST_SUB_WINDOW+1,
166 TYPE_APPLICATION_SUB_PANEL = FIRST_SUB_WINDOW+2,
167 TYPE_APPLICATION_ATTACHED_DIALOG = FIRST_SUB_WINDOW+3,
168 TYPE_APPLICATION_MEDIA_OVERLAY = FIRST_SUB_WINDOW+4,
169 LAST_SUB_WINDOW = 1999,
170 FIRST_SYSTEM_WINDOW = 2000,
171 TYPE_STATUS_BAR = FIRST_SYSTEM_WINDOW,
172 TYPE_SEARCH_BAR = FIRST_SYSTEM_WINDOW+1,
173 TYPE_PHONE = FIRST_SYSTEM_WINDOW+2,
174 TYPE_SYSTEM_ALERT = FIRST_SYSTEM_WINDOW+3,
175 TYPE_KEYGUARD = FIRST_SYSTEM_WINDOW+4,
176 TYPE_TOAST = FIRST_SYSTEM_WINDOW+5,
177 TYPE_SYSTEM_OVERLAY = FIRST_SYSTEM_WINDOW+6,
178 TYPE_PRIORITY_PHONE = FIRST_SYSTEM_WINDOW+7,
179 TYPE_SYSTEM_DIALOG = FIRST_SYSTEM_WINDOW+8,
180 TYPE_KEYGUARD_DIALOG = FIRST_SYSTEM_WINDOW+9,
181 TYPE_SYSTEM_ERROR = FIRST_SYSTEM_WINDOW+10,
182 TYPE_INPUT_METHOD = FIRST_SYSTEM_WINDOW+11,
183 TYPE_INPUT_METHOD_DIALOG= FIRST_SYSTEM_WINDOW+12,
184 TYPE_WALLPAPER = FIRST_SYSTEM_WINDOW+13,
185 TYPE_STATUS_BAR_PANEL = FIRST_SYSTEM_WINDOW+14,
Jeff Browne68d9e02010-10-15 00:54:27 -0700186 TYPE_SECURE_SYSTEM_OVERLAY = FIRST_SYSTEM_WINDOW+15,
Jeff Browna665ca82010-09-08 11:49:43 -0700187 LAST_SYSTEM_WINDOW = 2999,
188 };
189
190 sp<InputChannel> inputChannel;
Jeff Brown53a415e2010-09-15 15:18:56 -0700191 String8 name;
Jeff Browna665ca82010-09-08 11:49:43 -0700192 int32_t layoutParamsFlags;
193 int32_t layoutParamsType;
194 nsecs_t dispatchingTimeout;
195 int32_t frameLeft;
196 int32_t frameTop;
197 int32_t frameRight;
198 int32_t frameBottom;
199 int32_t visibleFrameLeft;
200 int32_t visibleFrameTop;
201 int32_t visibleFrameRight;
202 int32_t visibleFrameBottom;
203 int32_t touchableAreaLeft;
204 int32_t touchableAreaTop;
205 int32_t touchableAreaRight;
206 int32_t touchableAreaBottom;
207 bool visible;
Jeff Brown53a415e2010-09-15 15:18:56 -0700208 bool canReceiveKeys;
Jeff Browna665ca82010-09-08 11:49:43 -0700209 bool hasFocus;
210 bool hasWallpaper;
211 bool paused;
Jeff Brown53a415e2010-09-15 15:18:56 -0700212 int32_t layer;
Jeff Browna665ca82010-09-08 11:49:43 -0700213 int32_t ownerPid;
214 int32_t ownerUid;
215
Jeff Browna665ca82010-09-08 11:49:43 -0700216 bool touchableAreaContainsPoint(int32_t x, int32_t y) const;
Jeff Brown35cf0e92010-10-05 12:26:23 -0700217 bool frameContainsPoint(int32_t x, int32_t y) const;
218
219 /* Returns true if the window is of a trusted type that is allowed to silently
220 * overlay other windows for the purpose of implementing the secure views feature.
221 * Trusted overlays, such as IME windows, can partly obscure other windows without causing
222 * motion events to be delivered to them with AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED.
223 */
224 bool isTrustedOverlay() const;
Jeff Browna665ca82010-09-08 11:49:43 -0700225};
226
227
228/*
229 * A private handle type used by the input manager to track the window.
230 */
231class InputApplicationHandle : public RefBase {
232protected:
233 InputApplicationHandle() { }
234 virtual ~InputApplicationHandle() { }
235};
236
237
238/*
239 * An input application describes properties of an application that can receive input.
240 */
241struct InputApplication {
242 String8 name;
243 nsecs_t dispatchingTimeout;
244 sp<InputApplicationHandle> handle;
245};
246
247
248/*
Jeff Brown54bc2812010-06-15 01:31:58 -0700249 * Input dispatcher policy interface.
250 *
251 * The input reader policy is used by the input reader to interact with the Window Manager
252 * and other system components.
253 *
254 * The actual implementation is partially supported by callbacks into the DVM
255 * via JNI. This interface is also mocked in the unit tests.
256 */
257class InputDispatcherPolicyInterface : public virtual RefBase {
258protected:
259 InputDispatcherPolicyInterface() { }
260 virtual ~InputDispatcherPolicyInterface() { }
261
262public:
263 /* Notifies the system that a configuration change has occurred. */
264 virtual void notifyConfigurationChanged(nsecs_t when) = 0;
265
Jeff Browna665ca82010-09-08 11:49:43 -0700266 /* Notifies the system that an application is not responding.
267 * Returns a new timeout to continue waiting, or 0 to abort dispatch. */
Jeff Brown53a415e2010-09-15 15:18:56 -0700268 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
269 const sp<InputChannel>& inputChannel) = 0;
Jeff Browna665ca82010-09-08 11:49:43 -0700270
Jeff Brown54bc2812010-06-15 01:31:58 -0700271 /* Notifies the system that an input channel is unrecoverably broken. */
272 virtual void notifyInputChannelBroken(const sp<InputChannel>& inputChannel) = 0;
273
Jeff Brown61ce3982010-09-07 10:44:57 -0700274 /* Gets the key repeat initial timeout or -1 if automatic key repeating is disabled. */
Jeff Brown54bc2812010-06-15 01:31:58 -0700275 virtual nsecs_t getKeyRepeatTimeout() = 0;
276
Jeff Brown61ce3982010-09-07 10:44:57 -0700277 /* Gets the key repeat inter-key delay. */
278 virtual nsecs_t getKeyRepeatDelay() = 0;
279
Jeff Brown542412c2010-08-18 15:51:08 -0700280 /* Gets the maximum suggested event delivery rate per second.
281 * This value is used to throttle motion event movement actions on a per-device
282 * basis. It is not intended to be a hard limit.
283 */
284 virtual int32_t getMaxEventsPerSecond() = 0;
Jeff Browna665ca82010-09-08 11:49:43 -0700285
Jeff Brown90f0cee2010-10-08 22:31:17 -0700286 /* Intercepts a key event immediately before queueing it.
287 * The policy can use this method as an opportunity to perform power management functions
288 * and early event preprocessing such as updating policy flags.
289 *
290 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
291 * should be dispatched to applications.
292 */
293 virtual void interceptKeyBeforeQueueing(nsecs_t when, int32_t deviceId,
294 int32_t action, int32_t& flags, int32_t keyCode, int32_t scanCode,
295 uint32_t& policyFlags) = 0;
296
297 /* Intercepts a generic touch, trackball or other event before queueing it.
298 * The policy can use this method as an opportunity to perform power management functions
299 * and early event preprocessing such as updating policy flags.
300 *
301 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
302 * should be dispatched to applications.
303 */
304 virtual void interceptGenericBeforeQueueing(nsecs_t when, uint32_t& policyFlags) = 0;
305
Jeff Browna665ca82010-09-08 11:49:43 -0700306 /* Allows the policy a chance to intercept a key before dispatching. */
307 virtual bool interceptKeyBeforeDispatching(const sp<InputChannel>& inputChannel,
308 const KeyEvent* keyEvent, uint32_t policyFlags) = 0;
309
Jeff Brown90f0cee2010-10-08 22:31:17 -0700310 /* Notifies the policy about switch events.
311 */
312 virtual void notifySwitch(nsecs_t when,
313 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) = 0;
314
Jeff Browna665ca82010-09-08 11:49:43 -0700315 /* Poke user activity for an event dispatched to a window. */
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700316 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType) = 0;
Jeff Browna665ca82010-09-08 11:49:43 -0700317
318 /* Checks whether a given application pid/uid has permission to inject input events
319 * into other applications.
320 *
321 * This method is special in that its implementation promises to be non-reentrant and
322 * is safe to call while holding other locks. (Most other methods make no such guarantees!)
323 */
324 virtual bool checkInjectEventsPermissionNonReentrant(
325 int32_t injectorPid, int32_t injectorUid) = 0;
Jeff Brown54bc2812010-06-15 01:31:58 -0700326};
327
328
Jeff Browne839a582010-04-22 18:58:52 -0700329/* Notifies the system about input events generated by the input reader.
330 * The dispatcher is expected to be mostly asynchronous. */
331class InputDispatcherInterface : public virtual RefBase {
332protected:
333 InputDispatcherInterface() { }
334 virtual ~InputDispatcherInterface() { }
335
336public:
Jeff Browna665ca82010-09-08 11:49:43 -0700337 /* Dumps the state of the input dispatcher.
338 *
339 * This method may be called on any thread (usually by the input manager). */
340 virtual void dump(String8& dump) = 0;
341
Jeff Browne839a582010-04-22 18:58:52 -0700342 /* Runs a single iteration of the dispatch loop.
343 * Nominally processes one queued event, a timeout, or a response from an input consumer.
344 *
345 * This method should only be called on the input dispatcher thread.
346 */
347 virtual void dispatchOnce() = 0;
348
349 /* Notifies the dispatcher about new events.
Jeff Browne839a582010-04-22 18:58:52 -0700350 *
351 * These methods should only be called on the input reader thread.
352 */
Jeff Brown54bc2812010-06-15 01:31:58 -0700353 virtual void notifyConfigurationChanged(nsecs_t eventTime) = 0;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700354 virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Browne839a582010-04-22 18:58:52 -0700355 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode,
356 int32_t scanCode, int32_t metaState, nsecs_t downTime) = 0;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700357 virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Brownaf30ff62010-09-01 17:01:00 -0700358 uint32_t policyFlags, int32_t action, int32_t flags,
359 int32_t metaState, int32_t edgeFlags,
Jeff Browne839a582010-04-22 18:58:52 -0700360 uint32_t pointerCount, const int32_t* pointerIds, const PointerCoords* pointerCoords,
361 float xPrecision, float yPrecision, nsecs_t downTime) = 0;
Jeff Brown90f0cee2010-10-08 22:31:17 -0700362 virtual void notifySwitch(nsecs_t when,
363 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) = 0;
Jeff Browne839a582010-04-22 18:58:52 -0700364
Jeff Brown51d45a72010-06-17 20:52:56 -0700365 /* Injects an input event and optionally waits for sync.
Jeff Brownf67c53e2010-07-28 15:48:59 -0700366 * The synchronization mode determines whether the method blocks while waiting for
367 * input injection to proceed.
Jeff Brown51d45a72010-06-17 20:52:56 -0700368 * Returns one of the INPUT_EVENT_INJECTION_XXX constants.
369 *
370 * This method may be called on any thread (usually by the input manager).
371 */
372 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brownf67c53e2010-07-28 15:48:59 -0700373 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis) = 0;
Jeff Brown51d45a72010-06-17 20:52:56 -0700374
Jeff Browna665ca82010-09-08 11:49:43 -0700375 /* Sets the list of input windows.
376 *
377 * This method may be called on any thread (usually by the input manager).
378 */
379 virtual void setInputWindows(const Vector<InputWindow>& inputWindows) = 0;
380
381 /* Sets the focused application.
382 *
383 * This method may be called on any thread (usually by the input manager).
384 */
385 virtual void setFocusedApplication(const InputApplication* inputApplication) = 0;
386
387 /* Sets the input dispatching mode.
388 *
389 * This method may be called on any thread (usually by the input manager).
390 */
391 virtual void setInputDispatchMode(bool enabled, bool frozen) = 0;
392
Jeff Brown744c5592010-09-27 14:52:15 -0700393 /* Transfers touch focus from the window associated with one channel to the
394 * window associated with the other channel.
395 *
396 * Returns true on success. False if the window did not actually have touch focus.
397 */
398 virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel,
399 const sp<InputChannel>& toChannel) = 0;
400
Jeff Browne839a582010-04-22 18:58:52 -0700401 /* Registers or unregister input channels that may be used as targets for input events.
Jeff Browna665ca82010-09-08 11:49:43 -0700402 * If monitor is true, the channel will receive a copy of all input events.
Jeff Browne839a582010-04-22 18:58:52 -0700403 *
404 * These methods may be called on any thread (usually by the input manager).
405 */
Jeff Browna665ca82010-09-08 11:49:43 -0700406 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel, bool monitor) = 0;
Jeff Browne839a582010-04-22 18:58:52 -0700407 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) = 0;
408};
409
Jeff Brown54bc2812010-06-15 01:31:58 -0700410/* Dispatches events to input targets. Some functions of the input dispatcher, such as
411 * identifying input targets, are controlled by a separate policy object.
412 *
413 * IMPORTANT INVARIANT:
414 * Because the policy can potentially block or cause re-entrance into the input dispatcher,
415 * the input dispatcher never calls into the policy while holding its internal locks.
416 * The implementation is also carefully designed to recover from scenarios such as an
417 * input channel becoming unregistered while identifying input targets or processing timeouts.
418 *
419 * Methods marked 'Locked' must be called with the lock acquired.
420 *
421 * Methods marked 'LockedInterruptible' must be called with the lock acquired but
422 * may during the course of their execution release the lock, call into the policy, and
423 * then reacquire the lock. The caller is responsible for recovering gracefully.
424 *
425 * A 'LockedInterruptible' method may called a 'Locked' method, but NOT vice-versa.
426 */
Jeff Browne839a582010-04-22 18:58:52 -0700427class InputDispatcher : public InputDispatcherInterface {
428protected:
429 virtual ~InputDispatcher();
430
431public:
Jeff Brown54bc2812010-06-15 01:31:58 -0700432 explicit InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy);
Jeff Browne839a582010-04-22 18:58:52 -0700433
Jeff Browna665ca82010-09-08 11:49:43 -0700434 virtual void dump(String8& dump);
435
Jeff Browne839a582010-04-22 18:58:52 -0700436 virtual void dispatchOnce();
437
Jeff Brown54bc2812010-06-15 01:31:58 -0700438 virtual void notifyConfigurationChanged(nsecs_t eventTime);
Jeff Brown5c1ed842010-07-14 18:48:53 -0700439 virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Browne839a582010-04-22 18:58:52 -0700440 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode,
441 int32_t scanCode, int32_t metaState, nsecs_t downTime);
Jeff Brown5c1ed842010-07-14 18:48:53 -0700442 virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Brownaf30ff62010-09-01 17:01:00 -0700443 uint32_t policyFlags, int32_t action, int32_t flags,
444 int32_t metaState, int32_t edgeFlags,
Jeff Browne839a582010-04-22 18:58:52 -0700445 uint32_t pointerCount, const int32_t* pointerIds, const PointerCoords* pointerCoords,
446 float xPrecision, float yPrecision, nsecs_t downTime);
Jeff Brown90f0cee2010-10-08 22:31:17 -0700447 virtual void notifySwitch(nsecs_t when,
448 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) ;
Jeff Browne839a582010-04-22 18:58:52 -0700449
Jeff Brown51d45a72010-06-17 20:52:56 -0700450 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brownf67c53e2010-07-28 15:48:59 -0700451 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis);
Jeff Brown51d45a72010-06-17 20:52:56 -0700452
Jeff Browna665ca82010-09-08 11:49:43 -0700453 virtual void setInputWindows(const Vector<InputWindow>& inputWindows);
454 virtual void setFocusedApplication(const InputApplication* inputApplication);
455 virtual void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown50de30a2010-06-22 01:27:15 -0700456
Jeff Brown744c5592010-09-27 14:52:15 -0700457 virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel,
458 const sp<InputChannel>& toChannel);
459
Jeff Browna665ca82010-09-08 11:49:43 -0700460 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel, bool monitor);
Jeff Browne839a582010-04-22 18:58:52 -0700461 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel);
462
463private:
464 template <typename T>
465 struct Link {
466 T* next;
467 T* prev;
468 };
469
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700470 struct InjectionState {
471 mutable int32_t refCount;
472
473 int32_t injectorPid;
474 int32_t injectorUid;
475 int32_t injectionResult; // initially INPUT_EVENT_INJECTION_PENDING
476 bool injectionIsAsync; // set to true if injection is not waiting for the result
477 int32_t pendingForegroundDispatches; // the number of foreground dispatches in progress
478 };
479
Jeff Browne839a582010-04-22 18:58:52 -0700480 struct EventEntry : Link<EventEntry> {
481 enum {
482 TYPE_SENTINEL,
483 TYPE_CONFIGURATION_CHANGED,
484 TYPE_KEY,
485 TYPE_MOTION
486 };
487
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700488 mutable int32_t refCount;
Jeff Browne839a582010-04-22 18:58:52 -0700489 int32_t type;
490 nsecs_t eventTime;
Jeff Brown90f0cee2010-10-08 22:31:17 -0700491 uint32_t policyFlags;
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700492 InjectionState* injectionState;
Jeff Brown51d45a72010-06-17 20:52:56 -0700493
Jeff Brown54bc2812010-06-15 01:31:58 -0700494 bool dispatchInProgress; // initially false, set to true while dispatching
Jeff Brown51d45a72010-06-17 20:52:56 -0700495
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700496 inline bool isInjected() { return injectionState != NULL; }
Jeff Browne839a582010-04-22 18:58:52 -0700497 };
498
499 struct ConfigurationChangedEntry : EventEntry {
Jeff Browne839a582010-04-22 18:58:52 -0700500 };
501
502 struct KeyEntry : EventEntry {
503 int32_t deviceId;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700504 int32_t source;
Jeff Browne839a582010-04-22 18:58:52 -0700505 int32_t action;
506 int32_t flags;
507 int32_t keyCode;
508 int32_t scanCode;
509 int32_t metaState;
510 int32_t repeatCount;
511 nsecs_t downTime;
Jeff Browna665ca82010-09-08 11:49:43 -0700512
513 bool syntheticRepeat; // set to true for synthetic key repeats
514
515 enum InterceptKeyResult {
516 INTERCEPT_KEY_RESULT_UNKNOWN,
517 INTERCEPT_KEY_RESULT_SKIP,
518 INTERCEPT_KEY_RESULT_CONTINUE,
519 };
520 InterceptKeyResult interceptKeyResult; // set based on the interception result
Jeff Browne839a582010-04-22 18:58:52 -0700521 };
522
523 struct MotionSample {
524 MotionSample* next;
525
526 nsecs_t eventTime;
527 PointerCoords pointerCoords[MAX_POINTERS];
528 };
529
530 struct MotionEntry : EventEntry {
531 int32_t deviceId;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700532 int32_t source;
Jeff Browne839a582010-04-22 18:58:52 -0700533 int32_t action;
Jeff Brownaf30ff62010-09-01 17:01:00 -0700534 int32_t flags;
Jeff Browne839a582010-04-22 18:58:52 -0700535 int32_t metaState;
536 int32_t edgeFlags;
537 float xPrecision;
538 float yPrecision;
539 nsecs_t downTime;
540 uint32_t pointerCount;
541 int32_t pointerIds[MAX_POINTERS];
542
543 // Linked list of motion samples associated with this motion event.
544 MotionSample firstSample;
545 MotionSample* lastSample;
Jeff Brown542412c2010-08-18 15:51:08 -0700546
547 uint32_t countSamples() const;
Jeff Browne839a582010-04-22 18:58:52 -0700548 };
549
Jeff Brown54bc2812010-06-15 01:31:58 -0700550 // Tracks the progress of dispatching a particular event to a particular connection.
Jeff Browne839a582010-04-22 18:58:52 -0700551 struct DispatchEntry : Link<DispatchEntry> {
552 EventEntry* eventEntry; // the event to dispatch
553 int32_t targetFlags;
554 float xOffset;
555 float yOffset;
Jeff Browne839a582010-04-22 18:58:52 -0700556
557 // True if dispatch has started.
558 bool inProgress;
559
560 // For motion events:
561 // Pointer to the first motion sample to dispatch in this cycle.
562 // Usually NULL to indicate that the list of motion samples begins at
563 // MotionEntry::firstSample. Otherwise, some samples were dispatched in a previous
564 // cycle and this pointer indicates the location of the first remainining sample
565 // to dispatch during the current cycle.
566 MotionSample* headMotionSample;
567 // Pointer to a motion sample to dispatch in the next cycle if the dispatcher was
568 // unable to send all motion samples during this cycle. On the next cycle,
569 // headMotionSample will be initialized to tailMotionSample and tailMotionSample
570 // will be set to NULL.
571 MotionSample* tailMotionSample;
Jeff Brownf67c53e2010-07-28 15:48:59 -0700572
Jeff Brown53a415e2010-09-15 15:18:56 -0700573 inline bool hasForegroundTarget() const {
574 return targetFlags & InputTarget::FLAG_FOREGROUND;
Jeff Browna665ca82010-09-08 11:49:43 -0700575 }
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700576
577 inline bool isSplit() const {
578 return targetFlags & InputTarget::FLAG_SPLIT;
579 }
Jeff Browne839a582010-04-22 18:58:52 -0700580 };
581
Jeff Brown54bc2812010-06-15 01:31:58 -0700582 // A command entry captures state and behavior for an action to be performed in the
583 // dispatch loop after the initial processing has taken place. It is essentially
584 // a kind of continuation used to postpone sensitive policy interactions to a point
585 // in the dispatch loop where it is safe to release the lock (generally after finishing
586 // the critical parts of the dispatch cycle).
587 //
588 // The special thing about commands is that they can voluntarily release and reacquire
589 // the dispatcher lock at will. Initially when the command starts running, the
590 // dispatcher lock is held. However, if the command needs to call into the policy to
591 // do some work, it can release the lock, do the work, then reacquire the lock again
592 // before returning.
593 //
594 // This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch
595 // never calls into the policy while holding its lock.
596 //
597 // Commands are implicitly 'LockedInterruptible'.
598 struct CommandEntry;
599 typedef void (InputDispatcher::*Command)(CommandEntry* commandEntry);
600
Jeff Brown51d45a72010-06-17 20:52:56 -0700601 class Connection;
Jeff Brown54bc2812010-06-15 01:31:58 -0700602 struct CommandEntry : Link<CommandEntry> {
603 CommandEntry();
604 ~CommandEntry();
605
606 Command command;
607
608 // parameters for the command (usage varies by command)
Jeff Brown51d45a72010-06-17 20:52:56 -0700609 sp<Connection> connection;
Jeff Browna665ca82010-09-08 11:49:43 -0700610 nsecs_t eventTime;
611 KeyEntry* keyEntry;
612 sp<InputChannel> inputChannel;
613 sp<InputApplicationHandle> inputApplicationHandle;
Jeff Browna665ca82010-09-08 11:49:43 -0700614 int32_t userActivityEventType;
Jeff Brown54bc2812010-06-15 01:31:58 -0700615 };
616
617 // Generic queue implementation.
Jeff Browne839a582010-04-22 18:58:52 -0700618 template <typename T>
619 struct Queue {
Jeff Browna665ca82010-09-08 11:49:43 -0700620 T headSentinel;
621 T tailSentinel;
Jeff Browne839a582010-04-22 18:58:52 -0700622
623 inline Queue() {
Jeff Browna665ca82010-09-08 11:49:43 -0700624 headSentinel.prev = NULL;
625 headSentinel.next = & tailSentinel;
626 tailSentinel.prev = & headSentinel;
627 tailSentinel.next = NULL;
Jeff Browne839a582010-04-22 18:58:52 -0700628 }
629
Jeff Browna665ca82010-09-08 11:49:43 -0700630 inline bool isEmpty() const {
631 return headSentinel.next == & tailSentinel;
Jeff Browne839a582010-04-22 18:58:52 -0700632 }
633
634 inline void enqueueAtTail(T* entry) {
Jeff Browna665ca82010-09-08 11:49:43 -0700635 T* last = tailSentinel.prev;
Jeff Browne839a582010-04-22 18:58:52 -0700636 last->next = entry;
637 entry->prev = last;
Jeff Browna665ca82010-09-08 11:49:43 -0700638 entry->next = & tailSentinel;
639 tailSentinel.prev = entry;
Jeff Browne839a582010-04-22 18:58:52 -0700640 }
641
642 inline void enqueueAtHead(T* entry) {
Jeff Browna665ca82010-09-08 11:49:43 -0700643 T* first = headSentinel.next;
644 headSentinel.next = entry;
645 entry->prev = & headSentinel;
Jeff Browne839a582010-04-22 18:58:52 -0700646 entry->next = first;
647 first->prev = entry;
648 }
649
650 inline void dequeue(T* entry) {
651 entry->prev->next = entry->next;
652 entry->next->prev = entry->prev;
653 }
654
655 inline T* dequeueAtHead() {
Jeff Browna665ca82010-09-08 11:49:43 -0700656 T* first = headSentinel.next;
Jeff Browne839a582010-04-22 18:58:52 -0700657 dequeue(first);
658 return first;
659 }
Jeff Brown53a415e2010-09-15 15:18:56 -0700660
661 uint32_t count() const;
Jeff Browne839a582010-04-22 18:58:52 -0700662 };
663
664 /* Allocates queue entries and performs reference counting as needed. */
665 class Allocator {
666 public:
667 Allocator();
668
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700669 InjectionState* obtainInjectionState(int32_t injectorPid, int32_t injectorUid);
Jeff Brown51d45a72010-06-17 20:52:56 -0700670 ConfigurationChangedEntry* obtainConfigurationChangedEntry(nsecs_t eventTime);
671 KeyEntry* obtainKeyEntry(nsecs_t eventTime,
Jeff Brown5c1ed842010-07-14 18:48:53 -0700672 int32_t deviceId, int32_t source, uint32_t policyFlags, int32_t action,
Jeff Brown51d45a72010-06-17 20:52:56 -0700673 int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState,
674 int32_t repeatCount, nsecs_t downTime);
675 MotionEntry* obtainMotionEntry(nsecs_t eventTime,
Jeff Brown5c1ed842010-07-14 18:48:53 -0700676 int32_t deviceId, int32_t source, uint32_t policyFlags, int32_t action,
Jeff Brownaf30ff62010-09-01 17:01:00 -0700677 int32_t flags, int32_t metaState, int32_t edgeFlags,
678 float xPrecision, float yPrecision,
Jeff Brown51d45a72010-06-17 20:52:56 -0700679 nsecs_t downTime, uint32_t pointerCount,
680 const int32_t* pointerIds, const PointerCoords* pointerCoords);
Jeff Browna665ca82010-09-08 11:49:43 -0700681 DispatchEntry* obtainDispatchEntry(EventEntry* eventEntry,
Jeff Brown53a415e2010-09-15 15:18:56 -0700682 int32_t targetFlags, float xOffset, float yOffset);
Jeff Brown54bc2812010-06-15 01:31:58 -0700683 CommandEntry* obtainCommandEntry(Command command);
Jeff Browne839a582010-04-22 18:58:52 -0700684
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700685 void releaseInjectionState(InjectionState* injectionState);
Jeff Browne839a582010-04-22 18:58:52 -0700686 void releaseEventEntry(EventEntry* entry);
687 void releaseConfigurationChangedEntry(ConfigurationChangedEntry* entry);
688 void releaseKeyEntry(KeyEntry* entry);
689 void releaseMotionEntry(MotionEntry* entry);
690 void releaseDispatchEntry(DispatchEntry* entry);
Jeff Brown54bc2812010-06-15 01:31:58 -0700691 void releaseCommandEntry(CommandEntry* entry);
Jeff Browne839a582010-04-22 18:58:52 -0700692
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700693 void recycleKeyEntry(KeyEntry* entry);
694
Jeff Browne839a582010-04-22 18:58:52 -0700695 void appendMotionSample(MotionEntry* motionEntry,
Jeff Brown51d45a72010-06-17 20:52:56 -0700696 nsecs_t eventTime, const PointerCoords* pointerCoords);
Jeff Browne839a582010-04-22 18:58:52 -0700697
698 private:
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700699 Pool<InjectionState> mInjectionStatePool;
Jeff Browne839a582010-04-22 18:58:52 -0700700 Pool<ConfigurationChangedEntry> mConfigurationChangeEntryPool;
701 Pool<KeyEntry> mKeyEntryPool;
702 Pool<MotionEntry> mMotionEntryPool;
703 Pool<MotionSample> mMotionSamplePool;
704 Pool<DispatchEntry> mDispatchEntryPool;
Jeff Brown54bc2812010-06-15 01:31:58 -0700705 Pool<CommandEntry> mCommandEntryPool;
Jeff Brown51d45a72010-06-17 20:52:56 -0700706
Jeff Brown90f0cee2010-10-08 22:31:17 -0700707 void initializeEventEntry(EventEntry* entry, int32_t type, nsecs_t eventTime,
708 uint32_t policyFlags);
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700709 void releaseEventEntryInjectionState(EventEntry* entry);
Jeff Browne839a582010-04-22 18:58:52 -0700710 };
711
Jeff Browna665ca82010-09-08 11:49:43 -0700712 /* Tracks dispatched key and motion event state so that cancelation events can be
713 * synthesized when events are dropped. */
714 class InputState {
715 public:
716 // Specifies whether a given event will violate input state consistency.
717 enum Consistency {
718 // The event is consistent with the current input state.
719 CONSISTENT,
720 // The event is inconsistent with the current input state but applications
721 // will tolerate it. eg. Down followed by another down.
722 TOLERABLE,
723 // The event is inconsistent with the current input state and will probably
724 // cause applications to crash. eg. Up without prior down, move with
725 // unexpected number of pointers.
726 BROKEN
727 };
728
Jeff Brown90f0cee2010-10-08 22:31:17 -0700729 // Specifies the sources to cancel.
730 enum CancelationOptions {
731 CANCEL_ALL_EVENTS = 0,
732 CANCEL_POINTER_EVENTS = 1,
733 CANCEL_NON_POINTER_EVENTS = 2,
734 };
735
Jeff Browna665ca82010-09-08 11:49:43 -0700736 InputState();
737 ~InputState();
738
739 // Returns true if there is no state to be canceled.
740 bool isNeutral() const;
741
Jeff Browna665ca82010-09-08 11:49:43 -0700742 // Records tracking information for an event that has just been published.
743 // Returns whether the event is consistent with the current input state.
744 Consistency trackEvent(const EventEntry* entry);
745
746 // Records tracking information for a key event that has just been published.
747 // Returns whether the event is consistent with the current input state.
748 Consistency trackKey(const KeyEntry* entry);
749
750 // Records tracking information for a motion event that has just been published.
751 // Returns whether the event is consistent with the current input state.
752 Consistency trackMotion(const MotionEntry* entry);
753
Jeff Brown90f0cee2010-10-08 22:31:17 -0700754 // Synthesizes cancelation events for the current state and resets the tracked state.
755 void synthesizeCancelationEvents(nsecs_t currentTime, Allocator* allocator,
756 Vector<EventEntry*>& outEvents, CancelationOptions options);
Jeff Browna665ca82010-09-08 11:49:43 -0700757
758 // Clears the current state.
759 void clear();
760
Jeff Brownb6702e52010-10-11 18:32:20 -0700761 // Copies pointer-related parts of the input state to another instance.
762 void copyPointerStateTo(InputState& other) const;
763
Jeff Browna665ca82010-09-08 11:49:43 -0700764 private:
Jeff Browna665ca82010-09-08 11:49:43 -0700765 struct KeyMemento {
766 int32_t deviceId;
767 int32_t source;
768 int32_t keyCode;
769 int32_t scanCode;
770 nsecs_t downTime;
771 };
772
773 struct MotionMemento {
774 int32_t deviceId;
775 int32_t source;
776 float xPrecision;
777 float yPrecision;
778 nsecs_t downTime;
779 uint32_t pointerCount;
780 int32_t pointerIds[MAX_POINTERS];
781 PointerCoords pointerCoords[MAX_POINTERS];
782
783 void setPointers(const MotionEntry* entry);
784 };
785
786 Vector<KeyMemento> mKeyMementos;
787 Vector<MotionMemento> mMotionMementos;
Jeff Brown90f0cee2010-10-08 22:31:17 -0700788
789 static bool shouldCancelEvent(int32_t eventSource, CancelationOptions options);
Jeff Browna665ca82010-09-08 11:49:43 -0700790 };
791
Jeff Browne839a582010-04-22 18:58:52 -0700792 /* Manages the dispatch state associated with a single input channel. */
793 class Connection : public RefBase {
794 protected:
795 virtual ~Connection();
796
797 public:
798 enum Status {
799 // Everything is peachy.
800 STATUS_NORMAL,
801 // An unrecoverable communication error has occurred.
802 STATUS_BROKEN,
Jeff Browne839a582010-04-22 18:58:52 -0700803 // The input channel has been unregistered.
804 STATUS_ZOMBIE
805 };
806
807 Status status;
808 sp<InputChannel> inputChannel;
809 InputPublisher inputPublisher;
Jeff Browna665ca82010-09-08 11:49:43 -0700810 InputState inputState;
Jeff Browne839a582010-04-22 18:58:52 -0700811 Queue<DispatchEntry> outboundQueue;
Jeff Browne839a582010-04-22 18:58:52 -0700812
813 nsecs_t lastEventTime; // the time when the event was originally captured
814 nsecs_t lastDispatchTime; // the time when the last event was dispatched
Jeff Browne839a582010-04-22 18:58:52 -0700815
816 explicit Connection(const sp<InputChannel>& inputChannel);
817
Jeff Brown54bc2812010-06-15 01:31:58 -0700818 inline const char* getInputChannelName() const { return inputChannel->getName().string(); }
819
820 const char* getStatusLabel() const;
Jeff Browne839a582010-04-22 18:58:52 -0700821
822 // Finds a DispatchEntry in the outbound queue associated with the specified event.
823 // Returns NULL if not found.
824 DispatchEntry* findQueuedDispatchEntryForEvent(const EventEntry* eventEntry) const;
825
Jeff Browne839a582010-04-22 18:58:52 -0700826 // Gets the time since the current event was originally obtained from the input driver.
Jeff Browna665ca82010-09-08 11:49:43 -0700827 inline double getEventLatencyMillis(nsecs_t currentTime) const {
Jeff Browne839a582010-04-22 18:58:52 -0700828 return (currentTime - lastEventTime) / 1000000.0;
829 }
830
831 // Gets the time since the current event entered the outbound dispatch queue.
Jeff Browna665ca82010-09-08 11:49:43 -0700832 inline double getDispatchLatencyMillis(nsecs_t currentTime) const {
Jeff Browne839a582010-04-22 18:58:52 -0700833 return (currentTime - lastDispatchTime) / 1000000.0;
834 }
835
Jeff Browne839a582010-04-22 18:58:52 -0700836 status_t initialize();
837 };
838
Jeff Brown90f0cee2010-10-08 22:31:17 -0700839 enum DropReason {
840 DROP_REASON_NOT_DROPPED = 0,
841 DROP_REASON_POLICY = 1,
842 DROP_REASON_APP_SWITCH = 2,
843 DROP_REASON_DISABLED = 3,
844 };
845
Jeff Brown54bc2812010-06-15 01:31:58 -0700846 sp<InputDispatcherPolicyInterface> mPolicy;
Jeff Browne839a582010-04-22 18:58:52 -0700847
848 Mutex mLock;
849
Jeff Browne839a582010-04-22 18:58:52 -0700850 Allocator mAllocator;
Jeff Brown59abe7e2010-09-13 23:17:30 -0700851 sp<Looper> mLooper;
Jeff Browne839a582010-04-22 18:58:52 -0700852
Jeff Browna665ca82010-09-08 11:49:43 -0700853 EventEntry* mPendingEvent;
Jeff Brown54bc2812010-06-15 01:31:58 -0700854 Queue<EventEntry> mInboundQueue;
855 Queue<CommandEntry> mCommandQueue;
856
Jeff Browna665ca82010-09-08 11:49:43 -0700857 Vector<EventEntry*> mTempCancelationEvents;
858
859 void dispatchOnceInnerLocked(nsecs_t keyRepeatTimeout, nsecs_t keyRepeatDelay,
860 nsecs_t* nextWakeupTime);
861
Jeff Brown59abe7e2010-09-13 23:17:30 -0700862 // Enqueues an inbound event. Returns true if mLooper->wake() should be called.
Jeff Browna665ca82010-09-08 11:49:43 -0700863 bool enqueueInboundEventLocked(EventEntry* entry);
864
Jeff Brown90f0cee2010-10-08 22:31:17 -0700865 // Cleans up input state when dropping an inbound event.
866 void dropInboundEventLocked(EventEntry* entry, DropReason dropReason);
867
Jeff Browna665ca82010-09-08 11:49:43 -0700868 // App switch latency optimization.
Jeff Brown90f0cee2010-10-08 22:31:17 -0700869 bool mAppSwitchSawKeyDown;
Jeff Browna665ca82010-09-08 11:49:43 -0700870 nsecs_t mAppSwitchDueTime;
871
Jeff Brown90f0cee2010-10-08 22:31:17 -0700872 static bool isAppSwitchKeyCode(int32_t keyCode);
873 bool isAppSwitchKeyEventLocked(KeyEntry* keyEntry);
Jeff Browna665ca82010-09-08 11:49:43 -0700874 bool isAppSwitchPendingLocked();
Jeff Browna665ca82010-09-08 11:49:43 -0700875 void resetPendingAppSwitchLocked(bool handled);
876
Jeff Browne839a582010-04-22 18:58:52 -0700877 // All registered connections mapped by receive pipe file descriptor.
878 KeyedVector<int, sp<Connection> > mConnectionsByReceiveFd;
879
Jeff Brown53a415e2010-09-15 15:18:56 -0700880 ssize_t getConnectionIndexLocked(const sp<InputChannel>& inputChannel);
Jeff Brown0cacb872010-08-17 15:59:26 -0700881
Jeff Browne839a582010-04-22 18:58:52 -0700882 // Active connections are connections that have a non-empty outbound queue.
Jeff Brown51d45a72010-06-17 20:52:56 -0700883 // We don't use a ref-counted pointer here because we explicitly abort connections
884 // during unregistration which causes the connection's outbound queue to be cleared
885 // and the connection itself to be deactivated.
Jeff Browne839a582010-04-22 18:58:52 -0700886 Vector<Connection*> mActiveConnections;
887
Jeff Browna665ca82010-09-08 11:49:43 -0700888 // Input channels that will receive a copy of all input events.
889 Vector<sp<InputChannel> > mMonitoringChannels;
Jeff Browne839a582010-04-22 18:58:52 -0700890
Jeff Browna665ca82010-09-08 11:49:43 -0700891 // Preallocated key event object used for policy inquiries.
892 KeyEvent mReusableKeyEvent;
Jeff Browne839a582010-04-22 18:58:52 -0700893
Jeff Brown51d45a72010-06-17 20:52:56 -0700894 // Event injection and synchronization.
895 Condition mInjectionResultAvailableCondition;
Jeff Brown90f0cee2010-10-08 22:31:17 -0700896 bool hasInjectionPermission(int32_t injectorPid, int32_t injectorUid);
Jeff Brown51d45a72010-06-17 20:52:56 -0700897 void setInjectionResultLocked(EventEntry* entry, int32_t injectionResult);
898
Jeff Brownf67c53e2010-07-28 15:48:59 -0700899 Condition mInjectionSyncFinishedCondition;
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700900 void incrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brown53a415e2010-09-15 15:18:56 -0700901 void decrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brownf67c53e2010-07-28 15:48:59 -0700902
Jeff Brown542412c2010-08-18 15:51:08 -0700903 // Throttling state.
904 struct ThrottleState {
905 nsecs_t minTimeBetweenEvents;
906
907 nsecs_t lastEventTime;
908 int32_t lastDeviceId;
909 uint32_t lastSource;
910
911 uint32_t originalSampleCount; // only collected during debugging
912 } mThrottleState;
913
Jeff Browne839a582010-04-22 18:58:52 -0700914 // Key repeat tracking.
Jeff Browne839a582010-04-22 18:58:52 -0700915 struct KeyRepeatState {
916 KeyEntry* lastKeyEntry; // or null if no repeat
917 nsecs_t nextRepeatTime;
918 } mKeyRepeatState;
919
920 void resetKeyRepeatLocked();
Jeff Browna665ca82010-09-08 11:49:43 -0700921 KeyEntry* synthesizeKeyRepeatLocked(nsecs_t currentTime, nsecs_t keyRepeatTimeout);
Jeff Browne839a582010-04-22 18:58:52 -0700922
Jeff Brown54bc2812010-06-15 01:31:58 -0700923 // Deferred command processing.
924 bool runCommandsLockedInterruptible();
925 CommandEntry* postCommandLocked(Command command);
926
Jeff Browna665ca82010-09-08 11:49:43 -0700927 // Inbound event processing.
928 void drainInboundQueueLocked();
Jeff Brownd8816c32010-09-16 14:07:33 -0700929 void releasePendingEventLocked();
930 void releaseInboundEventLocked(EventEntry* entry);
Jeff Browne839a582010-04-22 18:58:52 -0700931
Jeff Browna665ca82010-09-08 11:49:43 -0700932 // Dispatch state.
933 bool mDispatchEnabled;
934 bool mDispatchFrozen;
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700935
Jeff Browna665ca82010-09-08 11:49:43 -0700936 Vector<InputWindow> mWindows;
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700937
938 const InputWindow* getWindowLocked(const sp<InputChannel>& inputChannel);
Jeff Browna665ca82010-09-08 11:49:43 -0700939
940 // Focus tracking for keys, trackball, etc.
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700941 const InputWindow* mFocusedWindow;
Jeff Browna665ca82010-09-08 11:49:43 -0700942
943 // Focus tracking for touch.
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700944 struct TouchedWindow {
945 const InputWindow* window;
946 int32_t targetFlags;
947 BitSet32 pointerIds;
948 sp<InputChannel> channel;
Jeff Browna665ca82010-09-08 11:49:43 -0700949 };
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700950 struct TouchState {
951 bool down;
952 bool split;
953 Vector<TouchedWindow> windows;
954
955 TouchState();
956 ~TouchState();
957 void reset();
958 void copyFrom(const TouchState& other);
959 void addOrUpdateWindow(const InputWindow* window, int32_t targetFlags, BitSet32 pointerIds);
960 void removeOutsideTouchWindows();
961 const InputWindow* getFirstForegroundWindow();
962 };
963
964 TouchState mTouchState;
965 TouchState mTempTouchState;
Jeff Browna665ca82010-09-08 11:49:43 -0700966
967 // Focused application.
968 InputApplication* mFocusedApplication;
969 InputApplication mFocusedApplicationStorage; // preallocated storage for mFocusedApplication
970 void releaseFocusedApplicationLocked();
971
972 // Dispatch inbound events.
973 bool dispatchConfigurationChangedLocked(
974 nsecs_t currentTime, ConfigurationChangedEntry* entry);
975 bool dispatchKeyLocked(
976 nsecs_t currentTime, KeyEntry* entry, nsecs_t keyRepeatTimeout,
Jeff Brown33d54ce2010-10-11 14:20:19 -0700977 DropReason* dropReason, nsecs_t* nextWakeupTime);
Jeff Browna665ca82010-09-08 11:49:43 -0700978 bool dispatchMotionLocked(
979 nsecs_t currentTime, MotionEntry* entry,
Jeff Brown33d54ce2010-10-11 14:20:19 -0700980 DropReason* dropReason, nsecs_t* nextWakeupTime);
Jeff Brown54bc2812010-06-15 01:31:58 -0700981 void dispatchEventToCurrentInputTargetsLocked(
982 nsecs_t currentTime, EventEntry* entry, bool resumeWithAppendedMotionSample);
Jeff Browne839a582010-04-22 18:58:52 -0700983
Jeff Browna665ca82010-09-08 11:49:43 -0700984 void logOutboundKeyDetailsLocked(const char* prefix, const KeyEntry* entry);
985 void logOutboundMotionDetailsLocked(const char* prefix, const MotionEntry* entry);
986
987 // The input targets that were most recently identified for dispatch.
Jeff Browna665ca82010-09-08 11:49:43 -0700988 bool mCurrentInputTargetsValid; // false while targets are being recomputed
989 Vector<InputTarget> mCurrentInputTargets;
Jeff Browna665ca82010-09-08 11:49:43 -0700990
991 enum InputTargetWaitCause {
992 INPUT_TARGET_WAIT_CAUSE_NONE,
993 INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY,
994 INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY,
995 };
996
997 InputTargetWaitCause mInputTargetWaitCause;
998 nsecs_t mInputTargetWaitStartTime;
999 nsecs_t mInputTargetWaitTimeoutTime;
1000 bool mInputTargetWaitTimeoutExpired;
1001
1002 // Finding targets for input events.
Jeff Brownd8816c32010-09-16 14:07:33 -07001003 void resetTargetsLocked();
Jeff Brownd1b0a2b2010-09-26 22:20:12 -07001004 void commitTargetsLocked();
Jeff Browna665ca82010-09-08 11:49:43 -07001005 int32_t handleTargetsNotReadyLocked(nsecs_t currentTime, const EventEntry* entry,
1006 const InputApplication* application, const InputWindow* window,
1007 nsecs_t* nextWakeupTime);
Jeff Brown53a415e2010-09-15 15:18:56 -07001008 void resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout,
1009 const sp<InputChannel>& inputChannel);
1010 nsecs_t getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime);
Jeff Browna665ca82010-09-08 11:49:43 -07001011 void resetANRTimeoutsLocked();
1012
Jeff Brownd1b0a2b2010-09-26 22:20:12 -07001013 int32_t findFocusedWindowTargetsLocked(nsecs_t currentTime, const EventEntry* entry,
1014 nsecs_t* nextWakeupTime);
1015 int32_t findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry* entry,
1016 nsecs_t* nextWakeupTime);
Jeff Browna665ca82010-09-08 11:49:43 -07001017
Jeff Brownd1b0a2b2010-09-26 22:20:12 -07001018 void addWindowTargetLocked(const InputWindow* window, int32_t targetFlags,
1019 BitSet32 pointerIds);
Jeff Browna665ca82010-09-08 11:49:43 -07001020 void addMonitoringTargetsLocked();
Jeff Brownd1b0a2b2010-09-26 22:20:12 -07001021 bool shouldPokeUserActivityForCurrentInputTargetsLocked();
1022 void pokeUserActivityLocked(nsecs_t eventTime, int32_t eventType);
1023 bool checkInjectionPermission(const InputWindow* window, const InjectionState* injectionState);
Jeff Brown35cf0e92010-10-05 12:26:23 -07001024 bool isWindowObscuredAtPointLocked(const InputWindow* window, int32_t x, int32_t y) const;
Jeff Brown53a415e2010-09-15 15:18:56 -07001025 bool isWindowFinishedWithPreviousInputLocked(const InputWindow* window);
Jeff Brown53a415e2010-09-15 15:18:56 -07001026 String8 getApplicationWindowLabelLocked(const InputApplication* application,
1027 const InputWindow* window);
Jeff Browna665ca82010-09-08 11:49:43 -07001028
Jeff Browne839a582010-04-22 18:58:52 -07001029 // Manage the dispatch cycle for a single connection.
Jeff Brown51d45a72010-06-17 20:52:56 -07001030 // These methods are deliberately not Interruptible because doing all of the work
1031 // with the mutex held makes it easier to ensure that connection invariants are maintained.
1032 // If needed, the methods post commands to run later once the critical bits are done.
1033 void prepareDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Jeff Browne839a582010-04-22 18:58:52 -07001034 EventEntry* eventEntry, const InputTarget* inputTarget,
1035 bool resumeWithAppendedMotionSample);
Jeff Brown53a415e2010-09-15 15:18:56 -07001036 void startDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown51d45a72010-06-17 20:52:56 -07001037 void finishDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Browna665ca82010-09-08 11:49:43 -07001038 void startNextDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown90f0cee2010-10-08 22:31:17 -07001039 void abortBrokenDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown53a415e2010-09-15 15:18:56 -07001040 void drainOutboundQueueLocked(Connection* connection);
Jeff Brown59abe7e2010-09-13 23:17:30 -07001041 static int handleReceiveCallback(int receiveFd, int events, void* data);
Jeff Browne839a582010-04-22 18:58:52 -07001042
Jeff Brown90f0cee2010-10-08 22:31:17 -07001043 void synthesizeCancelationEventsForAllConnectionsLocked(
1044 InputState::CancelationOptions options, const char* reason);
1045 void synthesizeCancelationEventsForInputChannelLocked(const sp<InputChannel>& channel,
1046 InputState::CancelationOptions options, const char* reason);
1047 void synthesizeCancelationEventsForConnectionLocked(const sp<Connection>& connection,
1048 InputState::CancelationOptions options, const char* reason);
1049
Jeff Brownd1b0a2b2010-09-26 22:20:12 -07001050 // Splitting motion events across windows.
1051 MotionEntry* splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds);
1052
Jeff Browna665ca82010-09-08 11:49:43 -07001053 // Dump state.
1054 void dumpDispatchStateLocked(String8& dump);
1055 void logDispatchStateLocked();
1056
Jeff Browne839a582010-04-22 18:58:52 -07001057 // Add or remove a connection to the mActiveConnections vector.
1058 void activateConnectionLocked(Connection* connection);
1059 void deactivateConnectionLocked(Connection* connection);
1060
1061 // Interesting events that we might like to log or tell the framework about.
Jeff Brown54bc2812010-06-15 01:31:58 -07001062 void onDispatchCycleStartedLocked(
Jeff Brown51d45a72010-06-17 20:52:56 -07001063 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown54bc2812010-06-15 01:31:58 -07001064 void onDispatchCycleFinishedLocked(
Jeff Brown51d45a72010-06-17 20:52:56 -07001065 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown54bc2812010-06-15 01:31:58 -07001066 void onDispatchCycleBrokenLocked(
Jeff Brown51d45a72010-06-17 20:52:56 -07001067 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown53a415e2010-09-15 15:18:56 -07001068 void onANRLocked(
1069 nsecs_t currentTime, const InputApplication* application, const InputWindow* window,
1070 nsecs_t eventTime, nsecs_t waitStartTime);
Jeff Brown54bc2812010-06-15 01:31:58 -07001071
Jeff Brown51d45a72010-06-17 20:52:56 -07001072 // Outbound policy interactions.
Jeff Browna665ca82010-09-08 11:49:43 -07001073 void doNotifyConfigurationChangedInterruptible(CommandEntry* commandEntry);
Jeff Brown54bc2812010-06-15 01:31:58 -07001074 void doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown53a415e2010-09-15 15:18:56 -07001075 void doNotifyANRLockedInterruptible(CommandEntry* commandEntry);
Jeff Browna665ca82010-09-08 11:49:43 -07001076 void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry);
1077 void doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown53a415e2010-09-15 15:18:56 -07001078
1079 // Statistics gathering.
1080 void updateDispatchStatisticsLocked(nsecs_t currentTime, const EventEntry* entry,
1081 int32_t injectionResult, nsecs_t timeSpentWaitingForApplication);
Jeff Browne839a582010-04-22 18:58:52 -07001082};
1083
1084/* Enqueues and dispatches input events, endlessly. */
1085class InputDispatcherThread : public Thread {
1086public:
1087 explicit InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher);
1088 ~InputDispatcherThread();
1089
1090private:
1091 virtual bool threadLoop();
1092
1093 sp<InputDispatcherInterface> mDispatcher;
1094};
1095
1096} // namespace android
1097
Jeff Browna665ca82010-09-08 11:49:43 -07001098#endif // _UI_INPUT_DISPATCHER_H