blob: 4955d473f0da30ff1723360cd4a9b165d8b5b9f6 [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 Brownd9dd44d2010-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 Browne839a582010-04-22 18:58:52 -0700393 /* Registers or unregister input channels that may be used as targets for input events.
Jeff Browna665ca82010-09-08 11:49:43 -0700394 * If monitor is true, the channel will receive a copy of all input events.
Jeff Browne839a582010-04-22 18:58:52 -0700395 *
396 * These methods may be called on any thread (usually by the input manager).
397 */
Jeff Browna665ca82010-09-08 11:49:43 -0700398 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel, bool monitor) = 0;
Jeff Browne839a582010-04-22 18:58:52 -0700399 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) = 0;
400};
401
Jeff Brown54bc2812010-06-15 01:31:58 -0700402/* Dispatches events to input targets. Some functions of the input dispatcher, such as
403 * identifying input targets, are controlled by a separate policy object.
404 *
405 * IMPORTANT INVARIANT:
406 * Because the policy can potentially block or cause re-entrance into the input dispatcher,
407 * the input dispatcher never calls into the policy while holding its internal locks.
408 * The implementation is also carefully designed to recover from scenarios such as an
409 * input channel becoming unregistered while identifying input targets or processing timeouts.
410 *
411 * Methods marked 'Locked' must be called with the lock acquired.
412 *
413 * Methods marked 'LockedInterruptible' must be called with the lock acquired but
414 * may during the course of their execution release the lock, call into the policy, and
415 * then reacquire the lock. The caller is responsible for recovering gracefully.
416 *
417 * A 'LockedInterruptible' method may called a 'Locked' method, but NOT vice-versa.
418 */
Jeff Browne839a582010-04-22 18:58:52 -0700419class InputDispatcher : public InputDispatcherInterface {
420protected:
421 virtual ~InputDispatcher();
422
423public:
Jeff Brown54bc2812010-06-15 01:31:58 -0700424 explicit InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy);
Jeff Browne839a582010-04-22 18:58:52 -0700425
Jeff Browna665ca82010-09-08 11:49:43 -0700426 virtual void dump(String8& dump);
427
Jeff Browne839a582010-04-22 18:58:52 -0700428 virtual void dispatchOnce();
429
Jeff Brown54bc2812010-06-15 01:31:58 -0700430 virtual void notifyConfigurationChanged(nsecs_t eventTime);
Jeff Brown5c1ed842010-07-14 18:48:53 -0700431 virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Browne839a582010-04-22 18:58:52 -0700432 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode,
433 int32_t scanCode, int32_t metaState, nsecs_t downTime);
Jeff Brown5c1ed842010-07-14 18:48:53 -0700434 virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, int32_t source,
Jeff Brownaf30ff62010-09-01 17:01:00 -0700435 uint32_t policyFlags, int32_t action, int32_t flags,
436 int32_t metaState, int32_t edgeFlags,
Jeff Browne839a582010-04-22 18:58:52 -0700437 uint32_t pointerCount, const int32_t* pointerIds, const PointerCoords* pointerCoords,
438 float xPrecision, float yPrecision, nsecs_t downTime);
Jeff Brown90f0cee2010-10-08 22:31:17 -0700439 virtual void notifySwitch(nsecs_t when,
440 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) ;
Jeff Browne839a582010-04-22 18:58:52 -0700441
Jeff Brown51d45a72010-06-17 20:52:56 -0700442 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brownf67c53e2010-07-28 15:48:59 -0700443 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis);
Jeff Brown51d45a72010-06-17 20:52:56 -0700444
Jeff Browna665ca82010-09-08 11:49:43 -0700445 virtual void setInputWindows(const Vector<InputWindow>& inputWindows);
446 virtual void setFocusedApplication(const InputApplication* inputApplication);
447 virtual void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown50de30a2010-06-22 01:27:15 -0700448
Jeff Browna665ca82010-09-08 11:49:43 -0700449 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel, bool monitor);
Jeff Browne839a582010-04-22 18:58:52 -0700450 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel);
451
452private:
453 template <typename T>
454 struct Link {
455 T* next;
456 T* prev;
457 };
458
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700459 struct InjectionState {
460 mutable int32_t refCount;
461
462 int32_t injectorPid;
463 int32_t injectorUid;
464 int32_t injectionResult; // initially INPUT_EVENT_INJECTION_PENDING
465 bool injectionIsAsync; // set to true if injection is not waiting for the result
466 int32_t pendingForegroundDispatches; // the number of foreground dispatches in progress
467 };
468
Jeff Browne839a582010-04-22 18:58:52 -0700469 struct EventEntry : Link<EventEntry> {
470 enum {
471 TYPE_SENTINEL,
472 TYPE_CONFIGURATION_CHANGED,
473 TYPE_KEY,
474 TYPE_MOTION
475 };
476
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700477 mutable int32_t refCount;
Jeff Browne839a582010-04-22 18:58:52 -0700478 int32_t type;
479 nsecs_t eventTime;
Jeff Brown90f0cee2010-10-08 22:31:17 -0700480 uint32_t policyFlags;
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700481 InjectionState* injectionState;
Jeff Brown51d45a72010-06-17 20:52:56 -0700482
Jeff Brown54bc2812010-06-15 01:31:58 -0700483 bool dispatchInProgress; // initially false, set to true while dispatching
Jeff Brown51d45a72010-06-17 20:52:56 -0700484
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700485 inline bool isInjected() { return injectionState != NULL; }
Jeff Browne839a582010-04-22 18:58:52 -0700486 };
487
488 struct ConfigurationChangedEntry : EventEntry {
Jeff Browne839a582010-04-22 18:58:52 -0700489 };
490
491 struct KeyEntry : EventEntry {
492 int32_t deviceId;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700493 int32_t source;
Jeff Browne839a582010-04-22 18:58:52 -0700494 int32_t action;
495 int32_t flags;
496 int32_t keyCode;
497 int32_t scanCode;
498 int32_t metaState;
499 int32_t repeatCount;
500 nsecs_t downTime;
Jeff Browna665ca82010-09-08 11:49:43 -0700501
502 bool syntheticRepeat; // set to true for synthetic key repeats
503
504 enum InterceptKeyResult {
505 INTERCEPT_KEY_RESULT_UNKNOWN,
506 INTERCEPT_KEY_RESULT_SKIP,
507 INTERCEPT_KEY_RESULT_CONTINUE,
508 };
509 InterceptKeyResult interceptKeyResult; // set based on the interception result
Jeff Browne839a582010-04-22 18:58:52 -0700510 };
511
512 struct MotionSample {
513 MotionSample* next;
514
515 nsecs_t eventTime;
516 PointerCoords pointerCoords[MAX_POINTERS];
517 };
518
519 struct MotionEntry : EventEntry {
520 int32_t deviceId;
Jeff Brown5c1ed842010-07-14 18:48:53 -0700521 int32_t source;
Jeff Browne839a582010-04-22 18:58:52 -0700522 int32_t action;
Jeff Brownaf30ff62010-09-01 17:01:00 -0700523 int32_t flags;
Jeff Browne839a582010-04-22 18:58:52 -0700524 int32_t metaState;
525 int32_t edgeFlags;
526 float xPrecision;
527 float yPrecision;
528 nsecs_t downTime;
529 uint32_t pointerCount;
530 int32_t pointerIds[MAX_POINTERS];
531
532 // Linked list of motion samples associated with this motion event.
533 MotionSample firstSample;
534 MotionSample* lastSample;
Jeff Brown542412c2010-08-18 15:51:08 -0700535
536 uint32_t countSamples() const;
Jeff Browne839a582010-04-22 18:58:52 -0700537 };
538
Jeff Brown54bc2812010-06-15 01:31:58 -0700539 // Tracks the progress of dispatching a particular event to a particular connection.
Jeff Browne839a582010-04-22 18:58:52 -0700540 struct DispatchEntry : Link<DispatchEntry> {
541 EventEntry* eventEntry; // the event to dispatch
542 int32_t targetFlags;
543 float xOffset;
544 float yOffset;
Jeff Browne839a582010-04-22 18:58:52 -0700545
546 // True if dispatch has started.
547 bool inProgress;
548
549 // For motion events:
550 // Pointer to the first motion sample to dispatch in this cycle.
551 // Usually NULL to indicate that the list of motion samples begins at
552 // MotionEntry::firstSample. Otherwise, some samples were dispatched in a previous
553 // cycle and this pointer indicates the location of the first remainining sample
554 // to dispatch during the current cycle.
555 MotionSample* headMotionSample;
556 // Pointer to a motion sample to dispatch in the next cycle if the dispatcher was
557 // unable to send all motion samples during this cycle. On the next cycle,
558 // headMotionSample will be initialized to tailMotionSample and tailMotionSample
559 // will be set to NULL.
560 MotionSample* tailMotionSample;
Jeff Brownf67c53e2010-07-28 15:48:59 -0700561
Jeff Brown53a415e2010-09-15 15:18:56 -0700562 inline bool hasForegroundTarget() const {
563 return targetFlags & InputTarget::FLAG_FOREGROUND;
Jeff Browna665ca82010-09-08 11:49:43 -0700564 }
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700565
566 inline bool isSplit() const {
567 return targetFlags & InputTarget::FLAG_SPLIT;
568 }
Jeff Browne839a582010-04-22 18:58:52 -0700569 };
570
Jeff Brown54bc2812010-06-15 01:31:58 -0700571 // A command entry captures state and behavior for an action to be performed in the
572 // dispatch loop after the initial processing has taken place. It is essentially
573 // a kind of continuation used to postpone sensitive policy interactions to a point
574 // in the dispatch loop where it is safe to release the lock (generally after finishing
575 // the critical parts of the dispatch cycle).
576 //
577 // The special thing about commands is that they can voluntarily release and reacquire
578 // the dispatcher lock at will. Initially when the command starts running, the
579 // dispatcher lock is held. However, if the command needs to call into the policy to
580 // do some work, it can release the lock, do the work, then reacquire the lock again
581 // before returning.
582 //
583 // This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch
584 // never calls into the policy while holding its lock.
585 //
586 // Commands are implicitly 'LockedInterruptible'.
587 struct CommandEntry;
588 typedef void (InputDispatcher::*Command)(CommandEntry* commandEntry);
589
Jeff Brown51d45a72010-06-17 20:52:56 -0700590 class Connection;
Jeff Brown54bc2812010-06-15 01:31:58 -0700591 struct CommandEntry : Link<CommandEntry> {
592 CommandEntry();
593 ~CommandEntry();
594
595 Command command;
596
597 // parameters for the command (usage varies by command)
Jeff Brown51d45a72010-06-17 20:52:56 -0700598 sp<Connection> connection;
Jeff Browna665ca82010-09-08 11:49:43 -0700599 nsecs_t eventTime;
600 KeyEntry* keyEntry;
601 sp<InputChannel> inputChannel;
602 sp<InputApplicationHandle> inputApplicationHandle;
Jeff Browna665ca82010-09-08 11:49:43 -0700603 int32_t userActivityEventType;
Jeff Brown54bc2812010-06-15 01:31:58 -0700604 };
605
606 // Generic queue implementation.
Jeff Browne839a582010-04-22 18:58:52 -0700607 template <typename T>
608 struct Queue {
Jeff Browna665ca82010-09-08 11:49:43 -0700609 T headSentinel;
610 T tailSentinel;
Jeff Browne839a582010-04-22 18:58:52 -0700611
612 inline Queue() {
Jeff Browna665ca82010-09-08 11:49:43 -0700613 headSentinel.prev = NULL;
614 headSentinel.next = & tailSentinel;
615 tailSentinel.prev = & headSentinel;
616 tailSentinel.next = NULL;
Jeff Browne839a582010-04-22 18:58:52 -0700617 }
618
Jeff Browna665ca82010-09-08 11:49:43 -0700619 inline bool isEmpty() const {
620 return headSentinel.next == & tailSentinel;
Jeff Browne839a582010-04-22 18:58:52 -0700621 }
622
623 inline void enqueueAtTail(T* entry) {
Jeff Browna665ca82010-09-08 11:49:43 -0700624 T* last = tailSentinel.prev;
Jeff Browne839a582010-04-22 18:58:52 -0700625 last->next = entry;
626 entry->prev = last;
Jeff Browna665ca82010-09-08 11:49:43 -0700627 entry->next = & tailSentinel;
628 tailSentinel.prev = entry;
Jeff Browne839a582010-04-22 18:58:52 -0700629 }
630
631 inline void enqueueAtHead(T* entry) {
Jeff Browna665ca82010-09-08 11:49:43 -0700632 T* first = headSentinel.next;
633 headSentinel.next = entry;
634 entry->prev = & headSentinel;
Jeff Browne839a582010-04-22 18:58:52 -0700635 entry->next = first;
636 first->prev = entry;
637 }
638
639 inline void dequeue(T* entry) {
640 entry->prev->next = entry->next;
641 entry->next->prev = entry->prev;
642 }
643
644 inline T* dequeueAtHead() {
Jeff Browna665ca82010-09-08 11:49:43 -0700645 T* first = headSentinel.next;
Jeff Browne839a582010-04-22 18:58:52 -0700646 dequeue(first);
647 return first;
648 }
Jeff Brown53a415e2010-09-15 15:18:56 -0700649
650 uint32_t count() const;
Jeff Browne839a582010-04-22 18:58:52 -0700651 };
652
653 /* Allocates queue entries and performs reference counting as needed. */
654 class Allocator {
655 public:
656 Allocator();
657
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700658 InjectionState* obtainInjectionState(int32_t injectorPid, int32_t injectorUid);
Jeff Brown51d45a72010-06-17 20:52:56 -0700659 ConfigurationChangedEntry* obtainConfigurationChangedEntry(nsecs_t eventTime);
660 KeyEntry* obtainKeyEntry(nsecs_t eventTime,
Jeff Brown5c1ed842010-07-14 18:48:53 -0700661 int32_t deviceId, int32_t source, uint32_t policyFlags, int32_t action,
Jeff Brown51d45a72010-06-17 20:52:56 -0700662 int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState,
663 int32_t repeatCount, nsecs_t downTime);
664 MotionEntry* obtainMotionEntry(nsecs_t eventTime,
Jeff Brown5c1ed842010-07-14 18:48:53 -0700665 int32_t deviceId, int32_t source, uint32_t policyFlags, int32_t action,
Jeff Brownaf30ff62010-09-01 17:01:00 -0700666 int32_t flags, int32_t metaState, int32_t edgeFlags,
667 float xPrecision, float yPrecision,
Jeff Brown51d45a72010-06-17 20:52:56 -0700668 nsecs_t downTime, uint32_t pointerCount,
669 const int32_t* pointerIds, const PointerCoords* pointerCoords);
Jeff Browna665ca82010-09-08 11:49:43 -0700670 DispatchEntry* obtainDispatchEntry(EventEntry* eventEntry,
Jeff Brown53a415e2010-09-15 15:18:56 -0700671 int32_t targetFlags, float xOffset, float yOffset);
Jeff Brown54bc2812010-06-15 01:31:58 -0700672 CommandEntry* obtainCommandEntry(Command command);
Jeff Browne839a582010-04-22 18:58:52 -0700673
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700674 void releaseInjectionState(InjectionState* injectionState);
Jeff Browne839a582010-04-22 18:58:52 -0700675 void releaseEventEntry(EventEntry* entry);
676 void releaseConfigurationChangedEntry(ConfigurationChangedEntry* entry);
677 void releaseKeyEntry(KeyEntry* entry);
678 void releaseMotionEntry(MotionEntry* entry);
679 void releaseDispatchEntry(DispatchEntry* entry);
Jeff Brown54bc2812010-06-15 01:31:58 -0700680 void releaseCommandEntry(CommandEntry* entry);
Jeff Browne839a582010-04-22 18:58:52 -0700681
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700682 void recycleKeyEntry(KeyEntry* entry);
683
Jeff Browne839a582010-04-22 18:58:52 -0700684 void appendMotionSample(MotionEntry* motionEntry,
Jeff Brown51d45a72010-06-17 20:52:56 -0700685 nsecs_t eventTime, const PointerCoords* pointerCoords);
Jeff Browne839a582010-04-22 18:58:52 -0700686
687 private:
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700688 Pool<InjectionState> mInjectionStatePool;
Jeff Browne839a582010-04-22 18:58:52 -0700689 Pool<ConfigurationChangedEntry> mConfigurationChangeEntryPool;
690 Pool<KeyEntry> mKeyEntryPool;
691 Pool<MotionEntry> mMotionEntryPool;
692 Pool<MotionSample> mMotionSamplePool;
693 Pool<DispatchEntry> mDispatchEntryPool;
Jeff Brown54bc2812010-06-15 01:31:58 -0700694 Pool<CommandEntry> mCommandEntryPool;
Jeff Brown51d45a72010-06-17 20:52:56 -0700695
Jeff Brown90f0cee2010-10-08 22:31:17 -0700696 void initializeEventEntry(EventEntry* entry, int32_t type, nsecs_t eventTime,
697 uint32_t policyFlags);
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700698 void releaseEventEntryInjectionState(EventEntry* entry);
Jeff Browne839a582010-04-22 18:58:52 -0700699 };
700
Jeff Browna665ca82010-09-08 11:49:43 -0700701 /* Tracks dispatched key and motion event state so that cancelation events can be
702 * synthesized when events are dropped. */
703 class InputState {
704 public:
705 // Specifies whether a given event will violate input state consistency.
706 enum Consistency {
707 // The event is consistent with the current input state.
708 CONSISTENT,
709 // The event is inconsistent with the current input state but applications
710 // will tolerate it. eg. Down followed by another down.
711 TOLERABLE,
712 // The event is inconsistent with the current input state and will probably
713 // cause applications to crash. eg. Up without prior down, move with
714 // unexpected number of pointers.
715 BROKEN
716 };
717
Jeff Brown90f0cee2010-10-08 22:31:17 -0700718 // Specifies the sources to cancel.
719 enum CancelationOptions {
720 CANCEL_ALL_EVENTS = 0,
721 CANCEL_POINTER_EVENTS = 1,
722 CANCEL_NON_POINTER_EVENTS = 2,
723 };
724
Jeff Browna665ca82010-09-08 11:49:43 -0700725 InputState();
726 ~InputState();
727
728 // Returns true if there is no state to be canceled.
729 bool isNeutral() const;
730
Jeff Browna665ca82010-09-08 11:49:43 -0700731 // Records tracking information for an event that has just been published.
732 // Returns whether the event is consistent with the current input state.
733 Consistency trackEvent(const EventEntry* entry);
734
735 // Records tracking information for a key event that has just been published.
736 // Returns whether the event is consistent with the current input state.
737 Consistency trackKey(const KeyEntry* entry);
738
739 // Records tracking information for a motion event that has just been published.
740 // Returns whether the event is consistent with the current input state.
741 Consistency trackMotion(const MotionEntry* entry);
742
Jeff Brown90f0cee2010-10-08 22:31:17 -0700743 // Synthesizes cancelation events for the current state and resets the tracked state.
744 void synthesizeCancelationEvents(nsecs_t currentTime, Allocator* allocator,
745 Vector<EventEntry*>& outEvents, CancelationOptions options);
Jeff Browna665ca82010-09-08 11:49:43 -0700746
747 // Clears the current state.
748 void clear();
749
750 private:
Jeff Browna665ca82010-09-08 11:49:43 -0700751 struct KeyMemento {
752 int32_t deviceId;
753 int32_t source;
754 int32_t keyCode;
755 int32_t scanCode;
756 nsecs_t downTime;
757 };
758
759 struct MotionMemento {
760 int32_t deviceId;
761 int32_t source;
762 float xPrecision;
763 float yPrecision;
764 nsecs_t downTime;
765 uint32_t pointerCount;
766 int32_t pointerIds[MAX_POINTERS];
767 PointerCoords pointerCoords[MAX_POINTERS];
768
769 void setPointers(const MotionEntry* entry);
770 };
771
772 Vector<KeyMemento> mKeyMementos;
773 Vector<MotionMemento> mMotionMementos;
Jeff Brown90f0cee2010-10-08 22:31:17 -0700774
775 static bool shouldCancelEvent(int32_t eventSource, CancelationOptions options);
Jeff Browna665ca82010-09-08 11:49:43 -0700776 };
777
Jeff Browne839a582010-04-22 18:58:52 -0700778 /* Manages the dispatch state associated with a single input channel. */
779 class Connection : public RefBase {
780 protected:
781 virtual ~Connection();
782
783 public:
784 enum Status {
785 // Everything is peachy.
786 STATUS_NORMAL,
787 // An unrecoverable communication error has occurred.
788 STATUS_BROKEN,
Jeff Browne839a582010-04-22 18:58:52 -0700789 // The input channel has been unregistered.
790 STATUS_ZOMBIE
791 };
792
793 Status status;
794 sp<InputChannel> inputChannel;
795 InputPublisher inputPublisher;
Jeff Browna665ca82010-09-08 11:49:43 -0700796 InputState inputState;
Jeff Browne839a582010-04-22 18:58:52 -0700797 Queue<DispatchEntry> outboundQueue;
Jeff Browne839a582010-04-22 18:58:52 -0700798
799 nsecs_t lastEventTime; // the time when the event was originally captured
800 nsecs_t lastDispatchTime; // the time when the last event was dispatched
Jeff Browne839a582010-04-22 18:58:52 -0700801
802 explicit Connection(const sp<InputChannel>& inputChannel);
803
Jeff Brown54bc2812010-06-15 01:31:58 -0700804 inline const char* getInputChannelName() const { return inputChannel->getName().string(); }
805
806 const char* getStatusLabel() const;
Jeff Browne839a582010-04-22 18:58:52 -0700807
808 // Finds a DispatchEntry in the outbound queue associated with the specified event.
809 // Returns NULL if not found.
810 DispatchEntry* findQueuedDispatchEntryForEvent(const EventEntry* eventEntry) const;
811
Jeff Browne839a582010-04-22 18:58:52 -0700812 // Gets the time since the current event was originally obtained from the input driver.
Jeff Browna665ca82010-09-08 11:49:43 -0700813 inline double getEventLatencyMillis(nsecs_t currentTime) const {
Jeff Browne839a582010-04-22 18:58:52 -0700814 return (currentTime - lastEventTime) / 1000000.0;
815 }
816
817 // Gets the time since the current event entered the outbound dispatch queue.
Jeff Browna665ca82010-09-08 11:49:43 -0700818 inline double getDispatchLatencyMillis(nsecs_t currentTime) const {
Jeff Browne839a582010-04-22 18:58:52 -0700819 return (currentTime - lastDispatchTime) / 1000000.0;
820 }
821
Jeff Browne839a582010-04-22 18:58:52 -0700822 status_t initialize();
823 };
824
Jeff Brown90f0cee2010-10-08 22:31:17 -0700825 enum DropReason {
826 DROP_REASON_NOT_DROPPED = 0,
827 DROP_REASON_POLICY = 1,
828 DROP_REASON_APP_SWITCH = 2,
829 DROP_REASON_DISABLED = 3,
830 };
831
Jeff Brown54bc2812010-06-15 01:31:58 -0700832 sp<InputDispatcherPolicyInterface> mPolicy;
Jeff Browne839a582010-04-22 18:58:52 -0700833
834 Mutex mLock;
835
Jeff Browne839a582010-04-22 18:58:52 -0700836 Allocator mAllocator;
Jeff Brown59abe7e2010-09-13 23:17:30 -0700837 sp<Looper> mLooper;
Jeff Browne839a582010-04-22 18:58:52 -0700838
Jeff Browna665ca82010-09-08 11:49:43 -0700839 EventEntry* mPendingEvent;
Jeff Brown54bc2812010-06-15 01:31:58 -0700840 Queue<EventEntry> mInboundQueue;
841 Queue<CommandEntry> mCommandQueue;
842
Jeff Browna665ca82010-09-08 11:49:43 -0700843 Vector<EventEntry*> mTempCancelationEvents;
844
845 void dispatchOnceInnerLocked(nsecs_t keyRepeatTimeout, nsecs_t keyRepeatDelay,
846 nsecs_t* nextWakeupTime);
847
Jeff Brown59abe7e2010-09-13 23:17:30 -0700848 // Enqueues an inbound event. Returns true if mLooper->wake() should be called.
Jeff Browna665ca82010-09-08 11:49:43 -0700849 bool enqueueInboundEventLocked(EventEntry* entry);
850
Jeff Brown90f0cee2010-10-08 22:31:17 -0700851 // Cleans up input state when dropping an inbound event.
852 void dropInboundEventLocked(EventEntry* entry, DropReason dropReason);
853
Jeff Browna665ca82010-09-08 11:49:43 -0700854 // App switch latency optimization.
Jeff Brown90f0cee2010-10-08 22:31:17 -0700855 bool mAppSwitchSawKeyDown;
Jeff Browna665ca82010-09-08 11:49:43 -0700856 nsecs_t mAppSwitchDueTime;
857
Jeff Brown90f0cee2010-10-08 22:31:17 -0700858 static bool isAppSwitchKeyCode(int32_t keyCode);
859 bool isAppSwitchKeyEventLocked(KeyEntry* keyEntry);
Jeff Browna665ca82010-09-08 11:49:43 -0700860 bool isAppSwitchPendingLocked();
Jeff Browna665ca82010-09-08 11:49:43 -0700861 void resetPendingAppSwitchLocked(bool handled);
862
Jeff Browne839a582010-04-22 18:58:52 -0700863 // All registered connections mapped by receive pipe file descriptor.
864 KeyedVector<int, sp<Connection> > mConnectionsByReceiveFd;
865
Jeff Brown53a415e2010-09-15 15:18:56 -0700866 ssize_t getConnectionIndexLocked(const sp<InputChannel>& inputChannel);
Jeff Brown0cacb872010-08-17 15:59:26 -0700867
Jeff Browne839a582010-04-22 18:58:52 -0700868 // Active connections are connections that have a non-empty outbound queue.
Jeff Brown51d45a72010-06-17 20:52:56 -0700869 // We don't use a ref-counted pointer here because we explicitly abort connections
870 // during unregistration which causes the connection's outbound queue to be cleared
871 // and the connection itself to be deactivated.
Jeff Browne839a582010-04-22 18:58:52 -0700872 Vector<Connection*> mActiveConnections;
873
Jeff Browna665ca82010-09-08 11:49:43 -0700874 // Input channels that will receive a copy of all input events.
875 Vector<sp<InputChannel> > mMonitoringChannels;
Jeff Browne839a582010-04-22 18:58:52 -0700876
Jeff Browna665ca82010-09-08 11:49:43 -0700877 // Preallocated key event object used for policy inquiries.
878 KeyEvent mReusableKeyEvent;
Jeff Browne839a582010-04-22 18:58:52 -0700879
Jeff Brown51d45a72010-06-17 20:52:56 -0700880 // Event injection and synchronization.
881 Condition mInjectionResultAvailableCondition;
Jeff Brown90f0cee2010-10-08 22:31:17 -0700882 bool hasInjectionPermission(int32_t injectorPid, int32_t injectorUid);
Jeff Brown51d45a72010-06-17 20:52:56 -0700883 void setInjectionResultLocked(EventEntry* entry, int32_t injectionResult);
884
Jeff Brownf67c53e2010-07-28 15:48:59 -0700885 Condition mInjectionSyncFinishedCondition;
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700886 void incrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brown53a415e2010-09-15 15:18:56 -0700887 void decrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brownf67c53e2010-07-28 15:48:59 -0700888
Jeff Brown542412c2010-08-18 15:51:08 -0700889 // Throttling state.
890 struct ThrottleState {
891 nsecs_t minTimeBetweenEvents;
892
893 nsecs_t lastEventTime;
894 int32_t lastDeviceId;
895 uint32_t lastSource;
896
897 uint32_t originalSampleCount; // only collected during debugging
898 } mThrottleState;
899
Jeff Browne839a582010-04-22 18:58:52 -0700900 // Key repeat tracking.
Jeff Browne839a582010-04-22 18:58:52 -0700901 struct KeyRepeatState {
902 KeyEntry* lastKeyEntry; // or null if no repeat
903 nsecs_t nextRepeatTime;
904 } mKeyRepeatState;
905
906 void resetKeyRepeatLocked();
Jeff Browna665ca82010-09-08 11:49:43 -0700907 KeyEntry* synthesizeKeyRepeatLocked(nsecs_t currentTime, nsecs_t keyRepeatTimeout);
Jeff Browne839a582010-04-22 18:58:52 -0700908
Jeff Brown54bc2812010-06-15 01:31:58 -0700909 // Deferred command processing.
910 bool runCommandsLockedInterruptible();
911 CommandEntry* postCommandLocked(Command command);
912
Jeff Browna665ca82010-09-08 11:49:43 -0700913 // Inbound event processing.
914 void drainInboundQueueLocked();
Jeff Brownd8816c32010-09-16 14:07:33 -0700915 void releasePendingEventLocked();
916 void releaseInboundEventLocked(EventEntry* entry);
Jeff Browne839a582010-04-22 18:58:52 -0700917
Jeff Browna665ca82010-09-08 11:49:43 -0700918 // Dispatch state.
919 bool mDispatchEnabled;
920 bool mDispatchFrozen;
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700921
Jeff Browna665ca82010-09-08 11:49:43 -0700922 Vector<InputWindow> mWindows;
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700923
924 const InputWindow* getWindowLocked(const sp<InputChannel>& inputChannel);
Jeff Browna665ca82010-09-08 11:49:43 -0700925
926 // Focus tracking for keys, trackball, etc.
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700927 const InputWindow* mFocusedWindow;
Jeff Browna665ca82010-09-08 11:49:43 -0700928
929 // Focus tracking for touch.
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700930 struct TouchedWindow {
931 const InputWindow* window;
932 int32_t targetFlags;
933 BitSet32 pointerIds;
934 sp<InputChannel> channel;
Jeff Browna665ca82010-09-08 11:49:43 -0700935 };
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700936 struct TouchState {
937 bool down;
938 bool split;
939 Vector<TouchedWindow> windows;
940
941 TouchState();
942 ~TouchState();
943 void reset();
944 void copyFrom(const TouchState& other);
945 void addOrUpdateWindow(const InputWindow* window, int32_t targetFlags, BitSet32 pointerIds);
946 void removeOutsideTouchWindows();
947 const InputWindow* getFirstForegroundWindow();
948 };
949
950 TouchState mTouchState;
951 TouchState mTempTouchState;
Jeff Browna665ca82010-09-08 11:49:43 -0700952
953 // Focused application.
954 InputApplication* mFocusedApplication;
955 InputApplication mFocusedApplicationStorage; // preallocated storage for mFocusedApplication
956 void releaseFocusedApplicationLocked();
957
958 // Dispatch inbound events.
959 bool dispatchConfigurationChangedLocked(
960 nsecs_t currentTime, ConfigurationChangedEntry* entry);
961 bool dispatchKeyLocked(
962 nsecs_t currentTime, KeyEntry* entry, nsecs_t keyRepeatTimeout,
Jeff Brown1fe6dec2010-10-11 14:20:19 -0700963 DropReason* dropReason, nsecs_t* nextWakeupTime);
Jeff Browna665ca82010-09-08 11:49:43 -0700964 bool dispatchMotionLocked(
965 nsecs_t currentTime, MotionEntry* entry,
Jeff Brown1fe6dec2010-10-11 14:20:19 -0700966 DropReason* dropReason, nsecs_t* nextWakeupTime);
Jeff Brown54bc2812010-06-15 01:31:58 -0700967 void dispatchEventToCurrentInputTargetsLocked(
968 nsecs_t currentTime, EventEntry* entry, bool resumeWithAppendedMotionSample);
Jeff Browne839a582010-04-22 18:58:52 -0700969
Jeff Browna665ca82010-09-08 11:49:43 -0700970 void logOutboundKeyDetailsLocked(const char* prefix, const KeyEntry* entry);
971 void logOutboundMotionDetailsLocked(const char* prefix, const MotionEntry* entry);
972
973 // The input targets that were most recently identified for dispatch.
Jeff Browna665ca82010-09-08 11:49:43 -0700974 bool mCurrentInputTargetsValid; // false while targets are being recomputed
975 Vector<InputTarget> mCurrentInputTargets;
Jeff Browna665ca82010-09-08 11:49:43 -0700976
977 enum InputTargetWaitCause {
978 INPUT_TARGET_WAIT_CAUSE_NONE,
979 INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY,
980 INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY,
981 };
982
983 InputTargetWaitCause mInputTargetWaitCause;
984 nsecs_t mInputTargetWaitStartTime;
985 nsecs_t mInputTargetWaitTimeoutTime;
986 bool mInputTargetWaitTimeoutExpired;
987
988 // Finding targets for input events.
Jeff Brownd8816c32010-09-16 14:07:33 -0700989 void resetTargetsLocked();
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700990 void commitTargetsLocked();
Jeff Browna665ca82010-09-08 11:49:43 -0700991 int32_t handleTargetsNotReadyLocked(nsecs_t currentTime, const EventEntry* entry,
992 const InputApplication* application, const InputWindow* window,
993 nsecs_t* nextWakeupTime);
Jeff Brown53a415e2010-09-15 15:18:56 -0700994 void resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout,
995 const sp<InputChannel>& inputChannel);
996 nsecs_t getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime);
Jeff Browna665ca82010-09-08 11:49:43 -0700997 void resetANRTimeoutsLocked();
998
Jeff Brownd1b0a2b2010-09-26 22:20:12 -0700999 int32_t findFocusedWindowTargetsLocked(nsecs_t currentTime, const EventEntry* entry,
1000 nsecs_t* nextWakeupTime);
1001 int32_t findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry* entry,
1002 nsecs_t* nextWakeupTime);
Jeff Browna665ca82010-09-08 11:49:43 -07001003
Jeff Brownd1b0a2b2010-09-26 22:20:12 -07001004 void addWindowTargetLocked(const InputWindow* window, int32_t targetFlags,
1005 BitSet32 pointerIds);
Jeff Browna665ca82010-09-08 11:49:43 -07001006 void addMonitoringTargetsLocked();
Jeff Brownd1b0a2b2010-09-26 22:20:12 -07001007 bool shouldPokeUserActivityForCurrentInputTargetsLocked();
1008 void pokeUserActivityLocked(nsecs_t eventTime, int32_t eventType);
1009 bool checkInjectionPermission(const InputWindow* window, const InjectionState* injectionState);
Jeff Brown35cf0e92010-10-05 12:26:23 -07001010 bool isWindowObscuredAtPointLocked(const InputWindow* window, int32_t x, int32_t y) const;
Jeff Brown53a415e2010-09-15 15:18:56 -07001011 bool isWindowFinishedWithPreviousInputLocked(const InputWindow* window);
Jeff Brown53a415e2010-09-15 15:18:56 -07001012 String8 getApplicationWindowLabelLocked(const InputApplication* application,
1013 const InputWindow* window);
Jeff Browna665ca82010-09-08 11:49:43 -07001014
Jeff Browne839a582010-04-22 18:58:52 -07001015 // Manage the dispatch cycle for a single connection.
Jeff Brown51d45a72010-06-17 20:52:56 -07001016 // These methods are deliberately not Interruptible because doing all of the work
1017 // with the mutex held makes it easier to ensure that connection invariants are maintained.
1018 // If needed, the methods post commands to run later once the critical bits are done.
1019 void prepareDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Jeff Browne839a582010-04-22 18:58:52 -07001020 EventEntry* eventEntry, const InputTarget* inputTarget,
1021 bool resumeWithAppendedMotionSample);
Jeff Brown53a415e2010-09-15 15:18:56 -07001022 void startDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown51d45a72010-06-17 20:52:56 -07001023 void finishDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Browna665ca82010-09-08 11:49:43 -07001024 void startNextDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown90f0cee2010-10-08 22:31:17 -07001025 void abortBrokenDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown53a415e2010-09-15 15:18:56 -07001026 void drainOutboundQueueLocked(Connection* connection);
Jeff Brown59abe7e2010-09-13 23:17:30 -07001027 static int handleReceiveCallback(int receiveFd, int events, void* data);
Jeff Browne839a582010-04-22 18:58:52 -07001028
Jeff Brown90f0cee2010-10-08 22:31:17 -07001029 void synthesizeCancelationEventsForAllConnectionsLocked(
1030 InputState::CancelationOptions options, const char* reason);
1031 void synthesizeCancelationEventsForInputChannelLocked(const sp<InputChannel>& channel,
1032 InputState::CancelationOptions options, const char* reason);
1033 void synthesizeCancelationEventsForConnectionLocked(const sp<Connection>& connection,
1034 InputState::CancelationOptions options, const char* reason);
1035
Jeff Brownd1b0a2b2010-09-26 22:20:12 -07001036 // Splitting motion events across windows.
1037 MotionEntry* splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds);
1038
Jeff Browna665ca82010-09-08 11:49:43 -07001039 // Dump state.
1040 void dumpDispatchStateLocked(String8& dump);
1041 void logDispatchStateLocked();
1042
Jeff Browne839a582010-04-22 18:58:52 -07001043 // Add or remove a connection to the mActiveConnections vector.
1044 void activateConnectionLocked(Connection* connection);
1045 void deactivateConnectionLocked(Connection* connection);
1046
1047 // Interesting events that we might like to log or tell the framework about.
Jeff Brown54bc2812010-06-15 01:31:58 -07001048 void onDispatchCycleStartedLocked(
Jeff Brown51d45a72010-06-17 20:52:56 -07001049 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown54bc2812010-06-15 01:31:58 -07001050 void onDispatchCycleFinishedLocked(
Jeff Brown51d45a72010-06-17 20:52:56 -07001051 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown54bc2812010-06-15 01:31:58 -07001052 void onDispatchCycleBrokenLocked(
Jeff Brown51d45a72010-06-17 20:52:56 -07001053 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown53a415e2010-09-15 15:18:56 -07001054 void onANRLocked(
1055 nsecs_t currentTime, const InputApplication* application, const InputWindow* window,
1056 nsecs_t eventTime, nsecs_t waitStartTime);
Jeff Brown54bc2812010-06-15 01:31:58 -07001057
Jeff Brown51d45a72010-06-17 20:52:56 -07001058 // Outbound policy interactions.
Jeff Browna665ca82010-09-08 11:49:43 -07001059 void doNotifyConfigurationChangedInterruptible(CommandEntry* commandEntry);
Jeff Brown54bc2812010-06-15 01:31:58 -07001060 void doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown53a415e2010-09-15 15:18:56 -07001061 void doNotifyANRLockedInterruptible(CommandEntry* commandEntry);
Jeff Browna665ca82010-09-08 11:49:43 -07001062 void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry);
1063 void doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown53a415e2010-09-15 15:18:56 -07001064
1065 // Statistics gathering.
1066 void updateDispatchStatisticsLocked(nsecs_t currentTime, const EventEntry* entry,
1067 int32_t injectionResult, nsecs_t timeSpentWaitingForApplication);
Jeff Browne839a582010-04-22 18:58:52 -07001068};
1069
1070/* Enqueues and dispatches input events, endlessly. */
1071class InputDispatcherThread : public Thread {
1072public:
1073 explicit InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher);
1074 ~InputDispatcherThread();
1075
1076private:
1077 virtual bool threadLoop();
1078
1079 sp<InputDispatcherInterface> mDispatcher;
1080};
1081
1082} // namespace android
1083
Jeff Browna665ca82010-09-08 11:49:43 -07001084#endif // _UI_INPUT_DISPATCHER_H