blob: 39fa2033af73ec449335f3b71ebd9197e0bf030f [file] [log] [blame]
Jeff Brown46b9ac0a2010-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 Brown46b9ac0a2010-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 Brown46b9ac0a2010-04-22 18:58:52 -070029#include <utils/Pool.h>
Jeff Brown01ce2e92010-09-26 22:20:12 -070030#include <utils/BitSet.h>
Jeff Brown46b9ac0a2010-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 Brown46b9ac0a2010-04-22 18:58:52 -070035
Jeff Brown928e0542011-01-10 11:17:36 -080036#include "InputWindow.h"
37#include "InputApplication.h"
38
Jeff Brown46b9ac0a2010-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. */
89 FLAG_FOREGROUND = 0x01,
Jeff Brown9c3cda02010-06-15 01:31:58 -070090
Jeff Brown85a31762010-09-01 17:01:00 -070091 /* This flag indicates that a MotionEvent with AMOTION_EVENT_ACTION_DOWN falls outside
92 * of the area of this target and so should instead be delivered as an
93 * AMOTION_EVENT_ACTION_OUTSIDE to this target. */
Jeff Brown9c3cda02010-06-15 01:31:58 -070094 FLAG_OUTSIDE = 0x02,
95
Jeff Brown85a31762010-09-01 17:01:00 -070096 /* This flag indicates that the target of a MotionEvent is partly or wholly
97 * obscured by another visible window above it. The motion event should be
98 * delivered with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED. */
Jeff Brown01ce2e92010-09-26 22:20:12 -070099 FLAG_WINDOW_IS_OBSCURED = 0x04,
100
101 /* This flag indicates that a motion event is being split across multiple windows. */
102 FLAG_SPLIT = 0x08,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700103 };
104
105 // The input channel to be targeted.
106 sp<InputChannel> inputChannel;
107
108 // Flags for the input target.
109 int32_t flags;
110
Jeff Brown9c3cda02010-06-15 01:31:58 -0700111 // The x and y offset to add to a MotionEvent as it is delivered.
112 // (ignored for KeyEvents)
113 float xOffset, yOffset;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700114
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400115 // Scaling factor to apply to MotionEvent as it is delivered.
116 // (ignored for KeyEvents)
117 float scaleFactor;
118
Jeff Brown01ce2e92010-09-26 22:20:12 -0700119 // The subset of pointer ids to include in motion events dispatched to this input target
120 // if FLAG_SPLIT is set.
121 BitSet32 pointerIds;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700122};
123
Jeff Brown7fbdc842010-06-17 20:52:56 -0700124
Jeff Brown9c3cda02010-06-15 01:31:58 -0700125/*
126 * Input dispatcher policy interface.
127 *
128 * The input reader policy is used by the input reader to interact with the Window Manager
129 * and other system components.
130 *
131 * The actual implementation is partially supported by callbacks into the DVM
132 * via JNI. This interface is also mocked in the unit tests.
133 */
134class InputDispatcherPolicyInterface : public virtual RefBase {
135protected:
136 InputDispatcherPolicyInterface() { }
137 virtual ~InputDispatcherPolicyInterface() { }
138
139public:
140 /* Notifies the system that a configuration change has occurred. */
141 virtual void notifyConfigurationChanged(nsecs_t when) = 0;
142
Jeff Brownb88102f2010-09-08 11:49:43 -0700143 /* Notifies the system that an application is not responding.
144 * Returns a new timeout to continue waiting, or 0 to abort dispatch. */
Jeff Brown519e0242010-09-15 15:18:56 -0700145 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brown928e0542011-01-10 11:17:36 -0800146 const sp<InputWindowHandle>& inputWindowHandle) = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700147
Jeff Brown9c3cda02010-06-15 01:31:58 -0700148 /* Notifies the system that an input channel is unrecoverably broken. */
Jeff Brown928e0542011-01-10 11:17:36 -0800149 virtual void notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700150
Jeff Brownb21fb102010-09-07 10:44:57 -0700151 /* Gets the key repeat initial timeout or -1 if automatic key repeating is disabled. */
Jeff Brown9c3cda02010-06-15 01:31:58 -0700152 virtual nsecs_t getKeyRepeatTimeout() = 0;
153
Jeff Brownb21fb102010-09-07 10:44:57 -0700154 /* Gets the key repeat inter-key delay. */
155 virtual nsecs_t getKeyRepeatDelay() = 0;
156
Jeff Brownae9fc032010-08-18 15:51:08 -0700157 /* Gets the maximum suggested event delivery rate per second.
158 * This value is used to throttle motion event movement actions on a per-device
159 * basis. It is not intended to be a hard limit.
160 */
161 virtual int32_t getMaxEventsPerSecond() = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700162
Jeff Brownb6997262010-10-08 22:31:17 -0700163 /* Intercepts a key event immediately before queueing it.
164 * The policy can use this method as an opportunity to perform power management functions
165 * and early event preprocessing such as updating policy flags.
166 *
167 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
168 * should be dispatched to applications.
169 */
Jeff Brown1f245102010-11-18 20:53:46 -0800170 virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags) = 0;
Jeff Brownb6997262010-10-08 22:31:17 -0700171
Jeff Brown56194eb2011-03-02 19:23:13 -0800172 /* Intercepts a touch, trackball or other motion event before queueing it.
Jeff Brownb6997262010-10-08 22:31:17 -0700173 * The policy can use this method as an opportunity to perform power management functions
174 * and early event preprocessing such as updating policy flags.
175 *
176 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
177 * should be dispatched to applications.
178 */
Jeff Brown56194eb2011-03-02 19:23:13 -0800179 virtual void interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) = 0;
Jeff Brownb6997262010-10-08 22:31:17 -0700180
Jeff Brownb88102f2010-09-08 11:49:43 -0700181 /* Allows the policy a chance to intercept a key before dispatching. */
Jeff Brown928e0542011-01-10 11:17:36 -0800182 virtual bool interceptKeyBeforeDispatching(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brownb88102f2010-09-08 11:49:43 -0700183 const KeyEvent* keyEvent, uint32_t policyFlags) = 0;
184
Jeff Brown49ed71d2010-12-06 17:13:33 -0800185 /* Allows the policy a chance to perform default processing for an unhandled key.
186 * Returns an alternate keycode to redispatch as a fallback, or 0 to give up. */
Jeff Brown928e0542011-01-10 11:17:36 -0800187 virtual bool dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800188 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) = 0;
Jeff Brown3915bb82010-11-05 15:02:16 -0700189
Jeff Brownb6997262010-10-08 22:31:17 -0700190 /* Notifies the policy about switch events.
191 */
192 virtual void notifySwitch(nsecs_t when,
193 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) = 0;
194
Jeff Brownb88102f2010-09-08 11:49:43 -0700195 /* Poke user activity for an event dispatched to a window. */
Jeff Brown01ce2e92010-09-26 22:20:12 -0700196 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType) = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700197
198 /* Checks whether a given application pid/uid has permission to inject input events
199 * into other applications.
200 *
201 * This method is special in that its implementation promises to be non-reentrant and
202 * is safe to call while holding other locks. (Most other methods make no such guarantees!)
203 */
204 virtual bool checkInjectEventsPermissionNonReentrant(
205 int32_t injectorPid, int32_t injectorUid) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700206};
207
208
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700209/* Notifies the system about input events generated by the input reader.
210 * The dispatcher is expected to be mostly asynchronous. */
211class InputDispatcherInterface : public virtual RefBase {
212protected:
213 InputDispatcherInterface() { }
214 virtual ~InputDispatcherInterface() { }
215
216public:
Jeff Brownb88102f2010-09-08 11:49:43 -0700217 /* Dumps the state of the input dispatcher.
218 *
219 * This method may be called on any thread (usually by the input manager). */
220 virtual void dump(String8& dump) = 0;
221
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700222 /* Runs a single iteration of the dispatch loop.
223 * Nominally processes one queued event, a timeout, or a response from an input consumer.
224 *
225 * This method should only be called on the input dispatcher thread.
226 */
227 virtual void dispatchOnce() = 0;
228
229 /* Notifies the dispatcher about new events.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700230 *
231 * These methods should only be called on the input reader thread.
232 */
Jeff Brown9c3cda02010-06-15 01:31:58 -0700233 virtual void notifyConfigurationChanged(nsecs_t eventTime) = 0;
Jeff Brown58a2da82011-01-25 16:02:22 -0800234 virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, uint32_t source,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700235 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode,
236 int32_t scanCode, int32_t metaState, nsecs_t downTime) = 0;
Jeff Brown58a2da82011-01-25 16:02:22 -0800237 virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, uint32_t source,
Jeff Brown85a31762010-09-01 17:01:00 -0700238 uint32_t policyFlags, int32_t action, int32_t flags,
239 int32_t metaState, int32_t edgeFlags,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700240 uint32_t pointerCount, const int32_t* pointerIds, const PointerCoords* pointerCoords,
241 float xPrecision, float yPrecision, nsecs_t downTime) = 0;
Jeff Brownb6997262010-10-08 22:31:17 -0700242 virtual void notifySwitch(nsecs_t when,
243 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700244
Jeff Brown7fbdc842010-06-17 20:52:56 -0700245 /* Injects an input event and optionally waits for sync.
Jeff Brown6ec402b2010-07-28 15:48:59 -0700246 * The synchronization mode determines whether the method blocks while waiting for
247 * input injection to proceed.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700248 * Returns one of the INPUT_EVENT_INJECTION_XXX constants.
249 *
250 * This method may be called on any thread (usually by the input manager).
251 */
252 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brown6ec402b2010-07-28 15:48:59 -0700253 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis) = 0;
Jeff Brown7fbdc842010-06-17 20:52:56 -0700254
Jeff Brownb88102f2010-09-08 11:49:43 -0700255 /* Sets the list of input windows.
256 *
257 * This method may be called on any thread (usually by the input manager).
258 */
259 virtual void setInputWindows(const Vector<InputWindow>& inputWindows) = 0;
260
261 /* Sets the focused application.
262 *
263 * This method may be called on any thread (usually by the input manager).
264 */
265 virtual void setFocusedApplication(const InputApplication* inputApplication) = 0;
266
267 /* Sets the input dispatching mode.
268 *
269 * This method may be called on any thread (usually by the input manager).
270 */
271 virtual void setInputDispatchMode(bool enabled, bool frozen) = 0;
272
Jeff Browne6504122010-09-27 14:52:15 -0700273 /* Transfers touch focus from the window associated with one channel to the
274 * window associated with the other channel.
275 *
276 * Returns true on success. False if the window did not actually have touch focus.
277 */
278 virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel,
279 const sp<InputChannel>& toChannel) = 0;
280
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700281 /* Registers or unregister input channels that may be used as targets for input events.
Jeff Brownb88102f2010-09-08 11:49:43 -0700282 * If monitor is true, the channel will receive a copy of all input events.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700283 *
284 * These methods may be called on any thread (usually by the input manager).
285 */
Jeff Brown928e0542011-01-10 11:17:36 -0800286 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel,
287 const sp<InputWindowHandle>& inputWindowHandle, bool monitor) = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700288 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) = 0;
289};
290
Jeff Brown9c3cda02010-06-15 01:31:58 -0700291/* Dispatches events to input targets. Some functions of the input dispatcher, such as
292 * identifying input targets, are controlled by a separate policy object.
293 *
294 * IMPORTANT INVARIANT:
295 * Because the policy can potentially block or cause re-entrance into the input dispatcher,
296 * the input dispatcher never calls into the policy while holding its internal locks.
297 * The implementation is also carefully designed to recover from scenarios such as an
298 * input channel becoming unregistered while identifying input targets or processing timeouts.
299 *
300 * Methods marked 'Locked' must be called with the lock acquired.
301 *
302 * Methods marked 'LockedInterruptible' must be called with the lock acquired but
303 * may during the course of their execution release the lock, call into the policy, and
304 * then reacquire the lock. The caller is responsible for recovering gracefully.
305 *
306 * A 'LockedInterruptible' method may called a 'Locked' method, but NOT vice-versa.
307 */
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700308class InputDispatcher : public InputDispatcherInterface {
309protected:
310 virtual ~InputDispatcher();
311
312public:
Jeff Brown9c3cda02010-06-15 01:31:58 -0700313 explicit InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700314
Jeff Brownb88102f2010-09-08 11:49:43 -0700315 virtual void dump(String8& dump);
316
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700317 virtual void dispatchOnce();
318
Jeff Brown9c3cda02010-06-15 01:31:58 -0700319 virtual void notifyConfigurationChanged(nsecs_t eventTime);
Jeff Brown58a2da82011-01-25 16:02:22 -0800320 virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, uint32_t source,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700321 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode,
322 int32_t scanCode, int32_t metaState, nsecs_t downTime);
Jeff Brown58a2da82011-01-25 16:02:22 -0800323 virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, uint32_t source,
Jeff Brown85a31762010-09-01 17:01:00 -0700324 uint32_t policyFlags, int32_t action, int32_t flags,
325 int32_t metaState, int32_t edgeFlags,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700326 uint32_t pointerCount, const int32_t* pointerIds, const PointerCoords* pointerCoords,
327 float xPrecision, float yPrecision, nsecs_t downTime);
Jeff Brownb6997262010-10-08 22:31:17 -0700328 virtual void notifySwitch(nsecs_t when,
329 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) ;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700330
Jeff Brown7fbdc842010-06-17 20:52:56 -0700331 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brown6ec402b2010-07-28 15:48:59 -0700332 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700333
Jeff Brownb88102f2010-09-08 11:49:43 -0700334 virtual void setInputWindows(const Vector<InputWindow>& inputWindows);
335 virtual void setFocusedApplication(const InputApplication* inputApplication);
336 virtual void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown349703e2010-06-22 01:27:15 -0700337
Jeff Browne6504122010-09-27 14:52:15 -0700338 virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel,
339 const sp<InputChannel>& toChannel);
340
Jeff Brown928e0542011-01-10 11:17:36 -0800341 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel,
342 const sp<InputWindowHandle>& inputWindowHandle, bool monitor);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700343 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel);
344
345private:
346 template <typename T>
347 struct Link {
348 T* next;
349 T* prev;
350 };
351
Jeff Brown01ce2e92010-09-26 22:20:12 -0700352 struct InjectionState {
353 mutable int32_t refCount;
354
355 int32_t injectorPid;
356 int32_t injectorUid;
357 int32_t injectionResult; // initially INPUT_EVENT_INJECTION_PENDING
358 bool injectionIsAsync; // set to true if injection is not waiting for the result
359 int32_t pendingForegroundDispatches; // the number of foreground dispatches in progress
360 };
361
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700362 struct EventEntry : Link<EventEntry> {
363 enum {
364 TYPE_SENTINEL,
365 TYPE_CONFIGURATION_CHANGED,
366 TYPE_KEY,
367 TYPE_MOTION
368 };
369
Jeff Brown01ce2e92010-09-26 22:20:12 -0700370 mutable int32_t refCount;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700371 int32_t type;
372 nsecs_t eventTime;
Jeff Brownb6997262010-10-08 22:31:17 -0700373 uint32_t policyFlags;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700374 InjectionState* injectionState;
Jeff Brown7fbdc842010-06-17 20:52:56 -0700375
Jeff Brown9c3cda02010-06-15 01:31:58 -0700376 bool dispatchInProgress; // initially false, set to true while dispatching
Jeff Brown7fbdc842010-06-17 20:52:56 -0700377
Jeff Brown5ced76a2011-05-24 11:23:27 -0700378 inline bool isInjected() const { return injectionState != NULL; }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700379 };
380
381 struct ConfigurationChangedEntry : EventEntry {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700382 };
383
384 struct KeyEntry : EventEntry {
385 int32_t deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -0800386 uint32_t source;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700387 int32_t action;
388 int32_t flags;
389 int32_t keyCode;
390 int32_t scanCode;
391 int32_t metaState;
392 int32_t repeatCount;
393 nsecs_t downTime;
Jeff Brownb88102f2010-09-08 11:49:43 -0700394
395 bool syntheticRepeat; // set to true for synthetic key repeats
396
397 enum InterceptKeyResult {
398 INTERCEPT_KEY_RESULT_UNKNOWN,
399 INTERCEPT_KEY_RESULT_SKIP,
400 INTERCEPT_KEY_RESULT_CONTINUE,
401 };
402 InterceptKeyResult interceptKeyResult; // set based on the interception result
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700403 };
404
405 struct MotionSample {
406 MotionSample* next;
407
Jeff Brown5ced76a2011-05-24 11:23:27 -0700408 nsecs_t eventTime; // may be updated during coalescing
409 nsecs_t eventTimeBeforeCoalescing; // not updated during coalescing
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700410 PointerCoords pointerCoords[MAX_POINTERS];
411 };
412
413 struct MotionEntry : EventEntry {
414 int32_t deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -0800415 uint32_t source;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700416 int32_t action;
Jeff Brown85a31762010-09-01 17:01:00 -0700417 int32_t flags;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700418 int32_t metaState;
419 int32_t edgeFlags;
420 float xPrecision;
421 float yPrecision;
422 nsecs_t downTime;
423 uint32_t pointerCount;
424 int32_t pointerIds[MAX_POINTERS];
425
426 // Linked list of motion samples associated with this motion event.
427 MotionSample firstSample;
428 MotionSample* lastSample;
Jeff Brownae9fc032010-08-18 15:51:08 -0700429
430 uint32_t countSamples() const;
Jeff Brown5ced76a2011-05-24 11:23:27 -0700431
432 // Checks whether we can append samples, assuming the device id and source are the same.
433 bool canAppendSamples(int32_t action, uint32_t pointerCount,
434 const int32_t* pointerIds) const;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700435 };
436
Jeff Brown9c3cda02010-06-15 01:31:58 -0700437 // Tracks the progress of dispatching a particular event to a particular connection.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700438 struct DispatchEntry : Link<DispatchEntry> {
439 EventEntry* eventEntry; // the event to dispatch
440 int32_t targetFlags;
441 float xOffset;
442 float yOffset;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400443 float scaleFactor;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700444
445 // True if dispatch has started.
446 bool inProgress;
447
448 // For motion events:
449 // Pointer to the first motion sample to dispatch in this cycle.
450 // Usually NULL to indicate that the list of motion samples begins at
451 // MotionEntry::firstSample. Otherwise, some samples were dispatched in a previous
452 // cycle and this pointer indicates the location of the first remainining sample
453 // to dispatch during the current cycle.
454 MotionSample* headMotionSample;
455 // Pointer to a motion sample to dispatch in the next cycle if the dispatcher was
456 // unable to send all motion samples during this cycle. On the next cycle,
457 // headMotionSample will be initialized to tailMotionSample and tailMotionSample
458 // will be set to NULL.
459 MotionSample* tailMotionSample;
Jeff Brown6ec402b2010-07-28 15:48:59 -0700460
Jeff Brown519e0242010-09-15 15:18:56 -0700461 inline bool hasForegroundTarget() const {
462 return targetFlags & InputTarget::FLAG_FOREGROUND;
Jeff Brownb88102f2010-09-08 11:49:43 -0700463 }
Jeff Brown01ce2e92010-09-26 22:20:12 -0700464
465 inline bool isSplit() const {
466 return targetFlags & InputTarget::FLAG_SPLIT;
467 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700468 };
469
Jeff Brown9c3cda02010-06-15 01:31:58 -0700470 // A command entry captures state and behavior for an action to be performed in the
471 // dispatch loop after the initial processing has taken place. It is essentially
472 // a kind of continuation used to postpone sensitive policy interactions to a point
473 // in the dispatch loop where it is safe to release the lock (generally after finishing
474 // the critical parts of the dispatch cycle).
475 //
476 // The special thing about commands is that they can voluntarily release and reacquire
477 // the dispatcher lock at will. Initially when the command starts running, the
478 // dispatcher lock is held. However, if the command needs to call into the policy to
479 // do some work, it can release the lock, do the work, then reacquire the lock again
480 // before returning.
481 //
482 // This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch
483 // never calls into the policy while holding its lock.
484 //
485 // Commands are implicitly 'LockedInterruptible'.
486 struct CommandEntry;
487 typedef void (InputDispatcher::*Command)(CommandEntry* commandEntry);
488
Jeff Brown7fbdc842010-06-17 20:52:56 -0700489 class Connection;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700490 struct CommandEntry : Link<CommandEntry> {
491 CommandEntry();
492 ~CommandEntry();
493
494 Command command;
495
496 // parameters for the command (usage varies by command)
Jeff Brown7fbdc842010-06-17 20:52:56 -0700497 sp<Connection> connection;
Jeff Brownb88102f2010-09-08 11:49:43 -0700498 nsecs_t eventTime;
499 KeyEntry* keyEntry;
500 sp<InputChannel> inputChannel;
501 sp<InputApplicationHandle> inputApplicationHandle;
Jeff Brown928e0542011-01-10 11:17:36 -0800502 sp<InputWindowHandle> inputWindowHandle;
Jeff Brownb88102f2010-09-08 11:49:43 -0700503 int32_t userActivityEventType;
Jeff Brown3915bb82010-11-05 15:02:16 -0700504 bool handled;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700505 };
506
507 // Generic queue implementation.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700508 template <typename T>
509 struct Queue {
Jeff Brownb88102f2010-09-08 11:49:43 -0700510 T headSentinel;
511 T tailSentinel;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700512
513 inline Queue() {
Jeff Brownb88102f2010-09-08 11:49:43 -0700514 headSentinel.prev = NULL;
515 headSentinel.next = & tailSentinel;
516 tailSentinel.prev = & headSentinel;
517 tailSentinel.next = NULL;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700518 }
519
Jeff Brownb88102f2010-09-08 11:49:43 -0700520 inline bool isEmpty() const {
521 return headSentinel.next == & tailSentinel;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700522 }
523
524 inline void enqueueAtTail(T* entry) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700525 T* last = tailSentinel.prev;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700526 last->next = entry;
527 entry->prev = last;
Jeff Brownb88102f2010-09-08 11:49:43 -0700528 entry->next = & tailSentinel;
529 tailSentinel.prev = entry;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700530 }
531
532 inline void enqueueAtHead(T* entry) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700533 T* first = headSentinel.next;
534 headSentinel.next = entry;
535 entry->prev = & headSentinel;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700536 entry->next = first;
537 first->prev = entry;
538 }
539
540 inline void dequeue(T* entry) {
541 entry->prev->next = entry->next;
542 entry->next->prev = entry->prev;
543 }
544
545 inline T* dequeueAtHead() {
Jeff Brownb88102f2010-09-08 11:49:43 -0700546 T* first = headSentinel.next;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700547 dequeue(first);
548 return first;
549 }
Jeff Brown519e0242010-09-15 15:18:56 -0700550
551 uint32_t count() const;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700552 };
553
554 /* Allocates queue entries and performs reference counting as needed. */
555 class Allocator {
556 public:
557 Allocator();
558
Jeff Brown01ce2e92010-09-26 22:20:12 -0700559 InjectionState* obtainInjectionState(int32_t injectorPid, int32_t injectorUid);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700560 ConfigurationChangedEntry* obtainConfigurationChangedEntry(nsecs_t eventTime);
561 KeyEntry* obtainKeyEntry(nsecs_t eventTime,
Jeff Brown58a2da82011-01-25 16:02:22 -0800562 int32_t deviceId, uint32_t source, uint32_t policyFlags, int32_t action,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700563 int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState,
564 int32_t repeatCount, nsecs_t downTime);
565 MotionEntry* obtainMotionEntry(nsecs_t eventTime,
Jeff Brown58a2da82011-01-25 16:02:22 -0800566 int32_t deviceId, uint32_t source, uint32_t policyFlags, int32_t action,
Jeff Brown85a31762010-09-01 17:01:00 -0700567 int32_t flags, int32_t metaState, int32_t edgeFlags,
568 float xPrecision, float yPrecision,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700569 nsecs_t downTime, uint32_t pointerCount,
570 const int32_t* pointerIds, const PointerCoords* pointerCoords);
Jeff Brownb88102f2010-09-08 11:49:43 -0700571 DispatchEntry* obtainDispatchEntry(EventEntry* eventEntry,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400572 int32_t targetFlags, float xOffset, float yOffset, float scaleFactor);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700573 CommandEntry* obtainCommandEntry(Command command);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700574
Jeff Brown01ce2e92010-09-26 22:20:12 -0700575 void releaseInjectionState(InjectionState* injectionState);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700576 void releaseEventEntry(EventEntry* entry);
577 void releaseConfigurationChangedEntry(ConfigurationChangedEntry* entry);
578 void releaseKeyEntry(KeyEntry* entry);
579 void releaseMotionEntry(MotionEntry* entry);
580 void releaseDispatchEntry(DispatchEntry* entry);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700581 void releaseCommandEntry(CommandEntry* entry);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700582
Jeff Brown01ce2e92010-09-26 22:20:12 -0700583 void recycleKeyEntry(KeyEntry* entry);
584
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700585 void appendMotionSample(MotionEntry* motionEntry,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700586 nsecs_t eventTime, const PointerCoords* pointerCoords);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700587
588 private:
Jeff Brown01ce2e92010-09-26 22:20:12 -0700589 Pool<InjectionState> mInjectionStatePool;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700590 Pool<ConfigurationChangedEntry> mConfigurationChangeEntryPool;
591 Pool<KeyEntry> mKeyEntryPool;
592 Pool<MotionEntry> mMotionEntryPool;
593 Pool<MotionSample> mMotionSamplePool;
594 Pool<DispatchEntry> mDispatchEntryPool;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700595 Pool<CommandEntry> mCommandEntryPool;
Jeff Brown7fbdc842010-06-17 20:52:56 -0700596
Jeff Brownb6997262010-10-08 22:31:17 -0700597 void initializeEventEntry(EventEntry* entry, int32_t type, nsecs_t eventTime,
598 uint32_t policyFlags);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700599 void releaseEventEntryInjectionState(EventEntry* entry);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700600 };
601
Jeff Brown524ee642011-03-29 15:11:34 -0700602 /* Specifies which events are to be canceled and why. */
603 struct CancelationOptions {
604 enum Mode {
Jeff Brownb6997262010-10-08 22:31:17 -0700605 CANCEL_ALL_EVENTS = 0,
606 CANCEL_POINTER_EVENTS = 1,
607 CANCEL_NON_POINTER_EVENTS = 2,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800608 CANCEL_FALLBACK_EVENTS = 3,
Jeff Brownb6997262010-10-08 22:31:17 -0700609 };
610
Jeff Brown524ee642011-03-29 15:11:34 -0700611 // The criterion to use to determine which events should be canceled.
612 Mode mode;
613
614 // Descriptive reason for the cancelation.
615 const char* reason;
616
617 // The specific keycode of the key event to cancel, or -1 to cancel any key event.
618 int32_t keyCode;
619
620 CancelationOptions(Mode mode, const char* reason) :
621 mode(mode), reason(reason), keyCode(-1) { }
622 };
623
624 /* Tracks dispatched key and motion event state so that cancelation events can be
625 * synthesized when events are dropped. */
626 class InputState {
627 public:
Jeff Brownb88102f2010-09-08 11:49:43 -0700628 InputState();
629 ~InputState();
630
631 // Returns true if there is no state to be canceled.
632 bool isNeutral() const;
633
Jeff Brownb88102f2010-09-08 11:49:43 -0700634 // Records tracking information for an event that has just been published.
Jeff Browncc0c1592011-02-19 05:07:28 -0800635 void trackEvent(const EventEntry* entry);
Jeff Brownb88102f2010-09-08 11:49:43 -0700636
637 // Records tracking information for a key event that has just been published.
Jeff Browncc0c1592011-02-19 05:07:28 -0800638 void trackKey(const KeyEntry* entry);
Jeff Brownb88102f2010-09-08 11:49:43 -0700639
640 // Records tracking information for a motion event that has just been published.
Jeff Browncc0c1592011-02-19 05:07:28 -0800641 void trackMotion(const MotionEntry* entry);
Jeff Brownb88102f2010-09-08 11:49:43 -0700642
Jeff Brownb6997262010-10-08 22:31:17 -0700643 // Synthesizes cancelation events for the current state and resets the tracked state.
644 void synthesizeCancelationEvents(nsecs_t currentTime, Allocator* allocator,
Jeff Brown524ee642011-03-29 15:11:34 -0700645 Vector<EventEntry*>& outEvents, const CancelationOptions& options);
Jeff Brownb88102f2010-09-08 11:49:43 -0700646
647 // Clears the current state.
648 void clear();
649
Jeff Brown9c9f1a32010-10-11 18:32:20 -0700650 // Copies pointer-related parts of the input state to another instance.
651 void copyPointerStateTo(InputState& other) const;
652
Jeff Brown524ee642011-03-29 15:11:34 -0700653 // Gets the fallback key associated with a keycode.
654 // Returns -1 if none.
655 // Returns AKEYCODE_UNKNOWN if we are only dispatching the unhandled key to the policy.
656 int32_t getFallbackKey(int32_t originalKeyCode);
657
658 // Sets the fallback key for a particular keycode.
659 void setFallbackKey(int32_t originalKeyCode, int32_t fallbackKeyCode);
660
661 // Removes the fallback key for a particular keycode.
662 void removeFallbackKey(int32_t originalKeyCode);
663
664 inline const KeyedVector<int32_t, int32_t>& getFallbackKeys() const {
665 return mFallbackKeys;
666 }
667
Jeff Brownb88102f2010-09-08 11:49:43 -0700668 private:
Jeff Brownb88102f2010-09-08 11:49:43 -0700669 struct KeyMemento {
670 int32_t deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -0800671 uint32_t source;
Jeff Brownb88102f2010-09-08 11:49:43 -0700672 int32_t keyCode;
673 int32_t scanCode;
Jeff Brown49ed71d2010-12-06 17:13:33 -0800674 int32_t flags;
Jeff Brownb88102f2010-09-08 11:49:43 -0700675 nsecs_t downTime;
676 };
677
678 struct MotionMemento {
679 int32_t deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -0800680 uint32_t source;
Jeff Brownb88102f2010-09-08 11:49:43 -0700681 float xPrecision;
682 float yPrecision;
683 nsecs_t downTime;
684 uint32_t pointerCount;
685 int32_t pointerIds[MAX_POINTERS];
686 PointerCoords pointerCoords[MAX_POINTERS];
687
688 void setPointers(const MotionEntry* entry);
689 };
690
691 Vector<KeyMemento> mKeyMementos;
692 Vector<MotionMemento> mMotionMementos;
Jeff Brown524ee642011-03-29 15:11:34 -0700693 KeyedVector<int32_t, int32_t> mFallbackKeys;
Jeff Brownb6997262010-10-08 22:31:17 -0700694
Jeff Brown49ed71d2010-12-06 17:13:33 -0800695 static bool shouldCancelKey(const KeyMemento& memento,
Jeff Brown524ee642011-03-29 15:11:34 -0700696 const CancelationOptions& options);
Jeff Brown49ed71d2010-12-06 17:13:33 -0800697 static bool shouldCancelMotion(const MotionMemento& memento,
Jeff Brown524ee642011-03-29 15:11:34 -0700698 const CancelationOptions& options);
Jeff Brownb88102f2010-09-08 11:49:43 -0700699 };
700
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700701 /* Manages the dispatch state associated with a single input channel. */
702 class Connection : public RefBase {
703 protected:
704 virtual ~Connection();
705
706 public:
707 enum Status {
708 // Everything is peachy.
709 STATUS_NORMAL,
710 // An unrecoverable communication error has occurred.
711 STATUS_BROKEN,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700712 // The input channel has been unregistered.
713 STATUS_ZOMBIE
714 };
715
716 Status status;
Jeff Brown928e0542011-01-10 11:17:36 -0800717 sp<InputChannel> inputChannel; // never null
718 sp<InputWindowHandle> inputWindowHandle; // may be null
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700719 InputPublisher inputPublisher;
Jeff Brownb88102f2010-09-08 11:49:43 -0700720 InputState inputState;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700721 Queue<DispatchEntry> outboundQueue;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700722
723 nsecs_t lastEventTime; // the time when the event was originally captured
724 nsecs_t lastDispatchTime; // the time when the last event was dispatched
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700725
Jeff Brown928e0542011-01-10 11:17:36 -0800726 explicit Connection(const sp<InputChannel>& inputChannel,
727 const sp<InputWindowHandle>& inputWindowHandle);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700728
Jeff Brown9c3cda02010-06-15 01:31:58 -0700729 inline const char* getInputChannelName() const { return inputChannel->getName().string(); }
730
731 const char* getStatusLabel() const;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700732
733 // Finds a DispatchEntry in the outbound queue associated with the specified event.
734 // Returns NULL if not found.
735 DispatchEntry* findQueuedDispatchEntryForEvent(const EventEntry* eventEntry) const;
736
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700737 // Gets the time since the current event was originally obtained from the input driver.
Jeff Brownb88102f2010-09-08 11:49:43 -0700738 inline double getEventLatencyMillis(nsecs_t currentTime) const {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700739 return (currentTime - lastEventTime) / 1000000.0;
740 }
741
742 // Gets the time since the current event entered the outbound dispatch queue.
Jeff Brownb88102f2010-09-08 11:49:43 -0700743 inline double getDispatchLatencyMillis(nsecs_t currentTime) const {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700744 return (currentTime - lastDispatchTime) / 1000000.0;
745 }
746
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700747 status_t initialize();
748 };
749
Jeff Brownb6997262010-10-08 22:31:17 -0700750 enum DropReason {
751 DROP_REASON_NOT_DROPPED = 0,
752 DROP_REASON_POLICY = 1,
753 DROP_REASON_APP_SWITCH = 2,
754 DROP_REASON_DISABLED = 3,
Jeff Brown928e0542011-01-10 11:17:36 -0800755 DROP_REASON_BLOCKED = 4,
756 DROP_REASON_STALE = 5,
Jeff Brownb6997262010-10-08 22:31:17 -0700757 };
758
Jeff Brown9c3cda02010-06-15 01:31:58 -0700759 sp<InputDispatcherPolicyInterface> mPolicy;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700760
761 Mutex mLock;
762
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700763 Allocator mAllocator;
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700764 sp<Looper> mLooper;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700765
Jeff Brownb88102f2010-09-08 11:49:43 -0700766 EventEntry* mPendingEvent;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700767 Queue<EventEntry> mInboundQueue;
768 Queue<CommandEntry> mCommandQueue;
769
Jeff Brownb88102f2010-09-08 11:49:43 -0700770 Vector<EventEntry*> mTempCancelationEvents;
771
772 void dispatchOnceInnerLocked(nsecs_t keyRepeatTimeout, nsecs_t keyRepeatDelay,
773 nsecs_t* nextWakeupTime);
774
Jeff Brown5ced76a2011-05-24 11:23:27 -0700775 // Batches a new sample onto a motion entry.
776 // Assumes that the we have already checked that we can append samples.
777 void batchMotionLocked(MotionEntry* entry, nsecs_t eventTime, int32_t metaState,
778 const PointerCoords* pointerCoords, const char* eventDescription);
779
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700780 // Enqueues an inbound event. Returns true if mLooper->wake() should be called.
Jeff Brownb88102f2010-09-08 11:49:43 -0700781 bool enqueueInboundEventLocked(EventEntry* entry);
782
Jeff Brownb6997262010-10-08 22:31:17 -0700783 // Cleans up input state when dropping an inbound event.
784 void dropInboundEventLocked(EventEntry* entry, DropReason dropReason);
785
Jeff Brownb88102f2010-09-08 11:49:43 -0700786 // App switch latency optimization.
Jeff Brownb6997262010-10-08 22:31:17 -0700787 bool mAppSwitchSawKeyDown;
Jeff Brownb88102f2010-09-08 11:49:43 -0700788 nsecs_t mAppSwitchDueTime;
789
Jeff Brownb6997262010-10-08 22:31:17 -0700790 static bool isAppSwitchKeyCode(int32_t keyCode);
791 bool isAppSwitchKeyEventLocked(KeyEntry* keyEntry);
Jeff Brownb88102f2010-09-08 11:49:43 -0700792 bool isAppSwitchPendingLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700793 void resetPendingAppSwitchLocked(bool handled);
794
Jeff Brown928e0542011-01-10 11:17:36 -0800795 // Stale event latency optimization.
796 static bool isStaleEventLocked(nsecs_t currentTime, EventEntry* entry);
797
798 // Blocked event latency optimization. Drops old events when the user intends
799 // to transfer focus to a new application.
800 EventEntry* mNextUnblockedEvent;
801
802 const InputWindow* findTouchedWindowAtLocked(int32_t x, int32_t y);
803
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700804 // All registered connections mapped by receive pipe file descriptor.
805 KeyedVector<int, sp<Connection> > mConnectionsByReceiveFd;
806
Jeff Brown519e0242010-09-15 15:18:56 -0700807 ssize_t getConnectionIndexLocked(const sp<InputChannel>& inputChannel);
Jeff Brown2cbecea2010-08-17 15:59:26 -0700808
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700809 // Active connections are connections that have a non-empty outbound queue.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700810 // We don't use a ref-counted pointer here because we explicitly abort connections
811 // during unregistration which causes the connection's outbound queue to be cleared
812 // and the connection itself to be deactivated.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700813 Vector<Connection*> mActiveConnections;
814
Jeff Brownb88102f2010-09-08 11:49:43 -0700815 // Input channels that will receive a copy of all input events.
816 Vector<sp<InputChannel> > mMonitoringChannels;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700817
Jeff Brown7fbdc842010-06-17 20:52:56 -0700818 // Event injection and synchronization.
819 Condition mInjectionResultAvailableCondition;
Jeff Brownb6997262010-10-08 22:31:17 -0700820 bool hasInjectionPermission(int32_t injectorPid, int32_t injectorUid);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700821 void setInjectionResultLocked(EventEntry* entry, int32_t injectionResult);
822
Jeff Brown6ec402b2010-07-28 15:48:59 -0700823 Condition mInjectionSyncFinishedCondition;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700824 void incrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brown519e0242010-09-15 15:18:56 -0700825 void decrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brown6ec402b2010-07-28 15:48:59 -0700826
Jeff Brownae9fc032010-08-18 15:51:08 -0700827 // Throttling state.
828 struct ThrottleState {
829 nsecs_t minTimeBetweenEvents;
830
831 nsecs_t lastEventTime;
832 int32_t lastDeviceId;
833 uint32_t lastSource;
834
835 uint32_t originalSampleCount; // only collected during debugging
836 } mThrottleState;
837
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700838 // Key repeat tracking.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700839 struct KeyRepeatState {
840 KeyEntry* lastKeyEntry; // or null if no repeat
841 nsecs_t nextRepeatTime;
842 } mKeyRepeatState;
843
844 void resetKeyRepeatLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700845 KeyEntry* synthesizeKeyRepeatLocked(nsecs_t currentTime, nsecs_t keyRepeatTimeout);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700846
Jeff Brown9c3cda02010-06-15 01:31:58 -0700847 // Deferred command processing.
848 bool runCommandsLockedInterruptible();
849 CommandEntry* postCommandLocked(Command command);
850
Jeff Brownb88102f2010-09-08 11:49:43 -0700851 // Inbound event processing.
852 void drainInboundQueueLocked();
Jeff Brown54a18252010-09-16 14:07:33 -0700853 void releasePendingEventLocked();
854 void releaseInboundEventLocked(EventEntry* entry);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700855
Jeff Brownb88102f2010-09-08 11:49:43 -0700856 // Dispatch state.
857 bool mDispatchEnabled;
858 bool mDispatchFrozen;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700859
Jeff Brownb88102f2010-09-08 11:49:43 -0700860 Vector<InputWindow> mWindows;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700861
862 const InputWindow* getWindowLocked(const sp<InputChannel>& inputChannel);
Jeff Brownb88102f2010-09-08 11:49:43 -0700863
864 // Focus tracking for keys, trackball, etc.
Jeff Brown01ce2e92010-09-26 22:20:12 -0700865 const InputWindow* mFocusedWindow;
Jeff Brownb88102f2010-09-08 11:49:43 -0700866
867 // Focus tracking for touch.
Jeff Brown01ce2e92010-09-26 22:20:12 -0700868 struct TouchedWindow {
869 const InputWindow* window;
870 int32_t targetFlags;
Jeff Brown46e75292010-11-10 16:53:45 -0800871 BitSet32 pointerIds; // zero unless target flag FLAG_SPLIT is set
Jeff Brown01ce2e92010-09-26 22:20:12 -0700872 sp<InputChannel> channel;
Jeff Brownb88102f2010-09-08 11:49:43 -0700873 };
Jeff Brown01ce2e92010-09-26 22:20:12 -0700874 struct TouchState {
875 bool down;
876 bool split;
Jeff Brown95712852011-01-04 19:41:59 -0800877 int32_t deviceId; // id of the device that is currently down, others are rejected
Jeff Brown58a2da82011-01-25 16:02:22 -0800878 uint32_t source; // source of the device that is current down, others are rejected
Jeff Brown01ce2e92010-09-26 22:20:12 -0700879 Vector<TouchedWindow> windows;
880
881 TouchState();
882 ~TouchState();
883 void reset();
884 void copyFrom(const TouchState& other);
885 void addOrUpdateWindow(const InputWindow* window, int32_t targetFlags, BitSet32 pointerIds);
886 void removeOutsideTouchWindows();
887 const InputWindow* getFirstForegroundWindow();
888 };
889
890 TouchState mTouchState;
891 TouchState mTempTouchState;
Jeff Brownb88102f2010-09-08 11:49:43 -0700892
893 // Focused application.
894 InputApplication* mFocusedApplication;
895 InputApplication mFocusedApplicationStorage; // preallocated storage for mFocusedApplication
896 void releaseFocusedApplicationLocked();
897
898 // Dispatch inbound events.
899 bool dispatchConfigurationChangedLocked(
900 nsecs_t currentTime, ConfigurationChangedEntry* entry);
901 bool dispatchKeyLocked(
902 nsecs_t currentTime, KeyEntry* entry, nsecs_t keyRepeatTimeout,
Jeff Browne20c9e02010-10-11 14:20:19 -0700903 DropReason* dropReason, nsecs_t* nextWakeupTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700904 bool dispatchMotionLocked(
905 nsecs_t currentTime, MotionEntry* entry,
Jeff Browne20c9e02010-10-11 14:20:19 -0700906 DropReason* dropReason, nsecs_t* nextWakeupTime);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700907 void dispatchEventToCurrentInputTargetsLocked(
908 nsecs_t currentTime, EventEntry* entry, bool resumeWithAppendedMotionSample);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700909
Jeff Brownb88102f2010-09-08 11:49:43 -0700910 void logOutboundKeyDetailsLocked(const char* prefix, const KeyEntry* entry);
911 void logOutboundMotionDetailsLocked(const char* prefix, const MotionEntry* entry);
912
913 // The input targets that were most recently identified for dispatch.
Jeff Brownb88102f2010-09-08 11:49:43 -0700914 bool mCurrentInputTargetsValid; // false while targets are being recomputed
915 Vector<InputTarget> mCurrentInputTargets;
Jeff Brownb88102f2010-09-08 11:49:43 -0700916
917 enum InputTargetWaitCause {
918 INPUT_TARGET_WAIT_CAUSE_NONE,
919 INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY,
920 INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY,
921 };
922
923 InputTargetWaitCause mInputTargetWaitCause;
924 nsecs_t mInputTargetWaitStartTime;
925 nsecs_t mInputTargetWaitTimeoutTime;
926 bool mInputTargetWaitTimeoutExpired;
Jeff Brown928e0542011-01-10 11:17:36 -0800927 sp<InputApplicationHandle> mInputTargetWaitApplication;
Jeff Brownb88102f2010-09-08 11:49:43 -0700928
929 // Finding targets for input events.
Jeff Brown54a18252010-09-16 14:07:33 -0700930 void resetTargetsLocked();
Jeff Brown01ce2e92010-09-26 22:20:12 -0700931 void commitTargetsLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700932 int32_t handleTargetsNotReadyLocked(nsecs_t currentTime, const EventEntry* entry,
933 const InputApplication* application, const InputWindow* window,
934 nsecs_t* nextWakeupTime);
Jeff Brown519e0242010-09-15 15:18:56 -0700935 void resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout,
936 const sp<InputChannel>& inputChannel);
937 nsecs_t getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700938 void resetANRTimeoutsLocked();
939
Jeff Brown01ce2e92010-09-26 22:20:12 -0700940 int32_t findFocusedWindowTargetsLocked(nsecs_t currentTime, const EventEntry* entry,
941 nsecs_t* nextWakeupTime);
942 int32_t findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry* entry,
Jeff Browncc0c1592011-02-19 05:07:28 -0800943 nsecs_t* nextWakeupTime, bool* outConflictingPointerActions);
Jeff Brownb88102f2010-09-08 11:49:43 -0700944
Jeff Brown01ce2e92010-09-26 22:20:12 -0700945 void addWindowTargetLocked(const InputWindow* window, int32_t targetFlags,
946 BitSet32 pointerIds);
Jeff Brownb88102f2010-09-08 11:49:43 -0700947 void addMonitoringTargetsLocked();
Jeff Browne2fe69e2010-10-18 13:21:23 -0700948 void pokeUserActivityLocked(const EventEntry* eventEntry);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700949 bool checkInjectionPermission(const InputWindow* window, const InjectionState* injectionState);
Jeff Brown19dfc832010-10-05 12:26:23 -0700950 bool isWindowObscuredAtPointLocked(const InputWindow* window, int32_t x, int32_t y) const;
Jeff Brown519e0242010-09-15 15:18:56 -0700951 bool isWindowFinishedWithPreviousInputLocked(const InputWindow* window);
Jeff Brown519e0242010-09-15 15:18:56 -0700952 String8 getApplicationWindowLabelLocked(const InputApplication* application,
953 const InputWindow* window);
Jeff Brownb88102f2010-09-08 11:49:43 -0700954
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700955 // Manage the dispatch cycle for a single connection.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700956 // These methods are deliberately not Interruptible because doing all of the work
957 // with the mutex held makes it easier to ensure that connection invariants are maintained.
958 // If needed, the methods post commands to run later once the critical bits are done.
959 void prepareDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700960 EventEntry* eventEntry, const InputTarget* inputTarget,
961 bool resumeWithAppendedMotionSample);
Jeff Brown519e0242010-09-15 15:18:56 -0700962 void startDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown3915bb82010-11-05 15:02:16 -0700963 void finishDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
964 bool handled);
Jeff Brownb88102f2010-09-08 11:49:43 -0700965 void startNextDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brownb6997262010-10-08 22:31:17 -0700966 void abortBrokenDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown519e0242010-09-15 15:18:56 -0700967 void drainOutboundQueueLocked(Connection* connection);
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700968 static int handleReceiveCallback(int receiveFd, int events, void* data);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700969
Jeff Brownb6997262010-10-08 22:31:17 -0700970 void synthesizeCancelationEventsForAllConnectionsLocked(
Jeff Brown524ee642011-03-29 15:11:34 -0700971 const CancelationOptions& options);
Jeff Brownb6997262010-10-08 22:31:17 -0700972 void synthesizeCancelationEventsForInputChannelLocked(const sp<InputChannel>& channel,
Jeff Brown524ee642011-03-29 15:11:34 -0700973 const CancelationOptions& options);
Jeff Brownb6997262010-10-08 22:31:17 -0700974 void synthesizeCancelationEventsForConnectionLocked(const sp<Connection>& connection,
Jeff Brown524ee642011-03-29 15:11:34 -0700975 const CancelationOptions& options);
Jeff Brownb6997262010-10-08 22:31:17 -0700976
Jeff Brown01ce2e92010-09-26 22:20:12 -0700977 // Splitting motion events across windows.
978 MotionEntry* splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds);
979
Jeff Brown120a4592010-10-27 18:43:51 -0700980 // Reset and drop everything the dispatcher is doing.
981 void resetAndDropEverythingLocked(const char* reason);
982
Jeff Brownb88102f2010-09-08 11:49:43 -0700983 // Dump state.
984 void dumpDispatchStateLocked(String8& dump);
985 void logDispatchStateLocked();
986
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700987 // Add or remove a connection to the mActiveConnections vector.
988 void activateConnectionLocked(Connection* connection);
989 void deactivateConnectionLocked(Connection* connection);
990
991 // Interesting events that we might like to log or tell the framework about.
Jeff Brown9c3cda02010-06-15 01:31:58 -0700992 void onDispatchCycleStartedLocked(
Jeff Brown7fbdc842010-06-17 20:52:56 -0700993 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700994 void onDispatchCycleFinishedLocked(
Jeff Brown3915bb82010-11-05 15:02:16 -0700995 nsecs_t currentTime, const sp<Connection>& connection, bool handled);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700996 void onDispatchCycleBrokenLocked(
Jeff Brown7fbdc842010-06-17 20:52:56 -0700997 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown519e0242010-09-15 15:18:56 -0700998 void onANRLocked(
999 nsecs_t currentTime, const InputApplication* application, const InputWindow* window,
1000 nsecs_t eventTime, nsecs_t waitStartTime);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001001
Jeff Brown7fbdc842010-06-17 20:52:56 -07001002 // Outbound policy interactions.
Jeff Brownb88102f2010-09-08 11:49:43 -07001003 void doNotifyConfigurationChangedInterruptible(CommandEntry* commandEntry);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001004 void doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown519e0242010-09-15 15:18:56 -07001005 void doNotifyANRLockedInterruptible(CommandEntry* commandEntry);
Jeff Brownb88102f2010-09-08 11:49:43 -07001006 void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown3915bb82010-11-05 15:02:16 -07001007 void doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry);
Jeff Brownb88102f2010-09-08 11:49:43 -07001008 void doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown3915bb82010-11-05 15:02:16 -07001009 void initializeKeyEvent(KeyEvent* event, const KeyEntry* entry);
Jeff Brown519e0242010-09-15 15:18:56 -07001010
1011 // Statistics gathering.
1012 void updateDispatchStatisticsLocked(nsecs_t currentTime, const EventEntry* entry,
1013 int32_t injectionResult, nsecs_t timeSpentWaitingForApplication);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001014};
1015
1016/* Enqueues and dispatches input events, endlessly. */
1017class InputDispatcherThread : public Thread {
1018public:
1019 explicit InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher);
1020 ~InputDispatcherThread();
1021
1022private:
1023 virtual bool threadLoop();
1024
1025 sp<InputDispatcherInterface> mDispatcher;
1026};
1027
1028} // namespace android
1029
Jeff Brownb88102f2010-09-08 11:49:43 -07001030#endif // _UI_INPUT_DISPATCHER_H