blob: 96ece32f59a181c282a76608c4221182f968b2e8 [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
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400135 // Scaling factor to apply to MotionEvent as it is delivered.
136 // (ignored for KeyEvents)
137 float scaleFactor;
138
Jeff Brown01ce2e92010-09-26 22:20:12 -0700139 // The subset of pointer ids to include in motion events dispatched to this input target
140 // if FLAG_SPLIT is set.
141 BitSet32 pointerIds;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700142};
143
Jeff Brown7fbdc842010-06-17 20:52:56 -0700144
Jeff Brown9c3cda02010-06-15 01:31:58 -0700145/*
146 * Input dispatcher policy interface.
147 *
148 * The input reader policy is used by the input reader to interact with the Window Manager
149 * and other system components.
150 *
151 * The actual implementation is partially supported by callbacks into the DVM
152 * via JNI. This interface is also mocked in the unit tests.
153 */
154class InputDispatcherPolicyInterface : public virtual RefBase {
155protected:
156 InputDispatcherPolicyInterface() { }
157 virtual ~InputDispatcherPolicyInterface() { }
158
159public:
160 /* Notifies the system that a configuration change has occurred. */
161 virtual void notifyConfigurationChanged(nsecs_t when) = 0;
162
Jeff Brownb88102f2010-09-08 11:49:43 -0700163 /* Notifies the system that an application is not responding.
164 * Returns a new timeout to continue waiting, or 0 to abort dispatch. */
Jeff Brown519e0242010-09-15 15:18:56 -0700165 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brown928e0542011-01-10 11:17:36 -0800166 const sp<InputWindowHandle>& inputWindowHandle) = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700167
Jeff Brown9c3cda02010-06-15 01:31:58 -0700168 /* Notifies the system that an input channel is unrecoverably broken. */
Jeff Brown928e0542011-01-10 11:17:36 -0800169 virtual void notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700170
Jeff Brownb21fb102010-09-07 10:44:57 -0700171 /* Gets the key repeat initial timeout or -1 if automatic key repeating is disabled. */
Jeff Brown9c3cda02010-06-15 01:31:58 -0700172 virtual nsecs_t getKeyRepeatTimeout() = 0;
173
Jeff Brownb21fb102010-09-07 10:44:57 -0700174 /* Gets the key repeat inter-key delay. */
175 virtual nsecs_t getKeyRepeatDelay() = 0;
176
Jeff Brownae9fc032010-08-18 15:51:08 -0700177 /* Gets the maximum suggested event delivery rate per second.
178 * This value is used to throttle motion event movement actions on a per-device
179 * basis. It is not intended to be a hard limit.
180 */
181 virtual int32_t getMaxEventsPerSecond() = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700182
Jeff Brown0029c662011-03-30 02:25:18 -0700183 /* Filters an input event.
184 * Return true to dispatch the event unmodified, false to consume the event.
185 * A filter can also transform and inject events later by passing POLICY_FLAG_FILTERED
186 * to injectInputEvent.
187 */
188 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) = 0;
189
Jeff Brownb6997262010-10-08 22:31:17 -0700190 /* Intercepts a key event immediately before queueing it.
191 * The policy can use this method as an opportunity to perform power management functions
192 * and early event preprocessing such as updating policy flags.
193 *
194 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
195 * should be dispatched to applications.
196 */
Jeff Brown1f245102010-11-18 20:53:46 -0800197 virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags) = 0;
Jeff Brownb6997262010-10-08 22:31:17 -0700198
Jeff Brown56194eb2011-03-02 19:23:13 -0800199 /* Intercepts a touch, trackball or other motion event before queueing it.
Jeff Brownb6997262010-10-08 22:31:17 -0700200 * The policy can use this method as an opportunity to perform power management functions
201 * and early event preprocessing such as updating policy flags.
202 *
203 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
204 * should be dispatched to applications.
205 */
Jeff Brown56194eb2011-03-02 19:23:13 -0800206 virtual void interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) = 0;
Jeff Brownb6997262010-10-08 22:31:17 -0700207
Jeff Brownb88102f2010-09-08 11:49:43 -0700208 /* Allows the policy a chance to intercept a key before dispatching. */
Jeff Brown928e0542011-01-10 11:17:36 -0800209 virtual bool interceptKeyBeforeDispatching(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brownb88102f2010-09-08 11:49:43 -0700210 const KeyEvent* keyEvent, uint32_t policyFlags) = 0;
211
Jeff Brown49ed71d2010-12-06 17:13:33 -0800212 /* Allows the policy a chance to perform default processing for an unhandled key.
213 * Returns an alternate keycode to redispatch as a fallback, or 0 to give up. */
Jeff Brown928e0542011-01-10 11:17:36 -0800214 virtual bool dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800215 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) = 0;
Jeff Brown3915bb82010-11-05 15:02:16 -0700216
Jeff Brownb6997262010-10-08 22:31:17 -0700217 /* Notifies the policy about switch events.
218 */
219 virtual void notifySwitch(nsecs_t when,
220 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) = 0;
221
Jeff Brownb88102f2010-09-08 11:49:43 -0700222 /* Poke user activity for an event dispatched to a window. */
Jeff Brown01ce2e92010-09-26 22:20:12 -0700223 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType) = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700224
225 /* Checks whether a given application pid/uid has permission to inject input events
226 * into other applications.
227 *
228 * This method is special in that its implementation promises to be non-reentrant and
229 * is safe to call while holding other locks. (Most other methods make no such guarantees!)
230 */
231 virtual bool checkInjectEventsPermissionNonReentrant(
232 int32_t injectorPid, int32_t injectorUid) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700233};
234
235
Jeff Brown46b9ac02010-04-22 18:58:52 -0700236/* Notifies the system about input events generated by the input reader.
237 * The dispatcher is expected to be mostly asynchronous. */
238class InputDispatcherInterface : public virtual RefBase {
239protected:
240 InputDispatcherInterface() { }
241 virtual ~InputDispatcherInterface() { }
242
243public:
Jeff Brownb88102f2010-09-08 11:49:43 -0700244 /* Dumps the state of the input dispatcher.
245 *
246 * This method may be called on any thread (usually by the input manager). */
247 virtual void dump(String8& dump) = 0;
248
Jeff Brown46b9ac02010-04-22 18:58:52 -0700249 /* Runs a single iteration of the dispatch loop.
250 * Nominally processes one queued event, a timeout, or a response from an input consumer.
251 *
252 * This method should only be called on the input dispatcher thread.
253 */
254 virtual void dispatchOnce() = 0;
255
256 /* Notifies the dispatcher about new events.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700257 *
258 * These methods should only be called on the input reader thread.
259 */
Jeff Brown9c3cda02010-06-15 01:31:58 -0700260 virtual void notifyConfigurationChanged(nsecs_t eventTime) = 0;
Jeff Brown58a2da82011-01-25 16:02:22 -0800261 virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, uint32_t source,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700262 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode,
263 int32_t scanCode, int32_t metaState, nsecs_t downTime) = 0;
Jeff Brown58a2da82011-01-25 16:02:22 -0800264 virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, uint32_t source,
Jeff Brown85a31762010-09-01 17:01:00 -0700265 uint32_t policyFlags, int32_t action, int32_t flags,
266 int32_t metaState, int32_t edgeFlags,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700267 uint32_t pointerCount, const int32_t* pointerIds, const PointerCoords* pointerCoords,
268 float xPrecision, float yPrecision, nsecs_t downTime) = 0;
Jeff Brownb6997262010-10-08 22:31:17 -0700269 virtual void notifySwitch(nsecs_t when,
270 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700271
Jeff Brown7fbdc842010-06-17 20:52:56 -0700272 /* Injects an input event and optionally waits for sync.
Jeff Brown6ec402b2010-07-28 15:48:59 -0700273 * The synchronization mode determines whether the method blocks while waiting for
274 * input injection to proceed.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700275 * Returns one of the INPUT_EVENT_INJECTION_XXX constants.
276 *
277 * This method may be called on any thread (usually by the input manager).
278 */
279 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brown0029c662011-03-30 02:25:18 -0700280 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,
281 uint32_t policyFlags) = 0;
Jeff Brown7fbdc842010-06-17 20:52:56 -0700282
Jeff Brownb88102f2010-09-08 11:49:43 -0700283 /* Sets the list of input windows.
284 *
285 * This method may be called on any thread (usually by the input manager).
286 */
287 virtual void setInputWindows(const Vector<InputWindow>& inputWindows) = 0;
288
289 /* Sets the focused application.
290 *
291 * This method may be called on any thread (usually by the input manager).
292 */
293 virtual void setFocusedApplication(const InputApplication* inputApplication) = 0;
294
295 /* Sets the input dispatching mode.
296 *
297 * This method may be called on any thread (usually by the input manager).
298 */
299 virtual void setInputDispatchMode(bool enabled, bool frozen) = 0;
300
Jeff Brown0029c662011-03-30 02:25:18 -0700301 /* Sets whether input event filtering is enabled.
302 * When enabled, incoming input events are sent to the policy's filterInputEvent
303 * method instead of being dispatched. The filter is expected to use
304 * injectInputEvent to inject the events it would like to have dispatched.
305 * It should include POLICY_FLAG_FILTERED in the policy flags during injection.
306 */
307 virtual void setInputFilterEnabled(bool enabled) = 0;
308
Jeff Browne6504122010-09-27 14:52:15 -0700309 /* Transfers touch focus from the window associated with one channel to the
310 * window associated with the other channel.
311 *
312 * Returns true on success. False if the window did not actually have touch focus.
313 */
314 virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel,
315 const sp<InputChannel>& toChannel) = 0;
316
Jeff Brown46b9ac02010-04-22 18:58:52 -0700317 /* Registers or unregister input channels that may be used as targets for input events.
Jeff Brownb88102f2010-09-08 11:49:43 -0700318 * If monitor is true, the channel will receive a copy of all input events.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700319 *
320 * These methods may be called on any thread (usually by the input manager).
321 */
Jeff Brown928e0542011-01-10 11:17:36 -0800322 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel,
323 const sp<InputWindowHandle>& inputWindowHandle, bool monitor) = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700324 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) = 0;
325};
326
Jeff Brown9c3cda02010-06-15 01:31:58 -0700327/* Dispatches events to input targets. Some functions of the input dispatcher, such as
328 * identifying input targets, are controlled by a separate policy object.
329 *
330 * IMPORTANT INVARIANT:
331 * Because the policy can potentially block or cause re-entrance into the input dispatcher,
332 * the input dispatcher never calls into the policy while holding its internal locks.
333 * The implementation is also carefully designed to recover from scenarios such as an
334 * input channel becoming unregistered while identifying input targets or processing timeouts.
335 *
336 * Methods marked 'Locked' must be called with the lock acquired.
337 *
338 * Methods marked 'LockedInterruptible' must be called with the lock acquired but
339 * may during the course of their execution release the lock, call into the policy, and
340 * then reacquire the lock. The caller is responsible for recovering gracefully.
341 *
342 * A 'LockedInterruptible' method may called a 'Locked' method, but NOT vice-versa.
343 */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700344class InputDispatcher : public InputDispatcherInterface {
345protected:
346 virtual ~InputDispatcher();
347
348public:
Jeff Brown9c3cda02010-06-15 01:31:58 -0700349 explicit InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700350
Jeff Brownb88102f2010-09-08 11:49:43 -0700351 virtual void dump(String8& dump);
352
Jeff Brown46b9ac02010-04-22 18:58:52 -0700353 virtual void dispatchOnce();
354
Jeff Brown9c3cda02010-06-15 01:31:58 -0700355 virtual void notifyConfigurationChanged(nsecs_t eventTime);
Jeff Brown58a2da82011-01-25 16:02:22 -0800356 virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, uint32_t source,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700357 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode,
358 int32_t scanCode, int32_t metaState, nsecs_t downTime);
Jeff Brown58a2da82011-01-25 16:02:22 -0800359 virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, uint32_t source,
Jeff Brown85a31762010-09-01 17:01:00 -0700360 uint32_t policyFlags, int32_t action, int32_t flags,
361 int32_t metaState, int32_t edgeFlags,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700362 uint32_t pointerCount, const int32_t* pointerIds, const PointerCoords* pointerCoords,
363 float xPrecision, float yPrecision, nsecs_t downTime);
Jeff Brownb6997262010-10-08 22:31:17 -0700364 virtual void notifySwitch(nsecs_t when,
365 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) ;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700366
Jeff Brown7fbdc842010-06-17 20:52:56 -0700367 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brown0029c662011-03-30 02:25:18 -0700368 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,
369 uint32_t policyFlags);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700370
Jeff Brownb88102f2010-09-08 11:49:43 -0700371 virtual void setInputWindows(const Vector<InputWindow>& inputWindows);
372 virtual void setFocusedApplication(const InputApplication* inputApplication);
373 virtual void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown0029c662011-03-30 02:25:18 -0700374 virtual void setInputFilterEnabled(bool enabled);
Jeff Brown349703e2010-06-22 01:27:15 -0700375
Jeff Browne6504122010-09-27 14:52:15 -0700376 virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel,
377 const sp<InputChannel>& toChannel);
378
Jeff Brown928e0542011-01-10 11:17:36 -0800379 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel,
380 const sp<InputWindowHandle>& inputWindowHandle, bool monitor);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700381 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel);
382
383private:
384 template <typename T>
385 struct Link {
386 T* next;
387 T* prev;
388 };
389
Jeff Brown01ce2e92010-09-26 22:20:12 -0700390 struct InjectionState {
391 mutable int32_t refCount;
392
393 int32_t injectorPid;
394 int32_t injectorUid;
395 int32_t injectionResult; // initially INPUT_EVENT_INJECTION_PENDING
396 bool injectionIsAsync; // set to true if injection is not waiting for the result
397 int32_t pendingForegroundDispatches; // the number of foreground dispatches in progress
398 };
399
Jeff Brown46b9ac02010-04-22 18:58:52 -0700400 struct EventEntry : Link<EventEntry> {
401 enum {
402 TYPE_SENTINEL,
403 TYPE_CONFIGURATION_CHANGED,
404 TYPE_KEY,
405 TYPE_MOTION
406 };
407
Jeff Brown01ce2e92010-09-26 22:20:12 -0700408 mutable int32_t refCount;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700409 int32_t type;
410 nsecs_t eventTime;
Jeff Brownb6997262010-10-08 22:31:17 -0700411 uint32_t policyFlags;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700412 InjectionState* injectionState;
Jeff Brown7fbdc842010-06-17 20:52:56 -0700413
Jeff Brown9c3cda02010-06-15 01:31:58 -0700414 bool dispatchInProgress; // initially false, set to true while dispatching
Jeff Brown7fbdc842010-06-17 20:52:56 -0700415
Jeff Brown4e91a182011-04-07 11:38:09 -0700416 inline bool isInjected() const { return injectionState != NULL; }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700417 };
418
419 struct ConfigurationChangedEntry : EventEntry {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700420 };
421
422 struct KeyEntry : EventEntry {
423 int32_t deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -0800424 uint32_t source;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700425 int32_t action;
426 int32_t flags;
427 int32_t keyCode;
428 int32_t scanCode;
429 int32_t metaState;
430 int32_t repeatCount;
431 nsecs_t downTime;
Jeff Brownb88102f2010-09-08 11:49:43 -0700432
433 bool syntheticRepeat; // set to true for synthetic key repeats
434
435 enum InterceptKeyResult {
436 INTERCEPT_KEY_RESULT_UNKNOWN,
437 INTERCEPT_KEY_RESULT_SKIP,
438 INTERCEPT_KEY_RESULT_CONTINUE,
439 };
440 InterceptKeyResult interceptKeyResult; // set based on the interception result
Jeff Brown46b9ac02010-04-22 18:58:52 -0700441 };
442
443 struct MotionSample {
444 MotionSample* next;
445
Jeff Brown4e91a182011-04-07 11:38:09 -0700446 nsecs_t eventTime; // may be updated during coalescing
447 nsecs_t eventTimeBeforeCoalescing; // not updated during coalescing
Jeff Brown46b9ac02010-04-22 18:58:52 -0700448 PointerCoords pointerCoords[MAX_POINTERS];
449 };
450
451 struct MotionEntry : EventEntry {
452 int32_t deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -0800453 uint32_t source;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700454 int32_t action;
Jeff Brown85a31762010-09-01 17:01:00 -0700455 int32_t flags;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700456 int32_t metaState;
457 int32_t edgeFlags;
458 float xPrecision;
459 float yPrecision;
460 nsecs_t downTime;
461 uint32_t pointerCount;
462 int32_t pointerIds[MAX_POINTERS];
463
464 // Linked list of motion samples associated with this motion event.
465 MotionSample firstSample;
466 MotionSample* lastSample;
Jeff Brownae9fc032010-08-18 15:51:08 -0700467
468 uint32_t countSamples() const;
Jeff Brown4e91a182011-04-07 11:38:09 -0700469
470 // Checks whether we can append samples, assuming the device id and source are the same.
471 bool canAppendSamples(int32_t action, uint32_t pointerCount,
472 const int32_t* pointerIds) const;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700473 };
474
Jeff Brown9c3cda02010-06-15 01:31:58 -0700475 // Tracks the progress of dispatching a particular event to a particular connection.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700476 struct DispatchEntry : Link<DispatchEntry> {
477 EventEntry* eventEntry; // the event to dispatch
478 int32_t targetFlags;
479 float xOffset;
480 float yOffset;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400481 float scaleFactor;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700482
483 // True if dispatch has started.
484 bool inProgress;
485
486 // For motion events:
487 // Pointer to the first motion sample to dispatch in this cycle.
488 // Usually NULL to indicate that the list of motion samples begins at
489 // MotionEntry::firstSample. Otherwise, some samples were dispatched in a previous
490 // cycle and this pointer indicates the location of the first remainining sample
491 // to dispatch during the current cycle.
492 MotionSample* headMotionSample;
493 // Pointer to a motion sample to dispatch in the next cycle if the dispatcher was
494 // unable to send all motion samples during this cycle. On the next cycle,
495 // headMotionSample will be initialized to tailMotionSample and tailMotionSample
496 // will be set to NULL.
497 MotionSample* tailMotionSample;
Jeff Brown6ec402b2010-07-28 15:48:59 -0700498
Jeff Brown519e0242010-09-15 15:18:56 -0700499 inline bool hasForegroundTarget() const {
500 return targetFlags & InputTarget::FLAG_FOREGROUND;
Jeff Brownb88102f2010-09-08 11:49:43 -0700501 }
Jeff Brown01ce2e92010-09-26 22:20:12 -0700502
503 inline bool isSplit() const {
504 return targetFlags & InputTarget::FLAG_SPLIT;
505 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700506 };
507
Jeff Brown9c3cda02010-06-15 01:31:58 -0700508 // A command entry captures state and behavior for an action to be performed in the
509 // dispatch loop after the initial processing has taken place. It is essentially
510 // a kind of continuation used to postpone sensitive policy interactions to a point
511 // in the dispatch loop where it is safe to release the lock (generally after finishing
512 // the critical parts of the dispatch cycle).
513 //
514 // The special thing about commands is that they can voluntarily release and reacquire
515 // the dispatcher lock at will. Initially when the command starts running, the
516 // dispatcher lock is held. However, if the command needs to call into the policy to
517 // do some work, it can release the lock, do the work, then reacquire the lock again
518 // before returning.
519 //
520 // This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch
521 // never calls into the policy while holding its lock.
522 //
523 // Commands are implicitly 'LockedInterruptible'.
524 struct CommandEntry;
525 typedef void (InputDispatcher::*Command)(CommandEntry* commandEntry);
526
Jeff Brown7fbdc842010-06-17 20:52:56 -0700527 class Connection;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700528 struct CommandEntry : Link<CommandEntry> {
529 CommandEntry();
530 ~CommandEntry();
531
532 Command command;
533
534 // parameters for the command (usage varies by command)
Jeff Brown7fbdc842010-06-17 20:52:56 -0700535 sp<Connection> connection;
Jeff Brownb88102f2010-09-08 11:49:43 -0700536 nsecs_t eventTime;
537 KeyEntry* keyEntry;
538 sp<InputChannel> inputChannel;
539 sp<InputApplicationHandle> inputApplicationHandle;
Jeff Brown928e0542011-01-10 11:17:36 -0800540 sp<InputWindowHandle> inputWindowHandle;
Jeff Brownb88102f2010-09-08 11:49:43 -0700541 int32_t userActivityEventType;
Jeff Brown3915bb82010-11-05 15:02:16 -0700542 bool handled;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700543 };
544
545 // Generic queue implementation.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700546 template <typename T>
547 struct Queue {
Jeff Brownb88102f2010-09-08 11:49:43 -0700548 T headSentinel;
549 T tailSentinel;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700550
551 inline Queue() {
Jeff Brownb88102f2010-09-08 11:49:43 -0700552 headSentinel.prev = NULL;
553 headSentinel.next = & tailSentinel;
554 tailSentinel.prev = & headSentinel;
555 tailSentinel.next = NULL;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700556 }
557
Jeff Brownb88102f2010-09-08 11:49:43 -0700558 inline bool isEmpty() const {
559 return headSentinel.next == & tailSentinel;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700560 }
561
562 inline void enqueueAtTail(T* entry) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700563 T* last = tailSentinel.prev;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700564 last->next = entry;
565 entry->prev = last;
Jeff Brownb88102f2010-09-08 11:49:43 -0700566 entry->next = & tailSentinel;
567 tailSentinel.prev = entry;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700568 }
569
570 inline void enqueueAtHead(T* entry) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700571 T* first = headSentinel.next;
572 headSentinel.next = entry;
573 entry->prev = & headSentinel;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700574 entry->next = first;
575 first->prev = entry;
576 }
577
578 inline void dequeue(T* entry) {
579 entry->prev->next = entry->next;
580 entry->next->prev = entry->prev;
581 }
582
583 inline T* dequeueAtHead() {
Jeff Brownb88102f2010-09-08 11:49:43 -0700584 T* first = headSentinel.next;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700585 dequeue(first);
586 return first;
587 }
Jeff Brown519e0242010-09-15 15:18:56 -0700588
589 uint32_t count() const;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700590 };
591
592 /* Allocates queue entries and performs reference counting as needed. */
593 class Allocator {
594 public:
595 Allocator();
596
Jeff Brown01ce2e92010-09-26 22:20:12 -0700597 InjectionState* obtainInjectionState(int32_t injectorPid, int32_t injectorUid);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700598 ConfigurationChangedEntry* obtainConfigurationChangedEntry(nsecs_t eventTime);
599 KeyEntry* obtainKeyEntry(nsecs_t eventTime,
Jeff Brown58a2da82011-01-25 16:02:22 -0800600 int32_t deviceId, uint32_t source, uint32_t policyFlags, int32_t action,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700601 int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState,
602 int32_t repeatCount, nsecs_t downTime);
603 MotionEntry* obtainMotionEntry(nsecs_t eventTime,
Jeff Brown58a2da82011-01-25 16:02:22 -0800604 int32_t deviceId, uint32_t source, uint32_t policyFlags, int32_t action,
Jeff Brown85a31762010-09-01 17:01:00 -0700605 int32_t flags, int32_t metaState, int32_t edgeFlags,
606 float xPrecision, float yPrecision,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700607 nsecs_t downTime, uint32_t pointerCount,
608 const int32_t* pointerIds, const PointerCoords* pointerCoords);
Jeff Brownb88102f2010-09-08 11:49:43 -0700609 DispatchEntry* obtainDispatchEntry(EventEntry* eventEntry,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400610 int32_t targetFlags, float xOffset, float yOffset, float scaleFactor);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700611 CommandEntry* obtainCommandEntry(Command command);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700612
Jeff Brown01ce2e92010-09-26 22:20:12 -0700613 void releaseInjectionState(InjectionState* injectionState);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700614 void releaseEventEntry(EventEntry* entry);
615 void releaseConfigurationChangedEntry(ConfigurationChangedEntry* entry);
616 void releaseKeyEntry(KeyEntry* entry);
617 void releaseMotionEntry(MotionEntry* entry);
Jeff Browna032cc02011-03-07 16:56:21 -0800618 void freeMotionSample(MotionSample* sample);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700619 void releaseDispatchEntry(DispatchEntry* entry);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700620 void releaseCommandEntry(CommandEntry* entry);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700621
Jeff Brown01ce2e92010-09-26 22:20:12 -0700622 void recycleKeyEntry(KeyEntry* entry);
623
Jeff Brown46b9ac02010-04-22 18:58:52 -0700624 void appendMotionSample(MotionEntry* motionEntry,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700625 nsecs_t eventTime, const PointerCoords* pointerCoords);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700626
627 private:
Jeff Brown01ce2e92010-09-26 22:20:12 -0700628 Pool<InjectionState> mInjectionStatePool;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700629 Pool<ConfigurationChangedEntry> mConfigurationChangeEntryPool;
630 Pool<KeyEntry> mKeyEntryPool;
631 Pool<MotionEntry> mMotionEntryPool;
632 Pool<MotionSample> mMotionSamplePool;
633 Pool<DispatchEntry> mDispatchEntryPool;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700634 Pool<CommandEntry> mCommandEntryPool;
Jeff Brown7fbdc842010-06-17 20:52:56 -0700635
Jeff Brownb6997262010-10-08 22:31:17 -0700636 void initializeEventEntry(EventEntry* entry, int32_t type, nsecs_t eventTime,
637 uint32_t policyFlags);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700638 void releaseEventEntryInjectionState(EventEntry* entry);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700639 };
640
Jeff Brownda3d5a92011-03-29 15:11:34 -0700641 /* Specifies which events are to be canceled and why. */
642 struct CancelationOptions {
643 enum Mode {
Jeff Brownb6997262010-10-08 22:31:17 -0700644 CANCEL_ALL_EVENTS = 0,
645 CANCEL_POINTER_EVENTS = 1,
646 CANCEL_NON_POINTER_EVENTS = 2,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800647 CANCEL_FALLBACK_EVENTS = 3,
Jeff Brownb6997262010-10-08 22:31:17 -0700648 };
649
Jeff Brownda3d5a92011-03-29 15:11:34 -0700650 // The criterion to use to determine which events should be canceled.
651 Mode mode;
652
653 // Descriptive reason for the cancelation.
654 const char* reason;
655
656 // The specific keycode of the key event to cancel, or -1 to cancel any key event.
657 int32_t keyCode;
658
659 CancelationOptions(Mode mode, const char* reason) :
660 mode(mode), reason(reason), keyCode(-1) { }
661 };
662
663 /* Tracks dispatched key and motion event state so that cancelation events can be
664 * synthesized when events are dropped. */
665 class InputState {
666 public:
Jeff Brownb88102f2010-09-08 11:49:43 -0700667 InputState();
668 ~InputState();
669
670 // Returns true if there is no state to be canceled.
671 bool isNeutral() const;
672
Jeff Brownb88102f2010-09-08 11:49:43 -0700673 // Records tracking information for an event that has just been published.
Jeff Browna032cc02011-03-07 16:56:21 -0800674 void trackEvent(const EventEntry* entry, int32_t action);
Jeff Brownb88102f2010-09-08 11:49:43 -0700675
676 // Records tracking information for a key event that has just been published.
Jeff Browna032cc02011-03-07 16:56:21 -0800677 void trackKey(const KeyEntry* entry, int32_t action);
Jeff Brownb88102f2010-09-08 11:49:43 -0700678
679 // Records tracking information for a motion event that has just been published.
Jeff Browna032cc02011-03-07 16:56:21 -0800680 void trackMotion(const MotionEntry* entry, int32_t action);
Jeff Brownb88102f2010-09-08 11:49:43 -0700681
Jeff Brownb6997262010-10-08 22:31:17 -0700682 // Synthesizes cancelation events for the current state and resets the tracked state.
683 void synthesizeCancelationEvents(nsecs_t currentTime, Allocator* allocator,
Jeff Brownda3d5a92011-03-29 15:11:34 -0700684 Vector<EventEntry*>& outEvents, const CancelationOptions& options);
Jeff Brownb88102f2010-09-08 11:49:43 -0700685
686 // Clears the current state.
687 void clear();
688
Jeff Brown9c9f1a32010-10-11 18:32:20 -0700689 // Copies pointer-related parts of the input state to another instance.
690 void copyPointerStateTo(InputState& other) const;
691
Jeff Brownda3d5a92011-03-29 15:11:34 -0700692 // Gets the fallback key associated with a keycode.
693 // Returns -1 if none.
694 // Returns AKEYCODE_UNKNOWN if we are only dispatching the unhandled key to the policy.
695 int32_t getFallbackKey(int32_t originalKeyCode);
696
697 // Sets the fallback key for a particular keycode.
698 void setFallbackKey(int32_t originalKeyCode, int32_t fallbackKeyCode);
699
700 // Removes the fallback key for a particular keycode.
701 void removeFallbackKey(int32_t originalKeyCode);
702
703 inline const KeyedVector<int32_t, int32_t>& getFallbackKeys() const {
704 return mFallbackKeys;
705 }
706
Jeff Brownb88102f2010-09-08 11:49:43 -0700707 private:
Jeff Brownb88102f2010-09-08 11:49:43 -0700708 struct KeyMemento {
709 int32_t deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -0800710 uint32_t source;
Jeff Brownb88102f2010-09-08 11:49:43 -0700711 int32_t keyCode;
712 int32_t scanCode;
Jeff Brown49ed71d2010-12-06 17:13:33 -0800713 int32_t flags;
Jeff Brownb88102f2010-09-08 11:49:43 -0700714 nsecs_t downTime;
715 };
716
717 struct MotionMemento {
718 int32_t deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -0800719 uint32_t source;
Jeff Brownb88102f2010-09-08 11:49:43 -0700720 float xPrecision;
721 float yPrecision;
722 nsecs_t downTime;
723 uint32_t pointerCount;
724 int32_t pointerIds[MAX_POINTERS];
725 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Browna032cc02011-03-07 16:56:21 -0800726 bool hovering;
Jeff Brownb88102f2010-09-08 11:49:43 -0700727
728 void setPointers(const MotionEntry* entry);
729 };
730
731 Vector<KeyMemento> mKeyMementos;
732 Vector<MotionMemento> mMotionMementos;
Jeff Brownda3d5a92011-03-29 15:11:34 -0700733 KeyedVector<int32_t, int32_t> mFallbackKeys;
Jeff Brownb6997262010-10-08 22:31:17 -0700734
Jeff Brown49ed71d2010-12-06 17:13:33 -0800735 static bool shouldCancelKey(const KeyMemento& memento,
Jeff Brownda3d5a92011-03-29 15:11:34 -0700736 const CancelationOptions& options);
Jeff Brown49ed71d2010-12-06 17:13:33 -0800737 static bool shouldCancelMotion(const MotionMemento& memento,
Jeff Brownda3d5a92011-03-29 15:11:34 -0700738 const CancelationOptions& options);
Jeff Brownb88102f2010-09-08 11:49:43 -0700739 };
740
Jeff Brown46b9ac02010-04-22 18:58:52 -0700741 /* Manages the dispatch state associated with a single input channel. */
742 class Connection : public RefBase {
743 protected:
744 virtual ~Connection();
745
746 public:
747 enum Status {
748 // Everything is peachy.
749 STATUS_NORMAL,
750 // An unrecoverable communication error has occurred.
751 STATUS_BROKEN,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700752 // The input channel has been unregistered.
753 STATUS_ZOMBIE
754 };
755
756 Status status;
Jeff Brown928e0542011-01-10 11:17:36 -0800757 sp<InputChannel> inputChannel; // never null
758 sp<InputWindowHandle> inputWindowHandle; // may be null
Jeff Brown46b9ac02010-04-22 18:58:52 -0700759 InputPublisher inputPublisher;
Jeff Brownb88102f2010-09-08 11:49:43 -0700760 InputState inputState;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700761 Queue<DispatchEntry> outboundQueue;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700762
763 nsecs_t lastEventTime; // the time when the event was originally captured
764 nsecs_t lastDispatchTime; // the time when the last event was dispatched
Jeff Brown46b9ac02010-04-22 18:58:52 -0700765
Jeff Brown928e0542011-01-10 11:17:36 -0800766 explicit Connection(const sp<InputChannel>& inputChannel,
767 const sp<InputWindowHandle>& inputWindowHandle);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700768
Jeff Brown9c3cda02010-06-15 01:31:58 -0700769 inline const char* getInputChannelName() const { return inputChannel->getName().string(); }
770
771 const char* getStatusLabel() const;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700772
773 // Finds a DispatchEntry in the outbound queue associated with the specified event.
774 // Returns NULL if not found.
775 DispatchEntry* findQueuedDispatchEntryForEvent(const EventEntry* eventEntry) const;
776
Jeff Brown46b9ac02010-04-22 18:58:52 -0700777 // Gets the time since the current event was originally obtained from the input driver.
Jeff Brownb88102f2010-09-08 11:49:43 -0700778 inline double getEventLatencyMillis(nsecs_t currentTime) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700779 return (currentTime - lastEventTime) / 1000000.0;
780 }
781
782 // Gets the time since the current event entered the outbound dispatch queue.
Jeff Brownb88102f2010-09-08 11:49:43 -0700783 inline double getDispatchLatencyMillis(nsecs_t currentTime) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700784 return (currentTime - lastDispatchTime) / 1000000.0;
785 }
786
Jeff Brown46b9ac02010-04-22 18:58:52 -0700787 status_t initialize();
788 };
789
Jeff Brownb6997262010-10-08 22:31:17 -0700790 enum DropReason {
791 DROP_REASON_NOT_DROPPED = 0,
792 DROP_REASON_POLICY = 1,
793 DROP_REASON_APP_SWITCH = 2,
794 DROP_REASON_DISABLED = 3,
Jeff Brown928e0542011-01-10 11:17:36 -0800795 DROP_REASON_BLOCKED = 4,
796 DROP_REASON_STALE = 5,
Jeff Brownb6997262010-10-08 22:31:17 -0700797 };
798
Jeff Brown9c3cda02010-06-15 01:31:58 -0700799 sp<InputDispatcherPolicyInterface> mPolicy;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700800
801 Mutex mLock;
802
Jeff Brown46b9ac02010-04-22 18:58:52 -0700803 Allocator mAllocator;
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700804 sp<Looper> mLooper;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700805
Jeff Brownb88102f2010-09-08 11:49:43 -0700806 EventEntry* mPendingEvent;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700807 Queue<EventEntry> mInboundQueue;
808 Queue<CommandEntry> mCommandQueue;
809
Jeff Brownb88102f2010-09-08 11:49:43 -0700810 Vector<EventEntry*> mTempCancelationEvents;
811
812 void dispatchOnceInnerLocked(nsecs_t keyRepeatTimeout, nsecs_t keyRepeatDelay,
813 nsecs_t* nextWakeupTime);
814
Jeff Brown4e91a182011-04-07 11:38:09 -0700815 // Batches a new sample onto a motion entry.
816 // Assumes that the we have already checked that we can append samples.
817 void batchMotionLocked(MotionEntry* entry, nsecs_t eventTime, int32_t metaState,
818 const PointerCoords* pointerCoords, const char* eventDescription);
819
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700820 // Enqueues an inbound event. Returns true if mLooper->wake() should be called.
Jeff Brownb88102f2010-09-08 11:49:43 -0700821 bool enqueueInboundEventLocked(EventEntry* entry);
822
Jeff Brownb6997262010-10-08 22:31:17 -0700823 // Cleans up input state when dropping an inbound event.
824 void dropInboundEventLocked(EventEntry* entry, DropReason dropReason);
825
Jeff Brownb88102f2010-09-08 11:49:43 -0700826 // App switch latency optimization.
Jeff Brownb6997262010-10-08 22:31:17 -0700827 bool mAppSwitchSawKeyDown;
Jeff Brownb88102f2010-09-08 11:49:43 -0700828 nsecs_t mAppSwitchDueTime;
829
Jeff Brownb6997262010-10-08 22:31:17 -0700830 static bool isAppSwitchKeyCode(int32_t keyCode);
831 bool isAppSwitchKeyEventLocked(KeyEntry* keyEntry);
Jeff Brownb88102f2010-09-08 11:49:43 -0700832 bool isAppSwitchPendingLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700833 void resetPendingAppSwitchLocked(bool handled);
834
Jeff Brown928e0542011-01-10 11:17:36 -0800835 // Stale event latency optimization.
836 static bool isStaleEventLocked(nsecs_t currentTime, EventEntry* entry);
837
838 // Blocked event latency optimization. Drops old events when the user intends
839 // to transfer focus to a new application.
840 EventEntry* mNextUnblockedEvent;
841
842 const InputWindow* findTouchedWindowAtLocked(int32_t x, int32_t y);
843
Jeff Brown46b9ac02010-04-22 18:58:52 -0700844 // All registered connections mapped by receive pipe file descriptor.
845 KeyedVector<int, sp<Connection> > mConnectionsByReceiveFd;
846
Jeff Brown519e0242010-09-15 15:18:56 -0700847 ssize_t getConnectionIndexLocked(const sp<InputChannel>& inputChannel);
Jeff Brown2cbecea2010-08-17 15:59:26 -0700848
Jeff Brown46b9ac02010-04-22 18:58:52 -0700849 // Active connections are connections that have a non-empty outbound queue.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700850 // We don't use a ref-counted pointer here because we explicitly abort connections
851 // during unregistration which causes the connection's outbound queue to be cleared
852 // and the connection itself to be deactivated.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700853 Vector<Connection*> mActiveConnections;
854
Jeff Brownb88102f2010-09-08 11:49:43 -0700855 // Input channels that will receive a copy of all input events.
856 Vector<sp<InputChannel> > mMonitoringChannels;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700857
Jeff Brown7fbdc842010-06-17 20:52:56 -0700858 // Event injection and synchronization.
859 Condition mInjectionResultAvailableCondition;
Jeff Brownb6997262010-10-08 22:31:17 -0700860 bool hasInjectionPermission(int32_t injectorPid, int32_t injectorUid);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700861 void setInjectionResultLocked(EventEntry* entry, int32_t injectionResult);
862
Jeff Brown6ec402b2010-07-28 15:48:59 -0700863 Condition mInjectionSyncFinishedCondition;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700864 void incrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brown519e0242010-09-15 15:18:56 -0700865 void decrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brown6ec402b2010-07-28 15:48:59 -0700866
Jeff Brownae9fc032010-08-18 15:51:08 -0700867 // Throttling state.
868 struct ThrottleState {
869 nsecs_t minTimeBetweenEvents;
870
871 nsecs_t lastEventTime;
872 int32_t lastDeviceId;
873 uint32_t lastSource;
874
875 uint32_t originalSampleCount; // only collected during debugging
876 } mThrottleState;
877
Jeff Brown46b9ac02010-04-22 18:58:52 -0700878 // Key repeat tracking.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700879 struct KeyRepeatState {
880 KeyEntry* lastKeyEntry; // or null if no repeat
881 nsecs_t nextRepeatTime;
882 } mKeyRepeatState;
883
884 void resetKeyRepeatLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700885 KeyEntry* synthesizeKeyRepeatLocked(nsecs_t currentTime, nsecs_t keyRepeatTimeout);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700886
Jeff Brown9c3cda02010-06-15 01:31:58 -0700887 // Deferred command processing.
888 bool runCommandsLockedInterruptible();
889 CommandEntry* postCommandLocked(Command command);
890
Jeff Brownb88102f2010-09-08 11:49:43 -0700891 // Inbound event processing.
892 void drainInboundQueueLocked();
Jeff Brown54a18252010-09-16 14:07:33 -0700893 void releasePendingEventLocked();
894 void releaseInboundEventLocked(EventEntry* entry);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700895
Jeff Brownb88102f2010-09-08 11:49:43 -0700896 // Dispatch state.
897 bool mDispatchEnabled;
898 bool mDispatchFrozen;
Jeff Brown0029c662011-03-30 02:25:18 -0700899 bool mInputFilterEnabled;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700900
Jeff Brownb88102f2010-09-08 11:49:43 -0700901 Vector<InputWindow> mWindows;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700902
903 const InputWindow* getWindowLocked(const sp<InputChannel>& inputChannel);
Jeff Brownb88102f2010-09-08 11:49:43 -0700904
905 // Focus tracking for keys, trackball, etc.
Jeff Brown01ce2e92010-09-26 22:20:12 -0700906 const InputWindow* mFocusedWindow;
Jeff Brownb88102f2010-09-08 11:49:43 -0700907
908 // Focus tracking for touch.
Jeff Brown01ce2e92010-09-26 22:20:12 -0700909 struct TouchedWindow {
910 const InputWindow* window;
911 int32_t targetFlags;
Jeff Brown46e75292010-11-10 16:53:45 -0800912 BitSet32 pointerIds; // zero unless target flag FLAG_SPLIT is set
Jeff Brown01ce2e92010-09-26 22:20:12 -0700913 sp<InputChannel> channel;
Jeff Brownb88102f2010-09-08 11:49:43 -0700914 };
Jeff Brown01ce2e92010-09-26 22:20:12 -0700915 struct TouchState {
916 bool down;
917 bool split;
Jeff Brown95712852011-01-04 19:41:59 -0800918 int32_t deviceId; // id of the device that is currently down, others are rejected
Jeff Brown58a2da82011-01-25 16:02:22 -0800919 uint32_t source; // source of the device that is current down, others are rejected
Jeff Brown01ce2e92010-09-26 22:20:12 -0700920 Vector<TouchedWindow> windows;
921
922 TouchState();
923 ~TouchState();
924 void reset();
925 void copyFrom(const TouchState& other);
926 void addOrUpdateWindow(const InputWindow* window, int32_t targetFlags, BitSet32 pointerIds);
Jeff Browna032cc02011-03-07 16:56:21 -0800927 void filterNonAsIsTouchWindows();
Jeff Brown01ce2e92010-09-26 22:20:12 -0700928 const InputWindow* getFirstForegroundWindow();
929 };
930
931 TouchState mTouchState;
932 TouchState mTempTouchState;
Jeff Brownb88102f2010-09-08 11:49:43 -0700933
934 // Focused application.
935 InputApplication* mFocusedApplication;
936 InputApplication mFocusedApplicationStorage; // preallocated storage for mFocusedApplication
937 void releaseFocusedApplicationLocked();
938
939 // Dispatch inbound events.
940 bool dispatchConfigurationChangedLocked(
941 nsecs_t currentTime, ConfigurationChangedEntry* entry);
942 bool dispatchKeyLocked(
943 nsecs_t currentTime, KeyEntry* entry, nsecs_t keyRepeatTimeout,
Jeff Browne20c9e02010-10-11 14:20:19 -0700944 DropReason* dropReason, nsecs_t* nextWakeupTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700945 bool dispatchMotionLocked(
946 nsecs_t currentTime, MotionEntry* entry,
Jeff Browne20c9e02010-10-11 14:20:19 -0700947 DropReason* dropReason, nsecs_t* nextWakeupTime);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700948 void dispatchEventToCurrentInputTargetsLocked(
949 nsecs_t currentTime, EventEntry* entry, bool resumeWithAppendedMotionSample);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700950
Jeff Brownb88102f2010-09-08 11:49:43 -0700951 void logOutboundKeyDetailsLocked(const char* prefix, const KeyEntry* entry);
952 void logOutboundMotionDetailsLocked(const char* prefix, const MotionEntry* entry);
953
954 // The input targets that were most recently identified for dispatch.
Jeff Brownb88102f2010-09-08 11:49:43 -0700955 bool mCurrentInputTargetsValid; // false while targets are being recomputed
956 Vector<InputTarget> mCurrentInputTargets;
Jeff Brownb88102f2010-09-08 11:49:43 -0700957
958 enum InputTargetWaitCause {
959 INPUT_TARGET_WAIT_CAUSE_NONE,
960 INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY,
961 INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY,
962 };
963
964 InputTargetWaitCause mInputTargetWaitCause;
965 nsecs_t mInputTargetWaitStartTime;
966 nsecs_t mInputTargetWaitTimeoutTime;
967 bool mInputTargetWaitTimeoutExpired;
Jeff Brown928e0542011-01-10 11:17:36 -0800968 sp<InputApplicationHandle> mInputTargetWaitApplication;
Jeff Brownb88102f2010-09-08 11:49:43 -0700969
Jeff Browna032cc02011-03-07 16:56:21 -0800970 // Contains the last window which received a hover event.
971 const InputWindow* mLastHoverWindow;
972
Jeff Brownb88102f2010-09-08 11:49:43 -0700973 // Finding targets for input events.
Jeff Brown54a18252010-09-16 14:07:33 -0700974 void resetTargetsLocked();
Jeff Brown01ce2e92010-09-26 22:20:12 -0700975 void commitTargetsLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700976 int32_t handleTargetsNotReadyLocked(nsecs_t currentTime, const EventEntry* entry,
977 const InputApplication* application, const InputWindow* window,
978 nsecs_t* nextWakeupTime);
Jeff Brown519e0242010-09-15 15:18:56 -0700979 void resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout,
980 const sp<InputChannel>& inputChannel);
981 nsecs_t getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700982 void resetANRTimeoutsLocked();
983
Jeff Brown01ce2e92010-09-26 22:20:12 -0700984 int32_t findFocusedWindowTargetsLocked(nsecs_t currentTime, const EventEntry* entry,
985 nsecs_t* nextWakeupTime);
986 int32_t findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry* entry,
Jeff Browna032cc02011-03-07 16:56:21 -0800987 nsecs_t* nextWakeupTime, bool* outConflictingPointerActions,
988 const MotionSample** outSplitBatchAfterSample);
Jeff Brownb88102f2010-09-08 11:49:43 -0700989
Jeff Brown01ce2e92010-09-26 22:20:12 -0700990 void addWindowTargetLocked(const InputWindow* window, int32_t targetFlags,
991 BitSet32 pointerIds);
Jeff Brownb88102f2010-09-08 11:49:43 -0700992 void addMonitoringTargetsLocked();
Jeff Browne2fe69e2010-10-18 13:21:23 -0700993 void pokeUserActivityLocked(const EventEntry* eventEntry);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700994 bool checkInjectionPermission(const InputWindow* window, const InjectionState* injectionState);
Jeff Brown19dfc832010-10-05 12:26:23 -0700995 bool isWindowObscuredAtPointLocked(const InputWindow* window, int32_t x, int32_t y) const;
Jeff Brown519e0242010-09-15 15:18:56 -0700996 bool isWindowFinishedWithPreviousInputLocked(const InputWindow* window);
Jeff Brown519e0242010-09-15 15:18:56 -0700997 String8 getApplicationWindowLabelLocked(const InputApplication* application,
998 const InputWindow* window);
Jeff Brownb88102f2010-09-08 11:49:43 -0700999
Jeff Brown46b9ac02010-04-22 18:58:52 -07001000 // Manage the dispatch cycle for a single connection.
Jeff Brown7fbdc842010-06-17 20:52:56 -07001001 // These methods are deliberately not Interruptible because doing all of the work
1002 // with the mutex held makes it easier to ensure that connection invariants are maintained.
1003 // If needed, the methods post commands to run later once the critical bits are done.
1004 void prepareDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001005 EventEntry* eventEntry, const InputTarget* inputTarget,
1006 bool resumeWithAppendedMotionSample);
Jeff Browna032cc02011-03-07 16:56:21 -08001007 void enqueueDispatchEntryLocked(const sp<Connection>& connection,
1008 EventEntry* eventEntry, const InputTarget* inputTarget,
1009 bool resumeWithAppendedMotionSample, int32_t dispatchMode);
Jeff Brown519e0242010-09-15 15:18:56 -07001010 void startDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown3915bb82010-11-05 15:02:16 -07001011 void finishDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
1012 bool handled);
Jeff Brownb88102f2010-09-08 11:49:43 -07001013 void startNextDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brownb6997262010-10-08 22:31:17 -07001014 void abortBrokenDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown519e0242010-09-15 15:18:56 -07001015 void drainOutboundQueueLocked(Connection* connection);
Jeff Brown4fe6c3e2010-09-13 23:17:30 -07001016 static int handleReceiveCallback(int receiveFd, int events, void* data);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001017
Jeff Brownb6997262010-10-08 22:31:17 -07001018 void synthesizeCancelationEventsForAllConnectionsLocked(
Jeff Brownda3d5a92011-03-29 15:11:34 -07001019 const CancelationOptions& options);
Jeff Brownb6997262010-10-08 22:31:17 -07001020 void synthesizeCancelationEventsForInputChannelLocked(const sp<InputChannel>& channel,
Jeff Brownda3d5a92011-03-29 15:11:34 -07001021 const CancelationOptions& options);
Jeff Brownb6997262010-10-08 22:31:17 -07001022 void synthesizeCancelationEventsForConnectionLocked(const sp<Connection>& connection,
Jeff Brownda3d5a92011-03-29 15:11:34 -07001023 const CancelationOptions& options);
Jeff Brownb6997262010-10-08 22:31:17 -07001024
Jeff Brown01ce2e92010-09-26 22:20:12 -07001025 // Splitting motion events across windows.
1026 MotionEntry* splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds);
1027
Jeff Brown120a4592010-10-27 18:43:51 -07001028 // Reset and drop everything the dispatcher is doing.
1029 void resetAndDropEverythingLocked(const char* reason);
1030
Jeff Brownb88102f2010-09-08 11:49:43 -07001031 // Dump state.
1032 void dumpDispatchStateLocked(String8& dump);
1033 void logDispatchStateLocked();
1034
Jeff Brown46b9ac02010-04-22 18:58:52 -07001035 // Add or remove a connection to the mActiveConnections vector.
1036 void activateConnectionLocked(Connection* connection);
1037 void deactivateConnectionLocked(Connection* connection);
1038
1039 // Interesting events that we might like to log or tell the framework about.
Jeff Brown9c3cda02010-06-15 01:31:58 -07001040 void onDispatchCycleStartedLocked(
Jeff Brown7fbdc842010-06-17 20:52:56 -07001041 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001042 void onDispatchCycleFinishedLocked(
Jeff Brown3915bb82010-11-05 15:02:16 -07001043 nsecs_t currentTime, const sp<Connection>& connection, bool handled);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001044 void onDispatchCycleBrokenLocked(
Jeff Brown7fbdc842010-06-17 20:52:56 -07001045 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown519e0242010-09-15 15:18:56 -07001046 void onANRLocked(
1047 nsecs_t currentTime, const InputApplication* application, const InputWindow* window,
1048 nsecs_t eventTime, nsecs_t waitStartTime);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001049
Jeff Brown7fbdc842010-06-17 20:52:56 -07001050 // Outbound policy interactions.
Jeff Brownb88102f2010-09-08 11:49:43 -07001051 void doNotifyConfigurationChangedInterruptible(CommandEntry* commandEntry);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001052 void doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown519e0242010-09-15 15:18:56 -07001053 void doNotifyANRLockedInterruptible(CommandEntry* commandEntry);
Jeff Brownb88102f2010-09-08 11:49:43 -07001054 void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown3915bb82010-11-05 15:02:16 -07001055 void doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry);
Jeff Brownb88102f2010-09-08 11:49:43 -07001056 void doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown3915bb82010-11-05 15:02:16 -07001057 void initializeKeyEvent(KeyEvent* event, const KeyEntry* entry);
Jeff Brown519e0242010-09-15 15:18:56 -07001058
1059 // Statistics gathering.
1060 void updateDispatchStatisticsLocked(nsecs_t currentTime, const EventEntry* entry,
1061 int32_t injectionResult, nsecs_t timeSpentWaitingForApplication);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001062};
1063
1064/* Enqueues and dispatches input events, endlessly. */
1065class InputDispatcherThread : public Thread {
1066public:
1067 explicit InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher);
1068 ~InputDispatcherThread();
1069
1070private:
1071 virtual bool threadLoop();
1072
1073 sp<InputDispatcherInterface> mDispatcher;
1074};
1075
1076} // namespace android
1077
Jeff Brownb88102f2010-09-08 11:49:43 -07001078#endif // _UI_INPUT_DISPATCHER_H