blob: af0153b8c2c2823ee053ffe41ee1fd3bf239c7bf [file] [log] [blame]
Jeff Brown46b9ac02010-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 Brown46b9ac02010-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 Brown4fe6c3e2010-09-13 23:17:30 -070028#include <utils/Looper.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070029#include <utils/Pool.h>
Jeff Brown01ce2e92010-09-26 22:20:12 -070030#include <utils/BitSet.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070031
32#include <stddef.h>
33#include <unistd.h>
Jeff Brownb88102f2010-09-08 11:49:43 -070034#include <limits.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070035
Jeff Brown928e0542011-01-10 11:17:36 -080036#include "InputWindow.h"
37#include "InputApplication.h"
38
Jeff Brown46b9ac02010-04-22 18:58:52 -070039
40namespace android {
41
Jeff Brown9c3cda02010-06-15 01:31:58 -070042/*
Jeff Brown7fbdc842010-06-17 20:52:56 -070043 * Constants used to report the outcome of input event injection.
44 */
45enum {
46 /* (INTERNAL USE ONLY) Specifies that injection is pending and its outcome is unknown. */
47 INPUT_EVENT_INJECTION_PENDING = -1,
48
49 /* Injection succeeded. */
50 INPUT_EVENT_INJECTION_SUCCEEDED = 0,
51
52 /* Injection failed because the injector did not have permission to inject
53 * into the application with input focus. */
54 INPUT_EVENT_INJECTION_PERMISSION_DENIED = 1,
55
56 /* Injection failed because there were no available input targets. */
57 INPUT_EVENT_INJECTION_FAILED = 2,
58
59 /* Injection failed due to a timeout. */
60 INPUT_EVENT_INJECTION_TIMED_OUT = 3
61};
62
Jeff Brown6ec402b2010-07-28 15:48:59 -070063/*
64 * Constants used to determine the input event injection synchronization mode.
65 */
66enum {
67 /* Injection is asynchronous and is assumed always to be successful. */
68 INPUT_EVENT_INJECTION_SYNC_NONE = 0,
69
70 /* Waits for previous events to be dispatched so that the input dispatcher can determine
71 * whether input event injection willbe permitted based on the current input focus.
72 * Does not wait for the input event to finish processing. */
73 INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT = 1,
74
75 /* Waits for the input event to be completely processed. */
76 INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISHED = 2,
77};
78
Jeff Brown7fbdc842010-06-17 20:52:56 -070079
80/*
Jeff Brown9c3cda02010-06-15 01:31:58 -070081 * An input target specifies how an input event is to be dispatched to a particular window
82 * including the window's input channel, control flags, a timeout, and an X / Y offset to
83 * be added to input event coordinates to compensate for the absolute position of the
84 * window area.
85 */
86struct InputTarget {
87 enum {
Jeff Brown519e0242010-09-15 15:18:56 -070088 /* This flag indicates that the event is being delivered to a foreground application. */
Jeff Browna032cc02011-03-07 16:56:21 -080089 FLAG_FOREGROUND = 1 << 0,
Jeff Brown9c3cda02010-06-15 01:31:58 -070090
Jeff Brown85a31762010-09-01 17:01:00 -070091 /* This flag indicates that the target of a MotionEvent is partly or wholly
92 * obscured by another visible window above it. The motion event should be
93 * delivered with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED. */
Jeff Browna032cc02011-03-07 16:56:21 -080094 FLAG_WINDOW_IS_OBSCURED = 1 << 1,
Jeff Brown01ce2e92010-09-26 22:20:12 -070095
96 /* This flag indicates that a motion event is being split across multiple windows. */
Jeff Browna032cc02011-03-07 16:56:21 -080097 FLAG_SPLIT = 1 << 2,
98
99 /* This flag indicates that the event should be sent as is.
100 * Should always be set unless the event is to be transmuted. */
101 FLAG_DISPATCH_AS_IS = 1 << 8,
102
103 /* This flag indicates that a MotionEvent with AMOTION_EVENT_ACTION_DOWN falls outside
104 * of the area of this target and so should instead be delivered as an
105 * AMOTION_EVENT_ACTION_OUTSIDE to this target. */
106 FLAG_DISPATCH_AS_OUTSIDE = 1 << 9,
107
108 /* This flag indicates that a hover sequence is starting in the given window.
109 * The event is transmuted into ACTION_HOVER_ENTER. */
110 FLAG_DISPATCH_AS_HOVER_ENTER = 1 << 10,
111
112 /* This flag indicates that a hover event happened outside of a window which handled
113 * previous hover events, signifying the end of the current hover sequence for that
114 * window.
115 * The event is transmuted into ACTION_HOVER_ENTER. */
116 FLAG_DISPATCH_AS_HOVER_EXIT = 1 << 11,
117
118 /* Mask for all dispatch modes. */
119 FLAG_DISPATCH_MASK = FLAG_DISPATCH_AS_IS
120 | FLAG_DISPATCH_AS_OUTSIDE
121 | FLAG_DISPATCH_AS_HOVER_ENTER
122 | FLAG_DISPATCH_AS_HOVER_EXIT,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700123 };
124
125 // The input channel to be targeted.
126 sp<InputChannel> inputChannel;
127
128 // Flags for the input target.
129 int32_t flags;
130
Jeff Brown9c3cda02010-06-15 01:31:58 -0700131 // The x and y offset to add to a MotionEvent as it is delivered.
132 // (ignored for KeyEvents)
133 float xOffset, yOffset;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700134
Jeff Brown01ce2e92010-09-26 22:20:12 -0700135 // The subset of pointer ids to include in motion events dispatched to this input target
136 // if FLAG_SPLIT is set.
137 BitSet32 pointerIds;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700138};
139
Jeff Brown7fbdc842010-06-17 20:52:56 -0700140
Jeff Brown9c3cda02010-06-15 01:31:58 -0700141/*
142 * Input dispatcher policy interface.
143 *
144 * The input reader policy is used by the input reader to interact with the Window Manager
145 * and other system components.
146 *
147 * The actual implementation is partially supported by callbacks into the DVM
148 * via JNI. This interface is also mocked in the unit tests.
149 */
150class InputDispatcherPolicyInterface : public virtual RefBase {
151protected:
152 InputDispatcherPolicyInterface() { }
153 virtual ~InputDispatcherPolicyInterface() { }
154
155public:
156 /* Notifies the system that a configuration change has occurred. */
157 virtual void notifyConfigurationChanged(nsecs_t when) = 0;
158
Jeff Brownb88102f2010-09-08 11:49:43 -0700159 /* Notifies the system that an application is not responding.
160 * Returns a new timeout to continue waiting, or 0 to abort dispatch. */
Jeff Brown519e0242010-09-15 15:18:56 -0700161 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brown928e0542011-01-10 11:17:36 -0800162 const sp<InputWindowHandle>& inputWindowHandle) = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700163
Jeff Brown9c3cda02010-06-15 01:31:58 -0700164 /* Notifies the system that an input channel is unrecoverably broken. */
Jeff Brown928e0542011-01-10 11:17:36 -0800165 virtual void notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700166
Jeff Brownb21fb102010-09-07 10:44:57 -0700167 /* Gets the key repeat initial timeout or -1 if automatic key repeating is disabled. */
Jeff Brown9c3cda02010-06-15 01:31:58 -0700168 virtual nsecs_t getKeyRepeatTimeout() = 0;
169
Jeff Brownb21fb102010-09-07 10:44:57 -0700170 /* Gets the key repeat inter-key delay. */
171 virtual nsecs_t getKeyRepeatDelay() = 0;
172
Jeff Brownae9fc032010-08-18 15:51:08 -0700173 /* Gets the maximum suggested event delivery rate per second.
174 * This value is used to throttle motion event movement actions on a per-device
175 * basis. It is not intended to be a hard limit.
176 */
177 virtual int32_t getMaxEventsPerSecond() = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700178
Jeff Brown0029c662011-03-30 02:25:18 -0700179 /* Filters an input event.
180 * Return true to dispatch the event unmodified, false to consume the event.
181 * A filter can also transform and inject events later by passing POLICY_FLAG_FILTERED
182 * to injectInputEvent.
183 */
184 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) = 0;
185
Jeff Brownb6997262010-10-08 22:31:17 -0700186 /* Intercepts a key event immediately before queueing it.
187 * The policy can use this method as an opportunity to perform power management functions
188 * and early event preprocessing such as updating policy flags.
189 *
190 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
191 * should be dispatched to applications.
192 */
Jeff Brown1f245102010-11-18 20:53:46 -0800193 virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags) = 0;
Jeff Brownb6997262010-10-08 22:31:17 -0700194
Jeff Brown56194eb2011-03-02 19:23:13 -0800195 /* Intercepts a touch, trackball or other motion event before queueing it.
Jeff Brownb6997262010-10-08 22:31:17 -0700196 * The policy can use this method as an opportunity to perform power management functions
197 * and early event preprocessing such as updating policy flags.
198 *
199 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
200 * should be dispatched to applications.
201 */
Jeff Brown56194eb2011-03-02 19:23:13 -0800202 virtual void interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) = 0;
Jeff Brownb6997262010-10-08 22:31:17 -0700203
Jeff Brownb88102f2010-09-08 11:49:43 -0700204 /* Allows the policy a chance to intercept a key before dispatching. */
Jeff Brown928e0542011-01-10 11:17:36 -0800205 virtual bool interceptKeyBeforeDispatching(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brownb88102f2010-09-08 11:49:43 -0700206 const KeyEvent* keyEvent, uint32_t policyFlags) = 0;
207
Jeff Brown49ed71d2010-12-06 17:13:33 -0800208 /* Allows the policy a chance to perform default processing for an unhandled key.
209 * Returns an alternate keycode to redispatch as a fallback, or 0 to give up. */
Jeff Brown928e0542011-01-10 11:17:36 -0800210 virtual bool dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800211 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) = 0;
Jeff Brown3915bb82010-11-05 15:02:16 -0700212
Jeff Brownb6997262010-10-08 22:31:17 -0700213 /* Notifies the policy about switch events.
214 */
215 virtual void notifySwitch(nsecs_t when,
216 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) = 0;
217
Jeff Brownb88102f2010-09-08 11:49:43 -0700218 /* Poke user activity for an event dispatched to a window. */
Jeff Brown01ce2e92010-09-26 22:20:12 -0700219 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType) = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700220
221 /* Checks whether a given application pid/uid has permission to inject input events
222 * into other applications.
223 *
224 * This method is special in that its implementation promises to be non-reentrant and
225 * is safe to call while holding other locks. (Most other methods make no such guarantees!)
226 */
227 virtual bool checkInjectEventsPermissionNonReentrant(
228 int32_t injectorPid, int32_t injectorUid) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700229};
230
231
Jeff Brown46b9ac02010-04-22 18:58:52 -0700232/* Notifies the system about input events generated by the input reader.
233 * The dispatcher is expected to be mostly asynchronous. */
234class InputDispatcherInterface : public virtual RefBase {
235protected:
236 InputDispatcherInterface() { }
237 virtual ~InputDispatcherInterface() { }
238
239public:
Jeff Brownb88102f2010-09-08 11:49:43 -0700240 /* Dumps the state of the input dispatcher.
241 *
242 * This method may be called on any thread (usually by the input manager). */
243 virtual void dump(String8& dump) = 0;
244
Jeff Brown46b9ac02010-04-22 18:58:52 -0700245 /* Runs a single iteration of the dispatch loop.
246 * Nominally processes one queued event, a timeout, or a response from an input consumer.
247 *
248 * This method should only be called on the input dispatcher thread.
249 */
250 virtual void dispatchOnce() = 0;
251
252 /* Notifies the dispatcher about new events.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700253 *
254 * These methods should only be called on the input reader thread.
255 */
Jeff Brown9c3cda02010-06-15 01:31:58 -0700256 virtual void notifyConfigurationChanged(nsecs_t eventTime) = 0;
Jeff Brown58a2da82011-01-25 16:02:22 -0800257 virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, uint32_t source,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700258 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode,
259 int32_t scanCode, int32_t metaState, nsecs_t downTime) = 0;
Jeff Brown58a2da82011-01-25 16:02:22 -0800260 virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, uint32_t source,
Jeff Brown85a31762010-09-01 17:01:00 -0700261 uint32_t policyFlags, int32_t action, int32_t flags,
262 int32_t metaState, int32_t edgeFlags,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700263 uint32_t pointerCount, const int32_t* pointerIds, const PointerCoords* pointerCoords,
264 float xPrecision, float yPrecision, nsecs_t downTime) = 0;
Jeff Brownb6997262010-10-08 22:31:17 -0700265 virtual void notifySwitch(nsecs_t when,
266 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700267
Jeff Brown7fbdc842010-06-17 20:52:56 -0700268 /* Injects an input event and optionally waits for sync.
Jeff Brown6ec402b2010-07-28 15:48:59 -0700269 * The synchronization mode determines whether the method blocks while waiting for
270 * input injection to proceed.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700271 * Returns one of the INPUT_EVENT_INJECTION_XXX constants.
272 *
273 * This method may be called on any thread (usually by the input manager).
274 */
275 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brown0029c662011-03-30 02:25:18 -0700276 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,
277 uint32_t policyFlags) = 0;
Jeff Brown7fbdc842010-06-17 20:52:56 -0700278
Jeff Brownb88102f2010-09-08 11:49:43 -0700279 /* Sets the list of input windows.
280 *
281 * This method may be called on any thread (usually by the input manager).
282 */
283 virtual void setInputWindows(const Vector<InputWindow>& inputWindows) = 0;
284
285 /* Sets the focused application.
286 *
287 * This method may be called on any thread (usually by the input manager).
288 */
289 virtual void setFocusedApplication(const InputApplication* inputApplication) = 0;
290
291 /* Sets the input dispatching mode.
292 *
293 * This method may be called on any thread (usually by the input manager).
294 */
295 virtual void setInputDispatchMode(bool enabled, bool frozen) = 0;
296
Jeff Brown0029c662011-03-30 02:25:18 -0700297 /* Sets whether input event filtering is enabled.
298 * When enabled, incoming input events are sent to the policy's filterInputEvent
299 * method instead of being dispatched. The filter is expected to use
300 * injectInputEvent to inject the events it would like to have dispatched.
301 * It should include POLICY_FLAG_FILTERED in the policy flags during injection.
302 */
303 virtual void setInputFilterEnabled(bool enabled) = 0;
304
Jeff Browne6504122010-09-27 14:52:15 -0700305 /* Transfers touch focus from the window associated with one channel to the
306 * window associated with the other channel.
307 *
308 * Returns true on success. False if the window did not actually have touch focus.
309 */
310 virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel,
311 const sp<InputChannel>& toChannel) = 0;
312
Jeff Brown46b9ac02010-04-22 18:58:52 -0700313 /* Registers or unregister input channels that may be used as targets for input events.
Jeff Brownb88102f2010-09-08 11:49:43 -0700314 * If monitor is true, the channel will receive a copy of all input events.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700315 *
316 * These methods may be called on any thread (usually by the input manager).
317 */
Jeff Brown928e0542011-01-10 11:17:36 -0800318 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel,
319 const sp<InputWindowHandle>& inputWindowHandle, bool monitor) = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700320 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) = 0;
321};
322
Jeff Brown9c3cda02010-06-15 01:31:58 -0700323/* Dispatches events to input targets. Some functions of the input dispatcher, such as
324 * identifying input targets, are controlled by a separate policy object.
325 *
326 * IMPORTANT INVARIANT:
327 * Because the policy can potentially block or cause re-entrance into the input dispatcher,
328 * the input dispatcher never calls into the policy while holding its internal locks.
329 * The implementation is also carefully designed to recover from scenarios such as an
330 * input channel becoming unregistered while identifying input targets or processing timeouts.
331 *
332 * Methods marked 'Locked' must be called with the lock acquired.
333 *
334 * Methods marked 'LockedInterruptible' must be called with the lock acquired but
335 * may during the course of their execution release the lock, call into the policy, and
336 * then reacquire the lock. The caller is responsible for recovering gracefully.
337 *
338 * A 'LockedInterruptible' method may called a 'Locked' method, but NOT vice-versa.
339 */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700340class InputDispatcher : public InputDispatcherInterface {
341protected:
342 virtual ~InputDispatcher();
343
344public:
Jeff Brown9c3cda02010-06-15 01:31:58 -0700345 explicit InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700346
Jeff Brownb88102f2010-09-08 11:49:43 -0700347 virtual void dump(String8& dump);
348
Jeff Brown46b9ac02010-04-22 18:58:52 -0700349 virtual void dispatchOnce();
350
Jeff Brown9c3cda02010-06-15 01:31:58 -0700351 virtual void notifyConfigurationChanged(nsecs_t eventTime);
Jeff Brown58a2da82011-01-25 16:02:22 -0800352 virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, uint32_t source,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700353 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode,
354 int32_t scanCode, int32_t metaState, nsecs_t downTime);
Jeff Brown58a2da82011-01-25 16:02:22 -0800355 virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, uint32_t source,
Jeff Brown85a31762010-09-01 17:01:00 -0700356 uint32_t policyFlags, int32_t action, int32_t flags,
357 int32_t metaState, int32_t edgeFlags,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700358 uint32_t pointerCount, const int32_t* pointerIds, const PointerCoords* pointerCoords,
359 float xPrecision, float yPrecision, nsecs_t downTime);
Jeff Brownb6997262010-10-08 22:31:17 -0700360 virtual void notifySwitch(nsecs_t when,
361 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) ;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700362
Jeff Brown7fbdc842010-06-17 20:52:56 -0700363 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brown0029c662011-03-30 02:25:18 -0700364 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,
365 uint32_t policyFlags);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700366
Jeff Brownb88102f2010-09-08 11:49:43 -0700367 virtual void setInputWindows(const Vector<InputWindow>& inputWindows);
368 virtual void setFocusedApplication(const InputApplication* inputApplication);
369 virtual void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown0029c662011-03-30 02:25:18 -0700370 virtual void setInputFilterEnabled(bool enabled);
Jeff Brown349703e2010-06-22 01:27:15 -0700371
Jeff Browne6504122010-09-27 14:52:15 -0700372 virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel,
373 const sp<InputChannel>& toChannel);
374
Jeff Brown928e0542011-01-10 11:17:36 -0800375 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel,
376 const sp<InputWindowHandle>& inputWindowHandle, bool monitor);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700377 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel);
378
379private:
380 template <typename T>
381 struct Link {
382 T* next;
383 T* prev;
384 };
385
Jeff Brown01ce2e92010-09-26 22:20:12 -0700386 struct InjectionState {
387 mutable int32_t refCount;
388
389 int32_t injectorPid;
390 int32_t injectorUid;
391 int32_t injectionResult; // initially INPUT_EVENT_INJECTION_PENDING
392 bool injectionIsAsync; // set to true if injection is not waiting for the result
393 int32_t pendingForegroundDispatches; // the number of foreground dispatches in progress
394 };
395
Jeff Brown46b9ac02010-04-22 18:58:52 -0700396 struct EventEntry : Link<EventEntry> {
397 enum {
398 TYPE_SENTINEL,
399 TYPE_CONFIGURATION_CHANGED,
400 TYPE_KEY,
401 TYPE_MOTION
402 };
403
Jeff Brown01ce2e92010-09-26 22:20:12 -0700404 mutable int32_t refCount;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700405 int32_t type;
406 nsecs_t eventTime;
Jeff Brownb6997262010-10-08 22:31:17 -0700407 uint32_t policyFlags;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700408 InjectionState* injectionState;
Jeff Brown7fbdc842010-06-17 20:52:56 -0700409
Jeff Brown9c3cda02010-06-15 01:31:58 -0700410 bool dispatchInProgress; // initially false, set to true while dispatching
Jeff Brown7fbdc842010-06-17 20:52:56 -0700411
Jeff Brown4e91a182011-04-07 11:38:09 -0700412 inline bool isInjected() const { return injectionState != NULL; }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700413 };
414
415 struct ConfigurationChangedEntry : EventEntry {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700416 };
417
418 struct KeyEntry : EventEntry {
419 int32_t deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -0800420 uint32_t source;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700421 int32_t action;
422 int32_t flags;
423 int32_t keyCode;
424 int32_t scanCode;
425 int32_t metaState;
426 int32_t repeatCount;
427 nsecs_t downTime;
Jeff Brownb88102f2010-09-08 11:49:43 -0700428
429 bool syntheticRepeat; // set to true for synthetic key repeats
430
431 enum InterceptKeyResult {
432 INTERCEPT_KEY_RESULT_UNKNOWN,
433 INTERCEPT_KEY_RESULT_SKIP,
434 INTERCEPT_KEY_RESULT_CONTINUE,
435 };
436 InterceptKeyResult interceptKeyResult; // set based on the interception result
Jeff Brown46b9ac02010-04-22 18:58:52 -0700437 };
438
439 struct MotionSample {
440 MotionSample* next;
441
Jeff Brown4e91a182011-04-07 11:38:09 -0700442 nsecs_t eventTime; // may be updated during coalescing
443 nsecs_t eventTimeBeforeCoalescing; // not updated during coalescing
Jeff Brown46b9ac02010-04-22 18:58:52 -0700444 PointerCoords pointerCoords[MAX_POINTERS];
445 };
446
447 struct MotionEntry : EventEntry {
448 int32_t deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -0800449 uint32_t source;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700450 int32_t action;
Jeff Brown85a31762010-09-01 17:01:00 -0700451 int32_t flags;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700452 int32_t metaState;
453 int32_t edgeFlags;
454 float xPrecision;
455 float yPrecision;
456 nsecs_t downTime;
457 uint32_t pointerCount;
458 int32_t pointerIds[MAX_POINTERS];
459
460 // Linked list of motion samples associated with this motion event.
461 MotionSample firstSample;
462 MotionSample* lastSample;
Jeff Brownae9fc032010-08-18 15:51:08 -0700463
464 uint32_t countSamples() const;
Jeff Brown4e91a182011-04-07 11:38:09 -0700465
466 // Checks whether we can append samples, assuming the device id and source are the same.
467 bool canAppendSamples(int32_t action, uint32_t pointerCount,
468 const int32_t* pointerIds) const;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700469 };
470
Jeff Brown9c3cda02010-06-15 01:31:58 -0700471 // Tracks the progress of dispatching a particular event to a particular connection.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700472 struct DispatchEntry : Link<DispatchEntry> {
473 EventEntry* eventEntry; // the event to dispatch
474 int32_t targetFlags;
475 float xOffset;
476 float yOffset;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700477
478 // True if dispatch has started.
479 bool inProgress;
480
481 // For motion events:
482 // Pointer to the first motion sample to dispatch in this cycle.
483 // Usually NULL to indicate that the list of motion samples begins at
484 // MotionEntry::firstSample. Otherwise, some samples were dispatched in a previous
485 // cycle and this pointer indicates the location of the first remainining sample
486 // to dispatch during the current cycle.
487 MotionSample* headMotionSample;
488 // Pointer to a motion sample to dispatch in the next cycle if the dispatcher was
489 // unable to send all motion samples during this cycle. On the next cycle,
490 // headMotionSample will be initialized to tailMotionSample and tailMotionSample
491 // will be set to NULL.
492 MotionSample* tailMotionSample;
Jeff Brown6ec402b2010-07-28 15:48:59 -0700493
Jeff Brown519e0242010-09-15 15:18:56 -0700494 inline bool hasForegroundTarget() const {
495 return targetFlags & InputTarget::FLAG_FOREGROUND;
Jeff Brownb88102f2010-09-08 11:49:43 -0700496 }
Jeff Brown01ce2e92010-09-26 22:20:12 -0700497
498 inline bool isSplit() const {
499 return targetFlags & InputTarget::FLAG_SPLIT;
500 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700501 };
502
Jeff Brown9c3cda02010-06-15 01:31:58 -0700503 // A command entry captures state and behavior for an action to be performed in the
504 // dispatch loop after the initial processing has taken place. It is essentially
505 // a kind of continuation used to postpone sensitive policy interactions to a point
506 // in the dispatch loop where it is safe to release the lock (generally after finishing
507 // the critical parts of the dispatch cycle).
508 //
509 // The special thing about commands is that they can voluntarily release and reacquire
510 // the dispatcher lock at will. Initially when the command starts running, the
511 // dispatcher lock is held. However, if the command needs to call into the policy to
512 // do some work, it can release the lock, do the work, then reacquire the lock again
513 // before returning.
514 //
515 // This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch
516 // never calls into the policy while holding its lock.
517 //
518 // Commands are implicitly 'LockedInterruptible'.
519 struct CommandEntry;
520 typedef void (InputDispatcher::*Command)(CommandEntry* commandEntry);
521
Jeff Brown7fbdc842010-06-17 20:52:56 -0700522 class Connection;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700523 struct CommandEntry : Link<CommandEntry> {
524 CommandEntry();
525 ~CommandEntry();
526
527 Command command;
528
529 // parameters for the command (usage varies by command)
Jeff Brown7fbdc842010-06-17 20:52:56 -0700530 sp<Connection> connection;
Jeff Brownb88102f2010-09-08 11:49:43 -0700531 nsecs_t eventTime;
532 KeyEntry* keyEntry;
533 sp<InputChannel> inputChannel;
534 sp<InputApplicationHandle> inputApplicationHandle;
Jeff Brown928e0542011-01-10 11:17:36 -0800535 sp<InputWindowHandle> inputWindowHandle;
Jeff Brownb88102f2010-09-08 11:49:43 -0700536 int32_t userActivityEventType;
Jeff Brown3915bb82010-11-05 15:02:16 -0700537 bool handled;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700538 };
539
540 // Generic queue implementation.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700541 template <typename T>
542 struct Queue {
Jeff Brownb88102f2010-09-08 11:49:43 -0700543 T headSentinel;
544 T tailSentinel;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700545
546 inline Queue() {
Jeff Brownb88102f2010-09-08 11:49:43 -0700547 headSentinel.prev = NULL;
548 headSentinel.next = & tailSentinel;
549 tailSentinel.prev = & headSentinel;
550 tailSentinel.next = NULL;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700551 }
552
Jeff Brownb88102f2010-09-08 11:49:43 -0700553 inline bool isEmpty() const {
554 return headSentinel.next == & tailSentinel;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700555 }
556
557 inline void enqueueAtTail(T* entry) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700558 T* last = tailSentinel.prev;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700559 last->next = entry;
560 entry->prev = last;
Jeff Brownb88102f2010-09-08 11:49:43 -0700561 entry->next = & tailSentinel;
562 tailSentinel.prev = entry;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700563 }
564
565 inline void enqueueAtHead(T* entry) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700566 T* first = headSentinel.next;
567 headSentinel.next = entry;
568 entry->prev = & headSentinel;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700569 entry->next = first;
570 first->prev = entry;
571 }
572
573 inline void dequeue(T* entry) {
574 entry->prev->next = entry->next;
575 entry->next->prev = entry->prev;
576 }
577
578 inline T* dequeueAtHead() {
Jeff Brownb88102f2010-09-08 11:49:43 -0700579 T* first = headSentinel.next;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700580 dequeue(first);
581 return first;
582 }
Jeff Brown519e0242010-09-15 15:18:56 -0700583
584 uint32_t count() const;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700585 };
586
587 /* Allocates queue entries and performs reference counting as needed. */
588 class Allocator {
589 public:
590 Allocator();
591
Jeff Brown01ce2e92010-09-26 22:20:12 -0700592 InjectionState* obtainInjectionState(int32_t injectorPid, int32_t injectorUid);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700593 ConfigurationChangedEntry* obtainConfigurationChangedEntry(nsecs_t eventTime);
594 KeyEntry* obtainKeyEntry(nsecs_t eventTime,
Jeff Brown58a2da82011-01-25 16:02:22 -0800595 int32_t deviceId, uint32_t source, uint32_t policyFlags, int32_t action,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700596 int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState,
597 int32_t repeatCount, nsecs_t downTime);
598 MotionEntry* obtainMotionEntry(nsecs_t eventTime,
Jeff Brown58a2da82011-01-25 16:02:22 -0800599 int32_t deviceId, uint32_t source, uint32_t policyFlags, int32_t action,
Jeff Brown85a31762010-09-01 17:01:00 -0700600 int32_t flags, int32_t metaState, int32_t edgeFlags,
601 float xPrecision, float yPrecision,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700602 nsecs_t downTime, uint32_t pointerCount,
603 const int32_t* pointerIds, const PointerCoords* pointerCoords);
Jeff Brownb88102f2010-09-08 11:49:43 -0700604 DispatchEntry* obtainDispatchEntry(EventEntry* eventEntry,
Jeff Brown519e0242010-09-15 15:18:56 -0700605 int32_t targetFlags, float xOffset, float yOffset);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700606 CommandEntry* obtainCommandEntry(Command command);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700607
Jeff Brown01ce2e92010-09-26 22:20:12 -0700608 void releaseInjectionState(InjectionState* injectionState);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700609 void releaseEventEntry(EventEntry* entry);
610 void releaseConfigurationChangedEntry(ConfigurationChangedEntry* entry);
611 void releaseKeyEntry(KeyEntry* entry);
612 void releaseMotionEntry(MotionEntry* entry);
Jeff Browna032cc02011-03-07 16:56:21 -0800613 void freeMotionSample(MotionSample* sample);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700614 void releaseDispatchEntry(DispatchEntry* entry);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700615 void releaseCommandEntry(CommandEntry* entry);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700616
Jeff Brown01ce2e92010-09-26 22:20:12 -0700617 void recycleKeyEntry(KeyEntry* entry);
618
Jeff Brown46b9ac02010-04-22 18:58:52 -0700619 void appendMotionSample(MotionEntry* motionEntry,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700620 nsecs_t eventTime, const PointerCoords* pointerCoords);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700621
622 private:
Jeff Brown01ce2e92010-09-26 22:20:12 -0700623 Pool<InjectionState> mInjectionStatePool;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700624 Pool<ConfigurationChangedEntry> mConfigurationChangeEntryPool;
625 Pool<KeyEntry> mKeyEntryPool;
626 Pool<MotionEntry> mMotionEntryPool;
627 Pool<MotionSample> mMotionSamplePool;
628 Pool<DispatchEntry> mDispatchEntryPool;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700629 Pool<CommandEntry> mCommandEntryPool;
Jeff Brown7fbdc842010-06-17 20:52:56 -0700630
Jeff Brownb6997262010-10-08 22:31:17 -0700631 void initializeEventEntry(EventEntry* entry, int32_t type, nsecs_t eventTime,
632 uint32_t policyFlags);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700633 void releaseEventEntryInjectionState(EventEntry* entry);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700634 };
635
Jeff Brownda3d5a92011-03-29 15:11:34 -0700636 /* Specifies which events are to be canceled and why. */
637 struct CancelationOptions {
638 enum Mode {
Jeff Brownb6997262010-10-08 22:31:17 -0700639 CANCEL_ALL_EVENTS = 0,
640 CANCEL_POINTER_EVENTS = 1,
641 CANCEL_NON_POINTER_EVENTS = 2,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800642 CANCEL_FALLBACK_EVENTS = 3,
Jeff Brownb6997262010-10-08 22:31:17 -0700643 };
644
Jeff Brownda3d5a92011-03-29 15:11:34 -0700645 // The criterion to use to determine which events should be canceled.
646 Mode mode;
647
648 // Descriptive reason for the cancelation.
649 const char* reason;
650
651 // The specific keycode of the key event to cancel, or -1 to cancel any key event.
652 int32_t keyCode;
653
654 CancelationOptions(Mode mode, const char* reason) :
655 mode(mode), reason(reason), keyCode(-1) { }
656 };
657
658 /* Tracks dispatched key and motion event state so that cancelation events can be
659 * synthesized when events are dropped. */
660 class InputState {
661 public:
Jeff Brownb88102f2010-09-08 11:49:43 -0700662 InputState();
663 ~InputState();
664
665 // Returns true if there is no state to be canceled.
666 bool isNeutral() const;
667
Jeff Brownb88102f2010-09-08 11:49:43 -0700668 // Records tracking information for an event that has just been published.
Jeff Browna032cc02011-03-07 16:56:21 -0800669 void trackEvent(const EventEntry* entry, int32_t action);
Jeff Brownb88102f2010-09-08 11:49:43 -0700670
671 // Records tracking information for a key event that has just been published.
Jeff Browna032cc02011-03-07 16:56:21 -0800672 void trackKey(const KeyEntry* entry, int32_t action);
Jeff Brownb88102f2010-09-08 11:49:43 -0700673
674 // Records tracking information for a motion event that has just been published.
Jeff Browna032cc02011-03-07 16:56:21 -0800675 void trackMotion(const MotionEntry* entry, int32_t action);
Jeff Brownb88102f2010-09-08 11:49:43 -0700676
Jeff Brownb6997262010-10-08 22:31:17 -0700677 // Synthesizes cancelation events for the current state and resets the tracked state.
678 void synthesizeCancelationEvents(nsecs_t currentTime, Allocator* allocator,
Jeff Brownda3d5a92011-03-29 15:11:34 -0700679 Vector<EventEntry*>& outEvents, const CancelationOptions& options);
Jeff Brownb88102f2010-09-08 11:49:43 -0700680
681 // Clears the current state.
682 void clear();
683
Jeff Brown9c9f1a32010-10-11 18:32:20 -0700684 // Copies pointer-related parts of the input state to another instance.
685 void copyPointerStateTo(InputState& other) const;
686
Jeff Brownda3d5a92011-03-29 15:11:34 -0700687 // Gets the fallback key associated with a keycode.
688 // Returns -1 if none.
689 // Returns AKEYCODE_UNKNOWN if we are only dispatching the unhandled key to the policy.
690 int32_t getFallbackKey(int32_t originalKeyCode);
691
692 // Sets the fallback key for a particular keycode.
693 void setFallbackKey(int32_t originalKeyCode, int32_t fallbackKeyCode);
694
695 // Removes the fallback key for a particular keycode.
696 void removeFallbackKey(int32_t originalKeyCode);
697
698 inline const KeyedVector<int32_t, int32_t>& getFallbackKeys() const {
699 return mFallbackKeys;
700 }
701
Jeff Brownb88102f2010-09-08 11:49:43 -0700702 private:
Jeff Brownb88102f2010-09-08 11:49:43 -0700703 struct KeyMemento {
704 int32_t deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -0800705 uint32_t source;
Jeff Brownb88102f2010-09-08 11:49:43 -0700706 int32_t keyCode;
707 int32_t scanCode;
Jeff Brown49ed71d2010-12-06 17:13:33 -0800708 int32_t flags;
Jeff Brownb88102f2010-09-08 11:49:43 -0700709 nsecs_t downTime;
710 };
711
712 struct MotionMemento {
713 int32_t deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -0800714 uint32_t source;
Jeff Brownb88102f2010-09-08 11:49:43 -0700715 float xPrecision;
716 float yPrecision;
717 nsecs_t downTime;
718 uint32_t pointerCount;
719 int32_t pointerIds[MAX_POINTERS];
720 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Browna032cc02011-03-07 16:56:21 -0800721 bool hovering;
Jeff Brownb88102f2010-09-08 11:49:43 -0700722
723 void setPointers(const MotionEntry* entry);
724 };
725
726 Vector<KeyMemento> mKeyMementos;
727 Vector<MotionMemento> mMotionMementos;
Jeff Brownda3d5a92011-03-29 15:11:34 -0700728 KeyedVector<int32_t, int32_t> mFallbackKeys;
Jeff Brownb6997262010-10-08 22:31:17 -0700729
Jeff Brown49ed71d2010-12-06 17:13:33 -0800730 static bool shouldCancelKey(const KeyMemento& memento,
Jeff Brownda3d5a92011-03-29 15:11:34 -0700731 const CancelationOptions& options);
Jeff Brown49ed71d2010-12-06 17:13:33 -0800732 static bool shouldCancelMotion(const MotionMemento& memento,
Jeff Brownda3d5a92011-03-29 15:11:34 -0700733 const CancelationOptions& options);
Jeff Brownb88102f2010-09-08 11:49:43 -0700734 };
735
Jeff Brown46b9ac02010-04-22 18:58:52 -0700736 /* Manages the dispatch state associated with a single input channel. */
737 class Connection : public RefBase {
738 protected:
739 virtual ~Connection();
740
741 public:
742 enum Status {
743 // Everything is peachy.
744 STATUS_NORMAL,
745 // An unrecoverable communication error has occurred.
746 STATUS_BROKEN,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700747 // The input channel has been unregistered.
748 STATUS_ZOMBIE
749 };
750
751 Status status;
Jeff Brown928e0542011-01-10 11:17:36 -0800752 sp<InputChannel> inputChannel; // never null
753 sp<InputWindowHandle> inputWindowHandle; // may be null
Jeff Brown46b9ac02010-04-22 18:58:52 -0700754 InputPublisher inputPublisher;
Jeff Brownb88102f2010-09-08 11:49:43 -0700755 InputState inputState;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700756 Queue<DispatchEntry> outboundQueue;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700757
758 nsecs_t lastEventTime; // the time when the event was originally captured
759 nsecs_t lastDispatchTime; // the time when the last event was dispatched
Jeff Brown46b9ac02010-04-22 18:58:52 -0700760
Jeff Brown928e0542011-01-10 11:17:36 -0800761 explicit Connection(const sp<InputChannel>& inputChannel,
762 const sp<InputWindowHandle>& inputWindowHandle);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700763
Jeff Brown9c3cda02010-06-15 01:31:58 -0700764 inline const char* getInputChannelName() const { return inputChannel->getName().string(); }
765
766 const char* getStatusLabel() const;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700767
768 // Finds a DispatchEntry in the outbound queue associated with the specified event.
769 // Returns NULL if not found.
770 DispatchEntry* findQueuedDispatchEntryForEvent(const EventEntry* eventEntry) const;
771
Jeff Brown46b9ac02010-04-22 18:58:52 -0700772 // Gets the time since the current event was originally obtained from the input driver.
Jeff Brownb88102f2010-09-08 11:49:43 -0700773 inline double getEventLatencyMillis(nsecs_t currentTime) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700774 return (currentTime - lastEventTime) / 1000000.0;
775 }
776
777 // Gets the time since the current event entered the outbound dispatch queue.
Jeff Brownb88102f2010-09-08 11:49:43 -0700778 inline double getDispatchLatencyMillis(nsecs_t currentTime) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700779 return (currentTime - lastDispatchTime) / 1000000.0;
780 }
781
Jeff Brown46b9ac02010-04-22 18:58:52 -0700782 status_t initialize();
783 };
784
Jeff Brownb6997262010-10-08 22:31:17 -0700785 enum DropReason {
786 DROP_REASON_NOT_DROPPED = 0,
787 DROP_REASON_POLICY = 1,
788 DROP_REASON_APP_SWITCH = 2,
789 DROP_REASON_DISABLED = 3,
Jeff Brown928e0542011-01-10 11:17:36 -0800790 DROP_REASON_BLOCKED = 4,
791 DROP_REASON_STALE = 5,
Jeff Brownb6997262010-10-08 22:31:17 -0700792 };
793
Jeff Brown9c3cda02010-06-15 01:31:58 -0700794 sp<InputDispatcherPolicyInterface> mPolicy;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700795
796 Mutex mLock;
797
Jeff Brown46b9ac02010-04-22 18:58:52 -0700798 Allocator mAllocator;
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700799 sp<Looper> mLooper;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700800
Jeff Brownb88102f2010-09-08 11:49:43 -0700801 EventEntry* mPendingEvent;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700802 Queue<EventEntry> mInboundQueue;
803 Queue<CommandEntry> mCommandQueue;
804
Jeff Brownb88102f2010-09-08 11:49:43 -0700805 Vector<EventEntry*> mTempCancelationEvents;
806
807 void dispatchOnceInnerLocked(nsecs_t keyRepeatTimeout, nsecs_t keyRepeatDelay,
808 nsecs_t* nextWakeupTime);
809
Jeff Brown4e91a182011-04-07 11:38:09 -0700810 // Batches a new sample onto a motion entry.
811 // Assumes that the we have already checked that we can append samples.
812 void batchMotionLocked(MotionEntry* entry, nsecs_t eventTime, int32_t metaState,
813 const PointerCoords* pointerCoords, const char* eventDescription);
814
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700815 // Enqueues an inbound event. Returns true if mLooper->wake() should be called.
Jeff Brownb88102f2010-09-08 11:49:43 -0700816 bool enqueueInboundEventLocked(EventEntry* entry);
817
Jeff Brownb6997262010-10-08 22:31:17 -0700818 // Cleans up input state when dropping an inbound event.
819 void dropInboundEventLocked(EventEntry* entry, DropReason dropReason);
820
Jeff Brownb88102f2010-09-08 11:49:43 -0700821 // App switch latency optimization.
Jeff Brownb6997262010-10-08 22:31:17 -0700822 bool mAppSwitchSawKeyDown;
Jeff Brownb88102f2010-09-08 11:49:43 -0700823 nsecs_t mAppSwitchDueTime;
824
Jeff Brownb6997262010-10-08 22:31:17 -0700825 static bool isAppSwitchKeyCode(int32_t keyCode);
826 bool isAppSwitchKeyEventLocked(KeyEntry* keyEntry);
Jeff Brownb88102f2010-09-08 11:49:43 -0700827 bool isAppSwitchPendingLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700828 void resetPendingAppSwitchLocked(bool handled);
829
Jeff Brown928e0542011-01-10 11:17:36 -0800830 // Stale event latency optimization.
831 static bool isStaleEventLocked(nsecs_t currentTime, EventEntry* entry);
832
833 // Blocked event latency optimization. Drops old events when the user intends
834 // to transfer focus to a new application.
835 EventEntry* mNextUnblockedEvent;
836
837 const InputWindow* findTouchedWindowAtLocked(int32_t x, int32_t y);
838
Jeff Brown46b9ac02010-04-22 18:58:52 -0700839 // All registered connections mapped by receive pipe file descriptor.
840 KeyedVector<int, sp<Connection> > mConnectionsByReceiveFd;
841
Jeff Brown519e0242010-09-15 15:18:56 -0700842 ssize_t getConnectionIndexLocked(const sp<InputChannel>& inputChannel);
Jeff Brown2cbecea2010-08-17 15:59:26 -0700843
Jeff Brown46b9ac02010-04-22 18:58:52 -0700844 // Active connections are connections that have a non-empty outbound queue.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700845 // We don't use a ref-counted pointer here because we explicitly abort connections
846 // during unregistration which causes the connection's outbound queue to be cleared
847 // and the connection itself to be deactivated.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700848 Vector<Connection*> mActiveConnections;
849
Jeff Brownb88102f2010-09-08 11:49:43 -0700850 // Input channels that will receive a copy of all input events.
851 Vector<sp<InputChannel> > mMonitoringChannels;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700852
Jeff Brown7fbdc842010-06-17 20:52:56 -0700853 // Event injection and synchronization.
854 Condition mInjectionResultAvailableCondition;
Jeff Brownb6997262010-10-08 22:31:17 -0700855 bool hasInjectionPermission(int32_t injectorPid, int32_t injectorUid);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700856 void setInjectionResultLocked(EventEntry* entry, int32_t injectionResult);
857
Jeff Brown6ec402b2010-07-28 15:48:59 -0700858 Condition mInjectionSyncFinishedCondition;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700859 void incrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brown519e0242010-09-15 15:18:56 -0700860 void decrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brown6ec402b2010-07-28 15:48:59 -0700861
Jeff Brownae9fc032010-08-18 15:51:08 -0700862 // Throttling state.
863 struct ThrottleState {
864 nsecs_t minTimeBetweenEvents;
865
866 nsecs_t lastEventTime;
867 int32_t lastDeviceId;
868 uint32_t lastSource;
869
870 uint32_t originalSampleCount; // only collected during debugging
871 } mThrottleState;
872
Jeff Brown46b9ac02010-04-22 18:58:52 -0700873 // Key repeat tracking.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700874 struct KeyRepeatState {
875 KeyEntry* lastKeyEntry; // or null if no repeat
876 nsecs_t nextRepeatTime;
877 } mKeyRepeatState;
878
879 void resetKeyRepeatLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700880 KeyEntry* synthesizeKeyRepeatLocked(nsecs_t currentTime, nsecs_t keyRepeatTimeout);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700881
Jeff Brown9c3cda02010-06-15 01:31:58 -0700882 // Deferred command processing.
883 bool runCommandsLockedInterruptible();
884 CommandEntry* postCommandLocked(Command command);
885
Jeff Brownb88102f2010-09-08 11:49:43 -0700886 // Inbound event processing.
887 void drainInboundQueueLocked();
Jeff Brown54a18252010-09-16 14:07:33 -0700888 void releasePendingEventLocked();
889 void releaseInboundEventLocked(EventEntry* entry);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700890
Jeff Brownb88102f2010-09-08 11:49:43 -0700891 // Dispatch state.
892 bool mDispatchEnabled;
893 bool mDispatchFrozen;
Jeff Brown0029c662011-03-30 02:25:18 -0700894 bool mInputFilterEnabled;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700895
Jeff Brownb88102f2010-09-08 11:49:43 -0700896 Vector<InputWindow> mWindows;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700897
898 const InputWindow* getWindowLocked(const sp<InputChannel>& inputChannel);
Jeff Brownb88102f2010-09-08 11:49:43 -0700899
900 // Focus tracking for keys, trackball, etc.
Jeff Brown01ce2e92010-09-26 22:20:12 -0700901 const InputWindow* mFocusedWindow;
Jeff Brownb88102f2010-09-08 11:49:43 -0700902
903 // Focus tracking for touch.
Jeff Brown01ce2e92010-09-26 22:20:12 -0700904 struct TouchedWindow {
905 const InputWindow* window;
906 int32_t targetFlags;
Jeff Brown46e75292010-11-10 16:53:45 -0800907 BitSet32 pointerIds; // zero unless target flag FLAG_SPLIT is set
Jeff Brown01ce2e92010-09-26 22:20:12 -0700908 sp<InputChannel> channel;
Jeff Brownb88102f2010-09-08 11:49:43 -0700909 };
Jeff Brown01ce2e92010-09-26 22:20:12 -0700910 struct TouchState {
911 bool down;
912 bool split;
Jeff Brown95712852011-01-04 19:41:59 -0800913 int32_t deviceId; // id of the device that is currently down, others are rejected
Jeff Brown58a2da82011-01-25 16:02:22 -0800914 uint32_t source; // source of the device that is current down, others are rejected
Jeff Brown01ce2e92010-09-26 22:20:12 -0700915 Vector<TouchedWindow> windows;
916
917 TouchState();
918 ~TouchState();
919 void reset();
920 void copyFrom(const TouchState& other);
921 void addOrUpdateWindow(const InputWindow* window, int32_t targetFlags, BitSet32 pointerIds);
Jeff Browna032cc02011-03-07 16:56:21 -0800922 void filterNonAsIsTouchWindows();
Jeff Brown01ce2e92010-09-26 22:20:12 -0700923 const InputWindow* getFirstForegroundWindow();
924 };
925
926 TouchState mTouchState;
927 TouchState mTempTouchState;
Jeff Brownb88102f2010-09-08 11:49:43 -0700928
929 // Focused application.
930 InputApplication* mFocusedApplication;
931 InputApplication mFocusedApplicationStorage; // preallocated storage for mFocusedApplication
932 void releaseFocusedApplicationLocked();
933
934 // Dispatch inbound events.
935 bool dispatchConfigurationChangedLocked(
936 nsecs_t currentTime, ConfigurationChangedEntry* entry);
937 bool dispatchKeyLocked(
938 nsecs_t currentTime, KeyEntry* entry, nsecs_t keyRepeatTimeout,
Jeff Browne20c9e02010-10-11 14:20:19 -0700939 DropReason* dropReason, nsecs_t* nextWakeupTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700940 bool dispatchMotionLocked(
941 nsecs_t currentTime, MotionEntry* entry,
Jeff Browne20c9e02010-10-11 14:20:19 -0700942 DropReason* dropReason, nsecs_t* nextWakeupTime);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700943 void dispatchEventToCurrentInputTargetsLocked(
944 nsecs_t currentTime, EventEntry* entry, bool resumeWithAppendedMotionSample);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700945
Jeff Brownb88102f2010-09-08 11:49:43 -0700946 void logOutboundKeyDetailsLocked(const char* prefix, const KeyEntry* entry);
947 void logOutboundMotionDetailsLocked(const char* prefix, const MotionEntry* entry);
948
949 // The input targets that were most recently identified for dispatch.
Jeff Brownb88102f2010-09-08 11:49:43 -0700950 bool mCurrentInputTargetsValid; // false while targets are being recomputed
951 Vector<InputTarget> mCurrentInputTargets;
Jeff Brownb88102f2010-09-08 11:49:43 -0700952
953 enum InputTargetWaitCause {
954 INPUT_TARGET_WAIT_CAUSE_NONE,
955 INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY,
956 INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY,
957 };
958
959 InputTargetWaitCause mInputTargetWaitCause;
960 nsecs_t mInputTargetWaitStartTime;
961 nsecs_t mInputTargetWaitTimeoutTime;
962 bool mInputTargetWaitTimeoutExpired;
Jeff Brown928e0542011-01-10 11:17:36 -0800963 sp<InputApplicationHandle> mInputTargetWaitApplication;
Jeff Brownb88102f2010-09-08 11:49:43 -0700964
Jeff Browna032cc02011-03-07 16:56:21 -0800965 // Contains the last window which received a hover event.
966 const InputWindow* mLastHoverWindow;
967
Jeff Brownb88102f2010-09-08 11:49:43 -0700968 // Finding targets for input events.
Jeff Brown54a18252010-09-16 14:07:33 -0700969 void resetTargetsLocked();
Jeff Brown01ce2e92010-09-26 22:20:12 -0700970 void commitTargetsLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700971 int32_t handleTargetsNotReadyLocked(nsecs_t currentTime, const EventEntry* entry,
972 const InputApplication* application, const InputWindow* window,
973 nsecs_t* nextWakeupTime);
Jeff Brown519e0242010-09-15 15:18:56 -0700974 void resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout,
975 const sp<InputChannel>& inputChannel);
976 nsecs_t getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700977 void resetANRTimeoutsLocked();
978
Jeff Brown01ce2e92010-09-26 22:20:12 -0700979 int32_t findFocusedWindowTargetsLocked(nsecs_t currentTime, const EventEntry* entry,
980 nsecs_t* nextWakeupTime);
981 int32_t findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry* entry,
Jeff Browna032cc02011-03-07 16:56:21 -0800982 nsecs_t* nextWakeupTime, bool* outConflictingPointerActions,
983 const MotionSample** outSplitBatchAfterSample);
Jeff Brownb88102f2010-09-08 11:49:43 -0700984
Jeff Brown01ce2e92010-09-26 22:20:12 -0700985 void addWindowTargetLocked(const InputWindow* window, int32_t targetFlags,
986 BitSet32 pointerIds);
Jeff Brownb88102f2010-09-08 11:49:43 -0700987 void addMonitoringTargetsLocked();
Jeff Browne2fe69e2010-10-18 13:21:23 -0700988 void pokeUserActivityLocked(const EventEntry* eventEntry);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700989 bool checkInjectionPermission(const InputWindow* window, const InjectionState* injectionState);
Jeff Brown19dfc832010-10-05 12:26:23 -0700990 bool isWindowObscuredAtPointLocked(const InputWindow* window, int32_t x, int32_t y) const;
Jeff Brown519e0242010-09-15 15:18:56 -0700991 bool isWindowFinishedWithPreviousInputLocked(const InputWindow* window);
Jeff Brown519e0242010-09-15 15:18:56 -0700992 String8 getApplicationWindowLabelLocked(const InputApplication* application,
993 const InputWindow* window);
Jeff Brownb88102f2010-09-08 11:49:43 -0700994
Jeff Brown46b9ac02010-04-22 18:58:52 -0700995 // Manage the dispatch cycle for a single connection.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700996 // These methods are deliberately not Interruptible because doing all of the work
997 // with the mutex held makes it easier to ensure that connection invariants are maintained.
998 // If needed, the methods post commands to run later once the critical bits are done.
999 void prepareDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001000 EventEntry* eventEntry, const InputTarget* inputTarget,
1001 bool resumeWithAppendedMotionSample);
Jeff Browna032cc02011-03-07 16:56:21 -08001002 void enqueueDispatchEntryLocked(const sp<Connection>& connection,
1003 EventEntry* eventEntry, const InputTarget* inputTarget,
1004 bool resumeWithAppendedMotionSample, int32_t dispatchMode);
Jeff Brown519e0242010-09-15 15:18:56 -07001005 void startDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown3915bb82010-11-05 15:02:16 -07001006 void finishDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
1007 bool handled);
Jeff Brownb88102f2010-09-08 11:49:43 -07001008 void startNextDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brownb6997262010-10-08 22:31:17 -07001009 void abortBrokenDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown519e0242010-09-15 15:18:56 -07001010 void drainOutboundQueueLocked(Connection* connection);
Jeff Brown4fe6c3e2010-09-13 23:17:30 -07001011 static int handleReceiveCallback(int receiveFd, int events, void* data);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001012
Jeff Brownb6997262010-10-08 22:31:17 -07001013 void synthesizeCancelationEventsForAllConnectionsLocked(
Jeff Brownda3d5a92011-03-29 15:11:34 -07001014 const CancelationOptions& options);
Jeff Brownb6997262010-10-08 22:31:17 -07001015 void synthesizeCancelationEventsForInputChannelLocked(const sp<InputChannel>& channel,
Jeff Brownda3d5a92011-03-29 15:11:34 -07001016 const CancelationOptions& options);
Jeff Brownb6997262010-10-08 22:31:17 -07001017 void synthesizeCancelationEventsForConnectionLocked(const sp<Connection>& connection,
Jeff Brownda3d5a92011-03-29 15:11:34 -07001018 const CancelationOptions& options);
Jeff Brownb6997262010-10-08 22:31:17 -07001019
Jeff Brown01ce2e92010-09-26 22:20:12 -07001020 // Splitting motion events across windows.
1021 MotionEntry* splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds);
1022
Jeff Brown120a4592010-10-27 18:43:51 -07001023 // Reset and drop everything the dispatcher is doing.
1024 void resetAndDropEverythingLocked(const char* reason);
1025
Jeff Brownb88102f2010-09-08 11:49:43 -07001026 // Dump state.
1027 void dumpDispatchStateLocked(String8& dump);
1028 void logDispatchStateLocked();
1029
Jeff Brown46b9ac02010-04-22 18:58:52 -07001030 // Add or remove a connection to the mActiveConnections vector.
1031 void activateConnectionLocked(Connection* connection);
1032 void deactivateConnectionLocked(Connection* connection);
1033
1034 // Interesting events that we might like to log or tell the framework about.
Jeff Brown9c3cda02010-06-15 01:31:58 -07001035 void onDispatchCycleStartedLocked(
Jeff Brown7fbdc842010-06-17 20:52:56 -07001036 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001037 void onDispatchCycleFinishedLocked(
Jeff Brown3915bb82010-11-05 15:02:16 -07001038 nsecs_t currentTime, const sp<Connection>& connection, bool handled);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001039 void onDispatchCycleBrokenLocked(
Jeff Brown7fbdc842010-06-17 20:52:56 -07001040 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown519e0242010-09-15 15:18:56 -07001041 void onANRLocked(
1042 nsecs_t currentTime, const InputApplication* application, const InputWindow* window,
1043 nsecs_t eventTime, nsecs_t waitStartTime);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001044
Jeff Brown7fbdc842010-06-17 20:52:56 -07001045 // Outbound policy interactions.
Jeff Brownb88102f2010-09-08 11:49:43 -07001046 void doNotifyConfigurationChangedInterruptible(CommandEntry* commandEntry);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001047 void doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown519e0242010-09-15 15:18:56 -07001048 void doNotifyANRLockedInterruptible(CommandEntry* commandEntry);
Jeff Brownb88102f2010-09-08 11:49:43 -07001049 void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown3915bb82010-11-05 15:02:16 -07001050 void doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry);
Jeff Brownb88102f2010-09-08 11:49:43 -07001051 void doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown3915bb82010-11-05 15:02:16 -07001052 void initializeKeyEvent(KeyEvent* event, const KeyEntry* entry);
Jeff Brown519e0242010-09-15 15:18:56 -07001053
1054 // Statistics gathering.
1055 void updateDispatchStatisticsLocked(nsecs_t currentTime, const EventEntry* entry,
1056 int32_t injectionResult, nsecs_t timeSpentWaitingForApplication);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001057};
1058
1059/* Enqueues and dispatches input events, endlessly. */
1060class InputDispatcherThread : public Thread {
1061public:
1062 explicit InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher);
1063 ~InputDispatcherThread();
1064
1065private:
1066 virtual bool threadLoop();
1067
1068 sp<InputDispatcherInterface> mDispatcher;
1069};
1070
1071} // namespace android
1072
Jeff Brownb88102f2010-09-08 11:49:43 -07001073#endif // _UI_INPUT_DISPATCHER_H